|
| 1 | +package getpubkeysfrommnemonic |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/ethpandaops/assertoor/pkg/coordinator/types" |
| 11 | + "github.com/sirupsen/logrus" |
| 12 | + "github.com/tyler-smith/go-bip39" |
| 13 | + util "github.com/wealdtech/go-eth2-util" |
| 14 | +) |
| 15 | + |
| 16 | +var ( |
| 17 | + TaskName = "get_pubkeys_from_mnemonic" |
| 18 | + TaskDescriptor = &types.TaskDescriptor{ |
| 19 | + Name: TaskName, |
| 20 | + Description: "Get public keys from mnemonic", |
| 21 | + Config: DefaultConfig(), |
| 22 | + NewTask: NewTask, |
| 23 | + } |
| 24 | +) |
| 25 | + |
| 26 | +type Task struct { |
| 27 | + ctx *types.TaskContext |
| 28 | + options *types.TaskOptions |
| 29 | + config Config |
| 30 | + logger logrus.FieldLogger |
| 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) Config() interface{} { |
| 42 | + return t.config |
| 43 | +} |
| 44 | + |
| 45 | +func (t *Task) Timeout() time.Duration { |
| 46 | + return t.options.Timeout.Duration |
| 47 | +} |
| 48 | + |
| 49 | +func (t *Task) LoadConfig() error { |
| 50 | + config := DefaultConfig() |
| 51 | + |
| 52 | + // parse static config |
| 53 | + if t.options.Config != nil { |
| 54 | + if err := t.options.Config.Unmarshal(&config); err != nil { |
| 55 | + return fmt.Errorf("error parsing task config for %v: %w", TaskName, err) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + // load dynamic vars |
| 60 | + err := t.ctx.Vars.ConsumeVars(&config, t.options.ConfigVars) |
| 61 | + if err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + |
| 65 | + // validate config |
| 66 | + if err := config.Validate(); err != nil { |
| 67 | + return err |
| 68 | + } |
| 69 | + |
| 70 | + t.config = config |
| 71 | + |
| 72 | + return nil |
| 73 | +} |
| 74 | + |
| 75 | +func (t *Task) Execute(_ context.Context) error { |
| 76 | + mnemonic := strings.TrimSpace(t.config.Mnemonic) |
| 77 | + if !bip39.IsMnemonicValid(mnemonic) { |
| 78 | + return errors.New("mnemonic is not valid") |
| 79 | + } |
| 80 | + |
| 81 | + seed := bip39.NewSeed(mnemonic, "") |
| 82 | + pubkeys := make([]string, 0, t.config.Count) |
| 83 | + |
| 84 | + for index := t.config.StartIndex; index < t.config.StartIndex+t.config.Count; index++ { |
| 85 | + validatorKeyPath := fmt.Sprintf("m/12381/3600/%d/0/0", index) |
| 86 | + |
| 87 | + validatorPriv, err := util.PrivateKeyFromSeedAndPath(seed, validatorKeyPath) |
| 88 | + if err != nil { |
| 89 | + return fmt.Errorf("failed generating validator key %v: %w", validatorKeyPath, err) |
| 90 | + } |
| 91 | + |
| 92 | + validatorPubkey := validatorPriv.PublicKey().Marshal() |
| 93 | + pubkey := fmt.Sprintf("0x%x", validatorPubkey) |
| 94 | + |
| 95 | + t.logger.Infof("Generated pubkey %v: %v", index, pubkey) |
| 96 | + |
| 97 | + pubkeys = append(pubkeys, pubkey) |
| 98 | + } |
| 99 | + |
| 100 | + t.ctx.Outputs.SetVar("pubkeys", pubkeys) |
| 101 | + |
| 102 | + return nil |
| 103 | +} |
0 commit comments