Skip to content

Commit a033ac2

Browse files
committed
index is left as it was, getWtxid is more an utils than part of the index file
1 parent 252f877 commit a033ac2

File tree

6 files changed

+30
-31
lines changed

6 files changed

+30
-31
lines changed

index.d.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,3 @@
44
* @param filteredHash: transaction hash
55
*/
66
export function buildPMT(leaves: string[], filteredHash: string): { totalTX: number, hashes: string[], flags: number, hex: string }
7-
8-
/**
9-
*
10-
* @param txid: transaction id
11-
*/
12-
export function getWtxid(txid: string): Promise<string>;

index.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const crypto = require('crypto');
2-
const bitcoin = require('bitcoinjs-lib');
32

43
const lpad = (number, length) => number.toString().padStart(length, '0');
54

@@ -20,12 +19,6 @@ const combineLeftAndRight = (left, right) => {
2019
return bufHashed.toString('hex');
2120
};
2221

23-
const getWtxid = async (rawTx) => {
24-
const tx = bitcoin.Transaction.fromHex(rawTx);
25-
const wtxid = tx.getHash(true).reverse().toString('hex');
26-
return wtxid;
27-
}
28-
2922
const buildPMT = (leaves, filteredHash) => {
3023
const matches = [];
3124
let filteredInLeaves = false;
@@ -116,4 +109,4 @@ const buildPMT = (leaves, filteredHash) => {
116109
};
117110
};
118111

119-
module.exports = { buildPMT, getWtxid };
112+
module.exports = { buildPMT };

test/test.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
let expect = require('chai').expect;
2-
const { buildPMT, getWtxid } = require('../index');
1+
const expect = require('chai').expect;
2+
const pmtBuilder = require('../index');
33
const txs3000 = require('./resources/3000-txs');
4+
const { getWtxid } = require('../tool/pmt-builder-utils');
45

