2016-08-13 03:42:16 +02:00
|
|
|
/* global describe, it */
|
|
|
|
|
|
|
|
var assert = require('assert')
|
|
|
|
var bitcoin = require('../../')
|
2018-01-31 02:46:35 +01:00
|
|
|
var regtestUtils = require('./_regtest')
|
|
|
|
var regtest = regtestUtils.network
|
|
|
|
var bip65 = require('bip65')
|
2016-08-13 03:42:16 +02:00
|
|
|
|
2018-01-31 02:46:35 +01:00
|
|
|
var alice = bitcoin.ECPair.fromWIF('cScfkGjbzzoeewVWmU2hYPUHeVGJRDdFt7WhmrVVGkxpmPP8BHWe', regtest)
|
|
|
|
var bob = bitcoin.ECPair.fromWIF('cMkopUXKWsEzAjfa1zApksGRwjVpJRB3831qM9W4gKZsLwjHXA9x', regtest)
|
2016-08-13 03:42:16 +02:00
|
|
|
|
2017-08-09 08:03:12 +02:00
|
|
|
describe('bitcoinjs-lib (transactions w/ CLTV)', function () {
|
2016-08-13 03:42:16 +02:00
|
|
|
var hashType = bitcoin.Transaction.SIGHASH_ALL
|
|
|
|
|
2018-02-01 02:54:20 +01:00
|
|
|
function cltvCheckSigOutput (aQ, bQ, lockTime) {
|
2016-08-13 03:42:16 +02:00
|
|
|
return bitcoin.script.compile([
|
|
|
|
bitcoin.opcodes.OP_IF,
|
2018-02-01 02:54:20 +01:00
|
|
|
bitcoin.script.number.encode(lockTime),
|
2016-08-13 03:42:16 +02:00
|
|
|
bitcoin.opcodes.OP_CHECKLOCKTIMEVERIFY,
|
|
|
|
bitcoin.opcodes.OP_DROP,
|
|
|
|
|
|
|
|
bitcoin.opcodes.OP_ELSE,
|
|
|
|
bQ.getPublicKeyBuffer(),
|
|
|
|
bitcoin.opcodes.OP_CHECKSIGVERIFY,
|
|
|
|
bitcoin.opcodes.OP_ENDIF,
|
|
|
|
|
|
|
|
aQ.getPublicKeyBuffer(),
|
|
|
|
bitcoin.opcodes.OP_CHECKSIG
|
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
function utcNow () {
|
|
|
|
return Math.floor(Date.now() / 1000)
|
|
|
|
}
|
|
|
|
|
|
|
|
// expiry past, {Alice's signature} OP_TRUE
|
2018-01-31 02:46:35 +01:00
|
|
|
it('can create (and broadcast via 3PBP) a Transaction where Alice can redeem the output after the expiry (in the past)', function (done) {
|
2016-08-13 03:42:16 +02:00
|
|
|
this.timeout(30000)
|
|
|
|
|
2018-01-31 02:46:35 +01:00
|
|
|
// 3 hours ago
|
2018-02-01 02:54:20 +01:00
|
|
|
var lockTime = bip65.encode({ utc: utcNow() - (3600 * 3) })
|
|
|
|
var redeemScript = cltvCheckSigOutput(alice, bob, lockTime)
|
2016-11-02 04:09:25 +01:00
|
|
|
var scriptPubKey = bitcoin.script.scriptHash.output.encode(bitcoin.crypto.hash160(redeemScript))
|
2018-01-31 02:46:35 +01:00
|
|
|
var address = bitcoin.address.fromOutputScript(scriptPubKey, regtest)
|
2016-08-13 03:42:16 +02:00
|
|
|
|
|
|
|
// fund the P2SH(CLTV) address
|
2018-01-31 02:46:35 +01:00
|
|
|
regtestUtils.faucet(address, 1e5, function (err, unspent) {
|
2016-08-13 03:42:16 +02:00
|
|
|
if (err) return done(err)
|
|
|
|
|
2018-01-31 02:46:35 +01:00
|
|
|
var txb = new bitcoin.TransactionBuilder(regtest)
|
2018-02-01 02:54:20 +01:00
|
|
|
txb.setLockTime(lockTime)
|
2018-01-31 02:46:35 +01:00
|
|
|
txb.addInput(unspent.txId, unspent.vout, 0xfffffffe)
|
|
|
|
txb.addOutput(regtestUtils.RANDOM_ADDRESS, 7e4)
|
|
|
|
|
|
|
|
// {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)
|
2016-08-13 03:42:16 +02:00
|
|
|
|
2018-01-31 02:46:35 +01:00
|
|
|
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 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)
|
|
|
|
|
2018-02-01 02:54:20 +01:00
|
|
|
regtestUtils.height(function (err, height) {
|
2018-01-31 02:46:35 +01:00
|
|
|
if (err) return done(err)
|
|
|
|
|
2018-02-01 02:54:20 +01:00
|
|
|
// 50 blocks from now
|
|
|
|
var lockTime = bip65.encode({ blocks: height + 50 })
|
|
|
|
var redeemScript = cltvCheckSigOutput(alice, bob, lockTime)
|
|
|
|
var scriptPubKey = bitcoin.script.scriptHash.output.encode(bitcoin.crypto.hash160(redeemScript))
|
|
|
|
var address = bitcoin.address.fromOutputScript(scriptPubKey, regtest)
|
2016-08-13 03:42:16 +02:00
|
|
|
|
2018-02-01 02:54:20 +01:00
|
|
|
// fund the P2SH(CLTV) address
|
|
|
|
regtestUtils.faucet(address, 1e5, function (err, unspent) {
|
2018-01-31 02:46:35 +01:00
|
|
|
if (err) return done(err)
|
|
|
|
|
2018-02-01 02:54:20 +01:00
|
|
|
var txb = new bitcoin.TransactionBuilder(regtest)
|
|
|
|
txb.setLockTime(lockTime)
|
|
|
|
txb.addInput(unspent.txId, unspent.vout, 0xfffffffe)
|
|
|
|
txb.addOutput(regtestUtils.RANDOM_ADDRESS, 7e4)
|
|
|
|
|
|
|
|
// {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) {
|
|
|
|
// 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)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2018-01-31 02:46:35 +01:00
|
|
|
})
|
2016-08-13 03:42:16 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
// expiry ignored, {Bob's signature} {Alice's signature} OP_FALSE
|
2017-08-09 08:03:12 +02:00
|
|
|
it('can create (and broadcast via 3PBP) a Transaction where Alice and Bob can redeem the output at any time', function (done) {
|
2016-08-13 03:42:16 +02:00
|
|
|
this.timeout(30000)
|
|
|
|
|
|
|
|
// two hours ago
|
|
|
|
var timeUtc = utcNow() - (3600 * 2)
|
2016-09-27 13:19:27 +02:00
|
|
|
var redeemScript = cltvCheckSigOutput(alice, bob, timeUtc)
|
2016-11-02 04:09:25 +01:00
|
|
|
var scriptPubKey = bitcoin.script.scriptHash.output.encode(bitcoin.crypto.hash160(redeemScript))
|
2018-01-31 02:46:35 +01:00
|
|
|
var address = bitcoin.address.fromOutputScript(scriptPubKey, regtest)
|
2016-08-13 03:42:16 +02:00
|
|
|
|
|
|
|
// fund the P2SH(CLTV) address
|
2018-01-31 02:46:35 +01:00
|
|
|
regtestUtils.faucet(address, 2e5, function (err, unspent) {
|
2016-08-13 03:42:16 +02:00
|
|
|
if (err) return done(err)
|
|
|
|
|
2018-01-31 02:46:35 +01:00
|
|
|
var txb = new bitcoin.TransactionBuilder(regtest)
|
|
|
|
txb.setLockTime(timeUtc)
|
|
|
|
txb.addInput(unspent.txId, unspent.vout, 0xfffffffe)
|
|
|
|
txb.addOutput(regtestUtils.RANDOM_ADDRESS, 8e4)
|
2016-08-13 03:42:16 +02:00
|
|
|
|
2018-01-31 02:46:35 +01:00
|
|
|
// {Alice's signature} {Bob's signature} OP_FALSE
|
|
|
|
var tx = txb.buildIncomplete()
|
|
|
|
var signatureHash = tx.hashForSignature(0, redeemScript, hashType)
|
2016-11-02 04:09:25 +01:00
|
|
|
var redeemScriptSig = bitcoin.script.scriptHash.input.encode([
|
2016-08-13 03:42:16 +02:00
|
|
|
alice.sign(signatureHash).toScriptSignature(hashType),
|
|
|
|
bob.sign(signatureHash).toScriptSignature(hashType),
|
|
|
|
bitcoin.opcodes.OP_FALSE
|
|
|
|
], redeemScript)
|
2018-01-31 02:46:35 +01:00
|
|
|
tx.setInputScript(0, redeemScriptSig)
|
2016-08-13 03:42:16 +02:00
|
|
|
|
2018-01-31 02:46:35 +01:00
|
|
|
regtestUtils.broadcast(tx.toHex(), function (err) {
|
|
|
|
if (err) return done(err)
|
2016-08-13 03:42:16 +02:00
|
|
|
|
2018-01-31 02:46:35 +01:00
|
|
|
regtestUtils.verify({
|
|
|
|
txId: tx.getId(),
|
|
|
|
address: regtestUtils.RANDOM_ADDRESS,
|
|
|
|
vout: 0,
|
|
|
|
value: 8e4
|
|
|
|
}, done)
|
|
|
|
})
|
2016-08-13 03:42:16 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
// expiry in the future, {Alice's signature} OP_TRUE
|
2017-08-09 08:03:12 +02:00
|
|
|
it('can create (but fail to broadcast via 3PBP) a Transaction where Alice attempts to redeem before the expiry', function (done) {
|
2016-08-13 03:42:16 +02:00
|
|
|
this.timeout(30000)
|
|
|
|
|
|
|
|
// two hours from now
|
|
|
|
var timeUtc = utcNow() + (3600 * 2)
|
2016-09-27 13:19:27 +02:00
|
|
|
var redeemScript = cltvCheckSigOutput(alice, bob, timeUtc)
|
2016-11-02 04:09:25 +01:00
|
|
|
var scriptPubKey = bitcoin.script.scriptHash.output.encode(bitcoin.crypto.hash160(redeemScript))
|
2018-01-31 02:46:35 +01:00
|
|
|
var address = bitcoin.address.fromOutputScript(scriptPubKey, regtest)
|
2016-08-13 03:42:16 +02:00
|
|
|
|
|
|
|
// fund the P2SH(CLTV) address
|
2018-01-31 02:46:35 +01:00
|
|
|
regtestUtils.faucet(address, 2e4, function (err, unspent) {
|
2016-08-13 03:42:16 +02:00
|
|
|
if (err) return done(err)
|
|
|
|
|
2018-01-31 02:46:35 +01:00
|
|
|
var txb = new bitcoin.TransactionBuilder(regtest)
|
|
|
|
txb.setLockTime(timeUtc)
|
|
|
|
txb.addInput(unspent.txId, unspent.vout, 0xfffffffe)
|
|
|
|
txb.addOutput(regtestUtils.RANDOM_ADDRESS, 1e4)
|
2016-08-13 03:42:16 +02:00
|
|
|
|
|
|
|
// {Alice's signature} OP_TRUE
|
2018-01-31 02:46:35 +01:00
|
|
|
var tx = txb.buildIncomplete()
|
|
|
|
var signatureHash = tx.hashForSignature(0, redeemScript, hashType)
|
2016-11-02 04:09:25 +01:00
|
|
|
var redeemScriptSig = bitcoin.script.scriptHash.input.encode([
|
2016-08-13 03:42:16 +02:00
|
|
|
alice.sign(signatureHash).toScriptSignature(hashType),
|
|
|
|
bitcoin.opcodes.OP_TRUE
|
|
|
|
], redeemScript)
|
2018-01-31 02:46:35 +01:00
|
|
|
tx.setInputScript(0, redeemScriptSig)
|
2016-08-13 03:42:16 +02:00
|
|
|
|
2018-01-31 02:46:35 +01:00
|
|
|
regtestUtils.broadcast(tx.toHex(), function (err) {
|
2016-08-13 03:42:16 +02:00
|
|
|
assert.throws(function () {
|
|
|
|
if (err) throw err
|
|
|
|
}, /Error: 64: non-final/)
|
|
|
|
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|