-
Notifications
You must be signed in to change notification settings - Fork 95
/
identifier_anchor.gen.go
182 lines (138 loc) · 4.43 KB
/
identifier_anchor.gen.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
package iotago
// Code generated by go generate; DO NOT EDIT. Check gen/ directory instead.
import (
"encoding/hex"
"sync"
"golang.org/x/crypto/blake2b"
"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/iota.go/v4/hexutil"
)
const (
// AnchorIDLength defines the length of an AnchorID.
AnchorIDLength = blake2b.Size256
)
var (
EmptyAnchorID = AnchorID{}
)
// AnchorID is a 32 byte hash value.
type AnchorID [AnchorIDLength]byte
type AnchorIDs []AnchorID
// AnchorIDFromData returns a new AnchorID for the given data by hashing it with blake2b.
func AnchorIDFromData(data []byte) AnchorID {
return blake2b.Sum256(data)
}
// AnchorIDFromHexString converts the hex to an AnchorID representation.
func AnchorIDFromHexString(hex string) (AnchorID, error) {
bytes, err := hexutil.DecodeHex(hex)
if err != nil {
return EmptyAnchorID, err
}
a, _, err := AnchorIDFromBytes(bytes)
return a, err
}
// MustAnchorIDFromHexString converts the hex to an AnchorID representation.
func MustAnchorIDFromHexString(hex string) AnchorID {
a, err := AnchorIDFromHexString(hex)
if err != nil {
panic(err)
}
return a
}
// IsValidAnchorID returns an error if the passed bytes are not a valid AnchorID, otherwise nil.
func IsValidAnchorID(b []byte) error {
if len(b) != AnchorIDLength {
return ierrors.Errorf("invalid AnchorID length: expected %d bytes, got %d bytes", AnchorIDLength, len(b))
}
return nil
}
func AnchorIDFromBytes(bytes []byte) (AnchorID, int, error) {
var a AnchorID
if len(bytes) < AnchorIDLength {
return a, 0, ierrors.Errorf("invalid length for anchorID, expected at least %d bytes, got %d bytes", AnchorIDLength, len(bytes))
}
copy(a[:], bytes)
return a, len(bytes), nil
}
func (a AnchorID) Bytes() ([]byte, error) {
return a[:], nil
}
func (a AnchorID) MarshalText() (text []byte, err error) {
dst := make([]byte, hex.EncodedLen(len(EmptyAnchorID)))
hex.Encode(dst, a[:])
return dst, nil
}
func (a *AnchorID) UnmarshalText(text []byte) error {
_, err := hex.Decode(a[:], text)
return err
}
// Empty tells whether the AnchorID is empty.
func (a AnchorID) Empty() bool {
return a == EmptyAnchorID
}
// ToHex converts the AnchorID to its hex representation.
func (a AnchorID) ToHex() string {
return hexutil.EncodeHex(a[:])
}
func (a AnchorID) String() string {
return a.Alias()
}
var (
// anchorIDAliases contains a dictionary of AnchorIDs associated to their human-readable alias.
anchorIDAliases = make(map[AnchorID]string)
// anchorIDAliasesMutex is the mutex that is used to synchronize access to the previous map.
anchorIDAliasesMutex = sync.RWMutex{}
)
// RegisterAlias allows to register a human-readable alias for the AnchorID which will be used as a replacement for
// the String method.
func (a AnchorID) RegisterAlias(alias string) {
anchorIDAliasesMutex.Lock()
defer anchorIDAliasesMutex.Unlock()
anchorIDAliases[a] = alias
}
// Alias returns the human-readable alias of the AnchorID (or the hex encoded bytes if no alias was set).
func (a AnchorID) Alias() (alias string) {
anchorIDAliasesMutex.RLock()
defer anchorIDAliasesMutex.RUnlock()
if existingAlias, exists := anchorIDAliases[a]; exists {
return existingAlias
}
return a.ToHex()
}
// UnregisterAlias allows to unregister a previously registered alias.
func (a AnchorID) UnregisterAlias() {
anchorIDAliasesMutex.Lock()
defer anchorIDAliasesMutex.Unlock()
delete(anchorIDAliases, a)
}
// UnregisterAnchorIDAliases allows to unregister all previously registered aliases.
func UnregisterAnchorIDAliases() {
anchorIDAliasesMutex.Lock()
defer anchorIDAliasesMutex.Unlock()
anchorIDAliases = make(map[AnchorID]string)
}
// Matches checks whether other matches this ChainID.
func (a AnchorID) Matches(other ChainID) bool {
otherAnchorID, isAnchorID := other.(AnchorID)
if !isAnchorID {
return false
}
return a == otherAnchorID
}
// Addressable tells whether this ChainID can be converted into a ChainAddress.
func (a AnchorID) Addressable() bool {
return true
}
// ToAddress converts this ChainID into an ChainAddress.
func (a AnchorID) ToAddress() ChainAddress {
var addr AnchorAddress
copy(addr[:], a[:])
return &addr
}
// FromOutputID returns the ChainID computed from a given OutputID.
func (a AnchorID) FromOutputID(in OutputID) ChainID {
return AnchorIDFromOutputID(in)
}
// AnchorIDFromOutputID returns the AnchorID computed from a given OutputID.
func AnchorIDFromOutputID(outputID OutputID) AnchorID {
return blake2b.Sum256(outputID[:])
}