Merge branch 'master' into addComplexScript

This commit is contained in:
junderw 2019-05-21 16:11:45 +09:00
commit 9f96fd097b
No known key found for this signature in database
GPG key ID: B256185D3A971908
179 changed files with 11314 additions and 4583 deletions
test/integration

View file

@ -1,5 +1,4 @@
/* global describe, it, before */
const { describe, it, before } = require('mocha')
const assert = require('assert')
const bitcoin = require('../../')
const regtestUtils = require('./_regtest')
@ -11,10 +10,10 @@ const bob = bitcoin.ECPair.fromWIF('cMkopUXKWsEzAjfa1zApksGRwjVpJRB3831qM9W4gKZs
const charles = bitcoin.ECPair.fromWIF('cMkopUXKWsEzAjfa1zApksGRwjVpJRB3831qM9W4gKZsMSb4Ubnf', regtest)
const dave = bitcoin.ECPair.fromWIF('cMkopUXKWsEzAjfa1zApksGRwjVpJRB3831qM9W4gKZsMwS4pqnx', regtest)
describe('bitcoinjs-lib (transactions w/ CSV)', function () {
describe('bitcoinjs-lib (transactions w/ CSV)', () => {
// force update MTP
before(function (done) {
regtestUtils.mine(11, done)
before(async () => {
await regtestUtils.mine(11)
})
const hashType = bitcoin.Transaction.SIGHASH_ALL
@ -70,66 +69,56 @@ describe('bitcoinjs-lib (transactions w/ CSV)', function () {
}
// 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) (simple CHECKSEQUENCEVERIFY)', function (done) {
regtestUtils.height(function (err, height) {
if (err) return done(err)
it('can create (and broadcast via 3PBP) a Transaction where Alice can redeem the output after the expiry (in the future) (simple CHECKSEQUENCEVERIFY)', async () => {
// 5 blocks from now
const sequence = bip68.encode({ blocks: 5 })
const p2sh = bitcoin.payments.p2sh({
redeem: {
output: csvCheckSigOutput(alice, bob, sequence)
},
network: regtest
})
// 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)
// fund the P2SH(CSV) address
regtestUtils.faucet(p2sh.address, 1e5, function (err, unspent) {
if (err) return done(err)
const txb = new bitcoin.TransactionBuilder(regtest)
txb.addInput(unspent.txId, unspent.vout, sequence)
txb.addOutput(regtestUtils.RANDOM_ADDRESS, 7e4)
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)
// {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)
// TODO: test that it failures _prior_ to expiry, unfortunately, race conditions when run concurrently
// ...
// into the future!
regtestUtils.mine(10, function (err) {
if (err) return done(err)
await regtestUtils.broadcast(tx.toHex())
regtestUtils.broadcast(tx.toHex(), function (err) {
if (err) return done(err)
regtestUtils.verify({
txId: tx.getId(),
address: regtestUtils.RANDOM_ADDRESS,
vout: 0,
value: 7e4
}, done)
})
})
})
await regtestUtils.verify({
txId: tx.getId(),
address: regtestUtils.RANDOM_ADDRESS,
vout: 0,
value: 7e4
})
})
// 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 (simple CHECKSEQUENCEVERIFY)', function (done) {
it('can create (but fail to broadcast via 3PBP) a Transaction where Alice attempts to redeem before the expiry (simple CHECKSEQUENCEVERIFY)', async () => {
// two hours after confirmation
const sequence = bip68.encode({ seconds: 7168 })
const p2sh = bitcoin.payments.p2sh({
@ -140,215 +129,189 @@ describe('bitcoinjs-lib (transactions w/ CSV)', function () {
})
// fund the P2SH(CSV) address
regtestUtils.faucet(p2sh.address, 2e4, function (err, unspent) {
if (err) return done(err)
const unspent = await regtestUtils.faucet(p2sh.address, 2e4)
const txb = new bitcoin.TransactionBuilder(regtest)
txb.addInput(unspent.txId, unspent.vout, sequence)
txb.addOutput(regtestUtils.RANDOM_ADDRESS, 1e4)
const txb = new bitcoin.TransactionBuilder(regtest)
txb.addInput(unspent.txId, unspent.vout, sequence)
txb.addOutput(regtestUtils.RANDOM_ADDRESS, 1e4)
// {Alice's signature} OP_TRUE
const tx = txb.buildIncomplete()
const signatureHash = tx.hashForSignature(0, p2sh.redeem.output, hashType)
const redeemScriptSig = bitcoin.payments.p2sh({
// {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,
redeem: {
network: regtest,
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)
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)
regtestUtils.broadcast(tx.toHex(), function (err) {
assert.throws(function () {
if (err) throw err
}, /Error: 64: non-BIP68-final/)
done()
})
await regtestUtils.broadcast(tx.toHex()).catch(err => {
assert.throws(() => {
if (err) throw err
}, /Error: non-BIP68-final \(code 64\)/)
})
})
// Check first combination of complex CSV, 2 of 3
it('can create (and broadcast via 3PBP) a Transaction where Bob and Charles can send (complex CHECKSEQUENCEVERIFY)', function (done) {
regtestUtils.height(function (err, height) {
if (err) return done(err)
it('can create (and broadcast via 3PBP) a Transaction where Bob and Charles can send (complex CHECKSEQUENCEVERIFY)', async () => {
const height = await regtestUtils.height()
// 2 blocks from now
const sequence1 = bip68.encode({ blocks: 2 })
// 5 blocks from now
const sequence2 = bip68.encode({ blocks: 5 })
const p2sh = bitcoin.payments.p2sh({
redeem: {
output: complexCsvOutput(alice, bob, charles, dave, sequence1, sequence2)
},
network: regtest
})
// 2 blocks from now
const sequence1 = bip68.encode({ blocks: 2 })
// 5 blocks from now
const sequence2 = bip68.encode({ blocks: 5 })
const p2sh = bitcoin.payments.p2sh({
redeem: {
output: complexCsvOutput(alice, bob, charles, dave, sequence1, sequence2)
},
network: regtest
})
// fund the P2SH(CCSV) address
regtestUtils.faucet(p2sh.address, 1e5, function (err, unspent) {
if (err) return done(err)
// fund the P2SH(CCSV) address
const unspent = await regtestUtils.faucet(p2sh.address, 1e5)
const txb = new bitcoin.TransactionBuilder(regtest)
txb.addInput(unspent.txId, unspent.vout)
txb.addOutput(regtestUtils.RANDOM_ADDRESS, 7e4)
const txb = new bitcoin.TransactionBuilder(regtest)
txb.addInput(unspent.txId, unspent.vout)
txb.addOutput(regtestUtils.RANDOM_ADDRESS, 7e4)
// OP_0 {Bob sig} {Charles sig} OP_TRUE 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.opcodes.OP_0,
bitcoin.script.signature.encode(bob.sign(signatureHash), hashType),
bitcoin.script.signature.encode(charles.sign(signatureHash), hashType),
bitcoin.opcodes.OP_TRUE,
bitcoin.opcodes.OP_TRUE
])
}
}).input
tx.setInputScript(0, redeemScriptSig)
// OP_0 {Bob sig} {Charles sig} OP_TRUE 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.opcodes.OP_0,
bitcoin.script.signature.encode(bob.sign(signatureHash), hashType),
bitcoin.script.signature.encode(charles.sign(signatureHash), hashType),
bitcoin.opcodes.OP_TRUE,
bitcoin.opcodes.OP_TRUE
])
}
}).input
tx.setInputScript(0, redeemScriptSig)
regtestUtils.broadcast(tx.toHex(), function (err) {
if (err) return done(err)
await regtestUtils.broadcast(tx.toHex())
regtestUtils.verify({
txId: tx.getId(),
address: regtestUtils.RANDOM_ADDRESS,
vout: 0,
value: 7e4
}, done)
})
})
await regtestUtils.verify({
txId: tx.getId(),
address: regtestUtils.RANDOM_ADDRESS,
vout: 0,
value: 7e4
})
})
// Check first combination of complex CSV, mediator + 1 of 3 after 2 blocks
it('can create (and broadcast via 3PBP) a Transaction where Alice (mediator) and Bob can send after 2 blocks (complex CHECKSEQUENCEVERIFY)', function (done) {
regtestUtils.height(function (err, height) {
if (err) return done(err)
it('can create (and broadcast via 3PBP) a Transaction where Alice (mediator) and Bob can send after 2 blocks (complex CHECKSEQUENCEVERIFY)', async () => {
const height = await regtestUtils.height()
// 2 blocks from now
const sequence1 = bip68.encode({ blocks: 2 })
// 5 blocks from now
const sequence2 = bip68.encode({ blocks: 5 })
const p2sh = bitcoin.payments.p2sh({
redeem: {
output: complexCsvOutput(alice, bob, charles, dave, sequence1, sequence2)
},
network: regtest
})
// 2 blocks from now
const sequence1 = bip68.encode({ blocks: 2 })
// 5 blocks from now
const sequence2 = bip68.encode({ blocks: 5 })
const p2sh = bitcoin.payments.p2sh({
redeem: {
output: complexCsvOutput(alice, bob, charles, dave, sequence1, sequence2)
},
network: regtest
})
// fund the P2SH(CCSV) address
regtestUtils.faucet(p2sh.address, 1e5, function (err, unspent) {
if (err) return done(err)
// fund the P2SH(CCSV) address
const unspent = await regtestUtils.faucet(p2sh.address, 1e5)
const txb = new bitcoin.TransactionBuilder(regtest)
txb.addInput(unspent.txId, unspent.vout, sequence1) // Set sequence1 for input
txb.addOutput(regtestUtils.RANDOM_ADDRESS, 7e4)
const txb = new bitcoin.TransactionBuilder(regtest)
txb.addInput(unspent.txId, unspent.vout, sequence1) // Set sequence1 for input
txb.addOutput(regtestUtils.RANDOM_ADDRESS, 7e4)
// OP_0 {Bob sig} {Alice mediator sig} OP_FALSE 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.opcodes.OP_0,
bitcoin.script.signature.encode(bob.sign(signatureHash), hashType),
bitcoin.script.signature.encode(alice.sign(signatureHash), hashType),
bitcoin.opcodes.OP_0,
bitcoin.opcodes.OP_TRUE
])
}
}).input
tx.setInputScript(0, redeemScriptSig)
// OP_0 {Bob sig} {Alice mediator sig} OP_FALSE 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.opcodes.OP_0,
bitcoin.script.signature.encode(bob.sign(signatureHash), hashType),
bitcoin.script.signature.encode(alice.sign(signatureHash), hashType),
bitcoin.opcodes.OP_0,
bitcoin.opcodes.OP_TRUE
])
}
}).input
tx.setInputScript(0, redeemScriptSig)
// Wait 2 blocks
regtestUtils.mine(2, function (err) {
if (err) return done(err)
// Wait 2 blocks
await regtestUtils.mine(2)
regtestUtils.broadcast(tx.toHex(), function (err) {
if (err) return done(err)
await regtestUtils.broadcast(tx.toHex())
regtestUtils.verify({
txId: tx.getId(),
address: regtestUtils.RANDOM_ADDRESS,
vout: 0,
value: 7e4
}, done)
})
})
})
await regtestUtils.verify({
txId: tx.getId(),
address: regtestUtils.RANDOM_ADDRESS,
vout: 0,
value: 7e4
})
})
// Check first combination of complex CSV, mediator after 5 blocks
it('can create (and broadcast via 3PBP) a Transaction where Alice (mediator) can send after 5 blocks (complex CHECKSEQUENCEVERIFY)', function (done) {
regtestUtils.height(function (err, height) {
if (err) return done(err)
it('can create (and broadcast via 3PBP) a Transaction where Alice (mediator) can send after 5 blocks (complex CHECKSEQUENCEVERIFY)', async () => {
const height = await regtestUtils.height()
// 2 blocks from now
const sequence1 = bip68.encode({ blocks: 2 })
// 5 blocks from now
const sequence2 = bip68.encode({ blocks: 5 })
const p2sh = bitcoin.payments.p2sh({
redeem: {
output: complexCsvOutput(alice, bob, charles, dave, sequence1, sequence2)
},
network: regtest
})
// 2 blocks from now
const sequence1 = bip68.encode({ blocks: 2 })
// 5 blocks from now
const sequence2 = bip68.encode({ blocks: 5 })
const p2sh = bitcoin.payments.p2sh({
redeem: {
output: complexCsvOutput(alice, bob, charles, dave, sequence1, sequence2)
},
network: regtest
})
// fund the P2SH(CCSV) address
regtestUtils.faucet(p2sh.address, 1e5, function (err, unspent) {
if (err) return done(err)
// fund the P2SH(CCSV) address
const unspent = await regtestUtils.faucet(p2sh.address, 1e5)
const txb = new bitcoin.TransactionBuilder(regtest)
txb.addInput(unspent.txId, unspent.vout, sequence2) // Set sequence2 for input
txb.addOutput(regtestUtils.RANDOM_ADDRESS, 7e4)
const txb = new bitcoin.TransactionBuilder(regtest)
txb.addInput(unspent.txId, unspent.vout, sequence2) // Set sequence2 for input
txb.addOutput(regtestUtils.RANDOM_ADDRESS, 7e4)
// {Alice mediator sig} OP_FALSE
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_0
])
}
}).input
tx.setInputScript(0, redeemScriptSig)
// {Alice mediator sig} OP_FALSE
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_0
])
}
}).input
tx.setInputScript(0, redeemScriptSig)
// Wait 5 blocks
regtestUtils.mine(5, function (err) {
if (err) return done(err)
// Wait 5 blocks
await regtestUtils.mine(5)
regtestUtils.broadcast(tx.toHex(), function (err) {
if (err) return done(err)
await regtestUtils.broadcast(tx.toHex())
regtestUtils.verify({
txId: tx.getId(),
address: regtestUtils.RANDOM_ADDRESS,
vout: 0,
value: 7e4
}, done)
})
})
})
await regtestUtils.verify({
txId: tx.getId(),
address: regtestUtils.RANDOM_ADDRESS,
vout: 0,
value: 7e4
})
})
})