From 80ff5c477faa1c5131c39b457571cec93daa2156 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Wed, 21 Aug 2024 21:37:29 +0300 Subject: [PATCH] implement pin check --- cmd/main.go | 128 ++++++++++++------ services/registration/account_creation.vis | 3 +- services/registration/balances.vis | 2 +- ...nity_balance_swa => comminity_balance_swa} | 0 ...my_community_balance => community_balance} | 0 services/registration/community_balance.vis | 6 + services/registration/enter_pin.vis | 2 +- services/registration/incorrect_pin | 1 + services/registration/incorrect_pin.vis | 7 + services/registration/incorrect_pin_swa | 1 + services/registration/my_balance.vis | 5 +- .../registration/my_community_balance.vis | 3 - 12 files changed, 113 insertions(+), 45 deletions(-) rename services/registration/{my_comminity_balance_swa => comminity_balance_swa} (100%) rename services/registration/{my_community_balance => community_balance} (100%) create mode 100644 services/registration/community_balance.vis create mode 100644 services/registration/incorrect_pin create mode 100644 services/registration/incorrect_pin.vis create mode 100644 services/registration/incorrect_pin_swa delete mode 100644 services/registration/my_community_balance.vis diff --git a/cmd/main.go b/cmd/main.go index 3a06b81..d7bf738 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -26,6 +26,7 @@ const ( USERFLAG_ACCOUNT_UNLOCKED invalidRecipient invalidRecipientWithInvite + USERFLAG_INCORRECTPIN ) const ( @@ -66,7 +67,7 @@ type balanceResponse struct { type fsData struct { path string - st *state.State + st *state.State } func (fsd *fsData) SetLanguageSelected(ctx context.Context, sym string, input []byte) (resource.Result, error) { @@ -104,16 +105,16 @@ func (fsd *fsData) create_account(ctx context.Context, sym string, input []byte) // } accountResp := accountResponse{ - Ok: true, - Result: struct { - CustodialId json.Number `json:"custodialId"` - PublicKey string `json:"publicKey"` - TrackingId string `json:"trackingId"` - }{ - CustodialId: "636", - PublicKey: "0x8d86F9D4A4eae41Dc3B68034895EA97BcA90e8c1", - TrackingId: "45c67314-7995-4890-89d6-e5af987754ac", - }} + Ok: true, + Result: struct { + CustodialId json.Number `json:"custodialId"` + PublicKey string `json:"publicKey"` + TrackingId string `json:"trackingId"` + }{ + CustodialId: "636", + PublicKey: "0x8d86F9D4A4eae41Dc3B68034895EA97BcA90e8c1", + TrackingId: "45c67314-7995-4890-89d6-e5af987754ac", + }} accountData := map[string]string{ "TrackingId": accountResp.Result.TrackingId, @@ -131,8 +132,7 @@ func (fsd *fsData) create_account(ctx context.Context, sym string, input []byte) if err != nil { return res, err } - - res.FlagSet = []uint32{USERFLAG_ACCOUNT_CREATED} + res.FlagSet = append(res.FlagSet, USERFLAG_ACCOUNT_CREATED) return res, err } @@ -156,16 +156,33 @@ func (fsd *fsData) checkIdentifier(ctx context.Context, sym string, input []byte return res, nil } - -func (fsd *fsData) unlock(ctx context.Context,sym string,input []byte) (resource.Result,error){ +func (fsd *fsData) unLock(ctx context.Context, sym string, input []byte) (resource.Result, error) { res := resource.Result{} - //res.FlagSet = []uint32{USERFLAG_ACCOUNT_UNLOCKED} - if fsd.st.MatchFlag(USERFLAG_ACCOUNT_UNLOCKED, false) { - res.FlagSet = append(res.FlagSet, USERFLAG_ACCOUNT_UNLOCKED) - } else { - res.FlagReset = append(res.FlagReset, USERFLAG_ACCOUNT_UNLOCKED) + pin := string(input) + if len(input) > 0 { + if pin == "0000" { + res.FlagSet = append(res.FlagSet, USERFLAG_INCORRECTPIN) + res.FlagReset = append(res.FlagReset, USERFLAG_ACCOUNT_UNLOCKED) + return res, nil + } + if fsd.st.MatchFlag(USERFLAG_ACCOUNT_UNLOCKED, false) { + res.FlagSet = append(res.FlagSet, USERFLAG_ACCOUNT_UNLOCKED) + } else { + res.FlagReset = append(res.FlagReset, USERFLAG_ACCOUNT_UNLOCKED) + } } - return res,nil + return res, nil +} + +func (fsd *fsData) ResetIncorrectPin(ctx context.Context, sym string, input []byte) (resource.Result, error) { + res := resource.Result{} + isIncorrectPinSet := fsd.st.MatchFlag(USERFLAG_INCORRECTPIN, true) + if isIncorrectPinSet { + res.FlagReset = append(res.FlagReset, USERFLAG_INCORRECTPIN) + } else { + res.FlagReset = append(res.FlagReset, USERFLAG_INCORRECTPIN) + } + return res, nil } func (fsd *fsData) check_account_status(ctx context.Context, sym string, input []byte) (resource.Result, error) { @@ -192,16 +209,14 @@ func (fsd *fsData) check_account_status(ctx context.Context, sym string, input [ accountData["Status"] = status - if status == "SUCCESS" { + if status == "REVERTED" { res.FlagSet = append(res.FlagSet, USERFLAG_ACCOUNT_SUCCESS) - res.FlagReset = append(res.FlagReset,USERFLAG_ACCOUNT_PENDING) + res.FlagReset = append(res.FlagReset, USERFLAG_ACCOUNT_PENDING) } else { res.FlagReset = append(res.FlagSet, USERFLAG_ACCOUNT_SUCCESS) - res.FlagSet = append(res.FlagReset,USERFLAG_ACCOUNT_PENDING) + res.FlagSet = append(res.FlagReset, USERFLAG_ACCOUNT_PENDING) } - - updatedJsonData, err := json.Marshal(accountData) if err != nil { return res, err @@ -259,17 +274,11 @@ func checkAccountStatus(trackingId string) (string, error) { return status, nil } -func (fsd *fsData) quit(ctx context.Context, sym string, input []byte) (resource.Result, error) { +func (fsd *fsData) quit(ctx context.Context, sym string, input []byte) (resource.Result, error) { res := resource.Result{ - Content: "", - } - st := fsd.st - if(st.MatchFlag(USERFLAG_ACCOUNT_UNLOCKED,true)){ - //res.FlagReset = []uint32{USERFLAG_ACCOUNT_UNLOCKED} - // res.FlagReset = append(res.FlagReset, USERFLAG_ACCOUNT_UNLOCKED) - }else { - res.FlagReset = append(res.FlagReset, USERFLAG_ACCOUNT_UNLOCKED) + Content: "Your account is being created", } + res.FlagReset = append(res.FlagReset, USERFLAG_ACCOUNT_UNLOCKED) return res, nil } @@ -480,6 +489,44 @@ func (fsd *fsData) get_sender(ctx context.Context, sym string, input []byte) (re return res, nil } + +func (fsd *fsData) quitWithBalance(ctx context.Context, sym string, input []byte) (resource.Result, error) { + res := resource.Result{ + } + fp := fsd.path + "_data" + + jsonData, err := os.ReadFile(fp) + if err != nil { + return res, err + } + + var accountData map[string]string + err = json.Unmarshal(jsonData, &accountData) + if err != nil { + return res, err + } + resp, err := http.Get(checkBalanceURL + accountData["PublicKey"]) + if err != nil { + return res, nil + } + defer resp.Body.Close() + + body, err := io.ReadAll(resp.Body) + if err != nil { + return res, nil + } + + var balanceResp balanceResponse + err = json.Unmarshal(body, &balanceResp) + if err != nil { + return res, nil + } + balance := balanceResp.Result.Balance + res.Content = fmt.Sprintf("Your account balance is: %s", balance) + res.FlagReset = append(res.FlagReset, USERFLAG_ACCOUNT_UNLOCKED) + return res, nil +} + var ( scriptDir = path.Join("services", "registration") ) @@ -497,12 +544,13 @@ func main() { fmt.Fprintf(os.Stderr, "starting session at symbol '%s' using resource dir: %s\n", root, dir) ctx := context.Background() - st := state.NewState(7) + st := state.NewState(9) st.UseDebug() state.FlagDebugger.Register(USERFLAG_LANGUAGE_SET, "LANGUAGE_CHANGE") state.FlagDebugger.Register(USERFLAG_ACCOUNT_CREATED, "ACCOUNT_CREATED") state.FlagDebugger.Register(USERFLAG_ACCOUNT_SUCCESS, "ACCOUNT_SUCCESS") state.FlagDebugger.Register(USERFLAG_ACCOUNT_PENDING, "ACCOUNT_PENDING") + state.FlagDebugger.Register(USERFLAG_INCORRECTPIN, "INCORRECTPIN") rfs := resource.NewFsResource(scriptDir) ca := cache.NewCache() @@ -519,6 +567,7 @@ func main() { } pr := persist.NewFsPersister(dp) en, err := engine.NewPersistedEngine(ctx, cfg, pr, rfs) + if err != nil { pr = pr.WithContent(&st, ca) err = pr.Save(cfg.SessionId) @@ -535,13 +584,13 @@ func main() { fp := path.Join(dp, sessionId) fs := &fsData{ path: fp, - st: &st, + st: &st, } rfs.AddLocalFunc("select_language", fs.SetLanguageSelected) rfs.AddLocalFunc("create_account", fs.create_account) rfs.AddLocalFunc("check_identifier", fs.checkIdentifier) rfs.AddLocalFunc("check_account_status", fs.check_account_status) - rfs.AddLocalFunc("unlock_account", fs.unlock) + rfs.AddLocalFunc("unlock_account", fs.unLock) rfs.AddLocalFunc("quit", fs.quit) rfs.AddLocalFunc("check_balance", fs.checkBalance) rfs.AddLocalFunc("validate_recipient", fs.validate_recipient) @@ -550,8 +599,11 @@ func main() { rfs.AddLocalFunc("validate_amount", fs.validate_amount) rfs.AddLocalFunc("get_recipient", fs.get_recipient) rfs.AddLocalFunc("get_sender", fs.get_sender) + rfs.AddLocalFunc("reset_incorrect", fs.ResetIncorrectPin) + rfs.AddLocalFunc("quit_with_balance",fs.quitWithBalance) cont, err := en.Init(ctx) + en.SetDebugger(engine.NewSimpleDebug(nil)) if err != nil { fmt.Fprintf(os.Stderr, "engine init exited with error: %v\n", err) os.Exit(1) diff --git a/services/registration/account_creation.vis b/services/registration/account_creation.vis index 72f46dd..b810e1a 100644 --- a/services/registration/account_creation.vis +++ b/services/registration/account_creation.vis @@ -1,3 +1,4 @@ LOAD create_account 0 +LOAD quit 0 HALT -MOVE quit + diff --git a/services/registration/balances.vis b/services/registration/balances.vis index e968325..c936c15 100644 --- a/services/registration/balances.vis +++ b/services/registration/balances.vis @@ -4,5 +4,5 @@ MOUT back 0 HALT INCMP ^ 0 INCMP my_balance 1 -INCMP my_community_balance 2 +INCMP community_balance 2 diff --git a/services/registration/my_comminity_balance_swa b/services/registration/comminity_balance_swa similarity index 100% rename from services/registration/my_comminity_balance_swa rename to services/registration/comminity_balance_swa diff --git a/services/registration/my_community_balance b/services/registration/community_balance similarity index 100% rename from services/registration/my_community_balance rename to services/registration/community_balance diff --git a/services/registration/community_balance.vis b/services/registration/community_balance.vis new file mode 100644 index 0000000..e6d5805 --- /dev/null +++ b/services/registration/community_balance.vis @@ -0,0 +1,6 @@ +LOAD reset_incorrect 0 +CATCH incorrect_pin 15 1 +CATCH pin_entry 12 0 +LOAD quit_with_balance 0 +HALT + diff --git a/services/registration/enter_pin.vis b/services/registration/enter_pin.vis index f9f06b9..1217074 100644 --- a/services/registration/enter_pin.vis +++ b/services/registration/enter_pin.vis @@ -1,4 +1,4 @@ MOUT back 0 HALT -INCMP ^ 0 +INCMP _ 0 INCMP display_profile_info * diff --git a/services/registration/incorrect_pin b/services/registration/incorrect_pin new file mode 100644 index 0000000..2bb04e6 --- /dev/null +++ b/services/registration/incorrect_pin @@ -0,0 +1 @@ +Incorrect pin \ No newline at end of file diff --git a/services/registration/incorrect_pin.vis b/services/registration/incorrect_pin.vis new file mode 100644 index 0000000..844f3d6 --- /dev/null +++ b/services/registration/incorrect_pin.vis @@ -0,0 +1,7 @@ +LOAD reset_incorrect 0 +RELOAD reset_incorrect +MOUT retry 1 +MOUT quit 9 +HALT +INCMP _ 1 +INCMP quit 9 diff --git a/services/registration/incorrect_pin_swa b/services/registration/incorrect_pin_swa new file mode 100644 index 0000000..34a0b28 --- /dev/null +++ b/services/registration/incorrect_pin_swa @@ -0,0 +1 @@ +PIN ulioeka sio sahihi \ No newline at end of file diff --git a/services/registration/my_balance.vis b/services/registration/my_balance.vis index 90c3937..e6d5805 100644 --- a/services/registration/my_balance.vis +++ b/services/registration/my_balance.vis @@ -1,3 +1,6 @@ +LOAD reset_incorrect 0 +CATCH incorrect_pin 15 1 CATCH pin_entry 12 0 +LOAD quit_with_balance 0 HALT -MOVE quit + diff --git a/services/registration/my_community_balance.vis b/services/registration/my_community_balance.vis deleted file mode 100644 index 90c3937..0000000 --- a/services/registration/my_community_balance.vis +++ /dev/null @@ -1,3 +0,0 @@ -CATCH pin_entry 12 0 -HALT -MOVE quit