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 3de4590..dd3efdb 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" @@ -159,13 +160,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 12f155e..a960cfc 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" @@ -98,7 +99,7 @@ func TestCreateAccount(t *testing.T) { Description: "Account creation successed", Result: map[string]any{ "trackingId": "1234567890", - "publicKey": "1235QERYU", + "publicKey": "0xD3adB33f", }, }, expectedResult: resource.Result{ @@ -119,9 +120,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) @@ -130,6 +132,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 3c2c81e..802e092 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 DATA_TEMPORARY_DECIMAL DATA_ACTIVE_DECIMAL DATA_TEMPORARY_ADDRESS