Compare commits
No commits in common. "master" and "lash/alias" have entirely different histories.
master
...
lash/alias
@ -71,3 +71,4 @@ func LoadConfig() error {
|
||||
CheckAliasURL, _ = url.JoinPath(dataURLBase, AliasPrefix)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
83
dev/api.go
83
dev/api.go
@ -12,20 +12,19 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.defalsify.org/vise.git/db"
|
||||
"git.defalsify.org/vise.git/logging"
|
||||
"git.grassecon.net/grassrootseconomics/common/phone"
|
||||
"git.grassecon.net/grassrootseconomics/sarafu-api/event"
|
||||
"git.grassecon.net/grassrootseconomics/sarafu-api/models"
|
||||
"git.grassecon.net/grassrootseconomics/visedriver/storage"
|
||||
"github.com/gofrs/uuid"
|
||||
"git.defalsify.org/vise.git/logging"
|
||||
"git.defalsify.org/vise.git/db"
|
||||
"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"
|
||||
)
|
||||
|
||||
var (
|
||||
logg = logging.NewVanilla().WithDomain("sarafu-api.devapi")
|
||||
aliasRegex = regexp.MustCompile("^\\+?[a-zA-Z0-9\\-_]+$")
|
||||
searchDomain = ".sarafu.local"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -33,7 +32,6 @@ const (
|
||||
hashLen int = 32
|
||||
defaultDecimals = 6
|
||||
zeroAddress string = "0x0000000000000000000000000000000000000000"
|
||||
defaultVoucherBalance float64 = 500.00
|
||||
)
|
||||
|
||||
type Tx struct {
|
||||
@ -106,6 +104,7 @@ type DevAccountService struct {
|
||||
defaultAccount string
|
||||
emitterFunc event.EmitterFunc
|
||||
pfx []byte
|
||||
// accountsSession map[string]string
|
||||
}
|
||||
|
||||
func NewDevAccountService(ctx context.Context, ss storage.StorageService) *DevAccountService {
|
||||
@ -184,15 +183,6 @@ func (das *DevAccountService) loadTx(ctx context.Context, hsh string, v []byte)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (das *DevAccountService) loadAlias(ctx context.Context, alias string, key []byte) error {
|
||||
result, err := das.db.Get(ctx, key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
das.accountsAlias[alias] = strings.ReplaceAll(string(result), `"`, "")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (das *DevAccountService) loadItem(ctx context.Context, k []byte, v []byte) error {
|
||||
var err error
|
||||
s := string(k)
|
||||
@ -204,8 +194,6 @@ 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 if ss[0] == "alias" {
|
||||
err = das.loadAlias(ctx, ss[1], k)
|
||||
} else {
|
||||
logg.ErrorCtxf(ctx, "unknown double underscore key", "key", ss[0])
|
||||
}
|
||||
@ -237,7 +225,7 @@ func (das *DevAccountService) loadAll(ctx context.Context) error {
|
||||
}
|
||||
|
||||
func (das *DevAccountService) indexAll(ctx context.Context) error {
|
||||
for k, v := range das.txs {
|
||||
for k, v := range(das.txs) {
|
||||
acc := das.accounts[v.From]
|
||||
acc.Txs = append(acc.Txs, k)
|
||||
logg.TraceCtxf(ctx, "add tx to sender index", "from", v.From, "tx", k)
|
||||
@ -307,7 +295,7 @@ func (das *DevAccountService) CheckBalance(ctx context.Context, publicKey string
|
||||
}
|
||||
|
||||
func (das *DevAccountService) balanceAuto(ctx context.Context, pubKey string) error {
|
||||
for _, v := range das.autoVouchers {
|
||||
for _, v := range(das.autoVouchers) {
|
||||
voucher, ok := das.vouchers[v]
|
||||
if !ok {
|
||||
return fmt.Errorf("balance auto voucher set but not resolved: %s", v)
|
||||
@ -324,10 +312,6 @@ func (das *DevAccountService) balanceAuto(ctx context.Context, pubKey string) er
|
||||
return nil
|
||||
}
|
||||
|
||||
func (das *DevAccountService) GetAliases(ctx context.Context) map[string]string {
|
||||
return das.accountsAlias
|
||||
}
|
||||
|
||||
func (das *DevAccountService) saveAccount(ctx context.Context, acc Account) error {
|
||||
if das.db == nil {
|
||||
return nil
|
||||
@ -342,23 +326,6 @@ func (das *DevAccountService) saveAccount(ctx context.Context, acc Account) erro
|
||||
return das.db.Put(ctx, []byte(k), v)
|
||||
}
|
||||
|
||||
func (das *DevAccountService) saveAlias(ctx context.Context, alias map[string]string) error {
|
||||
if das.db == nil {
|
||||
return fmt.Errorf("Db cannot be nil")
|
||||
}
|
||||
for k, v := range alias {
|
||||
k_ := das.prefixKeyFor("alias", k)
|
||||
v_, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
das.db.SetSession("")
|
||||
das.db.SetPrefix(db.DATATYPE_USERDATA)
|
||||
return das.db.Put(ctx, []byte(k_), v_)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (das *DevAccountService) CreateAccount(ctx context.Context) (*models.AccountResult, error) {
|
||||
var b [pubKeyLen]byte
|
||||
uid, err := uuid.NewV4()
|
||||
@ -425,20 +392,22 @@ func (das *DevAccountService) TrackAccountStatus(ctx context.Context, publicKey
|
||||
|
||||
func (das *DevAccountService) FetchVouchers(ctx context.Context, publicKey string) ([]dataserviceapi.TokenHoldings, error) {
|
||||
var holdings []dataserviceapi.TokenHoldings
|
||||
_, ok := das.accounts[publicKey]
|
||||
acc, ok := das.accounts[publicKey]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("account not found (publickey): %v", publicKey)
|
||||
}
|
||||
//TODO: Iterate over the account acc.Balances object
|
||||
for _, voucher := range das.vouchers {
|
||||
for k, v := range(acc.Balances) {
|
||||
voucher, ok := das.vouchers[k]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("voucher has balance but object not found: %v", k)
|
||||
}
|
||||
holdings = append(holdings, dataserviceapi.TokenHoldings{
|
||||
ContractAddress: voucher.Address,
|
||||
TokenSymbol: voucher.Symbol,
|
||||
TokenDecimals: strconv.Itoa(voucher.Decimals),
|
||||
Balance: strconv.Itoa(int(defaultVoucherBalance)),
|
||||
Balance: strconv.Itoa(v),
|
||||
})
|
||||
}
|
||||
|
||||
return holdings, nil
|
||||
}
|
||||
|
||||
@ -448,7 +417,7 @@ func (das *DevAccountService) FetchTransactions(ctx context.Context, publicKey s
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("account not found (publickey): %v", publicKey)
|
||||
}
|
||||
for i, v := range acc.Txs {
|
||||
for i, v := range(acc.Txs) {
|
||||
mytx := das.txs[v]
|
||||
if i == 10 {
|
||||
break
|
||||
@ -487,6 +456,7 @@ func (das *DevAccountService) VoucherData(ctx context.Context, address string) (
|
||||
SinkAddress: voucher.Sink,
|
||||
TokenCommodity: voucher.Commodity,
|
||||
TokenLocation: voucher.Location,
|
||||
|
||||
}, nil
|
||||
}
|
||||
|
||||
@ -598,22 +568,12 @@ func (das *DevAccountService) applyPhoneAlias(ctx context.Context, publicKey str
|
||||
|
||||
func (das *DevAccountService) RequestAlias(ctx context.Context, publicKey string, hint string) (*models.RequestAliasResult, error) {
|
||||
var alias string
|
||||
uid, err := uuid.NewV4()
|
||||
if !aliasRegex.MatchString(hint) {
|
||||
return nil, fmt.Errorf("alias hint does not match: %s", publicKey)
|
||||
}
|
||||
acc, ok := das.accounts[publicKey]
|
||||
if !ok {
|
||||
//Handle accounts created via the api
|
||||
acc = Account{
|
||||
Track: uid.String(),
|
||||
Address: publicKey,
|
||||
}
|
||||
err = das.saveAccount(ctx, acc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
das.accounts[publicKey] = acc
|
||||
return nil, fmt.Errorf("address %s not found", publicKey)
|
||||
}
|
||||
alias = hint
|
||||
isPhone, err := das.applyPhoneAlias(ctx, publicKey, alias)
|
||||
@ -632,12 +592,7 @@ func (das *DevAccountService) RequestAlias(ctx context.Context, publicKey string
|
||||
alias += "x"
|
||||
}
|
||||
acc.Alias = alias
|
||||
alias = alias + searchDomain
|
||||
das.accountsAlias[alias] = publicKey
|
||||
err := das.saveAlias(ctx, map[string]string{alias: publicKey})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to save the account alias with error: %s", err.Error())
|
||||
}
|
||||
}
|
||||
logg.DebugCtxf(ctx, "set alias", "addr", publicKey, "alias", alias)
|
||||
return &models.RequestAliasResult{
|
||||
|
@ -9,7 +9,6 @@ import (
|
||||
|
||||
func TestApiRequestAlias(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
ctx = context.WithValue(ctx, "SessionId", "+25471234565")
|
||||
storageService := mocks.NewMemStorageService(ctx)
|
||||
svc := NewDevAccountService(ctx, storageService)
|
||||
ra, err := svc.CreateAccount(ctx)
|
||||
@ -40,7 +39,6 @@ func TestApiRequestAlias(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
alias = "foo.sarafu.local"
|
||||
if rb.Alias != alias {
|
||||
t.Fatalf("expected '%s', got '%s'", alias, rb.Alias)
|
||||
}
|
||||
@ -58,12 +56,12 @@ func TestApiRequestAlias(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
addr = ra.PublicKey
|
||||
alias = "foox"
|
||||
|
||||
rb, err = svc.RequestAlias(ctx, addr, alias)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
alias = "foox.sarafu.local"
|
||||
alias = "foox"
|
||||
if rb.Alias != alias {
|
||||
t.Fatalf("expected '%s', got '%s'", alias, rb.Alias)
|
||||
}
|
||||
|
6
go.mod
6
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.20250120121301-10739fb4a8c9
|
||||
git.grassecon.net/grassrootseconomics/common v0.0.0-20250121134736-ba8cbbccea7d
|
||||
git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250122123424-6749c632b0a2
|
||||
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.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
|
||||
|
12
go.sum
12
go.sum
@ -1,9 +1,9 @@
|
||||
git.defalsify.org/vise.git v0.2.3-0.20250120121301-10739fb4a8c9 h1:sPcqXQcywxA8W3W+9qQncLPmsrgqTIlec7vmD4/7vyA=
|
||||
git.defalsify.org/vise.git v0.2.3-0.20250120121301-10739fb4a8c9/go.mod h1:jyBMe1qTYUz3mmuoC9JQ/TvFeW0vTanCUcPu3H8p4Ck=
|
||||
git.grassecon.net/grassrootseconomics/common v0.0.0-20250121134736-ba8cbbccea7d h1:5mzLas+jxTUtusOKx4XvU+n2QvrV/mH17MnJRy46siQ=
|
||||
git.grassecon.net/grassrootseconomics/common v0.0.0-20250121134736-ba8cbbccea7d/go.mod h1:wgQJZGIS6QuNLHqDhcsvehsbn5PvgV7aziRebMnJi60=
|
||||
git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250122123424-6749c632b0a2 h1:ON77G5K0JNuwPb5JT/hRfF6G6+xstlBQgEIEzWydnhg=
|
||||
git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250122123424-6749c632b0a2/go.mod h1:pjKp9L/ZsWW3kMB0UoIl1yv9TBIuU33mn9Aghxp7vGk=
|
||||
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.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=
|
||||
|
@ -3,30 +3,21 @@ package http
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"regexp"
|
||||
|
||||
"git.grassecon.net/grassrootseconomics/sarafu-api/config"
|
||||
"git.grassecon.net/grassrootseconomics/sarafu-api/dev"
|
||||
"git.grassecon.net/grassrootseconomics/sarafu-api/models"
|
||||
"git.grassecon.net/grassrootseconomics/visedriver/storage"
|
||||
"github.com/grassrootseconomics/eth-custodial/pkg/api"
|
||||
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
|
||||
)
|
||||
|
||||
var (
|
||||
aliasRegex = regexp.MustCompile("^\\+?[a-zA-Z0-9\\-_]+$")
|
||||
)
|
||||
|
||||
type HTTPAccountService struct {
|
||||
SS storage.StorageService
|
||||
UseApi bool
|
||||
}
|
||||
|
||||
// Parameters:
|
||||
@ -213,39 +204,24 @@ func (as *HTTPAccountService) TokenTransfer(ctx context.Context, amount, from, t
|
||||
// Parameters:
|
||||
// - alias: The alias of the user.
|
||||
func (as *HTTPAccountService) CheckAliasAddress(ctx context.Context, alias string) (*models.AliasAddress, error) {
|
||||
if as.SS == nil {
|
||||
return nil, fmt.Errorf("The storage service cannot be nil")
|
||||
}
|
||||
svc := dev.NewDevAccountService(ctx, as.SS)
|
||||
if as.UseApi {
|
||||
return resolveAliasAddress(ctx, alias)
|
||||
} else {
|
||||
return svc.CheckAliasAddress(ctx, alias)
|
||||
}
|
||||
}
|
||||
|
||||
func resolveAliasAddress(ctx context.Context, alias string) (*models.AliasAddress, error) {
|
||||
var r models.AliasAddress
|
||||
|
||||
ep, err := url.JoinPath(config.CheckAliasURL, alias)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("GET", ep, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, err = doRequest(ctx, req, &r)
|
||||
return &r, err
|
||||
}
|
||||
|
||||
// TODO: Use actual custodial api to request available alias
|
||||
func (as *HTTPAccountService) RequestAlias(ctx context.Context, publicKey string, hint string) (*models.RequestAliasResult, error) {
|
||||
if as.SS == nil {
|
||||
return nil, fmt.Errorf("The storage service cannot be nil")
|
||||
}
|
||||
svc := dev.NewDevAccountService(ctx, as.SS)
|
||||
return svc.RequestAlias(ctx, publicKey, hint)
|
||||
return nil, fmt.Errorf("not yet implemented")
|
||||
}
|
||||
|
||||
// TODO: remove eth-custodial api dependency
|
||||
|
@ -4,8 +4,8 @@ import (
|
||||
"context"
|
||||
|
||||
"git.defalsify.org/vise.git/logging"
|
||||
"git.grassecon.net/grassrootseconomics/sarafu-api/models"
|
||||
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
|
||||
"git.grassecon.net/grassrootseconomics/sarafu-api/models"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -61,3 +61,5 @@ func (m MockApi) RequestAlias(ctx context.Context, publicKey string, hint string
|
||||
func(m MockApi) TokenTransfer(ctx context.Context, amount, from, to, tokenAddress string) (*models.TokenTransferResponse, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user