dev-api-aliases #5
67
dev/api.go
67
dev/api.go
@ -12,19 +12,20 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"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.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"
|
||||
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 (
|
||||
@ -104,7 +105,7 @@ type DevAccountService struct {
|
||||
defaultAccount string
|
||||
emitterFunc event.EmitterFunc
|
||||
pfx []byte
|
||||
// accountsSession map[string]string
|
||||
// accountsSession map[string]string
|
||||
lash
commented
remove commentedp lease remove commentedp lease
|
||||
}
|
||||
|
||||
func NewDevAccountService(ctx context.Context, ss storage.StorageService) *DevAccountService {
|
||||
@ -151,7 +152,7 @@ func (das *DevAccountService) WithPrefix(pfx []byte) *DevAccountService {
|
||||
}
|
||||
|
||||
func (das *DevAccountService) prefixKeyFor(k string, v string) []byte {
|
||||
return append(das.pfx, []byte(k + "_" + v)...)
|
||||
return append(das.pfx, []byte(k+"_"+v)...)
|
||||
}
|
||||
|
||||
func (das *DevAccountService) loadAccount(ctx context.Context, pubKey string, v []byte) error {
|
||||
@ -183,6 +184,15 @@ 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)
|
||||
@ -194,6 +204,8 @@ 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])
|
||||
}
|
||||
@ -225,7 +237,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)
|
||||
@ -288,14 +300,14 @@ func (das *DevAccountService) CheckBalance(ctx context.Context, publicKey string
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("balance not found for default token %s pubkey %v", acc.DefaultVoucher, publicKey)
|
||||
}
|
||||
return &models.BalanceResult {
|
||||
return &models.BalanceResult{
|
||||
Balance: strconv.Itoa(bal),
|
||||
Nonce: json.Number(strconv.Itoa(acc.Nonce)),
|
||||
}, nil
|
||||
}
|
||||
|
||||
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)
|
||||
@ -326,6 +338,23 @@ 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("")
|
||||
lash
commented
This is probably not correct? Alias is bound to a session, right? This is probably not correct? Alias is bound to a session, right?
carlos
commented
Yeah that's right.Resolved by: Yeah that's right.Resolved by: ed549cba7046818638f28ce28fae15c3eb344bc8
|
||||
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()
|
||||
@ -392,22 +421,20 @@ func (das *DevAccountService) TrackAccountStatus(ctx context.Context, publicKey
|
||||
|
||||
func (das *DevAccountService) FetchVouchers(ctx context.Context, publicKey string) ([]dataserviceapi.TokenHoldings, error) {
|
||||
var holdings []dataserviceapi.TokenHoldings
|
||||
acc, ok := das.accounts[publicKey]
|
||||
_, ok := das.accounts[publicKey]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("account not found (publickey): %v", publicKey)
|
||||
}
|
||||
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)
|
||||
}
|
||||
//TODO: Iterate over the account acc.Balances object
|
||||
for _, voucher := range das.vouchers {
|
||||
holdings = append(holdings, dataserviceapi.TokenHoldings{
|
||||
ContractAddress: voucher.Address,
|
||||
TokenSymbol: voucher.Symbol,
|
||||
TokenDecimals: strconv.Itoa(voucher.Decimals),
|
||||
Balance: strconv.Itoa(v),
|
||||
Balance: strconv.Itoa(500),
|
||||
lash
commented
Why hardcoded number? Why hardcoded number?
|
||||
})
|
||||
}
|
||||
|
||||
return holdings, nil
|
||||
}
|
||||
|
||||
@ -417,7 +444,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
|
||||
@ -456,7 +483,6 @@ func (das *DevAccountService) VoucherData(ctx context.Context, address string) (
|
||||
SinkAddress: voucher.Sink,
|
||||
TokenCommodity: voucher.Commodity,
|
||||
TokenLocation: voucher.Location,
|
||||
|
||||
}, nil
|
||||
}
|
||||
|
||||
@ -592,7 +618,12 @@ 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{
|
||||
|
Loading…
Reference in New Issue
Block a user
Export please
@lash Should be moved to: https://git.grassecon.net/grassrootseconomics/common?
no its fine here, it's for dev only anyway.