|
| 1 | +/* global describe, it */ |
| 2 | + |
| 3 | +const bitcoin = require('../../') |
| 4 | + |
| 5 | +const regtestUtils = require('./_regtest') |
| 6 | +const NETWORK = regtestUtils.network |
| 7 | +const keyPairs = [ |
| 8 | + bitcoin.ECPair.makeRandom({ network: NETWORK }), |
| 9 | + bitcoin.ECPair.makeRandom({ network: NETWORK }) |
| 10 | +] |
| 11 | + |
| 12 | +function buildAndSign (depends, prevOutput, redeemScript, witnessScript, done) { |
| 13 | + regtestUtils.faucetComplex(prevOutput, 5e4, (err, unspent) => { |
| 14 | + if (err) return done(err) |
| 15 | + |
| 16 | + const txb = new bitcoin.TransactionBuilder(NETWORK) |
| 17 | + txb.addInput(unspent.txId, unspent.vout, null, prevOutput) |
| 18 | + txb.addOutput(regtestUtils.RANDOM_ADDRESS, 2e4) |
| 19 | + |
| 20 | + if (depends.signatures) { |
| 21 | + keyPairs.forEach((keyPair) => { |
| 22 | + txb.sign(0, keyPair, redeemScript, null, unspent.value, witnessScript) |
| 23 | + }) |
| 24 | + } else if (depends.signature) { |
| 25 | + txb.sign(0, keyPairs[0], redeemScript, null, unspent.value, witnessScript) |
| 26 | + } |
| 27 | + |
| 28 | + regtestUtils.broadcast(txb.build().toHex(), done) |
| 29 | + }) |
| 30 | +} |
| 31 | + |
| 32 | +;['p2ms', 'p2pk', 'p2pkh', 'p2wpkh'].forEach((k) => { |
| 33 | + const fixtures = require('../fixtures/' + k) |
| 34 | + const { depends } = fixtures.dynamic |
| 35 | + const fn = bitcoin.payments[k] |
| 36 | + |
| 37 | + const base = {} |
| 38 | + if (depends.pubkey) base.pubkey = keyPairs[0].publicKey |
| 39 | + if (depends.pubkeys) base.pubkeys = keyPairs.map(x => x.publicKey) |
| 40 | + if (depends.m) base.m = base.pubkeys.length |
| 41 | + |
| 42 | + const { output } = fn(base) |
| 43 | + if (!output) throw new TypeError('Missing output') |
| 44 | + |
| 45 | + describe('bitcoinjs-lib (payments - ' + k + ')', () => { |
| 46 | + it('can broadcast as an output, and be spent as an input', (done) => { |
| 47 | + buildAndSign(depends, output, null, null, done) |
| 48 | + }) |
| 49 | + |
| 50 | + it('can (as P2SH(' + k + ')) broadcast as an output, and be spent as an input', (done) => { |
| 51 | + const p2sh = bitcoin.payments.p2sh({ redeem: { output }, network: NETWORK }) |
| 52 | + buildAndSign(depends, p2sh.output, p2sh.redeem.output, null, done) |
| 53 | + }) |
| 54 | + |
| 55 | + it('can (as P2WSH(' + k + ')) broadcast as an output, and be spent as an input', (done) => { |
| 56 | + if (k === 'p2wpkh') return done() // skip P2WSH(P2WPKH) |
| 57 | + |
| 58 | + const p2wsh = bitcoin.payments.p2wsh({ redeem: { output }, network: NETWORK }) |
| 59 | + buildAndSign(depends, p2wsh.output, null, p2wsh.redeem.output, done) |
| 60 | + }) |
| 61 | + |
| 62 | + it('can (as P2SH(P2WSH(' + k + '))) broadcast as an output, and be spent as an input', (done) => { |
| 63 | + if (k === 'p2wpkh') return done() // skip P2SH(P2WSH(P2WPKH)) |
| 64 | + |
| 65 | + const p2wsh = bitcoin.payments.p2wsh({ redeem: { output }, network: NETWORK }) |
| 66 | + const p2sh = bitcoin.payments.p2sh({ redeem: { output: p2wsh.output }, network: NETWORK }) |
| 67 | + |
| 68 | + buildAndSign(depends, p2sh.output, p2sh.redeem.output, p2wsh.redeem.output, done) |
| 69 | + }) |
| 70 | + }) |
| 71 | +}) |
0 commit comments