From 18a30a00659d2d2795496ff1a4b9f8af461e6718 Mon Sep 17 00:00:00 2001 From: lash Date: Tue, 14 Jan 2025 13:53:29 +0000 Subject: [PATCH 1/6] Add dev api for alias handling' --- dev/api.go | 86 +++++++++++++++++++++++++++++++------- dev/api_test.go | 72 +++++++++++++++++++++++++++++++ go.mod | 1 + go.sum | 2 + models/alias_response.go | 9 ++++ remote/account_service.go | 1 + testutil/mocks/api_mock.go | 6 +++ 7 files changed, 163 insertions(+), 14 deletions(-) create mode 100644 dev/api_test.go create mode 100644 models/alias_response.go diff --git a/dev/api.go b/dev/api.go index 8047c34..bdf0886 100644 --- a/dev/api.go +++ b/dev/api.go @@ -7,6 +7,7 @@ import ( "fmt" "math/rand" "os" + "regexp" "strconv" "strings" "time" @@ -17,11 +18,13 @@ import ( 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" 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 ( @@ -104,10 +107,8 @@ type DevAccountService struct { // accountsSession map[string]string } -func NewDevAccountService(ctx context.Context, d string) *DevAccountService { +func NewDevAccountService() *DevAccountService { svc := &DevAccountService{ - dir: d, - db: fsdb.NewFsDb(), accounts: make(map[string]Account), accountsTrack: make(map[string]string), accountsAlias: make(map[string]string), @@ -122,18 +123,27 @@ func NewDevAccountService(ctx context.Context, d string) *DevAccountService { 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 } +func (das *DevAccountService) WithFs(ctx context.Context, dir string) *DevAccountService { + if das.db != nil { + return das + } + das.dir = dir + das.db = fsdb.NewFsDb() + err := das.db.Connect(ctx, dir) + if err != nil { + panic(err) + } + das.db.SetPrefix(db.DATATYPE_USERDATA) + err = das.loadAll(ctx) + if err != nil { + panic(err) + } + return das +} + func (das *DevAccountService) WithEmitter(fn event.EmitterFunc) *DevAccountService { das.emitterFunc = fn return das @@ -295,6 +305,9 @@ func (das *DevAccountService) balanceAuto(ctx context.Context, pubKey string) er } func (das *DevAccountService) saveAccount(ctx context.Context, acc Account) error { + if das.db == nil { + return nil + } k := "account_" + acc.Address v, err := json.Marshal(acc) if err != nil { @@ -516,7 +529,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 +538,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..3b3efcd --- /dev/null +++ b/dev/api_test.go @@ -0,0 +1,72 @@ +package dev + +import ( + "context" + "testing" +) + +func TestApiRequestAlias(t *testing.T) { + ctx := context.Background() + svc := NewDevAccountService() + 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..688ab66 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.23.4 require ( git.defalsify.org/vise.git v0.2.3-0.20250103172917-3e190a44568d + git.grassecon.net/grassrootseconomics/common v0.0.0-20250113174703-6afccefd1f05 git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250113213325-5228aef0889b github.com/gofrs/uuid v4.4.0+incompatible github.com/grassrootseconomics/eth-custodial v1.3.0-beta diff --git a/go.sum b/go.sum index 23eded9..bfe97ac 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ 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/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.20250113213325-5228aef0889b h1:6SieNUSEKbkjzquuwazs/lVG56zdEWF10zQQEMRJfMs= git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250113213325-5228aef0889b/go.mod h1:E6W7ZOa7ZvVr0Bc5ot0LNSwpSPYq4hXlAIvEPy3AJ7U= github.com/barbashov/iso639-3 v0.0.0-20211020172741-1f4ffb2d8d1c h1:H9Nm+I7Cg/YVPpEV1RzU3Wq2pjamPc/UtHDgItcb7lE= 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..ee86653 100644 --- a/remote/account_service.go +++ b/remote/account_service.go @@ -16,4 +16,5 @@ type AccountService interface { 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) + RequestAlias(ctx context.Context, hint string, publicKey string) (*models.RequestAliasResult, error) } diff --git a/testutil/mocks/api_mock.go b/testutil/mocks/api_mock.go index 35ce6e2..6794b64 100644 --- a/testutil/mocks/api_mock.go +++ b/testutil/mocks/api_mock.go @@ -54,6 +54,12 @@ func(m MockApi) CheckAliasAddress(ctx context.Context, alias string) (*dataservi 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 } + + -- 2.45.2 From 7a96b85b28692b15a745c17b5f36f4e96d816f98 Mon Sep 17 00:00:00 2001 From: lash Date: Tue, 14 Jan 2025 13:57:18 +0000 Subject: [PATCH 2/6] Correct alias result struct --- remote/account_service.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/remote/account_service.go b/remote/account_service.go index ee86653..2c67b12 100644 --- a/remote/account_service.go +++ b/remote/account_service.go @@ -15,6 +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) } -- 2.45.2 From 9645a2af7bef6252d52735b1e4a8b3db5d05f1e6 Mon Sep 17 00:00:00 2001 From: lash Date: Tue, 14 Jan 2025 14:20:51 +0000 Subject: [PATCH 3/6] Correct result structs for mock/test api implementations --- remote/http/service.go | 9 +++++++-- testutil/mocks/api_mock.go | 2 +- testutil/mocks/service_mock.go | 9 +++++++-- testutil/testservice/account_service.go | 8 ++++++-- 4 files changed, 21 insertions(+), 7 deletions(-) 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 6794b64..20b9a68 100644 --- a/testutil/mocks/api_mock.go +++ b/testutil/mocks/api_mock.go @@ -50,7 +50,7 @@ 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 } 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 } -- 2.45.2 From 7001d1f828aac0f106ca80558ce7ca2c31cf56f6 Mon Sep 17 00:00:00 2001 From: lash Date: Wed, 15 Jan 2025 00:12:38 +0000 Subject: [PATCH 4/6] Use storage service and db dumper in dev api --- dev/api.go | 67 +++++++++++++++++++++++++------------------------ dev/api_test.go | 5 +++- go.mod | 18 +++++++++++-- go.sum | 52 ++++++++++++++++++++++++++++++++++---- 4 files changed, 101 insertions(+), 41 deletions(-) diff --git a/dev/api.go b/dev/api.go index bdf0886..ea3cb3d 100644 --- a/dev/api.go +++ b/dev/api.go @@ -1,12 +1,12 @@ package dev import ( + "bytes" "context" "crypto/sha1" "encoding/json" "fmt" "math/rand" - "os" "regexp" "strconv" "strings" @@ -15,10 +15,10 @@ 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" ) @@ -90,7 +90,6 @@ type Voucher struct { } type DevAccountService struct { - dir string db db.Db accounts map[string]Account accountsTrack map[string]string @@ -107,7 +106,7 @@ type DevAccountService struct { // accountsSession map[string]string } -func NewDevAccountService() *DevAccountService { +func NewDevAccountService(ctx context.Context, ss storage.StorageService) *DevAccountService { svc := &DevAccountService{ accounts: make(map[string]Account), accountsTrack: make(map[string]string), @@ -119,6 +118,19 @@ func NewDevAccountService() *DevAccountService { autoVoucherValue: make(map[string]int), defaultAccount: zeroAddress, } + 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, } @@ -126,24 +138,6 @@ func NewDevAccountService() *DevAccountService { return svc } -func (das *DevAccountService) WithFs(ctx context.Context, dir string) *DevAccountService { - if das.db != nil { - return das - } - das.dir = dir - das.db = fsdb.NewFsDb() - err := das.db.Connect(ctx, dir) - if err != nil { - panic(err) - } - das.db.SetPrefix(db.DATATYPE_USERDATA) - err = das.loadAll(ctx) - if err != nil { - panic(err) - } - return das -} - func (das *DevAccountService) WithEmitter(fn event.EmitterFunc) *DevAccountService { das.emitterFunc = fn return das @@ -181,7 +175,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) } @@ -189,6 +183,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 } @@ -196,18 +192,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, []byte("__")) { + continue + } + err = das.loadItem(ctx, k, v) if err != nil { return err @@ -308,11 +305,13 @@ func (das *DevAccountService) saveAccount(ctx context.Context, acc Account) erro if das.db == nil { return nil } - k := "account_" + acc.Address + k := "__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) } @@ -451,11 +450,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 := "__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) } diff --git a/dev/api_test.go b/dev/api_test.go index 3b3efcd..2382bf0 100644 --- a/dev/api_test.go +++ b/dev/api_test.go @@ -3,11 +3,14 @@ package dev import ( "context" "testing" + + "git.grassecon.net/grassrootseconomics/visedriver/testutil/mocks" ) func TestApiRequestAlias(t *testing.T) { ctx := context.Background() - svc := NewDevAccountService() + storageService := mocks.NewMemStorageService(ctx) + svc := NewDevAccountService(ctx, storageService) ra, err := svc.CreateAccount(ctx) if err != nil { t.Fatal(err) diff --git a/go.mod b/go.mod index 688ab66..c4f2a66 100644 --- a/go.mod +++ b/go.mod @@ -3,9 +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.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.20250113213325-5228aef0889b + git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250115001111-24e729d27566 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 @@ -15,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 bfe97ac..2f00aad 100644 --- a/go.sum +++ b/go.sum @@ -1,28 +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.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.20250113213325-5228aef0889b h1:6SieNUSEKbkjzquuwazs/lVG56zdEWF10zQQEMRJfMs= -git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250113213325-5228aef0889b/go.mod h1:E6W7ZOa7ZvVr0Bc5ot0LNSwpSPYq4hXlAIvEPy3AJ7U= +git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250115001111-24e729d27566 h1:+Mrd6e7KnWpnzMTjwmPyXgF4+enAelmDbAqRzhq1aAM= +git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250115001111-24e729d27566/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= -- 2.45.2 From c128678f4a6dfd627f87b402cff6b6e964600991 Mon Sep 17 00:00:00 2001 From: lash Date: Wed, 15 Jan 2025 00:33:59 +0000 Subject: [PATCH 5/6] Upgrade dep --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index c4f2a66..8299445 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.23.4 require ( 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.20250115001111-24e729d27566 + 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 diff --git a/go.sum b/go.sum index 2f00aad..17d7b16 100644 --- a/go.sum +++ b/go.sum @@ -2,8 +2,8 @@ git.defalsify.org/vise.git v0.2.3-0.20250114225117-3b5fc85b650b h1:rwWXMtNSn7aqh 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.20250115001111-24e729d27566 h1:+Mrd6e7KnWpnzMTjwmPyXgF4+enAelmDbAqRzhq1aAM= -git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250115001111-24e729d27566/go.mod h1:Syw9TZyigPAM7t9FvicOm36iUnidt45f0SxzT2JniQ8= +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= -- 2.45.2 From bca7c5de969f5dad488fa8de92d99a79dcf271ea Mon Sep 17 00:00:00 2001 From: lash Date: Wed, 15 Jan 2025 07:22:14 +0000 Subject: [PATCH 6/6] Make prefix settable in dev api --- dev/api.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/dev/api.go b/dev/api.go index ea3cb3d..ff5ae02 100644 --- a/dev/api.go +++ b/dev/api.go @@ -103,6 +103,7 @@ type DevAccountService struct { autoVoucherValue map[string]int defaultAccount string emitterFunc event.EmitterFunc + pfx []byte // accountsSession map[string]string } @@ -117,6 +118,7 @@ func NewDevAccountService(ctx context.Context, ss storage.StorageService) *DevAc txsTrack: make(map[string]string), autoVoucherValue: make(map[string]int), defaultAccount: zeroAddress, + pfx: []byte("__"), } if ss != nil { var err error @@ -143,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 @@ -201,7 +212,7 @@ func (das *DevAccountService) loadAll(ctx context.Context) error { if k == nil { break } - if !bytes.HasPrefix(k, []byte("__")) { + if !bytes.HasPrefix(k, das.pfx) { continue } @@ -305,7 +316,7 @@ func (das *DevAccountService) saveAccount(ctx context.Context, acc Account) erro if das.db == nil { return nil } - k := "__account_" + acc.Address + k := das.prefixKeyFor("account", acc.Address) v, err := json.Marshal(acc) if err != nil { return err @@ -450,7 +461,7 @@ 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 -- 2.45.2