-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
id.go
225 lines (194 loc) · 4.15 KB
/
id.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package keys
import (
"crypto/sha256"
"fmt"
"strings"
"github.com/keys-pub/keys/bech32"
"github.com/pkg/errors"
)
// ID is a bech32 encoded string.
type ID string
func (i ID) String() string {
return string(i)
}
// Decode ID into HRP (human readable part) and bytes (data).
func (i ID) Decode() (string, []byte, error) {
return bech32.Decode(i.String())
}
// NewID creates ID from HRP (human readable part) and bytes.
func NewID(hrp string, b []byte) (ID, error) {
out, err := bech32.Encode(hrp, b)
if err != nil {
return "", err
}
return ID(out), nil
}
// MustID returns a (bech32) ID with HRP (human readable part) and bytes, or
// panics if invalid.
func MustID(hrp string, b []byte) ID {
id, err := NewID(hrp, b)
if err != nil {
panic(err)
}
return id
}
// ParseID parses a string and validates an ID.
func ParseID(s string) (ID, error) {
if s == "" {
return "", errors.Errorf("failed to parse id: empty string")
}
_, _, err := bech32.Decode(s)
if err != nil {
return "", errors.Wrapf(err, "failed to parse id")
}
return ID(s), nil
}
// IsValidID returns true if string is a valid ID.
func IsValidID(s string) bool {
_, err := ParseID(s)
return err == nil
}
// RandID returns a random (bech32) ID.
func RandID(hrp string) ID {
b := Rand32()
return MustID(hrp, b[:])
}
// IDsToStrings returns []strings for []ID.
func IDsToStrings(ids []ID) []string {
strs := make([]string, 0, len(ids))
for _, id := range ids {
strs = append(strs, id.String())
}
return strs
}
// IDsToString returns string for joined Ikeys.
func IDsToString(ids []ID, delim string) string {
return strings.Join(IDsToStrings(ids), delim)
}
// ParseIDs returns IDs from strings.
func ParseIDs(strs []string) ([]ID, error) {
ids := make([]ID, 0, len(strs))
for _, s := range strs {
id, err := ParseID(s)
if err != nil {
return nil, err
}
ids = append(ids, id)
}
return ids, nil
}
// WithSeq returns string with a sequence value appended to the ID.
func (i ID) WithSeq(seq int) string {
if seq == 0 {
panic("invalid seq")
}
return fmt.Sprintf("%s-%015d", i, seq)
}
// IsEdX25519 returns true if ID represents a EdX25519 key.
func (i ID) IsEdX25519() bool {
hrp, _, err := i.Decode()
if err != nil {
return false
}
return hrp == edx25519KeyHRP
}
// IsX25519 returns true if ID represents a X25519 key.
func (i ID) IsX25519() bool {
hrp, _, err := i.Decode()
if err != nil {
return false
}
return hrp == x25519KeyHRP
}
// ID for Keys interface.
func (i ID) ID() ID {
return i
}
// Type of key.
func (i ID) Type() KeyType {
hrp, _, err := i.Decode()
if err != nil {
return ""
}
switch hrp {
case edx25519KeyHRP:
return EdX25519
case x25519KeyHRP:
return X25519
default:
return ""
}
}
// Private returns nil (for Key interface).
func (i ID) Private() []byte {
return nil
}
// Public key data (for Key interface).
func (i ID) Public() []byte {
_, b, err := i.Decode()
if err != nil {
return nil
}
return b
}
// UUID returns a 16 byte hash of the public bytes.
func (i ID) UUID() *[16]byte {
hash := sha256.Sum256(i.Public())
return Bytes16(hash[:16])
}
// IDSet is a set of strings.
type IDSet struct {
keysMap map[ID]bool
keys []ID
}
// NewIDSet creates IDSet.
func NewIDSet(ids ...ID) *IDSet {
return newIDSet(len(ids), ids...)
}
// NewIDSetWithCapacity ..
func NewIDSetWithCapacity(capacity int) *IDSet {
return newIDSet(capacity)
}
func newIDSet(capacity int, ids ...ID) *IDSet {
keysMap := make(map[ID]bool, capacity)
keys := make([]ID, 0, capacity)
for _, v := range ids {
keysMap[v] = true
keys = append(keys, v)
}
return &IDSet{
keysMap: keysMap,
keys: keys,
}
}
// Contains returns true if set contains string.
func (s *IDSet) Contains(id ID) bool {
return s.keysMap[id]
}
// Add to set.
func (s *IDSet) Add(id ID) {
if s.Contains(id) {
return
}
s.keysMap[id] = true
s.keys = append(s.keys, id)
}
// AddAll to set.
func (s *IDSet) AddAll(ids []ID) {
for _, id := range ids {
s.Add(id)
}
}
// Clear set.
func (s *IDSet) Clear() {
s.keysMap = map[ID]bool{}
s.keys = []ID{}
}
// IDs returns IDs in set.
func (s *IDSet) IDs() []ID {
return s.keys
}
// Size for set.
func (s *IDSet) Size() int {
return len(s.keys)
}