This repository has been archived by the owner on Aug 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 110
/
network.go
203 lines (176 loc) · 5.32 KB
/
network.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
package network
import (
"crypto/ecdsa"
"encoding/hex"
"fmt"
"io"
"net"
"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/ethersphere/swarm/network/capability"
)
// BzzAddr implements the PeerAddr interface
type BzzAddr struct {
OAddr []byte
UAddr []byte
Capabilities *capability.Capabilities
}
// EncodeRLP implements rlp.Encoder
func (b *BzzAddr) EncodeRLP(w io.Writer) error {
err := rlp.Encode(w, b.OAddr)
if err != nil {
return err
}
err = rlp.Encode(w, b.UAddr)
if err != nil {
return err
}
y, err := rlp.EncodeToBytes(b.Capabilities)
if err != nil {
return err
}
err = rlp.Encode(w, y)
if err != nil {
return err
}
return nil
}
// DecodeRLP implements rlp.Decoder
func (b *BzzAddr) DecodeRLP(s *rlp.Stream) error {
var err error
b.OAddr, err = s.Bytes()
if err != nil {
return fmt.Errorf("oaddr --- %v", err)
}
b.UAddr, err = s.Bytes()
if err != nil {
return fmt.Errorf("uaddr --- %v", err)
}
y, err := s.Bytes()
if err != nil {
return fmt.Errorf("capsbytes --- %v", err)
}
err = rlp.DecodeBytes(y, &b.Capabilities)
if err != nil {
return fmt.Errorf("caps --- %v", err)
}
return nil
}
// NewBzzAddr creates a new BzzAddr with the specified byte values for over- and underlayaddresses
// It will contain an empty capabilities object
func NewBzzAddr(oaddr []byte, uaddr []byte) *BzzAddr {
return &BzzAddr{
OAddr: oaddr,
UAddr: uaddr,
Capabilities: capability.NewCapabilities(),
}
}
// Address implements OverlayPeer interface to be used in Overlay.
func (a *BzzAddr) Address() []byte {
return a.OAddr
}
// Over returns the overlay address.
func (a *BzzAddr) Over() []byte {
return a.OAddr
}
// Under returns the underlay address.
func (a *BzzAddr) Under() []byte {
return a.UAddr
}
// ShortString returns shortened versions of overlay and underlay address in a format: shortOver:shortUnder
// It can be used for logging
func (a *BzzAddr) ShortString() string {
return fmt.Sprintf("%s:%s", a.ShortOver(), a.ShortUnder())
}
// ShortOver returns shortened version of Overlay address
// It can be used for logging
func (a *BzzAddr) ShortOver() string {
if oaddr := hex.EncodeToString(a.OAddr); len(oaddr) >= 16 {
return oaddr[:16]
} else {
return oaddr
}
}
// ShortUnder returns shortened version of Underlay address
// It can be used for logging
func (a *BzzAddr) ShortUnder() string {
if uaddr := hex.EncodeToString(a.UAddr); len(uaddr) >= 16 {
return uaddr[:16]
} else {
return uaddr
}
}
// ID returns the node identifier in the underlay.
func (a *BzzAddr) ID() enode.ID {
n, err := enode.ParseV4(string(a.UAddr))
if err != nil {
return enode.ID{}
}
return n.ID()
}
// Update updates the underlay address of a peer record
func (a *BzzAddr) Update(na *BzzAddr) *BzzAddr {
return &BzzAddr{a.OAddr, na.UAddr, a.Capabilities}
}
// String pretty prints the address
func (a *BzzAddr) String() string {
return fmt.Sprintf("%x <%s> cap:%s", a.OAddr, a.UAddr, a.Capabilities)
}
// RandomBzzAddr is a utility method generating a private key and corresponding enode id
// It in turn calls NewBzzAddrFromEnode to generate a corresponding overlay address from enode
func RandomBzzAddr() *BzzAddr {
key, err := crypto.GenerateKey()
if err != nil {
panic("unable to generate key")
}
node := enode.NewV4(&key.PublicKey, net.IP{127, 0, 0, 1}, 30303, 30303)
return NewBzzAddrFromEnode(node)
}
// NewBzzAddrFromEnode creates a BzzAddr where the overlay address is the byte representation of the enode i
// It is only used for test purposes
// TODO: This method should be replaced by (optionally deterministic) generation of addresses using NewEnode and PrivateKeyToBzzKey
func NewBzzAddrFromEnode(enod *enode.Node) *BzzAddr {
return &BzzAddr{OAddr: enod.ID().Bytes(), UAddr: []byte(enod.URLv4()), Capabilities: capability.NewCapabilities()}
}
// WithCapabilities is a chained constructor method to set the capabilities array for a BzzAddr
func (b *BzzAddr) WithCapabilities(c *capability.Capabilities) *BzzAddr {
b.Capabilities = c
return b
}
// PrivateKeyToBzzKey create a swarm overlay address from the given private key
func PrivateKeyToBzzKey(prvKey *ecdsa.PrivateKey) []byte {
pubkeyBytes := crypto.FromECDSAPub(&prvKey.PublicKey)
return crypto.Keccak256Hash(pubkeyBytes).Bytes()
}
// EnodeParams contains the parameters used to create new Enode Records
type EnodeParams struct {
PrivateKey *ecdsa.PrivateKey
EnodeKey *ecdsa.PrivateKey
Lightnode bool
Bootnode bool
}
// NewEnodeRecord creates a new valid swarm node ENR record from the given parameters
func NewEnodeRecord(params *EnodeParams) (*enr.Record, error) {
if params.PrivateKey == nil {
return nil, fmt.Errorf("all param private keys must be defined")
}
bzzkeybytes := PrivateKeyToBzzKey(params.PrivateKey)
var record enr.Record
record.Set(NewENRAddrEntry(bzzkeybytes))
record.Set(ENRBootNodeEntry(params.Bootnode))
return &record, nil
}
// NewEnode creates a new enode object for the given parameters
func NewEnode(params *EnodeParams) (*enode.Node, error) {
record, err := NewEnodeRecord(params)
if err != nil {
return nil, err
}
err = enode.SignV4(record, params.EnodeKey)
if err != nil {
return nil, fmt.Errorf("ENR create fail: %v", err)
}
return enode.New(enode.V4ID{}, record)
}