@@ -6,7 +6,7 @@ const $ = require('../util/preconditions');
6
6
const constants = require ( '../constants' ) ;
7
7
const doubleSha256 = require ( '../crypto/hash' ) . sha256sha256 ;
8
8
9
- const { isHexaString, isHexStringOfSize } = require ( '../util/js' ) ;
9
+ const { isHexaString, isHexStringOfSize, isUnsignedInteger } = require ( '../util/js' ) ;
10
10
11
11
const { SHA256_HASH_SIZE , BLS_SIGNATURE_SIZE } = constants ;
12
12
const bls = require ( '../crypto/bls' ) ;
@@ -29,8 +29,10 @@ class InstantLock {
29
29
}
30
30
const info = InstantLock . _from ( arg ) ;
31
31
32
+ this . version = info . version ;
32
33
this . inputs = info . inputs ;
33
34
this . txid = info . txid ;
35
+ this . cyclehash = info . cyclehash ;
34
36
this . signature = info . signature ;
35
37
this . validate ( ) ;
36
38
return this ;
@@ -63,11 +65,14 @@ class InstantLock {
63
65
static _fromObject ( data ) {
64
66
$ . checkArgument ( data , 'data is required' ) ;
65
67
const txid = data . txid || data . txId ;
66
- const { signature } = data ;
68
+ const cyclehash = data . cyclehash || data . cycleHash ;
69
+ const { signature, version } = data ;
67
70
68
71
return {
72
+ version,
69
73
inputs : data . inputs ,
70
74
txid,
75
+ cyclehash,
71
76
signature,
72
77
} ;
73
78
}
@@ -79,6 +84,7 @@ class InstantLock {
79
84
*/
80
85
static _fromBufferReader ( br ) {
81
86
const info = { } ;
87
+ info . version = br . readUInt8 ( ) ;
82
88
const inputsCount = br . readVarintNum ( ) ;
83
89
info . inputs = [ ] ;
84
90
for ( let i = 0 ; i < inputsCount ; i += 1 ) {
@@ -88,6 +94,7 @@ class InstantLock {
88
94
info . inputs . push ( outpoint ) ;
89
95
}
90
96
info . txid = br . readReverse ( SHA256_HASH_SIZE ) . toString ( 'hex' ) ;
97
+ info . cyclehash = br . readReverse ( SHA256_HASH_SIZE ) . toString ( 'hex' ) ;
91
98
info . signature = br . read ( BLS_SIGNATURE_SIZE ) . toString ( 'hex' ) ;
92
99
return info ;
93
100
}
@@ -215,6 +222,10 @@ class InstantLock {
215
222
* Validate InstantLock structure
216
223
*/
217
224
validate ( ) {
225
+ $ . checkArgument (
226
+ isUnsignedInteger ( this . version ) ,
227
+ "Expected version to be an unsigned integer"
228
+ ) ;
218
229
$ . checkArgument (
219
230
this . inputs . length > 0 ,
220
231
"TXs with no inputs can't be locked"
@@ -227,6 +238,10 @@ class InstantLock {
227
238
isHexStringOfSize ( this . txid . toString ( 'hex' ) , SHA256_HASH_SIZE * 2 ) ,
228
239
`Expected txid to be a hex string of size ${ SHA256_HASH_SIZE } `
229
240
) ;
241
+ $ . checkArgument (
242
+ isHexStringOfSize ( this . cyclehash . toString ( 'hex' ) , SHA256_HASH_SIZE * 2 ) ,
243
+ `Expected cycleHash to be a hex string of size ${ SHA256_HASH_SIZE } `
244
+ ) ;
230
245
$ . checkArgument (
231
246
isHexStringOfSize ( this . signature . toString ( 'hex' ) , BLS_SIGNATURE_SIZE * 2 ) ,
232
247
'Expected signature to be a bls signature'
@@ -314,8 +329,10 @@ class InstantLock {
314
329
toObject ( ) {
315
330
this . validate ( ) ;
316
331
return {
332
+ version : this . version ,
317
333
inputs : this . inputs ,
318
334
txid : this . txid ,
335
+ cyclehash : this . cyclehash . toString ( 'hex' ) ,
319
336
signature : this . signature . toString ( 'hex' ) ,
320
337
} ;
321
338
}
@@ -351,6 +368,7 @@ class InstantLock {
351
368
*/
352
369
toBufferWriter ( bw ) {
353
370
const bufferWriter = bw || new BufferWriter ( ) ;
371
+ bufferWriter . writeUInt8 ( this . version ) ;
354
372
const inputsCount = this . inputs . length ;
355
373
bufferWriter . writeVarintNum ( inputsCount ) ;
356
374
for ( let i = 0 ; i < inputsCount ; i += 1 ) {
@@ -360,6 +378,7 @@ class InstantLock {
360
378
bufferWriter . writeInt32LE ( this . inputs [ i ] . outpointIndex ) ;
361
379
}
362
380
bufferWriter . writeReverse ( Buffer . from ( this . txid , 'hex' ) ) ;
381
+ bufferWriter . writeReverse ( Buffer . from ( this . cyclehash , 'hex' ) ) ;
363
382
bufferWriter . write ( Buffer . from ( this . signature , 'hex' ) ) ;
364
383
return bufferWriter ;
365
384
}
0 commit comments