|
|
|
|
@@ -193,8 +193,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 {
|
|
|
|
|
@@ -1460,7 +1461,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 +1483,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 +1493,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 +1501,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 +1940,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 +1956,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 +1972,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 +2073,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,25 +2081,44 @@ 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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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 := h.prefixDb.Get(ctx, storedb.ToBytes(storedb.DATA_VOUCHER_SYMBOLS))
|
|
|
|
|
voucherData, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_VOUCHER_SYMBOLS)
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
// 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))
|
|
|
|
|
|
|
|
|
|
@@ -2142,7 +2148,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)
|
|
|
|
|
}
|
|
|
|
|
|