-
Notifications
You must be signed in to change notification settings - Fork 0
/
set-withdrawal-creds.go
64 lines (49 loc) · 1.79 KB
/
set-withdrawal-creds.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package validator
import (
"fmt"
"strings"
"github.com/Seb369888/poolsea-go/types"
"github.com/Seb369888/smartnode/shared/types/eth2"
"github.com/ethereum/go-ethereum/common"
eth2types "github.com/wealdtech/go-eth2-types/v2"
)
// Get the withdrawal private key for a validator based on its mnemonic, index, and path
func GetWithdrawalKey(mnemonic string, index uint, validatorKeyPath string) (*eth2types.BLSPrivateKey, error) {
withdrawalKeyPath := strings.TrimSuffix(validatorKeyPath, "/0")
withdrawalKey, err := GetPrivateKey(mnemonic, index, withdrawalKeyPath)
if err != nil {
return nil, fmt.Errorf("error getting withdrawal private key: %w", err)
}
return withdrawalKey, nil
}
// Get a voluntary exit message signature for a given validator key and index
func GetSignedWithdrawalCredsChangeMessage(withdrawalKey *eth2types.BLSPrivateKey, validatorIndex uint64, newWithdrawalAddress common.Address, signatureDomain []byte) (types.ValidatorSignature, error) {
// Get the withdrawal pubkey
withdrawalPubkey := withdrawalKey.PublicKey().Marshal()
withdrawalPubkeyBuffer := [48]byte{}
copy(withdrawalPubkeyBuffer[:], withdrawalPubkey)
// Build withdrawal creds change message
message := eth2.WithdrawalCredentialsChange{
ValidatorIndex: validatorIndex,
FromBLSPubkey: withdrawalPubkeyBuffer,
ToExecutionAddress: newWithdrawalAddress,
}
// Get object root
or, err := message.HashTreeRoot()
if err != nil {
return types.ValidatorSignature{}, err
}
// Get signing root
sr := eth2.SigningRoot{
ObjectRoot: or[:],
Domain: signatureDomain,
}
srHash, err := sr.HashTreeRoot()
if err != nil {
return types.ValidatorSignature{}, err
}
// Sign message
signature := withdrawalKey.Sign(srHash[:]).Marshal()
// Return
return types.BytesToValidatorSignature(signature), nil
}