-
Notifications
You must be signed in to change notification settings - Fork 95
/
identifier_account.gen.go
182 lines (138 loc) · 4.52 KB
/
identifier_account.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 (
// AccountIDLength defines the length of an AccountID.
AccountIDLength = blake2b.Size256
)
var (
EmptyAccountID = AccountID{}
)
// AccountID is a 32 byte hash value.
type AccountID [AccountIDLength]byte
type AccountIDs []AccountID
// AccountIDFromData returns a new AccountID for the given data by hashing it with blake2b.
func AccountIDFromData(data []byte) AccountID {
return blake2b.Sum256(data)
}
// AccountIDFromHexString converts the hex to an AccountID representation.
func AccountIDFromHexString(hex string) (AccountID, error) {
bytes, err := hexutil.DecodeHex(hex)
if err != nil {
return EmptyAccountID, err
}
a, _, err := AccountIDFromBytes(bytes)
return a, err
}
// MustAccountIDFromHexString converts the hex to an AccountID representation.
func MustAccountIDFromHexString(hex string) AccountID {
a, err := AccountIDFromHexString(hex)
if err != nil {
panic(err)
}
return a
}
// IsValidAccountID returns an error if the passed bytes are not a valid AccountID, otherwise nil.
func IsValidAccountID(b []byte) error {
if len(b) != AccountIDLength {
return ierrors.Errorf("invalid AccountID length: expected %d bytes, got %d bytes", AccountIDLength, len(b))
}
return nil
}
func AccountIDFromBytes(bytes []byte) (AccountID, int, error) {
var a AccountID
if len(bytes) < AccountIDLength {
return a, 0, ierrors.Errorf("invalid length for accountID, expected at least %d bytes, got %d bytes", AccountIDLength, len(bytes))
}
copy(a[:], bytes)
return a, len(bytes), nil
}
func (a AccountID) Bytes() ([]byte, error) {
return a[:], nil
}
func (a AccountID) MarshalText() (text []byte, err error) {
dst := make([]byte, hex.EncodedLen(len(EmptyAccountID)))
hex.Encode(dst, a[:])
return dst, nil
}
func (a *AccountID) UnmarshalText(text []byte) error {
_, err := hex.Decode(a[:], text)
return err
}
// Empty tells whether the AccountID is empty.
func (a AccountID) Empty() bool {
return a == EmptyAccountID
}
// ToHex converts the AccountID to its hex representation.
func (a AccountID) ToHex() string {
return hexutil.EncodeHex(a[:])
}
func (a AccountID) String() string {
return a.Alias()
}
var (
// accountIDAliases contains a dictionary of AccountIDs associated to their human-readable alias.
accountIDAliases = make(map[AccountID]string)
// accountIDAliasesMutex is the mutex that is used to synchronize access to the previous map.
accountIDAliasesMutex = sync.RWMutex{}
)
// RegisterAlias allows to register a human-readable alias for the AccountID which will be used as a replacement for
// the String method.
func (a AccountID) RegisterAlias(alias string) {
accountIDAliasesMutex.Lock()
defer accountIDAliasesMutex.Unlock()
accountIDAliases[a] = alias
}
// Alias returns the human-readable alias of the AccountID (or the hex encoded bytes if no alias was set).
func (a AccountID) Alias() (alias string) {
accountIDAliasesMutex.RLock()
defer accountIDAliasesMutex.RUnlock()
if existingAlias, exists := accountIDAliases[a]; exists {
return existingAlias
}
return a.ToHex()
}
// UnregisterAlias allows to unregister a previously registered alias.
func (a AccountID) UnregisterAlias() {
accountIDAliasesMutex.Lock()
defer accountIDAliasesMutex.Unlock()
delete(accountIDAliases, a)
}
// UnregisterAccountIDAliases allows to unregister all previously registered aliases.
func UnregisterAccountIDAliases() {
accountIDAliasesMutex.Lock()
defer accountIDAliasesMutex.Unlock()
accountIDAliases = make(map[AccountID]string)
}
// Matches checks whether other matches this ChainID.
func (a AccountID) Matches(other ChainID) bool {
otherAccountID, isAccountID := other.(AccountID)
if !isAccountID {
return false
}
return a == otherAccountID
}
// Addressable tells whether this ChainID can be converted into a ChainAddress.
func (a AccountID) Addressable() bool {
return true
}
// ToAddress converts this ChainID into an ChainAddress.
func (a AccountID) ToAddress() ChainAddress {
var addr AccountAddress
copy(addr[:], a[:])
return &addr
}
// FromOutputID returns the ChainID computed from a given OutputID.
func (a AccountID) FromOutputID(in OutputID) ChainID {
return AccountIDFromOutputID(in)
}
// AccountIDFromOutputID returns the AccountID computed from a given OutputID.
func AccountIDFromOutputID(outputID OutputID) AccountID {
return blake2b.Sum256(outputID[:])
}