Skip to content

Commit 9c7ce94

Browse files
author
Konstantin Shuplenkov
authored
feat!: update InstantLock class to work with DashCore v0.18.0 (#239)
1 parent f348e7c commit 9c7ce94

File tree

4 files changed

+408
-59
lines changed

4 files changed

+408
-59
lines changed

lib/instantlock/instantlock.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const $ = require('../util/preconditions');
66
const constants = require('../constants');
77
const doubleSha256 = require('../crypto/hash').sha256sha256;
88

9-
const { isHexaString, isHexStringOfSize } = require('../util/js');
9+
const { isHexaString, isHexStringOfSize, isUnsignedInteger } = require('../util/js');
1010

1111
const { SHA256_HASH_SIZE, BLS_SIGNATURE_SIZE } = constants;
1212
const bls = require('../crypto/bls');
@@ -29,8 +29,10 @@ class InstantLock {
2929
}
3030
const info = InstantLock._from(arg);
3131

32+
this.version = info.version;
3233
this.inputs = info.inputs;
3334
this.txid = info.txid;
35+
this.cyclehash = info.cyclehash;
3436
this.signature = info.signature;
3537
this.validate();
3638
return this;
@@ -63,11 +65,14 @@ class InstantLock {
6365
static _fromObject(data) {
6466
$.checkArgument(data, 'data is required');
6567
const txid = data.txid || data.txId;
66-
const { signature } = data;
68+
const cyclehash = data.cyclehash || data.cycleHash;
69+
const { signature, version } = data;
6770

6871
return {
72+
version,
6973
inputs: data.inputs,
7074
txid,
75+
cyclehash,
7176
signature,
7277
};
7378
}
@@ -79,6 +84,7 @@ class InstantLock {
7984
*/
8085
static _fromBufferReader(br) {
8186
const info = {};
87+
info.version = br.readUInt8();
8288
const inputsCount = br.readVarintNum();
8389
info.inputs = [];
8490
for (let i = 0; i < inputsCount; i += 1) {
@@ -88,6 +94,7 @@ class InstantLock {
8894
info.inputs.push(outpoint);
8995
}
9096
info.txid = br.readReverse(SHA256_HASH_SIZE).toString('hex');
97+
info.cyclehash = br.readReverse(SHA256_HASH_SIZE).toString('hex');
9198
info.signature = br.read(BLS_SIGNATURE_SIZE).toString('hex');
9299
return info;
93100
}
@@ -215,6 +222,10 @@ class InstantLock {
215222
* Validate InstantLock structure
216223
*/
217224
validate() {
225+
$.checkArgument(
226+
isUnsignedInteger(this.version),
227+
"Expected version to be an unsigned integer"
228+
);
218229
$.checkArgument(
219230
this.inputs.length > 0,
220231
"TXs with no inputs can't be locked"
@@ -227,6 +238,10 @@ class InstantLock {
227238
isHexStringOfSize(this.txid.toString('hex'), SHA256_HASH_SIZE * 2),
228239
`Expected txid to be a hex string of size ${SHA256_HASH_SIZE}`
229240
);
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+
);
230245
$.checkArgument(
231246
isHexStringOfSize(this.signature.toString('hex'), BLS_SIGNATURE_SIZE * 2),
232247
'Expected signature to be a bls signature'
@@ -314,8 +329,10 @@ class InstantLock {
314329
toObject() {
315330
this.validate();
316331
return {
332+
version: this.version,
317333
inputs: this.inputs,
318334
txid: this.txid,
335+
cyclehash: this.cyclehash.toString('hex'),
319336
signature: this.signature.toString('hex'),
320337
};
321338
}
@@ -351,6 +368,7 @@ class InstantLock {
351368
*/
352369
toBufferWriter(bw) {
353370
const bufferWriter = bw || new BufferWriter();
371+
bufferWriter.writeUInt8(this.version);
354372
const inputsCount = this.inputs.length;
355373
bufferWriter.writeVarintNum(inputsCount);
356374
for (let i = 0; i < inputsCount; i += 1) {
@@ -360,6 +378,7 @@ class InstantLock {
360378
bufferWriter.writeInt32LE(this.inputs[i].outpointIndex);
361379
}
362380
bufferWriter.writeReverse(Buffer.from(this.txid, 'hex'));
381+
bufferWriter.writeReverse(Buffer.from(this.cyclehash, 'hex'));
363382
bufferWriter.write(Buffer.from(this.signature, 'hex'));
364383
return bufferWriter;
365384
}

0 commit comments

Comments
 (0)