wip-account-creation #4
189
cmd/main.go
189
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
|
||||
lash marked this conversation as resolved
|
||||
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"`
|
||||
Alfred-mk marked this conversation as resolved
Outdated
lash
commented
please rename this flag to USERFLAG_PINMISMATCH to avoid ambiguity. please rename this flag to USERFLAG_PINMISMATCH to avoid ambiguity.
lash
commented
priority **priority**
|
||||
TrackingId string `json:"trackingId"`
|
||||
} `json:"result"`
|
||||
}
|
||||
|
||||
Alfred-mk marked this conversation as resolved
Outdated
lash
commented
Please can we have all the http stuff in a separate package?
Please can we have all the http stuff in a separate package?
* urls
* responses
* gets
|
||||
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
|
||||
}
|
||||
|
||||
Alfred-mk marked this conversation as resolved
Outdated
lash
commented
can this (and all its methods) be moved to a separate package please? can this (and all its methods) be moved to a separate package please?
|
||||
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
|
||||
Alfred-mk marked this conversation as resolved
Outdated
lash
commented
This code is repeated many times. Could we please abstract the json fs part of the get and set to a module implementing an interface with This code is repeated many times. Could we please abstract the json fs part of the get and set to a module implementing an interface with `Get` and `Put`?
|
||||
|
||||
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 {
|
||||
Alfred-mk marked this conversation as resolved
Outdated
lash
commented
perhaps check this input, 2 or 4 digits, numeric perhaps check this input, 2 or 4 digits, numeric
lash
commented
priority **priority**
|
||||
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)
|
||||
|
1
services/registration/account_creation
Normal file
1
services/registration/account_creation
Normal file
@ -0,0 +1 @@
|
||||
Your account is being created...
|
2
services/registration/account_creation.vis
Normal file
2
services/registration/account_creation.vis
Normal file
@ -0,0 +1,2 @@
|
||||
LOAD create_account 0
|
||||
HALT
|
1
services/registration/account_pending
Normal file
1
services/registration/account_pending
Normal file
@ -0,0 +1 @@
|
||||
Your account is still being created.
|
3
services/registration/account_pending.vis
Normal file
3
services/registration/account_pending.vis
Normal file
@ -0,0 +1,3 @@
|
||||
RELOAD check_account_status
|
||||
CATCH profile 11 1
|
||||
HALT
|
1
services/registration/account_pending_swa
Normal file
1
services/registration/account_pending_swa
Normal file
@ -0,0 +1 @@
|
||||
Akaunti yako bado inatengenezwa
|
1
services/registration/no_menu
Normal file
1
services/registration/no_menu
Normal file
@ -0,0 +1 @@
|
||||
no
|
1
services/registration/no_menu_swa
Normal file
1
services/registration/no_menu_swa
Normal file
@ -0,0 +1 @@
|
||||
la
|
2
services/registration/profile
Normal file
2
services/registration/profile
Normal file
@ -0,0 +1,2 @@
|
||||
Welcome. Your account is
|
||||
{{.check_identifier}}
|
4
services/registration/profile.vis
Normal file
4
services/registration/profile.vis
Normal file
@ -0,0 +1,4 @@
|
||||
LOAD check_identifier 64
|
||||
RELOAD check_identifier
|
||||
MAP check_identifier
|
||||
HALT
|
2
services/registration/profile_swa
Normal file
2
services/registration/profile_swa
Normal file
@ -0,0 +1,2 @@
|
||||
Karibu. Akaunti yako ni
|
||||
{{.check_identifier}}
|
@ -1 +1 @@
|
||||
Welcome to Sarafu Network registration
|
||||
Welcome to Sarafu Network
|
@ -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
|
||||
|
@ -1 +1 @@
|
||||
Karibu Sarafu Network
|
||||
Karibu Sarafu Network
|
2
services/registration/select_language
Normal file
2
services/registration/select_language
Normal file
@ -0,0 +1,2 @@
|
||||
Welcome to Sarafu Network
|
||||
Please select a language
|
5
services/registration/select_language.vis
Normal file
5
services/registration/select_language.vis
Normal file
@ -0,0 +1,5 @@
|
||||
MOUT english 0
|
||||
MOUT kiswahili 1
|
||||
HALT
|
||||
INCMP terms 0
|
||||
INCMP terms 1
|
@ -2,3 +2,6 @@ LOAD select_language 0
|
||||
RELOAD select_language
|
||||
MOUT yes 0
|
||||
MOUT no 1
|
||||
HALT
|
||||
INCMP account_creation 0
|
||||
INCMP _ *
|
||||
|
1
services/registration/yes_menu
Normal file
1
services/registration/yes_menu
Normal file
@ -0,0 +1 @@
|
||||
yes
|
1
services/registration/yes_menu_swa
Normal file
1
services/registration/yes_menu_swa
Normal file
@ -0,0 +1 @@
|
||||
ndio
|
Loading…
Reference in New Issue
Block a user
Please add a documentation line on each.
priority