mirror of
https://github.com/grassrootseconomics/cic-custodial.git
synced 2024-11-23 06:36:46 +01:00
refactor: breaking API changes
Squashed commit of the following: commit05e1396121
Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Wed Feb 15 10:03:44 2023 +0300 feat: add status types to dispatcher commit397cd78ca9
Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Wed Feb 15 09:39:31 2023 +0300 deps: bump -> cic-celo-sdk commitf2ba079232
Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Sun Feb 12 16:53:53 2023 +0300 snapshot: 12-ebening commit4f7909e4ee
Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Sun Feb 12 12:50:43 2023 +0300 xnapshot: 12-02 commit773474cad9
Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Thu Feb 9 14:23:37 2023 +0300 update: deps initializers commit8a0880fcfc
Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Thu Feb 9 10:42:15 2023 +0300 wip: refactor taskers commit8676450122
Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Fri Feb 3 12:29:27 2023 +0300 refactor: decouple sql queries, remove transfer * add inline docs * removed transfer taks in prep for re-write commitb4c09cd11a
Author: Mohammed Sohail <sohailsameja@gmail.com> Date: Thu Feb 2 12:29:43 2023 +0000 refactor: cmd/service/* and api
This commit is contained in:
parent
2a5c87b22c
commit
4d13a14dc2
@ -1,43 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/arl/statsviz"
|
||||
"github.com/go-playground/validator"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/api"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
func initApiServer() *echo.Echo {
|
||||
lo.Debug("bootstrapping api server")
|
||||
server := echo.New()
|
||||
server.HideBanner = true
|
||||
server.HidePort = true
|
||||
|
||||
if ko.Bool("service.statsviz_debug") {
|
||||
lo.Debug("Starting stats_viz at /debug/statsviz")
|
||||
statsVizMux := http.NewServeMux()
|
||||
_ = statsviz.Register(statsVizMux)
|
||||
server.GET("/debug/statsviz/", echo.WrapHandler(statsVizMux))
|
||||
server.GET("/debug/statsviz/*", echo.WrapHandler(statsVizMux))
|
||||
}
|
||||
|
||||
server.Validator = &api.CustomValidator{
|
||||
Validator: validator.New(),
|
||||
}
|
||||
|
||||
apiRoute := server.Group("/api")
|
||||
|
||||
apiRoute.POST("/register", api.RegistrationHandler(
|
||||
taskerClient,
|
||||
postgresKeystore,
|
||||
))
|
||||
|
||||
apiRoute.POST("/transfer", api.TransferHandler(
|
||||
taskerClient,
|
||||
))
|
||||
|
||||
lo.Debug("Registered all api handlers")
|
||||
return server
|
||||
}
|
144
cmd/init_core.go
144
cmd/init_core.go
@ -1,144 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/bsm/redislock"
|
||||
celo "github.com/grassrootseconomics/cic-celo-sdk"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/keystore"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/nonce"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/tasker"
|
||||
"github.com/grassrootseconomics/cic-custodial/pkg/logg"
|
||||
"github.com/grassrootseconomics/cic-custodial/pkg/postgres"
|
||||
"github.com/grassrootseconomics/cic-custodial/pkg/redis"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/knadh/koanf"
|
||||
"github.com/knadh/koanf/parsers/toml"
|
||||
"github.com/knadh/koanf/providers/env"
|
||||
"github.com/knadh/koanf/providers/file"
|
||||
"github.com/zerodha/logf"
|
||||
)
|
||||
|
||||
func initConfig(configFilePath string) *koanf.Koanf {
|
||||
var (
|
||||
ko = koanf.New(".")
|
||||
)
|
||||
|
||||
confFile := file.Provider(configFilePath)
|
||||
if err := ko.Load(confFile, toml.Parser()); err != nil {
|
||||
lo.Fatal("Could not load config file", "error", err)
|
||||
}
|
||||
|
||||
if err := ko.Load(env.Provider("", ".", func(s string) string {
|
||||
return strings.ReplaceAll(strings.ToLower(
|
||||
strings.TrimPrefix(s, "")), "_", ".")
|
||||
}), nil); err != nil {
|
||||
lo.Fatal("Could not override config from env vars", "error", err)
|
||||
}
|
||||
|
||||
return ko
|
||||
}
|
||||
|
||||
func initLogger(debug bool) logf.Logger {
|
||||
loggOpts := logg.LoggOpts{
|
||||
Color: true,
|
||||
}
|
||||
|
||||
if debug {
|
||||
loggOpts.Caller = true
|
||||
loggOpts.Debug = true
|
||||
}
|
||||
|
||||
return logg.NewLogg(loggOpts)
|
||||
}
|
||||
|
||||
func initCeloProvider() *celo.Provider {
|
||||
providerOpts := celo.ProviderOpts{
|
||||
RpcEndpoint: ko.MustString("chain.rpc_endpoint"),
|
||||
}
|
||||
|
||||
if ko.Bool("chain.testnet") {
|
||||
providerOpts.ChainId = celo.TestnetChainId
|
||||
} else {
|
||||
providerOpts.ChainId = celo.MainnetChainId
|
||||
}
|
||||
|
||||
provider, err := celo.NewProvider(providerOpts)
|
||||
if err != nil {
|
||||
lo.Fatal("initChainProvider", "error", err)
|
||||
}
|
||||
|
||||
return provider
|
||||
}
|
||||
|
||||
func initPostgresPool() *pgxpool.Pool {
|
||||
poolOpts := postgres.PostgresPoolOpts{
|
||||
DSN: ko.MustString("postgres.dsn"),
|
||||
}
|
||||
|
||||
pool, err := postgres.NewPostgresPool(poolOpts)
|
||||
if err != nil {
|
||||
lo.Fatal("initPostgresPool", "error", err)
|
||||
}
|
||||
|
||||
return pool
|
||||
}
|
||||
|
||||
func initKeystore() keystore.Keystore {
|
||||
keystore, err := keystore.NewPostgresKeytore(keystore.Opts{
|
||||
PostgresPool: postgresPool,
|
||||
Logg: lo,
|
||||
})
|
||||
if err != nil {
|
||||
lo.Fatal("initKeystore", "error", err)
|
||||
}
|
||||
|
||||
return keystore
|
||||
}
|
||||
|
||||
func initAsynqRedisPool() *redis.RedisPool {
|
||||
poolOpts := redis.RedisPoolOpts{
|
||||
DSN: ko.MustString("asynq.dsn"),
|
||||
MinIdleConns: ko.MustInt("redis.minconn"),
|
||||
}
|
||||
|
||||
pool, err := redis.NewRedisPool(poolOpts)
|
||||
if err != nil {
|
||||
lo.Fatal("initAsynqRedisPool", "error", err)
|
||||
}
|
||||
|
||||
return pool
|
||||
}
|
||||
|
||||
func initCommonRedisPool() *redis.RedisPool {
|
||||
poolOpts := redis.RedisPoolOpts{
|
||||
DSN: ko.MustString("redis.dsn"),
|
||||
MinIdleConns: ko.MustInt("redis.minconn"),
|
||||
}
|
||||
|
||||
pool, err := redis.NewRedisPool(poolOpts)
|
||||
if err != nil {
|
||||
lo.Fatal("initCommonRedisPool", "error", err)
|
||||
}
|
||||
|
||||
return pool
|
||||
}
|
||||
|
||||
func initRedisNoncestore() nonce.Noncestore {
|
||||
return nonce.NewRedisNoncestore(nonce.Opts{
|
||||
RedisPool: commonRedisPool,
|
||||
ChainProvider: celoProvider,
|
||||
})
|
||||
}
|
||||
|
||||
func initLockProvider() *redislock.Client {
|
||||
return redislock.New(commonRedisPool.Client)
|
||||
}
|
||||
|
||||
func initTaskerClient() *tasker.TaskerClient {
|
||||
return tasker.NewTaskerClient(tasker.TaskerClientOpts{
|
||||
RedisPool: asynqRedisPool,
|
||||
TaskRetention: time.Hour * 12,
|
||||
})
|
||||
}
|
@ -1,58 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/ecdsa"
|
||||
"math/big"
|
||||
"time"
|
||||
|
||||
eth_crypto "github.com/celo-org/celo-blockchain/crypto"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/tasker"
|
||||
"github.com/grassrootseconomics/w3-celo-patch"
|
||||
)
|
||||
|
||||
func initSystemContainer() *tasker.SystemContainer {
|
||||
return &tasker.SystemContainer{
|
||||
Abis: initAbis(),
|
||||
GasRefillThreshold: big.NewInt(ko.MustInt64("system.gas_refill_threshold")),
|
||||
GasRefillValue: big.NewInt(ko.MustInt64("system.gas_refill_value")),
|
||||
GiftableGasValue: big.NewInt(ko.MustInt64("system.giftable_gas_value")),
|
||||
GiftableToken: w3.A(ko.MustString("system.giftable_token_address")),
|
||||
GiftableTokenValue: big.NewInt(ko.MustInt64("system.giftable_token_value")),
|
||||
LockPrefix: ko.MustString("system.lock_prefix"),
|
||||
LockTimeout: 1 * time.Second,
|
||||
PrivateKey: initSystemKey(),
|
||||
PublicKey: ko.MustString("system.public_key"),
|
||||
TokenDecimals: ko.MustInt("system.token_decimals"),
|
||||
TokenTransferGasLimit: uint64(ko.MustInt64("system.token_transfer_gas_limit")),
|
||||
}
|
||||
}
|
||||
|
||||
func initAbis() map[string]*w3.Func {
|
||||
return map[string]*w3.Func{
|
||||
"mint": w3.MustNewFunc("mint(address,uint256)", ""),
|
||||
"transfer": w3.MustNewFunc("transfer(address,uint256)", "bool"),
|
||||
}
|
||||
}
|
||||
|
||||
func initSystemKey() *ecdsa.PrivateKey {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
currentSystemNonce, err := redisNoncestore.Peek(ctx, ko.MustString("system.public_key"))
|
||||
lo.Debug("initNoncestore: loaded (noncestore) system nonce", "nonce", currentSystemNonce)
|
||||
if err != nil {
|
||||
nonce, err := redisNoncestore.SyncNetworkNonce(ctx, ko.MustString("system.public_key"))
|
||||
lo.Debug("initNoncestore: syncing system nonce", "nonce", nonce)
|
||||
if err != nil {
|
||||
lo.Fatal("initNonceStore", "error", "system account nonce sync failed")
|
||||
}
|
||||
}
|
||||
|
||||
loadedPrivateKey, err := eth_crypto.HexToECDSA(ko.MustString("system.private_key"))
|
||||
if err != nil {
|
||||
lo.Fatal("Failed to load system private key", "error", err)
|
||||
}
|
||||
|
||||
return loadedPrivateKey
|
||||
}
|
@ -1,65 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/tasker"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/tasker/task"
|
||||
"github.com/hibiken/asynq"
|
||||
)
|
||||
|
||||
func initTasker() *tasker.TaskerServer {
|
||||
lo.Debug("Bootstrapping tasker")
|
||||
|
||||
taskerServerOpts := tasker.TaskerServerOpts{
|
||||
Concurrency: ko.MustInt("asynq.concurrency"),
|
||||
Logg: lo,
|
||||
RedisPool: asynqRedisPool,
|
||||
SystemContainer: nil,
|
||||
TaskerClient: taskerClient,
|
||||
}
|
||||
|
||||
if debugFlag {
|
||||
taskerServerOpts.LogLevel = asynq.DebugLevel
|
||||
}
|
||||
|
||||
taskerServer := tasker.NewTaskerServer(taskerServerOpts)
|
||||
|
||||
taskerServer.RegisterHandlers(tasker.PrepareAccountTask, task.PrepareAccount(
|
||||
redisNoncestore,
|
||||
taskerClient,
|
||||
))
|
||||
taskerServer.RegisterHandlers(tasker.GiftGasTask, task.GiftGasProcessor(
|
||||
celoProvider,
|
||||
redisNoncestore,
|
||||
lockProvider,
|
||||
system,
|
||||
taskerClient,
|
||||
))
|
||||
taskerServer.RegisterHandlers(tasker.GiftTokenTask, task.GiftTokenProcessor(
|
||||
celoProvider,
|
||||
redisNoncestore,
|
||||
lockProvider,
|
||||
system,
|
||||
taskerClient,
|
||||
))
|
||||
taskerServer.RegisterHandlers(tasker.RefillGasTask, task.RefillGasProcessor(
|
||||
celoProvider,
|
||||
redisNoncestore,
|
||||
lockProvider,
|
||||
system,
|
||||
taskerClient,
|
||||
))
|
||||
taskerServer.RegisterHandlers(tasker.TransferTokenTask, task.TransferToken(
|
||||
celoProvider,
|
||||
redisNoncestore,
|
||||
postgresKeystore,
|
||||
lockProvider,
|
||||
system,
|
||||
taskerClient,
|
||||
))
|
||||
taskerServer.RegisterHandlers(tasker.TxDispatchTask, task.TxDispatch(
|
||||
celoProvider,
|
||||
))
|
||||
|
||||
lo.Debug("Registered all tasker handlers")
|
||||
return taskerServer
|
||||
}
|
122
cmd/main.go
122
cmd/main.go
@ -1,122 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strings"
|
||||
"sync"
|
||||
"syscall"
|
||||
|
||||
"github.com/bsm/redislock"
|
||||
celo "github.com/grassrootseconomics/cic-celo-sdk"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/keystore"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/nonce"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/tasker"
|
||||
"github.com/grassrootseconomics/cic-custodial/pkg/redis"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/knadh/koanf"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/zerodha/logf"
|
||||
)
|
||||
|
||||
var (
|
||||
confFlag string
|
||||
debugFlag bool
|
||||
taskerModeFlag bool
|
||||
apiModeFlag bool
|
||||
|
||||
asynqRedisPool *redis.RedisPool
|
||||
celoProvider *celo.Provider
|
||||
commonRedisPool *redis.RedisPool
|
||||
lo logf.Logger
|
||||
lockProvider *redislock.Client
|
||||
ko *koanf.Koanf
|
||||
postgresPool *pgxpool.Pool
|
||||
postgresKeystore keystore.Keystore
|
||||
redisNoncestore nonce.Noncestore
|
||||
system *tasker.SystemContainer
|
||||
taskerClient *tasker.TaskerClient
|
||||
)
|
||||
|
||||
func init() {
|
||||
flag.StringVar(&confFlag, "config", "config.toml", "Config file location")
|
||||
flag.BoolVar(&debugFlag, "log", false, "Enable debug logging")
|
||||
flag.BoolVar(&taskerModeFlag, "tasker", true, "Start tasker")
|
||||
flag.BoolVar(&apiModeFlag, "api", true, "Start API server")
|
||||
flag.Parse()
|
||||
|
||||
lo = initLogger(debugFlag)
|
||||
ko = initConfig(confFlag)
|
||||
|
||||
celoProvider = initCeloProvider()
|
||||
postgresPool = initPostgresPool()
|
||||
postgresKeystore = initKeystore()
|
||||
asynqRedisPool = initAsynqRedisPool()
|
||||
commonRedisPool = initCommonRedisPool()
|
||||
redisNoncestore = initRedisNoncestore()
|
||||
lockProvider = initLockProvider()
|
||||
taskerClient = initTaskerClient()
|
||||
system = initSystemContainer()
|
||||
}
|
||||
|
||||
func main() {
|
||||
var (
|
||||
tasker *tasker.TaskerServer
|
||||
apiServer *echo.Echo
|
||||
wg sync.WaitGroup
|
||||
)
|
||||
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
if apiModeFlag {
|
||||
apiServer = initApiServer()
|
||||
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
|
||||
lo.Info("Starting API server")
|
||||
if err := apiServer.Start(ko.MustString("service.address")); err != nil {
|
||||
if strings.Contains(err.Error(), "Server closed") {
|
||||
lo.Info("Shutting down server")
|
||||
} else {
|
||||
lo.Fatal("Could not start api server", "err", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
if taskerModeFlag {
|
||||
tasker = initTasker()
|
||||
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
|
||||
lo.Info("Starting tasker")
|
||||
if err := tasker.Start(); err != nil {
|
||||
lo.Fatal("Could not start task server", "err", err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
<-ctx.Done()
|
||||
lo.Info("Graceful shutdown triggered")
|
||||
|
||||
if taskerModeFlag {
|
||||
lo.Debug("Stopping tasker")
|
||||
tasker.Stop()
|
||||
}
|
||||
|
||||
if apiModeFlag {
|
||||
lo.Debug("Stopping api server")
|
||||
if err := apiServer.Shutdown(ctx); err != nil {
|
||||
lo.Error("Could not gracefully shutdown api server", "err", err)
|
||||
}
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
}
|
64
cmd/service/api.go
Normal file
64
cmd/service/api.go
Normal file
@ -0,0 +1,64 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/VictoriaMetrics/metrics"
|
||||
"github.com/go-playground/validator"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/api"
|
||||
"github.com/hibiken/asynq"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
// Bootstrap API server.
|
||||
func initApiServer(custodialContainer *custodial) *echo.Echo {
|
||||
lo.Debug("api: bootstrapping api server")
|
||||
server := echo.New()
|
||||
server.HideBanner = true
|
||||
server.HidePort = true
|
||||
|
||||
server.HTTPErrorHandler = func(err error, c echo.Context) {
|
||||
// Handle asynq duplication errors across all api handlers.
|
||||
if errors.Is(err, asynq.ErrTaskIDConflict) {
|
||||
c.JSON(http.StatusForbidden, api.ErrResp{
|
||||
Ok: false,
|
||||
Code: api.DUPLICATE_ERROR,
|
||||
Message: "Request with duplicate tracking id submitted.",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Log internal server error for further investigation.
|
||||
lo.Error("api:", "path", c.Path(), "err", err)
|
||||
|
||||
c.JSON(http.StatusInternalServerError, api.ErrResp{
|
||||
Ok: false,
|
||||
Code: api.INTERNAL_ERROR,
|
||||
Message: "Internal server error.",
|
||||
})
|
||||
}
|
||||
|
||||
if ko.Bool("service.metrics") {
|
||||
server.GET("/metrics", func(c echo.Context) error {
|
||||
metrics.WritePrometheus(c.Response(), true)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
server.Validator = &api.Validator{
|
||||
ValidatorProvider: validator.New(),
|
||||
}
|
||||
|
||||
apiRoute := server.Group("/api")
|
||||
apiRoute.POST("/account/create", api.CreateAccountHandler(
|
||||
custodialContainer.keystore,
|
||||
custodialContainer.taskerClient,
|
||||
))
|
||||
|
||||
apiRoute.POST("/sign/transfer", api.SignTransferHandler(
|
||||
custodialContainer.taskerClient,
|
||||
))
|
||||
|
||||
return server
|
||||
}
|
70
cmd/service/custodial.go
Normal file
70
cmd/service/custodial.go
Normal file
@ -0,0 +1,70 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"math/big"
|
||||
"time"
|
||||
|
||||
eth_crypto "github.com/celo-org/celo-blockchain/crypto"
|
||||
"github.com/go-redis/redis/v8"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/nonce"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/tasker"
|
||||
"github.com/grassrootseconomics/w3-celo-patch"
|
||||
)
|
||||
|
||||
// Define common smart contrcat ABI's that can be injected into the system container.
|
||||
// Any relevant function signature that will be used by the custodial system can be defined here.
|
||||
func initAbis() map[string]*w3.Func {
|
||||
return map[string]*w3.Func{
|
||||
// Keccak hash -> 0x449a52f8
|
||||
"mintTo": w3.MustNewFunc("mintTo(address, uint256)", "bool"),
|
||||
// Keccak hash -> 0xa9059cbb
|
||||
"transfer": w3.MustNewFunc("transfer(address,uint256)", "bool"),
|
||||
// Keccak hash -> 0x23b872dd
|
||||
"transferFrom": w3.MustNewFunc("transferFrom(address, address, uint256)", "bool"),
|
||||
// Add to account index
|
||||
"add": w3.MustNewFunc("add(address)", "bool"),
|
||||
// giveTo gas refill
|
||||
"giveTo": w3.MustNewFunc("giveTo(address)", "uint256"),
|
||||
}
|
||||
}
|
||||
|
||||
// Bootstrap the internal custodial system configs and system signer key.
|
||||
// This container is passed down to individual tasker and API handlers.
|
||||
func initSystemContainer(ctx context.Context, noncestore nonce.Noncestore) (*tasker.SystemContainer, error) {
|
||||
// Some custodial system defaults loaded from the config file.
|
||||
systemContainer := &tasker.SystemContainer{
|
||||
Abis: initAbis(),
|
||||
AccountIndexContract: w3.A(ko.MustString("system.account_index")),
|
||||
GasFaucetContract: w3.A(ko.MustString("system.gas_faucet")),
|
||||
GasRefillThreshold: big.NewInt(ko.MustInt64("system.gas_refill_threshold")),
|
||||
GasRefillValue: big.NewInt(ko.MustInt64("system.gas_refill_value")),
|
||||
GiftableGasValue: big.NewInt(ko.MustInt64("system.giftable_gas_value")),
|
||||
GiftableToken: w3.A(ko.MustString("system.giftable_token_address")),
|
||||
GiftableTokenValue: big.NewInt(ko.MustInt64("system.giftable_token_value")),
|
||||
LockPrefix: ko.MustString("system.lock_prefix"),
|
||||
LockTimeout: 1 * time.Second,
|
||||
PublicKey: ko.MustString("system.public_key"),
|
||||
TokenDecimals: ko.MustInt("system.token_decimals"),
|
||||
TokenTransferGasLimit: uint64(ko.MustInt64("system.token_transfer_gas_limit")),
|
||||
}
|
||||
// Check if system signer account nonce is present.
|
||||
// If not (first boot), we bootstrap it from the network.
|
||||
currentSystemNonce, err := noncestore.Peek(ctx, ko.MustString("system.public_key"))
|
||||
lo.Info("custodial: loaded (noncestore) system nonce", "nonce", currentSystemNonce)
|
||||
if err == redis.Nil {
|
||||
nonce, err := noncestore.SyncNetworkNonce(ctx, ko.MustString("system.public_key"))
|
||||
lo.Info("custodial: syncing system nonce", "nonce", nonce)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
loadedPrivateKey, err := eth_crypto.HexToECDSA(ko.MustString("system.private_key"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
systemContainer.PrivateKey = loadedPrivateKey
|
||||
|
||||
return systemContainer, nil
|
||||
}
|
210
cmd/service/init.go
Normal file
210
cmd/service/init.go
Normal file
@ -0,0 +1,210 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/bsm/redislock"
|
||||
celo "github.com/grassrootseconomics/cic-celo-sdk"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/keystore"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/nonce"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/queries"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/store"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/tasker"
|
||||
"github.com/grassrootseconomics/cic-custodial/pkg/logg"
|
||||
"github.com/grassrootseconomics/cic-custodial/pkg/postgres"
|
||||
"github.com/grassrootseconomics/cic-custodial/pkg/redis"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/knadh/goyesql/v2"
|
||||
"github.com/knadh/koanf"
|
||||
"github.com/knadh/koanf/parsers/toml"
|
||||
"github.com/knadh/koanf/providers/env"
|
||||
"github.com/knadh/koanf/providers/file"
|
||||
"github.com/nats-io/nats.go"
|
||||
"github.com/zerodha/logf"
|
||||
)
|
||||
|
||||
// Load config file.
|
||||
func initConfig(configFilePath string) *koanf.Koanf {
|
||||
var (
|
||||
ko = koanf.New(".")
|
||||
)
|
||||
|
||||
confFile := file.Provider(configFilePath)
|
||||
if err := ko.Load(confFile, toml.Parser()); err != nil {
|
||||
lo.Fatal("Could not load config file", "error", err)
|
||||
}
|
||||
|
||||
if err := ko.Load(env.Provider("", ".", func(s string) string {
|
||||
return strings.ReplaceAll(strings.ToLower(
|
||||
strings.TrimPrefix(s, "")), "_", ".")
|
||||
}), nil); err != nil {
|
||||
lo.Fatal("Could not override config from env vars", "error", err)
|
||||
}
|
||||
|
||||
return ko
|
||||
}
|
||||
|
||||
// Load logger.
|
||||
func initLogger(debug bool) logf.Logger {
|
||||
loggOpts := logg.LoggOpts{
|
||||
Color: true,
|
||||
}
|
||||
|
||||
if debug {
|
||||
loggOpts.Caller = true
|
||||
loggOpts.Debug = true
|
||||
}
|
||||
|
||||
return logg.NewLogg(loggOpts)
|
||||
}
|
||||
|
||||
// Load Celo chain provider.
|
||||
func initCeloProvider() (*celo.Provider, error) {
|
||||
providerOpts := celo.ProviderOpts{
|
||||
RpcEndpoint: ko.MustString("chain.rpc_endpoint"),
|
||||
}
|
||||
|
||||
if ko.Bool("chain.testnet") {
|
||||
// Devnet = 1337
|
||||
providerOpts.ChainId = 1337
|
||||
} else {
|
||||
providerOpts.ChainId = celo.MainnetChainId
|
||||
}
|
||||
|
||||
provider, err := celo.NewProvider(providerOpts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return provider, nil
|
||||
}
|
||||
|
||||
// Load postgres pool.
|
||||
func initPostgresPool() (*pgxpool.Pool, error) {
|
||||
poolOpts := postgres.PostgresPoolOpts{
|
||||
DSN: ko.MustString("postgres.dsn"),
|
||||
}
|
||||
|
||||
pool, err := postgres.NewPostgresPool(poolOpts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return pool, nil
|
||||
}
|
||||
|
||||
// Load separate redis connection for the tasker on a reserved db namespace.
|
||||
func initAsynqRedisPool() (*redis.RedisPool, error) {
|
||||
poolOpts := redis.RedisPoolOpts{
|
||||
DSN: ko.MustString("asynq.dsn"),
|
||||
MinIdleConns: ko.MustInt("redis.min_idle_conn"),
|
||||
}
|
||||
|
||||
pool, err := redis.NewRedisPool(poolOpts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return pool, nil
|
||||
}
|
||||
|
||||
// Common redis connection on a different db namespace from the takser.
|
||||
func initCommonRedisPool() (*redis.RedisPool, error) {
|
||||
poolOpts := redis.RedisPoolOpts{
|
||||
DSN: ko.MustString("redis.dsn"),
|
||||
MinIdleConns: ko.MustInt("redis.min_idle_conn"),
|
||||
}
|
||||
|
||||
pool, err := redis.NewRedisPool(poolOpts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return pool, nil
|
||||
}
|
||||
|
||||
// Load SQL statements into struct.
|
||||
func initQueries(queriesPath string) (*queries.Queries, error) {
|
||||
parsedQueries, err := goyesql.ParseFile(queriesFlag)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
loadedQueries, err := queries.LoadQueries(parsedQueries)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return loadedQueries, nil
|
||||
}
|
||||
|
||||
// Load postgres based keystore.
|
||||
func initPostgresKeystore(postgresPool *pgxpool.Pool, queries *queries.Queries) (keystore.Keystore, error) {
|
||||
keystore := keystore.NewPostgresKeytore(keystore.Opts{
|
||||
PostgresPool: postgresPool,
|
||||
Queries: queries,
|
||||
})
|
||||
|
||||
return keystore, nil
|
||||
}
|
||||
|
||||
// Load redis backed noncestore.
|
||||
func initRedisNoncestore(redisPool *redis.RedisPool, celoProvider *celo.Provider) nonce.Noncestore {
|
||||
return nonce.NewRedisNoncestore(nonce.Opts{
|
||||
RedisPool: redisPool,
|
||||
CeloProvider: celoProvider,
|
||||
})
|
||||
}
|
||||
|
||||
// Load global lock provider.
|
||||
func initLockProvider(redisPool redislock.RedisClient) *redislock.Client {
|
||||
return redislock.New(redisPool)
|
||||
}
|
||||
|
||||
// Load tasker client.
|
||||
func initTaskerClient(redisPool *redis.RedisPool) *tasker.TaskerClient {
|
||||
return tasker.NewTaskerClient(tasker.TaskerClientOpts{
|
||||
RedisPool: redisPool,
|
||||
TaskRetention: time.Duration(ko.MustInt64("asynq.task_retention_hrs")) * time.Hour,
|
||||
})
|
||||
}
|
||||
|
||||
// Load Postgres store
|
||||
func initPostgresStore(postgresPool *pgxpool.Pool, queries *queries.Queries) store.Store {
|
||||
return store.NewPostgresStore(store.Opts{
|
||||
PostgresPool: postgresPool,
|
||||
Queries: queries,
|
||||
})
|
||||
}
|
||||
|
||||
// Init JetStream context for tasker events.
|
||||
func initJetStream() (nats.JetStreamContext, error) {
|
||||
natsConn, err := nats.Connect(ko.MustString("jetstream.endpoint"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
js, err := natsConn.JetStream()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Bootstrap stream if it does not exist
|
||||
stream, _ := js.StreamInfo(ko.MustString("jetstream.stream_name"))
|
||||
if stream == nil {
|
||||
lo.Info("jetstream: bootstrapping stream")
|
||||
_, err = js.AddStream(&nats.StreamConfig{
|
||||
Name: ko.MustString("jetstream.stream_name"),
|
||||
MaxAge: time.Duration(ko.MustInt("jetstream.persist_duration_hours")) * time.Hour,
|
||||
Storage: nats.FileStorage,
|
||||
Subjects: ko.MustStrings("jetstream.stream_subjects"),
|
||||
Duplicates: time.Duration(ko.MustInt("jetstream.dedup_duration_hours")) * time.Hour,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return js, nil
|
||||
}
|
147
cmd/service/main.go
Normal file
147
cmd/service/main.go
Normal file
@ -0,0 +1,147 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"flag"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strings"
|
||||
"sync"
|
||||
"syscall"
|
||||
|
||||
"github.com/bsm/redislock"
|
||||
celo "github.com/grassrootseconomics/cic-celo-sdk"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/keystore"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/nonce"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/store"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/tasker"
|
||||
"github.com/knadh/koanf"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/zerodha/logf"
|
||||
)
|
||||
|
||||
var (
|
||||
confFlag string
|
||||
debugFlag bool
|
||||
queriesFlag string
|
||||
|
||||
lo logf.Logger
|
||||
ko *koanf.Koanf
|
||||
)
|
||||
|
||||
type custodial struct {
|
||||
celoProvider *celo.Provider
|
||||
keystore keystore.Keystore
|
||||
lockProvider *redislock.Client
|
||||
noncestore nonce.Noncestore
|
||||
pgStore store.Store
|
||||
systemContainer *tasker.SystemContainer
|
||||
taskerClient *tasker.TaskerClient
|
||||
}
|
||||
|
||||
func init() {
|
||||
flag.StringVar(&confFlag, "config", "config.toml", "Config file location")
|
||||
flag.BoolVar(&debugFlag, "log", false, "Enable debug logging")
|
||||
flag.StringVar(&queriesFlag, "queries", "queries.sql", "Queries file location")
|
||||
flag.Parse()
|
||||
|
||||
lo = initLogger(debugFlag)
|
||||
ko = initConfig(confFlag)
|
||||
}
|
||||
|
||||
func main() {
|
||||
var (
|
||||
tasker *tasker.TaskerServer
|
||||
apiServer *echo.Echo
|
||||
wg sync.WaitGroup
|
||||
)
|
||||
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
celoProvider, err := initCeloProvider()
|
||||
if err != nil {
|
||||
lo.Fatal("main: critical error loading chain provider", "error", err)
|
||||
}
|
||||
|
||||
queries, err := initQueries(queriesFlag)
|
||||
if err != nil {
|
||||
lo.Fatal("main: critical error loading SQL queries", "error", err)
|
||||
}
|
||||
|
||||
postgresPool, err := initPostgresPool()
|
||||
if err != nil {
|
||||
lo.Fatal("main: critical error connecting to postgres", "error", err)
|
||||
}
|
||||
|
||||
asynqRedisPool, err := initAsynqRedisPool()
|
||||
if err != nil {
|
||||
lo.Fatal("main: critical error connecting to asynq redis db", "error", err)
|
||||
}
|
||||
|
||||
redisPool, err := initCommonRedisPool()
|
||||
if err != nil {
|
||||
lo.Fatal("main: critical error connecting to common redis db", "error", err)
|
||||
}
|
||||
|
||||
postgresKeystore, err := initPostgresKeystore(postgresPool, queries)
|
||||
if err != nil {
|
||||
lo.Fatal("main: critical error loading keystore")
|
||||
}
|
||||
|
||||
pgStore := initPostgresStore(postgresPool, queries)
|
||||
redisNoncestore := initRedisNoncestore(redisPool, celoProvider)
|
||||
lockProvider := initLockProvider(redisPool.Client)
|
||||
taskerClient := initTaskerClient(asynqRedisPool)
|
||||
|
||||
systemContainer, err := initSystemContainer(context.Background(), redisNoncestore)
|
||||
if err != nil {
|
||||
lo.Fatal("main: critical error bootstrapping system container", "error", err)
|
||||
}
|
||||
|
||||
custodial := &custodial{
|
||||
celoProvider: celoProvider,
|
||||
keystore: postgresKeystore,
|
||||
lockProvider: lockProvider,
|
||||
noncestore: redisNoncestore,
|
||||
pgStore: pgStore,
|
||||
systemContainer: systemContainer,
|
||||
taskerClient: taskerClient,
|
||||
}
|
||||
|
||||
apiServer = initApiServer(custodial)
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
lo.Info("main: starting API server")
|
||||
if err := apiServer.Start(ko.MustString("service.address")); err != nil {
|
||||
if strings.Contains(err.Error(), "Server closed") {
|
||||
lo.Info("main: shutting down server")
|
||||
} else {
|
||||
lo.Fatal("main: critical error shutting down server", "err", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
tasker = initTasker(custodial, asynqRedisPool)
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
lo.Info("Starting tasker")
|
||||
if err := tasker.Start(); err != nil {
|
||||
lo.Fatal("main: could not start task server", "err", err)
|
||||
}
|
||||
}()
|
||||
|
||||
<-ctx.Done()
|
||||
|
||||
lo.Info("main: stopping tasker")
|
||||
tasker.Stop()
|
||||
|
||||
lo.Info("main: stopping api server")
|
||||
if err := apiServer.Shutdown(ctx); err != nil {
|
||||
lo.Error("Could not gracefully shutdown api server", "err", err)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
}
|
82
cmd/service/tasker.go
Normal file
82
cmd/service/tasker.go
Normal file
@ -0,0 +1,82 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/tasker"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/tasker/task"
|
||||
"github.com/grassrootseconomics/cic-custodial/pkg/redis"
|
||||
"github.com/hibiken/asynq"
|
||||
)
|
||||
|
||||
// Load tasker handlers, injecting any necessary handler dependencies from the system container.
|
||||
func initTasker(custodialContainer *custodial, redisPool *redis.RedisPool) *tasker.TaskerServer {
|
||||
lo.Debug("Bootstrapping tasker")
|
||||
js, err := initJetStream()
|
||||
if err != nil {
|
||||
lo.Fatal("filters: critical error loading jetstream", "error", err)
|
||||
}
|
||||
|
||||
taskerServerOpts := tasker.TaskerServerOpts{
|
||||
Concurrency: ko.MustInt("asynq.worker_count"),
|
||||
Logg: lo,
|
||||
LogLevel: asynq.ErrorLevel,
|
||||
RedisPool: redisPool,
|
||||
SystemContainer: custodialContainer.systemContainer,
|
||||
TaskerClient: custodialContainer.taskerClient,
|
||||
}
|
||||
|
||||
if debugFlag {
|
||||
taskerServerOpts.LogLevel = asynq.DebugLevel
|
||||
}
|
||||
|
||||
taskerServer := tasker.NewTaskerServer(taskerServerOpts)
|
||||
|
||||
taskerServer.RegisterHandlers(tasker.PrepareAccountTask, task.PrepareAccount(
|
||||
custodialContainer.noncestore,
|
||||
custodialContainer.taskerClient,
|
||||
js,
|
||||
))
|
||||
taskerServer.RegisterHandlers(tasker.RegisterAccountOnChain, task.RegisterAccountOnChainProcessor(
|
||||
custodialContainer.celoProvider,
|
||||
custodialContainer.lockProvider,
|
||||
custodialContainer.noncestore,
|
||||
custodialContainer.pgStore,
|
||||
custodialContainer.systemContainer,
|
||||
custodialContainer.taskerClient,
|
||||
js,
|
||||
))
|
||||
taskerServer.RegisterHandlers(tasker.GiftGasTask, task.GiftGasProcessor(
|
||||
custodialContainer.celoProvider,
|
||||
custodialContainer.lockProvider,
|
||||
custodialContainer.noncestore,
|
||||
custodialContainer.pgStore,
|
||||
custodialContainer.systemContainer,
|
||||
custodialContainer.taskerClient,
|
||||
js,
|
||||
))
|
||||
taskerServer.RegisterHandlers(tasker.GiftTokenTask, task.GiftTokenProcessor(
|
||||
custodialContainer.celoProvider,
|
||||
custodialContainer.lockProvider,
|
||||
custodialContainer.noncestore,
|
||||
custodialContainer.pgStore,
|
||||
custodialContainer.systemContainer,
|
||||
custodialContainer.taskerClient,
|
||||
js,
|
||||
))
|
||||
taskerServer.RegisterHandlers(tasker.SignTransferTask, task.SignTransfer(
|
||||
custodialContainer.celoProvider,
|
||||
custodialContainer.keystore,
|
||||
custodialContainer.lockProvider,
|
||||
custodialContainer.noncestore,
|
||||
custodialContainer.pgStore,
|
||||
custodialContainer.systemContainer,
|
||||
custodialContainer.taskerClient,
|
||||
js,
|
||||
))
|
||||
taskerServer.RegisterHandlers(tasker.TxDispatchTask, task.TxDispatch(
|
||||
custodialContainer.celoProvider,
|
||||
custodialContainer.pgStore,
|
||||
js,
|
||||
))
|
||||
|
||||
return taskerServer
|
||||
}
|
73
config.toml
73
config.toml
@ -1,33 +1,64 @@
|
||||
[service]
|
||||
address = ":5000"
|
||||
statsviz_debug = true
|
||||
address = ":5005"
|
||||
# Exposes Prometheus metrics
|
||||
# /metrics endpoint
|
||||
metrics = true
|
||||
|
||||
# System default values
|
||||
# Valus are in wei unless otherwise stated
|
||||
[system]
|
||||
gas_refill_threshold = 100000000000000
|
||||
gas_refill_value = 100000000000000
|
||||
giftable_gas_value = 200000000000000
|
||||
giftable_token_address = "0x486aD10d70107900546455F7a0e022c300F157Bf"
|
||||
giftable_token_value = 5000000000000000000
|
||||
private_key = "a6af6c597c614e3c8ee4b7638ab7c3f737aece3773a5413ca8caf4338e6b06d1"
|
||||
lock_prefix = "lock:"
|
||||
public_key = "0x80097c773B3E83472FC7952c5206a7DB35d42bEF"
|
||||
token_decimals = 18
|
||||
token_transfer_gas_limit = 100000
|
||||
# The giftable token is a training voucher
|
||||
# Every new user is given 5 DGFT
|
||||
gas_faucet = "0xA8b3Ffc715e85792FB361BDee9357B38D5A4a6cF"
|
||||
giftable_token_address = "0xdD4F5ea484F6b16f031eF7B98F3810365493BC20"
|
||||
giftable_token_value = 5000000
|
||||
gas_refill_threshold = 100000000000000000
|
||||
gas_refill_value = 100000000000000000
|
||||
# Every custodial account is given 2 KES worth of CELO
|
||||
giftable_gas_value = 2000000000000000000
|
||||
# System private key
|
||||
# Should always be toped up
|
||||
private_key = "bfa7222a7bea3bde312434abe819b14cf3bc8703ceaabb98a9e3a97ceb0b79fd"
|
||||
lock_prefix = "lock:"
|
||||
public_key = "0x08eb3a90128D5874da54cf654fCfA88cEd1bb047"
|
||||
token_decimals = 6
|
||||
token_transfer_gas_limit = 200000
|
||||
account_index = "0xdb2550ac5E52A54B6189FFAf17ECfF33AE190db9"
|
||||
|
||||
[chain]
|
||||
rpc_endpoint = "https://alfajores-forno.celo-testnet.org"
|
||||
testnet = true
|
||||
rpc_endpoint = "http://192.168.0.101:8545"
|
||||
testnet = true
|
||||
|
||||
[postgres]
|
||||
debug = false
|
||||
dsn = "postgres://postgres:postgres@localhost:5432/cic_custodial"
|
||||
dsn = "postgres://postgres:postgres@localhost:5432/cic_custodial"
|
||||
|
||||
[redis]
|
||||
debug = false
|
||||
dsn = "redis://localhost:6379/1"
|
||||
minconn = 5
|
||||
debug = false
|
||||
dsn = "redis://localhost:6379/1"
|
||||
min_idle_conn = 5
|
||||
|
||||
[asynq]
|
||||
concurrency = 25
|
||||
debug = false
|
||||
dsn = "redis://redis:6379/0"
|
||||
worker_count = 15
|
||||
debug = false
|
||||
dsn = "redis://localhost:6379/0"
|
||||
task_retention_hrs = 24
|
||||
|
||||
# https://docs.nats.io/
|
||||
[jetstream]
|
||||
endpoint = "nats://localhost:4222"
|
||||
stream_name = "CUSTODIAL"
|
||||
# Duration JetStream should keep the message before remocing it from the persistent store
|
||||
persist_duration_hours = 48
|
||||
# Duration to ignore duplicate transactions (e.g. due to restart)
|
||||
dedup_duration_hours = 6
|
||||
# Stream subjects
|
||||
stream_subjects = [
|
||||
"CUSTODIAL.accountNewNonce",
|
||||
"CUSTODIAL.accountRegister",
|
||||
"CUSTODIAL.giftNewAccountGas",
|
||||
"CUSTODIAL.giftNewAccountVoucher",
|
||||
"CUSTODIAL.dispatchFail",
|
||||
"CUSTODIAL.dispatchSuccess",
|
||||
"CUSTODIAL.transferSign"
|
||||
]
|
@ -1,4 +1,4 @@
|
||||
version: '3.9'
|
||||
version: "3.9"
|
||||
services:
|
||||
redis:
|
||||
image: redis:6-alpine
|
||||
@ -7,12 +7,12 @@ services:
|
||||
volumes:
|
||||
- cic-custodial-redis:/data
|
||||
ports:
|
||||
- '6379:6379'
|
||||
- "127.0.0.1:6379:6379"
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "redis-cli ping | grep PONG"]
|
||||
interval: 10s
|
||||
timeout: 5s
|
||||
retries: 5
|
||||
retries: 5
|
||||
postgres:
|
||||
image: postgres:14-alpine
|
||||
restart: unless-stopped
|
||||
@ -24,36 +24,35 @@ services:
|
||||
volumes:
|
||||
- cic-custodial-pg:/var/lib/postgresql/data
|
||||
ports:
|
||||
- '5432:5432'
|
||||
- "127.0.0.1:5432:5432"
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "pg_isready"]
|
||||
interval: 10s
|
||||
timeout: 5s
|
||||
retries: 5
|
||||
nats:
|
||||
image: nats:2.9
|
||||
restart: unless-stopped
|
||||
command: "-js -sd /nats/data"
|
||||
volumes:
|
||||
- cic-custodial-nats:/nats/data
|
||||
ports:
|
||||
- "4222:4222"
|
||||
- "8222:8222"
|
||||
asynqmon:
|
||||
image: hibiken/asynqmon
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
- REDIS_ADDR=redis:6379
|
||||
ports:
|
||||
- '8080:8080'
|
||||
- "127.0.0.1:8080:8080"
|
||||
depends_on:
|
||||
redis:
|
||||
condition: service_healthy
|
||||
cic-custodial:
|
||||
image: ghcr.io/grassrootseconomics/cic-custodial/cic-custodial:latest
|
||||
restart: unless-stopped
|
||||
depends_on:
|
||||
postgres:
|
||||
redis:
|
||||
condition: service_healthy
|
||||
redis:
|
||||
condition: service_healthy
|
||||
env_file:
|
||||
- .env
|
||||
ports:
|
||||
- '5000:5000'
|
||||
volumes:
|
||||
cic-custodial-pg:
|
||||
driver: local
|
||||
cic-custodial-redis:
|
||||
driver: local
|
||||
driver: local
|
||||
cic-custodial-nats:
|
||||
driver: local
|
@ -1,29 +0,0 @@
|
||||
version: '3.9'
|
||||
services:
|
||||
redis:
|
||||
image: redis:6-alpine
|
||||
restart: unless-stopped
|
||||
command: redis-server --save 60 1 --loglevel warning
|
||||
ports:
|
||||
- '6379:6379'
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "redis-cli ping | grep PONG"]
|
||||
interval: 10s
|
||||
timeout: 5s
|
||||
retries: 5
|
||||
postgres:
|
||||
image: postgres:14-alpine
|
||||
restart: unless-stopped
|
||||
user: postgres
|
||||
environment:
|
||||
- POSTGRES_PASSWORD=postgres
|
||||
- POSTGRES_USER=postgres
|
||||
- POSTGRES_DB=cic_custodial
|
||||
ports:
|
||||
- '5432:5432'
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "pg_isready"]
|
||||
interval: 10s
|
||||
timeout: 5s
|
||||
retries: 5
|
||||
|
27
go.mod
27
go.mod
@ -3,19 +3,19 @@ module github.com/grassrootseconomics/cic-custodial
|
||||
go 1.19
|
||||
|
||||
require (
|
||||
github.com/arl/statsviz v0.5.1
|
||||
github.com/VictoriaMetrics/metrics v1.23.1
|
||||
github.com/bsm/redislock v0.7.2
|
||||
github.com/celo-org/celo-blockchain v1.6.1
|
||||
github.com/celo-org/celo-blockchain v1.7.2
|
||||
github.com/go-playground/validator v9.31.0+incompatible
|
||||
github.com/go-redis/redis/v8 v8.11.5
|
||||
github.com/google/uuid v1.3.0
|
||||
github.com/grassrootseconomics/cic-celo-sdk v0.3.1
|
||||
github.com/grassrootseconomics/w3-celo-patch v0.1.0
|
||||
github.com/grassrootseconomics/cic-celo-sdk v0.4.0
|
||||
github.com/grassrootseconomics/w3-celo-patch v0.2.0
|
||||
github.com/hibiken/asynq v0.24.0
|
||||
github.com/jackc/pgx/v5 v5.2.0
|
||||
github.com/knadh/goyesql/v2 v2.2.0
|
||||
github.com/knadh/koanf v1.4.5
|
||||
github.com/labstack/echo/v4 v4.10.0
|
||||
github.com/stretchr/testify v1.8.1
|
||||
github.com/zerodha/logf v0.5.5
|
||||
)
|
||||
|
||||
@ -31,10 +31,10 @@ require (
|
||||
github.com/celo-org/celo-bls-go-other v0.6.3 // indirect
|
||||
github.com/celo-org/celo-bls-go-windows v0.6.3 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.1.2 // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/deckarep/golang-set v1.8.0 // indirect
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||
github.com/fsnotify/fsnotify v1.4.9 // indirect
|
||||
github.com/georgysavva/scany/v2 v2.0.0 // indirect
|
||||
github.com/go-ole/go-ole v1.2.6 // indirect
|
||||
github.com/go-playground/locales v0.14.0 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.0 // indirect
|
||||
@ -60,28 +60,33 @@ require (
|
||||
github.com/mitchellh/copystructure v1.2.0 // indirect
|
||||
github.com/mitchellh/mapstructure v1.5.0 // indirect
|
||||
github.com/mitchellh/reflectwalk v1.0.2 // indirect
|
||||
github.com/nats-io/nats.go v1.23.0 // indirect
|
||||
github.com/nats-io/nkeys v0.3.0 // indirect
|
||||
github.com/nats-io/nuid v1.0.1 // indirect
|
||||
github.com/olekukonko/tablewriter v0.0.5 // indirect
|
||||
github.com/onsi/gomega v1.24.1 // indirect
|
||||
github.com/pelletier/go-toml v1.7.0 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/prometheus/tsdb v0.10.0 // indirect
|
||||
github.com/rivo/uniseg v0.4.3 // indirect
|
||||
github.com/robfig/cron/v3 v3.0.1 // indirect
|
||||
github.com/rogpeppe/go-internal v1.9.0 // indirect
|
||||
github.com/shirou/gopsutil v3.21.11+incompatible // indirect
|
||||
github.com/spf13/cast v1.3.1 // indirect
|
||||
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // indirect
|
||||
github.com/tklauser/go-sysconf v0.3.11 // indirect
|
||||
github.com/tklauser/numcpus v0.6.0 // indirect
|
||||
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
||||
github.com/valyala/fastrand v1.1.0 // indirect
|
||||
github.com/valyala/fasttemplate v1.2.2 // indirect
|
||||
github.com/valyala/histogram v1.2.0 // indirect
|
||||
github.com/yusufpapurcu/wmi v1.2.2 // indirect
|
||||
go.uber.org/atomic v1.10.0 // indirect
|
||||
golang.org/x/crypto v0.3.0 // indirect
|
||||
golang.org/x/net v0.4.0 // indirect
|
||||
golang.org/x/crypto v0.5.0 // indirect
|
||||
golang.org/x/net v0.5.0 // indirect
|
||||
golang.org/x/sync v0.1.0 // indirect
|
||||
golang.org/x/sys v0.3.0 // indirect
|
||||
golang.org/x/text v0.5.0 // indirect
|
||||
golang.org/x/sys v0.4.0 // indirect
|
||||
golang.org/x/text v0.6.0 // indirect
|
||||
golang.org/x/time v0.2.0 // indirect
|
||||
google.golang.org/protobuf v1.28.0 // indirect
|
||||
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
|
||||
|
54
go.sum
54
go.sum
@ -43,6 +43,8 @@ github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6/go.mod h1:3eOhrU
|
||||
github.com/VictoriaMetrics/fastcache v1.6.0/go.mod h1:0qHz5QP0GMX4pfmMA/zt5RgfNuXJrTP0zS7DqpHGGTw=
|
||||
github.com/VictoriaMetrics/fastcache v1.12.0 h1:vnVi/y9yKDcD9akmc4NqAoqgQhJrOwUF+j9LTgn4QDE=
|
||||
github.com/VictoriaMetrics/fastcache v1.12.0/go.mod h1:tjiYeEfYXCqacuvYw/7UoDIeJaNxq6132xHICNP77w8=
|
||||
github.com/VictoriaMetrics/metrics v1.23.1 h1:/j8DzeJBxSpL2qSIdqnRFLvQQhbJyJbbEi22yMm7oL0=
|
||||
github.com/VictoriaMetrics/metrics v1.23.1/go.mod h1:rAr/llLpEnAdTehiNlUxKgnjcOuROSzpw0GvjpEbvFc=
|
||||
github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII=
|
||||
github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw=
|
||||
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
|
||||
@ -55,8 +57,6 @@ github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156/go.mod h1:Cb/ax
|
||||
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8=
|
||||
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
|
||||
github.com/apache/arrow/go/arrow v0.0.0-20191024131854-af6fa24be0db/go.mod h1:VTxUBvSJ3s3eHAg65PNgrsn5BtqCRPdmyXh6rAfdxN0=
|
||||
github.com/arl/statsviz v0.5.1 h1:3HY0ZEB738JtguWsD1Tf1pFJZiCcWUmYRq/3OTYKaSI=
|
||||
github.com/arl/statsviz v0.5.1/go.mod h1:zDnjgRblGm1Dyd7J5YlbH7gM1/+HRC+SfkhZhQb5AnM=
|
||||
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
|
||||
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
|
||||
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
|
||||
@ -102,19 +102,28 @@ github.com/buraksezer/consistent v0.0.0-20191006190839-693edf70fd72/go.mod h1:OE
|
||||
github.com/c-bata/go-prompt v0.2.2/go.mod h1:VzqtzE2ksDBcdln8G7mk2RX9QyGjH+OVqOCSiVIqS34=
|
||||
github.com/celo-org/celo-blockchain v1.6.1 h1:Kv+OXLXwdORDz3aIrGY1oCuw0aYSlK4KH7o8cGxO8FU=
|
||||
github.com/celo-org/celo-blockchain v1.6.1/go.mod h1:NdcP5idffWajmOP79Q0PCRfaFNTmTdSKBMbngLDxjpQ=
|
||||
github.com/celo-org/celo-blockchain v1.7.2 h1:LjQ+t89inzSK2K9i/twwwW07kIE1ubEVDlrcet+UvLU=
|
||||
github.com/celo-org/celo-blockchain v1.7.2/go.mod h1:x5HsfXAfUjxKQfTWzroQSb2HljaQEVgf2/mQVDPIMIY=
|
||||
github.com/celo-org/celo-bls-go v0.2.4/go.mod h1:eXUCLXu5F1yfd3M+3VaUk5ZUXaA0sLK2rWdLC1Cfaqo=
|
||||
github.com/celo-org/celo-bls-go v0.3.3/go.mod h1:eoMAORYWgZ5HOo3Z0bIAv/nbPtj/eArIO0nh7XFx7rk=
|
||||
github.com/celo-org/celo-bls-go v0.6.4 h1:zBf6pEr9k64gaO0VSYnDfLCBmrxDigc4yUaDvWub5G8=
|
||||
github.com/celo-org/celo-bls-go v0.6.4/go.mod h1:apSlSDPoXIdGseCS4Z2AMrhO5B1xpIVTXYpfv/uRd04=
|
||||
github.com/celo-org/celo-bls-go-android v0.3.2/go.mod h1:cFgtFRH8+6x5b+EyG5SqniXY3aKd03NBSGDgITscX34=
|
||||
github.com/celo-org/celo-bls-go-android v0.6.3 h1:mCtzKM99OCfsSu/i9A4+BaUJhEfYSEWPR0wHUu4YlO0=
|
||||
github.com/celo-org/celo-bls-go-android v0.6.3/go.mod h1:N8rOFwyCS3Ff9p45/rLfdLWfoyFwOvjVQ6F+VKbmIbU=
|
||||
github.com/celo-org/celo-bls-go-ios v0.3.2/go.mod h1:eaSoMpx29YV5oF7jXVChzJpNfxeZHnAa8G4PjL5CyW0=
|
||||
github.com/celo-org/celo-bls-go-ios v0.6.3 h1:8t08eq924VlxjTCKwzRWOlULRWlhqNmF+ok7pn4Unjg=
|
||||
github.com/celo-org/celo-bls-go-ios v0.6.3/go.mod h1:huG1qfXZVMDkFmEZo4UYOAu46EEzYrR8brE4flPul5c=
|
||||
github.com/celo-org/celo-bls-go-linux v0.3.2/go.mod h1:DVpJadg22OrxBtMb0ub6iNVdqDBL/r6EDdWVAA0bHa0=
|
||||
github.com/celo-org/celo-bls-go-linux v0.6.3 h1:9Evpz1ix4v7tswiJ89+ED3m8sYQ6PG9T0ZogHrvz0xE=
|
||||
github.com/celo-org/celo-bls-go-linux v0.6.3/go.mod h1:E6fEQ+whLgMqEToE1e3FWf/9+ikG+wNE5ik7z5Dxgbc=
|
||||
github.com/celo-org/celo-bls-go-macos v0.3.2/go.mod h1:mYPuRqGMVxj6yZUeL6Q6ggtP52HPBS1jz+FvBPXQ7QA=
|
||||
github.com/celo-org/celo-bls-go-macos v0.6.3 h1:+sEiFYoqdlDVO50LmONBSzu/PxCP0aP8fiGjWuAvBzg=
|
||||
github.com/celo-org/celo-bls-go-macos v0.6.3/go.mod h1:RC05VQ2cvF8vMc0tpLc7pfp8oSKrrGcI+o5LLXGequk=
|
||||
github.com/celo-org/celo-bls-go-other v0.3.2/go.mod h1:tNxZNfekzyT7TdYQbyNPhkfpcYtA3KCU/IKX5FNxM/U=
|
||||
github.com/celo-org/celo-bls-go-other v0.6.3 h1:DTVLfg/ZNizB5Wnbxq0mYh3VXsDtD1lZFvd0Ye4xv7o=
|
||||
github.com/celo-org/celo-bls-go-other v0.6.3/go.mod h1:1xEWTbCXUrDx3Z6R9YlLi1zFNNn1oMMnEcpLbcW+q74=
|
||||
github.com/celo-org/celo-bls-go-windows v0.3.2/go.mod h1:82GC5iJA9Qw5gynhYqR8ht3J+l/MO8eSNzgSTMI8UdA=
|
||||
github.com/celo-org/celo-bls-go-windows v0.6.3 h1:yrHZ+1EjONf++u5QEUseebImiD23Rv2GEcoYfPxYhuw=
|
||||
github.com/celo-org/celo-bls-go-windows v0.6.3/go.mod h1:8bRJbLcHREIAWn+5iZM3u8rGPBsWepp9BdF3SexisuM=
|
||||
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
|
||||
@ -169,6 +178,8 @@ github.com/fogleman/gg v1.2.1-0.20190220221249-0403632d5b90/go.mod h1:R/bRT+9gY/
|
||||
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
|
||||
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
|
||||
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
|
||||
github.com/georgysavva/scany/v2 v2.0.0 h1:RGXqxDv4row7/FYoK8MRXAZXqoWF/NM+NP0q50k3DKU=
|
||||
github.com/georgysavva/scany/v2 v2.0.0/go.mod h1:sigOdh+0qb/+aOs3TVhehVT10p8qJL7K/Zhyz8vWo38=
|
||||
github.com/getkin/kin-openapi v0.53.0/go.mod h1:7Yn5whZr5kJi6t+kShccXS8ae1APpYTW6yheSwk8Yi4=
|
||||
github.com/getkin/kin-openapi v0.61.0/go.mod h1:7Yn5whZr5kJi6t+kShccXS8ae1APpYTW6yheSwk8Yi4=
|
||||
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
|
||||
@ -203,6 +214,7 @@ github.com/go-redis/redis/v8 v8.11.4/go.mod h1:2Z2wHZXdQpCDXEGzqMockDpNyYvi2l4Px
|
||||
github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI=
|
||||
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
|
||||
github.com/go-sourcemap/sourcemap v2.1.2+incompatible/go.mod h1:F8jJfvm2KbVjc5NqelyYJmf/v5J0dwNLS2mL4sNA1Jg=
|
||||
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
|
||||
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
|
||||
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
|
||||
github.com/go-stack/stack v1.8.1 h1:ntEHSVwIt7PNXNpgPmVfMrNhLtgjlmnZha2kOpuRiDw=
|
||||
@ -278,12 +290,14 @@ github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/ad
|
||||
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
|
||||
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/graph-gophers/graphql-go v1.3.0/go.mod h1:9CQHMSxwO4MprSdzoIEobiHpoLtHm77vfxsvsIN5Vuc=
|
||||
github.com/grassrootseconomics/cic-celo-sdk v0.3.0 h1:uqYlad/sL4nWzExSE3ecfGQNdY0Gs6pqpkez1vX5XnI=
|
||||
github.com/grassrootseconomics/cic-celo-sdk v0.3.0/go.mod h1:EiR6d03GYu6jlVKNL1MbTAw/bqAW2WP3J/lkrZxPMdU=
|
||||
github.com/grassrootseconomics/cic-celo-sdk v0.3.1 h1:SzmMFrqxSIdgePqwbUdoS3PNP82MFnlOecycVk2ZYWg=
|
||||
github.com/grassrootseconomics/cic-celo-sdk v0.3.1/go.mod h1:EiR6d03GYu6jlVKNL1MbTAw/bqAW2WP3J/lkrZxPMdU=
|
||||
github.com/grassrootseconomics/cic-celo-sdk v0.4.0 h1:wh7aOQ/oK+q1nBl2koKe45WVbsWM0riPhhtEz6JLub4=
|
||||
github.com/grassrootseconomics/cic-celo-sdk v0.4.0/go.mod h1:G8uRw+rEw6yVP/+vBZ2V0UWXfs6iioit+eqVHrB9sBk=
|
||||
github.com/grassrootseconomics/w3-celo-patch v0.1.0 h1:0fev2hYkGEyFX2D4oUG8yy4jXhtHv7qUtLLboXL5ycw=
|
||||
github.com/grassrootseconomics/w3-celo-patch v0.1.0/go.mod h1:JtkXc+yDUiQQJdhYTqddZI/itdYGHY7H8PNZzBo4hCk=
|
||||
github.com/grassrootseconomics/w3-celo-patch v0.2.0 h1:YqibbPzX0tQKmxU1nUGzThPKk/fiYeYZY6Aif3eyu8U=
|
||||
github.com/grassrootseconomics/w3-celo-patch v0.2.0/go.mod h1:WhBXNzNIvHmS6B2hAeShs56oa9Azb4jQSrOMKuMdBWw=
|
||||
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
|
||||
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
|
||||
github.com/hashicorp/consul/api v1.13.0/go.mod h1:ZlVrynguJKcYr54zGaDbaL3fOvKC9m72FhPvA8T35KQ=
|
||||
@ -368,6 +382,7 @@ github.com/jedisct1/go-minisign v0.0.0-20190909160543-45766022959e/go.mod h1:G1C
|
||||
github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
|
||||
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
|
||||
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
|
||||
github.com/jmoiron/sqlx v1.2.0/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks=
|
||||
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
|
||||
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
|
||||
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
|
||||
@ -392,6 +407,8 @@ github.com/klauspost/compress v1.4.0/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0
|
||||
github.com/klauspost/cpuid v0.0.0-20170728055534-ae7887de9fa5/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
|
||||
github.com/klauspost/crc32 v0.0.0-20161016154125-cb6bfca970f6/go.mod h1:+ZoRqAPRLkC4NPOvfYeR5KNOrY6TD+/sAC3HXPZgDYg=
|
||||
github.com/klauspost/pgzip v1.0.2-0.20170402124221-0bf5dcad4ada/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs=
|
||||
github.com/knadh/goyesql/v2 v2.2.0 h1:DNQIzgITmMTXA+z+jDzbXCpgr7fGD6Hp0AJ7ZLEAem4=
|
||||
github.com/knadh/goyesql/v2 v2.2.0/go.mod h1:is+wK/XQBukYK3DdKfpJRyDH9U/ZTMyX2u6DFijjRnI=
|
||||
github.com/knadh/koanf v1.4.5 h1:yKWFswTrqFc0u7jBAoERUz30+N1b1yPXU01gAPr8IrY=
|
||||
github.com/knadh/koanf v1.4.5/go.mod h1:Hgyjp4y8v44hpZtPzs7JZfRAW5AhN7KfZcwv1RYggDs=
|
||||
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
||||
@ -443,6 +460,7 @@ github.com/mattn/go-runewidth v0.0.3/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzp
|
||||
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
|
||||
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
|
||||
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
||||
github.com/mattn/go-sqlite3 v1.9.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
|
||||
github.com/mattn/go-sqlite3 v1.11.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
|
||||
github.com/mattn/go-tty v0.0.0-20180907095812-13ff1204f104/go.mod h1:XPvLUNfbS4fJH25nqRHfWLMa1ONC8Amw+mIA639KxkE=
|
||||
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
|
||||
@ -475,6 +493,12 @@ github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRW
|
||||
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
|
||||
github.com/naoina/go-stringutil v0.1.0/go.mod h1:XJ2SJL9jCtBh+P9q5btrd/Ylo8XwT/h1USek5+NqSA0=
|
||||
github.com/naoina/toml v0.1.2-0.20170918210437-9fafd6967416/go.mod h1:NBIhNtsFMo3G2szEBne+bO4gS192HuIYRqfvOWb4i1E=
|
||||
github.com/nats-io/nats.go v1.23.0 h1:lR28r7IX44WjYgdiKz9GmUeW0uh/m33uD3yEjLZ2cOE=
|
||||
github.com/nats-io/nats.go v1.23.0/go.mod h1:ki/Scsa23edbh8IRZbCuNXR9TDcbvfaSijKtaqQgw+Q=
|
||||
github.com/nats-io/nkeys v0.3.0 h1:cgM5tL53EvYRU+2YLXIK0G2mJtK12Ft9oeooSZMA2G8=
|
||||
github.com/nats-io/nkeys v0.3.0/go.mod h1:gvUNGjVcM2IPr5rCsRsC6Wb3Hr2CQAm08dsxtV6A5y4=
|
||||
github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw=
|
||||
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
|
||||
github.com/npillmayer/nestext v0.1.3/go.mod h1:h2lrijH8jpicr25dFY+oAJLyzlya6jhnuG+zWp9L0Uk=
|
||||
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
|
||||
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
|
||||
@ -510,6 +534,7 @@ github.com/peterh/liner v1.0.1-0.20180619022028-8c1271fcf47f/go.mod h1:xIteQHvHu
|
||||
github.com/peterh/liner v1.1.1-0.20190123174540-a2c9a5303de7/go.mod h1:CRroGNssyjTd/qIG2FyxByd2S8JEAZXBl4qUrZf8GS0=
|
||||
github.com/philhofer/fwd v1.0.0/go.mod h1:gk3iGcWd9+svBvR0sR+KPcfE+RNWozjowpeBVG3ZVNU=
|
||||
github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
|
||||
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
|
||||
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
@ -550,6 +575,9 @@ github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzG
|
||||
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
|
||||
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
|
||||
github.com/rogpeppe/go-internal v1.8.1 h1:geMPLpDpQOgVyCg5z5GoRwLHepNdb71NXb67XFkP+Eg=
|
||||
github.com/rogpeppe/go-internal v1.8.1/go.mod h1:JeRgkft04UBgHMgCIwADu4Pn6Mtm5d4nPKWu0nJ5d+o=
|
||||
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
|
||||
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
|
||||
github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
|
||||
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
|
||||
@ -579,8 +607,6 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
|
||||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/testify v1.2.0/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
@ -588,10 +614,7 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P
|
||||
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
||||
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
|
||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 h1:epCh84lMvA70Z7CTTCmYQn2CKbY8j86K7/FAIr141uY=
|
||||
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7/go.mod h1:q4W45IWZaF22tdD+VEXcAWRA037jwmWEB5VWYORlTpc=
|
||||
github.com/tinylib/msgp v1.0.2/go.mod h1:+d+yLhGm8mzTaHzB+wgMYrodPfmZrzkirds8fDWklFE=
|
||||
@ -605,10 +628,14 @@ github.com/tyler-smith/go-bip39 v1.0.1-0.20181017060643-dbb3b84ba2ef/go.mod h1:s
|
||||
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
|
||||
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
|
||||
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
|
||||
github.com/valyala/fastrand v1.1.0 h1:f+5HkLW4rsgzdNoleUOB69hyT9IlD2ZQh9GyDMfb5G8=
|
||||
github.com/valyala/fastrand v1.1.0/go.mod h1:HWqCzkrkg6QXT8V2EXWvXCoow7vLwOFN002oeRzjapQ=
|
||||
github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
|
||||
github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
|
||||
github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
|
||||
github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
|
||||
github.com/valyala/histogram v1.2.0 h1:wyYGAZZt3CpwUiIb9AU/Zbllg1llXyrtApRS815OLoQ=
|
||||
github.com/valyala/histogram v1.2.0/go.mod h1:Hb4kBwb4UxsaNbbbh+RRz8ZR6pdodR57tzWUS3BUzXY=
|
||||
github.com/willf/bitset v1.1.3/go.mod h1:RjeCKbqT1RxIR/KWY6phxZiaY1IyutSBfGjNPySAYV4=
|
||||
github.com/xlab/treeprint v0.0.0-20180616005107-d6fb6747feb6/go.mod h1:ce1O1j6UtZfjr22oyGxGLbauSBp2YVXpARAosm7dHBg=
|
||||
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
@ -646,9 +673,12 @@ golang.org/x/crypto v0.0.0-20200115085410-6d4e4cb37c7d/go.mod h1:LzIPMQfyMNhhGPh
|
||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
|
||||
golang.org/x/crypto v0.0.0-20210314154223-e6e6c4f2bb5b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
|
||||
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
|
||||
golang.org/x/crypto v0.3.0 h1:a06MkbcxBrEFc0w0QIZWXrH/9cCX6KJyWbBOIwAn+7A=
|
||||
golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
|
||||
golang.org/x/crypto v0.5.0 h1:U/0M97KRkSFvyD/3FSmdP5W5swImpNgle/EHFhOsQPE=
|
||||
golang.org/x/crypto v0.5.0/go.mod h1:NK/OQwhpMQP3MwtdjgLlYHnH9ebylxKWv3e0fK+mkQU=
|
||||
golang.org/x/exp v0.0.0-20180321215751-8460e604b9de/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||
golang.org/x/exp v0.0.0-20180807140117-3d87b88a115f/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||
@ -711,6 +741,8 @@ golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT
|
||||
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/net v0.4.0 h1:Q5QPcMlvfxFTAPV0+07Xz/MpK9NTXu2VDUuy0FeMfaU=
|
||||
golang.org/x/net v0.4.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE=
|
||||
golang.org/x/net v0.5.0 h1:GyT4nK/YDHSqa1c4753ouYCDajOYKTja9Xb/OHtgvSw=
|
||||
golang.org/x/net v0.5.0/go.mod h1:DivGGAXEgPSlEBzxGzZI+ZLohi+xUj054jfeKui00ws=
|
||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||
@ -791,6 +823,8 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc
|
||||
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
|
||||
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18=
|
||||
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
@ -803,6 +837,8 @@ golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.5.0 h1:OLmvp0KP+FVG99Ct/qFiL/Fhk4zp4QQnZ7b2U+5piUM=
|
||||
golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||
golang.org/x/text v0.6.0 h1:3XmdazWV+ubf7QgHSTWeykHOci5oeekaGJBLkrkaw4k=
|
||||
golang.org/x/text v0.6.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||
golang.org/x/time v0.0.0-20201208040808-7e3f01d25324/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||
|
64
internal/api/account.go
Normal file
64
internal/api/account.go
Normal file
@ -0,0 +1,64 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/keystore"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/tasker"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/tasker/task"
|
||||
"github.com/grassrootseconomics/cic-custodial/pkg/keypair"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
// CreateAccountHandler route.
|
||||
// POST: /api/account/create
|
||||
// Returns the public key.
|
||||
func CreateAccountHandler(
|
||||
keystore keystore.Keystore,
|
||||
taskerClient *tasker.TaskerClient,
|
||||
) func(echo.Context) error {
|
||||
return func(c echo.Context) error {
|
||||
trackingId := uuid.NewString()
|
||||
|
||||
generatedKeyPair, err := keypair.Generate()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
id, err := keystore.WriteKeyPair(c.Request().Context(), generatedKeyPair)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
taskPayload, err := json.Marshal(task.AccountPayload{
|
||||
PublicKey: generatedKeyPair.Public,
|
||||
TrackingId: trackingId,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = taskerClient.CreateTask(
|
||||
tasker.PrepareAccountTask,
|
||||
tasker.DefaultPriority,
|
||||
&tasker.Task{
|
||||
Id: trackingId,
|
||||
Payload: taskPayload,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, OkResp{
|
||||
Ok: true,
|
||||
Result: H{
|
||||
"publicKey": generatedKeyPair.Public,
|
||||
"custodialId": id,
|
||||
"trackingId": trackingId,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
@ -1,71 +0,0 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/keystore"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/tasker"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/tasker/task"
|
||||
"github.com/grassrootseconomics/cic-custodial/pkg/keypair"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
type registrationResponse struct {
|
||||
PublicKey string `json:"publicKey"`
|
||||
TaskRef string `json:"taskRef"`
|
||||
}
|
||||
|
||||
func RegistrationHandler(
|
||||
taskerClient *tasker.TaskerClient,
|
||||
keystore keystore.Keystore,
|
||||
) func(echo.Context) error {
|
||||
return func(c echo.Context) error {
|
||||
generatedKeyPair, err := keypair.Generate()
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, errResp{
|
||||
Ok: false,
|
||||
Error: KEYPAIR_ERROR,
|
||||
})
|
||||
}
|
||||
|
||||
if err := keystore.WriteKeyPair(c.Request().Context(), generatedKeyPair); err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, errResp{
|
||||
Ok: false,
|
||||
Error: INTERNAL_ERROR,
|
||||
})
|
||||
}
|
||||
|
||||
taskPayload, err := json.Marshal(task.SystemPayload{
|
||||
PublicKey: generatedKeyPair.Public,
|
||||
})
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, errResp{
|
||||
Ok: false,
|
||||
Error: JSON_MARSHAL_ERROR,
|
||||
})
|
||||
}
|
||||
|
||||
task, err := taskerClient.CreateTask(
|
||||
tasker.PrepareAccountTask,
|
||||
tasker.DefaultPriority,
|
||||
&tasker.Task{
|
||||
Payload: taskPayload,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, errResp{
|
||||
Ok: false,
|
||||
Error: TASK_CHAIN_ERROR,
|
||||
})
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, okResp{
|
||||
Ok: true,
|
||||
Data: registrationResponse{
|
||||
PublicKey: generatedKeyPair.Public,
|
||||
TaskRef: task.ID,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
67
internal/api/sign.go
Normal file
67
internal/api/sign.go
Normal file
@ -0,0 +1,67 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/tasker"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
// SignTxHandler route.
|
||||
// POST: /api/sign/transfer
|
||||
// JSON Body:
|
||||
// trackingId -> Unique string
|
||||
// from -> ETH address
|
||||
// to -> ETH address
|
||||
// voucherAddress -> ETH address
|
||||
// amount -> int (6 d.p. precision)
|
||||
// e.g. 1000000 = 1 VOUCHER
|
||||
// Returns the task id.
|
||||
func SignTransferHandler(
|
||||
taskerClient *tasker.TaskerClient,
|
||||
) func(echo.Context) error {
|
||||
return func(c echo.Context) error {
|
||||
trackingId := uuid.NewString()
|
||||
|
||||
var transferRequest struct {
|
||||
From string `json:"from" validate:"required,eth_addr"`
|
||||
To string `json:"to" validate:"required,eth_addr"`
|
||||
VoucherAddress string `json:"voucherAddress" validate:"required,eth_addr"`
|
||||
Amount int64 `json:"amount" validate:"required,numeric"`
|
||||
}
|
||||
|
||||
if err := c.Bind(&transferRequest); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := c.Validate(transferRequest); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
taskPayload, err := json.Marshal(transferRequest)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = taskerClient.CreateTask(
|
||||
tasker.SignTransferTask,
|
||||
tasker.HighPriority,
|
||||
&tasker.Task{
|
||||
Id: trackingId,
|
||||
Payload: taskPayload,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, OkResp{
|
||||
Ok: true,
|
||||
Result: H{
|
||||
"trackingId": trackingId,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
@ -1,69 +0,0 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/tasker"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
type (
|
||||
transferRequest struct {
|
||||
From string `json:"from" validate:"required,eth_addr"`
|
||||
To string `json:"to" validate:"required,eth_addr"`
|
||||
VoucherAddress string `json:"voucherAddress" validate:"required,eth_addr"`
|
||||
Amount string `json:"amount" validate:"required,numeric"`
|
||||
}
|
||||
|
||||
transferResponse struct {
|
||||
TaskRef string `json:"taskRef"`
|
||||
}
|
||||
)
|
||||
|
||||
func TransferHandler(
|
||||
taskerClient *tasker.TaskerClient,
|
||||
) func(echo.Context) error {
|
||||
return func(c echo.Context) error {
|
||||
transferPayload := new(transferRequest)
|
||||
|
||||
if err := c.Bind(transferPayload); err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, errResp{
|
||||
Ok: false,
|
||||
Error: BIND_ERROR,
|
||||
})
|
||||
}
|
||||
if err := c.Validate(transferPayload); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
taskPayload, err := json.Marshal(transferPayload)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, errResp{
|
||||
Ok: false,
|
||||
Error: JSON_MARSHAL_ERROR,
|
||||
})
|
||||
}
|
||||
|
||||
task, err := taskerClient.CreateTask(
|
||||
tasker.TransferTokenTask,
|
||||
tasker.DefaultPriority,
|
||||
&tasker.Task{
|
||||
Payload: taskPayload,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, errResp{
|
||||
Ok: false,
|
||||
Error: TASK_CHAIN_ERROR,
|
||||
})
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, okResp{
|
||||
Ok: true,
|
||||
Data: transferResponse{
|
||||
TaskRef: task.ID,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
@ -1,20 +1,20 @@
|
||||
package api
|
||||
|
||||
const (
|
||||
INTERNAL_ERROR = "ERR_INTERNAL"
|
||||
KEYPAIR_ERROR = "ERR_GEN_KEYPAIR"
|
||||
JSON_MARSHAL_ERROR = "ERR_PAYLOAD_SERIALIZATION"
|
||||
TASK_CHAIN_ERROR = "ERR_START_TASK_CHAIN"
|
||||
VALIDATION_ERROR = "ERR_VALIDATE"
|
||||
BIND_ERROR = "ERR_BIND"
|
||||
INTERNAL_ERROR = "ERR_INTERNAL"
|
||||
VALIDATION_ERROR = "ERR_VALIDATE"
|
||||
DUPLICATE_ERROR = "ERR_DUPLICATE"
|
||||
)
|
||||
|
||||
type okResp struct {
|
||||
Ok bool `json:"ok"`
|
||||
Data interface{} `json:"data"`
|
||||
type H map[string]any
|
||||
|
||||
type OkResp struct {
|
||||
Ok bool `json:"ok"`
|
||||
Result H `json:"result"`
|
||||
}
|
||||
|
||||
type errResp struct {
|
||||
Ok bool `json:"ok"`
|
||||
Error string `json:"error"`
|
||||
type ErrResp struct {
|
||||
Ok bool `json:"ok"`
|
||||
Code string `json:"errorCode"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
@ -1,23 +1,21 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-playground/validator"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
type CustomValidator struct {
|
||||
Validator *validator.Validate
|
||||
type Validator struct {
|
||||
ValidatorProvider *validator.Validate
|
||||
}
|
||||
|
||||
func (cv *CustomValidator) Validate(i interface{}) error {
|
||||
if err := cv.Validator.Struct(i); err != nil {
|
||||
fmt.Println(err)
|
||||
return echo.NewHTTPError(http.StatusBadRequest, errResp{
|
||||
Ok: false,
|
||||
Error: VALIDATION_ERROR,
|
||||
func (v *Validator) Validate(i interface{}) error {
|
||||
if err := v.ValidatorProvider.Struct(i); err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, ErrResp{
|
||||
Ok: false,
|
||||
Code: VALIDATION_ERROR,
|
||||
})
|
||||
}
|
||||
return nil
|
||||
|
@ -7,7 +7,8 @@ import (
|
||||
"github.com/grassrootseconomics/cic-custodial/pkg/keypair"
|
||||
)
|
||||
|
||||
// Keystore defines how keypairs should be stored and accessed from a storage backend.
|
||||
type Keystore interface {
|
||||
WriteKeyPair(context.Context, keypair.Key) error
|
||||
WriteKeyPair(context.Context, keypair.Key) (uint, error)
|
||||
LoadPrivateKey(context.Context, string) (*ecdsa.PrivateKey, error)
|
||||
}
|
||||
|
@ -1,65 +0,0 @@
|
||||
package keystore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/grassrootseconomics/cic-custodial/pkg/keypair"
|
||||
"github.com/grassrootseconomics/cic-custodial/pkg/logg"
|
||||
"github.com/grassrootseconomics/cic-custodial/pkg/postgres"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/stretchr/testify/suite"
|
||||
"github.com/zerodha/logf"
|
||||
)
|
||||
|
||||
const (
|
||||
testDsn = "postgres://postgres:postgres@localhost:5432/cic_custodial"
|
||||
)
|
||||
|
||||
type itKeystoreSuite struct {
|
||||
suite.Suite
|
||||
keystore Keystore
|
||||
pgPool *pgxpool.Pool
|
||||
logg logf.Logger
|
||||
}
|
||||
|
||||
func TestItKeystoreSuite(t *testing.T) {
|
||||
suite.Run(t, new(itKeystoreSuite))
|
||||
}
|
||||
|
||||
func (s *itKeystoreSuite) SetupSuite() {
|
||||
logg := logg.NewLogg(logg.LoggOpts{
|
||||
Debug: true,
|
||||
Caller: true,
|
||||
})
|
||||
|
||||
pgPool, err := postgres.NewPostgresPool(postgres.PostgresPoolOpts{
|
||||
DSN: testDsn,
|
||||
})
|
||||
s.Require().NoError(err)
|
||||
s.pgPool = pgPool
|
||||
s.logg = logg
|
||||
|
||||
s.keystore, err = NewPostgresKeytore(Opts{
|
||||
PostgresPool: pgPool,
|
||||
Logg: logg,
|
||||
})
|
||||
s.Require().NoError(err)
|
||||
}
|
||||
|
||||
func (s *itKeystoreSuite) TearDownSuite() {
|
||||
_, err := s.pgPool.Exec(context.Background(), "DROP TABLE IF EXISTS keystore")
|
||||
s.Require().NoError(err)
|
||||
}
|
||||
|
||||
func (s *itKeystoreSuite) Test_Write_And_Load_KeyPair() {
|
||||
ctx := context.Background()
|
||||
keypair, err := keypair.Generate()
|
||||
s.NoError(err)
|
||||
|
||||
err = s.keystore.WriteKeyPair(ctx, keypair)
|
||||
s.NoError(err)
|
||||
|
||||
_, err = s.keystore.LoadPrivateKey(ctx, keypair.Public)
|
||||
s.NoError(err)
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
package keystore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
func applyMigration(dbPool *pgxpool.Pool) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
defer cancel()
|
||||
|
||||
_, err := dbPool.Exec(ctx, `
|
||||
CREATE TABLE IF NOT EXISTS keystore (
|
||||
id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
public_key TEXT NOT NULL,
|
||||
private_key TEXT NOT NULL,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
`)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@ -3,49 +3,52 @@ package keystore
|
||||
import (
|
||||
"context"
|
||||
"crypto/ecdsa"
|
||||
"fmt"
|
||||
|
||||
eth_crypto "github.com/celo-org/celo-blockchain/crypto"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/queries"
|
||||
"github.com/grassrootseconomics/cic-custodial/pkg/keypair"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/zerodha/logf"
|
||||
)
|
||||
|
||||
type Opts struct {
|
||||
PostgresPool *pgxpool.Pool
|
||||
Logg logf.Logger
|
||||
}
|
||||
|
||||
type PostgresKeystore struct {
|
||||
db *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewPostgresKeytore(o Opts) (Keystore, error) {
|
||||
if err := applyMigration(o.PostgresPool); err != nil {
|
||||
return nil, fmt.Errorf("keystore migration failed %v", err)
|
||||
type (
|
||||
Opts struct {
|
||||
PostgresPool *pgxpool.Pool
|
||||
Queries *queries.Queries
|
||||
}
|
||||
o.Logg.Info("Successfully ran keystore migrations")
|
||||
|
||||
PostgresKeystore struct {
|
||||
db *pgxpool.Pool
|
||||
queries *queries.Queries
|
||||
}
|
||||
)
|
||||
|
||||
func NewPostgresKeytore(o Opts) Keystore {
|
||||
return &PostgresKeystore{
|
||||
db: o.PostgresPool,
|
||||
}, nil
|
||||
db: o.PostgresPool,
|
||||
queries: o.Queries,
|
||||
}
|
||||
}
|
||||
|
||||
func (ks *PostgresKeystore) WriteKeyPair(ctx context.Context, keypair keypair.Key) error {
|
||||
_, err := ks.db.Exec(ctx, "INSERT INTO keystore(public_key, private_key) VALUES($1, $2)", keypair.Public, keypair.Private)
|
||||
if err != nil {
|
||||
return err
|
||||
// WriteKeyPair inserts a keypair into the db and returns the linked id.
|
||||
func (ks *PostgresKeystore) WriteKeyPair(ctx context.Context, keypair keypair.Key) (uint, error) {
|
||||
var (
|
||||
id uint
|
||||
)
|
||||
|
||||
if err := ks.db.QueryRow(ctx, ks.queries.WriteKeyPair, keypair.Public, keypair.Private).Scan(&id); err != nil {
|
||||
return id, err
|
||||
}
|
||||
|
||||
return nil
|
||||
return id, nil
|
||||
}
|
||||
|
||||
// LoadPrivateKey loads a private key as a crypto primitive for direct use. An id is used to search for the private key.
|
||||
func (ks *PostgresKeystore) LoadPrivateKey(ctx context.Context, publicKey string) (*ecdsa.PrivateKey, error) {
|
||||
var (
|
||||
privateKeyString string
|
||||
)
|
||||
|
||||
if err := ks.db.QueryRow(ctx, "SELECT private_key FROM keystore WHERE public_key=$1", publicKey).Scan(&privateKeyString); err != nil {
|
||||
if err := ks.db.QueryRow(ctx, ks.queries.LoadKeyPair, publicKey).Scan(&privateKeyString); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
@ -2,6 +2,7 @@ package nonce
|
||||
|
||||
import "context"
|
||||
|
||||
// Noncestore defines how a nonce store should be implemented for any storage backend.
|
||||
type Noncestore interface {
|
||||
Peek(context.Context, string) (uint64, error)
|
||||
Acquire(context.Context, string) (uint64, error)
|
||||
|
@ -10,8 +10,8 @@ import (
|
||||
)
|
||||
|
||||
type Opts struct {
|
||||
RedisPool *redispool.RedisPool
|
||||
ChainProvider *celo.Provider
|
||||
RedisPool *redispool.RedisPool
|
||||
CeloProvider *celo.Provider
|
||||
}
|
||||
|
||||
// RedisNoncestore implements `Noncestore`
|
||||
@ -23,7 +23,7 @@ type RedisNoncestore struct {
|
||||
func NewRedisNoncestore(o Opts) Noncestore {
|
||||
return &RedisNoncestore{
|
||||
redis: o.RedisPool,
|
||||
chainProvider: o.ChainProvider,
|
||||
chainProvider: o.CeloProvider,
|
||||
}
|
||||
}
|
||||
|
||||
|
27
internal/queries/queries.go
Normal file
27
internal/queries/queries.go
Normal file
@ -0,0 +1,27 @@
|
||||
package queries
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/knadh/goyesql/v2"
|
||||
)
|
||||
|
||||
type Queries struct {
|
||||
// Keystore
|
||||
WriteKeyPair string `query:"write-key-pair"`
|
||||
LoadKeyPair string `query:"load-key-pair"`
|
||||
// OTX
|
||||
CreateOTX string `query:"create-otx"`
|
||||
// Dispatch
|
||||
CreateDispatchStatus string `query:"create-dispatch-status"`
|
||||
}
|
||||
|
||||
func LoadQueries(q goyesql.Queries) (*Queries, error) {
|
||||
loadedQueries := &Queries{}
|
||||
|
||||
if err := goyesql.ScanToStruct(loadedQueries, q, nil); err != nil {
|
||||
return nil, fmt.Errorf("failed to scan queries %v", err)
|
||||
}
|
||||
|
||||
return loadedQueries, nil
|
||||
}
|
24
internal/store/dispatch.go
Normal file
24
internal/store/dispatch.go
Normal file
@ -0,0 +1,24 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
type Status string
|
||||
|
||||
func (s *PostgresStore) CreateDispatchStatus(ctx context.Context, dispatch DispatchStatus) (uint, error) {
|
||||
var (
|
||||
id uint
|
||||
)
|
||||
|
||||
if err := s.db.QueryRow(
|
||||
ctx,
|
||||
s.queries.CreateDispatchStatus,
|
||||
dispatch.OtxId,
|
||||
dispatch.Status,
|
||||
).Scan(&id); err != nil {
|
||||
return id, err
|
||||
}
|
||||
|
||||
return id, nil
|
||||
}
|
26
internal/store/otx.go
Normal file
26
internal/store/otx.go
Normal file
@ -0,0 +1,26 @@
|
||||
package store
|
||||
|
||||
import "context"
|
||||
|
||||
func (s *PostgresStore) CreateOTX(ctx context.Context, otx OTX) (uint, error) {
|
||||
var (
|
||||
id uint
|
||||
)
|
||||
|
||||
if err := s.db.QueryRow(
|
||||
ctx,
|
||||
s.queries.CreateOTX,
|
||||
otx.TrackingId,
|
||||
otx.Type,
|
||||
otx.RawTx,
|
||||
otx.TxHash,
|
||||
otx.From,
|
||||
otx.Data,
|
||||
otx.GasPrice,
|
||||
otx.Nonce,
|
||||
).Scan(&id); err != nil {
|
||||
return id, err
|
||||
}
|
||||
|
||||
return id, nil
|
||||
}
|
25
internal/store/postgres.go
Normal file
25
internal/store/postgres.go
Normal file
@ -0,0 +1,25 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/queries"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type (
|
||||
Opts struct {
|
||||
PostgresPool *pgxpool.Pool
|
||||
Queries *queries.Queries
|
||||
}
|
||||
|
||||
PostgresStore struct {
|
||||
db *pgxpool.Pool
|
||||
queries *queries.Queries
|
||||
}
|
||||
)
|
||||
|
||||
func NewPostgresStore(o Opts) Store {
|
||||
return &PostgresStore{
|
||||
db: o.PostgresPool,
|
||||
queries: o.Queries,
|
||||
}
|
||||
}
|
32
internal/store/store.go
Normal file
32
internal/store/store.go
Normal file
@ -0,0 +1,32 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/grassrootseconomics/cic-custodial/pkg/status"
|
||||
)
|
||||
|
||||
type (
|
||||
OTX struct {
|
||||
TrackingId string
|
||||
Type string
|
||||
RawTx string
|
||||
TxHash string
|
||||
From string
|
||||
Data string
|
||||
GasPrice uint64
|
||||
Nonce uint64
|
||||
}
|
||||
|
||||
DispatchStatus struct {
|
||||
OtxId uint
|
||||
Status status.Status
|
||||
}
|
||||
|
||||
Store interface {
|
||||
// OTX (Custodial originating transactions).
|
||||
CreateOTX(ctx context.Context, otx OTX) (id uint, err error)
|
||||
// Dispatch status.
|
||||
CreateDispatchStatus(ctx context.Context, dispatch DispatchStatus) (id uint, err error)
|
||||
}
|
||||
)
|
@ -73,6 +73,7 @@ func (ts *TaskerServer) Stop() {
|
||||
|
||||
func expectedFailures(err error) bool {
|
||||
switch err {
|
||||
// Ignore lock contention errors; retry until lock obtain.
|
||||
case redislock.ErrNotObtained:
|
||||
return false
|
||||
default:
|
||||
@ -80,6 +81,7 @@ func expectedFailures(err error) bool {
|
||||
}
|
||||
}
|
||||
|
||||
// Immidiatel
|
||||
func retryDelay(count int, err error, task *asynq.Task) time.Duration {
|
||||
if count < fixedRetryCount {
|
||||
return fixedRetryPeriod
|
||||
|
556
internal/tasker/task/account.go
Normal file
556
internal/tasker/task/account.go
Normal file
@ -0,0 +1,556 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math/big"
|
||||
|
||||
"github.com/bsm/redislock"
|
||||
"github.com/celo-org/celo-blockchain/common/hexutil"
|
||||
celo "github.com/grassrootseconomics/cic-celo-sdk"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/nonce"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/store"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/tasker"
|
||||
"github.com/grassrootseconomics/w3-celo-patch"
|
||||
"github.com/grassrootseconomics/w3-celo-patch/module/eth"
|
||||
"github.com/hibiken/asynq"
|
||||
"github.com/nats-io/nats.go"
|
||||
)
|
||||
|
||||
type (
|
||||
AccountPayload struct {
|
||||
PublicKey string `json:"publicKey"`
|
||||
TrackingId string `json:"trackingId"`
|
||||
}
|
||||
|
||||
accountEventPayload struct {
|
||||
TrackingId string `json:"trackingId"`
|
||||
}
|
||||
)
|
||||
|
||||
func PrepareAccount(
|
||||
noncestore nonce.Noncestore,
|
||||
taskerClient *tasker.TaskerClient,
|
||||
js nats.JetStreamContext,
|
||||
) func(context.Context, *asynq.Task) error {
|
||||
return func(ctx context.Context, t *asynq.Task) error {
|
||||
var (
|
||||
p AccountPayload
|
||||
)
|
||||
|
||||
if err := json.Unmarshal(t.Payload(), &p); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := noncestore.SetNewAccountNonce(ctx, p.PublicKey); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err := taskerClient.CreateTask(
|
||||
tasker.RegisterAccountOnChain,
|
||||
tasker.DefaultPriority,
|
||||
&tasker.Task{
|
||||
Payload: t.Payload(),
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = taskerClient.CreateTask(
|
||||
tasker.GiftTokenTask,
|
||||
tasker.DefaultPriority,
|
||||
&tasker.Task{
|
||||
Payload: t.Payload(),
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
eventPayload := &accountEventPayload{
|
||||
TrackingId: p.TrackingId,
|
||||
}
|
||||
|
||||
eventJson, err := json.Marshal(eventPayload)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = js.Publish("CUSTODIAL.accountNewNonce", eventJson)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func RegisterAccountOnChainProcessor(
|
||||
celoProvider *celo.Provider,
|
||||
lockProvider *redislock.Client,
|
||||
noncestore nonce.Noncestore,
|
||||
pg store.Store,
|
||||
system *tasker.SystemContainer,
|
||||
taskerClient *tasker.TaskerClient,
|
||||
js nats.JetStreamContext,
|
||||
) func(context.Context, *asynq.Task) error {
|
||||
return func(ctx context.Context, t *asynq.Task) error {
|
||||
var (
|
||||
p AccountPayload
|
||||
)
|
||||
|
||||
if err := json.Unmarshal(t.Payload(), &p); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
lock, err := lockProvider.Obtain(ctx, system.LockPrefix+system.PublicKey, system.LockTimeout, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer lock.Release(ctx)
|
||||
|
||||
nonce, err := noncestore.Acquire(ctx, system.PublicKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
input, err := system.Abis["add"].EncodeArgs(w3.A(p.PublicKey))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// TODO: Review gas params.
|
||||
builtTx, err := celoProvider.SignContractExecutionTx(
|
||||
system.PrivateKey,
|
||||
celo.ContractExecutionTxOpts{
|
||||
ContractAddress: system.AccountIndexContract,
|
||||
InputData: input,
|
||||
GasPrice: big.NewInt(20000000000),
|
||||
GasLimit: system.TokenTransferGasLimit,
|
||||
Nonce: nonce,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
if err := noncestore.Return(ctx, system.PublicKey); err != nil {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
rawTx, err := builtTx.MarshalBinary()
|
||||
if err != nil {
|
||||
if err := noncestore.Return(ctx, system.PublicKey); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
id, err := pg.CreateOTX(ctx, store.OTX{
|
||||
TrackingId: p.TrackingId,
|
||||
Type: "ACCOUNT_REGISTER",
|
||||
RawTx: hexutil.Encode(rawTx),
|
||||
TxHash: builtTx.Hash().Hex(),
|
||||
From: system.PublicKey,
|
||||
Data: hexutil.Encode(builtTx.Data()),
|
||||
GasPrice: builtTx.GasPrice().Uint64(),
|
||||
Nonce: builtTx.Nonce(),
|
||||
})
|
||||
if err != nil {
|
||||
if err := noncestore.Return(ctx, system.PublicKey); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
disptachJobPayload, err := json.Marshal(TxPayload{
|
||||
OtxId: id,
|
||||
Tx: builtTx,
|
||||
})
|
||||
if err != nil {
|
||||
if err := noncestore.Return(ctx, system.PublicKey); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = taskerClient.CreateTask(
|
||||
tasker.TxDispatchTask,
|
||||
tasker.HighPriority,
|
||||
&tasker.Task{
|
||||
Payload: disptachJobPayload,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
if err := noncestore.Return(ctx, system.PublicKey); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = taskerClient.CreateTask(
|
||||
tasker.GiftGasTask,
|
||||
tasker.DefaultPriority,
|
||||
&tasker.Task{
|
||||
Payload: t.Payload(),
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
eventPayload := &accountEventPayload{
|
||||
TrackingId: p.TrackingId,
|
||||
}
|
||||
|
||||
eventJson, err := json.Marshal(eventPayload)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = js.Publish("CUSTODIAL.accountRegister", eventJson)
|
||||
if err != nil {
|
||||
if err := noncestore.Return(ctx, system.PublicKey); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func GiftGasProcessor(
|
||||
celoProvider *celo.Provider,
|
||||
lockProvider *redislock.Client,
|
||||
noncestore nonce.Noncestore,
|
||||
pg store.Store,
|
||||
system *tasker.SystemContainer,
|
||||
taskerClient *tasker.TaskerClient,
|
||||
js nats.JetStreamContext,
|
||||
) func(context.Context, *asynq.Task) error {
|
||||
return func(ctx context.Context, t *asynq.Task) error {
|
||||
var (
|
||||
p AccountPayload
|
||||
)
|
||||
|
||||
if err := json.Unmarshal(t.Payload(), &p); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
lock, err := lockProvider.Obtain(ctx, system.LockPrefix+system.PublicKey, system.LockTimeout, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer lock.Release(ctx)
|
||||
|
||||
nonce, err := noncestore.Acquire(ctx, system.PublicKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// TODO: Review gas params
|
||||
builtTx, err := celoProvider.SignGasTransferTx(
|
||||
system.PrivateKey,
|
||||
celo.GasTransferTxOpts{
|
||||
To: w3.A(p.PublicKey),
|
||||
Nonce: nonce,
|
||||
Value: system.GiftableGasValue,
|
||||
GasPrice: celo.FixedMinGas,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
if err := noncestore.Return(ctx, system.PublicKey); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
rawTx, err := builtTx.MarshalBinary()
|
||||
if err != nil {
|
||||
if err := noncestore.Return(ctx, system.PublicKey); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
id, err := pg.CreateOTX(ctx, store.OTX{
|
||||
TrackingId: p.TrackingId,
|
||||
Type: "GIFT_GAS",
|
||||
RawTx: hexutil.Encode(rawTx),
|
||||
TxHash: builtTx.Hash().Hex(),
|
||||
From: system.PublicKey,
|
||||
Data: hexutil.Encode(builtTx.Data()),
|
||||
GasPrice: builtTx.GasPrice().Uint64(),
|
||||
Nonce: builtTx.Nonce(),
|
||||
})
|
||||
if err != nil {
|
||||
if err := noncestore.Return(ctx, system.PublicKey); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
disptachJobPayload, err := json.Marshal(TxPayload{
|
||||
OtxId: id,
|
||||
Tx: builtTx,
|
||||
})
|
||||
if err != nil {
|
||||
if err := noncestore.Return(ctx, system.PublicKey); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = taskerClient.CreateTask(
|
||||
tasker.TxDispatchTask,
|
||||
tasker.HighPriority,
|
||||
&tasker.Task{
|
||||
Payload: disptachJobPayload,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
if err := noncestore.Return(ctx, system.PublicKey); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
eventPayload := &accountEventPayload{
|
||||
TrackingId: p.TrackingId,
|
||||
}
|
||||
|
||||
eventJson, err := json.Marshal(eventPayload)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = js.Publish("CUSTODIAL.giftNewAccountGas", eventJson)
|
||||
if err != nil {
|
||||
if err := noncestore.Return(ctx, system.PublicKey); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func GiftTokenProcessor(
|
||||
celoProvider *celo.Provider,
|
||||
lockProvider *redislock.Client,
|
||||
noncestore nonce.Noncestore,
|
||||
pg store.Store,
|
||||
system *tasker.SystemContainer,
|
||||
taskerClient *tasker.TaskerClient,
|
||||
js nats.JetStreamContext,
|
||||
) func(context.Context, *asynq.Task) error {
|
||||
return func(ctx context.Context, t *asynq.Task) error {
|
||||
var (
|
||||
p AccountPayload
|
||||
)
|
||||
|
||||
if err := json.Unmarshal(t.Payload(), &p); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
lock, err := lockProvider.Obtain(ctx, system.LockPrefix+system.PublicKey, system.LockTimeout, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer lock.Release(ctx)
|
||||
|
||||
nonce, err := noncestore.Acquire(ctx, system.PublicKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
input, err := system.Abis["mintTo"].EncodeArgs(w3.A(p.PublicKey), system.GiftableTokenValue)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// TODO: Review gas params.
|
||||
builtTx, err := celoProvider.SignContractExecutionTx(
|
||||
system.PrivateKey,
|
||||
celo.ContractExecutionTxOpts{
|
||||
ContractAddress: system.GiftableToken,
|
||||
InputData: input,
|
||||
GasPrice: big.NewInt(20000000000),
|
||||
GasLimit: system.TokenTransferGasLimit,
|
||||
Nonce: nonce,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
if err := noncestore.Return(ctx, system.PublicKey); err != nil {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
rawTx, err := builtTx.MarshalBinary()
|
||||
if err != nil {
|
||||
if err := noncestore.Return(ctx, system.PublicKey); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
id, err := pg.CreateOTX(ctx, store.OTX{
|
||||
TrackingId: p.TrackingId,
|
||||
Type: "GIFT_VOUCHER",
|
||||
RawTx: hexutil.Encode(rawTx),
|
||||
TxHash: builtTx.Hash().Hex(),
|
||||
From: system.PublicKey,
|
||||
Data: hexutil.Encode(builtTx.Data()),
|
||||
GasPrice: builtTx.GasPrice().Uint64(),
|
||||
Nonce: builtTx.Nonce(),
|
||||
})
|
||||
if err != nil {
|
||||
if err := noncestore.Return(ctx, system.PublicKey); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
disptachJobPayload, err := json.Marshal(TxPayload{
|
||||
OtxId: id,
|
||||
Tx: builtTx,
|
||||
})
|
||||
if err != nil {
|
||||
if err := noncestore.Return(ctx, system.PublicKey); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = taskerClient.CreateTask(
|
||||
tasker.TxDispatchTask,
|
||||
tasker.HighPriority,
|
||||
&tasker.Task{
|
||||
Payload: disptachJobPayload,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
if err := noncestore.Return(ctx, system.PublicKey); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
eventPayload := &accountEventPayload{
|
||||
TrackingId: p.TrackingId,
|
||||
}
|
||||
|
||||
eventJson, err := json.Marshal(eventPayload)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = js.Publish("CUSTODIAL.giftNewAccountVoucher", eventJson)
|
||||
if err != nil {
|
||||
if err := noncestore.Return(ctx, system.PublicKey); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: https://github.com/grassrootseconomics/cic-custodial/issues/43
|
||||
// TODO:
|
||||
func RefillGasProcessor(
|
||||
celoProvider *celo.Provider,
|
||||
nonceProvider nonce.Noncestore,
|
||||
lockProvider *redislock.Client,
|
||||
system *tasker.SystemContainer,
|
||||
taskerClient *tasker.TaskerClient,
|
||||
) func(context.Context, *asynq.Task) error {
|
||||
return func(ctx context.Context, t *asynq.Task) error {
|
||||
var (
|
||||
p AccountPayload
|
||||
balance big.Int
|
||||
)
|
||||
if err := json.Unmarshal(t.Payload(), &p); err != nil {
|
||||
return fmt.Errorf("json.Unmarshal failed: %v: %w", err, asynq.SkipRetry)
|
||||
}
|
||||
|
||||
if err := celoProvider.Client.CallCtx(
|
||||
ctx,
|
||||
eth.Balance(w3.A(p.PublicKey), nil).Returns(&balance),
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if belowThreshold := balance.Cmp(system.GasRefillThreshold); belowThreshold > 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
lock, err := lockProvider.Obtain(ctx, system.LockPrefix+system.PublicKey, system.LockTimeout, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer lock.Release(ctx)
|
||||
|
||||
nonce, err := nonceProvider.Acquire(ctx, system.PublicKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
builtTx, err := celoProvider.SignGasTransferTx(
|
||||
system.PrivateKey,
|
||||
celo.GasTransferTxOpts{
|
||||
To: w3.A(p.PublicKey),
|
||||
Nonce: nonce,
|
||||
Value: system.GasRefillValue,
|
||||
GasPrice: celo.FixedMinGas,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
if err := nonceProvider.Return(ctx, p.PublicKey); err != nil {
|
||||
return err
|
||||
}
|
||||
return fmt.Errorf("nonce.Return failed: %v: %w", err, asynq.SkipRetry)
|
||||
}
|
||||
|
||||
disptachJobPayload, err := json.Marshal(TxPayload{
|
||||
Tx: builtTx,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("json.Marshal failed: %v: %w", err, asynq.SkipRetry)
|
||||
}
|
||||
|
||||
_, err = taskerClient.CreateTask(
|
||||
tasker.TxDispatchTask,
|
||||
tasker.HighPriority,
|
||||
&tasker.Task{
|
||||
Payload: disptachJobPayload,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
@ -8,16 +8,31 @@ import (
|
||||
"github.com/celo-org/celo-blockchain/common"
|
||||
"github.com/celo-org/celo-blockchain/core/types"
|
||||
celo "github.com/grassrootseconomics/cic-celo-sdk"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/store"
|
||||
"github.com/grassrootseconomics/cic-custodial/pkg/status"
|
||||
"github.com/grassrootseconomics/w3-celo-patch/module/eth"
|
||||
"github.com/hibiken/asynq"
|
||||
"github.com/nats-io/nats.go"
|
||||
)
|
||||
|
||||
type TxPayload struct {
|
||||
Tx *types.Transaction `json:"tx"`
|
||||
}
|
||||
type (
|
||||
TxPayload struct {
|
||||
OtxId uint `json:"otxId"`
|
||||
Tx *types.Transaction `json:"tx"`
|
||||
}
|
||||
|
||||
dispatchEventPayload struct {
|
||||
OtxId uint
|
||||
TxHash string
|
||||
DispatchStatus status.Status
|
||||
}
|
||||
)
|
||||
|
||||
func TxDispatch(
|
||||
celoProvider *celo.Provider,
|
||||
pg store.Store,
|
||||
js nats.JetStreamContext,
|
||||
|
||||
) func(context.Context, *asynq.Task) error {
|
||||
return func(ctx context.Context, t *asynq.Task) error {
|
||||
var (
|
||||
@ -26,13 +41,65 @@ func TxDispatch(
|
||||
)
|
||||
|
||||
if err := json.Unmarshal(t.Payload(), &p); err != nil {
|
||||
return fmt.Errorf("json.Unmarshal failed: %v: %w", err, asynq.SkipRetry)
|
||||
return err
|
||||
}
|
||||
|
||||
dispatchStatus := store.DispatchStatus{
|
||||
OtxId: p.OtxId,
|
||||
}
|
||||
|
||||
eventPayload := &dispatchEventPayload{
|
||||
OtxId: p.OtxId,
|
||||
}
|
||||
|
||||
if err := celoProvider.Client.CallCtx(
|
||||
ctx,
|
||||
eth.SendTx(p.Tx).Returns(&txHash),
|
||||
); err != nil {
|
||||
switch err.Error() {
|
||||
case celo.ErrGasPriceLow:
|
||||
dispatchStatus.Status = status.FailGasPrice
|
||||
case celo.ErrInsufficientGas:
|
||||
dispatchStatus.Status = status.FailInsufficientGas
|
||||
case celo.ErrNonceLow:
|
||||
dispatchStatus.Status = status.FailNonce
|
||||
default:
|
||||
dispatchStatus.Status = status.Unknown
|
||||
}
|
||||
|
||||
_, err := pg.CreateDispatchStatus(ctx, dispatchStatus)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
eventJson, err := json.Marshal(eventPayload)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = js.Publish("CUSTODIAL.dispatchFail", eventJson, nats.MsgId(txHash.Hex()))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return fmt.Errorf("dispatch: failed %v: %w", err, asynq.SkipRetry)
|
||||
}
|
||||
|
||||
dispatchStatus.Status = status.Successful
|
||||
_, err := pg.CreateDispatchStatus(ctx, dispatchStatus)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
eventPayload.TxHash = txHash.Hex()
|
||||
|
||||
eventJson, err := json.Marshal(eventPayload)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = js.Publish("CUSTODIAL.dispatchSuccess", eventJson, nats.MsgId(txHash.Hex()))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
182
internal/tasker/task/sign.go
Normal file
182
internal/tasker/task/sign.go
Normal file
@ -0,0 +1,182 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"math/big"
|
||||
|
||||
"github.com/bsm/redislock"
|
||||
"github.com/celo-org/celo-blockchain/common/hexutil"
|
||||
celo "github.com/grassrootseconomics/cic-celo-sdk"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/keystore"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/nonce"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/store"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/tasker"
|
||||
"github.com/grassrootseconomics/w3-celo-patch"
|
||||
"github.com/hibiken/asynq"
|
||||
"github.com/nats-io/nats.go"
|
||||
)
|
||||
|
||||
type (
|
||||
TransferPayload struct {
|
||||
TrackingId string `json:"trackingId"`
|
||||
From string `json:"from" `
|
||||
To string `json:"to"`
|
||||
VoucherAddress string `json:"voucherAddress"`
|
||||
Amount int64 `json:"amount"`
|
||||
}
|
||||
|
||||
transferEventPayload struct {
|
||||
DispatchTaskId string `json:"dispatchTaskId"`
|
||||
OTXId uint `json:"otxId"`
|
||||
TrackingId string `json:"trackingId"`
|
||||
TxHash string `json:"txHash"`
|
||||
}
|
||||
)
|
||||
|
||||
func SignTransfer(
|
||||
celoProvider *celo.Provider,
|
||||
keystore keystore.Keystore,
|
||||
lockProvider *redislock.Client,
|
||||
noncestore nonce.Noncestore,
|
||||
pg store.Store,
|
||||
system *tasker.SystemContainer,
|
||||
taskerClient *tasker.TaskerClient,
|
||||
js nats.JetStreamContext,
|
||||
) func(context.Context, *asynq.Task) error {
|
||||
return func(ctx context.Context, t *asynq.Task) error {
|
||||
var (
|
||||
p TransferPayload
|
||||
)
|
||||
|
||||
if err := json.Unmarshal(t.Payload(), &p); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
lock, err := lockProvider.Obtain(
|
||||
ctx,
|
||||
system.LockPrefix+p.From,
|
||||
system.LockTimeout,
|
||||
nil,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer lock.Release(ctx)
|
||||
|
||||
key, err := keystore.LoadPrivateKey(ctx, p.From)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
nonce, err := noncestore.Acquire(ctx, p.From)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
input, err := system.Abis["transfer"].EncodeArgs(w3.A(p.To), big.NewInt(p.Amount))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// TODO: Review gas params.
|
||||
builtTx, err := celoProvider.SignContractExecutionTx(
|
||||
key,
|
||||
celo.ContractExecutionTxOpts{
|
||||
ContractAddress: w3.A(p.VoucherAddress),
|
||||
InputData: input,
|
||||
GasPrice: big.NewInt(20000000000),
|
||||
GasLimit: system.TokenTransferGasLimit,
|
||||
Nonce: nonce,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
if err := noncestore.Return(ctx, p.From); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
rawTx, err := builtTx.MarshalBinary()
|
||||
if err != nil {
|
||||
if err := noncestore.Return(ctx, p.From); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
id, err := pg.CreateOTX(ctx, store.OTX{
|
||||
TrackingId: p.TrackingId,
|
||||
Type: "TRANSFER",
|
||||
RawTx: hexutil.Encode(rawTx),
|
||||
TxHash: builtTx.Hash().Hex(),
|
||||
From: p.From,
|
||||
Data: hexutil.Encode(builtTx.Data()),
|
||||
GasPrice: builtTx.GasPrice().Uint64(),
|
||||
Nonce: builtTx.Nonce(),
|
||||
})
|
||||
if err != nil {
|
||||
if err := noncestore.Return(ctx, p.From); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
disptachJobPayload, err := json.Marshal(TxPayload{
|
||||
OtxId: id,
|
||||
Tx: builtTx,
|
||||
})
|
||||
if err != nil {
|
||||
if err := noncestore.Return(ctx, p.From); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
dispatchTask, err := taskerClient.CreateTask(
|
||||
tasker.TxDispatchTask,
|
||||
tasker.HighPriority,
|
||||
&tasker.Task{
|
||||
Payload: disptachJobPayload,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
if err := noncestore.Return(ctx, p.From); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
eventPayload := &transferEventPayload{
|
||||
DispatchTaskId: dispatchTask.ID,
|
||||
OTXId: id,
|
||||
TrackingId: p.TrackingId,
|
||||
TxHash: builtTx.Hash().Hex(),
|
||||
}
|
||||
|
||||
eventJson, err := json.Marshal(eventPayload)
|
||||
if err != nil {
|
||||
if err := noncestore.Return(ctx, p.From); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = js.Publish("CUSTODIAL.transferSign", eventJson, nats.MsgId(builtTx.Hash().Hex()))
|
||||
if err != nil {
|
||||
if err := noncestore.Return(ctx, p.From); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
@ -1,268 +0,0 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math/big"
|
||||
|
||||
"github.com/bsm/redislock"
|
||||
celo "github.com/grassrootseconomics/cic-celo-sdk"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/nonce"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/tasker"
|
||||
"github.com/grassrootseconomics/w3-celo-patch"
|
||||
"github.com/grassrootseconomics/w3-celo-patch/module/eth"
|
||||
"github.com/hibiken/asynq"
|
||||
)
|
||||
|
||||
type SystemPayload struct {
|
||||
PublicKey string `json:"publicKey"`
|
||||
}
|
||||
|
||||
func PrepareAccount(
|
||||
nonceProvider nonce.Noncestore,
|
||||
taskerClient *tasker.TaskerClient,
|
||||
) func(context.Context, *asynq.Task) error {
|
||||
return func(ctx context.Context, t *asynq.Task) error {
|
||||
var p SystemPayload
|
||||
if err := json.Unmarshal(t.Payload(), &p); err != nil {
|
||||
return fmt.Errorf("json.Unmarshal failed: %v: %w", err, asynq.SkipRetry)
|
||||
}
|
||||
|
||||
if err := nonceProvider.SetNewAccountNonce(ctx, p.PublicKey); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err := taskerClient.CreateTask(
|
||||
tasker.GiftGasTask,
|
||||
tasker.DefaultPriority,
|
||||
&tasker.Task{
|
||||
Payload: t.Payload(),
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = taskerClient.CreateTask(
|
||||
tasker.GiftTokenTask,
|
||||
tasker.DefaultPriority,
|
||||
&tasker.Task{
|
||||
Payload: t.Payload(),
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func GiftGasProcessor(
|
||||
celoProvider *celo.Provider,
|
||||
nonceProvider nonce.Noncestore,
|
||||
lockProvider *redislock.Client,
|
||||
system *tasker.SystemContainer,
|
||||
taskerClient *tasker.TaskerClient,
|
||||
) func(context.Context, *asynq.Task) error {
|
||||
return func(ctx context.Context, t *asynq.Task) error {
|
||||
var p SystemPayload
|
||||
if err := json.Unmarshal(t.Payload(), &p); err != nil {
|
||||
return fmt.Errorf("json.Unmarshal failed: %v: %w", err, asynq.SkipRetry)
|
||||
}
|
||||
|
||||
lock, err := lockProvider.Obtain(ctx, system.LockPrefix+system.PublicKey, system.LockTimeout, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer lock.Release(ctx)
|
||||
|
||||
nonce, err := nonceProvider.Acquire(ctx, system.PublicKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
builtTx, err := celoProvider.SignGasTransferTx(
|
||||
system.PrivateKey,
|
||||
celo.GasTransferTxOpts{
|
||||
To: w3.A(p.PublicKey),
|
||||
Nonce: nonce,
|
||||
Value: system.GiftableGasValue,
|
||||
GasPrice: celo.FixedMinGas,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
if err := nonceProvider.Return(ctx, p.PublicKey); err != nil {
|
||||
return err
|
||||
}
|
||||
return fmt.Errorf("nonce.Return failed: %v: %w", err, asynq.SkipRetry)
|
||||
}
|
||||
|
||||
disptachJobPayload, err := json.Marshal(TxPayload{
|
||||
Tx: builtTx,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("json.Marshal failed: %v: %w", err, asynq.SkipRetry)
|
||||
}
|
||||
|
||||
_, err = taskerClient.CreateTask(
|
||||
tasker.TxDispatchTask,
|
||||
tasker.HighPriority,
|
||||
&tasker.Task{
|
||||
Payload: disptachJobPayload,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func GiftTokenProcessor(
|
||||
celoProvider *celo.Provider,
|
||||
nonceProvider nonce.Noncestore,
|
||||
lockProvider *redislock.Client,
|
||||
system *tasker.SystemContainer,
|
||||
taskerClient *tasker.TaskerClient,
|
||||
) func(context.Context, *asynq.Task) error {
|
||||
return func(ctx context.Context, t *asynq.Task) error {
|
||||
var p SystemPayload
|
||||
if err := json.Unmarshal(t.Payload(), &p); err != nil {
|
||||
return fmt.Errorf("json.Unmarshal failed: %v: %w", err, asynq.SkipRetry)
|
||||
}
|
||||
|
||||
publicKey := w3.A(p.PublicKey)
|
||||
|
||||
lock, err := lockProvider.Obtain(ctx, system.LockPrefix+system.PublicKey, system.LockTimeout, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer lock.Release(ctx)
|
||||
|
||||
nonce, err := nonceProvider.Acquire(ctx, system.PublicKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
input, err := system.Abis["mint"].EncodeArgs(publicKey, system.GiftableTokenValue)
|
||||
if err != nil {
|
||||
return fmt.Errorf("ABI encode failed %v: %w", err, asynq.SkipRetry)
|
||||
}
|
||||
|
||||
builtTx, err := celoProvider.SignContractExecutionTx(
|
||||
system.PrivateKey,
|
||||
celo.ContractExecutionTxOpts{
|
||||
ContractAddress: system.GiftableToken,
|
||||
InputData: input,
|
||||
GasPrice: celo.FixedMinGas,
|
||||
GasLimit: system.TokenTransferGasLimit,
|
||||
Nonce: nonce,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
if err := nonceProvider.Return(ctx, p.PublicKey); err != nil {
|
||||
return err
|
||||
}
|
||||
return fmt.Errorf("nonce.Return failed: %v: %w", err, asynq.SkipRetry)
|
||||
}
|
||||
|
||||
disptachJobPayload, err := json.Marshal(TxPayload{
|
||||
Tx: builtTx,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("json.Marshal failed: %v: %w", err, asynq.SkipRetry)
|
||||
}
|
||||
|
||||
_, err = taskerClient.CreateTask(
|
||||
tasker.TxDispatchTask,
|
||||
tasker.HighPriority,
|
||||
&tasker.Task{
|
||||
Payload: disptachJobPayload,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func RefillGasProcessor(
|
||||
celoProvider *celo.Provider,
|
||||
nonceProvider nonce.Noncestore,
|
||||
lockProvider *redislock.Client,
|
||||
system *tasker.SystemContainer,
|
||||
taskerClient *tasker.TaskerClient,
|
||||
) func(context.Context, *asynq.Task) error {
|
||||
return func(ctx context.Context, t *asynq.Task) error {
|
||||
var (
|
||||
p SystemPayload
|
||||
balance big.Int
|
||||
)
|
||||
if err := json.Unmarshal(t.Payload(), &p); err != nil {
|
||||
return fmt.Errorf("json.Unmarshal failed: %v: %w", err, asynq.SkipRetry)
|
||||
}
|
||||
|
||||
if err := celoProvider.Client.CallCtx(
|
||||
ctx,
|
||||
eth.Balance(w3.A(p.PublicKey), nil).Returns(&balance),
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if belowThreshold := balance.Cmp(system.GasRefillThreshold); belowThreshold > 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
lock, err := lockProvider.Obtain(ctx, system.LockPrefix+system.PublicKey, system.LockTimeout, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer lock.Release(ctx)
|
||||
|
||||
nonce, err := nonceProvider.Acquire(ctx, system.PublicKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
builtTx, err := celoProvider.SignGasTransferTx(
|
||||
system.PrivateKey,
|
||||
celo.GasTransferTxOpts{
|
||||
To: w3.A(p.PublicKey),
|
||||
Nonce: nonce,
|
||||
Value: system.GasRefillValue,
|
||||
GasPrice: celo.FixedMinGas,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
if err := nonceProvider.Return(ctx, p.PublicKey); err != nil {
|
||||
return err
|
||||
}
|
||||
return fmt.Errorf("nonce.Return failed: %v: %w", err, asynq.SkipRetry)
|
||||
}
|
||||
|
||||
disptachJobPayload, err := json.Marshal(TxPayload{
|
||||
Tx: builtTx,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("json.Marshal failed: %v: %w", err, asynq.SkipRetry)
|
||||
}
|
||||
|
||||
_, err = taskerClient.CreateTask(
|
||||
tasker.TxDispatchTask,
|
||||
tasker.HighPriority,
|
||||
&tasker.Task{
|
||||
Payload: disptachJobPayload,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
@ -1,124 +0,0 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math"
|
||||
"math/big"
|
||||
"strconv"
|
||||
|
||||
"github.com/bsm/redislock"
|
||||
celo "github.com/grassrootseconomics/cic-celo-sdk"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/keystore"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/nonce"
|
||||
"github.com/grassrootseconomics/cic-custodial/internal/tasker"
|
||||
"github.com/grassrootseconomics/w3-celo-patch"
|
||||
"github.com/hibiken/asynq"
|
||||
)
|
||||
|
||||
type TransferPayload struct {
|
||||
From string `json:"from"`
|
||||
To string `json:"to"`
|
||||
VoucherAddress string `json:"voucherAddress"`
|
||||
Amount string `json:"amount"`
|
||||
}
|
||||
|
||||
func TransferToken(
|
||||
celoProvider *celo.Provider,
|
||||
nonceProvider nonce.Noncestore,
|
||||
keystoreProvider keystore.Keystore,
|
||||
lockProvider *redislock.Client,
|
||||
system *tasker.SystemContainer,
|
||||
taskerClient *tasker.TaskerClient,
|
||||
) func(context.Context, *asynq.Task) error {
|
||||
return func(ctx context.Context, t *asynq.Task) error {
|
||||
var p TransferPayload
|
||||
|
||||
if err := json.Unmarshal(t.Payload(), &p); err != nil {
|
||||
return fmt.Errorf("json.Unmarshal failed: %v: %w", err, asynq.SkipRetry)
|
||||
}
|
||||
|
||||
lock, err := lockProvider.Obtain(ctx, system.LockPrefix+p.From, system.LockTimeout, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer lock.Release(ctx)
|
||||
|
||||
nonce, err := nonceProvider.Acquire(ctx, p.From)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
key, err := keystoreProvider.LoadPrivateKey(ctx, p.From)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
input, err := system.Abis["transfer"].EncodeArgs(w3.A(p.To), parseTransferValue(p.Amount, system.TokenDecimals))
|
||||
if err != nil {
|
||||
return fmt.Errorf("ABI encode failed %v: %w", err, asynq.SkipRetry)
|
||||
}
|
||||
|
||||
builtTx, err := celoProvider.SignContractExecutionTx(
|
||||
key,
|
||||
celo.ContractExecutionTxOpts{
|
||||
ContractAddress: system.GiftableToken,
|
||||
InputData: input,
|
||||
GasPrice: celo.FixedMinGas,
|
||||
GasLimit: system.TokenTransferGasLimit,
|
||||
Nonce: nonce,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
if err := nonceProvider.Return(ctx, p.From); err != nil {
|
||||
return err
|
||||
}
|
||||
return fmt.Errorf("nonce.Return failed: %v: %w", err, asynq.SkipRetry)
|
||||
}
|
||||
|
||||
disptachJobPayload, err := json.Marshal(TxPayload{
|
||||
Tx: builtTx,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("json.Marshal failed: %v: %w", err, asynq.SkipRetry)
|
||||
}
|
||||
|
||||
_, err = taskerClient.CreateTask(
|
||||
tasker.TxDispatchTask,
|
||||
tasker.HighPriority,
|
||||
&tasker.Task{
|
||||
Payload: disptachJobPayload,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
gasRefillPayload, err := json.Marshal(SystemPayload{
|
||||
PublicKey: p.From,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("json.Marshal failed: %v: %w", err, asynq.SkipRetry)
|
||||
}
|
||||
|
||||
_, err = taskerClient.CreateTask(
|
||||
tasker.RefillGasTask,
|
||||
tasker.DefaultPriority,
|
||||
&tasker.Task{
|
||||
Payload: gasRefillPayload,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func parseTransferValue(value string, tokenDecimals int) *big.Int {
|
||||
floatValue, _ := strconv.ParseFloat(value, 64)
|
||||
|
||||
return big.NewInt(int64(floatValue * math.Pow10(tokenDecimals)))
|
||||
}
|
@ -1,68 +0,0 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
"math/big"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_parseTransferValue(t *testing.T) {
|
||||
type args struct {
|
||||
value string
|
||||
tokenDecimals int
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want *big.Int
|
||||
}{
|
||||
{
|
||||
name: "zero value string",
|
||||
args: args{
|
||||
value: "0",
|
||||
tokenDecimals: 6,
|
||||
},
|
||||
want: big.NewInt(0),
|
||||
},
|
||||
{
|
||||
name: "fixed value string",
|
||||
args: args{
|
||||
value: "2",
|
||||
tokenDecimals: 6,
|
||||
},
|
||||
want: big.NewInt(2000000),
|
||||
},
|
||||
{
|
||||
name: "float (2 d.p) value string",
|
||||
args: args{
|
||||
value: "2.19",
|
||||
tokenDecimals: 6,
|
||||
},
|
||||
want: big.NewInt(2190000),
|
||||
},
|
||||
{
|
||||
name: "float (6 d.p) value string",
|
||||
args: args{
|
||||
value: "2.123456",
|
||||
tokenDecimals: 6,
|
||||
},
|
||||
want: big.NewInt(2123456),
|
||||
},
|
||||
{
|
||||
name: "float (10 d.p) value string",
|
||||
args: args{
|
||||
value: "2.1234567891",
|
||||
tokenDecimals: 6,
|
||||
},
|
||||
want: big.NewInt(2123456),
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := parseTransferValue(tt.args.value, tt.args.tokenDecimals)
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("parseValue() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
@ -17,6 +17,8 @@ type (
|
||||
|
||||
type SystemContainer struct {
|
||||
Abis map[string]*w3.Func
|
||||
AccountIndexContract common.Address
|
||||
GasFaucetContract common.Address
|
||||
GasRefillThreshold *big.Int
|
||||
GasRefillValue *big.Int
|
||||
GiftableGasValue *big.Int
|
||||
@ -37,12 +39,13 @@ type Task struct {
|
||||
|
||||
const (
|
||||
PrepareAccountTask TaskName = "sys:prepare_account"
|
||||
RegisterAccountOnChain TaskName = "sys:register_account"
|
||||
GiftGasTask TaskName = "sys:gift_gas"
|
||||
GiftTokenTask TaskName = "sys:gift_token"
|
||||
RefillGasTask TaskName = "admin:refill_gas"
|
||||
SweepGasTask TaskName = "admin:sweep_gas"
|
||||
AdminTokenApprovalTask TaskName = "admin:token_approval"
|
||||
TransferTokenTask TaskName = "usr:transfer_token"
|
||||
SignTransferTask TaskName = "usr:sign_transfer"
|
||||
TxDispatchTask TaskName = "rpc:dispatch"
|
||||
)
|
||||
|
||||
|
8
migrations/001_keystore.sql
Normal file
8
migrations/001_keystore.sql
Normal file
@ -0,0 +1,8 @@
|
||||
-- Keystore table
|
||||
CREATE TABLE IF NOT EXISTS keystore (
|
||||
id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
public_key TEXT NOT NULL,
|
||||
private_key TEXT NOT NULL,
|
||||
active BOOLEAN DEFAULT true,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
23
migrations/002_custodial_db.sql
Normal file
23
migrations/002_custodial_db.sql
Normal file
@ -0,0 +1,23 @@
|
||||
-- Origin tx table
|
||||
CREATE TABLE IF NOT EXISTS otx (
|
||||
id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
tracking_id TEXT NOT NULL,
|
||||
"type" TEXT NOT NULL,
|
||||
raw_tx TEXT NOT NULL,
|
||||
tx_hash TEXT NOT NULL,
|
||||
"from" TEXT NOT NULL,
|
||||
"data" TEXT NOT NULL,
|
||||
gas_price bigint NOT NULL,
|
||||
nonce int NOT NULL,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS tx_hash_idx ON otx USING hash(tx_hash);
|
||||
CREATE INDEX IF NOT EXISTS from_idx ON otx USING hash("from");
|
||||
|
||||
-- Dispatch status table
|
||||
CREATE TABLE IF NOT EXISTS dispatch (
|
||||
id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
otx_id INT REFERENCES otx(id),
|
||||
"status" TEXT NOT NULL,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
7
migrations/tern.conf
Normal file
7
migrations/tern.conf
Normal file
@ -0,0 +1,7 @@
|
||||
[database]
|
||||
host = {{env "PG_HOST"}}
|
||||
port = {{env "PG_PORT"}}
|
||||
database = {{env "PG_DB"}}
|
||||
user = {{env "PG_USER"}}
|
||||
password = {{env "PG_PASSWORD"}}
|
||||
sslmode = prefer
|
@ -12,6 +12,7 @@ type Key struct {
|
||||
Private string
|
||||
}
|
||||
|
||||
// Generate creates a new keypair from internally randomized entropy.
|
||||
func Generate() (Key, error) {
|
||||
privateKey, err := crypto.GenerateKey()
|
||||
if err != nil {
|
||||
|
11
pkg/status/status.go
Normal file
11
pkg/status/status.go
Normal file
@ -0,0 +1,11 @@
|
||||
package status
|
||||
|
||||
type Status string
|
||||
|
||||
const (
|
||||
FailGasPrice = "FAIL_LOW_GAS_PRICE"
|
||||
FailInsufficientGas = "FAIL_NO_GAS"
|
||||
FailNonce = "FAIL_LOW_NONCE"
|
||||
Successful = "SUCCESSFUL"
|
||||
Unknown = "UNKNOWN"
|
||||
)
|
47
queries.sql
Normal file
47
queries.sql
Normal file
@ -0,0 +1,47 @@
|
||||
-- Keystore queries
|
||||
|
||||
--name: write-key-pair
|
||||
-- Save hex encoded private key
|
||||
-- $1: public_key
|
||||
-- $2: private_key
|
||||
INSERT INTO keystore(public_key, private_key) VALUES($1, $2) RETURNING id
|
||||
|
||||
--name: load-key-pair
|
||||
-- Load saved key pair
|
||||
-- $1: public_key
|
||||
SELECT private_key FROM keystore WHERE public_key=$1
|
||||
|
||||
-- OTX queries
|
||||
|
||||
--name: create-otx
|
||||
-- Create a new locally originating tx
|
||||
-- $1: tracking_id
|
||||
-- $2: type
|
||||
-- $3: raw_tx
|
||||
-- $4: tx_hash
|
||||
-- $5: from
|
||||
-- $6: data
|
||||
-- $7: gas_price
|
||||
-- $8: nonce
|
||||
INSERT INTO otx(
|
||||
tracking_id,
|
||||
"type",
|
||||
raw_tx,
|
||||
tx_hash,
|
||||
"from",
|
||||
"data",
|
||||
gas_price,
|
||||
nonce
|
||||
) VALUES($1, $2, $3, $4, $5, $6, $7, $8) RETURNING id
|
||||
|
||||
|
||||
-- Dispatch status queries
|
||||
|
||||
--name: create-dispatch-status
|
||||
-- Create a new dispatch status
|
||||
-- $1: otx_id
|
||||
-- $2: status
|
||||
INSERT INTO dispatch(
|
||||
otx_id,
|
||||
"status"
|
||||
) VALUES($1, $2) RETURNING id
|
Loading…
Reference in New Issue
Block a user