forked from ethereum-optimism/optimism
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecrets.go
185 lines (165 loc) · 4.99 KB
/
secrets.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package e2eutils
import (
"crypto/ecdsa"
"fmt"
hdwallet "github.com/ethereum-optimism/go-ethereum-hdwallet"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
)
// DefaultMnemonicConfig is the default mnemonic used in testing.
// We prefer a mnemonic rather than direct private keys to make it easier
// to export all testing keys in external tooling for use during debugging.
var DefaultMnemonicConfig = &MnemonicConfig{
Mnemonic: "test test test test test test test test test test test junk",
Deployer: "m/44'/60'/0'/0/1",
CliqueSigner: "m/44'/60'/0'/0/2",
Proposer: "m/44'/60'/0'/0/3",
Batcher: "m/44'/60'/0'/0/4",
SequencerP2P: "m/44'/60'/0'/0/5",
Alice: "m/44'/60'/0'/0/6",
Bob: "m/44'/60'/0'/0/7",
Mallory: "m/44'/60'/0'/0/8",
SysCfgOwner: "m/44'/60'/0'/0/9",
}
// MnemonicConfig configures the private keys for the hive testnet.
// It's json-serializable, so we can ship it to e.g. the hardhat script client.
type MnemonicConfig struct {
Mnemonic string
Deployer string
CliqueSigner string
SysCfgOwner string
// rollup actors
Proposer string
Batcher string
SequencerP2P string
// prefunded L1/L2 accounts for testing
Alice string
Bob string
Mallory string
}
// Secrets computes the private keys for all mnemonic paths,
// which can then be kept around for fast precomputed private key access.
func (m *MnemonicConfig) Secrets() (*Secrets, error) {
wallet, err := hdwallet.NewFromMnemonic(m.Mnemonic)
if err != nil {
return nil, fmt.Errorf("failed to create wallet: %w", err)
}
account := func(path string) accounts.Account {
return accounts.Account{URL: accounts.URL{Path: path}}
}
deployer, err := wallet.PrivateKey(account(m.Deployer))
if err != nil {
return nil, err
}
cliqueSigner, err := wallet.PrivateKey(account(m.CliqueSigner))
if err != nil {
return nil, err
}
sysCfgOwner, err := wallet.PrivateKey(account(m.SysCfgOwner))
if err != nil {
return nil, err
}
proposer, err := wallet.PrivateKey(account(m.Proposer))
if err != nil {
return nil, err
}
batcher, err := wallet.PrivateKey(account(m.Batcher))
if err != nil {
return nil, err
}
sequencerP2P, err := wallet.PrivateKey(account(m.SequencerP2P))
if err != nil {
return nil, err
}
alice, err := wallet.PrivateKey(account(m.Alice))
if err != nil {
return nil, err
}
bob, err := wallet.PrivateKey(account(m.Bob))
if err != nil {
return nil, err
}
mallory, err := wallet.PrivateKey(account(m.Mallory))
if err != nil {
return nil, err
}
return &Secrets{
Deployer: deployer,
CliqueSigner: cliqueSigner,
SysCfgOwner: sysCfgOwner,
Proposer: proposer,
Batcher: batcher,
SequencerP2P: sequencerP2P,
Alice: alice,
Bob: bob,
Mallory: mallory,
Wallet: wallet,
}, nil
}
// Secrets bundles secp256k1 private keys for all common rollup actors for testing purposes.
type Secrets struct {
Deployer *ecdsa.PrivateKey
CliqueSigner *ecdsa.PrivateKey
SysCfgOwner *ecdsa.PrivateKey
// rollup actors
Proposer *ecdsa.PrivateKey
Batcher *ecdsa.PrivateKey
SequencerP2P *ecdsa.PrivateKey
// prefunded L1/L2 accounts for testing
Alice *ecdsa.PrivateKey
Bob *ecdsa.PrivateKey
Mallory *ecdsa.PrivateKey
// Share the wallet to be able to generate more accounts
Wallet *hdwallet.Wallet
}
// EncodePrivKey encodes the given private key in 32 bytes
func EncodePrivKey(priv *ecdsa.PrivateKey) hexutil.Bytes {
privkey := make([]byte, 32)
blob := priv.D.Bytes()
copy(privkey[32-len(blob):], blob)
return privkey
}
// Addresses computes the ethereum address of each account,
// which can then be kept around for fast precomputed address access.
func (s *Secrets) Addresses() *Addresses {
return &Addresses{
Deployer: crypto.PubkeyToAddress(s.Deployer.PublicKey),
CliqueSigner: crypto.PubkeyToAddress(s.CliqueSigner.PublicKey),
SysCfgOwner: crypto.PubkeyToAddress(s.SysCfgOwner.PublicKey),
Proposer: crypto.PubkeyToAddress(s.Proposer.PublicKey),
Batcher: crypto.PubkeyToAddress(s.Batcher.PublicKey),
SequencerP2P: crypto.PubkeyToAddress(s.SequencerP2P.PublicKey),
Alice: crypto.PubkeyToAddress(s.Alice.PublicKey),
Bob: crypto.PubkeyToAddress(s.Bob.PublicKey),
Mallory: crypto.PubkeyToAddress(s.Mallory.PublicKey),
}
}
// Addresses bundles the addresses for all common rollup addresses for testing purposes.
type Addresses struct {
Deployer common.Address
CliqueSigner common.Address
SysCfgOwner common.Address
// rollup actors
Proposer common.Address
Batcher common.Address
SequencerP2P common.Address
// prefunded L1/L2 accounts for testing
Alice common.Address
Bob common.Address
Mallory common.Address
}
func (a *Addresses) All() []common.Address {
return []common.Address{
a.Deployer,
a.CliqueSigner,
a.SysCfgOwner,
a.Proposer,
a.Batcher,
a.SequencerP2P,
a.Alice,
a.Bob,
a.Mallory,
}
}