bitcoinjs-lib/test/integration/payments.js

68 lines
2.5 KiB
JavaScript
Raw Normal View History

const bitcoin = require('../../')
2018-07-23 09:45:01 +02:00
const { describe, it } = require('mocha')
const regtestUtils = require('./_regtest')
const NETWORK = regtestUtils.network
const keyPairs = [
bitcoin.ECPair.makeRandom({ network: NETWORK }),
bitcoin.ECPair.makeRandom({ network: NETWORK })
]
2019-04-07 15:27:16 +02:00
async function buildAndSign (depends, prevOutput, redeemScript, witnessScript) {
const unspent = await regtestUtils.faucetComplex(prevOutput, 5e4)
2019-04-07 15:27:16 +02:00
const txb = new bitcoin.TransactionBuilder(NETWORK)
txb.addInput(unspent.txId, unspent.vout, null, prevOutput)
txb.addOutput(regtestUtils.RANDOM_ADDRESS, 2e4)
2019-04-07 15:27:16 +02:00
if (depends.signatures) {
keyPairs.forEach((keyPair) => {
txb.sign(0, keyPair, redeemScript, null, unspent.value, witnessScript)
})
} else if (depends.signature) {
txb.sign(0, keyPairs[0], redeemScript, null, unspent.value, witnessScript)
}
2019-04-07 15:27:16 +02:00
return regtestUtils.broadcast(txb.build().toHex())
}
;['p2ms', 'p2pk', 'p2pkh', 'p2wpkh'].forEach((k) => {
const fixtures = require('../fixtures/' + k)
const { depends } = fixtures.dynamic
const fn = bitcoin.payments[k]
const base = {}
if (depends.pubkey) base.pubkey = keyPairs[0].publicKey
if (depends.pubkeys) base.pubkeys = keyPairs.map(x => x.publicKey)
if (depends.m) base.m = base.pubkeys.length
const { output } = fn(base)
if (!output) throw new TypeError('Missing output')
2018-07-20 09:02:27 +02:00
describe('bitcoinjs-lib (payments - ' + k + ')', function () {
2019-04-07 15:27:16 +02:00
it('can broadcast as an output, and be spent as an input', async () => {
await buildAndSign(depends, output, null, null)
})
2019-04-07 15:27:16 +02:00
it('can (as P2SH(' + k + ')) broadcast as an output, and be spent as an input', async () => {
const p2sh = bitcoin.payments.p2sh({ redeem: { output }, network: NETWORK })
2019-04-07 15:27:16 +02:00
await buildAndSign(depends, p2sh.output, p2sh.redeem.output, null)
})
// NOTE: P2WPKH cannot be wrapped in P2WSH, consensus fail
if (k === 'p2wpkh') return
2019-04-07 15:27:16 +02:00
it('can (as P2WSH(' + k + ')) broadcast as an output, and be spent as an input', async () => {
const p2wsh = bitcoin.payments.p2wsh({ redeem: { output }, network: NETWORK })
2019-04-07 15:27:16 +02:00
await buildAndSign(depends, p2wsh.output, null, p2wsh.redeem.output)
})
2019-04-07 15:27:16 +02:00
it('can (as P2SH(P2WSH(' + k + '))) broadcast as an output, and be spent as an input', async () => {
const p2wsh = bitcoin.payments.p2wsh({ redeem: { output }, network: NETWORK })
const p2sh = bitcoin.payments.p2sh({ redeem: { output: p2wsh.output }, network: NETWORK })
2019-04-07 15:27:16 +02:00
await buildAndSign(depends, p2sh.output, p2sh.redeem.output, p2wsh.redeem.output)
})
})
})