Skip to content

Commit f376913

Browse files
committed
Remove TransactionBuilder from tests (besides transaction_builder.spec.ts)
1 parent 603b0eb commit f376913

File tree

6 files changed

+631
-1054
lines changed

6 files changed

+631
-1054
lines changed

README.md

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,6 @@ The below examples are implemented as integration tests, they should be very eas
8585
Otherwise, pull requests are appreciated.
8686
Some examples interact (via HTTPS) with a 3rd Party Blockchain Provider (3PBP).
8787

88-
### Warning: Currently the tests use TransactionBuilder, which will be removed in the future (v6.x.x or higher)
89-
We will move towards replacing all instances of TransactionBuilder in the tests with the new Psbt.
90-
91-
Currently we have a few examples on how to use the newer Psbt class at the following link:
92-
- [Psbt examples](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions-psbt.spec.ts)
93-
94-
The rest of the examples are below (using TransactionBuilder for Transaction creation)
95-
9688
- [Generate a random address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/addresses.spec.ts)
9789
- [Import an address via WIF](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/addresses.spec.ts)
9890
- [Generate a 2-of-3 P2SH multisig address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/addresses.spec.ts)
@@ -104,15 +96,14 @@ The rest of the examples are below (using TransactionBuilder for Transaction cre
10496
- [Generate a Testnet address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/addresses.spec.ts)
10597
- [Generate a Litecoin address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/addresses.spec.ts)
10698
- [Create a 1-to-1 Transaction](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.spec.ts)
107-
- [Create a 2-to-2 Transaction](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.spec.ts)
10899
- [Create (and broadcast via 3PBP) a typical Transaction](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.spec.ts)
109100
- [Create (and broadcast via 3PBP) a Transaction with an OP\_RETURN output](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.spec.ts)
110101
- [Create (and broadcast via 3PBP) a Transaction with a 2-of-4 P2SH(multisig) input](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.spec.ts)
111102
- [Create (and broadcast via 3PBP) a Transaction with a SegWit P2SH(P2WPKH) input](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.spec.ts)
112103
- [Create (and broadcast via 3PBP) a Transaction with a SegWit P2WPKH input](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.spec.ts)
113104
- [Create (and broadcast via 3PBP) a Transaction with a SegWit P2PK input](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.spec.ts)
114105
- [Create (and broadcast via 3PBP) a Transaction with a SegWit 3-of-4 P2SH(P2WSH(multisig)) input](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.spec.ts)
115-
- [Verify a Transaction signature](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.spec.ts)
106+
- [Create (and broadcast via 3PBP) a Transaction and sign with an HDSigner interface (bip32)](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.spec.ts)
116107
- [Import a BIP32 testnet xpriv and export to WIF](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/bip32.spec.ts)
117108
- [Export a BIP32 xpriv, then import it](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/bip32.spec.ts)
118109
- [Export a BIP32 xpub](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/bip32.spec.ts)

test/integration/cltv.spec.ts

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ import { regtestUtils } from './_regtest';
55
const regtest = regtestUtils.network;
66
const bip65 = require('bip65');
77

8+
function toOutputScript(address: string): Buffer {
9+
return bitcoin.address.toOutputScript(address, regtest);
10+
}
11+
12+
function idToHash(txid: string): Buffer {
13+
return Buffer.from(txid, 'hex').reverse() as Buffer;
14+
}
15+
816
const alice = bitcoin.ECPair.fromWIF(
917
'cScfkGjbzzoeewVWmU2hYPUHeVGJRDdFt7WhmrVVGkxpmPP8BHWe',
1018
regtest,
@@ -13,7 +21,6 @@ const bob = bitcoin.ECPair.fromWIF(
1321
'cMkopUXKWsEzAjfa1zApksGRwjVpJRB3831qM9W4gKZsLwjHXA9x',
1422
regtest,
1523
);
16-
console.warn = () => {}; // Silence the Deprecation Warning
1724

1825
describe('bitcoinjs-lib (transactions w/ CLTV)', () => {
1926
// force update MTP
@@ -59,14 +66,13 @@ describe('bitcoinjs-lib (transactions w/ CLTV)', () => {
5966

6067
// fund the P2SH(CLTV) address
6168
const unspent = await regtestUtils.faucet(address!, 1e5);
62-
const txb = new bitcoin.TransactionBuilder(regtest);
63-
txb.setLockTime(lockTime);
69+
const tx = new bitcoin.Transaction();
70+
tx.locktime = lockTime;
6471
// Note: nSequence MUST be <= 0xfffffffe otherwise LockTime is ignored, and is immediately spendable.
65-
txb.addInput(unspent.txId, unspent.vout, 0xfffffffe);
66-
txb.addOutput(regtestUtils.RANDOM_ADDRESS, 7e4);
72+
tx.addInput(idToHash(unspent.txId), unspent.vout, 0xfffffffe);
73+
tx.addOutput(toOutputScript(regtestUtils.RANDOM_ADDRESS), 7e4);
6774

6875
// {Alice's signature} OP_TRUE
69-
const tx = txb.buildIncomplete();
7076
const signatureHash = tx.hashForSignature(0, redeemScript, hashType);
7177
const redeemScriptSig = bitcoin.payments.p2sh({
7278
redeem: {
@@ -102,14 +108,13 @@ describe('bitcoinjs-lib (transactions w/ CLTV)', () => {
102108

103109
// fund the P2SH(CLTV) address
104110
const unspent = await regtestUtils.faucet(address!, 1e5);
105-
const txb = new bitcoin.TransactionBuilder(regtest);
106-
txb.setLockTime(lockTime);
111+
const tx = new bitcoin.Transaction();
112+
tx.locktime = lockTime;
107113
// Note: nSequence MUST be <= 0xfffffffe otherwise LockTime is ignored, and is immediately spendable.
108-
txb.addInput(unspent.txId, unspent.vout, 0xfffffffe);
109-
txb.addOutput(regtestUtils.RANDOM_ADDRESS, 7e4);
114+
tx.addInput(idToHash(unspent.txId), unspent.vout, 0xfffffffe);
115+
tx.addOutput(toOutputScript(regtestUtils.RANDOM_ADDRESS), 7e4);
110116

111117
// {Alice's signature} OP_TRUE
112-
const tx = txb.buildIncomplete();
113118
const signatureHash = tx.hashForSignature(0, redeemScript, hashType);
114119
const redeemScriptSig = bitcoin.payments.p2sh({
115120
redeem: {
@@ -147,14 +152,13 @@ describe('bitcoinjs-lib (transactions w/ CLTV)', () => {
147152

148153
// fund the P2SH(CLTV) address
149154
const unspent = await regtestUtils.faucet(address!, 2e5);
150-
const txb = new bitcoin.TransactionBuilder(regtest);
151-
txb.setLockTime(lockTime);
155+
const tx = new bitcoin.Transaction();
156+
tx.locktime = lockTime;
152157
// Note: nSequence MUST be <= 0xfffffffe otherwise LockTime is ignored, and is immediately spendable.
153-
txb.addInput(unspent.txId, unspent.vout, 0xfffffffe);
154-
txb.addOutput(regtestUtils.RANDOM_ADDRESS, 8e4);
158+
tx.addInput(idToHash(unspent.txId), unspent.vout, 0xfffffffe);
159+
tx.addOutput(toOutputScript(regtestUtils.RANDOM_ADDRESS), 8e4);
155160

156161
// {Alice's signature} {Bob's signature} OP_FALSE
157-
const tx = txb.buildIncomplete();
158162
const signatureHash = tx.hashForSignature(0, redeemScript, hashType);
159163
const redeemScriptSig = bitcoin.payments.p2sh({
160164
redeem: {
@@ -189,14 +193,13 @@ describe('bitcoinjs-lib (transactions w/ CLTV)', () => {
189193

190194
// fund the P2SH(CLTV) address
191195
const unspent = await regtestUtils.faucet(address!, 2e4);
192-
const txb = new bitcoin.TransactionBuilder(regtest);
193-
txb.setLockTime(lockTime);
196+
const tx = new bitcoin.Transaction();
197+
tx.locktime = lockTime;
194198
// Note: nSequence MUST be <= 0xfffffffe otherwise LockTime is ignored, and is immediately spendable.
195-
txb.addInput(unspent.txId, unspent.vout, 0xfffffffe);
196-
txb.addOutput(regtestUtils.RANDOM_ADDRESS, 1e4);
199+
tx.addInput(idToHash(unspent.txId), unspent.vout, 0xfffffffe);
200+
tx.addOutput(toOutputScript(regtestUtils.RANDOM_ADDRESS), 1e4);
197201

198202
// {Alice's signature} OP_TRUE
199-
const tx = txb.buildIncomplete();
200203
const signatureHash = tx.hashForSignature(0, redeemScript, hashType);
201204
const redeemScriptSig = bitcoin.payments.p2sh({
202205
redeem: {

test/integration/csv.spec.ts

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ import { regtestUtils } from './_regtest';
55
const regtest = regtestUtils.network;
66
const bip68 = require('bip68');
77

8+
function toOutputScript(address: string): Buffer {
9+
return bitcoin.address.toOutputScript(address, regtest);
10+
}
11+
12+
function idToHash(txid: string): Buffer {
13+
return Buffer.from(txid, 'hex').reverse() as Buffer;
14+
}
15+
816
const alice = bitcoin.ECPair.fromWIF(
917
'cScfkGjbzzoeewVWmU2hYPUHeVGJRDdFt7WhmrVVGkxpmPP8BHWe',
1018
regtest,
@@ -21,7 +29,6 @@ const dave = bitcoin.ECPair.fromWIF(
2129
'cMkopUXKWsEzAjfa1zApksGRwjVpJRB3831qM9W4gKZsMwS4pqnx',
2230
regtest,
2331
);
24-
console.warn = () => {}; // Silence the Deprecation Warning
2532

2633
describe('bitcoinjs-lib (transactions w/ CSV)', () => {
2734
// force update MTP
@@ -111,12 +118,12 @@ describe('bitcoinjs-lib (transactions w/ CSV)', () => {
111118
// fund the P2SH(CSV) address
112119
const unspent = await regtestUtils.faucet(p2sh.address!, 1e5);
113120

114-
const txb = new bitcoin.TransactionBuilder(regtest);
115-
txb.addInput(unspent.txId, unspent.vout, sequence);
116-
txb.addOutput(regtestUtils.RANDOM_ADDRESS, 7e4);
121+
const tx = new bitcoin.Transaction();
122+
tx.version = 2;
123+
tx.addInput(idToHash(unspent.txId), unspent.vout, sequence);
124+
tx.addOutput(toOutputScript(regtestUtils.RANDOM_ADDRESS), 7e4);
117125

118126
// {Alice's signature} OP_TRUE
119-
const tx = txb.buildIncomplete();
120127
const signatureHash = tx.hashForSignature(
121128
0,
122129
p2sh.redeem!.output!,
@@ -164,12 +171,12 @@ describe('bitcoinjs-lib (transactions w/ CSV)', () => {
164171
// fund the P2SH(CSV) address
165172
const unspent = await regtestUtils.faucet(p2sh.address!, 2e4);
166173

167-
const txb = new bitcoin.TransactionBuilder(regtest);
168-
txb.addInput(unspent.txId, unspent.vout, sequence);
169-
txb.addOutput(regtestUtils.RANDOM_ADDRESS, 1e4);
174+
const tx = new bitcoin.Transaction();
175+
tx.version = 2;
176+
tx.addInput(idToHash(unspent.txId), unspent.vout, sequence);
177+
tx.addOutput(toOutputScript(regtestUtils.RANDOM_ADDRESS), 1e4);
170178

171179
// {Alice's signature} OP_TRUE
172-
const tx = txb.buildIncomplete();
173180
const signatureHash = tx.hashForSignature(
174181
0,
175182
p2sh.redeem!.output!,
@@ -219,12 +226,12 @@ describe('bitcoinjs-lib (transactions w/ CSV)', () => {
219226
// fund the P2SH(CCSV) address
220227
const unspent = await regtestUtils.faucet(p2sh.address!, 1e5);
221228

222-
const txb = new bitcoin.TransactionBuilder(regtest);
223-
txb.addInput(unspent.txId, unspent.vout);
224-
txb.addOutput(regtestUtils.RANDOM_ADDRESS, 7e4);
229+
const tx = new bitcoin.Transaction();
230+
tx.version = 2;
231+
tx.addInput(idToHash(unspent.txId), unspent.vout);
232+
tx.addOutput(toOutputScript(regtestUtils.RANDOM_ADDRESS), 7e4);
225233

226234
// OP_0 {Bob sig} {Charles sig} OP_TRUE OP_TRUE
227-
const tx = txb.buildIncomplete();
228235
const signatureHash = tx.hashForSignature(
229236
0,
230237
p2sh.redeem!.output!,
@@ -282,12 +289,12 @@ describe('bitcoinjs-lib (transactions w/ CSV)', () => {
282289
// fund the P2SH(CCSV) address
283290
const unspent = await regtestUtils.faucet(p2sh.address!, 1e5);
284291

285-
const txb = new bitcoin.TransactionBuilder(regtest);
286-
txb.addInput(unspent.txId, unspent.vout, sequence1); // Set sequence1 for input
287-
txb.addOutput(regtestUtils.RANDOM_ADDRESS, 7e4);
292+
const tx = new bitcoin.Transaction();
293+
tx.version = 2;
294+
tx.addInput(idToHash(unspent.txId), unspent.vout, sequence1); // Set sequence1 for input
295+
tx.addOutput(toOutputScript(regtestUtils.RANDOM_ADDRESS), 7e4);
288296

289297
// OP_0 {Bob sig} {Alice mediator sig} OP_FALSE OP_TRUE
290-
const tx = txb.buildIncomplete();
291298
const signatureHash = tx.hashForSignature(
292299
0,
293300
p2sh.redeem!.output!,
@@ -345,12 +352,12 @@ describe('bitcoinjs-lib (transactions w/ CSV)', () => {
345352
// fund the P2SH(CCSV) address
346353
const unspent = await regtestUtils.faucet(p2sh.address!, 1e5);
347354

348-
const txb = new bitcoin.TransactionBuilder(regtest);
349-
txb.addInput(unspent.txId, unspent.vout, sequence2); // Set sequence2 for input
350-
txb.addOutput(regtestUtils.RANDOM_ADDRESS, 7e4);
355+
const tx = new bitcoin.Transaction();
356+
tx.version = 2;
357+
tx.addInput(idToHash(unspent.txId), unspent.vout, sequence2); // Set sequence2 for input
358+
tx.addOutput(toOutputScript(regtestUtils.RANDOM_ADDRESS), 7e4);
351359

352360
// {Alice mediator sig} OP_FALSE
353-
const tx = txb.buildIncomplete();
354361
const signatureHash = tx.hashForSignature(
355362
0,
356363
p2sh.redeem!.output!,

test/integration/payments.spec.ts

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const keyPairs = [
66
bitcoin.ECPair.makeRandom({ network: NETWORK }),
77
bitcoin.ECPair.makeRandom({ network: NETWORK }),
88
];
9-
console.warn = () => {}; // Silence the Deprecation Warning
109

1110
async function buildAndSign(
1211
depends: any,
@@ -15,37 +14,35 @@ async function buildAndSign(
1514
witnessScript: any,
1615
) {
1716
const unspent = await regtestUtils.faucetComplex(prevOutput, 5e4);
17+
const utx = await regtestUtils.fetch(unspent.txId);
1818

19-
const txb = new bitcoin.TransactionBuilder(NETWORK);
20-
txb.addInput(unspent.txId, unspent.vout, undefined, prevOutput);
21-
txb.addOutput(regtestUtils.RANDOM_ADDRESS, 2e4);
22-
23-
const posType = depends.prevOutScriptType;
24-
const needsValue = !!witnessScript || posType.slice(-6) === 'p2wpkh';
19+
const psbt = new bitcoin.Psbt({ network: NETWORK })
20+
.addInput({
21+
hash: unspent.txId,
22+
index: unspent.vout,
23+
nonWitnessUtxo: Buffer.from(utx.txHex, 'hex'),
24+
...(redeemScript ? { redeemScript } : {}),
25+
...(witnessScript ? { witnessScript } : {}),
26+
})
27+
.addOutput({
28+
address: regtestUtils.RANDOM_ADDRESS,
29+
value: 2e4,
30+
});
2531

2632
if (depends.signatures) {
2733
keyPairs.forEach(keyPair => {
28-
txb.sign({
29-
prevOutScriptType: posType,
30-
vin: 0,
31-
keyPair,
32-
redeemScript,
33-
witnessValue: needsValue ? unspent.value : undefined,
34-
witnessScript,
35-
});
34+
psbt.signInput(0, keyPair);
3635
});
3736
} else if (depends.signature) {
38-
txb.sign({
39-
prevOutScriptType: posType,
40-
vin: 0,
41-
keyPair: keyPairs[0],
42-
redeemScript,
43-
witnessValue: needsValue ? unspent.value : undefined,
44-
witnessScript,
45-
});
37+
psbt.signInput(0, keyPairs[0]);
4638
}
4739

48-
return regtestUtils.broadcast(txb.build().toHex());
40+
return regtestUtils.broadcast(
41+
psbt
42+
.finalizeAllInputs()
43+
.extractTransaction()
44+
.toHex(),
45+
);
4946
}
5047

5148
['p2ms', 'p2pk', 'p2pkh', 'p2wpkh'].forEach(k => {

0 commit comments

Comments
 (0)