Add dev api for alias handling'
This commit is contained in:
parent
a4976e6c52
commit
18a30a0065
86
dev/api.go
86
dev/api.go
@ -7,6 +7,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -17,11 +18,13 @@ import (
|
|||||||
fsdb "git.defalsify.org/vise.git/db/fs"
|
fsdb "git.defalsify.org/vise.git/db/fs"
|
||||||
"git.grassecon.net/grassrootseconomics/sarafu-api/models"
|
"git.grassecon.net/grassrootseconomics/sarafu-api/models"
|
||||||
"git.grassecon.net/grassrootseconomics/sarafu-api/event"
|
"git.grassecon.net/grassrootseconomics/sarafu-api/event"
|
||||||
|
"git.grassecon.net/grassrootseconomics/common/phone"
|
||||||
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
|
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
logg = logging.NewVanilla().WithDomain("sarafu-api.devapi")
|
logg = logging.NewVanilla().WithDomain("sarafu-api.devapi")
|
||||||
|
aliasRegex = regexp.MustCompile("^\\+?[a-zA-Z0-9\\-_]+$")
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -104,10 +107,8 @@ type DevAccountService struct {
|
|||||||
// accountsSession map[string]string
|
// accountsSession map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDevAccountService(ctx context.Context, d string) *DevAccountService {
|
func NewDevAccountService() *DevAccountService {
|
||||||
svc := &DevAccountService{
|
svc := &DevAccountService{
|
||||||
dir: d,
|
|
||||||
db: fsdb.NewFsDb(),
|
|
||||||
accounts: make(map[string]Account),
|
accounts: make(map[string]Account),
|
||||||
accountsTrack: make(map[string]string),
|
accountsTrack: make(map[string]string),
|
||||||
accountsAlias: make(map[string]string),
|
accountsAlias: make(map[string]string),
|
||||||
@ -122,18 +123,27 @@ func NewDevAccountService(ctx context.Context, d string) *DevAccountService {
|
|||||||
Address: zeroAddress,
|
Address: zeroAddress,
|
||||||
}
|
}
|
||||||
svc.accounts[acc.Address] = acc
|
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
|
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 {
|
func (das *DevAccountService) WithEmitter(fn event.EmitterFunc) *DevAccountService {
|
||||||
das.emitterFunc = fn
|
das.emitterFunc = fn
|
||||||
return das
|
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 {
|
func (das *DevAccountService) saveAccount(ctx context.Context, acc Account) error {
|
||||||
|
if das.db == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
k := "account_" + acc.Address
|
k := "account_" + acc.Address
|
||||||
v, err := json.Marshal(acc)
|
v, err := json.Marshal(acc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -516,7 +529,7 @@ func (das *DevAccountService) TokenTransfer(ctx context.Context, amount, from, t
|
|||||||
}, nil
|
}, 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]
|
addr, ok := das.accountsAlias[alias]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("alias %s not found", alias)
|
return nil, fmt.Errorf("alias %s not found", alias)
|
||||||
@ -525,7 +538,52 @@ func (das *DevAccountService) CheckAliasAddress(ctx context.Context, alias strin
|
|||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("alias %s found but does not resolve", alias)
|
return nil, fmt.Errorf("alias %s found but does not resolve", alias)
|
||||||
}
|
}
|
||||||
return &dataserviceapi.AliasAddress{
|
return &models.AliasAddress{
|
||||||
Address: acc.Address,
|
Address: acc.Address,
|
||||||
}, nil
|
}, 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
|
||||||
|
}
|
||||||
|
72
dev/api_test.go
Normal file
72
dev/api_test.go
Normal file
@ -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)
|
||||||
|
}
|
||||||
|
}
|
1
go.mod
1
go.mod
@ -4,6 +4,7 @@ go 1.23.4
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
git.defalsify.org/vise.git v0.2.3-0.20250103172917-3e190a44568d
|
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
|
git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250113213325-5228aef0889b
|
||||||
github.com/gofrs/uuid v4.4.0+incompatible
|
github.com/gofrs/uuid v4.4.0+incompatible
|
||||||
github.com/grassrootseconomics/eth-custodial v1.3.0-beta
|
github.com/grassrootseconomics/eth-custodial v1.3.0-beta
|
||||||
|
2
go.sum
2
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 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.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 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.20250113213325-5228aef0889b/go.mod h1:E6W7ZOa7ZvVr0Bc5ot0LNSwpSPYq4hXlAIvEPy3AJ7U=
|
||||||
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 h1:H9Nm+I7Cg/YVPpEV1RzU3Wq2pjamPc/UtHDgItcb7lE=
|
||||||
|
9
models/alias_response.go
Normal file
9
models/alias_response.go
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
type RequestAliasResult struct {
|
||||||
|
Alias string
|
||||||
|
}
|
||||||
|
|
||||||
|
type AliasAddress struct {
|
||||||
|
Address string
|
||||||
|
}
|
@ -16,4 +16,5 @@ type AccountService interface {
|
|||||||
VoucherData(ctx context.Context, address string) (*models.VoucherDataResult, error)
|
VoucherData(ctx context.Context, address string) (*models.VoucherDataResult, error)
|
||||||
TokenTransfer(ctx context.Context, amount, from, to, tokenAddress string) (*models.TokenTransferResponse, 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) (*dataserviceapi.AliasAddress, error)
|
||||||
|
RequestAlias(ctx context.Context, hint string, publicKey string) (*models.RequestAliasResult, error)
|
||||||
}
|
}
|
||||||
|
@ -54,6 +54,12 @@ func(m MockApi) CheckAliasAddress(ctx context.Context, alias string) (*dataservi
|
|||||||
return nil, nil
|
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) {
|
func(m MockApi) TokenTransfer(ctx context.Context, amount, from, to, tokenAddress string) (*models.TokenTransferResponse, error) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user