56
describe('PMT Builder', () => {
67
it('should create a valid PMT, block with a single transaction', () => {
78
let blockTransactions = [
89
'8f01832aa125683490e70a9142ccd9c49485b84708180487b5f35dc7795a3afd'
910
];
1011

11-
let resultPmt = buildPMT(blockTransactions, blockTransactions[0]);
12+
let resultPmt = pmtBuilder.buildPMT(blockTransactions, blockTransactions[0]);
1213
let expectedPMT = '0100000001fd3a5a79c75df3b58704180847b88594c4d9cc42910ae790346825a12a83018f0101';
1314

1415
expect(resultPmt.hex).to.be.eq(expectedPMT);
@@ -35,7 +36,7 @@ describe('PMT Builder', () => {
3536
];
3637

3738
for (let i = 0; i < blockTransactions.length; i++) {
38-
let resultPmt = buildPMT(blockTransactions, blockTransactions[i]);
39+
let resultPmt = pmtBuilder.buildPMT(blockTransactions, blockTransactions[i]);
3940
expect(resultPmt.hex).to.be.eq(expectedPMTs[i]);
4041
}
4142
});
@@ -59,7 +60,7 @@ describe('PMT Builder', () => {
5960
];
6061

6162
for (let i = 0; i < blockTransactions.length; i++) {
62-
let resultPmt = buildPMT(blockTransactions, blockTransactions[i]);
63+
let resultPmt = pmtBuilder.buildPMT(blockTransactions, blockTransactions[i]);
6364
expect(resultPmt.hex).to.be.eq(expectedPMTs[i]);
6465
}
6566
});
@@ -68,29 +69,29 @@ describe('PMT Builder', () => {
6869
const blockTransactions = txs3000.transactions;
6970

7071
// Test with a few different filtered transactions
71-
let resultPmt = buildPMT(blockTransactions, blockTransactions[0]);
72+
let resultPmt = pmtBuilder.buildPMT(blockTransactions, blockTransactions[0]);
7273
let expectedPMT = 'de0c00000d68101e92ce19aec6ca95e5f56b85c4f344ee89a49db4ed87e92d9e8845744eba72f5e40cff226665b5188733db71bc0026f723e2e7b1431e7a3f3da748fffe50045b6f9b0ad4b984d0cbfff9bc3fb936c4198055d7b9cea22f2f585a762124ccb3eb0955ba3da948730a6168a4ca278b0d80ce6bf6762f677eb14267dba84adb0bb3919c4648ca9cbdec22ddd15972b62e72c260fdd1bf8fca7b7dc29db20b8c011b8fde551f618df69b1b9f46da8cdfac68d90e0f019aef8f77226f73a534b059746c76b28b948a85a5332c9fb24a9682c4258cb67ff929fcf7bdd16591967e1c94e125f2465d7f882619da113bf0148fe1f6906cdc1f7c69d536b7bb372b33d0ed03f63e87db16bebb6e5a34353249a91f6d5c3474ae2e3218d107a3010a3407b0cd0bee035791b95b4544ac85e75e0aafb27ddc5f0aacb32cfed423f675e39d73cf2baaa20629c7997faa778e6333f0f89f07e9756977d70c946b642c57c1be886674fa7ffefb1a421ef02abe3aba989f33d76613249c8822c57df60ab5910641d0dfe22326e32f50af51df55af13010d7a341d63e523fc684e7a2e92d48a04ff1f0000';
7374
expect(resultPmt.hex).to.be.eq(expectedPMT);
7475

75-
resultPmt = buildPMT(blockTransactions, blockTransactions[1]);
76+
resultPmt = pmtBuilder.buildPMT(blockTransactions, blockTransactions[1]);
7677
expectedPMT = 'de0c00000d68101e92ce19aec6ca95e5f56b85c4f344ee89a49db4ed87e92d9e8845744eba72f5e40cff226665b5188733db71bc0026f723e2e7b1431e7a3f3da748fffe50045b6f9b0ad4b984d0cbfff9bc3fb936c4198055d7b9cea22f2f585a762124ccb3eb0955ba3da948730a6168a4ca278b0d80ce6bf6762f677eb14267dba84adb0bb3919c4648ca9cbdec22ddd15972b62e72c260fdd1bf8fca7b7dc29db20b8c011b8fde551f618df69b1b9f46da8cdfac68d90e0f019aef8f77226f73a534b059746c76b28b948a85a5332c9fb24a9682c4258cb67ff929fcf7bdd16591967e1c94e125f2465d7f882619da113bf0148fe1f6906cdc1f7c69d536b7bb372b33d0ed03f63e87db16bebb6e5a34353249a91f6d5c3474ae2e3218d107a3010a3407b0cd0bee035791b95b4544ac85e75e0aafb27ddc5f0aacb32cfed423f675e39d73cf2baaa20629c7997faa778e6333f0f89f07e9756977d70c946b642c57c1be886674fa7ffefb1a421ef02abe3aba989f33d76613249c8822c57df60ab5910641d0dfe22326e32f50af51df55af13010d7a341d63e523fc684e7a2e92d48a04ff2f0000';
7778
expect(resultPmt.hex).to.be.eq(expectedPMT);
7879

79-
resultPmt = buildPMT(blockTransactions, blockTransactions[1001]);
80+
resultPmt = pmtBuilder.buildPMT(blockTransactions, blockTransactions[1001]);
8081
expectedPMT = 'de0c00000d6c8d31cff7da0e4d9397d95d8f8f97522573354ac082ca3597fd14786dad4f7c58c28161414ee3d97c150d4aa53d0e7d9837e22c706ae11bff5c5c17a61bbe8ad39e07934c9619a3de2fc7512bb6fdabcfb02faf4d2b895bc324306463f20aa449731157a69afb1f07ca0071a7085972969fb24fb05c1be6955d6e50c8fd784709b5fb480b70c492a4b7aa353a2fb33332bddc1cb32f3da901f4093e7abbf51412805c24faf250e8bd847cb64473cb76726c9164cb6436facdc429fdb711776f2e296d80dc81462a8623565625ce42aabe4fe3deec6b99b73cd91e085a5000f888fbfc50f51f6103c0bb04070df9357c86140b07afe7d3a739b9189d10620ba48e50490597f8dbf57425e4b2846f9eb2e4bf9a97007e0f7a098042f519f81eaa30c6dc1b51c4b47f0179bec32aa463920fe2f621d14f256380db3e9f865f298a3251d9245f1569c2a1ab28faa7bc1a2a8302b0764cbfe15a9ebbb2dff56707a9be886674fa7ffefb1a421ef02abe3aba989f33d76613249c8822c57df60ab5910641d0dfe22326e32f50af51df55af13010d7a341d63e523fc684e7a2e92d48a0457b50b00';
8182
expect(resultPmt.hex).to.be.eq(expectedPMT);
8283

83-
resultPmt = buildPMT(blockTransactions, blockTransactions[2800]);
84+
resultPmt = pmtBuilder.buildPMT(blockTransactions, blockTransactions[2800]);
8485
expectedPMT = 'de0c00000d6865fa6df021b3ca9828486804e65da669ad6e728445b2dbc50908292d5fc0f8eeb2ce57b14ab3a0adc4a5a2c5126f779f42446c7c043db25fc30bc7d52732576963fc20e7c0eeb0cfed973437fd2a05c67de93c0bbb1d18c1ddbd85b21bb3200035751bac441dcb24e9c4dbd70c808b6a4f6388126f5307363695b57faf921db8b0f6440f33054fb73960fe58d8913b930964fa3f5e6e1cc6b8a38be946b9774f2b4ac8a267ef7d2980693ccb5a6a100a4122495edecfbe0d5efb5d65e31906432fe4e341c92c294a7e0c5f9d16e95630c974cefca5c9358aa639dd3de8dc0f3ec2cf204700bcfc8b672b8469701fdfdf16bc4d2e3c81ce71ef6ba37507b1df22238fd61ec2ddc7d204804e20f8330e51f2bcbdb7717b3b1d22f1f6f1ed2673b962542a1376fa6301d70fc8745a3f2739822a6efc140da1c41e784f3a8e797e5d4c58fd77bc94d3d305a198b2e4ff64123221c660bb4b616c4edb62e4de47dce6eb07494a65c8bab19acd445c856ea8a8573fcad310e1fd385dee4ef6c82755ac03bc893f866fdb79b43f92aea03883b8d9982159264b4494373f38aab268c8046dd50700';
8586
expect(resultPmt.hex).to.be.eq(expectedPMT);
8687

87-
resultPmt = buildPMT(blockTransactions, blockTransactions[3293]);
88+
resultPmt = pmtBuilder.buildPMT(blockTransactions, blockTransactions[3293]);
8889
expectedPMT = 'de0c0000096865fa6df021b3ca9828486804e65da669ad6e728445b2dbc50908292d5fc0f8f24cc35c45d5c556d711052239df3351f9c6ec06807b22819465c002da362210c23c429ab578a1574473be7231bb6d4863a9c544bf706a765a5ba4e2a428f77c64431ad750d8be98892b6098eb1a45dfdc864354e1d9eb0b692d5e02b19ff10a846a36d9ba8586f9f4a251b9cf1583e728a2c0234ba0f8531bd783d09ecbed0f094b478a03f283748f97761d0bd0c0fe50774baa221970bca5bcd17630f02700d69ba542836e4f35e7f931df29e60706c40a51ffb3f1628026b27ffef37342c323328a28ab35a0af61b5788cc6d5b64f4605a3e72f5bae344255d0d3036593e5a830e0e0dfaa09281757253c3925e878a8c27057fe0ecc5dfd26c484025f5fca0375ad16';
8990
expect(resultPmt.hex).to.be.eq(expectedPMT);
9091

9192
// fail because the filtered transaction is not part of the transactions list
9293
let randomHash = '1000000000000000000000000000000000000000000000000000000000000001'
93-
expect(() => buildPMT(blockTransactions, randomHash)).to.throw('Filtered hash provided is not part of the leaves');
94+
expect(() => pmtBuilder.buildPMT(blockTransactions, randomHash)).to.throw('Filtered hash provided is not part of the leaves');
9495
});
9596

9697
it('should fail when passing a filtered transaction that is not part of the block', () => {
@@ -106,7 +107,7 @@ describe('PMT Builder', () => {
106107
let randomHash = '1000000000000000000000000000000000000000000000000000000000000001'
107108

108109
// fail because the filtered transaction is not part of the transactions list
109-
expect(() => buildPMT(blockTransactions, randomHash)).to.throw('Filtered hash provided is not part of the leaves');
110+
expect(() => pmtBuilder.buildPMT(blockTransactions, randomHash)).to.throw('Filtered hash provided is not part of the leaves');
110111
});
111112
});
112113

tool/getInformationReadyForRegisterBtcTransaction.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const mempoolJS = require("@mempool/mempool.js");
2-
const { buildPMT, getWtxid } = require("../index");
2+
const pmtBuilder = require("../index");
3+
const { getWtxid } = require("./pmt-builder-utils");
34

45
const getInformationReadyForRegisterBtcTransaction = async (transactionHash, network) => {
56

@@ -23,11 +24,11 @@ const getInformationReadyForRegisterBtcTransaction = async (transactionHash, net
2324
blockTxWids.push(wTxId);
2425
}
2526

26-
const resultPmt = buildPMT(blockTxids, transactionHash);
27+
const resultPmt = pmtBuilder.buildPMT(blockTxids, transactionHash);
2728
const pmt = resultPmt.hex;
2829

2930
const targetTxWTxId = await getWtxid(rawBtcTransaction);
30-
const resultPmtConsideringWitness = buildPMT(blockTxWids, targetTxWTxId);
31+
const resultPmtConsideringWitness = pmtBuilder.buildPMT(blockTxWids, targetTxWTxId);
3132
const pmtConsideringWitness = resultPmtConsideringWitness.hex;
3233

3334
const informationReadyForRegisterBtcTransaction = {

tool/pmt-builder-utils.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const bitcoin = require('bitcoinjs-lib');
2+
3+
const getWtxid = async (rawTx) => {
4+
const tx = bitcoin.Transaction.fromHex(rawTx);
5+
const wtxid = tx.getHash(true).reverse().toString('hex');
6+
return wtxid;
7+
}
8+
9+
module.exports = { getWtxid };

tool/pmt-witness-builder.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const mempoolJS = require("@mempool/mempool.js");
2-
const { buildPMT, getWtxid } = require("../index");
2+
const pmtBuilder = require("../index");
3+
const { getWtxid } = require("./pmt-builder-utils");
34

45
const getPmtInformationWithWitness = async (network, txHash) => {
56
const { bitcoin: { blocks, transactions } } = mempoolJS({
@@ -21,7 +22,7 @@ const getPmtInformationWithWitness = async (network, txHash) => {
2122

2223
const rawTx = await transactions.getTxHex({ txid: txHash });
2324
const targetTxWTxId = await getWtxid(rawTx);
24-
const resultPmt = buildPMT(blockWtxids, targetTxWTxId);
25+
const resultPmt = pmtBuilder.buildPMT(blockWtxids, targetTxWTxId);
2526

2627
return resultPmt;
2728
};

0 commit comments

Comments
 (0)