tests: add into-the-future CLTV test
This commit is contained in:
parent
4ca6bf4e81
commit
31703e2e40
3 changed files with 56 additions and 40 deletions
|
@ -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,47 +78,55 @@ 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)
|
||||||
|
|
||||||
// 50 blocks from now
|
regtestUtils.height(function (err, height) {
|
||||||
var time
|
|
||||||
var redeemScript = cltvCheckSigOutput(alice, bob, timeUtc)
|
|
||||||
var scriptPubKey = bitcoin.script.scriptHash.output.encode(bitcoin.crypto.hash160(redeemScript))
|
|
||||||
var address = bitcoin.address.fromOutputScript(scriptPubKey, regtest)
|
|
||||||
|
|
||||||
// fund the P2SH(CLTV) address
|
|
||||||
regtestUtils.faucet(address, 1e5, function (err, unspent) {
|
|
||||||
if (err) return done(err)
|
if (err) return done(err)
|
||||||
|
|
||||||
var txb = new bitcoin.TransactionBuilder(regtest)
|
// 50 blocks from now
|
||||||
txb.setLockTime(timeUtc)
|
var lockTime = bip65.encode({ blocks: height + 50 })
|
||||||
txb.addInput(unspent.txId, unspent.vout, 0xfffffffe)
|
var redeemScript = cltvCheckSigOutput(alice, bob, lockTime)
|
||||||
txb.addOutput(regtestUtils.RANDOM_ADDRESS, 7e4)
|
var scriptPubKey = bitcoin.script.scriptHash.output.encode(bitcoin.crypto.hash160(redeemScript))
|
||||||
|
var address = bitcoin.address.fromOutputScript(scriptPubKey, regtest)
|
||||||
|
|
||||||
// {Alice's signature} OP_TRUE
|
// fund the P2SH(CLTV) address
|
||||||
var tx = txb.buildIncomplete()
|
regtestUtils.faucet(address, 1e5, function (err, unspent) {
|
||||||
var signatureHash = tx.hashForSignature(0, redeemScript, hashType)
|
|
||||||
var redeemScriptSig = bitcoin.script.scriptHash.input.encode([
|
|
||||||
alice.sign(signatureHash).toScriptSignature(hashType),
|
|
||||||
bitcoin.opcodes.OP_TRUE
|
|
||||||
], redeemScript)
|
|
||||||
tx.setInputScript(0, redeemScriptSig)
|
|
||||||
|
|
||||||
regtestUtils.broadcast(tx.toHex(), function (err) {
|
|
||||||
if (err) return done(err)
|
if (err) return done(err)
|
||||||
|
|
||||||
// fails before the expiry
|
var txb = new bitcoin.TransactionBuilder(regtest)
|
||||||
assert.throws(function () {
|
txb.setLockTime(lockTime)
|
||||||
if (err) throw err
|
txb.addInput(unspent.txId, unspent.vout, 0xfffffffe)
|
||||||
}, /Error: 64: non-final/)
|
txb.addOutput(regtestUtils.RANDOM_ADDRESS, 7e4)
|
||||||
|
|
||||||
// into the future!
|
// {Alice's signature} OP_TRUE
|
||||||
regtestUtils.mine(
|
var tx = txb.buildIncomplete()
|
||||||
|
var signatureHash = tx.hashForSignature(0, redeemScript, hashType)
|
||||||
|
var redeemScriptSig = bitcoin.script.scriptHash.input.encode([
|
||||||
|
alice.sign(signatureHash).toScriptSignature(hashType),
|
||||||
|
bitcoin.opcodes.OP_TRUE
|
||||||
|
], redeemScript)
|
||||||
|
tx.setInputScript(0, redeemScriptSig)
|
||||||
|
|
||||||
regtestUtils.verify({
|
regtestUtils.broadcast(tx.toHex(), function (err) {
|
||||||
txId: tx.getId(),
|
// fails before the expiry
|
||||||
address: regtestUtils.RANDOM_ADDRESS,
|
assert.throws(function () {
|
||||||
vout: 0,
|
if (err) throw err
|
||||||
value: 7e4
|
}, /Error: 64: non-final/)
|
||||||
}, done)
|
|
||||||
|
// into the future!
|
||||||
|
regtestUtils.mine(51, 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)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue