-
Notifications
You must be signed in to change notification settings - Fork 95
/
address_multi.go
193 lines (155 loc) · 4.99 KB
/
address_multi.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
package iotago
import (
"bytes"
"context"
"io"
"sort"
"golang.org/x/crypto/blake2b"
"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/lo"
"github.com/iotaledger/hive.go/serializer/v2"
"github.com/iotaledger/hive.go/serializer/v2/byteutils"
"github.com/iotaledger/hive.go/serializer/v2/serix"
"github.com/iotaledger/hive.go/serializer/v2/stream"
"github.com/iotaledger/iota.go/v4/hexutil"
)
const (
AddressWeightSerializedBytesSize = serializer.OneByte
AddressMultiIDLength = serializer.OneByte + blake2b.Size256
)
var (
ErrMultiAddressWeightInvalid = ierrors.New("multi address weight invalid")
ErrMultiAddressThresholdInvalid = ierrors.New("multi address treshold invalid")
)
// AddressWithWeight is an Address with a weight used for threshold calculation in a MultiAddress.
type AddressWithWeight struct {
Address Address `serix:""`
Weight byte `serix:""`
}
func (a *AddressWithWeight) Size() int {
// address + weight
return a.Address.Size() + AddressWeightSerializedBytesSize
}
func (a *AddressWithWeight) Compare(other *AddressWithWeight) int {
return bytes.Compare(a.Address.ID(), other.Address.ID())
}
// AddressesWithWeight is a set of AddressWithWeight.
type AddressesWithWeight []*AddressWithWeight
// Sort sorts the addresses in lexical order.
func (a AddressesWithWeight) Sort() {
sort.Slice(a, func(i, j int) bool {
return a[i].Compare(a[j]) < 0
})
}
// MultiAddress defines a multi address that consists of addresses with weights and
// a threshold value that needs to be reached to unlock the multi address.
type MultiAddress struct {
Addresses AddressesWithWeight `serix:""`
Threshold uint16 `serix:""`
}
func (addr *MultiAddress) Clone() Address {
cpy := &MultiAddress{
Addresses: make(AddressesWithWeight, 0, len(addr.Addresses)),
Threshold: addr.Threshold,
}
for i, address := range addr.Addresses {
cpy.Addresses[i] = &AddressWithWeight{
Address: address.Address.Clone(),
Weight: address.Weight,
}
}
return cpy
}
func (addr *MultiAddress) StorageScore(_ *StorageScoreStructure, _ StorageScoreFunc) StorageScore {
return 0
}
func (addr *MultiAddress) ID() []byte {
hash := blake2b.Sum256(lo.PanicOnErr(CommonSerixAPI().Encode(context.TODO(), addr)))
// prefix the hash of the multi address bytes with the AddressType
return byteutils.ConcatBytes([]byte{byte(AddressMulti)}, hash[:])
}
func (addr *MultiAddress) Key() string {
return string(addr.ID())
}
func (addr *MultiAddress) Equal(other Address) bool {
otherAddr, is := other.(*MultiAddress)
if !is {
return false
}
if len(addr.Addresses) != len(otherAddr.Addresses) {
return false
}
if addr.Threshold != otherAddr.Threshold {
return false
}
for i, address := range addr.Addresses {
if !bytes.Equal(address.Address.ID(), otherAddr.Addresses[i].Address.ID()) {
return false
}
if address.Weight != otherAddr.Addresses[i].Weight {
return false
}
}
return true
}
func (addr *MultiAddress) Type() AddressType {
return AddressMulti
}
func (addr *MultiAddress) Bech32(hrp NetworkPrefix) string {
return bech32StringBytes(hrp, addr.ID())
}
func (addr *MultiAddress) String() string {
return hexutil.EncodeHex(addr.ID())
}
func (addr *MultiAddress) Size() int {
// Address Type + Addresses Length + Threshold
sum := serializer.SmallTypeDenotationByteSize + serializer.SmallTypeDenotationByteSize + serializer.UInt16ByteSize
for _, address := range addr.Addresses {
sum += address.Size()
}
return sum
}
func NewMultiAddress(addresses AddressesWithWeight, threshold uint16) *MultiAddress {
return &MultiAddress{
Addresses: addresses,
Threshold: threshold,
}
}
// MultiAddressFromReader parses the MultiAddress from the given reader.
func MultiAddressFromReader(reader io.ReadSeeker) (Address, error) {
// skip the address type byte
if _, err := stream.Skip(reader, serializer.SmallTypeDenotationByteSize); err != nil {
return nil, ierrors.Wrap(err, "unable to skip address type byte")
}
var addressesWithWeight AddressesWithWeight
if err := stream.ReadCollection(reader, serializer.SeriLengthPrefixTypeAsByte, func(index int) error {
address, err := AddressFromReader(reader)
if err != nil {
return ierrors.Wrapf(err, "unable to read address %d", index)
}
weight, err := stream.Read[byte](reader)
if err != nil {
return ierrors.Wrapf(err, "unable to read address %d weight", index)
}
addressesWithWeight = append(addressesWithWeight, &AddressWithWeight{
Address: address,
Weight: weight,
})
return nil
}); err != nil {
return nil, ierrors.Wrap(err, "unable to read addresses with weight")
}
threshold, err := stream.Read[uint16](reader)
if err != nil {
return nil, ierrors.Wrap(err, "unable to read threshold")
}
multiAddress := &MultiAddress{
Addresses: addressesWithWeight,
Threshold: threshold,
}
_, err = CommonSerixAPI().Encode(context.TODO(), multiAddress, serix.WithValidation())
if err != nil {
return nil, ierrors.Wrap(err, "multi address validation failed")
}
return multiAddress, nil
}