Compare commits
2 Commits
master
...
self-pin-r
Author | SHA1 | Date | |
---|---|---|---|
2c671bc0d4 | |||
c5673b339b |
@ -21,7 +21,7 @@ RUN make VISE_PATH=/build/go-vise -B
|
|||||||
WORKDIR /build/sarafu-vise
|
WORKDIR /build/sarafu-vise
|
||||||
RUN echo "Building on $BUILDPLATFORM, building for $TARGETPLATFORM"
|
RUN echo "Building on $BUILDPLATFORM, building for $TARGETPLATFORM"
|
||||||
RUN go mod download
|
RUN go mod download
|
||||||
RUN go build -tags logdebug,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
|
FROM debian:bookworm-slim
|
||||||
|
|
||||||
|
2
go.mod
2
go.mod
@ -3,7 +3,7 @@ module git.grassecon.net/grassrootseconomics/sarafu-vise
|
|||||||
go 1.23.4
|
go 1.23.4
|
||||||
|
|
||||||
require (
|
require (
|
||||||
git.defalsify.org/vise.git v0.3.2-0.20250401123711-d481b04a6805
|
git.defalsify.org/vise.git v0.3.1
|
||||||
git.grassecon.net/grassrootseconomics/common v0.0.0-20250121134736-ba8cbbccea7d
|
git.grassecon.net/grassrootseconomics/common v0.0.0-20250121134736-ba8cbbccea7d
|
||||||
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250310093912-8145b4bd004b
|
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250310093912-8145b4bd004b
|
||||||
git.grassecon.net/grassrootseconomics/visedriver v0.9.0-beta.2
|
git.grassecon.net/grassrootseconomics/visedriver v0.9.0-beta.2
|
||||||
|
2
go.sum
2
go.sum
@ -1,7 +1,5 @@
|
|||||||
git.defalsify.org/vise.git v0.3.1 h1:A6FhMcur09ft/JzUPGXR+KpA17fltfeBnasyvLMZmq4=
|
git.defalsify.org/vise.git v0.3.1 h1:A6FhMcur09ft/JzUPGXR+KpA17fltfeBnasyvLMZmq4=
|
||||||
git.defalsify.org/vise.git v0.3.1/go.mod h1:jyBMe1qTYUz3mmuoC9JQ/TvFeW0vTanCUcPu3H8p4Ck=
|
git.defalsify.org/vise.git v0.3.1/go.mod h1:jyBMe1qTYUz3mmuoC9JQ/TvFeW0vTanCUcPu3H8p4Ck=
|
||||||
git.defalsify.org/vise.git v0.3.2-0.20250401123711-d481b04a6805 h1:FnT39aqXcP5YWhwPDBABopSjCu2SlbPFoOVitSpAVxU=
|
|
||||||
git.defalsify.org/vise.git v0.3.2-0.20250401123711-d481b04a6805/go.mod h1:jyBMe1qTYUz3mmuoC9JQ/TvFeW0vTanCUcPu3H8p4Ck=
|
|
||||||
git.grassecon.net/grassrootseconomics/common v0.0.0-20250121134736-ba8cbbccea7d h1:5mzLas+jxTUtusOKx4XvU+n2QvrV/mH17MnJRy46siQ=
|
git.grassecon.net/grassrootseconomics/common v0.0.0-20250121134736-ba8cbbccea7d h1:5mzLas+jxTUtusOKx4XvU+n2QvrV/mH17MnJRy46siQ=
|
||||||
git.grassecon.net/grassrootseconomics/common v0.0.0-20250121134736-ba8cbbccea7d/go.mod h1:wgQJZGIS6QuNLHqDhcsvehsbn5PvgV7aziRebMnJi60=
|
git.grassecon.net/grassrootseconomics/common v0.0.0-20250121134736-ba8cbbccea7d/go.mod h1:wgQJZGIS6QuNLHqDhcsvehsbn5PvgV7aziRebMnJi60=
|
||||||
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250310093912-8145b4bd004b h1:xiTpaqWWoF5qcnarY/9ZkT6aVdnKwqztb2gzIahJn4w=
|
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250310093912-8145b4bd004b h1:xiTpaqWWoF5qcnarY/9ZkT6aVdnKwqztb2gzIahJn4w=
|
||||||
|
@ -245,7 +245,6 @@ func (h *MenuHandlers) CreateAccount(ctx context.Context, sym string, input []by
|
|||||||
|
|
||||||
func (h *MenuHandlers) CheckAccountCreated(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
func (h *MenuHandlers) CheckAccountCreated(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||||
var res resource.Result
|
var res resource.Result
|
||||||
flag_language_set, _ := h.flagManager.GetFlag("flag_language_set")
|
|
||||||
flag_account_created, _ := h.flagManager.GetFlag("flag_account_created")
|
flag_account_created, _ := h.flagManager.GetFlag("flag_account_created")
|
||||||
|
|
||||||
store := h.userdataStore
|
store := h.userdataStore
|
||||||
@ -257,17 +256,11 @@ func (h *MenuHandlers) CheckAccountCreated(ctx context.Context, sym string, inpu
|
|||||||
|
|
||||||
_, err := store.ReadEntry(ctx, sessionId, storedb.DATA_PUBLIC_KEY)
|
_, err := store.ReadEntry(ctx, sessionId, storedb.DATA_PUBLIC_KEY)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if db.IsNotFound(err) {
|
if !db.IsNotFound(err) {
|
||||||
// reset major flags
|
return res, err
|
||||||
res.FlagReset = append(res.FlagReset, flag_language_set)
|
|
||||||
res.FlagReset = append(res.FlagReset, flag_account_created)
|
|
||||||
|
|
||||||
return res, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
res.FlagSet = append(res.FlagSet, flag_account_created)
|
res.FlagSet = append(res.FlagSet, flag_account_created)
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
@ -280,18 +273,29 @@ func (h *MenuHandlers) ResetValidPin(ctx context.Context, sym string, input []by
|
|||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// CheckBlockedStatus resets the account blocked flag if the PIN attempts have been reset by an admin.
|
// CheckBlockedStatus:
|
||||||
|
// 1. Checks whether the DATA_SELF_PIN_RESET is 1 and sets the flag_account_pin_reset
|
||||||
|
// 2. resets the account blocked flag if the PIN attempts have been reset by an admin.
|
||||||
func (h *MenuHandlers) CheckBlockedStatus(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
func (h *MenuHandlers) CheckBlockedStatus(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||||
var res resource.Result
|
var res resource.Result
|
||||||
store := h.userdataStore
|
store := h.userdataStore
|
||||||
|
|
||||||
flag_account_blocked, _ := h.flagManager.GetFlag("flag_account_blocked")
|
flag_account_blocked, _ := h.flagManager.GetFlag("flag_account_blocked")
|
||||||
|
flag_account_pin_reset, _ := h.flagManager.GetFlag("flag_account_pin_reset")
|
||||||
|
|
||||||
sessionId, ok := ctx.Value("SessionId").(string)
|
sessionId, ok := ctx.Value("SessionId").(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
return res, fmt.Errorf("missing session")
|
return res, fmt.Errorf("missing session")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
selfPinReset, err := store.ReadEntry(ctx, sessionId, storedb.DATA_SELF_PIN_RESET)
|
||||||
|
if err == nil {
|
||||||
|
pinResetValue, _ := strconv.ParseUint(string(selfPinReset), 0, 64)
|
||||||
|
if pinResetValue == 1 {
|
||||||
|
res.FlagSet = append(res.FlagSet, flag_account_pin_reset)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
currentWrongPinAttempts, err := store.ReadEntry(ctx, sessionId, storedb.DATA_INCORRECT_PIN_ATTEMPTS)
|
currentWrongPinAttempts, err := store.ReadEntry(ctx, sessionId, storedb.DATA_INCORRECT_PIN_ATTEMPTS)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !db.IsNotFound(err) {
|
if !db.IsNotFound(err) {
|
||||||
@ -300,7 +304,6 @@ func (h *MenuHandlers) CheckBlockedStatus(ctx context.Context, sym string, input
|
|||||||
}
|
}
|
||||||
|
|
||||||
pinAttemptsValue, _ := strconv.ParseUint(string(currentWrongPinAttempts), 0, 64)
|
pinAttemptsValue, _ := strconv.ParseUint(string(currentWrongPinAttempts), 0, 64)
|
||||||
|
|
||||||
if pinAttemptsValue == 0 {
|
if pinAttemptsValue == 0 {
|
||||||
res.FlagReset = append(res.FlagReset, flag_account_blocked)
|
res.FlagReset = append(res.FlagReset, flag_account_blocked)
|
||||||
return res, nil
|
return res, nil
|
||||||
@ -637,6 +640,7 @@ func (h *MenuHandlers) ResetUnregisteredNumber(ctx context.Context, sym string,
|
|||||||
|
|
||||||
// ValidateBlockedNumber performs validation of phone numbers, specifically for blocked numbers in the system.
|
// ValidateBlockedNumber performs validation of phone numbers, specifically for blocked numbers in the system.
|
||||||
// It checks phone number format and verifies registration status.
|
// It checks phone number format and verifies registration status.
|
||||||
|
// If valid, it sets DATA_SELF_PIN_RESET as 1 on the account
|
||||||
func (h *MenuHandlers) ValidateBlockedNumber(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
func (h *MenuHandlers) ValidateBlockedNumber(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||||
var res resource.Result
|
var res resource.Result
|
||||||
var err error
|
var err error
|
||||||
@ -672,10 +676,18 @@ func (h *MenuHandlers) ValidateBlockedNumber(ctx context.Context, sym string, in
|
|||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = store.WriteEntry(ctx, sessionId, storedb.DATA_BLOCKED_NUMBER, []byte(formattedNumber))
|
err = store.WriteEntry(ctx, sessionId, storedb.DATA_BLOCKED_NUMBER, []byte(formattedNumber))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// set the DATA_SELF_PIN_RESET for the account
|
||||||
|
err = store.WriteEntry(ctx, formattedNumber, storedb.DATA_SELF_PIN_RESET, []byte("1"))
|
||||||
|
if err != nil {
|
||||||
|
return res, nil
|
||||||
|
}
|
||||||
|
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1953,11 +1965,9 @@ func (h *MenuHandlers) InitiateTransaction(ctx context.Context, sym string, inpu
|
|||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ManageVouchers retrieves the token holdings from the API using the "PublicKey" and
|
// SetDefaultVoucher retrieves the current vouchers
|
||||||
// 1. sets the first as the default voucher if no active voucher is set.
|
// and sets the first as the default voucher, if no active voucher is set.
|
||||||
// 2. Stores list of vouchers
|
func (h *MenuHandlers) SetDefaultVoucher(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||||
// 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
|
var res resource.Result
|
||||||
userStore := h.userdataStore
|
userStore := h.userdataStore
|
||||||
|
|
||||||
@ -1968,31 +1978,30 @@ func (h *MenuHandlers) ManageVouchers(ctx context.Context, sym string, input []b
|
|||||||
|
|
||||||
flag_no_active_voucher, _ := h.flagManager.GetFlag("flag_no_active_voucher")
|
flag_no_active_voucher, _ := h.flagManager.GetFlag("flag_no_active_voucher")
|
||||||
|
|
||||||
publicKey, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_PUBLIC_KEY)
|
// check if the user has an active sym
|
||||||
if err != nil {
|
_, err := userStore.ReadEntry(ctx, sessionId, 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 err != nil {
|
||||||
if db.IsNotFound(err) {
|
if db.IsNotFound(err) {
|
||||||
// No active voucher, set the first one as default
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
firstVoucher := vouchersResp[0]
|
firstVoucher := vouchersResp[0]
|
||||||
defaultSym := firstVoucher.TokenSymbol
|
defaultSym := firstVoucher.TokenSymbol
|
||||||
defaultBal := firstVoucher.Balance
|
defaultBal := firstVoucher.Balance
|
||||||
@ -2002,27 +2011,71 @@ func (h *MenuHandlers) ManageVouchers(ctx context.Context, sym string, input []b
|
|||||||
// Scale down the balance
|
// Scale down the balance
|
||||||
scaledBalance := store.ScaleDownBalance(defaultBal, defaultDec)
|
scaledBalance := store.ScaleDownBalance(defaultBal, defaultDec)
|
||||||
|
|
||||||
firstVoucherMap := map[storedb.DataTyp]string{
|
// TODO: implement atomic transaction
|
||||||
storedb.DATA_ACTIVE_SYM: defaultSym,
|
// set the active symbol
|
||||||
storedb.DATA_ACTIVE_BAL: scaledBalance,
|
err = userStore.WriteEntry(ctx, sessionId, storedb.DATA_ACTIVE_SYM, []byte(defaultSym))
|
||||||
storedb.DATA_ACTIVE_DECIMAL: defaultDec,
|
if err != nil {
|
||||||
storedb.DATA_ACTIVE_ADDRESS: defaultAddr,
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
for key, value := range firstVoucherMap {
|
return res, nil
|
||||||
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
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
// Update active voucher balance
|
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 {
|
||||||
activeSymStr := string(activeSym)
|
activeSymStr := string(activeSym)
|
||||||
|
|
||||||
// Find the matching voucher data
|
// Find the matching voucher data
|
||||||
@ -2052,8 +2105,14 @@ func (h *MenuHandlers) ManageVouchers(ctx context.Context, sym string, input []b
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Store all voucher data
|
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)
|
data := store.ProcessVouchers(vouchersResp)
|
||||||
|
|
||||||
|
// Store all voucher data
|
||||||
dataMap := map[storedb.DataTyp]string{
|
dataMap := map[storedb.DataTyp]string{
|
||||||
storedb.DATA_VOUCHER_SYMBOLS: data.Symbols,
|
storedb.DATA_VOUCHER_SYMBOLS: data.Symbols,
|
||||||
storedb.DATA_VOUCHER_BALANCES: data.Balances,
|
storedb.DATA_VOUCHER_BALANCES: data.Balances,
|
||||||
@ -2063,6 +2122,7 @@ func (h *MenuHandlers) ManageVouchers(ctx context.Context, sym string, input []b
|
|||||||
|
|
||||||
// Write data entries
|
// Write data entries
|
||||||
for key, value := range dataMap {
|
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 {
|
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)
|
logg.ErrorCtxf(ctx, "Failed to write data entry for sessionId: %s", sessionId, "key", key, "error", err)
|
||||||
continue
|
continue
|
||||||
|
@ -1990,46 +1990,36 @@ func TestFetchCommunityBalance(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestManageVouchers(t *testing.T) {
|
func TestSetDefaultVoucher(t *testing.T) {
|
||||||
sessionId := "session123"
|
sessionId := "session123"
|
||||||
publicKey := "0X13242618721"
|
|
||||||
|
|
||||||
ctx, store := InitializeTestStore(t)
|
ctx, store := InitializeTestStore(t)
|
||||||
ctx = context.WithValue(ctx, "SessionId", sessionId)
|
ctx = context.WithValue(ctx, "SessionId", sessionId)
|
||||||
|
|
||||||
fm, err := NewFlagManager(flagsPath)
|
fm, err := NewFlagManager(flagsPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Logf(err.Error())
|
||||||
}
|
}
|
||||||
flag_no_active_voucher, err := fm.GetFlag("flag_no_active_voucher")
|
flag_no_active_voucher, err := fm.GetFlag("flag_no_active_voucher")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Logf(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
err = store.WriteEntry(ctx, sessionId, storedb.DATA_PUBLIC_KEY, []byte(publicKey))
|
publicKey := "0X13242618721"
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
vouchersResp []dataserviceapi.TokenHoldings
|
vouchersResp []dataserviceapi.TokenHoldings
|
||||||
storedActiveVoucher string
|
expectedResult resource.Result
|
||||||
expectedVoucherSymbols []byte
|
|
||||||
expectedUpdatedAddress []byte
|
|
||||||
expectedResult resource.Result
|
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "No vouchers available",
|
name: "Test no vouchers available",
|
||||||
vouchersResp: []dataserviceapi.TokenHoldings{},
|
vouchersResp: []dataserviceapi.TokenHoldings{},
|
||||||
expectedVoucherSymbols: []byte(""),
|
|
||||||
expectedUpdatedAddress: []byte(""),
|
|
||||||
expectedResult: resource.Result{
|
expectedResult: resource.Result{
|
||||||
FlagSet: []uint32{flag_no_active_voucher},
|
FlagSet: []uint32{flag_no_active_voucher},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "Set default voucher when no active voucher is set",
|
name: "Test set default voucher when no active voucher is set",
|
||||||
vouchersResp: []dataserviceapi.TokenHoldings{
|
vouchersResp: []dataserviceapi.TokenHoldings{
|
||||||
{
|
{
|
||||||
ContractAddress: "0x123",
|
ContractAddress: "0x123",
|
||||||
@ -2038,24 +2028,7 @@ func TestManageVouchers(t *testing.T) {
|
|||||||
Balance: "100",
|
Balance: "100",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
expectedVoucherSymbols: []byte("1:TOKEN1"),
|
expectedResult: resource.Result{},
|
||||||
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},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2069,41 +2042,85 @@ func TestManageVouchers(t *testing.T) {
|
|||||||
flagManager: fm,
|
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)
|
mockAccountService.On("FetchVouchers", string(publicKey)).Return(tt.vouchersResp, nil)
|
||||||
|
|
||||||
// Store active voucher if needed
|
res, err := h.SetDefaultVoucher(ctx, "set_default_voucher", []byte("some-input"))
|
||||||
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.NoError(t, err)
|
||||||
assert.Equal(t, tt.expectedResult, res)
|
|
||||||
|
|
||||||
if tt.storedActiveVoucher != "" {
|
assert.Equal(t, res, tt.expectedResult, "Expected result should be equal to the actual result")
|
||||||
// Validate stored voucher symbols
|
|
||||||
voucherData, err := store.ReadEntry(ctx, sessionId, storedb.DATA_VOUCHER_SYMBOLS)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, tt.expectedVoucherSymbols, voucherData)
|
|
||||||
|
|
||||||
// Validate stored active contract address
|
mockAccountService.AssertExpectations(t)
|
||||||
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)
|
||||||
|
|
||||||
|
h := &MenuHandlers{
|
||||||
|
userdataStore: store,
|
||||||
|
accountService: mockAccountService,
|
||||||
|
}
|
||||||
|
|
||||||
|
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 := store.ReadEntry(ctx, sessionId, 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) {
|
func TestGetVoucherList(t *testing.T) {
|
||||||
sessionId := "session123"
|
sessionId := "session123"
|
||||||
|
|
||||||
|
@ -103,7 +103,8 @@ func (ls *LocalHandlerService) GetHandler(accountService remote.AccountService)
|
|||||||
ls.DbRs.AddLocalFunc("confirm_pin_change", appHandlers.ConfirmPinChange)
|
ls.DbRs.AddLocalFunc("confirm_pin_change", appHandlers.ConfirmPinChange)
|
||||||
ls.DbRs.AddLocalFunc("quit_with_help", appHandlers.QuitWithHelp)
|
ls.DbRs.AddLocalFunc("quit_with_help", appHandlers.QuitWithHelp)
|
||||||
ls.DbRs.AddLocalFunc("fetch_community_balance", appHandlers.FetchCommunityBalance)
|
ls.DbRs.AddLocalFunc("fetch_community_balance", appHandlers.FetchCommunityBalance)
|
||||||
ls.DbRs.AddLocalFunc("manage_vouchers", appHandlers.ManageVouchers)
|
ls.DbRs.AddLocalFunc("set_default_voucher", appHandlers.SetDefaultVoucher)
|
||||||
|
ls.DbRs.AddLocalFunc("check_vouchers", appHandlers.CheckVouchers)
|
||||||
ls.DbRs.AddLocalFunc("get_vouchers", appHandlers.GetVoucherList)
|
ls.DbRs.AddLocalFunc("get_vouchers", appHandlers.GetVoucherList)
|
||||||
ls.DbRs.AddLocalFunc("view_voucher", appHandlers.ViewVoucher)
|
ls.DbRs.AddLocalFunc("view_voucher", appHandlers.ViewVoucher)
|
||||||
ls.DbRs.AddLocalFunc("set_voucher", appHandlers.SetVoucher)
|
ls.DbRs.AddLocalFunc("set_voucher", appHandlers.SetVoucher)
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
LOAD clear_temporary_value 2
|
LOAD clear_temporary_value 2
|
||||||
RELOAD clear_temporary_value
|
RELOAD clear_temporary_value
|
||||||
LOAD manage_vouchers 160
|
LOAD set_default_voucher 8
|
||||||
RELOAD manage_vouchers
|
RELOAD set_default_voucher
|
||||||
|
LOAD check_vouchers 10
|
||||||
|
RELOAD check_vouchers
|
||||||
LOAD check_balance 128
|
LOAD check_balance 128
|
||||||
RELOAD check_balance
|
RELOAD check_balance
|
||||||
CATCH api_failure flag_api_call_error 1
|
CATCH api_failure flag_api_call_error 1
|
||||||
|
@ -31,3 +31,4 @@ flag,flag_back_set,37,this is set when it is a back navigation
|
|||||||
flag,flag_account_blocked,38,this is set when an account has been blocked after the allowed incorrect PIN attempts have been exceeded
|
flag,flag_account_blocked,38,this is set when an account has been blocked after the allowed incorrect PIN attempts have been exceeded
|
||||||
flag,flag_invalid_pin,39,this is set when the given PIN is invalid(is less than or more than 4 digits)
|
flag,flag_invalid_pin,39,this is set when the given PIN is invalid(is less than or more than 4 digits)
|
||||||
flag,flag_alias_set,40,this is set when an account alias has been assigned to a user
|
flag,flag_alias_set,40,this is set when an account alias has been assigned to a user
|
||||||
|
flag,flag_account_pin_reset,41,this is set on an account when an admin triggers a PIN reset for them
|
||||||
|
|
@ -65,6 +65,8 @@ const (
|
|||||||
DATA_ACCOUNT_ALIAS
|
DATA_ACCOUNT_ALIAS
|
||||||
//currently suggested alias by the api awaiting user's confirmation as accepted account alias
|
//currently suggested alias by the api awaiting user's confirmation as accepted account alias
|
||||||
DATA_SUGGESTED_ALIAS
|
DATA_SUGGESTED_ALIAS
|
||||||
|
//currently suggested alias by the api awaiting user's confirmation as accepted account alias
|
||||||
|
DATA_SELF_PIN_RESET
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
Loading…
Reference in New Issue
Block a user