Skip to content

Commit 732df83

Browse files
committed
tests/integration: simplify the bare witness examples
1 parent b7e7d87 commit 732df83

File tree

2 files changed

+68
-69
lines changed

2 files changed

+68
-69
lines changed

test/integration/_regtest.js

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const dhttp = require('dhttp/200')
44

55
const APIPASS = process.env.APIPASS || 'satoshi'
66
const APIURL = 'https://api.dcousens.cloud/1'
7+
const NETWORK = bitcoin.networks.testnet
78

89
function broadcast (txHex, callback) {
910
dhttp({
@@ -42,6 +43,31 @@ function faucet (address, value, callback) {
4243
})
4344
}
4445

46+
function faucetComplex (output, value, callback) {
47+
const keyPair = bitcoin.ECPair.makeRandom({ network: NETWORK })
48+
const p2pkh = bitcoin.payments.p2pkh({ pubkey: keyPair.publicKey, network: NETWORK })
49+
50+
faucet(p2pkh.address, value * 2, (err, unspent) => {
51+
if (err) return callback(err)
52+
53+
const txvb = new bitcoin.TransactionBuilder(NETWORK)
54+
txvb.addInput(unspent.txId, unspent.vout, null, p2pkh.output)
55+
txvb.addOutput(output, value)
56+
txvb.sign(0, keyPair)
57+
const txv = txvb.build()
58+
59+
broadcast(txv.toHex(), function (err) {
60+
if (err) return callback(err)
61+
62+
return callback(null, {
63+
txId: txv.getId(),
64+
vout: 0,
65+
value
66+
})
67+
})
68+
})
69+
}
70+
4571
function fetch (txId, callback) {
4672
dhttp({
4773
method: 'GET',
@@ -78,14 +104,15 @@ function randomAddress () {
78104
}
79105

80106
module.exports = {
81-
broadcast: broadcast,
82-
faucet: faucet,
83-
fetch: fetch,
84-
height: height,
85-
mine: mine,
86-
network: bitcoin.networks.testnet,
87-
unspents: unspents,
88-
verify: verify,
89-
randomAddress: randomAddress,
107+
broadcast,
108+
faucet,
109+
faucetComplex,
110+
fetch,
111+
height,
112+
mine,
113+
network: NETWORK,
114+
unspents,
115+
verify,
116+
randomAddress,
90117
RANDOM_ADDRESS: randomAddress()
91118
}

test/integration/transactions.js

Lines changed: 32 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -171,91 +171,63 @@ describe('bitcoinjs-lib (transactions)', function () {
171171
})
172172
})
173173

174-
it('can create (and broadcast via 3PBP) a Transaction, w/ a P2WPKH input (via a P2SH(P2WPKH) transaction)', function (done) {
174+
it('can create (and broadcast via 3PBP) a Transaction, w/ a P2WPKH input', function (done) {
175175
this.timeout(30000)
176176

177177
const keyPair = bitcoin.ECPair.makeRandom({ network: regtest })
178178
const p2wpkh = bitcoin.payments.p2wpkh({ pubkey: keyPair.publicKey, network: regtest })
179179

180-
// prepare a P2SH(P2WPKH) faucet transaction, as Bitcoin-core doesn't support bare P2WPKH outputs (...yet)
181-
const p2sh = bitcoin.payments.p2sh({ redeem: p2wpkh, network: regtest })
182-
183-
regtestUtils.faucet(p2sh.address, 10e4, function (err, unspent) {
180+
regtestUtils.faucetComplex(p2wpkh.address, 5e4, function (err, unspent) {
184181
if (err) return done(err)
185182

186-
const txvb = new bitcoin.TransactionBuilder(regtest)
187-
txvb.addInput(unspent.txId, unspent.vout)
188-
txvb.addOutput(p2wpkh.address, 6e4) // funds a P2WPKH address
189-
txvb.sign(0, keyPair, p2sh.redeem.output, null, unspent.value)
190-
const txv = txvb.build()
183+
// XXX: build the Transaction w/ a P2WPKH input
184+
const txb = new bitcoin.TransactionBuilder(regtest)
185+
txb.addInput(unspent.txId, unspent.vout, null, p2wpkh.output) // NOTE: provide the prevOutScript!
186+
txb.addOutput(regtestUtils.RANDOM_ADDRESS, 2e4)
187+
txb.sign(0, keyPair, null, null, unspent.value) // NOTE: no redeem script
188+
const tx = txb.build()
191189

192-
// build and broadcast (the via transaction) to the Bitcoin RegTest network
193-
regtestUtils.broadcast(txv.toHex(), function (err) {
190+
// build and broadcast (the P2WPKH transaction) to the Bitcoin RegTest network
191+
regtestUtils.broadcast(tx.toHex(), function (err) {
194192
if (err) return done(err)
195193

196-
// XXX: build the Transaction w/ a P2WPKH input
197-
const txb = new bitcoin.TransactionBuilder(regtest)
198-
txb.addInput(txv.getId(), 0, null, p2wpkh.output) // NOTE: provide the prevOutScript!
199-
txb.addOutput(regtestUtils.RANDOM_ADDRESS, 2e4)
200-
txb.sign(0, keyPair, null, null, 6e4) // NOTE: no redeem script
201-
const tx = txb.build()
202-
203-
// build and broadcast (the P2WPKH transaction) to the Bitcoin RegTest network
204-
regtestUtils.broadcast(tx.toHex(), function (err) {
205-
if (err) return done(err)
206-
207-
regtestUtils.verify({
208-
txId: tx.getId(),
209-
address: regtestUtils.RANDOM_ADDRESS,
210-
vout: 0,
211-
value: 2e4
212-
}, done)
213-
})
194+
regtestUtils.verify({
195+
txId: tx.getId(),
196+
address: regtestUtils.RANDOM_ADDRESS,
197+
vout: 0,
198+
value: 2e4
199+
}, done)
214200
})
215201
})
216202
})
217203

218-
it('can create (and broadcast via 3PBP) a Transaction, w/ a P2WSH(P2PK) input (via a P2SH(P2PK) transaction)', function (done) {
204+
it('can create (and broadcast via 3PBP) a Transaction, w/ a P2WSH(P2PK) input', function (done) {
219205
this.timeout(30000)
220206

221207
const keyPair = bitcoin.ECPair.makeRandom({ network: regtest })
222208
const p2pk = bitcoin.payments.p2pk({ pubkey: keyPair.publicKey, network: regtest })
223209
const p2wsh = bitcoin.payments.p2wsh({ redeem: p2pk, network: regtest })
224210

225-
// prepare a P2SH(P2PK) faucet transaction, as Bitcoin-core doesn't support bare P2WSH outputs (...yet)
226-
const p2sh = bitcoin.payments.p2sh({ redeem: p2pk, network: regtest })
227-
228-
regtestUtils.faucet(p2sh.address, 10e4, function (err, unspent) {
211+
regtestUtils.faucetComplex(p2wsh.address, 5e4, function (err, unspent) {
229212
if (err) return done(err)
230213

231-
const txvb = new bitcoin.TransactionBuilder(regtest)
232-
txvb.addInput(unspent.txId, unspent.vout)
233-
txvb.addOutput(p2wsh.address, 6e4) // funds a P2WPKH address
234-
txvb.sign(0, keyPair, p2sh.redeem.output)
235-
const txv = txvb.build()
214+
// XXX: build the Transaction w/ a P2WSH input
215+
const txb = new bitcoin.TransactionBuilder(regtest)
216+
txb.addInput(unspent.txId, unspent.vout, null, p2wsh.output) // NOTE: provide the prevOutScript!
217+
txb.addOutput(regtestUtils.RANDOM_ADDRESS, 2e4)
218+
txb.sign(0, keyPair, null, null, 5e4, p2wsh.redeem.output) // NOTE: provide a witnessScript!
219+
const tx = txb.build()
236220

237-
// build and broadcast (the via transaction) to the Bitcoin RegTest network
238-
regtestUtils.broadcast(txv.toHex(), function (err) {
221+
// build and broadcast (the P2WSH transaction) to the Bitcoin RegTest network
222+
regtestUtils.broadcast(tx.toHex(), function (err) {
239223
if (err) return done(err)
240224

241-
// XXX: build the Transaction w/ a P2WSH input
242-
const txb = new bitcoin.TransactionBuilder(regtest)
243-
txb.addInput(txv.getId(), 0, null, p2wsh.output) // NOTE: provide the prevOutScript!
244-
txb.addOutput(regtestUtils.RANDOM_ADDRESS, 2e4)
245-
txb.sign(0, keyPair, null, null, 6e4, p2wsh.redeem.output) // NOTE: provide a witnessScript!
246-
const tx = txb.build()
247-
248-
// build and broadcast (the P2WSH transaction) to the Bitcoin RegTest network
249-
regtestUtils.broadcast(tx.toHex(), function (err) {
250-
if (err) return done(err)
251-
252-
regtestUtils.verify({
253-
txId: tx.getId(),
254-
address: regtestUtils.RANDOM_ADDRESS,
255-
vout: 0,
256-
value: 2e4
257-
}, done)
258-
})
225+
regtestUtils.verify({
226+
txId: tx.getId(),
227+
address: regtestUtils.RANDOM_ADDRESS,
228+
vout: 0,
229+
value: 2e4
230+
}, done)
259231
})
260232
})
261233
})

0 commit comments

Comments
 (0)