From cb997159f785e796f9d2bdb1ab7f58cae46d99f7 Mon Sep 17 00:00:00 2001 From: lash Date: Wed, 30 Oct 2024 00:59:59 +0000 Subject: [PATCH] Add reverse sessionid address lookup --- common/hex.go | 18 ++++++++++++++++++ internal/handlers/ussd/menuhandler.go | 13 +++++++++++-- internal/handlers/ussd/menuhandler_test.go | 12 ++++++++++-- internal/utils/db.go | 1 + 4 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 common/hex.go diff --git a/common/hex.go b/common/hex.go new file mode 100644 index 0000000..f5aa7ed --- /dev/null +++ b/common/hex.go @@ -0,0 +1,18 @@ +package common + +import ( + "encoding/hex" +) + +func NormalizeHex(s string) (string, error) { + if len(s) >= 2 { + if s[:2] == "0x" { + s = s[2:] + } + } + r, err := hex.DecodeString(s) + if err != nil { + return "", err + } + return hex.EncodeToString(r), nil +} diff --git a/internal/handlers/ussd/menuhandler.go b/internal/handlers/ussd/menuhandler.go index 7bc8250..dae4236 100644 --- a/internal/handlers/ussd/menuhandler.go +++ b/internal/handlers/ussd/menuhandler.go @@ -21,6 +21,7 @@ import ( "git.defalsify.org/vise.git/state" "git.grassecon.net/urdt/ussd/internal/handlers/server" "git.grassecon.net/urdt/ussd/internal/utils" + "git.grassecon.net/urdt/ussd/common" "gopkg.in/leonelquinteros/gotext.v1" "git.grassecon.net/urdt/ussd/internal/storage" @@ -154,13 +155,21 @@ func (h *Handlers) createAccountNoExist(ctx context.Context, sessionId string, r utils.DATA_TRACKING_ID: trackingId, utils.DATA_PUBLIC_KEY: publicKey, } + store := h.userdataStore for key, value := range data { - store := h.userdataStore - err := store.WriteEntry(ctx, sessionId, key, []byte(value)) + err = store.WriteEntry(ctx, sessionId, key, []byte(value)) if err != nil { return err } } + publicKeyNormalized, err := common.NormalizeHex(publicKey) + if err != nil { + return err + } + err = store.WriteEntry(ctx, publicKeyNormalized, utils.DATA_PUBLIC_KEY_REVERSE, []byte(sessionId)) + if err != nil { + return err + } res.FlagSet = append(res.FlagSet, flag_account_created) return nil diff --git a/internal/handlers/ussd/menuhandler_test.go b/internal/handlers/ussd/menuhandler_test.go index 28d25e8..0b70e80 100644 --- a/internal/handlers/ussd/menuhandler_test.go +++ b/internal/handlers/ussd/menuhandler_test.go @@ -20,6 +20,7 @@ import ( "git.grassecon.net/urdt/ussd/internal/testutil/testservice" "git.grassecon.net/urdt/ussd/internal/utils" + "git.grassecon.net/urdt/ussd/common" "github.com/alecthomas/assert/v2" "github.com/grassrootseconomics/eth-custodial/pkg/api" testdataloader "github.com/peteole/testdata-loader" @@ -96,7 +97,7 @@ func TestCreateAccount(t *testing.T) { Description: "Account creation successed", Result: map[string]any{ "trackingId": "1234567890", - "publicKey": "1235QERYU", + "publicKey": "0xD3adB33f", }, }, expectedResult: resource.Result{ @@ -117,9 +118,10 @@ func TestCreateAccount(t *testing.T) { flagManager: fm.parser, } + publicKey := tt.serverResponse.Result["publicKey"].(string) data := map[utils.DataTyp]string{ utils.DATA_TRACKING_ID: tt.serverResponse.Result["trackingId"].(string), - utils.DATA_PUBLIC_KEY: tt.serverResponse.Result["publicKey"].(string), + utils.DATA_PUBLIC_KEY: publicKey, } mockDataStore.On("ReadEntry", ctx, sessionId, utils.DATA_ACCOUNT_CREATED).Return([]byte(""), notFoundErr) @@ -128,6 +130,12 @@ func TestCreateAccount(t *testing.T) { for key, value := range data { mockDataStore.On("WriteEntry", ctx, sessionId, key, []byte(value)).Return(nil) } + publicKeyNormalized, err := common.NormalizeHex(publicKey) + if err != nil { + t.Fatal(err) + } + + mockDataStore.On("WriteEntry", ctx, publicKeyNormalized, utils.DATA_PUBLIC_KEY_REVERSE, []byte(sessionId)).Return(nil) // Call the method you want to test res, err := h.CreateAccount(ctx, "create_account", []byte("some-input")) diff --git a/internal/utils/db.go b/internal/utils/db.go index 45e7681..2c1e6fa 100644 --- a/internal/utils/db.go +++ b/internal/utils/db.go @@ -28,6 +28,7 @@ const ( DATA_ACTIVE_SYM DATA_TEMPORARY_BAL DATA_ACTIVE_BAL + DATA_PUBLIC_KEY_REVERSE ) func typToBytes(typ DataTyp) []byte {