-
Notifications
You must be signed in to change notification settings - Fork 9
/
address.go
159 lines (132 loc) · 4.16 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
// Package aurora exposes the data structure and operations
// necessary on the aurora.Address type which used in the handshake
// protocol, address-book and hive protocol.
package aurora
import (
"bytes"
"encoding/base64"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"github.com/gauss-project/aurorafs/pkg/boson"
"github.com/gauss-project/aurorafs/pkg/crypto"
ma "github.com/multiformats/go-multiaddr"
)
var ErrInvalidAddress = errors.New("invalid address")
// Address represents the aurora address in boson.
// 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 boson.Address
Signature []byte
}
type addressJSON struct {
Overlay string `json:"overlay"`
Underlay string `json:"underlay"`
Signature string `json:"signature"`
}
type FileInfo struct {
TreeSize int `json:"treeSize"`
FileSize int `json:"fileSize"`
Bitvector BitVectorApi `json:"bitvector"`
PinState bool `json:"pinState"`
}
type ChunkInfoOverlay struct {
Overlay string `json:"overlay"`
Bit BitVectorApi `json:"bit"`
}
type BitVectorApi struct {
Len int `json:"len"`
B []byte `json:"b"`
}
type ChunkSourceApi struct {
Overlay string `json:"overlay"`
ChunkBit BitVectorApi `json:"chunkBit"`
}
type ChunkInfoSourceApi struct {
PyramidSource string `json:"pyramidSource"`
ChunkSource []ChunkSourceApi `json:"chunkSource"`
}
func NewAddress(signer crypto.Signer, underlay ma.Multiaddr, overlay boson.Address, networkID uint64) (*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,
}, nil
}
func ParseAddress(underlay, overlay, signature []byte, networkID uint64) (*Address, error) {
recoveredPK, err := crypto.Recover(signature, generateSignData(underlay, overlay, networkID))
if err != nil {
return nil, ErrInvalidAddress
}
recoveredOverlay, err := crypto.NewOverlayAddress(*recoveredPK, networkID)
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
}
return &Address{
Underlay: multiUnderlay,
Overlay: boson.NewAddress(overlay),
Signature: signature,
}, nil
}
func generateSignData(underlay, overlay []byte, networkID uint64) []byte {
networkIDBytes := make([]byte, 8)
binary.BigEndian.PutUint64(networkIDBytes, networkID)
signData := append([]byte("aurorafs-handshake-"), underlay...)
signData = append(signData, overlay...)
return append(signData, networkIDBytes...)
}
func (a *Address) Equal(b *Address) bool {
return a.Overlay.Equal(b.Overlay) && a.Underlay.Equal(b.Underlay) && bytes.Equal(a.Signature, b.Signature)
}
func (a *Address) MarshalJSON() ([]byte, error) {
return json.Marshal(&addressJSON{
Overlay: a.Overlay.String(),
Underlay: a.Underlay.String(),
Signature: base64.StdEncoding.EncodeToString(a.Signature),
})
}
func (a *Address) UnmarshalJSON(b []byte) error {
v := &addressJSON{}
err := json.Unmarshal(b, v)
if err != nil {
return err
}
addr, err := boson.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)
return err
}
func (a *Address) String() string {
return fmt.Sprintf("[Underlay: %v, Overlay %v, Signature %x]", a.Underlay, a.Overlay, a.Signature)
}
// ShortString returns shortened versions of aurora 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())
}