2023-02-20 10:56:30 +01:00
|
|
|
package custodial
|
|
|
|
|
|
|
|
import (
|
2023-03-29 18:10:58 +02:00
|
|
|
"context"
|
2023-03-06 09:18:41 +01:00
|
|
|
"crypto/ecdsa"
|
2023-07-10 08:32:05 +02:00
|
|
|
"time"
|
2023-03-06 09:18:41 +01:00
|
|
|
|
2023-02-20 10:56:30 +01:00
|
|
|
"github.com/bsm/redislock"
|
2023-03-06 09:18:41 +01:00
|
|
|
"github.com/celo-org/celo-blockchain/common"
|
2023-03-29 18:10:58 +02:00
|
|
|
eth_crypto "github.com/celo-org/celo-blockchain/crypto"
|
2023-02-20 10:56:30 +01:00
|
|
|
"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"
|
2023-04-11 12:14:49 +02:00
|
|
|
"github.com/grassrootseconomics/cic-custodial/pkg/util"
|
2023-03-06 09:18:41 +01:00
|
|
|
"github.com/grassrootseconomics/w3-celo-patch"
|
2023-03-16 13:03:51 +01:00
|
|
|
"github.com/redis/go-redis/v9"
|
2023-05-16 14:20:01 +02:00
|
|
|
"github.com/zerodha/logf"
|
2023-02-20 10:56:30 +01:00
|
|
|
)
|
|
|
|
|
2023-03-06 09:18:41 +01:00
|
|
|
type (
|
2023-03-29 18:10:58 +02:00
|
|
|
Opts struct {
|
2023-07-10 08:32:05 +02:00
|
|
|
ApprovalTimeout time.Duration
|
2023-03-29 18:10:58 +02:00
|
|
|
CeloProvider *celoutils.Provider
|
|
|
|
LockProvider *redislock.Client
|
2023-05-16 14:20:01 +02:00
|
|
|
Logg logf.Logger
|
2023-03-29 18:10:58 +02:00
|
|
|
Noncestore nonce.Noncestore
|
2023-04-11 12:14:49 +02:00
|
|
|
Store store.Store
|
2023-03-29 18:10:58 +02:00
|
|
|
RedisClient *redis.Client
|
|
|
|
RegistryAddress string
|
|
|
|
SystemPrivateKey string
|
|
|
|
SystemPublicKey string
|
|
|
|
TaskerClient *tasker.TaskerClient
|
2023-03-06 09:18:41 +01:00
|
|
|
}
|
2023-03-29 18:10:58 +02:00
|
|
|
|
2023-03-06 09:18:41 +01:00
|
|
|
Custodial struct {
|
2023-07-10 08:32:05 +02:00
|
|
|
ApprovalTimeout time.Duration
|
2023-03-29 18:10:58 +02:00
|
|
|
Abis map[string]*w3.Func
|
|
|
|
CeloProvider *celoutils.Provider
|
|
|
|
LockProvider *redislock.Client
|
2023-06-08 06:08:58 +02:00
|
|
|
Logg logf.Logger
|
2023-03-29 18:10:58 +02:00
|
|
|
Noncestore nonce.Noncestore
|
2023-04-11 12:14:49 +02:00
|
|
|
Store store.Store
|
2023-03-29 18:10:58 +02:00
|
|
|
RedisClient *redis.Client
|
|
|
|
RegistryMap map[string]common.Address
|
|
|
|
SystemPrivateKey *ecdsa.PrivateKey
|
|
|
|
SystemPublicKey string
|
|
|
|
TaskerClient *tasker.TaskerClient
|
2023-03-06 09:18:41 +01:00
|
|
|
}
|
|
|
|
)
|
2023-03-29 18:10:58 +02:00
|
|
|
|
|
|
|
func NewCustodial(o Opts) (*Custodial, error) {
|
2023-04-11 12:14:49 +02:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), util.SLATimeout)
|
2023-03-29 18:10:58 +02:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
registryMap, err := o.CeloProvider.RegistryMap(ctx, celoutils.HexToAddress(o.RegistryAddress))
|
|
|
|
if err != nil {
|
2023-06-08 06:08:58 +02:00
|
|
|
o.Logg.Error("custodial: critical error loading contracts from registry: %v", err)
|
2023-03-29 18:10:58 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-05-16 14:20:01 +02:00
|
|
|
systemNonce, err := o.Noncestore.Peek(ctx, o.SystemPublicKey)
|
2023-04-13 12:38:23 +02:00
|
|
|
if err != nil {
|
2023-03-29 18:10:58 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-05-16 14:20:01 +02:00
|
|
|
o.Logg.Info("custodial: loaded_nonce", "system_nonce", systemNonce)
|
|
|
|
|
2023-03-29 18:10:58 +02:00
|
|
|
privateKey, err := eth_crypto.HexToECDSA(o.SystemPrivateKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Custodial{
|
2023-07-10 08:32:05 +02:00
|
|
|
ApprovalTimeout: o.ApprovalTimeout,
|
2023-03-29 18:10:58 +02:00
|
|
|
Abis: initAbis(),
|
|
|
|
CeloProvider: o.CeloProvider,
|
|
|
|
LockProvider: o.LockProvider,
|
2023-06-08 06:08:58 +02:00
|
|
|
Logg: o.Logg,
|
2023-03-29 18:10:58 +02:00
|
|
|
Noncestore: o.Noncestore,
|
2023-04-11 12:14:49 +02:00
|
|
|
Store: o.Store,
|
2023-03-29 18:10:58 +02:00
|
|
|
RedisClient: o.RedisClient,
|
|
|
|
RegistryMap: registryMap,
|
|
|
|
SystemPrivateKey: privateKey,
|
|
|
|
SystemPublicKey: o.SystemPublicKey,
|
|
|
|
TaskerClient: o.TaskerClient,
|
|
|
|
}, nil
|
|
|
|
|
|
|
|
}
|