-
Notifications
You must be signed in to change notification settings - Fork 115
/
unlockblock.go
411 lines (336 loc) · 14.8 KB
/
unlockblock.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
package ledgerstate
import (
"strconv"
"github.com/cockroachdb/errors"
"github.com/iotaledger/hive.go/bytesfilter"
"github.com/iotaledger/hive.go/byteutils"
"github.com/iotaledger/hive.go/cerrors"
"github.com/iotaledger/hive.go/marshalutil"
"github.com/iotaledger/hive.go/stringify"
)
// region UnlockBlockType //////////////////////////////////////////////////////////////////////////////////////////////
const (
// SignatureUnlockBlockType represents the type of a SignatureUnlockBlock.
SignatureUnlockBlockType UnlockBlockType = iota
// ReferenceUnlockBlockType represents the type of a ReferenceUnlockBlock.
ReferenceUnlockBlockType
// AliasUnlockBlockType represents the type of a AliasUnlockBlock.
AliasUnlockBlockType
)
// UnlockBlockType represents the type of the UnlockBlock. Different types of UnlockBlocks can unlock different types of
// Outputs.
type UnlockBlockType uint8
// String returns a human readable representation of the UnlockBlockType.
func (a UnlockBlockType) String() string {
return [...]string{
"SignatureUnlockBlockType",
"ReferenceUnlockBlockType",
"AliasUnlockBlockType",
}[a]
}
// endregion ///////////////////////////////////////////////////////////////////////////////////////////////////////////
// region UnlockBlock //////////////////////////////////////////////////////////////////////////////////////////////////
// UnlockBlock represents a generic interface to address the different kinds of unlock information that are required to
// authorize the spending of different Output types.
type UnlockBlock interface {
// Type returns the UnlockBlockType of the UnlockBlock.
Type() UnlockBlockType
// Bytes returns a marshaled version of the UnlockBlock.
Bytes() []byte
// String returns a human readable version of the UnlockBlock.
String() string
}
// UnlockBlockFromBytes unmarshals an UnlockBlock from a sequence of bytes.
func UnlockBlockFromBytes(bytes []byte) (unlockBlock UnlockBlock, consumedBytes int, err error) {
marshalUtil := marshalutil.New(bytes)
if unlockBlock, err = UnlockBlockFromMarshalUtil(marshalUtil); err != nil {
err = errors.Errorf("failed to parse UnlockBlock from MarshalUtil: %w", err)
return
}
consumedBytes = marshalUtil.ReadOffset()
return
}
// UnlockBlockFromMarshalUtil unmarshals an UnlockBlock using a MarshalUtil (for easier unmarshaling).
func UnlockBlockFromMarshalUtil(marshalUtil *marshalutil.MarshalUtil) (unlockBlock UnlockBlock, err error) {
unlockBlockType, err := marshalUtil.ReadByte()
if err != nil {
err = errors.Errorf("failed to parse UnlockBlockType (%v): %w", err, cerrors.ErrParseBytesFailed)
return
}
marshalUtil.ReadSeek(-1)
switch UnlockBlockType(unlockBlockType) {
case SignatureUnlockBlockType:
if unlockBlock, err = SignatureUnlockBlockFromMarshalUtil(marshalUtil); err != nil {
err = errors.Errorf("failed to parse SignatureUnlockBlock from MarshalUtil: %w", err)
return
}
case ReferenceUnlockBlockType:
if unlockBlock, err = ReferenceUnlockBlockFromMarshalUtil(marshalUtil); err != nil {
err = errors.Errorf("failed to parse ReferenceUnlockBlock from MarshalUtil: %w", err)
return
}
case AliasUnlockBlockType:
if unlockBlock, err = AliasUnlockBlockFromMarshalUtil(marshalUtil); err != nil {
err = errors.Errorf("failed to parse AliasUnlockBlock from MarshalUtil: %w", err)
return
}
default:
err = errors.Errorf("unsupported UnlockBlockType (%X): %w", unlockBlockType, cerrors.ErrParseBytesFailed)
return
}
return
}
// endregion ///////////////////////////////////////////////////////////////////////////////////////////////////////////
// region UnlockBlocks /////////////////////////////////////////////////////////////////////////////////////////////////
// UnlockBlocks is slice of UnlockBlocks that offers additional methods for easier marshaling and unmarshaling.
type UnlockBlocks []UnlockBlock
// UnlockBlocksFromBytes unmarshals UnlockBlocks from a sequence of bytes.
func UnlockBlocksFromBytes(bytes []byte) (unlockBlocks UnlockBlocks, consumedBytes int, err error) {
marshalUtil := marshalutil.New(bytes)
if unlockBlocks, err = UnlockBlocksFromMarshalUtil(marshalUtil); err != nil {
err = errors.Errorf("failed to parse UnlockBlocks from MarshalUtil: %w", err)
return
}
consumedBytes = marshalUtil.ReadOffset()
return
}
// UnlockBlocksFromMarshalUtil unmarshals UnlockBlocks using a MarshalUtil (for easier unmarshaling).
func UnlockBlocksFromMarshalUtil(marshalUtil *marshalutil.MarshalUtil) (unlockBlocks UnlockBlocks, err error) {
unlockBlockCount, err := marshalUtil.ReadUint16()
if err != nil {
err = errors.Errorf("failed to parse UnlockBlock count (%v): %w", err, cerrors.ErrParseBytesFailed)
return
}
seenUnlockBlocks := bytesfilter.New(int(unlockBlockCount))
unlockBlocks = make(UnlockBlocks, unlockBlockCount)
for i := uint16(0); i < unlockBlockCount; i++ {
unlockBlockBytesStart := marshalUtil.ReadOffset()
unlockBlock, unlockBlockErr := UnlockBlockFromMarshalUtil(marshalUtil)
if unlockBlockErr != nil {
err = errors.Errorf("failed to parse UnlockBlock from MarshalUtil: %w", unlockBlockErr)
return
}
unlockBlockBytes, unlockBlockBytesErr := marshalUtil.ReadBytes(marshalUtil.ReadOffset()-unlockBlockBytesStart, unlockBlockBytesStart)
if unlockBlockBytesErr != nil {
err = errors.Errorf("failed to parse UnlockBlock bytes from MarshalUtil: %w", unlockBlockBytesErr)
return
}
if unlockBlock.Type() != ReferenceUnlockBlockType &&
unlockBlock.Type() != AliasUnlockBlockType &&
!seenUnlockBlocks.Add(unlockBlockBytes) {
err = errors.Errorf("duplicate UnlockBlock detected at index %d: %w", i, cerrors.ErrParseBytesFailed)
return
}
unlockBlocks[i] = unlockBlock
}
return
}
// Bytes returns a marshaled version of the UnlockBlocks.
func (u UnlockBlocks) Bytes() []byte {
marshalUtil := marshalutil.New()
marshalUtil.WriteUint16(uint16(len(u)))
for _, unlockBlock := range u {
marshalUtil.WriteBytes(unlockBlock.Bytes())
}
return marshalUtil.Bytes()
}
// String returns a human readable version of the UnlockBlocks.
func (u UnlockBlocks) String() string {
structBuilder := stringify.StructBuilder("UnlockBlocks")
for i, unlockBlock := range u {
structBuilder.AddField(stringify.StructField(strconv.Itoa(i), unlockBlock))
}
return structBuilder.String()
}
// endregion ///////////////////////////////////////////////////////////////////////////////////////////////////////////
// region SignatureUnlockBlock /////////////////////////////////////////////////////////////////////////////////////////
// SignatureUnlockBlock represents an UnlockBlock that contains a Signature for an Address.
type SignatureUnlockBlock struct {
signature Signature
}
// NewSignatureUnlockBlock is the constructor for SignatureUnlockBlock objects.
func NewSignatureUnlockBlock(signature Signature) *SignatureUnlockBlock {
return &SignatureUnlockBlock{
signature: signature,
}
}
// SignatureUnlockBlockFromBytes unmarshals a SignatureUnlockBlock from a sequence of bytes.
func SignatureUnlockBlockFromBytes(bytes []byte) (unlockBlock *SignatureUnlockBlock, consumedBytes int, err error) {
marshalUtil := marshalutil.New(bytes)
if unlockBlock, err = SignatureUnlockBlockFromMarshalUtil(marshalUtil); err != nil {
err = errors.Errorf("failed to parse SignatureUnlockBlock from MarshalUtil: %w", err)
return
}
consumedBytes = marshalUtil.ReadOffset()
return
}
// SignatureUnlockBlockFromMarshalUtil unmarshals a SignatureUnlockBlock using a MarshalUtil (for easier unmarshaling).
func SignatureUnlockBlockFromMarshalUtil(marshalUtil *marshalutil.MarshalUtil) (unlockBlock *SignatureUnlockBlock, err error) {
unlockBlockType, err := marshalUtil.ReadByte()
if err != nil {
err = errors.Errorf("failed to parse UnlockBlockType (%v): %w", err, cerrors.ErrParseBytesFailed)
return
}
if UnlockBlockType(unlockBlockType) != SignatureUnlockBlockType {
err = errors.Errorf("invalid UnlockBlockType (%X): %w", unlockBlockType, cerrors.ErrParseBytesFailed)
return
}
unlockBlock = &SignatureUnlockBlock{}
if unlockBlock.signature, err = SignatureFromMarshalUtil(marshalUtil); err != nil {
err = errors.Errorf("failed to parse Signature from MarshalUtil: %w", err)
return
}
return
}
// AddressSignatureValid returns true if the UnlockBlock correctly signs the given Address.
func (s *SignatureUnlockBlock) AddressSignatureValid(address Address, signedData []byte) bool {
return s.signature.AddressSignatureValid(address, signedData)
}
// Type returns the UnlockBlockType of the UnlockBlock.
func (s *SignatureUnlockBlock) Type() UnlockBlockType {
return SignatureUnlockBlockType
}
// Bytes returns a marshaled version of the UnlockBlock.
func (s *SignatureUnlockBlock) Bytes() []byte {
return byteutils.ConcatBytes([]byte{byte(SignatureUnlockBlockType)}, s.signature.Bytes())
}
// String returns a human readable version of the UnlockBlock.
func (s *SignatureUnlockBlock) String() string {
return stringify.Struct("SignatureUnlockBlock",
stringify.StructField("signature", s.signature),
)
}
// Signature return the signature itself.
func (s *SignatureUnlockBlock) Signature() Signature {
return s.signature
}
// code contract (make sure the type implements all required methods)
var _ UnlockBlock = &SignatureUnlockBlock{}
// endregion ///////////////////////////////////////////////////////////////////////////////////////////////////////////
// region ReferenceUnlockBlock /////////////////////////////////////////////////////////////////////////////////////////
// ReferenceUnlockBlock defines an UnlockBlock which references a previous UnlockBlock (which must not be another
// ReferenceUnlockBlock).
type ReferenceUnlockBlock struct {
referencedIndex uint16
}
// NewReferenceUnlockBlock is the constructor for ReferenceUnlockBlocks.
func NewReferenceUnlockBlock(referencedIndex uint16) *ReferenceUnlockBlock {
return &ReferenceUnlockBlock{
referencedIndex: referencedIndex,
}
}
// ReferenceUnlockBlockFromBytes unmarshals a ReferenceUnlockBlock from a sequence of bytes.
func ReferenceUnlockBlockFromBytes(bytes []byte) (unlockBlock *ReferenceUnlockBlock, consumedBytes int, err error) {
marshalUtil := marshalutil.New(bytes)
if unlockBlock, err = ReferenceUnlockBlockFromMarshalUtil(marshalUtil); err != nil {
err = errors.Errorf("failed to parse ReferenceUnlockBlock from MarshalUtil: %w", err)
return
}
consumedBytes = marshalUtil.ReadOffset()
return
}
// ReferenceUnlockBlockFromMarshalUtil unmarshals a ReferenceUnlockBlock using a MarshalUtil (for easier unmarshaling).
func ReferenceUnlockBlockFromMarshalUtil(marshalUtil *marshalutil.MarshalUtil) (unlockBlock *ReferenceUnlockBlock, err error) {
unlockBlockType, err := marshalUtil.ReadByte()
if err != nil {
err = errors.Errorf("failed to parse UnlockBlockType (%v): %w", err, cerrors.ErrParseBytesFailed)
return
}
if UnlockBlockType(unlockBlockType) != ReferenceUnlockBlockType {
err = errors.Errorf("invalid UnlockBlockType (%X): %w", unlockBlockType, cerrors.ErrParseBytesFailed)
return
}
unlockBlock = &ReferenceUnlockBlock{}
if unlockBlock.referencedIndex, err = marshalUtil.ReadUint16(); err != nil {
err = errors.Errorf("failed to parse referencedIndex (%v): %w", err, cerrors.ErrParseBytesFailed)
return
}
return
}
// ReferencedIndex returns the index of the referenced UnlockBlock.
func (r *ReferenceUnlockBlock) ReferencedIndex() uint16 {
return r.referencedIndex
}
// Type returns the UnlockBlockType of the UnlockBlock.
func (r *ReferenceUnlockBlock) Type() UnlockBlockType {
return ReferenceUnlockBlockType
}
// Bytes returns a marshaled version of the UnlockBlock.
func (r *ReferenceUnlockBlock) Bytes() []byte {
return marshalutil.New(1 + marshalutil.Uint16Size).
WriteByte(byte(ReferenceUnlockBlockType)).
WriteUint16(r.referencedIndex).
Bytes()
}
// String returns a human readable version of the UnlockBlock.
func (r *ReferenceUnlockBlock) String() string {
return stringify.Struct("ReferenceUnlockBlock",
stringify.StructField("referencedIndex", int(r.referencedIndex)),
)
}
// code contract (make sure the type implements all required methods)
var _ UnlockBlock = &ReferenceUnlockBlock{}
// endregion ///////////////////////////////////////////////////////////////////////////////////////////////////////////
// region AliasUnlockBlock /////////////////////////////////////////////////////////////////////////////////////////
// AliasUnlockBlock defines an UnlockBlock which contains an index of corresponding AliasOutput.
type AliasUnlockBlock struct {
referencedIndex uint16
}
// NewAliasUnlockBlock is the constructor for AliasUnlockBlocks.
func NewAliasUnlockBlock(chainInputIndex uint16) *AliasUnlockBlock {
return &AliasUnlockBlock{
referencedIndex: chainInputIndex,
}
}
// AliasUnlockBlockFromBytes unmarshals a AliasUnlockBlock from a sequence of bytes.
func AliasUnlockBlockFromBytes(bytes []byte) (unlockBlock *AliasUnlockBlock, consumedBytes int, err error) {
marshalUtil := marshalutil.New(bytes)
if unlockBlock, err = AliasUnlockBlockFromMarshalUtil(marshalUtil); err != nil {
err = errors.Errorf("failed to parse AliasUnlockBlock from MarshalUtil: %w", err)
return
}
consumedBytes = marshalUtil.ReadOffset()
return
}
// AliasUnlockBlockFromMarshalUtil unmarshals a AliasUnlockBlock using a MarshalUtil (for easier unmarshaling).
func AliasUnlockBlockFromMarshalUtil(marshalUtil *marshalutil.MarshalUtil) (unlockBlock *AliasUnlockBlock, err error) {
unlockBlockType, err := marshalUtil.ReadByte()
if err != nil {
err = errors.Errorf("failed to parse UnlockBlockType (%v): %w", err, cerrors.ErrParseBytesFailed)
return
}
if UnlockBlockType(unlockBlockType) != AliasUnlockBlockType {
err = errors.Errorf("invalid UnlockBlockType (%X): %w", unlockBlockType, cerrors.ErrParseBytesFailed)
return
}
unlockBlock = &AliasUnlockBlock{}
if unlockBlock.referencedIndex, err = marshalUtil.ReadUint16(); err != nil {
err = errors.Errorf("failed to parse referencedIndex (%v): %w", err, cerrors.ErrParseBytesFailed)
return
}
return
}
// AliasInputIndex returns the index of the input, the AliasOutput which contains AliasAddress.
func (r *AliasUnlockBlock) AliasInputIndex() uint16 {
return r.referencedIndex
}
// Type returns the UnlockBlockType of the UnlockBlock.
func (r *AliasUnlockBlock) Type() UnlockBlockType {
return AliasUnlockBlockType
}
// Bytes returns a marshaled version of the UnlockBlock.
func (r *AliasUnlockBlock) Bytes() []byte {
return marshalutil.New(1 + marshalutil.Uint16Size).
WriteByte(byte(AliasUnlockBlockType)).
WriteUint16(r.referencedIndex).
Bytes()
}
// String returns a human readable version of the UnlockBlock.
func (r *AliasUnlockBlock) String() string {
return stringify.Struct("AliasUnlockBlock",
stringify.StructField("referencedIndex", int(r.referencedIndex)),
)
}
// code contract (make sure the type implements all required methods).
var _ UnlockBlock = &AliasUnlockBlock{}
// endregion ///////////////////////////////////////////////////////////////////////////////////////////////////////////