-
Notifications
You must be signed in to change notification settings - Fork 338
/
address.go
165 lines (137 loc) · 4.49 KB
/
address.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
// Copyright 2020 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package bzz exposes the data structure and operations
// necessary on the bzz.Address type which used in the handshake
// protocol, address-book and hive protocol.
package bzz
import (
"bytes"
"encoding/base64"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/ethersphere/bee/pkg/crypto"
"github.com/ethersphere/bee/pkg/swarm"
ma "github.com/multiformats/go-multiaddr"
)
var ErrInvalidAddress = errors.New("invalid address")
// Address represents the bzz address in swarm.
// It consists of a peers underlay (physical) address, overlay (topology) address and signature.
// Signature is used to verify the `Overlay/Underlay` pair, as it is based on `underlay|networkID`, signed with the public key of Overlay address
type Address struct {
Underlay ma.Multiaddr
Overlay swarm.Address
Signature []byte
Transaction []byte
EthereumAddress []byte
}
type addressJSON struct {
Overlay string `json:"overlay"`
Underlay string `json:"underlay"`
Signature string `json:"signature"`
Transaction string `json:"transaction"`
}
func NewAddress(signer crypto.Signer, underlay ma.Multiaddr, overlay swarm.Address, networkID uint64, trx []byte) (*Address, error) {
underlayBinary, err := underlay.MarshalBinary()
if err != nil {
return nil, err
}
signature, err := signer.Sign(generateSignData(underlayBinary, overlay.Bytes(), networkID))
if err != nil {
return nil, err
}
return &Address{
Underlay: underlay,
Overlay: overlay,
Signature: signature,
Transaction: trx,
}, nil
}
func ParseAddress(underlay, overlay, signature, nonce []byte, validateOverlay bool, networkID uint64) (*Address, error) {
recoveredPK, err := crypto.Recover(signature, generateSignData(underlay, overlay, networkID))
if err != nil {
return nil, ErrInvalidAddress
}
if validateOverlay {
recoveredOverlay, err := crypto.NewOverlayAddress(*recoveredPK, networkID, nonce)
if err != nil {
return nil, ErrInvalidAddress
}
if !bytes.Equal(recoveredOverlay.Bytes(), overlay) {
return nil, ErrInvalidAddress
}
}
multiUnderlay, err := ma.NewMultiaddrBytes(underlay)
if err != nil {
return nil, ErrInvalidAddress
}
ethAddress, err := crypto.NewEthereumAddress(*recoveredPK)
if err != nil {
return nil, fmt.Errorf("extract ethereum address: %w: %w", err, ErrInvalidAddress)
}
return &Address{
Underlay: multiUnderlay,
Overlay: swarm.NewAddress(overlay),
Signature: signature,
Transaction: nonce,
EthereumAddress: ethAddress,
}, nil
}
func generateSignData(underlay, overlay []byte, networkID uint64) []byte {
networkIDBytes := make([]byte, 8)
binary.BigEndian.PutUint64(networkIDBytes, networkID)
signData := append([]byte("bee-handshake-"), underlay...)
signData = append(signData, overlay...)
return append(signData, networkIDBytes...)
}
func (a *Address) Equal(b *Address) bool {
if a == nil || b == nil {
return a == b
}
return a.Overlay.Equal(b.Overlay) && multiaddrEqual(a.Underlay, b.Underlay) && bytes.Equal(a.Signature, b.Signature) && bytes.Equal(a.Transaction, b.Transaction)
}
func multiaddrEqual(a, b ma.Multiaddr) bool {
if a == nil || b == nil {
return a == b
}
return a.Equal(b)
}
func (a *Address) MarshalJSON() ([]byte, error) {
return json.Marshal(&addressJSON{
Overlay: a.Overlay.String(),
Underlay: a.Underlay.String(),
Signature: base64.StdEncoding.EncodeToString(a.Signature),
Transaction: common.Bytes2Hex(a.Transaction),
})
}
func (a *Address) UnmarshalJSON(b []byte) error {
v := &addressJSON{}
err := json.Unmarshal(b, v)
if err != nil {
return err
}
addr, err := swarm.ParseHexAddress(v.Overlay)
if err != nil {
return err
}
a.Overlay = addr
m, err := ma.NewMultiaddr(v.Underlay)
if err != nil {
return err
}
a.Underlay = m
a.Signature, err = base64.StdEncoding.DecodeString(v.Signature)
a.Transaction = common.Hex2Bytes(v.Transaction)
return err
}
func (a *Address) String() string {
return fmt.Sprintf("[Underlay: %v, Overlay %v, Signature %x, Transaction %x]", a.Underlay, a.Overlay, a.Signature, a.Transaction)
}
// ShortString returns shortened versions of bzz address in a format: [Overlay, Underlay]
// It can be used for logging
func (a *Address) ShortString() string {
return fmt.Sprintf("[Overlay: %s, Underlay: %s]", a.Overlay.String(), a.Underlay.String())
}