tests/integration: simplify the bare witness examples

This commit is contained in:
Daniel Cousens 2018-07-18 14:00:53 +10:00
parent b7e7d879b3
commit 732df83346
2 changed files with 68 additions and 69 deletions

View file

@ -4,6 +4,7 @@ const dhttp = require('dhttp/200')
const APIPASS = process.env.APIPASS || 'satoshi' const APIPASS = process.env.APIPASS || 'satoshi'
const APIURL = 'https://api.dcousens.cloud/1' const APIURL = 'https://api.dcousens.cloud/1'
const NETWORK = bitcoin.networks.testnet
function broadcast (txHex, callback) { function broadcast (txHex, callback) {
dhttp({ dhttp({
@ -42,6 +43,31 @@ function faucet (address, value, callback) {
}) })
} }
function faucetComplex (output, value, callback) {
const keyPair = bitcoin.ECPair.makeRandom({ network: NETWORK })
const p2pkh = bitcoin.payments.p2pkh({ pubkey: keyPair.publicKey, network: NETWORK })
faucet(p2pkh.address, value * 2, (err, unspent) => {
if (err) return callback(err)
const txvb = new bitcoin.TransactionBuilder(NETWORK)
txvb.addInput(unspent.txId, unspent.vout, null, p2pkh.output)
txvb.addOutput(output, value)
txvb.sign(0, keyPair)
const txv = txvb.build()
broadcast(txv.toHex(), function (err) {
if (err) return callback(err)
return callback(null, {
txId: txv.getId(),
vout: 0,
value
})
})
})
}
function fetch (txId, callback) { function fetch (txId, callback) {
dhttp({ dhttp({
method: 'GET', method: 'GET',
@ -78,14 +104,15 @@ function randomAddress () {
} }
module.exports = { module.exports = {
broadcast: broadcast, broadcast,
faucet: faucet, faucet,
fetch: fetch, faucetComplex,
height: height, fetch,
mine: mine, height,
network: bitcoin.networks.testnet, mine,
unspents: unspents, network: NETWORK,
verify: verify, unspents,
randomAddress: randomAddress, verify,
randomAddress,
RANDOM_ADDRESS: randomAddress() RANDOM_ADDRESS: randomAddress()
} }

View file

@ -171,91 +171,63 @@ describe('bitcoinjs-lib (transactions)', function () {
}) })
}) })
it('can create (and broadcast via 3PBP) a Transaction, w/ a P2WPKH input (via a P2SH(P2WPKH) transaction)', function (done) { it('can create (and broadcast via 3PBP) a Transaction, w/ a P2WPKH input', function (done) {
this.timeout(30000) this.timeout(30000)
const keyPair = bitcoin.ECPair.makeRandom({ network: regtest }) const keyPair = bitcoin.ECPair.makeRandom({ network: regtest })
const p2wpkh = bitcoin.payments.p2wpkh({ pubkey: keyPair.publicKey, network: regtest }) const p2wpkh = bitcoin.payments.p2wpkh({ pubkey: keyPair.publicKey, network: regtest })
// prepare a P2SH(P2WPKH) faucet transaction, as Bitcoin-core doesn't support bare P2WPKH outputs (...yet) regtestUtils.faucetComplex(p2wpkh.address, 5e4, function (err, unspent) {
const p2sh = bitcoin.payments.p2sh({ redeem: p2wpkh, network: regtest })
regtestUtils.faucet(p2sh.address, 10e4, function (err, unspent) {
if (err) return done(err) if (err) return done(err)
const txvb = new bitcoin.TransactionBuilder(regtest) // XXX: build the Transaction w/ a P2WPKH input
txvb.addInput(unspent.txId, unspent.vout) const txb = new bitcoin.TransactionBuilder(regtest)
txvb.addOutput(p2wpkh.address, 6e4) // funds a P2WPKH address txb.addInput(unspent.txId, unspent.vout, null, p2wpkh.output) // NOTE: provide the prevOutScript!
txvb.sign(0, keyPair, p2sh.redeem.output, null, unspent.value) txb.addOutput(regtestUtils.RANDOM_ADDRESS, 2e4)
const txv = txvb.build() txb.sign(0, keyPair, null, null, unspent.value) // NOTE: no redeem script
const tx = txb.build()
// build and broadcast (the via transaction) to the Bitcoin RegTest network // build and broadcast (the P2WPKH transaction) to the Bitcoin RegTest network
regtestUtils.broadcast(txv.toHex(), function (err) { regtestUtils.broadcast(tx.toHex(), function (err) {
if (err) return done(err) if (err) return done(err)
// XXX: build the Transaction w/ a P2WPKH input regtestUtils.verify({
const txb = new bitcoin.TransactionBuilder(regtest) txId: tx.getId(),
txb.addInput(txv.getId(), 0, null, p2wpkh.output) // NOTE: provide the prevOutScript! address: regtestUtils.RANDOM_ADDRESS,
txb.addOutput(regtestUtils.RANDOM_ADDRESS, 2e4) vout: 0,
txb.sign(0, keyPair, null, null, 6e4) // NOTE: no redeem script value: 2e4
const tx = txb.build() }, done)
// build and broadcast (the P2WPKH transaction) to the Bitcoin RegTest network
regtestUtils.broadcast(tx.toHex(), function (err) {
if (err) return done(err)
regtestUtils.verify({
txId: tx.getId(),
address: regtestUtils.RANDOM_ADDRESS,
vout: 0,
value: 2e4
}, done)
})
}) })
}) })
}) })
it('can create (and broadcast via 3PBP) a Transaction, w/ a P2WSH(P2PK) input (via a P2SH(P2PK) transaction)', function (done) { it('can create (and broadcast via 3PBP) a Transaction, w/ a P2WSH(P2PK) input', function (done) {
this.timeout(30000) this.timeout(30000)
const keyPair = bitcoin.ECPair.makeRandom({ network: regtest }) const keyPair = bitcoin.ECPair.makeRandom({ network: regtest })
const p2pk = bitcoin.payments.p2pk({ pubkey: keyPair.publicKey, network: regtest }) const p2pk = bitcoin.payments.p2pk({ pubkey: keyPair.publicKey, network: regtest })
const p2wsh = bitcoin.payments.p2wsh({ redeem: p2pk, network: regtest }) const p2wsh = bitcoin.payments.p2wsh({ redeem: p2pk, network: regtest })
// prepare a P2SH(P2PK) faucet transaction, as Bitcoin-core doesn't support bare P2WSH outputs (...yet) regtestUtils.faucetComplex(p2wsh.address, 5e4, function (err, unspent) {
const p2sh = bitcoin.payments.p2sh({ redeem: p2pk, network: regtest })
regtestUtils.faucet(p2sh.address, 10e4, function (err, unspent) {
if (err) return done(err) if (err) return done(err)
const txvb = new bitcoin.TransactionBuilder(regtest) // XXX: build the Transaction w/ a P2WSH input
txvb.addInput(unspent.txId, unspent.vout) const txb = new bitcoin.TransactionBuilder(regtest)
txvb.addOutput(p2wsh.address, 6e4) // funds a P2WPKH address txb.addInput(unspent.txId, unspent.vout, null, p2wsh.output) // NOTE: provide the prevOutScript!
txvb.sign(0, keyPair, p2sh.redeem.output) txb.addOutput(regtestUtils.RANDOM_ADDRESS, 2e4)
const txv = txvb.build() txb.sign(0, keyPair, null, null, 5e4, p2wsh.redeem.output) // NOTE: provide a witnessScript!
const tx = txb.build()
// build and broadcast (the via transaction) to the Bitcoin RegTest network // build and broadcast (the P2WSH transaction) to the Bitcoin RegTest network
regtestUtils.broadcast(txv.toHex(), function (err) { regtestUtils.broadcast(tx.toHex(), function (err) {
if (err) return done(err) if (err) return done(err)
// XXX: build the Transaction w/ a P2WSH input regtestUtils.verify({
const txb = new bitcoin.TransactionBuilder(regtest) txId: tx.getId(),
txb.addInput(txv.getId(), 0, null, p2wsh.output) // NOTE: provide the prevOutScript! address: regtestUtils.RANDOM_ADDRESS,
txb.addOutput(regtestUtils.RANDOM_ADDRESS, 2e4) vout: 0,
txb.sign(0, keyPair, null, null, 6e4, p2wsh.redeem.output) // NOTE: provide a witnessScript! value: 2e4
const tx = txb.build() }, done)
// build and broadcast (the P2WSH transaction) to the Bitcoin RegTest network
regtestUtils.broadcast(tx.toHex(), function (err) {
if (err) return done(err)
regtestUtils.verify({
txId: tx.getId(),
address: regtestUtils.RANDOM_ADDRESS,
vout: 0,
value: 2e4
}, done)
})
}) })
}) })
}) })