|
| 1 | +package generatechildwallet |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "math/rand" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/ethereum/go-ethereum/crypto" |
| 10 | + "github.com/ethpandaops/assertoor/pkg/coordinator/types" |
| 11 | + "github.com/ethpandaops/assertoor/pkg/coordinator/wallet" |
| 12 | + "github.com/sirupsen/logrus" |
| 13 | +) |
| 14 | + |
| 15 | +var ( |
| 16 | + TaskName = "generate_child_wallet" |
| 17 | + TaskDescriptor = &types.TaskDescriptor{ |
| 18 | + Name: TaskName, |
| 19 | + Description: "Generates a funded child wallet.", |
| 20 | + Config: DefaultConfig(), |
| 21 | + NewTask: NewTask, |
| 22 | + } |
| 23 | +) |
| 24 | + |
| 25 | +type Task struct { |
| 26 | + ctx *types.TaskContext |
| 27 | + options *types.TaskOptions |
| 28 | + config Config |
| 29 | + logger logrus.FieldLogger |
| 30 | + wallet *wallet.Wallet |
| 31 | +} |
| 32 | + |
| 33 | +func NewTask(ctx *types.TaskContext, options *types.TaskOptions) (types.Task, error) { |
| 34 | + return &Task{ |
| 35 | + ctx: ctx, |
| 36 | + options: options, |
| 37 | + logger: ctx.Logger.GetLogger(), |
| 38 | + }, nil |
| 39 | +} |
| 40 | + |
| 41 | +func (t *Task) Name() string { |
| 42 | + return TaskDescriptor.Name |
| 43 | +} |
| 44 | + |
| 45 | +func (t *Task) Title() string { |
| 46 | + return t.ctx.Vars.ResolvePlaceholders(t.options.Title) |
| 47 | +} |
| 48 | + |
| 49 | +func (t *Task) Description() string { |
| 50 | + return TaskDescriptor.Description |
| 51 | +} |
| 52 | + |
| 53 | +func (t *Task) Config() interface{} { |
| 54 | + return t.config |
| 55 | +} |
| 56 | + |
| 57 | +func (t *Task) Logger() logrus.FieldLogger { |
| 58 | + return t.logger |
| 59 | +} |
| 60 | + |
| 61 | +func (t *Task) Timeout() time.Duration { |
| 62 | + return t.options.Timeout.Duration |
| 63 | +} |
| 64 | + |
| 65 | +func (t *Task) LoadConfig() error { |
| 66 | + config := DefaultConfig() |
| 67 | + |
| 68 | + // parse static config |
| 69 | + if t.options.Config != nil { |
| 70 | + if err := t.options.Config.Unmarshal(&config); err != nil { |
| 71 | + return fmt.Errorf("error parsing task config for %v: %w", TaskName, err) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // load dynamic vars |
| 76 | + err := t.ctx.Vars.ConsumeVars(&config, t.options.ConfigVars) |
| 77 | + if err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + |
| 81 | + // validate config |
| 82 | + if err2 := config.Validate(); err2 != nil { |
| 83 | + return err2 |
| 84 | + } |
| 85 | + |
| 86 | + // Load root wallet |
| 87 | + privKey, err := crypto.HexToECDSA(config.PrivateKey) |
| 88 | + if err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + |
| 92 | + t.wallet, err = t.ctx.Scheduler.GetCoordinator().WalletManager().GetWalletByPrivkey(privKey) |
| 93 | + if err != nil { |
| 94 | + return fmt.Errorf("cannot initialize wallet: %w", err) |
| 95 | + } |
| 96 | + |
| 97 | + t.config = config |
| 98 | + |
| 99 | + return nil |
| 100 | +} |
| 101 | + |
| 102 | +func (t *Task) Execute(ctx context.Context) error { |
| 103 | + err := t.wallet.AwaitReady(ctx) |
| 104 | + if err != nil { |
| 105 | + return err |
| 106 | + } |
| 107 | + |
| 108 | + t.logger.Infof("root wallet: %v [nonce: %v] %v ETH", t.wallet.GetAddress().Hex(), t.wallet.GetNonce(), t.wallet.GetReadableBalance(18, 0, 4, false, false)) |
| 109 | + |
| 110 | + walletSeed := t.config.WalletSeed |
| 111 | + if t.config.RandomSeed { |
| 112 | + walletSeed = t.randStringBytes(20) |
| 113 | + } |
| 114 | + |
| 115 | + walletPool, err := t.ctx.Scheduler.GetCoordinator().WalletManager().GetWalletPoolByPrivkey(t.wallet.GetPrivateKey(), 1, walletSeed) |
| 116 | + if err != nil { |
| 117 | + return err |
| 118 | + } |
| 119 | + |
| 120 | + err = walletPool.EnsureFunding(ctx, t.config.PrefundMinBalance, t.config.PrefundAmount, t.config.PrefundFeeCap, t.config.PrefundTipCap, 1) |
| 121 | + if err != nil { |
| 122 | + return err |
| 123 | + } |
| 124 | + |
| 125 | + childWallet := walletPool.GetNextChildWallet() |
| 126 | + t.logger.Infof("child wallet: %v [nonce: %v] %v ETH", childWallet.GetAddress().Hex(), childWallet.GetNonce(), childWallet.GetReadableBalance(18, 0, 4, false, false)) |
| 127 | + |
| 128 | + if t.config.WalletAddressResultVar != "" { |
| 129 | + t.ctx.Vars.SetVar(t.config.WalletAddressResultVar, childWallet.GetAddress().Hex()) |
| 130 | + } |
| 131 | + |
| 132 | + if t.config.WalletPrivateKeyResultVar != "" { |
| 133 | + t.ctx.Vars.SetVar(t.config.WalletPrivateKeyResultVar, fmt.Sprintf("%x", crypto.FromECDSA(childWallet.GetPrivateKey()))) |
| 134 | + } |
| 135 | + |
| 136 | + return ctx.Err() |
| 137 | +} |
| 138 | + |
| 139 | +const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 140 | + |
| 141 | +func (t *Task) randStringBytes(n int) string { |
| 142 | + b := make([]byte, n) |
| 143 | + for i := range b { |
| 144 | + //nolint:gosec // ignore |
| 145 | + b[i] = letterBytes[rand.Intn(len(letterBytes))] |
| 146 | + } |
| 147 | + |
| 148 | + return string(b) |
| 149 | +} |
0 commit comments