Compare commits
No commits in common. "master" and "lash/alias" have entirely different histories.
master
...
lash/alias
@ -16,15 +16,12 @@ const (
|
|||||||
voucherTransfersPathPrefix = "/api/v1/transfers/last10"
|
voucherTransfersPathPrefix = "/api/v1/transfers/last10"
|
||||||
voucherDataPathPrefix = "/api/v1/token"
|
voucherDataPathPrefix = "/api/v1/token"
|
||||||
AliasPrefix = "api/v1/alias"
|
AliasPrefix = "api/v1/alias"
|
||||||
SendSMSPrefix = "api/v1/external/upsell"
|
|
||||||
AliasEnsPrefix = "/api/v1/bypass"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
custodialURLBase string
|
custodialURLBase string
|
||||||
dataURLBase string
|
dataURLBase string
|
||||||
BearerToken string
|
BearerToken string
|
||||||
aliasEnsURLBase string
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -37,8 +34,6 @@ var (
|
|||||||
VoucherTransfersURL string
|
VoucherTransfersURL string
|
||||||
VoucherDataURL string
|
VoucherDataURL string
|
||||||
CheckAliasURL string
|
CheckAliasURL string
|
||||||
SendSMSURL string
|
|
||||||
AliasEnsURL string
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func setBase() error {
|
func setBase() error {
|
||||||
@ -46,7 +41,6 @@ func setBase() error {
|
|||||||
|
|
||||||
custodialURLBase = env.GetEnv("CUSTODIAL_URL_BASE", "http://localhost:5003")
|
custodialURLBase = env.GetEnv("CUSTODIAL_URL_BASE", "http://localhost:5003")
|
||||||
dataURLBase = env.GetEnv("DATA_URL_BASE", "http://localhost:5006")
|
dataURLBase = env.GetEnv("DATA_URL_BASE", "http://localhost:5006")
|
||||||
aliasEnsURLBase = env.GetEnv("ALIAS_ENS_BASE", "http://localhost:5015")
|
|
||||||
BearerToken = env.GetEnv("BEARER_TOKEN", "")
|
BearerToken = env.GetEnv("BEARER_TOKEN", "")
|
||||||
|
|
||||||
_, err = url.Parse(custodialURLBase)
|
_, err = url.Parse(custodialURLBase)
|
||||||
@ -75,7 +69,6 @@ func LoadConfig() error {
|
|||||||
VoucherTransfersURL, _ = url.JoinPath(dataURLBase, voucherTransfersPathPrefix)
|
VoucherTransfersURL, _ = url.JoinPath(dataURLBase, voucherTransfersPathPrefix)
|
||||||
VoucherDataURL, _ = url.JoinPath(dataURLBase, voucherDataPathPrefix)
|
VoucherDataURL, _ = url.JoinPath(dataURLBase, voucherDataPathPrefix)
|
||||||
CheckAliasURL, _ = url.JoinPath(dataURLBase, AliasPrefix)
|
CheckAliasURL, _ = url.JoinPath(dataURLBase, AliasPrefix)
|
||||||
SendSMSURL, _ = url.JoinPath(dataURLBase, SendSMSPrefix)
|
|
||||||
AliasEnsURL, _ = url.JoinPath(aliasEnsURLBase, AliasEnsPrefix)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
99
dev/api.go
99
dev/api.go
@ -12,20 +12,19 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"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"
|
"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"
|
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\\-_]+$")
|
aliasRegex = regexp.MustCompile("^\\+?[a-zA-Z0-9\\-_]+$")
|
||||||
searchDomain = ".sarafu.local"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -33,7 +32,6 @@ const (
|
|||||||
hashLen int = 32
|
hashLen int = 32
|
||||||
defaultDecimals = 6
|
defaultDecimals = 6
|
||||||
zeroAddress string = "0x0000000000000000000000000000000000000000"
|
zeroAddress string = "0x0000000000000000000000000000000000000000"
|
||||||
defaultVoucherBalance float64 = 500.00
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Tx struct {
|
type Tx struct {
|
||||||
@ -106,6 +104,7 @@ type DevAccountService struct {
|
|||||||
defaultAccount string
|
defaultAccount string
|
||||||
emitterFunc event.EmitterFunc
|
emitterFunc event.EmitterFunc
|
||||||
pfx []byte
|
pfx []byte
|
||||||
|
// accountsSession map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDevAccountService(ctx context.Context, ss storage.StorageService) *DevAccountService {
|
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
|
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 {
|
func (das *DevAccountService) loadItem(ctx context.Context, k []byte, v []byte) error {
|
||||||
var err error
|
var err error
|
||||||
s := string(k)
|
s := string(k)
|
||||||
@ -202,13 +192,8 @@ func (das *DevAccountService) loadItem(ctx context.Context, k []byte, v []byte)
|
|||||||
}
|
}
|
||||||
if ss[0] == "account" {
|
if ss[0] == "account" {
|
||||||
err = das.loadAccount(ctx, ss[1], v)
|
err = das.loadAccount(ctx, ss[1], v)
|
||||||
logg.ErrorCtxf(ctx, "loading saved account failed", "error_load_account", err)
|
|
||||||
} else if ss[0] == "tx" {
|
} else if ss[0] == "tx" {
|
||||||
err = das.loadTx(ctx, ss[1], v)
|
err = das.loadTx(ctx, ss[1], v)
|
||||||
logg.ErrorCtxf(ctx, "loading transactions failed", "error_load_txs", err)
|
|
||||||
} else if ss[0] == "alias" {
|
|
||||||
err = das.loadAlias(ctx, ss[1], k)
|
|
||||||
logg.ErrorCtxf(ctx, "loading aliases failed", "error_load_aliases", err)
|
|
||||||
} else {
|
} else {
|
||||||
logg.ErrorCtxf(ctx, "unknown double underscore key", "key", ss[0])
|
logg.ErrorCtxf(ctx, "unknown double underscore key", "key", ss[0])
|
||||||
}
|
}
|
||||||
@ -240,7 +225,7 @@ func (das *DevAccountService) loadAll(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (das *DevAccountService) indexAll(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 := das.accounts[v.From]
|
||||||
acc.Txs = append(acc.Txs, k)
|
acc.Txs = append(acc.Txs, k)
|
||||||
logg.TraceCtxf(ctx, "add tx to sender index", "from", v.From, "tx", k)
|
logg.TraceCtxf(ctx, "add tx to sender index", "from", v.From, "tx", k)
|
||||||
@ -310,7 +295,7 @@ func (das *DevAccountService) CheckBalance(ctx context.Context, publicKey string
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (das *DevAccountService) balanceAuto(ctx context.Context, pubKey string) error {
|
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]
|
voucher, ok := das.vouchers[v]
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("balance auto voucher set but not resolved: %s", v)
|
return fmt.Errorf("balance auto voucher set but not resolved: %s", v)
|
||||||
@ -327,10 +312,6 @@ func (das *DevAccountService) balanceAuto(ctx context.Context, pubKey string) er
|
|||||||
return nil
|
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 {
|
func (das *DevAccountService) saveAccount(ctx context.Context, acc Account) error {
|
||||||
if das.db == nil {
|
if das.db == nil {
|
||||||
return nil
|
return nil
|
||||||
@ -345,23 +326,6 @@ func (das *DevAccountService) saveAccount(ctx context.Context, acc Account) erro
|
|||||||
return das.db.Put(ctx, []byte(k), v)
|
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) {
|
func (das *DevAccountService) CreateAccount(ctx context.Context) (*models.AccountResult, error) {
|
||||||
var b [pubKeyLen]byte
|
var b [pubKeyLen]byte
|
||||||
uid, err := uuid.NewV4()
|
uid, err := uuid.NewV4()
|
||||||
@ -428,20 +392,22 @@ func (das *DevAccountService) TrackAccountStatus(ctx context.Context, publicKey
|
|||||||
|
|
||||||
func (das *DevAccountService) FetchVouchers(ctx context.Context, publicKey string) ([]dataserviceapi.TokenHoldings, error) {
|
func (das *DevAccountService) FetchVouchers(ctx context.Context, publicKey string) ([]dataserviceapi.TokenHoldings, error) {
|
||||||
var holdings []dataserviceapi.TokenHoldings
|
var holdings []dataserviceapi.TokenHoldings
|
||||||
_, ok := das.accounts[publicKey]
|
acc, ok := das.accounts[publicKey]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("account not found (publickey): %v", publicKey)
|
return nil, fmt.Errorf("account not found (publickey): %v", publicKey)
|
||||||
}
|
}
|
||||||
//TODO: Iterate over the account acc.Balances object
|
for k, v := range(acc.Balances) {
|
||||||
for _, voucher := range das.vouchers {
|
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{
|
holdings = append(holdings, dataserviceapi.TokenHoldings{
|
||||||
ContractAddress: voucher.Address,
|
ContractAddress: voucher.Address,
|
||||||
TokenSymbol: voucher.Symbol,
|
TokenSymbol: voucher.Symbol,
|
||||||
TokenDecimals: strconv.Itoa(voucher.Decimals),
|
TokenDecimals: strconv.Itoa(voucher.Decimals),
|
||||||
Balance: strconv.Itoa(int(defaultVoucherBalance)),
|
Balance: strconv.Itoa(v),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return holdings, nil
|
return holdings, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -451,7 +417,7 @@ func (das *DevAccountService) FetchTransactions(ctx context.Context, publicKey s
|
|||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("account not found (publickey): %v", publicKey)
|
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]
|
mytx := das.txs[v]
|
||||||
if i == 10 {
|
if i == 10 {
|
||||||
break
|
break
|
||||||
@ -490,6 +456,7 @@ func (das *DevAccountService) VoucherData(ctx context.Context, address string) (
|
|||||||
SinkAddress: voucher.Sink,
|
SinkAddress: voucher.Sink,
|
||||||
TokenCommodity: voucher.Commodity,
|
TokenCommodity: voucher.Commodity,
|
||||||
TokenLocation: voucher.Location,
|
TokenLocation: voucher.Location,
|
||||||
|
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -577,12 +544,10 @@ func (das *DevAccountService) TokenTransfer(ctx context.Context, amount, from, t
|
|||||||
func (das *DevAccountService) CheckAliasAddress(ctx context.Context, alias string) (*models.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 {
|
||||||
logg.ErrorCtxf(ctx, "alias check failed", "alias", alias)
|
|
||||||
return nil, fmt.Errorf("alias %s not found", alias)
|
return nil, fmt.Errorf("alias %s not found", alias)
|
||||||
}
|
}
|
||||||
acc, ok := das.accounts[addr]
|
acc, ok := das.accounts[addr]
|
||||||
if !ok {
|
if !ok {
|
||||||
logg.ErrorCtxf(ctx, "failed to resolve alias", "alias", alias)
|
|
||||||
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 &models.AliasAddress{
|
return &models.AliasAddress{
|
||||||
@ -603,29 +568,16 @@ func (das *DevAccountService) applyPhoneAlias(ctx context.Context, publicKey str
|
|||||||
|
|
||||||
func (das *DevAccountService) RequestAlias(ctx context.Context, publicKey string, hint string) (*models.RequestAliasResult, error) {
|
func (das *DevAccountService) RequestAlias(ctx context.Context, publicKey string, hint string) (*models.RequestAliasResult, error) {
|
||||||
var alias string
|
var alias string
|
||||||
uid, err := uuid.NewV4()
|
|
||||||
if !aliasRegex.MatchString(hint) {
|
if !aliasRegex.MatchString(hint) {
|
||||||
logg.ErrorCtxf(ctx, "alias hint does not match", "key", publicKey, "hint", hint)
|
|
||||||
return nil, fmt.Errorf("alias hint does not match: %s", publicKey)
|
return nil, fmt.Errorf("alias hint does not match: %s", publicKey)
|
||||||
}
|
}
|
||||||
acc, ok := das.accounts[publicKey]
|
acc, ok := das.accounts[publicKey]
|
||||||
if !ok {
|
if !ok {
|
||||||
//Handle accounts created via the api
|
return nil, fmt.Errorf("address %s not found", publicKey)
|
||||||
acc = Account{
|
|
||||||
Track: uid.String(),
|
|
||||||
Address: publicKey,
|
|
||||||
}
|
|
||||||
err = das.saveAccount(ctx, acc)
|
|
||||||
if err != nil {
|
|
||||||
logg.ErrorCtxf(ctx, "account save failed with", "account", acc, "account_save_error", err)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
das.accounts[publicKey] = acc
|
|
||||||
}
|
}
|
||||||
alias = hint
|
alias = hint
|
||||||
isPhone, err := das.applyPhoneAlias(ctx, publicKey, alias)
|
isPhone, err := das.applyPhoneAlias(ctx, publicKey, alias)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logg.ErrorCtxf(ctx, "failed to apply phone alias", "public key", publicKey, "alias", alias, "error", err)
|
|
||||||
return nil, fmt.Errorf("phone parser error: %v", err)
|
return nil, fmt.Errorf("phone parser error: %v", err)
|
||||||
}
|
}
|
||||||
if !isPhone {
|
if !isPhone {
|
||||||
@ -640,23 +592,10 @@ func (das *DevAccountService) RequestAlias(ctx context.Context, publicKey string
|
|||||||
alias += "x"
|
alias += "x"
|
||||||
}
|
}
|
||||||
acc.Alias = alias
|
acc.Alias = alias
|
||||||
alias = alias + searchDomain
|
|
||||||
das.accountsAlias[alias] = publicKey
|
das.accountsAlias[alias] = publicKey
|
||||||
err := das.saveAlias(ctx, map[string]string{alias: publicKey})
|
|
||||||
if err != nil {
|
|
||||||
logg.ErrorCtxf(ctx, "account save error", "public key", publicKey, "alias", alias, "alias_save_error", err)
|
|
||||||
return nil, fmt.Errorf("Failed to save the account alias with error: %s", err.Error())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
logg.DebugCtxf(ctx, "set alias", "addr", publicKey, "alias", alias)
|
logg.DebugCtxf(ctx, "set alias", "addr", publicKey, "alias", alias)
|
||||||
return &models.RequestAliasResult{
|
return &models.RequestAliasResult{
|
||||||
Alias: alias,
|
Alias: alias,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (das *DevAccountService) SendUpsellSMS(ctx context.Context, inviterPhone, inviteePhone string) (*models.SendSMSResponse, error) {
|
|
||||||
logg.DebugCtxf(ctx, "sent an SMS", "inviterPhone", inviterPhone, "inviteePhone", inviteePhone)
|
|
||||||
return &models.SendSMSResponse{
|
|
||||||
Invitee: inviteePhone,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
@ -9,7 +9,6 @@ import (
|
|||||||
|
|
||||||
func TestApiRequestAlias(t *testing.T) {
|
func TestApiRequestAlias(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
ctx = context.WithValue(ctx, "SessionId", "+25471234565")
|
|
||||||
storageService := mocks.NewMemStorageService(ctx)
|
storageService := mocks.NewMemStorageService(ctx)
|
||||||
svc := NewDevAccountService(ctx, storageService)
|
svc := NewDevAccountService(ctx, storageService)
|
||||||
ra, err := svc.CreateAccount(ctx)
|
ra, err := svc.CreateAccount(ctx)
|
||||||
@ -40,7 +39,6 @@ func TestApiRequestAlias(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
alias = "foo.sarafu.local"
|
|
||||||
if rb.Alias != alias {
|
if rb.Alias != alias {
|
||||||
t.Fatalf("expected '%s', got '%s'", alias, rb.Alias)
|
t.Fatalf("expected '%s', got '%s'", alias, rb.Alias)
|
||||||
}
|
}
|
||||||
@ -58,12 +56,12 @@ func TestApiRequestAlias(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
addr = ra.PublicKey
|
addr = ra.PublicKey
|
||||||
alias = "foox"
|
|
||||||
rb, err = svc.RequestAlias(ctx, addr, alias)
|
rb, err = svc.RequestAlias(ctx, addr, alias)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
alias = "foox.sarafu.local"
|
alias = "foox"
|
||||||
if rb.Alias != alias {
|
if rb.Alias != alias {
|
||||||
t.Fatalf("expected '%s', got '%s'", alias, rb.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
|
go 1.23.4
|
||||||
|
|
||||||
require (
|
require (
|
||||||
git.defalsify.org/vise.git v0.2.3-0.20250120121301-10739fb4a8c9
|
git.defalsify.org/vise.git v0.2.3-0.20250114225117-3b5fc85b650b
|
||||||
git.grassecon.net/grassrootseconomics/common v0.0.0-20250121134736-ba8cbbccea7d
|
git.grassecon.net/grassrootseconomics/common v0.0.0-20250113174703-6afccefd1f05
|
||||||
git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250122123424-6749c632b0a2
|
git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250115003256-c0534ede1b63
|
||||||
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
|
||||||
github.com/grassrootseconomics/ussd-data-service v1.2.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.20250114225117-3b5fc85b650b h1:rwWXMtNSn7aqhb4p1oVZkCA1vC7pVdohwW61QQM8fUs=
|
||||||
git.defalsify.org/vise.git v0.2.3-0.20250120121301-10739fb4a8c9/go.mod h1:jyBMe1qTYUz3mmuoC9JQ/TvFeW0vTanCUcPu3H8p4Ck=
|
git.defalsify.org/vise.git v0.2.3-0.20250114225117-3b5fc85b650b/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-20250113174703-6afccefd1f05 h1:BenzGx6aDHKDwE23/mWIFD2InYIXyzHroZWV3MF5WUk=
|
||||||
git.grassecon.net/grassrootseconomics/common v0.0.0-20250121134736-ba8cbbccea7d/go.mod h1:wgQJZGIS6QuNLHqDhcsvehsbn5PvgV7aziRebMnJi60=
|
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.20250122123424-6749c632b0a2 h1:ON77G5K0JNuwPb5JT/hRfF6G6+xstlBQgEIEzWydnhg=
|
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.20250122123424-6749c632b0a2/go.mod h1:pjKp9L/ZsWW3kMB0UoIl1yv9TBIuU33mn9Aghxp7vGk=
|
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 h1:H9Nm+I7Cg/YVPpEV1RzU3Wq2pjamPc/UtHDgItcb7lE=
|
||||||
github.com/barbashov/iso639-3 v0.0.0-20211020172741-1f4ffb2d8d1c/go.mod h1:rGod7o6KPeJ+hyBpHfhi4v7blx9sf+QsHsA7KAsdN6U=
|
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=
|
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||||
|
@ -7,13 +7,3 @@ type RequestAliasResult struct {
|
|||||||
type AliasAddress struct {
|
type AliasAddress struct {
|
||||||
Address string
|
Address string
|
||||||
}
|
}
|
||||||
|
|
||||||
type AliasEnsResult struct {
|
|
||||||
Address string `json:"address"`
|
|
||||||
AutoChoose bool `json:"autoChoose"`
|
|
||||||
Name string `json:"name"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type AliasEnsAddressResult struct {
|
|
||||||
Address string `json:"address"`
|
|
||||||
}
|
|
||||||
|
@ -1,5 +0,0 @@
|
|||||||
package models
|
|
||||||
|
|
||||||
type SendSMSResponse struct {
|
|
||||||
Invitee string `json:"invitee"`
|
|
||||||
}
|
|
@ -17,5 +17,4 @@ type AccountService interface {
|
|||||||
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) (*models.AliasAddress, error)
|
CheckAliasAddress(ctx context.Context, alias string) (*models.AliasAddress, error)
|
||||||
RequestAlias(ctx context.Context, hint string, publicKey string) (*models.RequestAliasResult, error)
|
RequestAlias(ctx context.Context, hint string, publicKey string) (*models.RequestAliasResult, error)
|
||||||
SendUpsellSMS(ctx context.Context, inviterPhone, inviteePhone string) (*models.SendSMSResponse, error)
|
|
||||||
}
|
}
|
||||||
|
@ -3,33 +3,21 @@ package http
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"regexp"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"git.defalsify.org/vise.git/logging"
|
|
||||||
"git.grassecon.net/grassrootseconomics/sarafu-api/config"
|
"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/sarafu-api/models"
|
||||||
"git.grassecon.net/grassrootseconomics/visedriver/storage"
|
|
||||||
"github.com/grassrootseconomics/eth-custodial/pkg/api"
|
"github.com/grassrootseconomics/eth-custodial/pkg/api"
|
||||||
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
|
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
aliasRegex = regexp.MustCompile("^\\+?[a-zA-Z0-9\\-_]+$")
|
|
||||||
logg = logging.NewVanilla().WithDomain("sarafu-api.devapi")
|
|
||||||
)
|
|
||||||
|
|
||||||
type HTTPAccountService struct {
|
type HTTPAccountService struct {
|
||||||
SS storage.StorageService
|
|
||||||
UseApi bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parameters:
|
// Parameters:
|
||||||
@ -62,10 +50,6 @@ func (as *HTTPAccountService) TrackAccountStatus(ctx context.Context, publicKey
|
|||||||
return &r, nil
|
return &r, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (as *HTTPAccountService) ToFqdn(alias string) string {
|
|
||||||
return alias + ".sarafu.eth"
|
|
||||||
}
|
|
||||||
|
|
||||||
// CheckBalance retrieves the balance for a given public key from the custodial balance API endpoint.
|
// CheckBalance retrieves the balance for a given public key from the custodial balance API endpoint.
|
||||||
// Parameters:
|
// Parameters:
|
||||||
// - publicKey: The public key associated with the account whose balance needs to be checked.
|
// - publicKey: The public key associated with the account whose balance needs to be checked.
|
||||||
@ -220,128 +204,24 @@ func (as *HTTPAccountService) TokenTransfer(ctx context.Context, amount, from, t
|
|||||||
// Parameters:
|
// Parameters:
|
||||||
// - alias: The alias of the user.
|
// - alias: The alias of the user.
|
||||||
func (as *HTTPAccountService) CheckAliasAddress(ctx context.Context, alias string) (*models.AliasAddress, error) {
|
func (as *HTTPAccountService) CheckAliasAddress(ctx context.Context, alias string) (*models.AliasAddress, error) {
|
||||||
if as.SS == nil {
|
var r models.AliasAddress
|
||||||
return nil, fmt.Errorf("The storage service cannot be nil")
|
|
||||||
}
|
|
||||||
logg.InfoCtxf(ctx, "resolving alias before formatting", "alias", alias)
|
|
||||||
svc := dev.NewDevAccountService(ctx, as.SS)
|
|
||||||
if as.UseApi {
|
|
||||||
logg.InfoCtxf(ctx, "resolving alias to address", "alias", alias)
|
|
||||||
return resolveAliasAddress(ctx, alias)
|
|
||||||
} else {
|
|
||||||
return svc.CheckAliasAddress(ctx, alias)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func resolveAliasAddress(ctx context.Context, alias string) (*models.AliasAddress, error) {
|
ep, err := url.JoinPath(config.CheckAliasURL, alias)
|
||||||
var (
|
|
||||||
aliasEnsResult models.AliasEnsAddressResult
|
|
||||||
)
|
|
||||||
|
|
||||||
ep, err := url.JoinPath(config.AliasEnsURL, "/resolve")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
u, err := url.Parse(ep)
|
req, err := http.NewRequest("GET", ep, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
query := u.Query()
|
_, err = doRequest(ctx, req, &r)
|
||||||
query.Set("name", alias)
|
return &r, err
|
||||||
u.RawQuery = query.Encode()
|
|
||||||
|
|
||||||
req, err := http.NewRequest("GET", u.String(), nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
_, err = doRequest(ctx, req, &aliasEnsResult)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &models.AliasAddress{Address: aliasEnsResult.Address}, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (as *HTTPAccountService) RequestAlias(ctx context.Context, publicKey string, hint string) (*models.RequestAliasResult, error) {
|
func (as *HTTPAccountService) RequestAlias(ctx context.Context, publicKey string, hint string) (*models.RequestAliasResult, error) {
|
||||||
if as.SS == nil {
|
return nil, fmt.Errorf("not yet implemented")
|
||||||
return nil, fmt.Errorf("The storage service cannot be nil")
|
|
||||||
}
|
|
||||||
if as.UseApi {
|
|
||||||
if !strings.Contains(hint, ".") {
|
|
||||||
hint = as.ToFqdn(hint)
|
|
||||||
}
|
|
||||||
enr, err := requestEnsAlias(ctx, publicKey, hint)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &models.RequestAliasResult{Alias: enr.Name}, nil
|
|
||||||
} else {
|
|
||||||
svc := dev.NewDevAccountService(ctx, as.SS)
|
|
||||||
return svc.RequestAlias(ctx, publicKey, hint)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func requestEnsAlias(ctx context.Context, publicKey string, hint string) (*models.AliasEnsResult, error) {
|
|
||||||
var r models.AliasEnsResult
|
|
||||||
|
|
||||||
ep, err := url.JoinPath(config.AliasEnsURL, "/register")
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
logg.InfoCtxf(ctx, "requesting alias", "endpoint", ep, "hint", hint)
|
|
||||||
//Payload with the address and hint to derive an ENS name
|
|
||||||
payload := map[string]string{
|
|
||||||
"address": publicKey,
|
|
||||||
"hint": hint,
|
|
||||||
}
|
|
||||||
payloadBytes, err := json.Marshal(payload)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
req, err := http.NewRequest("POST", ep, bytes.NewBuffer(payloadBytes))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
// Log the request body
|
|
||||||
logg.InfoCtxf(ctx, "request body", "payload", string(payloadBytes))
|
|
||||||
_, err = doRequest(ctx, req, &r)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
logg.InfoCtxf(ctx, "alias successfully assigned", "alias", r.Name)
|
|
||||||
return &r, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// SendSMS calls the API to send out an SMS.
|
|
||||||
// Parameters:
|
|
||||||
// - inviterPhone: The user initiating the SMS.
|
|
||||||
// - inviteePhone: The number being invited to Sarafu.
|
|
||||||
func (as *HTTPAccountService) SendUpsellSMS(ctx context.Context, inviterPhone, inviteePhone string) (*models.SendSMSResponse, error) {
|
|
||||||
var r models.SendSMSResponse
|
|
||||||
|
|
||||||
// Create request payload
|
|
||||||
payload := map[string]string{
|
|
||||||
"inviterPhone": inviterPhone,
|
|
||||||
"inviteePhone": inviteePhone,
|
|
||||||
}
|
|
||||||
|
|
||||||
payloadBytes, err := json.Marshal(payload)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a new request
|
|
||||||
req, err := http.NewRequest("POST", config.SendSMSURL, bytes.NewBuffer(payloadBytes))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
_, err = doRequest(ctx, req, &r)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return &r, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: remove eth-custodial api dependency
|
// TODO: remove eth-custodial api dependency
|
||||||
|
@ -4,8 +4,8 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
|
|
||||||
"git.defalsify.org/vise.git/logging"
|
"git.defalsify.org/vise.git/logging"
|
||||||
"git.grassecon.net/grassrootseconomics/sarafu-api/models"
|
|
||||||
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
|
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
|
||||||
|
"git.grassecon.net/grassrootseconomics/sarafu-api/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -62,6 +62,4 @@ func (m MockApi) TokenTransfer(ctx context.Context, amount, from, to, tokenAddre
|
|||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m MockApi) SendUpsellSMS(ctx context.Context, inviterPhone, inviteePhone string) (*models.SendSMSResponse, error) {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
@ -53,12 +53,7 @@ func (m *MockAccountService) CheckAliasAddress(ctx context.Context, alias string
|
|||||||
return args.Get(0).(*models.AliasAddress), args.Error(1)
|
return args.Get(0).(*models.AliasAddress), args.Error(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockAccountService) RequestAlias(ctx context.Context, publicKey string, hint string) (*models.RequestAliasResult, error) {
|
func(m MockAccountService) RequestAlias(ctx context.Context, publicKey string, hint string) (*models.RequestAliasResult, error) {
|
||||||
args := m.Called(publicKey, hint)
|
args := m.Called(publicKey, hint)
|
||||||
return args.Get(0).(*models.RequestAliasResult), args.Error(1)
|
return args.Get(0).(*models.RequestAliasResult), args.Error(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockAccountService) SendUpsellSMS(ctx context.Context, inviterPhone, inviteePhone string) (*models.SendSMSResponse, error) {
|
|
||||||
args := m.Called(inviterPhone, inviteePhone)
|
|
||||||
return args.Get(0).(*models.SendSMSResponse), args.Error(1)
|
|
||||||
}
|
|
||||||
|
@ -64,7 +64,3 @@ func (m TestAccountService) CheckAliasAddress(ctx context.Context, alias string)
|
|||||||
func (m TestAccountService) RequestAlias(ctx context.Context, publicKey string, hint string) (*models.RequestAliasResult, error) {
|
func (m TestAccountService) RequestAlias(ctx context.Context, publicKey string, hint string) (*models.RequestAliasResult, error) {
|
||||||
return &models.RequestAliasResult{}, nil
|
return &models.RequestAliasResult{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m TestAccountService) SendUpsellSMS(ctx context.Context, inviterPhone, inviteePhone string) (*models.SendSMSResponse, error) {
|
|
||||||
return &models.SendSMSResponse{}, nil
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user