diff --git a/dev/api.go b/dev/api.go index 8047c34..ff5ae02 100644 --- a/dev/api.go +++ b/dev/api.go @@ -1,12 +1,13 @@ package dev import ( + "bytes" "context" "crypto/sha1" "encoding/json" "fmt" "math/rand" - "os" + "regexp" "strconv" "strings" "time" @@ -14,14 +15,16 @@ import ( "github.com/gofrs/uuid" "git.defalsify.org/vise.git/logging" "git.defalsify.org/vise.git/db" - fsdb "git.defalsify.org/vise.git/db/fs" "git.grassecon.net/grassrootseconomics/sarafu-api/models" "git.grassecon.net/grassrootseconomics/sarafu-api/event" + "git.grassecon.net/grassrootseconomics/common/phone" + "git.grassecon.net/grassrootseconomics/visedriver/storage" dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api" ) var ( logg = logging.NewVanilla().WithDomain("sarafu-api.devapi") + aliasRegex = regexp.MustCompile("^\\+?[a-zA-Z0-9\\-_]+$") ) const ( @@ -87,7 +90,6 @@ type Voucher struct { } type DevAccountService struct { - dir string db db.Db accounts map[string]Account accountsTrack map[string]string @@ -101,13 +103,12 @@ type DevAccountService struct { autoVoucherValue map[string]int defaultAccount string emitterFunc event.EmitterFunc + pfx []byte // accountsSession map[string]string } -func NewDevAccountService(ctx context.Context, d string) *DevAccountService { +func NewDevAccountService(ctx context.Context, ss storage.StorageService) *DevAccountService { svc := &DevAccountService{ - dir: d, - db: fsdb.NewFsDb(), accounts: make(map[string]Account), accountsTrack: make(map[string]string), accountsAlias: make(map[string]string), @@ -117,20 +118,25 @@ func NewDevAccountService(ctx context.Context, d string) *DevAccountService { txsTrack: make(map[string]string), autoVoucherValue: make(map[string]int), defaultAccount: zeroAddress, + pfx: []byte("__"), + } + if ss != nil { + var err error + svc.db, err = ss.GetUserdataDb(ctx) + if err != nil { + panic(err) + } + svc.db.SetSession("") + svc.db.SetPrefix(db.DATATYPE_USERDATA) + err = svc.loadAll(ctx) + if err != nil { + logg.DebugCtxf(ctx, "loadall error", "err", err) + } } acc := Account{ Address: zeroAddress, } svc.accounts[acc.Address] = acc - err := svc.db.Connect(ctx, d) - if err != nil { - panic(err) - } - svc.db.SetPrefix(db.DATATYPE_USERDATA) - err = svc.loadAll(ctx) - if err != nil { - panic(err) - } return svc } @@ -139,6 +145,15 @@ func (das *DevAccountService) WithEmitter(fn event.EmitterFunc) *DevAccountServi return das } +func (das *DevAccountService) WithPrefix(pfx []byte) *DevAccountService { + das.pfx = pfx + return das +} + +func (das *DevAccountService) prefixKeyFor(k string, v string) []byte { + return append(das.pfx, []byte(k + "_" + v)...) +} + func (das *DevAccountService) loadAccount(ctx context.Context, pubKey string, v []byte) error { var acc Account @@ -171,7 +186,7 @@ func (das *DevAccountService) loadTx(ctx context.Context, hsh string, v []byte) func (das *DevAccountService) loadItem(ctx context.Context, k []byte, v []byte) error { var err error s := string(k) - ss := strings.SplitN(s, "_", 2) + ss := strings.SplitN(s[2:], "_", 2) if len(ss) != 2 { return fmt.Errorf("malformed key: %s", s) } @@ -179,6 +194,8 @@ func (das *DevAccountService) loadItem(ctx context.Context, k []byte, v []byte) err = das.loadAccount(ctx, ss[1], v) } else if ss[0] == "tx" { err = das.loadTx(ctx, ss[1], v) + } else { + logg.ErrorCtxf(ctx, "unknown double underscore key", "key", ss[0]) } return err } @@ -186,18 +203,19 @@ func (das *DevAccountService) loadItem(ctx context.Context, k []byte, v []byte) // TODO: Add connect tx and account // TODO: update balance func (das *DevAccountService) loadAll(ctx context.Context) error { - d, err := os.ReadDir(das.dir) + dumper, err := das.db.Dump(ctx, []byte{}) if err != nil { return err } - for _, v := range(d) { - // TODO: move decoding to vise - fp := v.Name() - k := []byte(fp[1:]) - v, err := das.db.Get(ctx, k) - if err != nil { - return err + for true { + k, v := dumper.Next(ctx) + if k == nil { + break } + if !bytes.HasPrefix(k, das.pfx) { + continue + } + err = das.loadItem(ctx, k, v) if err != nil { return err @@ -295,11 +313,16 @@ func (das *DevAccountService) balanceAuto(ctx context.Context, pubKey string) er } func (das *DevAccountService) saveAccount(ctx context.Context, acc Account) error { - k := "account_" + acc.Address + if das.db == nil { + return nil + } + k := das.prefixKeyFor("account", acc.Address) v, err := json.Marshal(acc) if err != nil { return err } + das.db.SetSession("") + das.db.SetPrefix(db.DATATYPE_USERDATA) return das.db.Put(ctx, []byte(k), v) } @@ -438,11 +461,13 @@ func (das *DevAccountService) VoucherData(ctx context.Context, address string) ( } func (das *DevAccountService) saveTokenTransfer(ctx context.Context, mytx Tx) error { - k := "tx_" + mytx.Hsh + k := das.prefixKeyFor("tx", mytx.Hsh) v, err := json.Marshal(mytx) if err != nil { return err } + das.db.SetSession("") + das.db.SetPrefix(db.DATATYPE_USERDATA) return das.db.Put(ctx, []byte(k), v) } @@ -516,7 +541,7 @@ func (das *DevAccountService) TokenTransfer(ctx context.Context, amount, from, t }, nil } -func (das *DevAccountService) CheckAliasAddress(ctx context.Context, alias string) (*dataserviceapi.AliasAddress, error) { +func (das *DevAccountService) CheckAliasAddress(ctx context.Context, alias string) (*models.AliasAddress, error) { addr, ok := das.accountsAlias[alias] if !ok { return nil, fmt.Errorf("alias %s not found", alias) @@ -525,7 +550,52 @@ func (das *DevAccountService) CheckAliasAddress(ctx context.Context, alias strin if !ok { return nil, fmt.Errorf("alias %s found but does not resolve", alias) } - return &dataserviceapi.AliasAddress{ + return &models.AliasAddress{ Address: acc.Address, }, nil } + +func (das *DevAccountService) applyPhoneAlias(ctx context.Context, publicKey string, phoneNumber string) (bool, error) { + if phoneNumber[0] == '+' { + if !phone.IsValidPhoneNumber(phoneNumber) { + return false, fmt.Errorf("Invalid phoneNumber number: %v", phoneNumber) + } + logg.DebugCtxf(ctx, "matched phoneNumber alias", "phoneNumber", phoneNumber, "address", publicKey) + return true, nil + } + return false, nil +} + +func (das *DevAccountService) RequestAlias(ctx context.Context, publicKey string, hint string) (*models.RequestAliasResult, error) { + var alias string + if !aliasRegex.MatchString(hint) { + return nil, fmt.Errorf("alias hint does not match: %s", publicKey) + } + acc, ok := das.accounts[publicKey] + if !ok { + return nil, fmt.Errorf("address %s not found", publicKey) + } + alias = hint + isPhone, err := das.applyPhoneAlias(ctx, publicKey, alias) + if err != nil { + return nil, fmt.Errorf("phone parser error: %v", err) + } + if !isPhone { + for true { + addr, ok := das.accountsAlias[alias] + if !ok { + break + } + if addr == publicKey { + break + } + alias += "x" + } + acc.Alias = alias + das.accountsAlias[alias] = publicKey + } + logg.DebugCtxf(ctx, "set alias", "addr", publicKey, "alias", alias) + return &models.RequestAliasResult{ + Alias: alias, + }, nil +} diff --git a/dev/api_test.go b/dev/api_test.go new file mode 100644 index 0000000..2382bf0 --- /dev/null +++ b/dev/api_test.go @@ -0,0 +1,75 @@ +package dev + +import ( + "context" + "testing" + + "git.grassecon.net/grassrootseconomics/visedriver/testutil/mocks" +) + +func TestApiRequestAlias(t *testing.T) { + ctx := context.Background() + storageService := mocks.NewMemStorageService(ctx) + svc := NewDevAccountService(ctx, storageService) + ra, err := svc.CreateAccount(ctx) + if err != nil { + t.Fatal(err) + } + addr := ra.PublicKey + + _, err = svc.RequestAlias(ctx, addr, "+254f00") + if err == nil { + t.Fatalf("expected error") + } + alias := "+254712345678" + rb, err := svc.RequestAlias(ctx, addr, alias) + if err != nil { + t.Fatal(err) + } + if rb.Alias != alias { + t.Fatalf("expected '%s', got '%s'", alias, rb.Alias) + } + _, err = svc.CheckAliasAddress(ctx, alias) + if err == nil { + t.Fatalf("expected error") + } + + alias = "foo" + rb, err = svc.RequestAlias(ctx, addr, alias) + if err != nil { + t.Fatal(err) + } + if rb.Alias != alias { + t.Fatalf("expected '%s', got '%s'", alias, rb.Alias) + } + rc, err := svc.CheckAliasAddress(ctx, alias) + if err != nil { + t.Fatal(err) + } + if rc.Address != addr { + t.Fatalf("expected '%s', got '%s'", addr, rc.Address) + } + + // create a second account + ra, err = svc.CreateAccount(ctx) + if err != nil { + t.Fatal(err) + } + addr = ra.PublicKey + + rb, err = svc.RequestAlias(ctx, addr, alias) + if err != nil { + t.Fatal(err) + } + alias = "foox" + if rb.Alias != alias { + t.Fatalf("expected '%s', got '%s'", alias, rb.Alias) + } + rc, err = svc.CheckAliasAddress(ctx, alias) + if err != nil { + t.Fatal(err) + } + if rc.Address != addr { + t.Fatalf("expected '%s', got '%s'", addr, rc.Address) + } +} diff --git a/go.mod b/go.mod index e132cbe..8299445 100644 --- a/go.mod +++ b/go.mod @@ -3,8 +3,9 @@ module git.grassecon.net/grassrootseconomics/sarafu-api go 1.23.4 require ( - git.defalsify.org/vise.git v0.2.3-0.20250103172917-3e190a44568d - git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250113213325-5228aef0889b + git.defalsify.org/vise.git v0.2.3-0.20250114225117-3b5fc85b650b + git.grassecon.net/grassrootseconomics/common v0.0.0-20250113174703-6afccefd1f05 + git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250115003256-c0534ede1b63 github.com/gofrs/uuid v4.4.0+incompatible github.com/grassrootseconomics/eth-custodial v1.3.0-beta github.com/grassrootseconomics/ussd-data-service v1.2.0-beta @@ -14,8 +15,22 @@ require ( require ( github.com/barbashov/iso639-3 v0.0.0-20211020172741-1f4ffb2d8d1c // indirect github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect + github.com/fxamacker/cbor/v2 v2.4.0 // indirect + github.com/graygnuorg/go-gdbm v0.0.0-20220711140707-71387d66dce4 // indirect + github.com/jackc/pgpassfile v1.0.0 // indirect + github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect + github.com/jackc/pgx/v5 v5.7.1 // indirect + github.com/jackc/puddle/v2 v2.2.2 // indirect github.com/joho/godotenv v1.5.1 // indirect + github.com/kr/text v0.2.0 // indirect + github.com/mattn/kinako v0.0.0-20170717041458-332c0a7e205a // indirect github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect + github.com/rogpeppe/go-internal v1.13.1 // indirect github.com/stretchr/objx v0.5.2 // indirect + github.com/x448/float16 v0.8.4 // indirect + golang.org/x/crypto v0.32.0 // indirect + golang.org/x/sync v0.10.0 // indirect + golang.org/x/text v0.21.0 // indirect + gopkg.in/leonelquinteros/gotext.v1 v1.3.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 23eded9..17d7b16 100644 --- a/go.sum +++ b/go.sum @@ -1,26 +1,70 @@ -git.defalsify.org/vise.git v0.2.3-0.20250103172917-3e190a44568d h1:bPAOVZOX4frSGhfOdcj7kc555f8dc9DmMd2YAyC2AMw= -git.defalsify.org/vise.git v0.2.3-0.20250103172917-3e190a44568d/go.mod h1:jyBMe1qTYUz3mmuoC9JQ/TvFeW0vTanCUcPu3H8p4Ck= -git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250113213325-5228aef0889b h1:6SieNUSEKbkjzquuwazs/lVG56zdEWF10zQQEMRJfMs= -git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250113213325-5228aef0889b/go.mod h1:E6W7ZOa7ZvVr0Bc5ot0LNSwpSPYq4hXlAIvEPy3AJ7U= +git.defalsify.org/vise.git v0.2.3-0.20250114225117-3b5fc85b650b h1:rwWXMtNSn7aqhb4p1oVZkCA1vC7pVdohwW61QQM8fUs= +git.defalsify.org/vise.git v0.2.3-0.20250114225117-3b5fc85b650b/go.mod h1:jyBMe1qTYUz3mmuoC9JQ/TvFeW0vTanCUcPu3H8p4Ck= +git.grassecon.net/grassrootseconomics/common v0.0.0-20250113174703-6afccefd1f05 h1:BenzGx6aDHKDwE23/mWIFD2InYIXyzHroZWV3MF5WUk= +git.grassecon.net/grassrootseconomics/common v0.0.0-20250113174703-6afccefd1f05/go.mod h1:wgQJZGIS6QuNLHqDhcsvehsbn5PvgV7aziRebMnJi60= +git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250115003256-c0534ede1b63 h1:bX7klKZpX+ZZu1LKbtOXDAhV/KK0YwExehiIi0jusAM= +git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250115003256-c0534ede1b63/go.mod h1:Syw9TZyigPAM7t9FvicOm36iUnidt45f0SxzT2JniQ8= github.com/barbashov/iso639-3 v0.0.0-20211020172741-1f4ffb2d8d1c h1:H9Nm+I7Cg/YVPpEV1RzU3Wq2pjamPc/UtHDgItcb7lE= github.com/barbashov/iso639-3 v0.0.0-20211020172741-1f4ffb2d8d1c/go.mod h1:rGod7o6KPeJ+hyBpHfhi4v7blx9sf+QsHsA7KAsdN6U= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/fxamacker/cbor/v2 v2.4.0 h1:ri0ArlOR+5XunOP8CRUowT0pSJOwhW098ZCUyskZD88= +github.com/fxamacker/cbor/v2 v2.4.0/go.mod h1:TA1xS00nchWmaBnEIxPSE5oHLuJBAVvqrtAnWBwBCVo= github.com/gofrs/uuid v4.4.0+incompatible h1:3qXRTX8/NbyulANqlc0lchS1gqAVxRgsuW1YrTJupqA= github.com/gofrs/uuid v4.4.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= github.com/grassrootseconomics/eth-custodial v1.3.0-beta h1:twrMBhl89GqDUL9PlkzQxMP/6OST1BByrNDj+rqXDmU= github.com/grassrootseconomics/eth-custodial v1.3.0-beta/go.mod h1:7uhRcdnJplX4t6GKCEFkbeDhhjlcaGJeJqevbcvGLZo= github.com/grassrootseconomics/ussd-data-service v1.2.0-beta h1:fn1gwbWIwHVEBtUC2zi5OqTlfI/5gU1SMk0fgGixIXk= github.com/grassrootseconomics/ussd-data-service v1.2.0-beta/go.mod h1:omfI0QtUwIdpu9gMcUqLMCG8O1XWjqJGBx1qUMiGWC0= +github.com/graygnuorg/go-gdbm v0.0.0-20220711140707-71387d66dce4 h1:U4kkNYryi/qfbBF8gh7Vsbuz+cVmhf5kt6pE9bYYyLo= +github.com/graygnuorg/go-gdbm v0.0.0-20220711140707-71387d66dce4/go.mod h1:zpZDgZFzeq9s0MIeB1P50NIEWDFFHSFBohI/NbaTD/Y= +github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= +github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= +github.com/jackc/pgx/v5 v5.7.1 h1:x7SYsPBYDkHDksogeSmZZ5xzThcTgRz++I5E+ePFUcs= +github.com/jackc/pgx/v5 v5.7.1/go.mod h1:e7O26IywZZ+naJtWWos6i6fvWK+29etgITqrqHLfoZA= +github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo= +github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= +github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/mattn/kinako v0.0.0-20170717041458-332c0a7e205a h1:0Q3H0YXzMHiciXtRcM+j0jiCe8WKPQHoRgQiRTnfcLY= +github.com/mattn/kinako v0.0.0-20170717041458-332c0a7e205a/go.mod h1:CdTTBOYzS5E4mWS1N8NWP6AHI19MP0A2B18n3hLzRMk= +github.com/pashagolub/pgxmock/v4 v4.3.0 h1:DqT7fk0OCK6H0GvqtcMsLpv8cIwWqdxWgfZNLeHCb/s= +github.com/pashagolub/pgxmock/v4 v4.3.0/go.mod h1:9VoVHXwS3XR/yPtKGzwQvwZX1kzGB9sM8SviDcHDa3A= +github.com/peteole/testdata-loader v0.3.0 h1:8jckE9KcyNHgyv/VPoaljvKZE0Rqr8+dPVYH6rfNr9I= +github.com/peteole/testdata-loader v0.3.0/go.mod h1:Mt0ZbRtb56u8SLJpNP+BnQbENljMorYBpqlvt3cS83U= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII= +github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY= github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM= +github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg= +golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc= +golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc= +golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ= +golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo= +golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +gopkg.in/leonelquinteros/gotext.v1 v1.3.1 h1:8d9/fdTG0kn/B7NNGV1BsEyvektXFAbkMsTZS2sFSCc= +gopkg.in/leonelquinteros/gotext.v1 v1.3.1/go.mod h1:X1WlGDeAFIYsW6GjgMm4VwUwZ2XjI7Zan2InxSUQWrU= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/models/alias_response.go b/models/alias_response.go new file mode 100644 index 0000000..295c9ef --- /dev/null +++ b/models/alias_response.go @@ -0,0 +1,9 @@ +package models + +type RequestAliasResult struct { + Alias string +} + +type AliasAddress struct { + Address string +} diff --git a/remote/account_service.go b/remote/account_service.go index c6dd56b..2c67b12 100644 --- a/remote/account_service.go +++ b/remote/account_service.go @@ -15,5 +15,6 @@ type AccountService interface { FetchTransactions(ctx context.Context, publicKey string) ([]dataserviceapi.Last10TxResponse, error) VoucherData(ctx context.Context, address string) (*models.VoucherDataResult, error) TokenTransfer(ctx context.Context, amount, from, to, tokenAddress string) (*models.TokenTransferResponse, error) - CheckAliasAddress(ctx context.Context, alias string) (*dataserviceapi.AliasAddress, error) + CheckAliasAddress(ctx context.Context, alias string) (*models.AliasAddress, error) + RequestAlias(ctx context.Context, hint string, publicKey string) (*models.RequestAliasResult, error) } diff --git a/remote/http/service.go b/remote/http/service.go index 53b8091..2c1aa85 100644 --- a/remote/http/service.go +++ b/remote/http/service.go @@ -3,6 +3,7 @@ package http import ( "bytes" "context" + "fmt" "encoding/json" "errors" "io" @@ -202,8 +203,8 @@ func (as *HTTPAccountService) TokenTransfer(ctx context.Context, amount, from, t // CheckAliasAddress retrieves the address of an alias from the API endpoint. // Parameters: // - alias: The alias of the user. -func (as *HTTPAccountService) CheckAliasAddress(ctx context.Context, alias string) (*dataserviceapi.AliasAddress, error) { - var r dataserviceapi.AliasAddress +func (as *HTTPAccountService) CheckAliasAddress(ctx context.Context, alias string) (*models.AliasAddress, error) { + var r models.AliasAddress ep, err := url.JoinPath(config.CheckAliasURL, alias) if err != nil { @@ -219,6 +220,10 @@ func (as *HTTPAccountService) CheckAliasAddress(ctx context.Context, alias strin return &r, err } +func (as *HTTPAccountService) RequestAlias(ctx context.Context, publicKey string, hint string) (*models.RequestAliasResult, error) { + return nil, fmt.Errorf("not yet implemented") +} + // TODO: remove eth-custodial api dependency func doRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKResponse, error) { var okResponse api.OKResponse diff --git a/testutil/mocks/api_mock.go b/testutil/mocks/api_mock.go index 35ce6e2..20b9a68 100644 --- a/testutil/mocks/api_mock.go +++ b/testutil/mocks/api_mock.go @@ -50,10 +50,16 @@ func(m MockApi) VoucherData(ctx context.Context, address string) (*models.Vouche return m.VoucherDataContent, nil } -func(m MockApi) CheckAliasAddress(ctx context.Context, alias string) (*dataserviceapi.AliasAddress, error) { +func(m MockApi) CheckAliasAddress(ctx context.Context, alias string) (*models.AliasAddress, error) { + return nil, nil +} + +func(m MockApi) RequestAlias(ctx context.Context, publicKey string, hint string) (*models.RequestAliasResult, error) { return nil, nil } func(m MockApi) TokenTransfer(ctx context.Context, amount, from, to, tokenAddress string) (*models.TokenTransferResponse, error) { return nil, nil } + + diff --git a/testutil/mocks/service_mock.go b/testutil/mocks/service_mock.go index 3e230a4..d500508 100644 --- a/testutil/mocks/service_mock.go +++ b/testutil/mocks/service_mock.go @@ -48,7 +48,12 @@ func (m *MockAccountService) TokenTransfer(ctx context.Context, amount, from, to return args.Get(0).(*models.TokenTransferResponse), args.Error(1) } -func (m *MockAccountService) CheckAliasAddress(ctx context.Context, alias string) (*dataserviceapi.AliasAddress, error) { +func (m *MockAccountService) CheckAliasAddress(ctx context.Context, alias string) (*models.AliasAddress, error) { args := m.Called(alias) - return args.Get(0).(*dataserviceapi.AliasAddress), args.Error(1) + return args.Get(0).(*models.AliasAddress), args.Error(1) +} + +func(m MockAccountService) RequestAlias(ctx context.Context, publicKey string, hint string) (*models.RequestAliasResult, error) { + args := m.Called(publicKey, hint) + return args.Get(0).(*models.RequestAliasResult), args.Error(1) } diff --git a/testutil/testservice/account_service.go b/testutil/testservice/account_service.go index a368102..12b9de4 100644 --- a/testutil/testservice/account_service.go +++ b/testutil/testservice/account_service.go @@ -57,6 +57,10 @@ func (tas *TestAccountService) TokenTransfer(ctx context.Context, amount, from, }, nil } -func (m TestAccountService) CheckAliasAddress(ctx context.Context, alias string) (*dataserviceapi.AliasAddress, error) { - return &dataserviceapi.AliasAddress{}, nil +func (m TestAccountService) CheckAliasAddress(ctx context.Context, alias string) (*models.AliasAddress, error) { + return &models.AliasAddress{}, nil +} + +func (m TestAccountService) RequestAlias(ctx context.Context, publicKey string, hint string) (*models.RequestAliasResult, error) { + return &models.RequestAliasResult{}, nil }