Skip to content

Commit 202c5ca

Browse files
authored
fix: chainlock hash endianness (#208)
BREAKING CHANGE: ChainLock#hash is now big-endian
1 parent f8bef8a commit 202c5ca

File tree

4 files changed

+10
-11
lines changed

4 files changed

+10
-11
lines changed

lib/chainlock/chainlock.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class ChainLock {
6565
let blockHash = data.blockHash || data.blockhash;
6666
let { signature } = data;
6767
if (isString(blockHash)) {
68-
blockHash = BufferUtil.reverse(Buffer.from(blockHash, 'hex'));
68+
blockHash = Buffer.from(blockHash, 'hex');
6969
}
7070

7171
if (isString(data.signature)) {
@@ -86,7 +86,7 @@ class ChainLock {
8686
static _fromBufferReader(br) {
8787
const info = {};
8888
info.height = br.readInt32LE();
89-
info.blockHash = br.read(SHA256_HASH_SIZE);
89+
info.blockHash = br.readReverse(SHA256_HASH_SIZE);
9090
info.signature = br.read(BLS_SIGNATURE_SIZE);
9191
return info;
9292
}
@@ -215,7 +215,7 @@ class ChainLock {
215215
* @returns {Buffer}
216216
*/
217217
getHash() {
218-
return doubleSha256(this.toBuffer()).reverse();
218+
return doubleSha256(this.toBuffer());
219219
}
220220

221221
/**
@@ -268,7 +268,7 @@ class ChainLock {
268268
bufferWriter.writeUInt8(llmqType);
269269
bufferWriter.writeReverse(Buffer.from(quorumHash, 'hex'));
270270
bufferWriter.writeReverse(requestId);
271-
bufferWriter.write(blockHash);
271+
bufferWriter.writeReverse(blockHash);
272272
return doubleSha256(bufferWriter.toBuffer());
273273
}
274274

@@ -279,7 +279,7 @@ class ChainLock {
279279
toObject() {
280280
return {
281281
height: this.height,
282-
blockHash: BufferUtil.reverse(this.blockHash).toString('hex'),
282+
blockHash: this.blockHash.toString('hex'),
283283
signature: this.signature.toString('hex'),
284284
};
285285
}
@@ -315,7 +315,7 @@ class ChainLock {
315315
toBufferWriter(bw) {
316316
const bufferWriter = bw || new BufferWriter();
317317
bufferWriter.writeInt32LE(this.height);
318-
bufferWriter.write(this.blockHash);
318+
bufferWriter.write(Buffer.from(this.blockHash).reverse());
319319
bufferWriter.write(this.signature);
320320
return bufferWriter;
321321
}
@@ -334,8 +334,7 @@ class ChainLock {
334334
* @returns {string} ChainLock block hash and height
335335
*/
336336
inspect() {
337-
const reversedBlockHash = BufferUtil.reverse(this.blockHash).toString('hex');
338-
return `<ChainLock: ${reversedBlockHash}, height: ${this.height}>`;
337+
return `<ChainLock: ${this.blockHash.toString('hex')}, height: ${this.height}>`;
339338
}
340339
}
341340

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@dashevo/dashcore-lib",
3-
"version": "0.19.12",
3+
"version": "0.19.13",
44
"description": "A pure and powerful JavaScript Dash library.",
55
"author": "Dash Core Group, Inc. <dev@dash.org>",
66
"main": "index.js",

test/chainlock/chainlock.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ describe('ChainLock', function () {
5050
signature: '0a43f1c3e5b3e8dbd670bca8d437dc25572f72d8e1e9be673e9ebbb606570307c3e5f5d073f7beb209dd7e0b8f96c751060ab3a7fb69a71d5ccab697b8cfa5a91038a6fecf76b7a827d75d17f01496302942aa5e2c7f4a48246efc8d3941bf6c'
5151
};
5252
buf2 = Buffer.from(str2, 'hex');
53-
expectedHash2 = "3764ada6c32f09bb4f02295415b230657720f8be17d6fe046f0f8bf3db72b8e0";
53+
expectedHash2 = "e0b872dbf38b0f6f04fed617bef820776530b2155429024fbb092fc3a6ad6437";
5454
expectedRequestId2 = "5d92e094e2aa582b76e8bf519f42c5e8fc141bbe548e9660726f744adad03966";
5555

5656
// DashSync test vectors : https://github.com/dashevo/dashsync-iOS/blob/master/Example/Tests/DSChainLockTests.m

0 commit comments

Comments
 (0)