-
Notifications
You must be signed in to change notification settings - Fork 95
/
block_issuer_key.go
186 lines (150 loc) · 4.88 KB
/
block_issuer_key.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
package iotago
import (
"context"
"io"
"slices"
"github.com/iotaledger/hive.go/constraints"
"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/lo"
"github.com/iotaledger/hive.go/serializer/v2"
"github.com/iotaledger/hive.go/serializer/v2/stream"
)
// BlockIssuerKeyType defines the type of block issuer key.
type BlockIssuerKeyType byte
const (
// BlockIssuerKeyPublicKeyHash denotes a Ed25519PublicKeyHashBlockIssuerKey.
BlockIssuerKeyPublicKeyHash BlockIssuerKeyType = iota
)
// BlockIssuerKeys are the keys allowed to issue blocks from an account with a BlockIssuerFeature.
type BlockIssuerKeys []BlockIssuerKey
func NewBlockIssuerKeys(blockIssuerKey ...BlockIssuerKey) BlockIssuerKeys {
blockIssuerKeys := make(BlockIssuerKeys, 0, len(blockIssuerKey))
for _, key := range blockIssuerKey {
blockIssuerKeys = append(blockIssuerKeys, key)
}
blockIssuerKeys.Sort()
return blockIssuerKeys
}
func (keys BlockIssuerKeys) Clone() BlockIssuerKeys {
return lo.CloneSlice(keys)
}
// Add adds a new block issuer key if it doesn't exist yet.
func (keys *BlockIssuerKeys) Add(key BlockIssuerKey) {
for _, k := range *keys {
if k.Equal(key) {
// key already exists, don't add it
return
}
}
// we use the pointer, otherwise the outer slice header is not updated
*keys = append(*keys, key)
keys.Sort()
}
// Remove removes a block issuer key in case it exists.
func (keys *BlockIssuerKeys) Remove(key BlockIssuerKey) {
keysDereferenced := *keys
for idx, k := range keysDereferenced {
if k.Equal(key) {
keysDereferenced = append(keysDereferenced[:idx], keysDereferenced[idx+1:]...)
keysDereferenced.Sort()
// we use the pointer, otherwise the outer slice header is not updated
*keys = keysDereferenced
return
}
}
}
// Has checks if a block issuer key exists.
func (keys BlockIssuerKeys) Has(key BlockIssuerKey) bool {
for _, k := range keys {
if k.Equal(key) {
return true
}
}
return false
}
// Sort sorts the BlockIssuerKeys in place.
func (keys BlockIssuerKeys) Sort() {
slices.SortFunc(keys, func(x BlockIssuerKey, y BlockIssuerKey) int {
return x.Compare(y)
})
}
func (keys BlockIssuerKeys) Equal(other BlockIssuerKeys) bool {
if len(keys) != len(other) {
return false
}
for idx, key := range keys {
if !key.Equal(other[idx]) {
return false
}
}
return true
}
// Size returns the size of the block issuer key when serialized.
func (keys BlockIssuerKeys) Size() int {
// keys length prefix + size of each key
size := serializer.OneByte
for _, key := range keys {
size += key.Size()
}
return size
}
func (keys BlockIssuerKeys) StorageScore(storageScoreStruct *StorageScoreStructure, _ StorageScoreFunc) StorageScore {
var storageScore StorageScore
for _, key := range keys {
storageScore += key.StorageScore(storageScoreStruct, nil)
}
return storageScore
}
func (keys BlockIssuerKeys) Bytes() ([]byte, error) {
return CommonSerixAPI().Encode(context.TODO(), keys)
}
// BlockIssuerKey is a key that is allowed to issue blocks from an account with a BlockIssuerFeature.
type BlockIssuerKey interface {
Sizer
NonEphemeralObject
constraints.Cloneable[BlockIssuerKey]
constraints.Equalable[BlockIssuerKey]
constraints.Comparable[BlockIssuerKey]
serializer.Byter
// Type returns the BlockIssuerKeyType.
Type() BlockIssuerKeyType
}
func BlockIssuerKeysFromReader(reader io.ReadSeeker) (BlockIssuerKeys, error) {
b := make(BlockIssuerKeys, 0)
// serix.LengthPrefixTypeAsByte is used to read the length prefix of the array
if err := stream.ReadCollection(reader, serializer.SeriLengthPrefixTypeAsByte, func(i int) error {
blockIssuerKey, err := BlockIssuerKeyFromReader(reader)
if err != nil {
return ierrors.Wrapf(err, "unable to read block issuer key at index %d", i)
}
b = append(b, blockIssuerKey)
return nil
}); err != nil {
return nil, ierrors.Wrap(err, "unable to read block issuer keys")
}
return b, nil
}
func BlockIssuerKeyFromReader(reader io.ReadSeeker) (BlockIssuerKey, error) {
blockIssuerKeyType, err := stream.PeekSize(reader, serializer.SeriLengthPrefixTypeAsByte)
if err != nil {
return nil, ierrors.Wrap(err, "unable to read block issuer key type")
}
switch BlockIssuerKeyType(blockIssuerKeyType) {
case BlockIssuerKeyPublicKeyHash:
readBytes, err := stream.ReadBytes(reader, Ed25519PublicKeyHashBlockIssuerKeyLength)
if err != nil {
return nil, ierrors.Wrap(err, "unable to read block issuer key bytes")
}
return lo.DropCount(Ed25519PublicKeyHashBlockIssuerKeyFromBytes(readBytes))
default:
return nil, ierrors.Errorf("unsupported block issuer key type: %d", blockIssuerKeyType)
}
}
func BlockIssuerKeyFromBytes(bytes []byte) (BlockIssuerKey, int, error) {
var blockIssuerKey BlockIssuerKey
n, err := CommonSerixAPI().Decode(context.TODO(), bytes, blockIssuerKey)
if err != nil {
return nil, 0, ierrors.Wrap(err, "unable to decode block issuer key")
}
return blockIssuerKey, n, nil
}