tests: add into-the-future CLTV test
This commit is contained in:
parent
4ca6bf4e81
commit
31703e2e40
|
@ -48,6 +48,7 @@
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"bip39": "^2.3.0",
|
"bip39": "^2.3.0",
|
||||||
|
"bip65": "^1.0.1",
|
||||||
"bs58": "^4.0.0",
|
"bs58": "^4.0.0",
|
||||||
"dhttp": "^2.4.2",
|
"dhttp": "^2.4.2",
|
||||||
"minimaldata": "^1.0.2",
|
"minimaldata": "^1.0.2",
|
||||||
|
|
|
@ -19,6 +19,13 @@ function mine (count, callback) {
|
||||||
}, callback)
|
}, callback)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function height (callback) {
|
||||||
|
dhttp({
|
||||||
|
method: 'GET',
|
||||||
|
url: APIURL + '/b/best/height'
|
||||||
|
}, callback)
|
||||||
|
}
|
||||||
|
|
||||||
function faucet (address, value, callback) {
|
function faucet (address, value, callback) {
|
||||||
dhttp({
|
dhttp({
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
|
@ -69,6 +76,7 @@ module.exports = {
|
||||||
broadcast: broadcast,
|
broadcast: broadcast,
|
||||||
faucet: faucet,
|
faucet: faucet,
|
||||||
fetch: fetch,
|
fetch: fetch,
|
||||||
|
height: height,
|
||||||
mine: mine,
|
mine: mine,
|
||||||
network: bitcoin.networks.testnet,
|
network: bitcoin.networks.testnet,
|
||||||
unspents: unspents,
|
unspents: unspents,
|
||||||
|
|
|
@ -12,11 +12,10 @@ var bob = bitcoin.ECPair.fromWIF('cMkopUXKWsEzAjfa1zApksGRwjVpJRB3831qM9W4gKZsLw
|
||||||
describe('bitcoinjs-lib (transactions w/ CLTV)', function () {
|
describe('bitcoinjs-lib (transactions w/ CLTV)', function () {
|
||||||
var hashType = bitcoin.Transaction.SIGHASH_ALL
|
var hashType = bitcoin.Transaction.SIGHASH_ALL
|
||||||
|
|
||||||
// waitUntil is of the form { blocks: ... } or { utc: ... }
|
function cltvCheckSigOutput (aQ, bQ, lockTime) {
|
||||||
function cltvCheckSigOutput (aQ, bQ, waitUntil) {
|
|
||||||
return bitcoin.script.compile([
|
return bitcoin.script.compile([
|
||||||
bitcoin.opcodes.OP_IF,
|
bitcoin.opcodes.OP_IF,
|
||||||
bitcoin.script.number.encode(bip65.encode(waitUntil)),
|
bitcoin.script.number.encode(lockTime),
|
||||||
bitcoin.opcodes.OP_CHECKLOCKTIMEVERIFY,
|
bitcoin.opcodes.OP_CHECKLOCKTIMEVERIFY,
|
||||||
bitcoin.opcodes.OP_DROP,
|
bitcoin.opcodes.OP_DROP,
|
||||||
|
|
||||||
|
@ -39,8 +38,8 @@ describe('bitcoinjs-lib (transactions w/ CLTV)', function () {
|
||||||
this.timeout(30000)
|
this.timeout(30000)
|
||||||
|
|
||||||
// 3 hours ago
|
// 3 hours ago
|
||||||
var timeUtc = utcNow() - (3600 * 3)
|
var lockTime = bip65.encode({ utc: utcNow() - (3600 * 3) })
|
||||||
var redeemScript = cltvCheckSigOutput(alice, bob, { utc: timeUtc })
|
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)
|
||||||
|
|
||||||
|
@ -49,7 +48,7 @@ describe('bitcoinjs-lib (transactions w/ CLTV)', function () {
|
||||||
if (err) return done(err)
|
if (err) return done(err)
|
||||||
|
|
||||||
var txb = new bitcoin.TransactionBuilder(regtest)
|
var txb = new bitcoin.TransactionBuilder(regtest)
|
||||||
txb.setLockTime(timeUtc)
|
txb.setLockTime(lockTime)
|
||||||
txb.addInput(unspent.txId, unspent.vout, 0xfffffffe)
|
txb.addInput(unspent.txId, unspent.vout, 0xfffffffe)
|
||||||
txb.addOutput(regtestUtils.RANDOM_ADDRESS, 7e4)
|
txb.addOutput(regtestUtils.RANDOM_ADDRESS, 7e4)
|
||||||
|
|
||||||
|
@ -79,9 +78,12 @@ describe('bitcoinjs-lib (transactions w/ CLTV)', function () {
|
||||||
it('can create (and broadcast via 3PBP) a Transaction where Alice can redeem the output after the expiry (in the future)', function (done) {
|
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)
|
this.timeout(30000)
|
||||||
|
|
||||||
|
regtestUtils.height(function (err, height) {
|
||||||
|
if (err) return done(err)
|
||||||
|
|
||||||
// 50 blocks from now
|
// 50 blocks from now
|
||||||
var time
|
var lockTime = bip65.encode({ blocks: height + 50 })
|
||||||
var redeemScript = cltvCheckSigOutput(alice, bob, timeUtc)
|
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)
|
||||||
|
|
||||||
|
@ -90,7 +92,7 @@ describe('bitcoinjs-lib (transactions w/ CLTV)', function () {
|
||||||
if (err) return done(err)
|
if (err) return done(err)
|
||||||
|
|
||||||
var txb = new bitcoin.TransactionBuilder(regtest)
|
var txb = new bitcoin.TransactionBuilder(regtest)
|
||||||
txb.setLockTime(timeUtc)
|
txb.setLockTime(lockTime)
|
||||||
txb.addInput(unspent.txId, unspent.vout, 0xfffffffe)
|
txb.addInput(unspent.txId, unspent.vout, 0xfffffffe)
|
||||||
txb.addOutput(regtestUtils.RANDOM_ADDRESS, 7e4)
|
txb.addOutput(regtestUtils.RANDOM_ADDRESS, 7e4)
|
||||||
|
|
||||||
|
@ -104,15 +106,17 @@ describe('bitcoinjs-lib (transactions w/ CLTV)', function () {
|
||||||
tx.setInputScript(0, redeemScriptSig)
|
tx.setInputScript(0, redeemScriptSig)
|
||||||
|
|
||||||
regtestUtils.broadcast(tx.toHex(), function (err) {
|
regtestUtils.broadcast(tx.toHex(), function (err) {
|
||||||
if (err) return done(err)
|
|
||||||
|
|
||||||
// fails before the expiry
|
// fails before the expiry
|
||||||
assert.throws(function () {
|
assert.throws(function () {
|
||||||
if (err) throw err
|
if (err) throw err
|
||||||
}, /Error: 64: non-final/)
|
}, /Error: 64: non-final/)
|
||||||
|
|
||||||
// into the future!
|
// into the future!
|
||||||
regtestUtils.mine(
|
regtestUtils.mine(51, function (err) {
|
||||||
|
if (err) return done(err)
|
||||||
|
|
||||||
|
regtestUtils.broadcast(tx.toHex(), function (err) {
|
||||||
|
if (err) return done(err)
|
||||||
|
|
||||||
regtestUtils.verify({
|
regtestUtils.verify({
|
||||||
txId: tx.getId(),
|
txId: tx.getId(),
|
||||||
|
@ -123,6 +127,9 @@ describe('bitcoinjs-lib (transactions w/ CLTV)', function () {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
// expiry ignored, {Bob's signature} {Alice's signature} OP_FALSE
|
// expiry ignored, {Bob's signature} {Alice's signature} OP_FALSE
|
||||||
it('can create (and broadcast via 3PBP) a Transaction where Alice and Bob can redeem the output at any time', function (done) {
|
it('can create (and broadcast via 3PBP) a Transaction where Alice and Bob can redeem the output at any time', function (done) {
|
||||||
|
|
Loading…
Reference in a new issue