Compare commits
3 Commits
wip-go-vis
...
lash/draft
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1b712dbbb7
|
||
|
|
3c6585e387
|
||
|
|
7fc235c84c
|
143
cmd/main.go
143
cmd/main.go
@@ -2,40 +2,40 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/csv"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path"
|
||||
"strconv"
|
||||
|
||||
"git.defalsify.org/vise.git/asm"
|
||||
"git.defalsify.org/vise.git/db"
|
||||
fsdb "git.defalsify.org/vise.git/db/fs"
|
||||
gdbmdb "git.defalsify.org/vise.git/db/gdbm"
|
||||
"git.defalsify.org/vise.git/engine"
|
||||
"git.defalsify.org/vise.git/persist"
|
||||
"git.defalsify.org/vise.git/resource"
|
||||
"git.defalsify.org/vise.git/state"
|
||||
"git.defalsify.org/vise.git/logging"
|
||||
"git.grassecon.net/urdt/ussd/internal/handlers/ussd"
|
||||
)
|
||||
|
||||
var (
|
||||
scriptDir = path.Join("services", "registration")
|
||||
store = fsdb.NewFsDb()
|
||||
pr = persist.NewPersister(store)
|
||||
logg = logging.NewVanilla()
|
||||
flags *FlagParser
|
||||
)
|
||||
|
||||
func getFlags(fp string, debug bool) (*asm.FlagParser, error) {
|
||||
flagParser := asm.NewFlagParser().WithDebug()
|
||||
_, err := flagParser.Load(fp)
|
||||
|
||||
func getFlags(fp string, debug bool) error {
|
||||
Flags = NewFlagParser().WithDebug()
|
||||
flags, err := Flags.Load(fp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
return flagParser, nil
|
||||
}
|
||||
|
||||
func getHandler(appFlags *asm.FlagParser, rs *resource.DbResource, pe *persist.Persister, userdataStore db.Db) (*ussd.Handlers, error) {
|
||||
|
||||
ussdHandlers, err := ussd.NewHandlers(appFlags, pr, store)
|
||||
ussdHandlers, err := ussd.NewHandlers(appFlags pr.GetState(), dataStore)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -75,115 +75,94 @@ func getHandler(appFlags *asm.FlagParser, rs *resource.DbResource, pe *persist.P
|
||||
return ussdHandlers, nil
|
||||
}
|
||||
|
||||
func getPersister(dbDir string, ctx context.Context) (*persist.Persister, error) {
|
||||
err := os.MkdirAll(dbDir, 0700)
|
||||
func getPersister(dbDir string) (*persist.Persister, error) {
|
||||
err = os.MkdirAll(dp, 0700)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("state dir create exited with error: %v\n", err)
|
||||
}
|
||||
|
||||
store := gdbmdb.NewGdbmDb()
|
||||
storeFile := path.Join(dbDir, ".state")
|
||||
storeFile := path.Join(dbDir, "states.gdbm")
|
||||
store.Connect(ctx, storeFile)
|
||||
pr := persist.NewPersister(store)
|
||||
return pr, nil
|
||||
|
||||
return pr
|
||||
}
|
||||
|
||||
func getUserdataDb(dbDir string, ctx context.Context) db.Db {
|
||||
func getUserdataDb(dbDir string) {
|
||||
store := gdbmdb.NewGdbmDb()
|
||||
storeFile := path.Join(dbDir, "userdata.gdbm")
|
||||
store.Connect(ctx, storeFile)
|
||||
|
||||
return store
|
||||
}
|
||||
|
||||
func getResource(resourceDir string, ctx context.Context) (resource.Resource, error) {
|
||||
func getResource(resourceDir string) (resource.Resource, error) {
|
||||
store := fsdb.NewFsDb()
|
||||
err := store.Connect(ctx, resourceDir)
|
||||
err = store.Connect(ctx, resourceDir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
rfs := resource.NewDbResource(store)
|
||||
return rfs, nil
|
||||
}
|
||||
|
||||
func getEngine(cfg engine.Config, rs resource.Resource, pr *persist.Persister) *engine.DefaultEngine {
|
||||
en := engine.NewEngine(cfg, rs)
|
||||
func getEngine(cfg Config, rs resource.Resource, pr *persister.Persister) {
|
||||
|
||||
en := engine.NewEngine(cfg, rfs)
|
||||
en = en.WithPersister(pr)
|
||||
return en
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
//var dir string
|
||||
var dbDir string
|
||||
var resourceDir string
|
||||
//var root string
|
||||
var root string
|
||||
var size uint
|
||||
var sessionId string
|
||||
//flag.StringVar(&dir, "d", ".", "resource dir to read from")
|
||||
// flag.UintVar(&size, "s", 0, "max size of output")
|
||||
// flag.StringVar(&root, "root", "root", "entry point symbol")
|
||||
flag.StringVar(&sessionId, "session-id", "075xx2123", "session id")
|
||||
// flag.Parse()
|
||||
// fmt.Fprintf(os.Stderr, "starting session at symbol '%s' using resource dir: %s\n", root, dir)
|
||||
//logg.Infof("starting session", "symbol", root, "dbdir", dbDir, "sessionid", sessionId, "outsize", size)
|
||||
|
||||
flag.StringVar(&dbDir, "dbdir", ".state", "database dir to read from")
|
||||
flag.StringVar(&resourceDir, "resourcedir", path.Join("services", "registration"), "resource dir")
|
||||
flag.UintVar(&size, "s", 160, "max size of output")
|
||||
flag.StringVar(&root, "root", "root", "entry point symbol")
|
||||
flag.StringVar(&sessionId, "session-id", "default", "session id")
|
||||
flag.Parse()
|
||||
logg.Infof("starting session", "symbol", root, "dbdir", dbDir, "sessionid", sessionId, "outsize", size)
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
|
||||
ctx = context.WithValue(ctx, "SessionId",sessionId)
|
||||
pfp := path.Join(scriptDir, "pp.csv")
|
||||
flagParser, err := getFlags(pfp, true)
|
||||
|
||||
fl, err := getFlags()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
rs, err := getResource(resourceDir)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
pr, err := getDataPersister(dbDir)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
store, err := getUserdataDb(dataDir)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
hn, err := getHandlers(fl, rs, pr, store)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
cfg := engine.Config{
|
||||
Root: "root",
|
||||
SessionId: sessionId,
|
||||
OutputSize: uint32(size),
|
||||
FlagCount: uint32(16),
|
||||
Root: sym,
|
||||
SessionId: sessionId,
|
||||
OutputSize: size,
|
||||
FlagCount: uint32(16),
|
||||
}
|
||||
//ca := cache.Cache{}
|
||||
|
||||
rs, err := getResource(resourceDir, ctx)
|
||||
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
pr, err := getPersister(dbDir, ctx)
|
||||
//pr.WithContent(state.NewState(uint32(16)),&ca)
|
||||
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
store := getUserdataDb(dbDir, ctx)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
dbResource, ok := rs.(*resource.DbResource)
|
||||
|
||||
if !ok {
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
_, err = getHandler(flagParser, dbResource, pr, store)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
en := getEngine(cfg, rs, pr)
|
||||
en.WithState(state.NewState(uint32(16)))
|
||||
|
||||
_, err = en.Init(ctx)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "engine init exited with error: %v\n", err)
|
||||
|
||||
2
go-vise
2
go-vise
Submodule go-vise updated: 1f47a674d9...326bdb5018
@@ -3,6 +3,7 @@ package ussd
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"path"
|
||||
"regexp"
|
||||
@@ -10,19 +11,21 @@ import (
|
||||
"strings"
|
||||
|
||||
"git.defalsify.org/vise.git/asm"
|
||||
|
||||
"git.defalsify.org/vise.git/cache"
|
||||
"git.defalsify.org/vise.git/db"
|
||||
"git.defalsify.org/vise.git/lang"
|
||||
"git.defalsify.org/vise.git/persist"
|
||||
"git.defalsify.org/vise.git/resource"
|
||||
"git.defalsify.org/vise.git/state"
|
||||
"git.defalsify.org/vise.git/cache"
|
||||
"git.defalsify.org/vise.git/persist"
|
||||
"git.defalsify.org/vise.git/logging"
|
||||
"git.grassecon.net/urdt/ussd/internal/handlers/server"
|
||||
"git.grassecon.net/urdt/ussd/internal/utils"
|
||||
"github.com/graygnuorg/go-gdbm"
|
||||
"gopkg.in/leonelquinteros/gotext.v1"
|
||||
)
|
||||
|
||||
var (
|
||||
logg = logging.NewVanilla().WithDomain("urdt_ussdhandler")
|
||||
scriptDir = path.Join("services", "registration")
|
||||
translationDir = path.Join(scriptDir, "locale")
|
||||
)
|
||||
@@ -44,46 +47,10 @@ const (
|
||||
AccountCreated = "ACCOUNTCREATED"
|
||||
)
|
||||
|
||||
type FSData struct {
|
||||
Path string
|
||||
St *state.State
|
||||
}
|
||||
|
||||
// FlagManager handles centralized flag management
|
||||
type FlagManager struct {
|
||||
parser *asm.FlagParser
|
||||
}
|
||||
|
||||
// NewFlagManager creates a new FlagManager instance
|
||||
func NewFlagManager(csvPath string) (*FlagManager, error) {
|
||||
parser := asm.NewFlagParser()
|
||||
_, err := parser.Load(csvPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to load flag parser: %v", err)
|
||||
}
|
||||
|
||||
return &FlagManager{
|
||||
parser: parser,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetFlag retrieves a flag value by its label
|
||||
func (fm *FlagManager) GetFlag(label string) (uint32, error) {
|
||||
return fm.parser.GetFlag(label)
|
||||
}
|
||||
|
||||
// type Handlers struct {
|
||||
// fs *FSData
|
||||
// db *gdbm.Database
|
||||
// flagManager *FlagManager
|
||||
// accountFileHandler utils.AccountFileHandlerInterface
|
||||
// accountService server.AccountServiceInterface
|
||||
// }
|
||||
|
||||
type Handlers struct {
|
||||
st *state.State
|
||||
ca cache.Memory
|
||||
userdataStore db.Db
|
||||
st *state.State
|
||||
ca cache.Memory
|
||||
userdataStore db.Db
|
||||
flagManager *asm.FlagParser
|
||||
accountFileHandler *utils.AccountFileHandler
|
||||
accountService server.AccountServiceInterface
|
||||
@@ -91,15 +58,15 @@ type Handlers struct {
|
||||
|
||||
func NewHandlers(appFlags *asm.FlagParser, pe *persist.Persister, userdataStore db.Db) (*Handlers, error) {
|
||||
h := &Handlers{
|
||||
st: state.NewState(uint32(16)),
|
||||
ca: pe.GetMemory(),
|
||||
userdataStore: userdataStore,
|
||||
st: pe.GetState(),
|
||||
ca: pe.GetMemory(),
|
||||
userdataStore: userdataStore,
|
||||
flagManager: appFlags,
|
||||
accountFileHandler: utils.NewAccountFileHandler(userdataStore),
|
||||
accountService: &server.AccountService{},
|
||||
}
|
||||
if h.st == nil || h.ca == nil || h.userdataStore == nil || h.flagManager == nil {
|
||||
//logg.Errorf("have nil for essential value in handler", "state", h.st, "cache", h.ca, "store", h.userdataStore, "flags", h.flagManager)
|
||||
logg.Errorf("have nil for essential value in handler", "state", h.st, "cache", h.ca, "store", h.userdataStore, "flags", h.flagManager)
|
||||
return nil, fmt.Errorf("have nil for essential value")
|
||||
}
|
||||
return h, nil
|
||||
@@ -140,6 +107,16 @@ func (h *Handlers) SetLanguage(ctx context.Context, sym string, input []byte) (r
|
||||
|
||||
func (h *Handlers) createAccountNoExist(ctx context.Context, sessionId string, res *resource.Result) error {
|
||||
accountResp, err := h.accountService.CreateAccount()
|
||||
if err != nil {
|
||||
flag_account_creation_failed, _ := h.flagManager.GetFlag("flag_account_creation_failed")
|
||||
res.FlagSet = append(res.FlagSet, flag_account_creation_failed)
|
||||
return err
|
||||
}
|
||||
// data := map[string]string{
|
||||
// TrackingIdKey: accountResp.Result.TrackingId,
|
||||
// PublicKeyKey: accountResp.Result.PublicKey,
|
||||
// CustodialIdKey: accountResp.Result.CustodialId.String(),
|
||||
// }
|
||||
data := map[utils.DataTyp]string{
|
||||
utils.DATA_TRACKING_ID: accountResp.Result.TrackingId,
|
||||
utils.DATA_PUBLIC_KEY: accountResp.Result.PublicKey,
|
||||
@@ -152,39 +129,34 @@ func (h *Handlers) createAccountNoExist(ctx context.Context, sessionId string, r
|
||||
return err
|
||||
}
|
||||
}
|
||||
// NOTE: this is covered by the flag, no?
|
||||
//key := []byte(AccountCreated)
|
||||
//value := []byte("1")
|
||||
//h.db.Store(key, value, true)
|
||||
flag_account_created, _ := h.flagManager.GetFlag("flag_account_created")
|
||||
res.FlagSet = append(res.FlagSet, flag_account_created)
|
||||
return err
|
||||
|
||||
}
|
||||
|
||||
// CreateAccount checks if any account exists on the JSON data file, and if not
|
||||
// creates an account on the API,
|
||||
// sets the default values and flags
|
||||
func (h *Handlers) CreateAccount(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||
res := resource.Result{}
|
||||
var err error
|
||||
var res resource.Result
|
||||
|
||||
sessionId, ok := ctx.Value("SessionId").(string)
|
||||
if !ok {
|
||||
return res, fmt.Errorf("missing session")
|
||||
}
|
||||
|
||||
_, err = utils.ReadEntry(ctx, h.userdataStore, sessionId, utils.DATA_ACCOUNT_CREATED)
|
||||
if err != nil {
|
||||
if db.IsNotFound(err) {
|
||||
fmt.Println("Creating an account because it doesn't exist")
|
||||
err = h.createAccountNoExist(ctx, sessionId, &res)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
} else {
|
||||
fmt.Println("Error here:", err)
|
||||
err = h.createAccountNoExist(ctx, sessionId, &res)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
}
|
||||
}
|
||||
return res, nil
|
||||
return res, err
|
||||
}
|
||||
|
||||
// SavePin persists the user's PIN choice into the filesystem
|
||||
@@ -202,10 +174,10 @@ func (h *Handlers) SavePin(ctx context.Context, sym string, input []byte) (resou
|
||||
|
||||
res.FlagReset = append(res.FlagReset, flag_incorrect_pin)
|
||||
|
||||
// key := []byte(AccountPin)
|
||||
// value := []byte(accountPIN)
|
||||
key := []byte(AccountPin)
|
||||
value := []byte(accountPIN)
|
||||
|
||||
//h.db.Store(key, value, true)
|
||||
h.db.Store(key, value, true)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
@@ -244,11 +216,10 @@ func (h *Handlers) VerifyPin(ctx context.Context, sym string, input []byte) (res
|
||||
flag_pin_mismatch, _ := h.flagManager.GetFlag("flag_pin_mismatch")
|
||||
flag_pin_set, _ := h.flagManager.GetFlag("flag_pin_set")
|
||||
|
||||
// AccountPin, err := h.db.Fetch([]byte(AccountPin))
|
||||
// if err != nil {
|
||||
// return res, err
|
||||
// }
|
||||
AccountPin := []byte("2768")
|
||||
AccountPin, err := h.db.Fetch([]byte(AccountPin))
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
if bytes.Equal(input, AccountPin) {
|
||||
res.FlagSet = []uint32{flag_valid_pin}
|
||||
res.FlagReset = []uint32{flag_pin_mismatch}
|
||||
@@ -274,10 +245,10 @@ func codeFromCtx(ctx context.Context) string {
|
||||
func (h *Handlers) SaveFirstname(cxt context.Context, sym string, input []byte) (resource.Result, error) {
|
||||
res := resource.Result{}
|
||||
if len(input) > 0 {
|
||||
//name := string(input)
|
||||
//key := []byte(FirstName)
|
||||
//value := []byte(name)
|
||||
//h.db.Store(key, value, true)
|
||||
name := string(input)
|
||||
key := []byte(FirstName)
|
||||
value := []byte(name)
|
||||
h.db.Store(key, value, true)
|
||||
}
|
||||
|
||||
return res, nil
|
||||
@@ -287,10 +258,10 @@ func (h *Handlers) SaveFirstname(cxt context.Context, sym string, input []byte)
|
||||
func (h *Handlers) SaveFamilyname(cxt context.Context, sym string, input []byte) (resource.Result, error) {
|
||||
res := resource.Result{}
|
||||
if len(input) > 0 {
|
||||
//secondname := string(input)
|
||||
//key := []byte(FamilyName)
|
||||
//value := []byte(secondname)
|
||||
//h.db.Store(key, value, true)
|
||||
secondname := string(input)
|
||||
key := []byte(FamilyName)
|
||||
value := []byte(secondname)
|
||||
h.db.Store(key, value, true)
|
||||
}
|
||||
|
||||
return res, nil
|
||||
@@ -301,10 +272,10 @@ func (h *Handlers) SaveYob(cxt context.Context, sym string, input []byte) (resou
|
||||
res := resource.Result{}
|
||||
yob := string(input)
|
||||
if len(yob) == 4 {
|
||||
//yob := string(input)
|
||||
//key := []byte(YearOfBirth)
|
||||
//value := []byte(yob)
|
||||
//h.db.Store(key, value, true)
|
||||
yob := string(input)
|
||||
key := []byte(YearOfBirth)
|
||||
value := []byte(yob)
|
||||
h.db.Store(key, value, true)
|
||||
}
|
||||
|
||||
return res, nil
|
||||
@@ -314,11 +285,11 @@ func (h *Handlers) SaveYob(cxt context.Context, sym string, input []byte) (resou
|
||||
func (h *Handlers) SaveLocation(cxt context.Context, sym string, input []byte) (resource.Result, error) {
|
||||
res := resource.Result{}
|
||||
if len(input) > 0 {
|
||||
//location := string(input)
|
||||
//key := []byte(Location)
|
||||
//value := []byte(location)
|
||||
location := string(input)
|
||||
key := []byte(Location)
|
||||
value := []byte(location)
|
||||
|
||||
//h.db.Store(key, value, true)
|
||||
h.db.Store(key, value, true)
|
||||
}
|
||||
|
||||
return res, nil
|
||||
@@ -337,9 +308,9 @@ func (h *Handlers) SaveGender(ctx context.Context, sym string, input []byte) (re
|
||||
case "3":
|
||||
gender = "Unspecified"
|
||||
}
|
||||
//key := []byte(Gender)
|
||||
//value := []byte(gender)
|
||||
//h.db.Store(key, value, true)
|
||||
key := []byte(Gender)
|
||||
value := []byte(gender)
|
||||
h.db.Store(key, value, true)
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
@@ -348,10 +319,10 @@ func (h *Handlers) SaveGender(ctx context.Context, sym string, input []byte) (re
|
||||
func (h *Handlers) SaveOfferings(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||
res := resource.Result{}
|
||||
if len(input) > 0 {
|
||||
//offerings := string(input)
|
||||
//key := []byte(Offerings)
|
||||
//value := []byte(offerings)
|
||||
//h.db.Store(key, value, true)
|
||||
offerings := string(input)
|
||||
key := []byte(Offerings)
|
||||
value := []byte(offerings)
|
||||
h.db.Store(key, value, true)
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
@@ -379,11 +350,11 @@ func (h *Handlers) ResetAccountAuthorized(ctx context.Context, sym string, input
|
||||
// CheckIdentifier retrieves the PublicKey from the JSON data file.
|
||||
func (h *Handlers) CheckIdentifier(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||
res := resource.Result{}
|
||||
//publicKey, err := h.db.Fetch([]byte(PublicKeyKey))
|
||||
// if err != nil {
|
||||
// return res, err
|
||||
// }
|
||||
res.Content = "string(publicKey)"
|
||||
publicKey, err := h.db.Fetch([]byte(PublicKeyKey))
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
res.Content = string(publicKey)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
@@ -392,32 +363,32 @@ func (h *Handlers) CheckIdentifier(ctx context.Context, sym string, input []byte
|
||||
func (h *Handlers) Authorize(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||
res := resource.Result{}
|
||||
|
||||
// flag_incorrect_pin, _ := h.flagManager.GetFlag("flag_incorrect_pin")
|
||||
// flag_account_authorized, _ := h.flagManager.GetFlag("flag_account_authorized")
|
||||
// flag_allow_update, _ := h.flagManager.GetFlag("flag_allow_update")
|
||||
flag_incorrect_pin, _ := h.flagManager.GetFlag("flag_incorrect_pin")
|
||||
flag_account_authorized, _ := h.flagManager.GetFlag("flag_account_authorized")
|
||||
flag_allow_update, _ := h.flagManager.GetFlag("flag_allow_update")
|
||||
|
||||
// storedpin, err := h.db.Fetch([]byte(AccountPin))
|
||||
// if err == nil {
|
||||
// if len(input) == 4 {
|
||||
// if bytes.Equal(input, storedpin) {
|
||||
// if h.fs.St.MatchFlag(flag_account_authorized, false) {
|
||||
// res.FlagReset = append(res.FlagReset, flag_incorrect_pin)
|
||||
// res.FlagSet = append(res.FlagSet, flag_allow_update, flag_account_authorized)
|
||||
// } else {
|
||||
// res.FlagSet = append(res.FlagSet, flag_allow_update)
|
||||
// res.FlagReset = append(res.FlagReset, flag_account_authorized)
|
||||
// }
|
||||
// } else {
|
||||
// res.FlagSet = append(res.FlagSet, flag_incorrect_pin)
|
||||
// res.FlagReset = append(res.FlagReset, flag_account_authorized)
|
||||
// return res, nil
|
||||
// }
|
||||
// }
|
||||
// } else if errors.Is(err, gdbm.ErrItemNotFound) {
|
||||
// return res, err
|
||||
// } else {
|
||||
// return res, err
|
||||
// }
|
||||
storedpin, err := h.db.Fetch([]byte(AccountPin))
|
||||
if err == nil {
|
||||
if len(input) == 4 {
|
||||
if bytes.Equal(input, storedpin) {
|
||||
if h.fs.St.MatchFlag(flag_account_authorized, false) {
|
||||
res.FlagReset = append(res.FlagReset, flag_incorrect_pin)
|
||||
res.FlagSet = append(res.FlagSet, flag_allow_update, flag_account_authorized)
|
||||
} else {
|
||||
res.FlagSet = append(res.FlagSet, flag_allow_update)
|
||||
res.FlagReset = append(res.FlagReset, flag_account_authorized)
|
||||
}
|
||||
} else {
|
||||
res.FlagSet = append(res.FlagSet, flag_incorrect_pin)
|
||||
res.FlagReset = append(res.FlagReset, flag_account_authorized)
|
||||
return res, nil
|
||||
}
|
||||
}
|
||||
} else if errors.Is(err, gdbm.ErrItemNotFound) {
|
||||
return res, err
|
||||
} else {
|
||||
return res, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
@@ -439,15 +410,13 @@ func (h *Handlers) CheckAccountStatus(ctx context.Context, sym string, input []b
|
||||
flag_account_success, _ := h.flagManager.GetFlag("flag_account_success")
|
||||
flag_account_pending, _ := h.flagManager.GetFlag("flag_account_pending")
|
||||
|
||||
sessionId, ok := ctx.Value("SessionId").(string)
|
||||
if !ok {
|
||||
return res, fmt.Errorf("missing session")
|
||||
trackingId, err := h.db.Fetch([]byte(TrackingIdKey))
|
||||
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
trackingId, err := utils.ReadEntry(ctx, h.userdataStore, sessionId, utils.DATA_TRACKING_ID)
|
||||
fmt.Println("Checking status with tracking id:", string(trackingId))
|
||||
|
||||
status, err := h.accountService.CheckAccountStatus(string("1234"))
|
||||
status, err := h.accountService.CheckAccountStatus(string(trackingId))
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("Error checking account status:", err)
|
||||
@@ -455,15 +424,15 @@ func (h *Handlers) CheckAccountStatus(ctx context.Context, sym string, input []b
|
||||
|
||||
}
|
||||
|
||||
// err = h.db.Store(toBytes(AccountStatus), toBytes(status), true)
|
||||
// if err != nil {
|
||||
// return res, nil
|
||||
// }
|
||||
err = h.db.Store([]byte(AccountStatus), []byte(status), true)
|
||||
if err != nil {
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// err = h.db.Store(toBytes(TrackingIdKey), toBytes(status), true)
|
||||
// if err != nil {
|
||||
// return res, nil
|
||||
// }
|
||||
err = h.db.Store([]byte(TrackingIdKey), []byte(status), true)
|
||||
if err != nil {
|
||||
return res, nil
|
||||
}
|
||||
|
||||
if status == "SUCCESS" {
|
||||
res.FlagSet = append(res.FlagSet, flag_account_success)
|
||||
@@ -527,13 +496,13 @@ func (h *Handlers) ResetIncorrectYob(ctx context.Context, sym string, input []by
|
||||
// the balance as the result content
|
||||
func (h *Handlers) CheckBalance(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||
res := resource.Result{}
|
||||
// publicKey, err := h.db.Fetch([]byte(PublicKeyKey))
|
||||
publicKey, err := h.db.Fetch([]byte(PublicKeyKey))
|
||||
|
||||
// if err != nil {
|
||||
// return res, err
|
||||
// }
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
balance, err := h.accountService.CheckBalance(string("publicKey"))
|
||||
balance, err := h.accountService.CheckBalance(string(publicKey))
|
||||
if err != nil {
|
||||
return res, nil
|
||||
}
|
||||
@@ -559,10 +528,10 @@ func (h *Handlers) ValidateRecipient(ctx context.Context, sym string, input []by
|
||||
}
|
||||
|
||||
// accountData["Recipient"] = recipient
|
||||
// key := []byte(Recipient)
|
||||
// value := []byte(recipient)
|
||||
key := []byte(Recipient)
|
||||
value := []byte(recipient)
|
||||
|
||||
// h.db.Store(key, value, true)
|
||||
h.db.Store(key, value, true)
|
||||
}
|
||||
|
||||
return res, nil
|
||||
@@ -576,14 +545,14 @@ func (h *Handlers) TransactionReset(ctx context.Context, sym string, input []byt
|
||||
flag_invalid_recipient, _ := h.flagManager.GetFlag("flag_invalid_recipient")
|
||||
flag_invalid_recipient_with_invite, _ := h.flagManager.GetFlag("flag_invalid_recipient_with_invite")
|
||||
|
||||
// err := h.db.Delete([]byte(Amount))
|
||||
// if err != nil && !errors.Is(err, gdbm.ErrItemNotFound) {
|
||||
// return res, err
|
||||
// }
|
||||
// err = h.db.Delete([]byte(Recipient))
|
||||
// if err != nil && !errors.Is(err, gdbm.ErrItemNotFound) {
|
||||
// return res, err
|
||||
// }
|
||||
err := h.db.Delete([]byte(Amount))
|
||||
if err != nil && !errors.Is(err, gdbm.ErrItemNotFound) {
|
||||
return res, err
|
||||
}
|
||||
err = h.db.Delete([]byte(Recipient))
|
||||
if err != nil && !errors.Is(err, gdbm.ErrItemNotFound) {
|
||||
return res, err
|
||||
}
|
||||
|
||||
res.FlagReset = append(res.FlagReset, flag_invalid_recipient, flag_invalid_recipient_with_invite)
|
||||
|
||||
@@ -596,10 +565,10 @@ func (h *Handlers) ResetTransactionAmount(ctx context.Context, sym string, input
|
||||
|
||||
flag_invalid_amount, _ := h.flagManager.GetFlag("flag_invalid_amount")
|
||||
|
||||
// err := h.db.Delete([]byte(Amount))
|
||||
// if err != nil && !errors.Is(err, gdbm.ErrItemNotFound) {
|
||||
// return res, err
|
||||
// }
|
||||
err := h.db.Delete([]byte(Amount))
|
||||
if err != nil && !errors.Is(err, gdbm.ErrItemNotFound) {
|
||||
return res, err
|
||||
}
|
||||
|
||||
res.FlagReset = append(res.FlagReset, flag_invalid_amount)
|
||||
|
||||
@@ -610,12 +579,12 @@ func (h *Handlers) ResetTransactionAmount(ctx context.Context, sym string, input
|
||||
// the result content.
|
||||
func (h *Handlers) MaxAmount(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||
res := resource.Result{}
|
||||
// publicKey, err := h.db.Fetch([]byte(PublicKeyKey))
|
||||
// if err != nil {
|
||||
// return res, err
|
||||
// }
|
||||
publicKey, err := h.db.Fetch([]byte(PublicKeyKey))
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
balance, err := h.accountService.CheckBalance(string("publicKey"))
|
||||
balance, err := h.accountService.CheckBalance(string(publicKey))
|
||||
if err != nil {
|
||||
return res, nil
|
||||
}
|
||||
@@ -633,13 +602,13 @@ func (h *Handlers) ValidateAmount(ctx context.Context, sym string, input []byte)
|
||||
flag_invalid_amount, _ := h.flagManager.GetFlag("flag_invalid_amount")
|
||||
|
||||
amountStr := string(input)
|
||||
// publicKey, err := h.db.Fetch([]byte(PublicKeyKey))
|
||||
publicKey, err := h.db.Fetch([]byte(PublicKeyKey))
|
||||
|
||||
// if err != nil {
|
||||
// return res, err
|
||||
// }
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
balanceStr, err := h.accountService.CheckBalance(string("publicKey"))
|
||||
balanceStr, err := h.accountService.CheckBalance(string(publicKey))
|
||||
|
||||
if err != nil {
|
||||
return res, err
|
||||
@@ -679,9 +648,9 @@ func (h *Handlers) ValidateAmount(ctx context.Context, sym string, input []byte)
|
||||
}
|
||||
|
||||
res.Content = fmt.Sprintf("%.3f", inputAmount) // Format to 3 decimal places
|
||||
// key := []byte(Amount)
|
||||
// value := []byte(res.Content)
|
||||
// h.db.Store(key, value, true)
|
||||
key := []byte(Amount)
|
||||
value := []byte(res.Content)
|
||||
h.db.Store(key, value, true)
|
||||
|
||||
if err != nil {
|
||||
return res, err
|
||||
@@ -693,12 +662,12 @@ func (h *Handlers) ValidateAmount(ctx context.Context, sym string, input []byte)
|
||||
// GetRecipient returns the transaction recipient from a JSON data file.
|
||||
func (h *Handlers) GetRecipient(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||
res := resource.Result{}
|
||||
// recipient, err := h.db.Fetch([]byte(Recipient))
|
||||
// if err != nil {
|
||||
// return res, err
|
||||
// }
|
||||
recipient, err := h.db.Fetch([]byte(Recipient))
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
res.Content = string("recipient")
|
||||
res.Content = string(recipient)
|
||||
|
||||
return res, nil
|
||||
}
|
||||
@@ -706,12 +675,12 @@ func (h *Handlers) GetRecipient(ctx context.Context, sym string, input []byte) (
|
||||
// GetSender retrieves the public key from the Gdbm Db
|
||||
func (h *Handlers) GetSender(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||
res := resource.Result{}
|
||||
// publicKey, err := h.db.Fetch([]byte(PublicKeyKey))
|
||||
// if err != nil {
|
||||
// return res, err
|
||||
// }
|
||||
publicKey, err := h.db.Fetch([]byte(PublicKeyKey))
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
res.Content = string("publicKey")
|
||||
res.Content = string(publicKey)
|
||||
|
||||
return res, nil
|
||||
}
|
||||
@@ -719,11 +688,11 @@ func (h *Handlers) GetSender(ctx context.Context, sym string, input []byte) (res
|
||||
// GetAmount retrieves the amount from teh Gdbm Db
|
||||
func (h *Handlers) GetAmount(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||
res := resource.Result{}
|
||||
// amount, err := h.db.Fetch([]byte(Amount))
|
||||
// if err != nil {
|
||||
// return res, err
|
||||
// }
|
||||
res.Content = string("amount")
|
||||
amount, err := h.db.Fetch([]byte(Amount))
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
res.Content = string(amount)
|
||||
|
||||
return res, nil
|
||||
}
|
||||
@@ -738,11 +707,11 @@ func (h *Handlers) QuitWithBalance(ctx context.Context, sym string, input []byte
|
||||
code := codeFromCtx(ctx)
|
||||
l := gotext.NewLocale(translationDir, code)
|
||||
l.AddDomain("default")
|
||||
// publicKey, err := h.db.Fetch([]byte(PublicKeyKey))
|
||||
// if err != nil {
|
||||
// return res, err
|
||||
// }
|
||||
balance, err := h.accountService.CheckBalance(string("publicKey"))
|
||||
publicKey, err := h.db.Fetch([]byte(PublicKeyKey))
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
balance, err := h.accountService.CheckBalance(string(publicKey))
|
||||
if err != nil {
|
||||
return res, nil
|
||||
}
|
||||
@@ -761,20 +730,20 @@ func (h *Handlers) InitiateTransaction(ctx context.Context, sym string, input []
|
||||
// TODO
|
||||
// Use the amount, recipient and sender to call the API and initialize the transaction
|
||||
|
||||
// publicKey, err := h.db.Fetch([]byte(PublicKeyKey))
|
||||
// if err != nil {
|
||||
// return res, err
|
||||
// }
|
||||
// amount, err := h.db.Fetch([]byte(Amount))
|
||||
// if err != nil {
|
||||
// return res, err
|
||||
// }
|
||||
// recipient, err := h.db.Fetch([]byte(Recipient))
|
||||
// if err != nil {
|
||||
// return res, err
|
||||
// }
|
||||
publicKey, err := h.db.Fetch([]byte(PublicKeyKey))
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
amount, err := h.db.Fetch([]byte(Amount))
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
recipient, err := h.db.Fetch([]byte(Recipient))
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
//res.Content = l.Get("Your request has been sent. %s will receive %s from %s.", string(recipient), string(amount), string(publicKey))
|
||||
res.Content = l.Get("Your request has been sent. %s will receive %s from %s.", string(recipient), string(amount), string(publicKey))
|
||||
|
||||
account_authorized_flag, err := h.flagManager.GetFlag("flag_account_authorized")
|
||||
if err != nil {
|
||||
@@ -799,27 +768,27 @@ func (h *Handlers) GetProfileInfo(ctx context.Context, sym string, input []byte)
|
||||
offerings := defaultValue
|
||||
|
||||
// Fetch data using a map for better organization
|
||||
// dataKeys := map[string]*string{
|
||||
// FirstName: &name,
|
||||
// FamilyName: &familyName,
|
||||
// YearOfBirth: &yob,
|
||||
// Location: &location,
|
||||
// Gender: &gender,
|
||||
// Offerings: &offerings,
|
||||
// }
|
||||
dataKeys := map[string]*string{
|
||||
FirstName: &name,
|
||||
FamilyName: &familyName,
|
||||
YearOfBirth: &yob,
|
||||
Location: &location,
|
||||
Gender: &gender,
|
||||
Offerings: &offerings,
|
||||
}
|
||||
|
||||
// Iterate over keys and fetch values
|
||||
//iter := h.db.Iterator()
|
||||
// next := h.db.Iterator()
|
||||
// //defer iter.Close() // Ensure the iterator is closed
|
||||
// for key, err := next(); err == nil; key, err = next() {
|
||||
// if valuePointer, ok := dataKeys[string(key)]; ok {
|
||||
// // value, fetchErr := h.db.Fetch(key)
|
||||
// // if fetchErr == nil {
|
||||
// // *valuePointer = string(value)
|
||||
// // }
|
||||
// }
|
||||
// }
|
||||
next := h.db.Iterator()
|
||||
//defer iter.Close() // Ensure the iterator is closed
|
||||
for key, err := next(); err == nil; key, err = next() {
|
||||
if valuePointer, ok := dataKeys[string(key)]; ok {
|
||||
value, fetchErr := h.db.Fetch(key)
|
||||
if fetchErr == nil {
|
||||
*valuePointer = string(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Construct the full name
|
||||
if familyName != defaultValue {
|
||||
|
||||
@@ -8,35 +8,15 @@ import (
|
||||
)
|
||||
|
||||
type AccountFileHandler struct {
|
||||
//FilePath string
|
||||
store db.Db
|
||||
}
|
||||
|
||||
// func NewAccountFileHandler(path string) *AccountFileHandler {
|
||||
// return &AccountFileHandler{FilePath: path}
|
||||
// }
|
||||
|
||||
func NewAccountFileHandler(store db.Db) *AccountFileHandler {
|
||||
return &AccountFileHandler{
|
||||
store: store,
|
||||
}
|
||||
}
|
||||
|
||||
// func (afh *AccountFileHandler) ReadAccountData() (map[string]string, error) {
|
||||
// jsonData, err := os.ReadFile(afh.FilePath)
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
|
||||
// var accountData map[string]string
|
||||
// err = json.Unmarshal(jsonData, &accountData)
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
|
||||
// return accountData, nil
|
||||
// }
|
||||
|
||||
func (afh *AccountFileHandler) ReadAccountData(ctx context.Context, sessionId string) (map[string]string, error) {
|
||||
var accountData map[string]string
|
||||
jsonData, err := ReadEntry(ctx, afh.store, sessionId, DATA_ACCOUNT)
|
||||
@@ -44,34 +24,14 @@ func (afh *AccountFileHandler) ReadAccountData(ctx context.Context, sessionId st
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return accountData, nil
|
||||
}
|
||||
|
||||
// func (afh *AccountFileHandler) WriteAccountData(accountData map[string]string) error {
|
||||
// jsonData, err := json.Marshal(accountData)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
|
||||
// return os.WriteFile(afh.FilePath, jsonData, 0644)
|
||||
// }
|
||||
|
||||
func (afh *AccountFileHandler) WriteAccountData(ctx context.Context, sessionId string, accountData map[string]string) error {
|
||||
_, err := json.Marshal(accountData)
|
||||
b, err := json.Marshal(accountData)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// func (afh *AccountFileHandler) EnsureFileExists() error {
|
||||
// f, err := os.OpenFile(afh.FilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// return f.Close()
|
||||
// }
|
||||
func (afh *AccountFileHandler) EnsureFileExists() error {
|
||||
return nil
|
||||
return WriteEntry(ctx, afh.store, sessionId, DATA_ACCOUNT, b)
|
||||
}
|
||||
|
||||
48
internal/utils/db.go
Normal file
48
internal/utils/db.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/binary"
|
||||
|
||||
"git.defalsify.org/vise.git/db"
|
||||
)
|
||||
|
||||
type DataTyp uint16
|
||||
|
||||
const (
|
||||
DATA_ACCOUNT DataTyp = iota
|
||||
DATA_ACCOUNT_CREATED
|
||||
DATA_TRACKING_ID
|
||||
DATA_PUBLIC_KEY
|
||||
DATA_CUSTODIAL_ID
|
||||
)
|
||||
|
||||
func typToBytes(typ DataTyp) []byte {
|
||||
var b []byte
|
||||
binary.BigEndian.PutUint16(b, uint16(typ))
|
||||
return b
|
||||
}
|
||||
|
||||
func packKey(typ DataTyp, data []byte) []byte {
|
||||
v := typToBytes(typ)
|
||||
return append(v, data...)
|
||||
}
|
||||
|
||||
func ReadEntry(ctx context.Context, store db.Db, sessionId string, typ DataTyp) ([]byte, error) {
|
||||
store.SetPrefix(db.DATATYPE_USERDATA)
|
||||
store.SetSession(sessionId)
|
||||
k := packKey(typ, []byte(sessionId))
|
||||
b, err := store.Get(ctx, k)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return b, nil
|
||||
|
||||
}
|
||||
|
||||
func WriteEntry(ctx context.Context, store db.Db, sessionId string, typ DataTyp, value []byte) error {
|
||||
store.SetPrefix(db.DATATYPE_USERDATA)
|
||||
store.SetSession(sessionId)
|
||||
k := packKey(typ, []byte(sessionId))
|
||||
return store.Put(ctx, k, value)
|
||||
}
|
||||
@@ -1,13 +1 @@
|
||||
package utils
|
||||
|
||||
|
||||
|
||||
|
||||
type AccountFileHandlerInterface interface {
|
||||
EnsureFileExists() error
|
||||
ReadAccountData() (map[string]string, error)
|
||||
WriteAccountData(data map[string]string) error
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user