wip-account-creation #4

Merged
lash merged 143 commits from wip-account-creation into master 2024-08-30 14:37:58 +02:00
19 changed files with 225 additions and 7 deletions
Showing only changes of commit 88731124df - Show all commits

View File

@ -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
Review

Please add a documentation line on each.

Please add a documentation line on each.
Review

priority

**priority**
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
Outdated
Review

please rename this flag to USERFLAG_PINMISMATCH to avoid ambiguity.

please rename this flag to USERFLAG_PINMISMATCH to avoid ambiguity.
Outdated
Review

priority

**priority**
TrackingId string `json:"trackingId"`
} `json:"result"`
}
Alfred-mk marked this conversation as resolved Outdated
Outdated
Review

Please can we have all the http stuff in a separate package?

  • urls
  • responses
  • gets
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
Outdated
Review

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
Outdated
Review

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?

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
Outdated
Review

perhaps check this input, 2 or 4 digits, numeric

perhaps check this input, 2 or 4 digits, numeric
Outdated
Review

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)

View File

@ -0,0 +1 @@
Your account is being created...

View File

@ -0,0 +1,2 @@
LOAD create_account 0
HALT

View File

@ -0,0 +1 @@
Your account is still being created.

View File

@ -0,0 +1,3 @@
RELOAD check_account_status
CATCH profile 11 1
HALT

View File

@ -0,0 +1 @@
Akaunti yako bado inatengenezwa

View File

@ -0,0 +1 @@
no

View File

@ -0,0 +1 @@
la

View File

@ -0,0 +1,2 @@
Welcome. Your account is
{{.check_identifier}}

View File

@ -0,0 +1,4 @@
LOAD check_identifier 64
RELOAD check_identifier
MAP check_identifier
HALT

View File

@ -0,0 +1,2 @@
Karibu. Akaunti yako ni
{{.check_identifier}}

View File

@ -1 +1 @@
Welcome to Sarafu Network registration
Welcome to Sarafu Network

View File

@ -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

View File

@ -0,0 +1,2 @@
Welcome to Sarafu Network
Please select a language

View File

@ -0,0 +1,5 @@
MOUT english 0
MOUT kiswahili 1
HALT
INCMP terms 0
INCMP terms 1

View File

@ -2,3 +2,6 @@ LOAD select_language 0
RELOAD select_language
MOUT yes 0
MOUT no 1
HALT
INCMP account_creation 0
INCMP _ *

View File

@ -0,0 +1 @@
yes

View File

@ -0,0 +1 @@
ndio