Compare commits
35 Commits
active-sym
...
master
Author | SHA1 | Date | |
---|---|---|---|
c3567313af | |||
f82b413e34 | |||
ea0fc4491d | |||
2634790118 | |||
272a7ac05f | |||
39c0d31995 | |||
18acb53ead | |||
43d91f84f6 | |||
0914217769 | |||
ea117b7222 | |||
39e1c84a45 | |||
20ee4dfb24 | |||
0791eb1f11 | |||
8ebd2bbbfa | |||
06c534cbb1 | |||
b8d53f82bb | |||
71c01e00cd | |||
419716a2b4 | |||
54b2088842 | |||
b2208359d6 | |||
0828b1eb10 | |||
c22f9edeca | |||
c81b17994a | |||
64b42b92ec | |||
493f64157b | |||
f6c9d54a65 | |||
74c82de472 | |||
ddb8c6e748 | |||
87ebd0029b | |||
b941cf2562 | |||
1c8bda5ded | |||
30cb800450 | |||
680a1e9681 | |||
b327b569fb | |||
445ca0d0ff |
@ -21,7 +21,7 @@ RUN make VISE_PATH=/build/go-vise -B
|
||||
WORKDIR /build/sarafu-vise
|
||||
RUN echo "Building on $BUILDPLATFORM, building for $TARGETPLATFORM"
|
||||
RUN go mod download
|
||||
RUN go build -tags logtrace,online -o sarafu-at -ldflags="-X main.build=${BUILD} -s -w" cmd/africastalking/main.go
|
||||
RUN go build -tags logwarn,online -o sarafu-at -ldflags="-X main.build=${BUILD} -s -w" cmd/africastalking/main.go
|
||||
|
||||
FROM debian:bookworm-slim
|
||||
|
||||
|
@ -8,6 +8,7 @@ import (
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"gopkg.in/leonelquinteros/gotext.v1"
|
||||
|
||||
@ -193,8 +194,9 @@ func (h *MenuHandlers) createAccountNoExist(ctx context.Context, sessionId strin
|
||||
publicKey := r.PublicKey
|
||||
|
||||
data := map[storedb.DataTyp]string{
|
||||
storedb.DATA_TRACKING_ID: trackingId,
|
||||
storedb.DATA_PUBLIC_KEY: publicKey,
|
||||
storedb.DATA_TRACKING_ID: trackingId,
|
||||
storedb.DATA_PUBLIC_KEY: publicKey,
|
||||
storedb.DATA_ACCOUNT_ALIAS: "",
|
||||
}
|
||||
store := h.userdataStore
|
||||
for key, value := range data {
|
||||
@ -241,6 +243,28 @@ func (h *MenuHandlers) CreateAccount(ctx context.Context, sym string, input []by
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (h *MenuHandlers) CheckAccountCreated(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||
var res resource.Result
|
||||
flag_account_created, _ := h.flagManager.GetFlag("flag_account_created")
|
||||
|
||||
store := h.userdataStore
|
||||
|
||||
sessionId, ok := ctx.Value("SessionId").(string)
|
||||
if !ok {
|
||||
return res, fmt.Errorf("missing session")
|
||||
}
|
||||
|
||||
_, err := store.ReadEntry(ctx, sessionId, storedb.DATA_PUBLIC_KEY)
|
||||
if err != nil {
|
||||
if !db.IsNotFound(err) {
|
||||
return res, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
res.FlagSet = append(res.FlagSet, flag_account_created)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// ResetValidPin resets the flag_valid_pin flag.
|
||||
func (h *MenuHandlers) ResetValidPin(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||
var res resource.Result
|
||||
@ -1120,7 +1144,12 @@ func (h *MenuHandlers) GetCurrentProfileInfo(ctx context.Context, sym string, in
|
||||
logg.ErrorCtxf(ctx, "Failed to read account alias entry with", "key", "error", storedb.DATA_ACCOUNT_ALIAS, err)
|
||||
return res, err
|
||||
}
|
||||
res.Content = string(profileInfo)
|
||||
alias := string(profileInfo)
|
||||
if alias == "" {
|
||||
res.Content = defaultValue
|
||||
} else {
|
||||
res.Content = alias
|
||||
}
|
||||
default:
|
||||
break
|
||||
}
|
||||
@ -1164,8 +1193,10 @@ func (h *MenuHandlers) GetProfileInfo(ctx context.Context, sym string, input []b
|
||||
offerings := getEntryOrDefault(store.ReadEntry(ctx, sessionId, storedb.DATA_OFFERINGS))
|
||||
alias := getEntryOrDefault(store.ReadEntry(ctx, sessionId, storedb.DATA_ACCOUNT_ALIAS))
|
||||
|
||||
if alias != defaultValue {
|
||||
if alias != defaultValue && alias != "" {
|
||||
alias = strings.Split(alias, ".")[0]
|
||||
} else {
|
||||
alias = defaultValue
|
||||
}
|
||||
|
||||
// Construct the full name
|
||||
@ -1246,20 +1277,10 @@ func (h *MenuHandlers) UpdateAllProfileItems(ctx context.Context, sym string, in
|
||||
if !ok {
|
||||
return res, fmt.Errorf("missing session")
|
||||
}
|
||||
flag_alias_set, _ := h.flagManager.GetFlag("flag_alias_set")
|
||||
aliasSet := h.st.MatchFlag(flag_alias_set, true)
|
||||
|
||||
err := h.insertProfileItems(ctx, sessionId, &res)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
//Only request an alias if it has not been set yet:
|
||||
if !aliasSet {
|
||||
err = h.constructAccountAlias(ctx)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
@ -1460,7 +1481,7 @@ func loadUserContent(ctx context.Context, activeSym string, balance string, alia
|
||||
if alias != "" {
|
||||
content = l.Get("%s balance: %s\n", alias, balStr)
|
||||
} else {
|
||||
content = l.Get("balance: %s\n", balStr)
|
||||
content = l.Get("Balance: %s\n", balStr)
|
||||
}
|
||||
return content, nil
|
||||
}
|
||||
@ -1482,16 +1503,6 @@ func (h *MenuHandlers) CheckBalance(ctx context.Context, sym string, input []byt
|
||||
|
||||
store := h.userdataStore
|
||||
|
||||
accAlias, err := store.ReadEntry(ctx, sessionId, storedb.DATA_ACCOUNT_ALIAS)
|
||||
if err != nil {
|
||||
if !db.IsNotFound(err) {
|
||||
logg.ErrorCtxf(ctx, "failed to read account alias entry with", "key", storedb.DATA_ACCOUNT_ALIAS, "error", err)
|
||||
return res, err
|
||||
}
|
||||
} else {
|
||||
alias = strings.Split(string(accAlias), ".")[0]
|
||||
}
|
||||
|
||||
// get the active sym and active balance
|
||||
activeSym, err := store.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_SYM)
|
||||
if err != nil {
|
||||
@ -1502,8 +1513,6 @@ func (h *MenuHandlers) CheckBalance(ctx context.Context, sym string, input []byt
|
||||
}
|
||||
}
|
||||
|
||||
logg.InfoCtxf(ctx, "The active data in CheckBalance:", "activeSym", string(activeSym))
|
||||
|
||||
activeBal, err := store.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_BAL)
|
||||
if err != nil {
|
||||
if !db.IsNotFound(err) {
|
||||
@ -1512,6 +1521,16 @@ func (h *MenuHandlers) CheckBalance(ctx context.Context, sym string, input []byt
|
||||
}
|
||||
}
|
||||
|
||||
accAlias, err := store.ReadEntry(ctx, sessionId, storedb.DATA_ACCOUNT_ALIAS)
|
||||
if err != nil {
|
||||
if !db.IsNotFound(err) {
|
||||
logg.ErrorCtxf(ctx, "failed to read account alias entry with", "key", storedb.DATA_ACCOUNT_ALIAS, "error", err)
|
||||
return res, err
|
||||
}
|
||||
} else {
|
||||
alias = strings.Split(string(accAlias), ".")[0]
|
||||
}
|
||||
|
||||
content, err = loadUserContent(ctx, string(activeSym), string(activeBal), alias)
|
||||
if err != nil {
|
||||
return res, err
|
||||
@ -1927,9 +1946,11 @@ func (h *MenuHandlers) InitiateTransaction(ctx context.Context, sym string, inpu
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// SetDefaultVoucher retrieves the current vouchers
|
||||
// and sets the first as the default voucher, if no active voucher is set.
|
||||
func (h *MenuHandlers) SetDefaultVoucher(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||
// ManageVouchers retrieves the token holdings from the API using the "PublicKey" and
|
||||
// 1. sets the first as the default voucher if no active voucher is set.
|
||||
// 2. Stores list of vouchers
|
||||
// 3. updates the balance of the active voucher
|
||||
func (h *MenuHandlers) ManageVouchers(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||
var res resource.Result
|
||||
userStore := h.userdataStore
|
||||
|
||||
@ -1940,36 +1961,31 @@ func (h *MenuHandlers) SetDefaultVoucher(ctx context.Context, sym string, input
|
||||
|
||||
flag_no_active_voucher, _ := h.flagManager.GetFlag("flag_no_active_voucher")
|
||||
|
||||
// check if the user has an active sym
|
||||
activeSym, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_SYM)
|
||||
logg.InfoCtxf(ctx, "The activeSym in SetDefaultVoucher:", "activeSym", string(activeSym))
|
||||
publicKey, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_PUBLIC_KEY)
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "The err", err)
|
||||
logg.InfoCtxf(ctx, "Checking the data as no activeSym", "DATA_ACTIVE_SYM", storedb.DATA_ACTIVE_SYM)
|
||||
logg.ErrorCtxf(ctx, "failed to read publicKey entry", "key", storedb.DATA_PUBLIC_KEY, "error", err)
|
||||
return res, err
|
||||
}
|
||||
|
||||
// Fetch vouchers from API
|
||||
vouchersResp, err := h.accountService.FetchVouchers(ctx, string(publicKey))
|
||||
if err != nil {
|
||||
res.FlagSet = append(res.FlagSet, flag_no_active_voucher)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
if len(vouchersResp) == 0 {
|
||||
res.FlagSet = append(res.FlagSet, flag_no_active_voucher)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
res.FlagReset = append(res.FlagReset, flag_no_active_voucher)
|
||||
|
||||
// Check if user has an active voucher with proper error handling
|
||||
activeSym, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_SYM)
|
||||
if err != nil {
|
||||
if db.IsNotFound(err) {
|
||||
publicKey, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_PUBLIC_KEY)
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "failed to read publicKey entry with", "key", storedb.DATA_PUBLIC_KEY, "error", err)
|
||||
return res, err
|
||||
}
|
||||
|
||||
// Fetch vouchers from the API using the public key
|
||||
vouchersResp, err := h.accountService.FetchVouchers(ctx, string(publicKey))
|
||||
if err != nil {
|
||||
res.FlagSet = append(res.FlagSet, flag_no_active_voucher)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
logg.InfoCtxf(ctx, "fetched user vouchers in SetDefaultVoucher", "public_key", string(publicKey), "vouchers", vouchersResp)
|
||||
|
||||
// Return if there is no voucher
|
||||
if len(vouchersResp) == 0 {
|
||||
res.FlagSet = append(res.FlagSet, flag_no_active_voucher)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// Use only the first voucher
|
||||
// No active voucher, set the first one as default
|
||||
firstVoucher := vouchersResp[0]
|
||||
defaultSym := firstVoucher.TokenSymbol
|
||||
defaultBal := firstVoucher.Balance
|
||||
@ -1979,75 +1995,27 @@ func (h *MenuHandlers) SetDefaultVoucher(ctx context.Context, sym string, input
|
||||
// Scale down the balance
|
||||
scaledBalance := store.ScaleDownBalance(defaultBal, defaultDec)
|
||||
|
||||
logg.InfoCtxf(ctx, "firstVoucher data", "defaultSym", defaultSym, "defaultBal", defaultBal, "defaultDec", defaultDec, "defaultAddr", defaultAddr)
|
||||
|
||||
// TODO: implement atomic transaction
|
||||
// set the active symbol
|
||||
err = userStore.WriteEntry(ctx, sessionId, storedb.DATA_ACTIVE_SYM, []byte(defaultSym))
|
||||
if err != nil {
|
||||
logg.InfoCtxf(ctx, "got an error in writing DATA_ACTIVE_SYM", "defaultSym", defaultSym)
|
||||
|
||||
logg.ErrorCtxf(ctx, "failed to write defaultSym entry with", "key", storedb.DATA_ACTIVE_SYM, "value", defaultSym, "error", err)
|
||||
return res, err
|
||||
}
|
||||
// set the active balance
|
||||
err = userStore.WriteEntry(ctx, sessionId, storedb.DATA_ACTIVE_BAL, []byte(scaledBalance))
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "failed to write defaultBal entry with", "key", storedb.DATA_ACTIVE_BAL, "value", scaledBalance, "error", err)
|
||||
return res, err
|
||||
}
|
||||
// set the active decimals
|
||||
err = userStore.WriteEntry(ctx, sessionId, storedb.DATA_ACTIVE_DECIMAL, []byte(defaultDec))
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "failed to write defaultDec entry with", "key", storedb.DATA_ACTIVE_DECIMAL, "value", defaultDec, "error", err)
|
||||
return res, err
|
||||
}
|
||||
// set the active contract address
|
||||
err = userStore.WriteEntry(ctx, sessionId, storedb.DATA_ACTIVE_ADDRESS, []byte(defaultAddr))
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "failed to write defaultAddr entry with", "key", storedb.DATA_ACTIVE_ADDRESS, "value", defaultAddr, "error", err)
|
||||
return res, err
|
||||
firstVoucherMap := map[storedb.DataTyp]string{
|
||||
storedb.DATA_ACTIVE_SYM: defaultSym,
|
||||
storedb.DATA_ACTIVE_BAL: scaledBalance,
|
||||
storedb.DATA_ACTIVE_DECIMAL: defaultDec,
|
||||
storedb.DATA_ACTIVE_ADDRESS: defaultAddr,
|
||||
}
|
||||
|
||||
return res, nil
|
||||
for key, value := range firstVoucherMap {
|
||||
if err := userStore.WriteEntry(ctx, sessionId, key, []byte(value)); err != nil {
|
||||
logg.ErrorCtxf(ctx, "Failed to write active voucher data", "key", key, "error", err)
|
||||
return res, err
|
||||
}
|
||||
}
|
||||
|
||||
logg.InfoCtxf(ctx, "Default voucher set", "symbol", defaultSym, "balance", defaultBal, "decimals", defaultDec, "address", defaultAddr)
|
||||
} else {
|
||||
logg.ErrorCtxf(ctx, "failed to read activeSym entry with", "key", storedb.DATA_ACTIVE_SYM, "error", err)
|
||||
return res, err
|
||||
}
|
||||
|
||||
logg.ErrorCtxf(ctx, "failed to read activeSym entry with", "key", storedb.DATA_ACTIVE_SYM, "error", err)
|
||||
return res, err
|
||||
}
|
||||
|
||||
res.FlagReset = append(res.FlagReset, flag_no_active_voucher)
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// CheckVouchers retrieves the token holdings from the API using the "PublicKey" and stores
|
||||
// them to gdbm.
|
||||
func (h *MenuHandlers) CheckVouchers(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||
var res resource.Result
|
||||
sessionId, ok := ctx.Value("SessionId").(string)
|
||||
if !ok {
|
||||
return res, fmt.Errorf("missing session")
|
||||
}
|
||||
|
||||
userStore := h.userdataStore
|
||||
publicKey, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_PUBLIC_KEY)
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "failed to read publicKey entry with", "key", storedb.DATA_PUBLIC_KEY, "error", err)
|
||||
return res, err
|
||||
}
|
||||
|
||||
// Fetch vouchers from the API using the public key
|
||||
vouchersResp, err := h.accountService.FetchVouchers(ctx, string(publicKey))
|
||||
if err != nil {
|
||||
return res, nil
|
||||
}
|
||||
|
||||
logg.InfoCtxf(ctx, "fetched user vouchers", "public_key", string(publicKey), "vouchers", vouchersResp)
|
||||
|
||||
// check the current active sym and update the data
|
||||
activeSym, _ := userStore.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_SYM)
|
||||
if activeSym != nil {
|
||||
} else {
|
||||
// Update active voucher balance
|
||||
activeSymStr := string(activeSym)
|
||||
|
||||
// Find the matching voucher data
|
||||
@ -2077,16 +2045,8 @@ func (h *MenuHandlers) CheckVouchers(ctx context.Context, sym string, input []by
|
||||
}
|
||||
}
|
||||
|
||||
activeBal, _ := userStore.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_BAL)
|
||||
activeAddr, _ := userStore.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_ADDRESS)
|
||||
|
||||
logg.InfoCtxf(ctx, "The active data in CheckVouchers:", "activeSym", string(activeSym), string(activeBal), string(activeAddr))
|
||||
|
||||
data := store.ProcessVouchers(vouchersResp)
|
||||
|
||||
logg.InfoCtxf(ctx, "The data in CheckVouchers:", "data", data)
|
||||
|
||||
// Store all voucher data
|
||||
data := store.ProcessVouchers(vouchersResp)
|
||||
dataMap := map[storedb.DataTyp]string{
|
||||
storedb.DATA_VOUCHER_SYMBOLS: data.Symbols,
|
||||
storedb.DATA_VOUCHER_BALANCES: data.Balances,
|
||||
@ -2094,9 +2054,11 @@ func (h *MenuHandlers) CheckVouchers(ctx context.Context, sym string, input []by
|
||||
storedb.DATA_VOUCHER_ADDRESSES: data.Addresses,
|
||||
}
|
||||
|
||||
// Write data entries
|
||||
for key, value := range dataMap {
|
||||
if err := h.prefixDb.Put(ctx, []byte(storedb.ToBytes(key)), []byte(value)); err != nil {
|
||||
return res, nil
|
||||
if err := userStore.WriteEntry(ctx, sessionId, key, []byte(value)); err != nil {
|
||||
logg.ErrorCtxf(ctx, "Failed to write data entry for sessionId: %s", sessionId, "key", key, "error", err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
@ -2106,16 +2068,25 @@ func (h *MenuHandlers) CheckVouchers(ctx context.Context, sym string, input []by
|
||||
// GetVoucherList fetches the list of vouchers and formats them.
|
||||
func (h *MenuHandlers) GetVoucherList(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||
var res resource.Result
|
||||
sessionId, ok := ctx.Value("SessionId").(string)
|
||||
if !ok {
|
||||
return res, fmt.Errorf("missing session")
|
||||
}
|
||||
|
||||
userStore := h.userdataStore
|
||||
|
||||
// Read vouchers from the store
|
||||
voucherData, err := h.prefixDb.Get(ctx, storedb.ToBytes(storedb.DATA_VOUCHER_SYMBOLS))
|
||||
voucherData, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_VOUCHER_SYMBOLS)
|
||||
logg.InfoCtxf(ctx, "reading GetVoucherList entries for sessionId: %s", sessionId, "key", storedb.DATA_VOUCHER_SYMBOLS, "voucherData", voucherData)
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "Failed to read the voucherData from prefixDb", "error", err)
|
||||
logg.ErrorCtxf(ctx, "failed to read voucherData entires with", "key", storedb.DATA_VOUCHER_SYMBOLS, "error", err)
|
||||
return res, err
|
||||
}
|
||||
|
||||
formattedData := h.ReplaceSeparatorFunc(string(voucherData))
|
||||
|
||||
logg.InfoCtxf(ctx, "final output for sessionId: %s", sessionId, "key", storedb.DATA_VOUCHER_SYMBOLS, "formattedData", formattedData)
|
||||
|
||||
res.Content = string(formattedData)
|
||||
|
||||
return res, nil
|
||||
@ -2142,7 +2113,7 @@ func (h *MenuHandlers) ViewVoucher(ctx context.Context, sym string, input []byte
|
||||
return res, nil
|
||||
}
|
||||
|
||||
metadata, err := store.GetVoucherData(ctx, h.prefixDb, inputStr)
|
||||
metadata, err := store.GetVoucherData(ctx, h.userdataStore, sessionId, inputStr)
|
||||
if err != nil {
|
||||
return res, fmt.Errorf("failed to retrieve voucher data: %v", err)
|
||||
}
|
||||
@ -2502,15 +2473,17 @@ func (h *MenuHandlers) RequestCustomAlias(ctx context.Context, sym string, input
|
||||
return res, nil
|
||||
}
|
||||
}
|
||||
aliasResult, err := h.accountService.RequestAlias(ctx, string(pubKey), string(input))
|
||||
sanitizedInput := sanitizeAliasHint(string(input))
|
||||
aliasResult, err := h.accountService.RequestAlias(ctx, string(pubKey), sanitizedInput)
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "failed to retrieve alias", "alias", string(aliasHint), "error_alias_request", err)
|
||||
return res, fmt.Errorf("Failed to retrieve alias: %s", err.Error())
|
||||
}
|
||||
alias := aliasResult.Alias
|
||||
logg.InfoCtxf(ctx, "Suggested alias ", "alias", alias)
|
||||
|
||||
//Store the returned alias,wait for user to confirm it as new account alias
|
||||
err = store.WriteEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE, []byte(alias))
|
||||
err = store.WriteEntry(ctx, sessionId, storedb.DATA_SUGGESTED_ALIAS, []byte(alias))
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "failed to write account alias", "key", storedb.DATA_TEMPORARY_VALUE, "value", alias, "error", err)
|
||||
return res, err
|
||||
@ -2519,6 +2492,17 @@ func (h *MenuHandlers) RequestCustomAlias(ctx context.Context, sym string, input
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func sanitizeAliasHint(input string) string {
|
||||
for i, r := range input {
|
||||
// Check if the character is a special character (non-alphanumeric)
|
||||
if !unicode.IsLetter(r) && !unicode.IsNumber(r) {
|
||||
return input[:i]
|
||||
}
|
||||
}
|
||||
// If no special character is found, return the whole input
|
||||
return input
|
||||
}
|
||||
|
||||
// GetSuggestedAlias loads and displays the suggested alias name from the temporary value
|
||||
func (h *MenuHandlers) GetSuggestedAlias(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||
var res resource.Result
|
||||
@ -2528,7 +2512,7 @@ func (h *MenuHandlers) GetSuggestedAlias(ctx context.Context, sym string, input
|
||||
if !ok {
|
||||
return res, fmt.Errorf("missing session")
|
||||
}
|
||||
suggestedAlias, err := store.ReadEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE)
|
||||
suggestedAlias, err := store.ReadEntry(ctx, sessionId, storedb.DATA_SUGGESTED_ALIAS)
|
||||
if err != nil {
|
||||
return res, nil
|
||||
}
|
||||
@ -2536,7 +2520,7 @@ func (h *MenuHandlers) GetSuggestedAlias(ctx context.Context, sym string, input
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// ConfirmNewAlias reads the suggested alias from the temporary value and confirms it as the new account alias.
|
||||
// ConfirmNewAlias reads the suggested alias from the [DATA_SUGGECTED_ALIAS] key and confirms it as the new account alias.
|
||||
func (h *MenuHandlers) ConfirmNewAlias(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||
var res resource.Result
|
||||
store := h.userdataStore
|
||||
@ -2547,10 +2531,11 @@ func (h *MenuHandlers) ConfirmNewAlias(ctx context.Context, sym string, input []
|
||||
if !ok {
|
||||
return res, fmt.Errorf("missing session")
|
||||
}
|
||||
newAlias, err := store.ReadEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE)
|
||||
newAlias, err := store.ReadEntry(ctx, sessionId, storedb.DATA_SUGGESTED_ALIAS)
|
||||
if err != nil {
|
||||
return res, nil
|
||||
}
|
||||
logg.InfoCtxf(ctx, "Confirming new alias", "alias", string(newAlias))
|
||||
err = store.WriteEntry(ctx, sessionId, storedb.DATA_ACCOUNT_ALIAS, []byte(string(newAlias)))
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "failed to clear DATA_ACCOUNT_ALIAS_VALUE entry with", "key", storedb.DATA_ACCOUNT_ALIAS, "value", "empty", "error", err)
|
||||
|
@ -1727,7 +1727,7 @@ func TestCheckBalance(t *testing.T) {
|
||||
publicKey: "0X98765432109",
|
||||
activeSym: "ETH",
|
||||
activeBal: "1.5",
|
||||
expectedResult: resource.Result{Content: "balance: 1.50 ETH\n"},
|
||||
expectedResult: resource.Result{Content: "Balance: 1.50 ETH\n"},
|
||||
expectError: false,
|
||||
},
|
||||
}
|
||||
@ -1990,36 +1990,46 @@ func TestFetchCommunityBalance(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetDefaultVoucher(t *testing.T) {
|
||||
func TestManageVouchers(t *testing.T) {
|
||||
sessionId := "session123"
|
||||
publicKey := "0X13242618721"
|
||||
|
||||
ctx, store := InitializeTestStore(t)
|
||||
ctx = context.WithValue(ctx, "SessionId", sessionId)
|
||||
|
||||
fm, err := NewFlagManager(flagsPath)
|
||||
if err != nil {
|
||||
t.Logf(err.Error())
|
||||
t.Fatal(err)
|
||||
}
|
||||
flag_no_active_voucher, err := fm.GetFlag("flag_no_active_voucher")
|
||||
if err != nil {
|
||||
t.Logf(err.Error())
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
publicKey := "0X13242618721"
|
||||
err = store.WriteEntry(ctx, sessionId, storedb.DATA_PUBLIC_KEY, []byte(publicKey))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
vouchersResp []dataserviceapi.TokenHoldings
|
||||
expectedResult resource.Result
|
||||
name string
|
||||
vouchersResp []dataserviceapi.TokenHoldings
|
||||
storedActiveVoucher string
|
||||
expectedVoucherSymbols []byte
|
||||
expectedUpdatedAddress []byte
|
||||
expectedResult resource.Result
|
||||
}{
|
||||
{
|
||||
name: "Test no vouchers available",
|
||||
vouchersResp: []dataserviceapi.TokenHoldings{},
|
||||
name: "No vouchers available",
|
||||
vouchersResp: []dataserviceapi.TokenHoldings{},
|
||||
expectedVoucherSymbols: []byte(""),
|
||||
expectedUpdatedAddress: []byte(""),
|
||||
expectedResult: resource.Result{
|
||||
FlagSet: []uint32{flag_no_active_voucher},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Test set default voucher when no active voucher is set",
|
||||
name: "Set default voucher when no active voucher is set",
|
||||
vouchersResp: []dataserviceapi.TokenHoldings{
|
||||
{
|
||||
ContractAddress: "0x123",
|
||||
@ -2028,7 +2038,24 @@ func TestSetDefaultVoucher(t *testing.T) {
|
||||
Balance: "100",
|
||||
},
|
||||
},
|
||||
expectedResult: resource.Result{},
|
||||
expectedVoucherSymbols: []byte("1:TOKEN1"),
|
||||
expectedUpdatedAddress: []byte("0x123"),
|
||||
expectedResult: resource.Result{
|
||||
FlagReset: []uint32{flag_no_active_voucher},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "Check and update active voucher balance",
|
||||
vouchersResp: []dataserviceapi.TokenHoldings{
|
||||
{ContractAddress: "0xd4c288865Ce", TokenSymbol: "SRF", TokenDecimals: "6", Balance: "100"},
|
||||
{ContractAddress: "0x41c188d63Qa", TokenSymbol: "MILO", TokenDecimals: "4", Balance: "200"},
|
||||
},
|
||||
storedActiveVoucher: "SRF",
|
||||
expectedVoucherSymbols: []byte("1:SRF\n2:MILO"),
|
||||
expectedUpdatedAddress: []byte("0xd4c288865Ce"),
|
||||
expectedResult: resource.Result{
|
||||
FlagReset: []uint32{flag_no_active_voucher},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
@ -2042,104 +2069,57 @@ func TestSetDefaultVoucher(t *testing.T) {
|
||||
flagManager: fm,
|
||||
}
|
||||
|
||||
err := store.WriteEntry(ctx, sessionId, storedb.DATA_PUBLIC_KEY, []byte(publicKey))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
mockAccountService.On("FetchVouchers", string(publicKey)).Return(tt.vouchersResp, nil)
|
||||
|
||||
res, err := h.SetDefaultVoucher(ctx, "set_default_voucher", []byte("some-input"))
|
||||
// Store active voucher if needed
|
||||
if tt.storedActiveVoucher != "" {
|
||||
err := store.WriteEntry(ctx, sessionId, storedb.DATA_ACTIVE_SYM, []byte(tt.storedActiveVoucher))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = store.WriteEntry(ctx, sessionId, storedb.DATA_ACTIVE_ADDRESS, []byte("0x41c188D45rfg6ds"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
res, err := h.ManageVouchers(ctx, "manage_vouchers", []byte(""))
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, tt.expectedResult, res)
|
||||
|
||||
assert.Equal(t, res, tt.expectedResult, "Expected result should be equal to the actual result")
|
||||
if tt.storedActiveVoucher != "" {
|
||||
// Validate stored voucher symbols
|
||||
voucherData, err := store.ReadEntry(ctx, sessionId, storedb.DATA_VOUCHER_SYMBOLS)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, tt.expectedVoucherSymbols, voucherData)
|
||||
|
||||
mockAccountService.AssertExpectations(t)
|
||||
// Validate stored active contract address
|
||||
updatedAddress, err := store.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_ADDRESS)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, tt.expectedUpdatedAddress, updatedAddress)
|
||||
|
||||
mockAccountService.AssertExpectations(t)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckVouchers(t *testing.T) {
|
||||
mockAccountService := new(mocks.MockAccountService)
|
||||
sessionId := "session123"
|
||||
publicKey := "0X13242618721"
|
||||
|
||||
ctx, store := InitializeTestStore(t)
|
||||
ctx = context.WithValue(ctx, "SessionId", sessionId)
|
||||
spdb := InitializeTestSubPrefixDb(t, ctx)
|
||||
|
||||
h := &MenuHandlers{
|
||||
userdataStore: store,
|
||||
accountService: mockAccountService,
|
||||
prefixDb: spdb,
|
||||
}
|
||||
|
||||
err := store.WriteEntry(ctx, sessionId, storedb.DATA_PUBLIC_KEY, []byte(publicKey))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
mockVouchersResponse := []dataserviceapi.TokenHoldings{
|
||||
{ContractAddress: "0xd4c288865Ce", TokenSymbol: "SRF", TokenDecimals: "6", Balance: "100"},
|
||||
{ContractAddress: "0x41c188d63Qa", TokenSymbol: "MILO", TokenDecimals: "4", Balance: "200"},
|
||||
}
|
||||
|
||||
// store the default voucher data
|
||||
err = store.WriteEntry(ctx, sessionId, storedb.DATA_ACTIVE_SYM, []byte("SRF"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = store.WriteEntry(ctx, sessionId, storedb.DATA_ACTIVE_ADDRESS, []byte("0x41c188D45rfg6ds"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
expectedSym := []byte("1:SRF\n2:MILO")
|
||||
expectedUpdatedAddress := []byte("0xd4c288865Ce")
|
||||
|
||||
mockAccountService.On("FetchVouchers", string(publicKey)).Return(mockVouchersResponse, nil)
|
||||
|
||||
_, err = h.CheckVouchers(ctx, "check_vouchers", []byte(""))
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Read voucher sym data from the store
|
||||
voucherData, err := spdb.Get(ctx, storedb.ToBytes(storedb.DATA_VOUCHER_SYMBOLS))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Read active contract address from the store
|
||||
updatedAddress, err := store.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_ADDRESS)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// assert that the data is stored correctly
|
||||
assert.Equal(t, expectedSym, voucherData)
|
||||
// assert that the address is updated
|
||||
assert.Equal(t, expectedUpdatedAddress, updatedAddress)
|
||||
|
||||
mockAccountService.AssertExpectations(t)
|
||||
}
|
||||
|
||||
func TestGetVoucherList(t *testing.T) {
|
||||
sessionId := "session123"
|
||||
|
||||
ctx := context.WithValue(context.Background(), "SessionId", sessionId)
|
||||
|
||||
spdb := InitializeTestSubPrefixDb(t, ctx)
|
||||
ctx, store := InitializeTestStore(t)
|
||||
ctx = context.WithValue(ctx, "SessionId", sessionId)
|
||||
|
||||
// Initialize MenuHandlers
|
||||
h := &MenuHandlers{
|
||||
prefixDb: spdb,
|
||||
userdataStore: store,
|
||||
ReplaceSeparatorFunc: mockReplaceSeparator,
|
||||
}
|
||||
|
||||
mockSyms := []byte("1:SRF\n2:MILO")
|
||||
|
||||
// Put voucher sym data from the store
|
||||
err := spdb.Put(ctx, storedb.ToBytes(storedb.DATA_VOUCHER_SYMBOLS), mockSyms)
|
||||
err := store.WriteEntry(ctx, sessionId, storedb.DATA_VOUCHER_SYMBOLS, mockSyms)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -2159,15 +2139,11 @@ func TestViewVoucher(t *testing.T) {
|
||||
}
|
||||
ctx, store := InitializeTestStore(t)
|
||||
sessionId := "session123"
|
||||
|
||||
ctx = context.WithValue(ctx, "SessionId", sessionId)
|
||||
|
||||
spdb := InitializeTestSubPrefixDb(t, ctx)
|
||||
|
||||
h := &MenuHandlers{
|
||||
userdataStore: store,
|
||||
flagManager: fm,
|
||||
prefixDb: spdb,
|
||||
}
|
||||
|
||||
// Define mock voucher data
|
||||
@ -2180,7 +2156,7 @@ func TestViewVoucher(t *testing.T) {
|
||||
|
||||
// Put the data
|
||||
for key, value := range mockData {
|
||||
err = spdb.Put(ctx, []byte(storedb.ToBytes(key)), []byte(value))
|
||||
err := store.WriteEntry(ctx, sessionId, key, []byte(value))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@ -3476,15 +3452,6 @@ func TestUpdateAllProfileItems(t *testing.T) {
|
||||
err = store.WriteEntry(ctx, sessionId, storedb.DATA_PUBLIC_KEY, []byte(publicKey))
|
||||
require.NoError(t, err)
|
||||
|
||||
aliasInput := fmt.Sprintf("%s%s", profileItems[0], profileItems[1])
|
||||
|
||||
// Mock the account alias response
|
||||
mockAccountService.On(
|
||||
"RequestAlias",
|
||||
publicKey,
|
||||
aliasInput,
|
||||
).Return(&models.RequestAliasResult{Alias: "JohnDoe"}, nil)
|
||||
|
||||
// Call the function under test
|
||||
res, err := h.UpdateAllProfileItems(ctx, "symbol", nil)
|
||||
assert.NoError(t, err)
|
||||
@ -3496,10 +3463,6 @@ func TestUpdateAllProfileItems(t *testing.T) {
|
||||
assert.Equal(t, profileItems[i], string(storedValue))
|
||||
}
|
||||
|
||||
// Validate alias storage
|
||||
storedAlias, err := store.ReadEntry(ctx, sessionId, storedb.DATA_ACCOUNT_ALIAS)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "JohnDoe", string(storedAlias))
|
||||
assert.Equal(t, expectedResult, res)
|
||||
}
|
||||
|
||||
|
@ -49,9 +49,8 @@ func (eu *EventsUpdater) updateToken(ctx context.Context, identity identity.Iden
|
||||
|
||||
// set default token to given symbol.
|
||||
func (eu *EventsUpdater) updateDefaultToken(ctx context.Context, identity identity.Identity, userStore *store.UserDataStore, activeSym string) error {
|
||||
pfxDb := toPrefixDb(userStore, identity.SessionId)
|
||||
// TODO: the activeSym input should instead be newline separated list?
|
||||
tokenData, err := store.GetVoucherData(ctx, pfxDb, activeSym)
|
||||
tokenData, err := store.GetVoucherData(ctx, userStore, identity.SessionId, activeSym)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -103,8 +103,7 @@ func (ls *LocalHandlerService) GetHandler(accountService remote.AccountService)
|
||||
ls.DbRs.AddLocalFunc("confirm_pin_change", appHandlers.ConfirmPinChange)
|
||||
ls.DbRs.AddLocalFunc("quit_with_help", appHandlers.QuitWithHelp)
|
||||
ls.DbRs.AddLocalFunc("fetch_community_balance", appHandlers.FetchCommunityBalance)
|
||||
ls.DbRs.AddLocalFunc("set_default_voucher", appHandlers.SetDefaultVoucher)
|
||||
ls.DbRs.AddLocalFunc("check_vouchers", appHandlers.CheckVouchers)
|
||||
ls.DbRs.AddLocalFunc("manage_vouchers", appHandlers.ManageVouchers)
|
||||
ls.DbRs.AddLocalFunc("get_vouchers", appHandlers.GetVoucherList)
|
||||
ls.DbRs.AddLocalFunc("view_voucher", appHandlers.ViewVoucher)
|
||||
ls.DbRs.AddLocalFunc("set_voucher", appHandlers.SetVoucher)
|
||||
@ -128,6 +127,7 @@ func (ls *LocalHandlerService) GetHandler(accountService remote.AccountService)
|
||||
ls.DbRs.AddLocalFunc("request_custom_alias", appHandlers.RequestCustomAlias)
|
||||
ls.DbRs.AddLocalFunc("get_suggested_alias", appHandlers.GetSuggestedAlias)
|
||||
ls.DbRs.AddLocalFunc("confirm_new_alias", appHandlers.ConfirmNewAlias)
|
||||
ls.DbRs.AddLocalFunc("check_account_created", appHandlers.CheckAccountCreated)
|
||||
|
||||
ls.first = appHandlers.Init
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
LOAD reset_transaction_amount 0
|
||||
LOAD max_amount 10
|
||||
LOAD max_amount 40
|
||||
RELOAD max_amount
|
||||
MAP max_amount
|
||||
MOUT back 0
|
||||
|
@ -30,3 +30,7 @@ msgstr "Salio la Kikundi: 0.00"
|
||||
|
||||
msgid "Symbol: %s\nBalance: %s"
|
||||
msgstr "Sarafu: %s\nSalio: %s"
|
||||
|
||||
|
||||
msgid "%s balance: %s\n"
|
||||
msgstr "%s salio: %s\n"
|
@ -1,9 +1,7 @@
|
||||
LOAD clear_temporary_value 2
|
||||
RELOAD clear_temporary_value
|
||||
LOAD set_default_voucher 8
|
||||
RELOAD set_default_voucher
|
||||
LOAD check_vouchers 10
|
||||
RELOAD check_vouchers
|
||||
LOAD manage_vouchers 160
|
||||
RELOAD manage_vouchers
|
||||
LOAD check_balance 128
|
||||
RELOAD check_balance
|
||||
CATCH api_failure flag_api_call_error 1
|
||||
|
@ -1,2 +1,2 @@
|
||||
Current alias: {{.get_current_profile_info}}
|
||||
Edit my alias:
|
||||
Enter your preferred alias:
|
@ -1,2 +1,2 @@
|
||||
Lakabu ya sasa: {{.get_current_profile_info}}
|
||||
Badilisha Lakabu yangu:
|
||||
Weka lakabu unalopendelea:
|
@ -1,5 +1,7 @@
|
||||
LOAD check_blocked_status 1
|
||||
RELOAD check_blocked_status
|
||||
LOAD check_account_created 2
|
||||
RELOAD check_account_created
|
||||
CATCH blocked_account flag_account_blocked 1
|
||||
CATCH select_language flag_language_set 0
|
||||
CATCH terms flag_account_created 0
|
||||
|
@ -63,6 +63,8 @@ const (
|
||||
DATA_INITIAL_LANGUAGE_CODE
|
||||
//Fully qualified account alias string
|
||||
DATA_ACCOUNT_ALIAS
|
||||
//currently suggested alias by the api awaiting user's confirmation as accepted account alias
|
||||
DATA_SUGGESTED_ALIAS
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -68,7 +68,7 @@ func ScaleDownBalance(balance, decimals string) string {
|
||||
}
|
||||
|
||||
// GetVoucherData retrieves and matches voucher data
|
||||
func GetVoucherData(ctx context.Context, db storedb.PrefixDb, input string) (*dataserviceapi.TokenHoldings, error) {
|
||||
func GetVoucherData(ctx context.Context, store DataStore, sessionId string, input string) (*dataserviceapi.TokenHoldings, error) {
|
||||
keys := []storedb.DataTyp{
|
||||
storedb.DATA_VOUCHER_SYMBOLS,
|
||||
storedb.DATA_VOUCHER_BALANCES,
|
||||
@ -78,9 +78,9 @@ func GetVoucherData(ctx context.Context, db storedb.PrefixDb, input string) (*da
|
||||
data := make(map[storedb.DataTyp]string)
|
||||
|
||||
for _, key := range keys {
|
||||
value, err := db.Get(ctx, storedb.ToBytes(key))
|
||||
value, err := store.ReadEntry(ctx, sessionId, key)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get prefix key %x: %v", storedb.ToBytes(key), err)
|
||||
return nil, fmt.Errorf("failed to get data key %x: %v", key, err)
|
||||
}
|
||||
data[key] = string(value)
|
||||
}
|
||||
|
@ -8,7 +8,6 @@ import (
|
||||
"github.com/alecthomas/assert/v2"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
visedb "git.defalsify.org/vise.git/db"
|
||||
memdb "git.defalsify.org/vise.git/db/mem"
|
||||
storedb "git.grassecon.net/grassrootseconomics/sarafu-vise/store/db"
|
||||
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
|
||||
@ -77,16 +76,8 @@ func TestProcessVouchers(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetVoucherData(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
db := memdb.NewMemDb()
|
||||
err := db.Connect(ctx, "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
prefix := storedb.ToBytes(visedb.DATATYPE_USERDATA)
|
||||
spdb := storedb.NewSubPrefixDb(db, prefix)
|
||||
ctx, store := InitializeTestDb(t)
|
||||
sessionId := "session123"
|
||||
|
||||
// Test voucher data
|
||||
mockData := map[storedb.DataTyp][]byte{
|
||||
@ -98,13 +89,13 @@ func TestGetVoucherData(t *testing.T) {
|
||||
|
||||
// Put the data
|
||||
for key, value := range mockData {
|
||||
err = spdb.Put(ctx, []byte(storedb.ToBytes(key)), []byte(value))
|
||||
err := store.WriteEntry(ctx, sessionId, key, []byte(value))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
result, err := GetVoucherData(ctx, spdb, "1")
|
||||
result, err := GetVoucherData(ctx, store, sessionId, "1")
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "SRF", result.TokenSymbol)
|
||||
|
Loading…
Reference in New Issue
Block a user