/
util.go
390 lines (359 loc) · 9.49 KB
/
util.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package addrutil
import (
"bytes"
"crypto/ecdsa"
"encoding/base64"
"encoding/hex"
"fmt"
"github.com/btcsuite/btcd/btcec"
gcrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/p2p/enr"
"github.com/ethereum/go-ethereum/rlp"
"github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/peer"
ma "github.com/multiformats/go-multiaddr"
"github.com/protolambda/zrnt/eth2/beacon/common"
"github.com/protolambda/ztyp/codec"
"net"
"strings"
)
type Eth2ENREntry []byte
func NewEth2DataEntry(dat *common.Eth2Data) Eth2ENREntry {
var buf bytes.Buffer
if err := dat.Serialize(codec.NewEncodingWriter(&buf)); err != nil {
return nil
}
return buf.Bytes()
}
func (eee Eth2ENREntry) ENRKey() string {
return "eth2"
}
func (eee Eth2ENREntry) Eth2Data() (*common.Eth2Data, error) {
var dat common.Eth2Data
if err := dat.Deserialize(codec.NewDecodingReader(bytes.NewReader(eee), uint64(len(eee)))); err != nil {
return nil, err
}
return &dat, nil
}
func (eee Eth2ENREntry) String() string {
dat, err := eee.Eth2Data()
if err != nil {
return fmt.Sprintf("invalid eth2 data! Raw: %x", eee[:])
}
return fmt.Sprintf("digest: %s, next fork version: %s, next fork epoch: %d",
dat.ForkDigest, dat.NextForkVersion, dat.NextForkEpoch)
}
type AttnetsENREntry []byte
func NewAttnetsENREntry(dat *common.AttnetBits) AttnetsENREntry {
var buf bytes.Buffer
if err := dat.Serialize(codec.NewEncodingWriter(&buf)); err != nil {
return nil
}
return buf.Bytes()
}
func (aee AttnetsENREntry) ENRKey() string {
return "attnets"
}
func (aee AttnetsENREntry) AttnetBits() (common.AttnetBits, error) {
var dat common.AttnetBits
if err := dat.Deserialize(codec.NewDecodingReader(bytes.NewReader(aee), uint64(len(aee)))); err != nil {
return common.AttnetBits{}, err
}
return dat, nil
}
func (aee AttnetsENREntry) String() string {
return hex.EncodeToString(aee)
}
var EnrEntries = map[string]func() (enr.Entry, func() string){
"secp256k1": func() (enr.Entry, func() string) {
res := new(enode.Secp256k1)
return res, func() string {
var out [64]byte
copy(out[:32], res.X.Bytes())
copy(out[32:], res.Y.Bytes())
return hex.EncodeToString(out[:])
}
},
"tcp": func() (enr.Entry, func() string) {
res := new(enr.TCP)
return res, func() string {
return fmt.Sprintf("%d", *res)
}
},
"tcp6": func() (enr.Entry, func() string) {
res := new(enr.TCP6)
return res, func() string {
return fmt.Sprintf("%d", *res)
}
},
"udp": func() (enr.Entry, func() string) {
res := new(enr.UDP)
return res, func() string {
return fmt.Sprintf("%d", *res)
}
},
"udp6": func() (enr.Entry, func() string) {
res := new(enr.UDP6)
return res, func() string {
return fmt.Sprintf("%d", *res)
}
},
"id": func() (enr.Entry, func() string) {
res := new(enr.ID)
return res, func() string {
return string(*res)
}
},
"ip": func() (enr.Entry, func() string) {
res := new(enr.IP)
return res, func() string {
return ((*net.IP)(res)).String()
}
},
"ip6": func() (enr.Entry, func() string) {
res := new(enr.IPv6)
return res, func() string {
return ((*net.IP)(res)).String()
}
},
"eth2": func() (enr.Entry, func() string) {
res := new(Eth2ENREntry)
return res, func() string {
return res.String()
}
},
"attnets": func() (enr.Entry, func() string) {
res := new(AttnetsENREntry)
return res, func() string {
return res.String()
}
},
}
func ParseEnrBytes(v string) ([]byte, error) {
if strings.HasPrefix(v, "enr:") {
v = v[4:]
if strings.HasPrefix(v, "//") {
v = v[2:]
}
}
return base64.RawURLEncoding.DecodeString(v)
}
func ParseEnr(v string) (*enr.Record, error) {
data, err := ParseEnrBytes(v)
if err != nil {
return nil, err
}
var record enr.Record
if err := rlp.Decode(bytes.NewReader(data), &record); err != nil {
return nil, err
}
return &record, nil
}
func ParseEnode(v string) (*enode.Node, error) {
addr := new(enode.Node)
err := addr.UnmarshalText([]byte(v))
if err != nil {
return nil, err
}
return addr, nil
}
func ParseEnrOrEnode(v string) (*enode.Node, error) {
if strings.HasPrefix(v, "enode://") {
return ParseEnode(v)
} else {
enrAddr, err := ParseEnr(v)
if err != nil {
return nil, err
}
enodeAddr, err := EnrToEnode(enrAddr, true)
if err != nil {
return nil, err
}
return enodeAddr, nil
}
}
func ParseNodeID(v string) (enode.ID, error) {
if h, err := hex.DecodeString(v); err != nil {
return enode.ID{}, fmt.Errorf("provided target node is not a valid node ID: %s", v)
} else {
if len(h) != 32 {
return enode.ID{}, fmt.Errorf("hex node ID is not 32 bytes: %s", v)
} else {
var out enode.ID
copy(out[:], h)
return out, nil
}
}
}
func ParseNodeIDOrEnrOrEnode(v string) (enode.ID, error) {
if h, err := hex.DecodeString(v); err != nil {
en, err := ParseEnrOrEnode(v)
if err != nil {
return enode.ID{}, fmt.Errorf("provided target node is not a valid node ID, enode address or ENR: %s", v)
}
return en.ID(), nil
} else {
if len(h) != 32 {
return enode.ID{}, fmt.Errorf("hex node ID is not 32 bytes: %s", v)
} else {
var out enode.ID
copy(out[:], h)
return out, nil
}
}
}
func ParsePrivateKey(v string) (*crypto.Secp256k1PrivateKey, error) {
if strings.HasPrefix(v, "0x") {
v = v[2:]
}
privKeyBytes, err := hex.DecodeString(v)
if err != nil {
return nil, fmt.Errorf("cannot parse private key, expected hex string: %v", err)
}
var priv crypto.PrivKey
priv, err = crypto.UnmarshalSecp256k1PrivateKey(privKeyBytes)
if err != nil {
return nil, fmt.Errorf("cannot parse private key, invalid private key (Secp256k1): %v", err)
}
key := (priv).(*crypto.Secp256k1PrivateKey)
key.Curve = gcrypto.S256() // Temporary hack, so libp2p Secp256k1 is recognized as geth Secp256k1 in disc v5.1
if !key.Curve.IsOnCurve(key.X, key.Y) { // TODO: should we be checking this?
return nil, fmt.Errorf("invalid private key, not on curve")
}
return key, nil
}
func PrivKeysEqual(a *crypto.Secp256k1PrivateKey, b *crypto.Secp256k1PrivateKey) (ok bool, err error) {
if a == nil {
if b == nil {
return true, nil
} else {
return false, nil
}
}
aRaw, err := a.Raw()
if err != nil {
return false, err
}
bRaw, err := b.Raw()
if err != nil {
return false, err
}
return bytes.Equal(aRaw, bRaw), nil
}
func ParsePubkey(v string) (*ecdsa.PublicKey, error) {
if strings.HasPrefix(v, "0x") {
v = v[2:]
}
pubKeyBytes, err := hex.DecodeString(v)
if err != nil {
return nil, fmt.Errorf("cannot parse public key, expected hex string: %v", err)
}
var pub crypto.PubKey
pub, err = crypto.UnmarshalSecp256k1PublicKey(pubKeyBytes)
if err != nil {
return nil, fmt.Errorf("cannot parse public key, invalid public key (Secp256k1): %v", err)
}
return (*ecdsa.PublicKey)((pub).(*crypto.Secp256k1PublicKey)), nil
}
func EnrToEnode(record *enr.Record, verifySig bool) (*enode.Node, error) {
idSchemeName := record.IdentityScheme()
if verifySig {
if err := record.VerifySignature(enode.ValidSchemes[idSchemeName]); err != nil {
return nil, err
}
}
return enode.New(enode.ValidSchemes[idSchemeName], record)
}
func EnrToString(record *enr.Record) (string, error) {
enc, err := rlp.EncodeToBytes(record)
if err != nil {
return "", err
}
b64 := base64.RawURLEncoding.EncodeToString(enc)
return "enr:" + b64, nil
}
func EnodesToMultiAddrs(nodes []*enode.Node) ([]ma.Multiaddr, error) {
var out []ma.Multiaddr
for _, n := range nodes {
if n.IP() == nil {
continue
}
multiAddr, err := EnodeToMultiAddr(n)
if err != nil {
return nil, err
}
out = append(out, multiAddr)
}
return out, nil
}
func PeerIDFromPubkey(pubkey *ecdsa.PublicKey) peer.ID {
// save for this kind of pubkey
// TODO: struct init, instead of type cast
id, _ := peer.IDFromPublicKey(crypto.PubKey((*crypto.Secp256k1PublicKey)((*btcec.PublicKey)(pubkey))))
return id
}
func EnodeToMultiAddr(node *enode.Node) (ma.Multiaddr, error) {
ipScheme := "ip4"
if len(node.IP()) == net.IPv6len {
ipScheme = "ip6"
}
pubkey := node.Pubkey()
peerID := PeerIDFromPubkey(pubkey)
multiAddrStr := fmt.Sprintf("/%s/%s/tcp/%d/p2p/%s", ipScheme, node.IP().String(), node.TCP(), peerID)
multiAddr, err := ma.NewMultiaddr(multiAddrStr)
if err != nil {
return nil, err
}
return multiAddr, nil
}
// Create an ENR. All arguments are optional.
func MakeENR(ip net.IP, tcpPort uint16, udpPort uint16, priv *crypto.Secp256k1PrivateKey) *enr.Record {
var rec enr.Record
if ip != nil {
if len(ip) == net.IPv4len {
rec.Set(enr.IDv4)
} else {
rec.Set(enr.ID("v6"))
}
}
if ip != nil {
rec.Set(enr.IP(ip))
}
if priv != nil {
pub := (*ecdsa.PrivateKey)(priv).Public().(*ecdsa.PublicKey)
rec.Set(enode.Secp256k1(*pub))
}
if tcpPort != 0 {
rec.Set(enr.TCP(tcpPort))
}
if udpPort != 0 {
rec.Set(enr.UDP(udpPort))
}
if priv != nil {
_ = enode.SignV4(&rec, (*ecdsa.PrivateKey)(priv))
}
return &rec
}
func ParseEnrEth2Data(n *enode.Node) (data *common.Eth2Data, exists bool, err error) {
var eth2 Eth2ENREntry
if err := n.Load(ð2); err != nil {
return nil, false, nil
}
dat, err := eth2.Eth2Data()
if err != nil {
return nil, true, fmt.Errorf("failed parsing eth2 bytes: %v", err)
}
return dat, true, nil
}
func ParseEnrAttnets(n *enode.Node) (attnetbits *common.AttnetBits, exists bool, err error) {
var attnets AttnetsENREntry
if err := n.Load(&attnets); err != nil {
return nil, false, nil
}
dat, err := attnets.AttnetBits()
if err != nil {
return nil, true, fmt.Errorf("failed parsing attnets bytes: %v", err)
}
return &dat, true, nil
}