Compare commits

...

13 Commits

Author SHA1 Message Date
Carlosokumu
c81b17994a remove redundant cast on sanitized input 2025-03-24 16:03:30 +03:00
Carlosokumu
64b42b92ec Merge branch 'master' into fix-multiple-alias-requests 2025-03-24 16:00:20 +03:00
Carlosokumu
493f64157b sanitize alias hint before request 2025-03-24 15:59:13 +03:00
Carlosokumu
f6c9d54a65 register check-account_created handler
Some checks failed
release / docker (push) Has been cancelled
2025-03-24 12:39:03 +03:00
Carlosokumu
74c82de472 add check for if public key exists,then explicitly set flag_account_created
Some checks failed
release / docker (push) Has been cancelled
2025-03-24 12:29:40 +03:00
Carlosokumu
ddb8c6e748 add lowercase balance text translation 2025-03-22 18:39:05 +03:00
Carlosokumu
87ebd0029b chore: show default value when profile info is not set 2025-03-22 18:38:35 +03:00
Carlosokumu
b941cf2562 chore: match profile text prompt 2025-03-22 18:37:27 +03:00
Carlosokumu
1c8bda5ded fix(alias): remove alias requests made when a profile item is edited,show a default value when an alias is not set 2025-03-22 09:49:19 +03:00
alfred-mk
30cb800450 Added log lines for voucherlist
Some checks failed
release / docker (push) Has been cancelled
2025-03-21 13:58:30 +03:00
alfred-mk
680a1e9681 Use userstore for the voucher data instead of subprefixdb
Some checks failed
release / docker (push) Has been cancelled
2025-03-21 13:46:13 +03:00
Carlosokumu
b327b569fb fix failing test 2025-03-21 09:05:51 +03:00
alfred-mk
445ca0d0ff Add a default alias to remove bug in retrieving the data
Some checks failed
release / docker (push) Has been cancelled
2025-03-20 22:34:04 +03:00
8 changed files with 105 additions and 56 deletions

View File

@@ -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
@@ -1941,12 +1960,8 @@ 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))
_, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_SYM)
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)
if db.IsNotFound(err) {
publicKey, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_PUBLIC_KEY)
if err != nil {
@@ -1961,8 +1976,6 @@ func (h *MenuHandlers) SetDefaultVoucher(ctx context.Context, sym string, input
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)
@@ -1979,14 +1992,10 @@ 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
}
@@ -2084,8 +2093,6 @@ func (h *MenuHandlers) CheckVouchers(ctx context.Context, sym string, input []by
data := store.ProcessVouchers(vouchersResp)
logg.InfoCtxf(ctx, "The data in CheckVouchers:", "data", data)
// Store all voucher data
dataMap := map[storedb.DataTyp]string{
storedb.DATA_VOUCHER_SYMBOLS: data.Symbols,
@@ -2094,28 +2101,51 @@ 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
logg.InfoCtxf(ctx, "Writing data entry for sessionId: %s", sessionId, "key", key, "value", value)
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
}
}
// for key, value := range dataMap {
// if err := h.prefixDb.Put(ctx, []byte(storedb.ToBytes(key)), []byte(value)); err != nil {
// return res, nil
// }
// }
return res, nil
}
// 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
// Read vouchers from the store
voucherData, err := h.prefixDb.Get(ctx, storedb.ToBytes(storedb.DATA_VOUCHER_SYMBOLS))
if err != nil {
logg.ErrorCtxf(ctx, "Failed to read the voucherData from prefixDb", "error", err)
return res, err
sessionId, ok := ctx.Value("SessionId").(string)
if !ok {
return res, fmt.Errorf("missing session")
}
userStore := h.userdataStore
// 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)
if err != nil {
logg.ErrorCtxf(ctx, "failed to read voucherData entires with", "key", storedb.DATA_VOUCHER_SYMBOLS, "error", err)
return res, err
}
// voucherData, err := h.prefixDb.Get(ctx, storedb.ToBytes(storedb.DATA_VOUCHER_SYMBOLS))
// if err != nil {
// logg.ErrorCtxf(ctx, "Failed to read the voucherData from prefixDb", "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 +2172,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,7 +2532,8 @@ 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())
@@ -2519,6 +2550,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

View File

@@ -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,
},
}

View File

@@ -128,6 +128,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

View File

@@ -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"

View File

@@ -1,2 +1,2 @@
Current alias: {{.get_current_profile_info}}
Edit my alias:
Enter your preferred alias::

View File

@@ -1,2 +1,2 @@
Lakabu ya sasa: {{.get_current_profile_info}}
Badilisha Lakabu yangu:
Weka lakabu unalopendelea::

View File

@@ -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

View File

@@ -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)
}