Compare commits

...

21 Commits

Author SHA1 Message Date
823133aa37 Merge pull request 'New alias flow' (#96) from new-alias-flow into master
Some checks failed
release / docker (push) Has been cancelled
Reviewed-on: #96
2025-07-08 13:07:01 +02:00
44673ef61b
remove extra whitespace 2025-07-07 21:25:29 +03:00
559b67a3e6
display the full alias on its own line 2025-07-07 21:24:16 +03:00
b1eb460988
add a isAliasUnavailable helper function
Some checks failed
release / docker (push) Has been cancelled
2025-07-07 18:55:17 +03:00
619edc39f8
remove unused function GetSuggestedAlias 2025-07-07 17:52:59 +03:00
efa29b087e
remove unused node and function confirm_new_alias 2025-07-07 17:51:45 +03:00
e9988242b1
CATCH unavailable aliases and navigate to the alias_updated node 2025-07-07 17:47:18 +03:00
55afbedd1e
have a node to CATCH aliases that are not available (already registered aliases) 2025-07-07 17:46:25 +03:00
a3ab91f6a3
handle alias registration and updates on the single RequestCustomAlias function 2025-07-07 17:43:04 +03:00
92a122732d
add the home menu option 2025-07-07 17:41:56 +03:00
682785fc3f
add a new flag_alias_unavailable flag 2025-07-07 17:40:06 +03:00
ebb4581c27
renamed the update_alias to alias_updated 2025-07-07 14:41:46 +03:00
cd1936c12f
require accounts to be authorized to access the alias node 2025-07-07 14:17:29 +03:00
16dd0f6ebf
set the first voucher as the active voucher when the current active sym is not found in the vouchers response
Some checks failed
release / docker (push) Has been cancelled
2025-07-02 19:10:02 +03:00
a3dae12c7a
update the TestCheckBalance and add more test cases
Some checks failed
release / docker (push) Has been cancelled
2025-07-02 09:30:30 +03:00
57426b3565
use the TruncateDecimalString to format the displayed balance 2025-07-02 09:29:56 +03:00
b42dec8373 Merge pull request 'add balance to voucher list' (#89) from add-balance-to-voucher-list into master
Some checks failed
release / docker (push) Has been cancelled
Reviewed-on: #89
2025-07-02 07:51:34 +02:00
2807f039a7 Merge branch 'master' into add-balance-to-voucher-list 2025-07-02 08:50:50 +03:00
9234bfd579
update the TestGetVoucherList to the expected content 2025-06-30 13:36:07 +03:00
706b6fe629
include the balance next to each voucher 2025-06-30 13:31:55 +03:00
a543569236
add a helper function FormatVoucherList to combine the voucher symbols with their balances 2025-06-30 13:14:34 +03:00
17 changed files with 158 additions and 105 deletions

View File

@ -3,7 +3,6 @@ package application
import (
"bytes"
"context"
"errors"
"fmt"
"path"
"strconv"
@ -1496,7 +1495,7 @@ func (h *MenuHandlers) ShowBlockedAccount(ctx context.Context, sym string, input
return res, nil
}
// loadUserContent loads the main user content in the main menu: the alias,balance associated with active voucher
// loadUserContent loads the main user content in the main menu: the alias, balance and active symbol associated with active voucher
func loadUserContent(ctx context.Context, activeSym string, balance string, alias string) (string, error) {
var content string
@ -1504,19 +1503,16 @@ func loadUserContent(ctx context.Context, activeSym string, balance string, alia
l := gotext.NewLocale(translationDir, code)
l.AddDomain("default")
balFloat, err := strconv.ParseFloat(balance, 64)
// Format the balance to 2 decimal places or default to 0.00
formattedAmount, err := store.TruncateDecimalString(balance, 2)
if err != nil {
//Only exclude ErrSyntax error to avoid returning an error if the active bal is not available yet
if !errors.Is(err, strconv.ErrSyntax) {
logg.ErrorCtxf(ctx, "failed to parse activeBal as float", "value", balance, "error", err)
return "", err
}
balFloat = 0.00
formattedAmount = "0.00"
}
// Format to 2 decimal places
balStr := fmt.Sprintf("%.2f %s", balFloat, activeSym)
// format the final output
balStr := fmt.Sprintf("%s %s", formattedAmount, activeSym)
if alias != "" {
content = l.Get("%s balance: %s\n", alias, balStr)
content = l.Get("%s\nBalance: %s\n", alias, balStr)
} else {
content = l.Get("Balance: %s\n", balStr)
}
@ -1529,7 +1525,6 @@ func (h *MenuHandlers) CheckBalance(ctx context.Context, sym string, input []byt
var (
res resource.Result
err error
alias string
content string
)
@ -1564,11 +1559,8 @@ func (h *MenuHandlers) CheckBalance(ctx context.Context, sym string, input []byt
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)
content, err = loadUserContent(ctx, string(activeSym), string(activeBal), string(accAlias))
if err != nil {
return res, err
}
@ -2092,8 +2084,9 @@ func (h *MenuHandlers) ManageVouchers(ctx context.Context, sym string, input []b
}
if activeData == nil {
logg.ErrorCtxf(ctx, "activeSym not found in vouchers", "activeSym", activeSymStr)
return res, fmt.Errorf("activeSym %s not found in vouchers", activeSymStr)
logg.ErrorCtxf(ctx, "activeSym not found in vouchers, setting the first voucher as the default", "activeSym", activeSymStr)
firstVoucher := vouchersResp[0]
activeData = &firstVoucher
}
// Scale down the balance
@ -2141,17 +2134,25 @@ func (h *MenuHandlers) GetVoucherList(ctx context.Context, sym string, input []b
// Read vouchers from the store
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)
logg.InfoCtxf(ctx, "reading voucherData in GetVoucherList", "sessionId", sessionId, "key", storedb.DATA_VOUCHER_SYMBOLS, "voucherData", voucherData)
if err != nil {
logg.ErrorCtxf(ctx, "failed to read voucherData entires with", "key", storedb.DATA_VOUCHER_SYMBOLS, "error", err)
return res, err
}
formattedData := h.ReplaceSeparatorFunc(string(voucherData))
voucherBalances, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_VOUCHER_BALANCES)
logg.InfoCtxf(ctx, "reading voucherBalances in GetVoucherList", "sessionId", sessionId, "key", storedb.DATA_VOUCHER_BALANCES, "voucherBalances", voucherBalances)
if err != nil {
logg.ErrorCtxf(ctx, "failed to read voucherData entires with", "key", storedb.DATA_VOUCHER_BALANCES, "error", err)
return res, err
}
logg.InfoCtxf(ctx, "final output for sessionId: %s", sessionId, "key", storedb.DATA_VOUCHER_SYMBOLS, "formattedData", formattedData)
formattedVoucherList := store.FormatVoucherList(ctx, string(voucherData), string(voucherBalances))
finalOutput := strings.Join(formattedVoucherList, "\n")
res.Content = string(formattedData)
logg.InfoCtxf(ctx, "final output for GetVoucherList", "sessionId", sessionId, "finalOutput", finalOutput)
res.Content = finalOutput
return res, nil
}
@ -2580,6 +2581,7 @@ func (h *MenuHandlers) RequestCustomAlias(ctx context.Context, sym string, input
}
flag_api_error, _ := h.flagManager.GetFlag("flag_api_call_error")
flag_alias_unavailable, _ := h.flagManager.GetFlag("flag_alias_unavailable")
store := h.userdataStore
aliasHint, err := store.ReadEntry(ctx, sessionId, storedb.DATA_TEMPORARY_VALUE)
@ -2603,9 +2605,19 @@ func (h *MenuHandlers) RequestCustomAlias(ctx context.Context, sym string, input
}
sanitizedInput := sanitizeAliasHint(string(input))
// Check if an alias already exists
existingAlias, err := store.ReadEntry(ctx, sessionId, storedb.DATA_SUGGESTED_ALIAS)
existingAlias, err := store.ReadEntry(ctx, sessionId, storedb.DATA_ACCOUNT_ALIAS)
if err == nil && len(existingAlias) > 0 {
logg.InfoCtxf(ctx, "Current alias", "alias", string(existingAlias))
unavailable, err := h.isAliasUnavailable(ctx, sanitizedInput)
if err == nil && unavailable {
res.FlagSet = append(res.FlagSet, flag_alias_unavailable)
res.FlagReset = append(res.FlagReset, flag_api_error)
return res, nil
}
res.FlagReset = append(res.FlagReset, flag_alias_unavailable)
// Update existing alias
aliasResult, err := h.accountService.UpdateAlias(ctx, sanitizedInput, string(publicKey))
if err != nil {
@ -2617,6 +2629,16 @@ func (h *MenuHandlers) RequestCustomAlias(ctx context.Context, sym string, input
logg.InfoCtxf(ctx, "Updated alias", "alias", alias)
} else {
logg.InfoCtxf(ctx, "Registering a new alias", "err", err)
unavailable, err := h.isAliasUnavailable(ctx, sanitizedInput)
if err == nil && unavailable {
res.FlagSet = append(res.FlagSet, flag_alias_unavailable)
res.FlagReset = append(res.FlagReset, flag_api_error)
return res, nil
}
res.FlagReset = append(res.FlagReset, flag_alias_unavailable)
// Register a new alias
aliasResult, err := h.accountService.RequestAlias(ctx, string(publicKey), sanitizedInput)
if err != nil {
@ -2627,16 +2649,18 @@ func (h *MenuHandlers) RequestCustomAlias(ctx context.Context, sym string, input
res.FlagReset = append(res.FlagReset, flag_api_error)
alias = aliasResult.Alias
logg.InfoCtxf(ctx, "Suggested alias", "alias", alias)
logg.InfoCtxf(ctx, "Registered alias", "alias", alias)
}
//Store the returned alias,wait for user to confirm it as new account alias
logg.InfoCtxf(ctx, "Final suggested alias", "alias", alias)
err = store.WriteEntry(ctx, sessionId, storedb.DATA_SUGGESTED_ALIAS, []byte(alias))
//Store the new account alias
logg.InfoCtxf(ctx, "Final registered alias", "alias", alias)
err = store.WriteEntry(ctx, sessionId, storedb.DATA_ACCOUNT_ALIAS, []byte(alias))
if err != nil {
logg.ErrorCtxf(ctx, "failed to write suggested alias", "key", storedb.DATA_SUGGESTED_ALIAS, "value", alias, "error", err)
logg.ErrorCtxf(ctx, "failed to write account alias", "key", storedb.DATA_ACCOUNT_ALIAS, "value", alias, "error", err)
return res, err
}
}
return res, nil
}
@ -2651,54 +2675,19 @@ func sanitizeAliasHint(input string) string {
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
store := h.userdataStore
func (h *MenuHandlers) isAliasUnavailable(ctx context.Context, alias string) (bool, error) {
fqdn := fmt.Sprintf("%s.%s", alias, "sarafu.eth")
logg.InfoCtxf(ctx, "Checking if the fqdn alias is taken", "fqdn", fqdn)
sessionId, ok := ctx.Value("SessionId").(string)
if !ok {
return res, fmt.Errorf("missing session")
}
suggestedAlias, err := store.ReadEntry(ctx, sessionId, storedb.DATA_SUGGESTED_ALIAS)
if err != nil && len(suggestedAlias) <= 0 {
logg.ErrorCtxf(ctx, "failed to read suggested alias", "key", storedb.DATA_SUGGESTED_ALIAS, "error", err)
return res, nil
}
res.Content = string(suggestedAlias)
return res, nil
}
// 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
logdb := h.logDb
flag_alias_set, _ := h.flagManager.GetFlag("flag_alias_set")
sessionId, ok := ctx.Value("SessionId").(string)
if !ok {
return res, fmt.Errorf("missing session")
}
newAlias, err := store.ReadEntry(ctx, sessionId, storedb.DATA_SUGGESTED_ALIAS)
aliasAddress, err := h.accountService.CheckAliasAddress(ctx, fqdn)
if err != nil {
return res, nil
return false, err
}
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)
return res, err
if len(aliasAddress.Address) > 0 {
return true, nil
}
err = logdb.WriteLogEntry(ctx, sessionId, storedb.DATA_ACCOUNT_ALIAS, []byte(newAlias))
if err != nil {
logg.DebugCtxf(ctx, "Failed to write account alias db log entry", "key", storedb.DATA_ACCOUNT_ALIAS, "value", newAlias)
}
res.FlagSet = append(res.FlagSet, flag_alias_set)
return res, nil
return false, nil
}
// ClearTemporaryValue empties the DATA_TEMPORARY_VALUE at the main menu to prevent

View File

@ -1840,20 +1840,42 @@ func TestCheckBalance(t *testing.T) {
name string
sessionId string
publicKey string
alias string
activeSym string
activeBal string
expectedResult resource.Result
expectError bool
}{
{
name: "User with no active sym",
sessionId: "session123",
publicKey: "0X98765432109",
alias: "",
activeSym: "",
activeBal: "",
expectedResult: resource.Result{Content: "Balance: 0.00 \n"},
expectError: false,
},
{
name: "User with active sym",
sessionId: "session123",
publicKey: "0X98765432109",
alias: "",
activeSym: "ETH",
activeBal: "1.5",
expectedResult: resource.Result{Content: "Balance: 1.50 ETH\n"},
expectError: false,
},
{
name: "User with active sym and alias",
sessionId: "session123",
publicKey: "0X98765432109",
alias: "user72",
activeSym: "SRF",
activeBal: "10.967",
expectedResult: resource.Result{Content: "user72 balance: 10.96 SRF\n"},
expectError: false,
},
}
for _, tt := range tests {
@ -1866,13 +1888,25 @@ func TestCheckBalance(t *testing.T) {
accountService: mockAccountService,
}
err := store.WriteEntry(ctx, tt.sessionId, storedb.DATA_ACTIVE_SYM, []byte(tt.activeSym))
if err != nil {
t.Fatal(err)
if tt.alias != "" {
err := store.WriteEntry(ctx, tt.sessionId, storedb.DATA_ACCOUNT_ALIAS, []byte(tt.alias))
if err != nil {
t.Fatal(err)
}
}
err = store.WriteEntry(ctx, tt.sessionId, storedb.DATA_ACTIVE_BAL, []byte(tt.activeBal))
if err != nil {
t.Fatal(err)
if tt.activeSym != "" {
err := store.WriteEntry(ctx, tt.sessionId, storedb.DATA_ACTIVE_SYM, []byte(tt.activeSym))
if err != nil {
t.Fatal(err)
}
}
if tt.activeBal != "" {
err := store.WriteEntry(ctx, tt.sessionId, storedb.DATA_ACTIVE_BAL, []byte(tt.activeBal))
if err != nil {
t.Fatal(err)
}
}
res, err := h.CheckBalance(ctx, "check_balance", []byte(""))
@ -2208,20 +2242,25 @@ func TestGetVoucherList(t *testing.T) {
ReplaceSeparatorFunc: mockReplaceSeparator,
}
mockSyms := []byte("1:SRF\n2:MILO")
mockSymbols := []byte("1:SRF\n2:MILO")
mockBalances := []byte("1:10.099999\n2:40.7")
// Put voucher sym data from the store
err := store.WriteEntry(ctx, sessionId, storedb.DATA_VOUCHER_SYMBOLS, mockSyms)
// Put voucher symnols and balances data to the store
err := store.WriteEntry(ctx, sessionId, storedb.DATA_VOUCHER_SYMBOLS, mockSymbols)
if err != nil {
t.Fatal(err)
}
err = store.WriteEntry(ctx, sessionId, storedb.DATA_VOUCHER_BALANCES, mockBalances)
if err != nil {
t.Fatal(err)
}
expectedSyms := []byte("1: SRF\n2: MILO")
expectedList := []byte("1: SRF 10.09\n2: MILO 40.70")
res, err := h.GetVoucherList(ctx, "", []byte(""))
assert.NoError(t, err)
assert.Equal(t, res.Content, string(expectedSyms))
assert.Equal(t, res.Content, string(expectedList))
}
func TestViewVoucher(t *testing.T) {

View File

@ -130,8 +130,6 @@ func (ls *LocalHandlerService) GetHandler(accountService remote.AccountService)
ls.DbRs.AddLocalFunc("clear_temporary_value", appHandlers.ClearTemporaryValue)
ls.DbRs.AddLocalFunc("reset_invalid_pin", appHandlers.ResetInvalidPIN)
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.DbRs.AddLocalFunc("reset_api_call_failure", appHandlers.ResetApiCallFailure)
ls.DbRs.AddLocalFunc("swap_to_list", appHandlers.LoadSwapToList)

View File

@ -1,5 +1,3 @@
LOAD confirm_new_alias 0
RELOAD confirm_new_alias
MOUT back 0
MOUT quit 9
HALT

View File

@ -1,2 +0,0 @@
Your full alias will be: {{.get_suggested_alias}}
Please enter your PIN to confirm:

View File

@ -1,12 +0,0 @@
LOAD reset_invalid_pin 6
RELOAD reset_invalid_pin
LOAD get_suggested_alias 0
RELOAD get_suggested_alias
MAP get_suggested_alias
MOUT back 0
HALT
INCMP _ 0
RELOAD authorize_account
CATCH incorrect_pin flag_incorrect_pin 1
CATCH invalid_pin flag_invalid_pin 1
CATCH update_alias flag_allow_update 1

View File

@ -1,2 +0,0 @@
Lakabu yako kamili itakuwa: {{.get_suggested_alias}}
Tafadhali weka PIN yako ili kuthibitisha:

View File

@ -0,0 +1 @@
Home

View File

@ -0,0 +1 @@
Mwanzo

View File

@ -1,3 +1,7 @@
LOAD reset_account_authorized 0
LOAD reset_incorrect_pin 0
CATCH incorrect_pin flag_incorrect_pin 1
CATCH pin_entry flag_account_authorized 0
LOAD get_current_profile_info 0
MAP get_current_profile_info
MOUT back 0
@ -5,5 +9,7 @@ HALT
INCMP _ 0
LOAD request_custom_alias 0
RELOAD request_custom_alias
MAP request_custom_alias
CATCH unavailable_alias flag_alias_unavailable 1
CATCH api_failure flag_api_call_error 1
INCMP confirm_new_alias *
INCMP alias_updated *

View File

@ -34,4 +34,4 @@ flag,flag_alias_set,40,this is set when an account alias has been assigned to a
flag,flag_account_pin_reset,41,this is set on an account when an admin triggers a PIN reset for themflag,flag_incorrect_pool,39,this is set when the user selects an invalid pool
flag,flag_incorrect_pool,42,this is set when the user selects an invalid pool
flag,flag_low_swap_amount,43,this is set when the swap max limit is less than 0.1
flag,flag_alias_unavailable,44,this is set when the preferred alias is not available

Can't render this file because it has a wrong number of fields in line 34.

View File

@ -0,0 +1 @@
The alias {{.request_custom_alias}} isn't available

View File

@ -0,0 +1,8 @@
MAP request_custom_alias
MOUT back 0
MOUT home 9
MOUT quit 99
HALT
INCMP _ 0
INCMP ^ 9
INCMP quit 99

View File

@ -0,0 +1 @@
Lakabu ulilochagua {{.request_custom_alias}} halipatikani

View File

@ -182,3 +182,30 @@ func UpdateVoucherData(ctx context.Context, store DataStore, sessionId string, d
return nil
}
// FormatVoucherList combines the voucher symbols with their balances (SRF 0.11)
func FormatVoucherList(ctx context.Context, symbolsData, balancesData string) []string {
symbols := strings.Split(symbolsData, "\n")
balances := strings.Split(balancesData, "\n")
var combined []string
for i := 0; i < len(symbols) && i < len(balances); i++ {
symbolParts := strings.SplitN(symbols[i], ":", 2)
balanceParts := strings.SplitN(balances[i], ":", 2)
if len(symbolParts) == 2 && len(balanceParts) == 2 {
index := strings.TrimSpace(symbolParts[0])
symbol := strings.TrimSpace(symbolParts[1])
rawBalance := strings.TrimSpace(balanceParts[1])
formattedBalance, err := TruncateDecimalString(rawBalance, 2)
if err != nil {
logg.ErrorCtxf(ctx, "failed to format balance", "balance", rawBalance, "error", err)
formattedBalance = rawBalance
}
combined = append(combined, fmt.Sprintf("%s: %s %s", index, symbol, formattedBalance))
}
}
return combined
}