Skip to content

Commit 84eea75

Browse files
authored
add better comments to the critical part for endian conversion (#43)
Signed-off-by: sczembor <stanislaw.czembor@gmail.com>
1 parent 490f06c commit 84eea75

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

packages/btcindexer/src/btcindexer.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,9 @@ export class Indexer {
258258
if (!block.transactions || block.transactions.length === 0) {
259259
return null;
260260
}
261+
// NOTE: The `merkletreejs` library expects its input leaves to be in big-endian format.
262+
// However, `tx.getHash()` from `bitcoinjs-lib` returns the little-endian format used by the Bitcoin protocol.
263+
// We must reverse each hash to convert it to big-endian before creating the tree.
261264
const leaves = block.transactions.map((tx) => Buffer.from(tx.getHash()).reverse());
262265
return new MerkleTree(leaves, SHA256, { isBitcoinTree: true });
263266
}

packages/btcindexer/src/sui_client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export class SuiClient {
4343
const target = `${this.nbtcPkg}::${this.nbtcModule}::mint` as const;
4444
const serializedTx = serializeBtcTx(transaction);
4545

46-
// NOTE: the contract is expecting the proofs to be in big-endian format, while the bitcon-js lib operates internally on little-endian.
46+
// NOTE: the contract is expecting the proofs to be in little-endian format, while the merkletreejs lib operates internally on big-endian.
4747
const proofBigEndian = proof.proofPath.map((p) => Array.from(Buffer.from(p).reverse()));
4848

4949
tx.moveCall({

0 commit comments

Comments
 (0)