bitcoinjs-lib/test/integration/csv.js

128 lines
4.1 KiB
JavaScript
Raw Normal View History

2018-07-23 09:45:01 +02:00
const { describe, it, before } = require('mocha')
2018-06-25 08:24:37 +02:00
const assert = require('assert')
const bitcoin = require('../../')
const regtestUtils = require('./_regtest')
const regtest = regtestUtils.network
const bip68 = require('bip68')
2017-12-01 03:13:55 +01:00
2018-06-25 08:24:37 +02:00
const alice = bitcoin.ECPair.fromWIF('cScfkGjbzzoeewVWmU2hYPUHeVGJRDdFt7WhmrVVGkxpmPP8BHWe', regtest)
const bob = bitcoin.ECPair.fromWIF('cMkopUXKWsEzAjfa1zApksGRwjVpJRB3831qM9W4gKZsLwjHXA9x', regtest)
2017-12-01 03:13:55 +01:00
describe('bitcoinjs-lib (transactions w/ CSV)', () => {
2018-05-30 02:23:55 +02:00
// force update MTP
before(async () => {
2019-04-07 15:27:16 +02:00
await regtestUtils.mine(11)
2018-05-30 02:23:55 +02:00
})
const hashType = bitcoin.Transaction.SIGHASH_ALL
2017-12-01 03:13:55 +01:00
// IF MTP (from when confirmed) > seconds, aQ can redeem
function csvCheckSigOutput (aQ, bQ, sequence) {
return bitcoin.script.compile([
bitcoin.opcodes.OP_IF,
bitcoin.script.number.encode(sequence),
bitcoin.opcodes.OP_CHECKSEQUENCEVERIFY,
bitcoin.opcodes.OP_DROP,
bitcoin.opcodes.OP_ELSE,
bQ.publicKey,
2017-12-01 03:13:55 +01:00
bitcoin.opcodes.OP_CHECKSIGVERIFY,
bitcoin.opcodes.OP_ENDIF,
aQ.publicKey,
2017-12-01 03:13:55 +01:00
bitcoin.opcodes.OP_CHECKSIG
])
}
// expiry will pass, {Alice's signature} OP_TRUE
it('can create (and broadcast via 3PBP) a Transaction where Alice can redeem the output after the expiry (in the future)', async () => {
2019-04-07 15:27:16 +02:00
// 5 blocks from now
const sequence = bip68.encode({ blocks: 5 })
const p2sh = bitcoin.payments.p2sh({
redeem: {
output: csvCheckSigOutput(alice, bob, sequence)
},
network: regtest
})
// fund the P2SH(CSV) address
const unspent = await regtestUtils.faucet(p2sh.address, 1e5)
const txb = new bitcoin.TransactionBuilder(regtest)
txb.addInput(unspent.txId, unspent.vout, sequence)
txb.addOutput(regtestUtils.RANDOM_ADDRESS, 7e4)
// {Alice's signature} OP_TRUE
const tx = txb.buildIncomplete()
const signatureHash = tx.hashForSignature(0, p2sh.redeem.output, hashType)
const redeemScriptSig = bitcoin.payments.p2sh({
network: regtest,
redeem: {
network: regtest,
output: p2sh.redeem.output,
input: bitcoin.script.compile([
bitcoin.script.signature.encode(alice.sign(signatureHash), hashType),
bitcoin.opcodes.OP_TRUE
])
}
}).input
tx.setInputScript(0, redeemScriptSig)
// TODO: test that it failures _prior_ to expiry, unfortunately, race conditions when run concurrently
// ...
// into the future!
await regtestUtils.mine(10)
await regtestUtils.broadcast(tx.toHex())
await regtestUtils.verify({
txId: tx.getId(),
address: regtestUtils.RANDOM_ADDRESS,
vout: 0,
value: 7e4
2017-12-01 03:13:55 +01:00
})
})
// expiry in the future, {Alice's signature} OP_TRUE
it('can create (but fail to broadcast via 3PBP) a Transaction where Alice attempts to redeem before the expiry', async () => {
2017-12-01 03:13:55 +01:00
// two hours after confirmation
const sequence = bip68.encode({ seconds: 7168 })
2018-07-03 14:54:24 +02:00
const p2sh = bitcoin.payments.p2sh({
network: regtest,
redeem: {
output: csvCheckSigOutput(alice, bob, sequence)
}
})
2017-12-01 03:13:55 +01:00
// fund the P2SH(CSV) address
2019-04-07 15:27:16 +02:00
const unspent = await regtestUtils.faucet(p2sh.address, 2e4)
2017-12-01 03:13:55 +01:00
2019-04-07 15:27:16 +02:00
const txb = new bitcoin.TransactionBuilder(regtest)
txb.addInput(unspent.txId, unspent.vout, sequence)
txb.addOutput(regtestUtils.RANDOM_ADDRESS, 1e4)
2017-12-01 03:13:55 +01:00
2019-04-07 15:27:16 +02:00
// {Alice's signature} OP_TRUE
const tx = txb.buildIncomplete()
const signatureHash = tx.hashForSignature(0, p2sh.redeem.output, hashType)
const redeemScriptSig = bitcoin.payments.p2sh({
network: regtest,
redeem: {
2018-07-03 14:54:24 +02:00
network: regtest,
2019-04-07 15:27:16 +02:00
output: p2sh.redeem.output,
input: bitcoin.script.compile([
bitcoin.script.signature.encode(alice.sign(signatureHash), hashType),
bitcoin.script.signature.encode(bob.sign(signatureHash), hashType),
bitcoin.opcodes.OP_TRUE
])
}
}).input
tx.setInputScript(0, redeemScriptSig)
await regtestUtils.broadcast(tx.toHex()).catch(err => {
assert.throws(() => {
2019-04-07 15:27:16 +02:00
if (err) throw err
}, /Error: non-BIP68-final \(code 64\)/)
2017-12-01 03:13:55 +01:00
})
})
})