|
|
|
|
@@ -8,7 +8,6 @@ import (
|
|
|
|
|
"path"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
"unicode"
|
|
|
|
|
|
|
|
|
|
"gopkg.in/leonelquinteros/gotext.v1"
|
|
|
|
|
|
|
|
|
|
@@ -194,9 +193,8 @@ 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_ACCOUNT_ALIAS: "",
|
|
|
|
|
storedb.DATA_TRACKING_ID: trackingId,
|
|
|
|
|
storedb.DATA_PUBLIC_KEY: publicKey,
|
|
|
|
|
}
|
|
|
|
|
store := h.userdataStore
|
|
|
|
|
for key, value := range data {
|
|
|
|
|
@@ -243,28 +241,6 @@ 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
|
|
|
|
|
@@ -1144,12 +1120,7 @@ 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
|
|
|
|
|
}
|
|
|
|
|
alias := string(profileInfo)
|
|
|
|
|
if alias == "" {
|
|
|
|
|
res.Content = defaultValue
|
|
|
|
|
} else {
|
|
|
|
|
res.Content = alias
|
|
|
|
|
}
|
|
|
|
|
res.Content = string(profileInfo)
|
|
|
|
|
default:
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
@@ -1193,10 +1164,8 @@ 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 && alias != "" {
|
|
|
|
|
if alias != defaultValue {
|
|
|
|
|
alias = strings.Split(alias, ".")[0]
|
|
|
|
|
} else {
|
|
|
|
|
alias = defaultValue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Construct the full name
|
|
|
|
|
@@ -1277,10 +1246,20 @@ 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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -1481,7 +1460,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
|
|
|
|
|
}
|
|
|
|
|
@@ -1503,24 +1482,6 @@ func (h *MenuHandlers) CheckBalance(ctx context.Context, sym string, input []byt
|
|
|
|
|
|
|
|
|
|
store := h.userdataStore
|
|
|
|
|
|
|
|
|
|
// get the active sym and active balance
|
|
|
|
|
activeSym, err := store.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_SYM)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logg.InfoCtxf(ctx, "could not find the activeSym in checkBalance:", "err", err)
|
|
|
|
|
if !db.IsNotFound(err) {
|
|
|
|
|
logg.ErrorCtxf(ctx, "failed to read activeSym entry with", "key", storedb.DATA_ACTIVE_SYM, "error", err)
|
|
|
|
|
return res, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
activeBal, err := store.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_BAL)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if !db.IsNotFound(err) {
|
|
|
|
|
logg.ErrorCtxf(ctx, "failed to read activeBal entry with", "key", storedb.DATA_ACTIVE_BAL, "error", err)
|
|
|
|
|
return res, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
accAlias, err := store.ReadEntry(ctx, sessionId, storedb.DATA_ACCOUNT_ALIAS)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if !db.IsNotFound(err) {
|
|
|
|
|
@@ -1531,6 +1492,27 @@ func (h *MenuHandlers) CheckBalance(ctx context.Context, sym string, input []byt
|
|
|
|
|
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 {
|
|
|
|
|
fmt.Println("could not find the activeSym in checkBalance:", err)
|
|
|
|
|
if !db.IsNotFound(err) {
|
|
|
|
|
fmt.Println("the err:", err)
|
|
|
|
|
logg.ErrorCtxf(ctx, "failed to read activeSym entry with", "key", storedb.DATA_ACTIVE_SYM, "error", err)
|
|
|
|
|
return res, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Println("The active data in CheckBalance:", string(activeSym))
|
|
|
|
|
|
|
|
|
|
activeBal, err := store.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_BAL)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if !db.IsNotFound(err) {
|
|
|
|
|
logg.ErrorCtxf(ctx, "failed to read activeBal entry with", "key", storedb.DATA_ACTIVE_BAL, "error", err)
|
|
|
|
|
return res, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
content, err = loadUserContent(ctx, string(activeSym), string(activeBal), alias)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return res, err
|
|
|
|
|
@@ -1960,8 +1942,12 @@ 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
|
|
|
|
|
_, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_SYM)
|
|
|
|
|
activeSym, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_SYM)
|
|
|
|
|
fmt.Println("The activeSym in SetDefaultVoucher:", string(activeSym))
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println("The err", err)
|
|
|
|
|
fmt.Println("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 {
|
|
|
|
|
@@ -1972,12 +1958,25 @@ func (h *MenuHandlers) SetDefaultVoucher(ctx context.Context, sym string, input
|
|
|
|
|
// Fetch vouchers from the API using the public key
|
|
|
|
|
vouchersResp, err := h.accountService.FetchVouchers(ctx, string(publicKey))
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println("setting the flag_no_active_voucher")
|
|
|
|
|
res.FlagSet = append(res.FlagSet, flag_no_active_voucher)
|
|
|
|
|
return res, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// vouchersResp := []dataserviceapi.TokenHoldings{
|
|
|
|
|
// {
|
|
|
|
|
// ContractAddress: "0xcB453B742Bc8BE1BAa03Ce1927e287788d0aF065",
|
|
|
|
|
// TokenSymbol: "PES",
|
|
|
|
|
// TokenDecimals: "6",
|
|
|
|
|
// Balance: "10000000",
|
|
|
|
|
// },
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
fmt.Println("fetched user vouchers in SetDefaultVoucher", "public_key", string(publicKey), "vouchers", vouchersResp)
|
|
|
|
|
|
|
|
|
|
// Return if there is no voucher
|
|
|
|
|
if len(vouchersResp) == 0 {
|
|
|
|
|
fmt.Println("setting the flag_no_active_voucher in SetDefaultVoucher")
|
|
|
|
|
res.FlagSet = append(res.FlagSet, flag_no_active_voucher)
|
|
|
|
|
return res, nil
|
|
|
|
|
}
|
|
|
|
|
@@ -1992,10 +1991,14 @@ func (h *MenuHandlers) SetDefaultVoucher(ctx context.Context, sym string, input
|
|
|
|
|
// Scale down the balance
|
|
|
|
|
scaledBalance := store.ScaleDownBalance(defaultBal, defaultDec)
|
|
|
|
|
|
|
|
|
|
fmt.Println("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 {
|
|
|
|
|
fmt.Println("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
|
|
|
|
|
}
|
|
|
|
|
@@ -2021,7 +2024,7 @@ func (h *MenuHandlers) SetDefaultVoucher(ctx context.Context, sym string, input
|
|
|
|
|
return res, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logg.ErrorCtxf(ctx, "failed to read activeSym entry with", "key", storedb.DATA_ACTIVE_SYM, "error", err)
|
|
|
|
|
fmt.Println("failed to read activeSym entry with", "key", storedb.DATA_ACTIVE_SYM, "error", err)
|
|
|
|
|
return res, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -2052,7 +2055,16 @@ func (h *MenuHandlers) CheckVouchers(ctx context.Context, sym string, input []by
|
|
|
|
|
return res, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logg.InfoCtxf(ctx, "fetched user vouchers", "public_key", string(publicKey), "vouchers", vouchersResp)
|
|
|
|
|
// vouchersResp := []dataserviceapi.TokenHoldings{
|
|
|
|
|
// {
|
|
|
|
|
// ContractAddress: "0xcB453B742Bc8BE1BAa03Ce1927e287788d0aF065",
|
|
|
|
|
// TokenSymbol: "PES",
|
|
|
|
|
// TokenDecimals: "6",
|
|
|
|
|
// Balance: "10000000",
|
|
|
|
|
// },
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
fmt.Println("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)
|
|
|
|
|
@@ -2089,10 +2101,12 @@ 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))
|
|
|
|
|
fmt.Println(ctx, "The active data in CheckVouchers:", "activeSym", string(activeSym), string(activeBal), string(activeAddr))
|
|
|
|
|
|
|
|
|
|
data := store.ProcessVouchers(vouchersResp)
|
|
|
|
|
|
|
|
|
|
fmt.Println(ctx, "The data in CheckVouchers:", "data", data)
|
|
|
|
|
|
|
|
|
|
// Store all voucher data
|
|
|
|
|
dataMap := map[storedb.DataTyp]string{
|
|
|
|
|
storedb.DATA_VOUCHER_SYMBOLS: data.Symbols,
|
|
|
|
|
@@ -2101,51 +2115,28 @@ 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 {
|
|
|
|
|
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
|
|
|
|
|
if err := h.prefixDb.Put(ctx, []byte(storedb.ToBytes(key)), []byte(value)); err != nil {
|
|
|
|
|
return res, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
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)
|
|
|
|
|
voucherData, err := h.prefixDb.Get(ctx, storedb.ToBytes(storedb.DATA_VOUCHER_SYMBOLS))
|
|
|
|
|
if err != nil {
|
|
|
|
|
logg.ErrorCtxf(ctx, "failed to read voucherData entires with", "key", storedb.DATA_VOUCHER_SYMBOLS, "error", err)
|
|
|
|
|
logg.ErrorCtxf(ctx, "Failed to read the voucherData from prefixDb", "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
|
|
|
|
|
@@ -2172,7 +2163,7 @@ func (h *MenuHandlers) ViewVoucher(ctx context.Context, sym string, input []byte
|
|
|
|
|
return res, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
metadata, err := store.GetVoucherData(ctx, h.userdataStore, sessionId, inputStr)
|
|
|
|
|
metadata, err := store.GetVoucherData(ctx, h.prefixDb, inputStr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return res, fmt.Errorf("failed to retrieve voucher data: %v", err)
|
|
|
|
|
}
|
|
|
|
|
@@ -2532,8 +2523,7 @@ func (h *MenuHandlers) RequestCustomAlias(ctx context.Context, sym string, input
|
|
|
|
|
return res, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
sanitizedInput := sanitizeAliasHint(string(input))
|
|
|
|
|
aliasResult, err := h.accountService.RequestAlias(ctx, string(pubKey), sanitizedInput)
|
|
|
|
|
aliasResult, err := h.accountService.RequestAlias(ctx, string(pubKey), string(input))
|
|
|
|
|
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())
|
|
|
|
|
@@ -2550,17 +2540,6 @@ 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
|
|
|
|
|
|