-
Notifications
You must be signed in to change notification settings - Fork 95
/
address_multi_reference.go
77 lines (60 loc) · 1.95 KB
/
address_multi_reference.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
package iotago
import (
"bytes"
"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/lo"
"github.com/iotaledger/iota.go/v4/hexutil"
)
// MultiAddressReference is a reference to a MultiAddress.
// HINT: This is not an actual AddressType that is used in the protocol, so it should not be registered in serix.
// It should only be used internally or in APIs.
type MultiAddressReference struct {
MultiAddressID []byte
}
func (addr *MultiAddressReference) Clone() Address {
return &MultiAddressReference{
MultiAddressID: lo.CopySlice(addr.MultiAddressID),
}
}
func (addr *MultiAddressReference) StorageScore(_ *StorageScoreStructure, _ StorageScoreFunc) StorageScore {
panic("not used")
}
func (addr *MultiAddressReference) ID() []byte {
return addr.MultiAddressID
}
func (addr *MultiAddressReference) Key() string {
return string(addr.ID())
}
func (addr *MultiAddressReference) Equal(other Address) bool {
otherAddr, is := other.(*MultiAddressReference)
if !is {
return false
}
return bytes.Equal(addr.MultiAddressID, otherAddr.MultiAddressID)
}
func (addr *MultiAddressReference) Type() AddressType {
return AddressMulti
}
func (addr *MultiAddressReference) Bech32(hrp NetworkPrefix) string {
return bech32StringBytes(hrp, addr.ID())
}
func (addr *MultiAddressReference) String() string {
return hexutil.EncodeHex(addr.ID())
}
func (addr *MultiAddressReference) Size() int {
panic("not used")
}
func MultiAddressReferenceFromBytes(bytes []byte) (*MultiAddressReference, int, error) {
if len(bytes) < AddressMultiIDLength {
return nil, 0, ierrors.New("invalid multi address ID length")
}
if bytes[0] != byte(AddressMulti) {
return nil, 0, ErrInvalidAddressType
}
return &MultiAddressReference{MultiAddressID: bytes[:AddressMultiIDLength]}, AddressMultiIDLength, nil
}
func NewMultiAddressReferenceFromMultiAddress(address *MultiAddress) *MultiAddressReference {
return &MultiAddressReference{
MultiAddressID: address.ID(),
}
}