From 88731124df6219eac16c4b45e63adc7150d3c7be Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Fri, 16 Aug 2024 13:38:58 +0300 Subject: [PATCH] generate account using english and swahili --- cmd/main.go | 189 ++++++++++++++++++++- services/registration/account_creation | 1 + services/registration/account_creation.vis | 2 + services/registration/account_pending | 1 + services/registration/account_pending.vis | 3 + services/registration/account_pending_swa | 1 + services/registration/no_menu | 1 + services/registration/no_menu_swa | 1 + services/registration/profile | 2 + services/registration/profile.vis | 4 + services/registration/profile_swa | 2 + services/registration/registration | 2 +- services/registration/registration.vis | 9 +- services/registration/registration_swa | 2 +- services/registration/select_language | 2 + services/registration/select_language.vis | 5 + services/registration/terms.vis | 3 + services/registration/yes_menu | 1 + services/registration/yes_menu_swa | 1 + 19 files changed, 225 insertions(+), 7 deletions(-) create mode 100644 services/registration/account_creation create mode 100644 services/registration/account_creation.vis create mode 100644 services/registration/account_pending create mode 100644 services/registration/account_pending.vis create mode 100644 services/registration/account_pending_swa create mode 100644 services/registration/no_menu create mode 100644 services/registration/no_menu_swa create mode 100644 services/registration/profile create mode 100644 services/registration/profile.vis create mode 100644 services/registration/profile_swa create mode 100644 services/registration/select_language create mode 100644 services/registration/select_language.vis create mode 100644 services/registration/yes_menu create mode 100644 services/registration/yes_menu_swa diff --git a/cmd/main.go b/cmd/main.go index ebbfc75..6e75eec 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -2,10 +2,14 @@ package main import ( "context" + "encoding/json" "flag" "fmt" + "io" + "net/http" "os" "path" + "time" "git.defalsify.org/vise.git/cache" "git.defalsify.org/vise.git/engine" @@ -16,8 +20,38 @@ import ( const ( USERFLAG_LANGUAGE_SET = iota + state.FLAG_USERSTART + USERFLAG_ACCOUNT_CREATED + USERFLAG_ACCOUNT_PENDING + USERFLAG_ACCOUNT_SUCCESS ) +const ( + createAccountURL = "https://custodial.sarafu.africa/api/account/create" + trackStatusURL = "https://custodial.sarafu.africa/api/track/" +) + +type accountResponse struct { + Ok bool `json:"ok"` + Result struct { + CustodialId json.Number `json:"custodialId"` + PublicKey string `json:"publicKey"` + TrackingId string `json:"trackingId"` + } `json:"result"` +} + +type trackStatusResponse struct { + Ok bool `json:"ok"` + Result struct { + Transaction struct { + CreatedAt time.Time `json:"createdAt"` + Status string `json:"status"` + TransferValue json.Number `json:"transferValue"` + TxHash string `json:"txHash"` + TxType string `json:"txType"` + } + } `json:"result"` +} + type fsData struct { path string } @@ -35,9 +69,158 @@ func (fsd *fsData) SetLanguageSelected(ctx context.Context, sym string, input [] res.Content = "swa" default: } + + res.FlagSet = append(res.FlagSet, USERFLAG_LANGUAGE_SET) + return res, nil } +func (fsd *fsData) create_account(ctx context.Context, sym string, input []byte) (resource.Result, error) { + res := resource.Result{} + fp := fsd.path + "_data" + f, err := os.OpenFile(fp, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { + return res, err + } + f.Close() + + accountResp, err := createAccount() + + if err != nil { + fmt.Println("Failed to create account:", err) + return res, err + } + + accountData := map[string]string{ + "TrackingId": accountResp.Result.TrackingId, + "PublicKey": accountResp.Result.PublicKey, + "CustodialId": accountResp.Result.CustodialId.String(), + "Status": "PENDING", + } + + jsonData, err := json.Marshal(accountData) + if err != nil { + return res, err + } + + err = os.WriteFile(fp, jsonData, 0644) + if err != nil { + return res, err + } + + res.FlagSet = []uint32{USERFLAG_ACCOUNT_CREATED} + return res, err +} + +func (fsd *fsData) checkIdentifier(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 + } + + res.Content = accountData["PublicKey"] + + return res, nil +} + +func (fsd *fsData) check_account_status(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 + } + + status, err := checkAccountStatus(accountData["TrackingId"]) + + if err != nil { + fmt.Println("Error checking account status:", err) + return res, nil + } + + accountData["Status"] = status + + if status == "SUCCESS" { + res.FlagSet = []uint32{USERFLAG_ACCOUNT_SUCCESS} + res.FlagReset = []uint32{USERFLAG_ACCOUNT_PENDING} + } else { + res.FlagSet = []uint32{USERFLAG_ACCOUNT_PENDING} + res.FlagReset = []uint32{USERFLAG_ACCOUNT_SUCCESS} + } + + updatedJsonData, err := json.Marshal(accountData) + if err != nil { + return res, err + } + + err = os.WriteFile(fp, updatedJsonData, 0644) + if err != nil { + return res, err + } + + return res, nil +} + +func createAccount() (*accountResponse, error) { + resp, err := http.Post(createAccountURL, "application/json", nil) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, err + } + + var accountResp accountResponse + err = json.Unmarshal(body, &accountResp) + if err != nil { + return nil, err + } + + return &accountResp, nil +} + +func checkAccountStatus(trackingId string) (string, error) { + resp, err := http.Get(trackStatusURL + trackingId) + if err != nil { + return "", err + } + defer resp.Body.Close() + + body, err := io.ReadAll(resp.Body) + if err != nil { + return "", err + } + + var trackResp trackStatusResponse + err = json.Unmarshal(body, &trackResp) + if err != nil { + return "", err + } + + status := trackResp.Result.Transaction.Status + + return status, nil +} + var ( scriptDir = path.Join("services", "registration") ) @@ -55,7 +238,7 @@ func main() { fmt.Fprintf(os.Stderr, "starting session at symbol '%s' using resource dir: %s\n", root, dir) ctx := context.Background() - st := state.NewState(3) + st := state.NewState(7) rfs := resource.NewFsResource(scriptDir) ca := cache.NewCache() cfg := engine.Config{ @@ -86,6 +269,10 @@ func main() { path: fp, } 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) + cont, err := en.Init(ctx) if err != nil { fmt.Fprintf(os.Stderr, "engine init exited with error: %v\n", err) diff --git a/services/registration/account_creation b/services/registration/account_creation new file mode 100644 index 0000000..e9463a6 --- /dev/null +++ b/services/registration/account_creation @@ -0,0 +1 @@ +Your account is being created... \ No newline at end of file diff --git a/services/registration/account_creation.vis b/services/registration/account_creation.vis new file mode 100644 index 0000000..3476cfc --- /dev/null +++ b/services/registration/account_creation.vis @@ -0,0 +1,2 @@ +LOAD create_account 0 +HALT diff --git a/services/registration/account_pending b/services/registration/account_pending new file mode 100644 index 0000000..4eadf25 --- /dev/null +++ b/services/registration/account_pending @@ -0,0 +1 @@ +Your account is still being created. \ No newline at end of file diff --git a/services/registration/account_pending.vis b/services/registration/account_pending.vis new file mode 100644 index 0000000..43b2dd4 --- /dev/null +++ b/services/registration/account_pending.vis @@ -0,0 +1,3 @@ +RELOAD check_account_status +CATCH profile 11 1 +HALT diff --git a/services/registration/account_pending_swa b/services/registration/account_pending_swa new file mode 100644 index 0000000..2e514b5 --- /dev/null +++ b/services/registration/account_pending_swa @@ -0,0 +1 @@ +Akaunti yako bado inatengenezwa \ No newline at end of file diff --git a/services/registration/no_menu b/services/registration/no_menu new file mode 100644 index 0000000..7ecb56e --- /dev/null +++ b/services/registration/no_menu @@ -0,0 +1 @@ +no diff --git a/services/registration/no_menu_swa b/services/registration/no_menu_swa new file mode 100644 index 0000000..2ece186 --- /dev/null +++ b/services/registration/no_menu_swa @@ -0,0 +1 @@ +la diff --git a/services/registration/profile b/services/registration/profile new file mode 100644 index 0000000..89144dd --- /dev/null +++ b/services/registration/profile @@ -0,0 +1,2 @@ +Welcome. Your account is +{{.check_identifier}} diff --git a/services/registration/profile.vis b/services/registration/profile.vis new file mode 100644 index 0000000..56dc044 --- /dev/null +++ b/services/registration/profile.vis @@ -0,0 +1,4 @@ +LOAD check_identifier 64 +RELOAD check_identifier +MAP check_identifier +HALT diff --git a/services/registration/profile_swa b/services/registration/profile_swa new file mode 100644 index 0000000..2738ff1 --- /dev/null +++ b/services/registration/profile_swa @@ -0,0 +1,2 @@ +Karibu. Akaunti yako ni +{{.check_identifier}} diff --git a/services/registration/registration b/services/registration/registration index 01db78c..3928a82 100644 --- a/services/registration/registration +++ b/services/registration/registration @@ -1 +1 @@ -Welcome to Sarafu Network registration \ No newline at end of file +Welcome to Sarafu Network \ No newline at end of file diff --git a/services/registration/registration.vis b/services/registration/registration.vis index 1017a0f..76fa921 100644 --- a/services/registration/registration.vis +++ b/services/registration/registration.vis @@ -1,5 +1,6 @@ -MOUT english 0 -MOUT kiswahili 1 +CATCH select_language 8 0 +CATCH terms 9 0 +CATCH account_pending 10 1 +CATCH profile 11 1 +LOAD check_account_status 0 HALT -INCMP terms 0 -INCMP terms 1 diff --git a/services/registration/registration_swa b/services/registration/registration_swa index 7edd566..75bb624 100644 --- a/services/registration/registration_swa +++ b/services/registration/registration_swa @@ -1 +1 @@ -Karibu Sarafu Network \ No newline at end of file +Karibu Sarafu Network \ No newline at end of file diff --git a/services/registration/select_language b/services/registration/select_language new file mode 100644 index 0000000..b3d4304 --- /dev/null +++ b/services/registration/select_language @@ -0,0 +1,2 @@ +Welcome to Sarafu Network +Please select a language \ No newline at end of file diff --git a/services/registration/select_language.vis b/services/registration/select_language.vis new file mode 100644 index 0000000..1017a0f --- /dev/null +++ b/services/registration/select_language.vis @@ -0,0 +1,5 @@ +MOUT english 0 +MOUT kiswahili 1 +HALT +INCMP terms 0 +INCMP terms 1 diff --git a/services/registration/terms.vis b/services/registration/terms.vis index 572e7e8..5375925 100644 --- a/services/registration/terms.vis +++ b/services/registration/terms.vis @@ -2,3 +2,6 @@ LOAD select_language 0 RELOAD select_language MOUT yes 0 MOUT no 1 +HALT +INCMP account_creation 0 +INCMP _ * diff --git a/services/registration/yes_menu b/services/registration/yes_menu new file mode 100644 index 0000000..7cfab5b --- /dev/null +++ b/services/registration/yes_menu @@ -0,0 +1 @@ +yes diff --git a/services/registration/yes_menu_swa b/services/registration/yes_menu_swa new file mode 100644 index 0000000..6908a6c --- /dev/null +++ b/services/registration/yes_menu_swa @@ -0,0 +1 @@ +ndio