lash/export-to-term #157
18
common/hex.go
Normal file
18
common/hex.go
Normal file
@ -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
|
||||
}
|
@ -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"
|
||||
@ -176,13 +177,21 @@ func (h *Handlers) createAccountNoExist(ctx context.Context, sessionId string, r
|
||||
utils.DATA_TRACKING_ID: trackingId,
|
||||
utils.DATA_PUBLIC_KEY: publicKey,
|
||||
}
|
||||
for key, value := range data {
|
||||
store := h.userdataStore
|
||||
err := store.WriteEntry(ctx, sessionId, key, []byte(value))
|
||||
for key, value := range data {
|
||||
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
|
||||
|
||||
|
@ -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"))
|
||||
|
@ -29,6 +29,7 @@ const (
|
||||
DATA_TEMPORARY_BAL
|
||||
DATA_ACTIVE_BAL
|
||||
DATA_BLOCKED_NUMBER
|
||||
DATA_PUBLIC_KEY_REVERSE
|
||||
)
|
||||
|
||||
func typToBytes(typ DataTyp) []byte {
|
||||
|
Loading…
Reference in New Issue
Block a user