Compare commits
35 Commits
dev-api-al
...
sohail/exp
| Author | SHA1 | Date | |
|---|---|---|---|
|
efb9784098
|
|||
|
16595dd69a
|
|||
|
|
5d221b8d56 | ||
|
|
341901ec83
|
||
|
|
ed416c57c7
|
||
|
|
6fc99661f7
|
||
|
|
d509477de3
|
||
|
|
4ea8fd1f04
|
||
|
|
5c0ed48e67
|
||
|
|
5ff139b649
|
||
|
|
86cfb9c020
|
||
|
|
58366274c3
|
||
|
|
b85511e436
|
||
|
|
a243fe8c29
|
||
|
|
492b101aa4
|
||
| 34957e5b6f | |||
|
|
6597d6b6a2
|
||
|
|
441e289854
|
||
|
|
5b41c8dc64
|
||
|
|
417e7f0179
|
||
|
|
2eed990921
|
||
|
|
8145b4bd00
|
||
|
|
dda02852bf
|
||
|
|
6e437cb8e0
|
||
|
|
4af0e9709d
|
||
|
|
9c8a3df971
|
||
|
|
62c33e2587 | ||
|
|
4a5de68a8c
|
||
|
|
6c4702e2ba
|
||
|
|
7280784ee1
|
||
|
|
1f3ac220d1
|
||
|
|
e64eb265a5
|
||
|
|
d5dc792dce
|
||
|
|
31eb30de0f
|
||
| 2181388f5b |
@@ -16,12 +16,17 @@ const (
|
||||
voucherTransfersPathPrefix = "/api/v1/transfers/last10"
|
||||
voucherDataPathPrefix = "/api/v1/token"
|
||||
AliasPrefix = "api/v1/alias"
|
||||
SendSMSPrefix = "api/v1/external/upsell"
|
||||
AliasEnsPrefix = "/api/v1/bypass"
|
||||
ExternalSMSPrefix = "/api/v1/external"
|
||||
)
|
||||
|
||||
var (
|
||||
custodialURLBase string
|
||||
dataURLBase string
|
||||
BearerToken string
|
||||
aliasEnsURLBase string
|
||||
externalSMSBase string
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -34,6 +39,9 @@ var (
|
||||
VoucherTransfersURL string
|
||||
VoucherDataURL string
|
||||
CheckAliasURL string
|
||||
SendSMSURL string
|
||||
AliasEnsURL string
|
||||
ExternalSMSURL string
|
||||
)
|
||||
|
||||
func setBase() error {
|
||||
@@ -41,6 +49,8 @@ func setBase() error {
|
||||
|
||||
custodialURLBase = env.GetEnv("CUSTODIAL_URL_BASE", "http://localhost:5003")
|
||||
dataURLBase = env.GetEnv("DATA_URL_BASE", "http://localhost:5006")
|
||||
aliasEnsURLBase = env.GetEnv("ALIAS_ENS_BASE", "http://localhost:5015")
|
||||
externalSMSBase = env.GetEnv("EXTERNAL_SMS_BASE", "http://localhost:5035")
|
||||
BearerToken = env.GetEnv("BEARER_TOKEN", "")
|
||||
|
||||
_, err = url.Parse(custodialURLBase)
|
||||
@@ -69,5 +79,9 @@ func LoadConfig() error {
|
||||
VoucherTransfersURL, _ = url.JoinPath(dataURLBase, voucherTransfersPathPrefix)
|
||||
VoucherDataURL, _ = url.JoinPath(dataURLBase, voucherDataPathPrefix)
|
||||
CheckAliasURL, _ = url.JoinPath(dataURLBase, AliasPrefix)
|
||||
SendSMSURL, _ = url.JoinPath(dataURLBase, SendSMSPrefix)
|
||||
AliasEnsURL, _ = url.JoinPath(aliasEnsURLBase, AliasEnsPrefix)
|
||||
ExternalSMSURL, _ = url.JoinPath(externalSMSBase, ExternalSMSPrefix)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
28
dev/api.go
28
dev/api.go
@@ -12,13 +12,13 @@ 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"
|
||||
"github.com/grassrootseconomics/go-vise/db"
|
||||
"github.com/grassrootseconomics/go-vise/logging"
|
||||
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
|
||||
)
|
||||
|
||||
@@ -202,10 +202,13 @@ func (das *DevAccountService) loadItem(ctx context.Context, k []byte, v []byte)
|
||||
}
|
||||
if ss[0] == "account" {
|
||||
err = das.loadAccount(ctx, ss[1], v)
|
||||
logg.ErrorCtxf(ctx, "loading saved account failed", "error_load_account", err)
|
||||
} else if ss[0] == "tx" {
|
||||
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 {
|
||||
logg.ErrorCtxf(ctx, "unknown double underscore key", "key", ss[0])
|
||||
}
|
||||
@@ -574,10 +577,12 @@ func (das *DevAccountService) TokenTransfer(ctx context.Context, amount, from, t
|
||||
func (das *DevAccountService) CheckAliasAddress(ctx context.Context, alias string) (*models.AliasAddress, error) {
|
||||
addr, ok := das.accountsAlias[alias]
|
||||
if !ok {
|
||||
logg.ErrorCtxf(ctx, "alias check failed", "alias", alias)
|
||||
return nil, fmt.Errorf("alias %s not found", alias)
|
||||
}
|
||||
acc, ok := das.accounts[addr]
|
||||
if !ok {
|
||||
logg.ErrorCtxf(ctx, "failed to resolve alias", "alias", alias)
|
||||
return nil, fmt.Errorf("alias %s found but does not resolve", alias)
|
||||
}
|
||||
return &models.AliasAddress{
|
||||
@@ -600,6 +605,7 @@ func (das *DevAccountService) RequestAlias(ctx context.Context, publicKey string
|
||||
var alias string
|
||||
uid, err := uuid.NewV4()
|
||||
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)
|
||||
}
|
||||
acc, ok := das.accounts[publicKey]
|
||||
@@ -611,6 +617,7 @@ func (das *DevAccountService) RequestAlias(ctx context.Context, publicKey string
|
||||
}
|
||||
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
|
||||
@@ -618,6 +625,7 @@ func (das *DevAccountService) RequestAlias(ctx context.Context, publicKey string
|
||||
alias = hint
|
||||
isPhone, err := das.applyPhoneAlias(ctx, publicKey, alias)
|
||||
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)
|
||||
}
|
||||
if !isPhone {
|
||||
@@ -636,6 +644,7 @@ func (das *DevAccountService) RequestAlias(ctx context.Context, publicKey string
|
||||
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())
|
||||
}
|
||||
}
|
||||
@@ -644,3 +653,18 @@ func (das *DevAccountService) RequestAlias(ctx context.Context, publicKey string
|
||||
Alias: alias,
|
||||
}, 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
|
||||
}
|
||||
|
||||
func (das *DevAccountService) SendPINResetSMS(ctx context.Context, admin, phone string) error {
|
||||
return fmt.Errorf("unimplemented")
|
||||
}
|
||||
|
||||
func (das *DevAccountService) SendAddressSMS(ctx context.Context, publicKey, originPhone string) error {
|
||||
return fmt.Errorf("unimplemented")
|
||||
}
|
||||
|
||||
17
go.mod
17
go.mod
@@ -1,25 +1,28 @@
|
||||
module git.grassecon.net/grassrootseconomics/sarafu-api
|
||||
|
||||
go 1.23.4
|
||||
go 1.24
|
||||
|
||||
toolchain go1.24.2
|
||||
|
||||
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.grassecon.net/grassrootseconomics/visedriver v0.9.0-beta.2.0.20250507100938-d07f95e1b4ad
|
||||
github.com/gofrs/uuid v4.4.0+incompatible
|
||||
github.com/grassrootseconomics/eth-custodial v1.3.0-beta
|
||||
github.com/grassrootseconomics/go-vise v0.4.2
|
||||
github.com/grassrootseconomics/ussd-data-service v1.2.0-beta
|
||||
github.com/stretchr/testify v1.9.0
|
||||
)
|
||||
|
||||
require (
|
||||
git.defalsify.org/vise.git v0.2.3-0.20250120121301-10739fb4a8c9 // indirect
|
||||
github.com/barbashov/iso639-3 v0.0.0-20211020172741-1f4ffb2d8d1c // indirect
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
|
||||
github.com/fxamacker/cbor/v2 v2.4.0 // indirect
|
||||
github.com/graygnuorg/go-gdbm v0.0.0-20220711140707-71387d66dce4 // indirect
|
||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
|
||||
github.com/jackc/pgx/v5 v5.7.1 // indirect
|
||||
github.com/jackc/pgx/v5 v5.7.4 // indirect
|
||||
github.com/jackc/puddle/v2 v2.2.2 // indirect
|
||||
github.com/joho/godotenv v1.5.1 // indirect
|
||||
github.com/kr/text v0.2.0 // indirect
|
||||
@@ -28,9 +31,9 @@ require (
|
||||
github.com/rogpeppe/go-internal v1.13.1 // indirect
|
||||
github.com/stretchr/objx v0.5.2 // indirect
|
||||
github.com/x448/float16 v0.8.4 // indirect
|
||||
golang.org/x/crypto v0.32.0 // indirect
|
||||
golang.org/x/sync v0.10.0 // indirect
|
||||
golang.org/x/text v0.21.0 // indirect
|
||||
golang.org/x/crypto v0.38.0 // indirect
|
||||
golang.org/x/sync v0.14.0 // indirect
|
||||
golang.org/x/text v0.25.0 // indirect
|
||||
gopkg.in/leonelquinteros/gotext.v1 v1.3.1 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
26
go.sum
26
go.sum
@@ -4,6 +4,8 @@ git.grassecon.net/grassrootseconomics/common v0.0.0-20250121134736-ba8cbbccea7d
|
||||
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.grassecon.net/grassrootseconomics/visedriver v0.9.0-beta.2.0.20250507100938-d07f95e1b4ad h1:u5em3djRpQ+iMruyDtjCKq+yY+g4dMivz4kbl9hVknw=
|
||||
git.grassecon.net/grassrootseconomics/visedriver v0.9.0-beta.2.0.20250507100938-d07f95e1b4ad/go.mod h1:QnfvU88T7bSoXFkcXrUgTLpKD/YYiXjYh0rfFoNbGaE=
|
||||
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=
|
||||
@@ -16,6 +18,10 @@ github.com/gofrs/uuid v4.4.0+incompatible h1:3qXRTX8/NbyulANqlc0lchS1gqAVxRgsuW1
|
||||
github.com/gofrs/uuid v4.4.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
|
||||
github.com/grassrootseconomics/eth-custodial v1.3.0-beta h1:twrMBhl89GqDUL9PlkzQxMP/6OST1BByrNDj+rqXDmU=
|
||||
github.com/grassrootseconomics/eth-custodial v1.3.0-beta/go.mod h1:7uhRcdnJplX4t6GKCEFkbeDhhjlcaGJeJqevbcvGLZo=
|
||||
github.com/grassrootseconomics/go-vise v0.4.1 h1:g26jvYnsWaTns60ZHdltGcsL9cjWqvYZwUZ2FWVKOGo=
|
||||
github.com/grassrootseconomics/go-vise v0.4.1/go.mod h1:tt3/Swp9F9PdhSDBDvwuI1GZRrCg32kWXWzyATIpTO4=
|
||||
github.com/grassrootseconomics/go-vise v0.4.2 h1:6c6ncOCvvXOGWBtMRhuDDLvhsUb+wn11zW6NuCmlFXY=
|
||||
github.com/grassrootseconomics/go-vise v0.4.2/go.mod h1:tt3/Swp9F9PdhSDBDvwuI1GZRrCg32kWXWzyATIpTO4=
|
||||
github.com/grassrootseconomics/ussd-data-service v1.2.0-beta h1:fn1gwbWIwHVEBtUC2zi5OqTlfI/5gU1SMk0fgGixIXk=
|
||||
github.com/grassrootseconomics/ussd-data-service v1.2.0-beta/go.mod h1:omfI0QtUwIdpu9gMcUqLMCG8O1XWjqJGBx1qUMiGWC0=
|
||||
github.com/graygnuorg/go-gdbm v0.0.0-20220711140707-71387d66dce4 h1:U4kkNYryi/qfbBF8gh7Vsbuz+cVmhf5kt6pE9bYYyLo=
|
||||
@@ -24,8 +30,8 @@ github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsI
|
||||
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
|
||||
github.com/jackc/pgx/v5 v5.7.1 h1:x7SYsPBYDkHDksogeSmZZ5xzThcTgRz++I5E+ePFUcs=
|
||||
github.com/jackc/pgx/v5 v5.7.1/go.mod h1:e7O26IywZZ+naJtWWos6i6fvWK+29etgITqrqHLfoZA=
|
||||
github.com/jackc/pgx/v5 v5.7.4 h1:9wKznZrhWa2QiHL+NjTSPP6yjl3451BX3imWDnokYlg=
|
||||
github.com/jackc/pgx/v5 v5.7.4/go.mod h1:ncY89UGWxg82EykZUwSpUKEfccBGGYq1xjrOpsbsfGQ=
|
||||
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
|
||||
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
|
||||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||
@@ -36,8 +42,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/mattn/kinako v0.0.0-20170717041458-332c0a7e205a h1:0Q3H0YXzMHiciXtRcM+j0jiCe8WKPQHoRgQiRTnfcLY=
|
||||
github.com/mattn/kinako v0.0.0-20170717041458-332c0a7e205a/go.mod h1:CdTTBOYzS5E4mWS1N8NWP6AHI19MP0A2B18n3hLzRMk=
|
||||
github.com/pashagolub/pgxmock/v4 v4.3.0 h1:DqT7fk0OCK6H0GvqtcMsLpv8cIwWqdxWgfZNLeHCb/s=
|
||||
github.com/pashagolub/pgxmock/v4 v4.3.0/go.mod h1:9VoVHXwS3XR/yPtKGzwQvwZX1kzGB9sM8SviDcHDa3A=
|
||||
github.com/pashagolub/pgxmock/v4 v4.7.0 h1:de2ORuFYyjwOQR7NBm57+321RnZxpYiuUjsmqRiqgh8=
|
||||
github.com/pashagolub/pgxmock/v4 v4.7.0/go.mod h1:9L57pC193h2aKRHVyiiE817avasIPZnPwPlw3JczWvM=
|
||||
github.com/peteole/testdata-loader v0.3.0 h1:8jckE9KcyNHgyv/VPoaljvKZE0Rqr8+dPVYH6rfNr9I=
|
||||
github.com/peteole/testdata-loader v0.3.0/go.mod h1:Mt0ZbRtb56u8SLJpNP+BnQbENljMorYBpqlvt3cS83U=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
@@ -54,12 +60,12 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
|
||||
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
|
||||
golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc=
|
||||
golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc=
|
||||
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
|
||||
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
|
||||
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
|
||||
golang.org/x/crypto v0.38.0 h1:jt+WWG8IZlBnVbomuhg2Mdq0+BBQaHbtqHEFEigjUV8=
|
||||
golang.org/x/crypto v0.38.0/go.mod h1:MvrbAqul58NNYPKnOra203SB9vpuZW0e+RRZV+Ggqjw=
|
||||
golang.org/x/sync v0.14.0 h1:woo0S4Yywslg6hp4eUFjTVOyKt0RookbpAHG4c1HmhQ=
|
||||
golang.org/x/sync v0.14.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
|
||||
golang.org/x/text v0.25.0 h1:qVyWApTSYLk/drJRO5mDlNYskwQznZmkpV2c8q9zls4=
|
||||
golang.org/x/text v0.25.0/go.mod h1:WEdwpYrmk1qmdHvhkSTNPm3app7v4rsT8F2UD6+VHIA=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||
|
||||
@@ -7,3 +7,13 @@ type RequestAliasResult struct {
|
||||
type AliasAddress struct {
|
||||
Address string
|
||||
}
|
||||
|
||||
type AliasEnsResult struct {
|
||||
Address string `json:"address"`
|
||||
AutoChoose bool `json:"autoChoose"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type AliasEnsAddressResult struct {
|
||||
Address string `json:"address"`
|
||||
}
|
||||
|
||||
5
models/send_sms_response.go
Normal file
5
models/send_sms_response.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package models
|
||||
|
||||
type SendSMSResponse struct {
|
||||
Invitee string `json:"invitee"`
|
||||
}
|
||||
@@ -17,4 +17,7 @@ type AccountService interface {
|
||||
TokenTransfer(ctx context.Context, amount, from, to, tokenAddress string) (*models.TokenTransferResponse, error)
|
||||
CheckAliasAddress(ctx context.Context, alias string) (*models.AliasAddress, error)
|
||||
RequestAlias(ctx context.Context, hint string, publicKey string) (*models.RequestAliasResult, error)
|
||||
SendUpsellSMS(ctx context.Context, inviterPhone, inviteePhone string) (*models.SendSMSResponse, error)
|
||||
SendAddressSMS(ctx context.Context, publicKey, originPhone string) error
|
||||
SendPINResetSMS(ctx context.Context, admin, phone string) error
|
||||
}
|
||||
|
||||
@@ -11,17 +11,20 @@ import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"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"
|
||||
"github.com/grassrootseconomics/go-vise/logging"
|
||||
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 {
|
||||
@@ -59,6 +62,10 @@ func (as *HTTPAccountService) TrackAccountStatus(ctx context.Context, publicKey
|
||||
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.
|
||||
// Parameters:
|
||||
// - publicKey: The public key associated with the account whose balance needs to be checked.
|
||||
@@ -216,8 +223,10 @@ func (as *HTTPAccountService) CheckAliasAddress(ctx context.Context, alias strin
|
||||
if as.SS == nil {
|
||||
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)
|
||||
@@ -225,27 +234,164 @@ func (as *HTTPAccountService) CheckAliasAddress(ctx context.Context, alias strin
|
||||
}
|
||||
|
||||
func resolveAliasAddress(ctx context.Context, alias string) (*models.AliasAddress, error) {
|
||||
var r models.AliasAddress
|
||||
var (
|
||||
aliasEnsResult models.AliasEnsAddressResult
|
||||
)
|
||||
|
||||
ep, err := url.JoinPath(config.CheckAliasURL, alias)
|
||||
ep, err := url.JoinPath(config.AliasEnsURL, "/resolve")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req, err := http.NewRequest("GET", ep, nil)
|
||||
|
||||
u, err := url.Parse(ep)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, err = doRequest(ctx, req, &r)
|
||||
return &r, err
|
||||
|
||||
query := u.Query()
|
||||
query.Set("name", alias)
|
||||
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
|
||||
}
|
||||
|
||||
// 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)
|
||||
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
|
||||
}
|
||||
|
||||
func (as *HTTPAccountService) SendAddressSMS(ctx context.Context, publicKey, originPhone string) error {
|
||||
ep, err := url.JoinPath(config.ExternalSMSURL, "address")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
logg.InfoCtxf(ctx, "sending an address sms", "endpoint", ep, "address", publicKey, "origin-phone", originPhone)
|
||||
payload := map[string]string{
|
||||
"address": publicKey,
|
||||
"originPhone": originPhone,
|
||||
}
|
||||
payloadBytes, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req, err := http.NewRequest("POST", ep, bytes.NewBuffer(payloadBytes))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = doRequest(ctx, req, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (as *HTTPAccountService) SendPINResetSMS(ctx context.Context, admin, phone string) error {
|
||||
ep, err := url.JoinPath(config.ExternalSMSURL, "pinreset")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
logg.InfoCtxf(ctx, "sending pin reset sms", "endpoint", ep, "admin", admin, "phone", phone)
|
||||
payload := map[string]string{
|
||||
"admin": admin,
|
||||
"phone": phone,
|
||||
}
|
||||
payloadBytes, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req, err := http.NewRequest("POST", ep, bytes.NewBuffer(payloadBytes))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = doRequest(ctx, req, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// TODO: remove eth-custodial api dependency
|
||||
|
||||
@@ -3,8 +3,8 @@ package mocks
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.defalsify.org/vise.git/logging"
|
||||
"git.grassecon.net/grassrootseconomics/sarafu-api/models"
|
||||
"github.com/grassrootseconomics/go-vise/logging"
|
||||
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
|
||||
)
|
||||
|
||||
@@ -61,3 +61,7 @@ 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
|
||||
}
|
||||
|
||||
func (m MockApi) SendUpsellSMS(ctx context.Context, inviterPhone, inviteePhone string) (*models.SendSMSResponse, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
@@ -53,7 +53,20 @@ func (m *MockAccountService) CheckAliasAddress(ctx context.Context, alias string
|
||||
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)
|
||||
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)
|
||||
}
|
||||
|
||||
func (m *MockAccountService) SendPINResetSMS(ctx context.Context, admin, phone string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockAccountService) SendAddressSMS(ctx context.Context, publicKey, originPhone string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -57,10 +57,22 @@ func (tas *TestAccountService) TokenTransfer(ctx context.Context, amount, from,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (m TestAccountService) CheckAliasAddress(ctx context.Context, alias string) (*models.AliasAddress, error) {
|
||||
func (m *TestAccountService) CheckAliasAddress(ctx context.Context, alias string) (*models.AliasAddress, error) {
|
||||
return &models.AliasAddress{}, nil
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func (m *TestAccountService) SendUpsellSMS(ctx context.Context, inviterPhone, inviteePhone string) (*models.SendSMSResponse, error) {
|
||||
return &models.SendSMSResponse{}, nil
|
||||
}
|
||||
|
||||
func (m *TestAccountService) SendAddressSMS(ctx context.Context, publicKey, originPhone string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *TestAccountService) SendPINResetSMS(ctx context.Context, admin, phone string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user