generate account using english and swahili

This commit is contained in:
Alfred Kamanda 2024-08-16 13:38:58 +03:00
parent 46b9e2eba9
commit 88731124df
19 changed files with 225 additions and 7 deletions

View File

@ -2,10 +2,14 @@ package main
import ( import (
"context" "context"
"encoding/json"
"flag" "flag"
"fmt" "fmt"
"io"
"net/http"
"os" "os"
"path" "path"
"time"
"git.defalsify.org/vise.git/cache" "git.defalsify.org/vise.git/cache"
"git.defalsify.org/vise.git/engine" "git.defalsify.org/vise.git/engine"
@ -16,8 +20,38 @@ import (
const ( const (
USERFLAG_LANGUAGE_SET = iota + state.FLAG_USERSTART 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 { type fsData struct {
path string path string
} }
@ -35,9 +69,158 @@ func (fsd *fsData) SetLanguageSelected(ctx context.Context, sym string, input []
res.Content = "swa" res.Content = "swa"
default: default:
} }
res.FlagSet = append(res.FlagSet, USERFLAG_LANGUAGE_SET)
return res, nil 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 ( var (
scriptDir = path.Join("services", "registration") 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) fmt.Fprintf(os.Stderr, "starting session at symbol '%s' using resource dir: %s\n", root, dir)
ctx := context.Background() ctx := context.Background()
st := state.NewState(3) st := state.NewState(7)
rfs := resource.NewFsResource(scriptDir) rfs := resource.NewFsResource(scriptDir)
ca := cache.NewCache() ca := cache.NewCache()
cfg := engine.Config{ cfg := engine.Config{
@ -86,6 +269,10 @@ func main() {
path: fp, path: fp,
} }
rfs.AddLocalFunc("select_language", fs.SetLanguageSelected) 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) cont, err := en.Init(ctx)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "engine init exited with error: %v\n", err) 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 CATCH select_language 8 0
MOUT kiswahili 1 CATCH terms 9 0
CATCH account_pending 10 1
CATCH profile 11 1
LOAD check_account_status 0
HALT 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 RELOAD select_language
MOUT yes 0 MOUT yes 0
MOUT no 1 MOUT no 1
HALT
INCMP account_creation 0
INCMP _ *

View File

@ -0,0 +1 @@
yes

View File

@ -0,0 +1 @@
ndio