Merge pull request #944 from bitcoinjs/csv

Add BIP68 / BIP112 CSV integration tests
This commit is contained in:
Jonathan Underwood 2018-05-22 18:37:37 +09:00 committed by GitHub
commit 080675a5cc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 127 additions and 11 deletions

View file

@ -34,7 +34,7 @@
"bigi": "^1.4.0", "bigi": "^1.4.0",
"bip32": "0.0.3", "bip32": "0.0.3",
"bip66": "^1.1.0", "bip66": "^1.1.0",
"bitcoin-ops": "^1.3.0", "bitcoin-ops": "^1.4.0",
"bs58check": "^2.0.0", "bs58check": "^2.0.0",
"create-hash": "^1.1.0", "create-hash": "^1.1.0",
"create-hmac": "^1.1.3", "create-hmac": "^1.1.3",
@ -52,6 +52,7 @@
"bigi": "^1.4.2", "bigi": "^1.4.2",
"bip39": "^2.3.0", "bip39": "^2.3.0",
"bip65": "^1.0.1", "bip65": "^1.0.1",
"bip68": "^1.0.3",
"bs58": "^4.0.0", "bs58": "^4.0.0",
"dhttp": "^2.4.2", "dhttp": "^2.4.2",
"hoodwink": "^1.0.0", "hoodwink": "^1.0.0",

View file

@ -81,8 +81,8 @@ describe('bitcoinjs-lib (transactions w/ CLTV)', function () {
regtestUtils.height(function (err, height) { regtestUtils.height(function (err, height) {
if (err) return done(err) if (err) return done(err)
// 50 blocks from now // 5 blocks from now
var lockTime = bip65.encode({ blocks: height + 50 }) var lockTime = bip65.encode({ blocks: height + 5 })
var redeemScript = cltvCheckSigOutput(alice, bob, lockTime) var redeemScript = cltvCheckSigOutput(alice, bob, lockTime)
var scriptPubKey = bitcoin.script.scriptHash.output.encode(bitcoin.crypto.hash160(redeemScript)) var scriptPubKey = bitcoin.script.scriptHash.output.encode(bitcoin.crypto.hash160(redeemScript))
var address = bitcoin.address.fromOutputScript(scriptPubKey, regtest) var address = bitcoin.address.fromOutputScript(scriptPubKey, regtest)
@ -106,14 +106,9 @@ describe('bitcoinjs-lib (transactions w/ CLTV)', function () {
tx.setInputScript(0, redeemScriptSig) tx.setInputScript(0, redeemScriptSig)
// TODO: test that it failures _prior_ to expiry, unfortunately, race conditions when run concurrently // TODO: test that it failures _prior_ to expiry, unfortunately, race conditions when run concurrently
// regtestUtils.broadcast(tx.toHex(), function (err) { // ...
// // fails before the expiry
// assert.throws(function () {
// if (err) throw err
// }, /Error: 64: non-final/)
// into the future! // into the future!
regtestUtils.mine(51, function (err) { regtestUtils.mine(5, function (err) {
if (err) return done(err) if (err) return done(err)
regtestUtils.broadcast(tx.toHex(), function (err) { regtestUtils.broadcast(tx.toHex(), function (err) {
@ -127,7 +122,6 @@ describe('bitcoinjs-lib (transactions w/ CLTV)', function () {
}, done) }, done)
}) })
}) })
// })
}) })
}) })
}) })

121
test/integration/csv.js Normal file
View file

@ -0,0 +1,121 @@
/* global describe, it */
let assert = require('assert')
let bitcoin = require('../../')
let regtestUtils = require('./_regtest')
let regtest = regtestUtils.network
let bip68 = require('bip68')
let alice = bitcoin.ECPair.fromWIF('cScfkGjbzzoeewVWmU2hYPUHeVGJRDdFt7WhmrVVGkxpmPP8BHWe', regtest)
let bob = bitcoin.ECPair.fromWIF('cMkopUXKWsEzAjfa1zApksGRwjVpJRB3831qM9W4gKZsLwjHXA9x', regtest)
describe('bitcoinjs-lib (transactions w/ CSV)', function () {
let hashType = bitcoin.Transaction.SIGHASH_ALL
// 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.getPublicKeyBuffer(),
bitcoin.opcodes.OP_CHECKSIGVERIFY,
bitcoin.opcodes.OP_ENDIF,
aQ.getPublicKeyBuffer(),
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)', function (done) {
this.timeout(30000)
regtestUtils.height(function (err, height) {
if (err) return done(err)
// 5 blocks from now
let sequence = bip68.encode({ blocks: 5 })
let redeemScript = csvCheckSigOutput(alice, bob, sequence)
let scriptPubKey = bitcoin.script.scriptHash.output.encode(bitcoin.crypto.hash160(redeemScript))
let address = bitcoin.address.fromOutputScript(scriptPubKey, regtest)
// fund the P2SH(CSV) address
regtestUtils.faucet(address, 1e5, function (err, unspent) {
if (err) return done(err)
let txb = new bitcoin.TransactionBuilder(regtest)
txb.addInput(unspent.txId, unspent.vout, sequence)
txb.addOutput(regtestUtils.RANDOM_ADDRESS, 7e4)
// {Alice's signature} OP_TRUE
let tx = txb.buildIncomplete()
let signatureHash = tx.hashForSignature(0, redeemScript, hashType)
let redeemScriptSig = bitcoin.script.scriptHash.input.encode([
bitcoin.script.signature.encode(alice.sign(signatureHash), hashType),
bitcoin.opcodes.OP_TRUE
], redeemScript)
tx.setInputScript(0, redeemScriptSig)
// 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)
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)
})
})
})
})
})
// 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', function (done) {
this.timeout(30000)
// two hours after confirmation
let sequence = bip68.encode({ seconds: 7168 })
let redeemScript = csvCheckSigOutput(alice, bob, sequence)
let scriptPubKey = bitcoin.script.scriptHash.output.encode(bitcoin.crypto.hash160(redeemScript))
let address = bitcoin.address.fromOutputScript(scriptPubKey, regtest)
// fund the P2SH(CSV) address
regtestUtils.faucet(address, 2e4, function (err, unspent) {
if (err) return done(err)
let txb = new bitcoin.TransactionBuilder(regtest)
txb.addInput(unspent.txId, unspent.vout, sequence)
txb.addOutput(regtestUtils.RANDOM_ADDRESS, 1e4)
// {Alice's signature} OP_TRUE
let tx = txb.buildIncomplete()
let signatureHash = tx.hashForSignature(0, redeemScript, hashType)
let redeemScriptSig = bitcoin.script.scriptHash.input.encode([
bitcoin.script.signature.encode(alice.sign(signatureHash), hashType),
bitcoin.script.signature.encode(bob.sign(signatureHash), hashType),
bitcoin.opcodes.OP_TRUE
], redeemScript)
tx.setInputScript(0, redeemScriptSig)
regtestUtils.broadcast(tx.toHex(), function (err) {
assert.throws(function () {
if (err) throw err
}, /Error: 64: non-BIP68-final/)
done()
})
})
})
})