-
Notifications
You must be signed in to change notification settings - Fork 95
/
identifier.gen.go
149 lines (112 loc) · 3.75 KB
/
identifier.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
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 (
// IdentifierLength defines the length of an Identifier.
IdentifierLength = blake2b.Size256
)
var (
EmptyIdentifier = Identifier{}
)
// Identifier is a 32 byte hash value.
type Identifier [IdentifierLength]byte
type Identifiers []Identifier
// IdentifierFromData returns a new Identifier for the given data by hashing it with blake2b.
func IdentifierFromData(data []byte) Identifier {
return blake2b.Sum256(data)
}
// IdentifierFromHexString converts the hex to an Identifier representation.
func IdentifierFromHexString(hex string) (Identifier, error) {
bytes, err := hexutil.DecodeHex(hex)
if err != nil {
return EmptyIdentifier, err
}
i, _, err := IdentifierFromBytes(bytes)
return i, err
}
// MustIdentifierFromHexString converts the hex to an Identifier representation.
func MustIdentifierFromHexString(hex string) Identifier {
i, err := IdentifierFromHexString(hex)
if err != nil {
panic(err)
}
return i
}
// IsValidIdentifier returns an error if the passed bytes are not a valid Identifier, otherwise nil.
func IsValidIdentifier(b []byte) error {
if len(b) != IdentifierLength {
return ierrors.Errorf("invalid Identifier length: expected %d bytes, got %d bytes", IdentifierLength, len(b))
}
return nil
}
func IdentifierFromBytes(bytes []byte) (Identifier, int, error) {
var i Identifier
if len(bytes) < IdentifierLength {
return i, 0, ierrors.Errorf("invalid length for identifier, expected at least %d bytes, got %d bytes", IdentifierLength, len(bytes))
}
copy(i[:], bytes)
return i, len(bytes), nil
}
func (i Identifier) Bytes() ([]byte, error) {
return i[:], nil
}
func (i Identifier) MarshalText() (text []byte, err error) {
dst := make([]byte, hex.EncodedLen(len(EmptyIdentifier)))
hex.Encode(dst, i[:])
return dst, nil
}
func (i *Identifier) UnmarshalText(text []byte) error {
_, err := hex.Decode(i[:], text)
return err
}
// Empty tells whether the Identifier is empty.
func (i Identifier) Empty() bool {
return i == EmptyIdentifier
}
// ToHex converts the Identifier to its hex representation.
func (i Identifier) ToHex() string {
return hexutil.EncodeHex(i[:])
}
func (i Identifier) String() string {
return i.Alias()
}
var (
// identifierAliases contains a dictionary of Identifiers associated to their human-readable alias.
identifierAliases = make(map[Identifier]string)
// identifierAliasesMutex is the mutex that is used to synchronize access to the previous map.
identifierAliasesMutex = sync.RWMutex{}
)
// RegisterAlias allows to register a human-readable alias for the Identifier which will be used as a replacement for
// the String method.
func (i Identifier) RegisterAlias(alias string) {
identifierAliasesMutex.Lock()
defer identifierAliasesMutex.Unlock()
identifierAliases[i] = alias
}
// Alias returns the human-readable alias of the Identifier (or the hex encoded bytes if no alias was set).
func (i Identifier) Alias() (alias string) {
identifierAliasesMutex.RLock()
defer identifierAliasesMutex.RUnlock()
if existingAlias, exists := identifierAliases[i]; exists {
return existingAlias
}
return i.ToHex()
}
// UnregisterAlias allows to unregister a previously registered alias.
func (i Identifier) UnregisterAlias() {
identifierAliasesMutex.Lock()
defer identifierAliasesMutex.Unlock()
delete(identifierAliases, i)
}
// UnregisterIdentifierAliases allows to unregister all previously registered aliases.
func UnregisterIdentifierAliases() {
identifierAliasesMutex.Lock()
defer identifierAliasesMutex.Unlock()
identifierAliases = make(map[Identifier]string)
}