WIP implement dev api balance, account create
This commit is contained in:
parent
6fc27c7a81
commit
56f3266378
69
dev/api.go
Normal file
69
dev/api.go
Normal file
@ -0,0 +1,69 @@
|
||||
package dev
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"strconv"
|
||||
|
||||
"github.com/gofrs/uuid"
|
||||
"git.grassecon.net/grassrootseconomics/sarafu-api/models"
|
||||
)
|
||||
|
||||
const (
|
||||
pubKeyLen int = 20
|
||||
)
|
||||
|
||||
type account struct {
|
||||
balance int
|
||||
nonce int
|
||||
}
|
||||
|
||||
type DevAccountService struct {
|
||||
accounts map[string]account
|
||||
accountsTrack map[string]string
|
||||
// accountsSession map[string]string
|
||||
}
|
||||
|
||||
func NewDevAccountService() *DevAccountService {
|
||||
return &DevAccountService{
|
||||
accounts: make(map[string]account),
|
||||
accountsTrack: make(map[string]string),
|
||||
//accountsSession: make(map[string]string),
|
||||
}
|
||||
}
|
||||
|
||||
func (das *DevAccountService) CheckBalance(ctx context.Context, publicKey string) (*models.BalanceResult, error) {
|
||||
acc, ok := das.accounts[publicKey]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("account not found (publickey): %v", publicKey)
|
||||
}
|
||||
return &models.BalanceResult {
|
||||
Balance: strconv.Itoa(acc.balance),
|
||||
Nonce: json.Number(strconv.Itoa(acc.nonce)),
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
func (das *DevAccountService) CreateAccount(ctx context.Context) (*models.AccountResult, error) {
|
||||
var b [pubKeyLen]byte
|
||||
uid, err := uuid.NewV4()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c, err := rand.Read(b[:])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if c != pubKeyLen {
|
||||
return nil, fmt.Errorf("short read: %d", c)
|
||||
}
|
||||
pubKey := fmt.Sprintf("0x%x", b)
|
||||
das.accounts[pubKey] = account{}
|
||||
das.accountsTrack[uid.String()] = pubKey
|
||||
return &models.AccountResult{
|
||||
PublicKey: pubKey,
|
||||
TrackingId: uid.String(),
|
||||
}, nil
|
||||
}
|
1
go.mod
1
go.mod
@ -4,6 +4,7 @@ go 1.23.4
|
||||
|
||||
require (
|
||||
git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250112121325-9e4c65c8b4d1
|
||||
github.com/gofrs/uuid v4.4.0+incompatible
|
||||
github.com/grassrootseconomics/eth-custodial v1.3.0-beta
|
||||
github.com/grassrootseconomics/ussd-data-service v1.2.0-beta
|
||||
github.com/stretchr/testify v1.9.0
|
||||
|
2
go.sum
2
go.sum
@ -2,6 +2,8 @@ git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250112121325
|
||||
git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250112121325-9e4c65c8b4d1/go.mod h1:E6W7ZOa7ZvVr0Bc5ot0LNSwpSPYq4hXlAIvEPy3AJ7U=
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/gofrs/uuid v4.4.0+incompatible h1:3qXRTX8/NbyulANqlc0lchS1gqAVxRgsuW1YrTJupqA=
|
||||
github.com/gofrs/uuid v4.4.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
|
||||
github.com/grassrootseconomics/eth-custodial v1.3.0-beta h1:twrMBhl89GqDUL9PlkzQxMP/6OST1BByrNDj+rqXDmU=
|
||||
github.com/grassrootseconomics/eth-custodial v1.3.0-beta/go.mod h1:7uhRcdnJplX4t6GKCEFkbeDhhjlcaGJeJqevbcvGLZo=
|
||||
github.com/grassrootseconomics/ussd-data-service v1.2.0-beta h1:fn1gwbWIwHVEBtUC2zi5OqTlfI/5gU1SMk0fgGixIXk=
|
||||
|
Loading…
Reference in New Issue
Block a user