|
@ -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
|
|
|
|
|
|
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.broadcast(tx.toHex(), function (err) { |
|
|
// fund the P2SH(CLTV) address
|
|
|
|
|
|
regtestUtils.faucet(address, 1e5, function (err, unspent) { |
|
|
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) |
|
|
regtestUtils.verify({ |
|
|
var redeemScriptSig = bitcoin.script.scriptHash.input.encode([ |
|
|
txId: tx.getId(), |
|
|
alice.sign(signatureHash).toScriptSignature(hashType), |
|
|
address: regtestUtils.RANDOM_ADDRESS, |
|
|
bitcoin.opcodes.OP_TRUE |
|
|
vout: 0, |
|
|
], redeemScript) |
|
|
value: 7e4 |
|
|
tx.setInputScript(0, redeemScriptSig) |
|
|
}, done) |
|
|
|
|
|
|
|
|
regtestUtils.broadcast(tx.toHex(), function (err) { |
|
|
|
|
|
// fails before the expiry
|
|
|
|
|
|
assert.throws(function () { |
|
|
|
|
|
if (err) throw err |
|
|
|
|
|
}, /Error: 64: non-final/) |
|
|
|
|
|
|
|
|
|
|
|
// 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) |
|
|
|
|
|
}) |
|
|
|
|
|
}) |
|
|
|
|
|
}) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|
}) |
|
|