mirror of
https://github.com/grassrootseconomics/cic-custodial.git
synced 2024-11-29 00:46:46 +01:00
Mohamed Sohail
9e1d62a014
* wip: add transfer auithorization * feat: update transferAuthPayload * switch to MAX_INT for value and use revoke flag * tranfer handler: fix nonce rollback on EOA account * feat: switch to session based session transfer auth * Demurrage contracts require setting the approval value to 0 before updating the value after the initial limit is set * This implementation auto-revokes every 15 min after arequest is created. Subsequent requests will fail untill the value is set back to 0 * feat: settable approve session timeout
90 lines
2.4 KiB
Go
90 lines
2.4 KiB
Go
package custodial
|
|
|
|
import (
|
|
"context"
|
|
"crypto/ecdsa"
|
|
"time"
|
|
|
|
"github.com/bsm/redislock"
|
|
"github.com/celo-org/celo-blockchain/common"
|
|
eth_crypto "github.com/celo-org/celo-blockchain/crypto"
|
|
"github.com/grassrootseconomics/celoutils"
|
|
"github.com/grassrootseconomics/cic-custodial/internal/nonce"
|
|
"github.com/grassrootseconomics/cic-custodial/internal/store"
|
|
"github.com/grassrootseconomics/cic-custodial/internal/tasker"
|
|
"github.com/grassrootseconomics/cic-custodial/pkg/util"
|
|
"github.com/grassrootseconomics/w3-celo-patch"
|
|
"github.com/redis/go-redis/v9"
|
|
"github.com/zerodha/logf"
|
|
)
|
|
|
|
type (
|
|
Opts struct {
|
|
ApprovalTimeout time.Duration
|
|
CeloProvider *celoutils.Provider
|
|
LockProvider *redislock.Client
|
|
Logg logf.Logger
|
|
Noncestore nonce.Noncestore
|
|
Store store.Store
|
|
RedisClient *redis.Client
|
|
RegistryAddress string
|
|
SystemPrivateKey string
|
|
SystemPublicKey string
|
|
TaskerClient *tasker.TaskerClient
|
|
}
|
|
|
|
Custodial struct {
|
|
ApprovalTimeout time.Duration
|
|
Abis map[string]*w3.Func
|
|
CeloProvider *celoutils.Provider
|
|
LockProvider *redislock.Client
|
|
Logg logf.Logger
|
|
Noncestore nonce.Noncestore
|
|
Store store.Store
|
|
RedisClient *redis.Client
|
|
RegistryMap map[string]common.Address
|
|
SystemPrivateKey *ecdsa.PrivateKey
|
|
SystemPublicKey string
|
|
TaskerClient *tasker.TaskerClient
|
|
}
|
|
)
|
|
|
|
func NewCustodial(o Opts) (*Custodial, error) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), util.SLATimeout)
|
|
defer cancel()
|
|
|
|
registryMap, err := o.CeloProvider.RegistryMap(ctx, celoutils.HexToAddress(o.RegistryAddress))
|
|
if err != nil {
|
|
o.Logg.Error("custodial: critical error loading contracts from registry: %v", err)
|
|
return nil, err
|
|
}
|
|
|
|
systemNonce, err := o.Noncestore.Peek(ctx, o.SystemPublicKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
o.Logg.Info("custodial: loaded_nonce", "system_nonce", systemNonce)
|
|
|
|
privateKey, err := eth_crypto.HexToECDSA(o.SystemPrivateKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &Custodial{
|
|
ApprovalTimeout: o.ApprovalTimeout,
|
|
Abis: initAbis(),
|
|
CeloProvider: o.CeloProvider,
|
|
LockProvider: o.LockProvider,
|
|
Logg: o.Logg,
|
|
Noncestore: o.Noncestore,
|
|
Store: o.Store,
|
|
RedisClient: o.RedisClient,
|
|
RegistryMap: registryMap,
|
|
SystemPrivateKey: privateKey,
|
|
SystemPublicKey: o.SystemPublicKey,
|
|
TaskerClient: o.TaskerClient,
|
|
}, nil
|
|
|
|
}
|