Skip to content

Commit 31703e2

Browse files
committed
tests: add into-the-future CLTV test
1 parent 4ca6bf4 commit 31703e2

File tree

3 files changed

+58
-42
lines changed

3 files changed

+58
-42
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
},
4949
"devDependencies": {
5050
"bip39": "^2.3.0",
51+
"bip65": "^1.0.1",
5152
"bs58": "^4.0.0",
5253
"dhttp": "^2.4.2",
5354
"minimaldata": "^1.0.2",

test/integration/_regtest.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ function mine (count, callback) {
1919
}, callback)
2020
}
2121

22+
function height (callback) {
23+
dhttp({
24+
method: 'GET',
25+
url: APIURL + '/b/best/height'
26+
}, callback)
27+
}
28+
2229
function faucet (address, value, callback) {
2330
dhttp({
2431
method: 'POST',
@@ -69,6 +76,7 @@ module.exports = {
6976
broadcast: broadcast,
7077
faucet: faucet,
7178
fetch: fetch,
79+
height: height,
7280
mine: mine,
7381
network: bitcoin.networks.testnet,
7482
unspents: unspents,

test/integration/cltv.js

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@ var bob = bitcoin.ECPair.fromWIF('cMkopUXKWsEzAjfa1zApksGRwjVpJRB3831qM9W4gKZsLw
1212
describe('bitcoinjs-lib (transactions w/ CLTV)', function () {
1313
var hashType = bitcoin.Transaction.SIGHASH_ALL
1414

15-
// waitUntil is of the form { blocks: ... } or { utc: ... }
16-
function cltvCheckSigOutput (aQ, bQ, waitUntil) {
15+
function cltvCheckSigOutput (aQ, bQ, lockTime) {
1716
return bitcoin.script.compile([
1817
bitcoin.opcodes.OP_IF,
19-
bitcoin.script.number.encode(bip65.encode(waitUntil)),
18+
bitcoin.script.number.encode(lockTime),
2019
bitcoin.opcodes.OP_CHECKLOCKTIMEVERIFY,
2120
bitcoin.opcodes.OP_DROP,
2221

@@ -39,8 +38,8 @@ describe('bitcoinjs-lib (transactions w/ CLTV)', function () {
3938
this.timeout(30000)
4039

4140
// 3 hours ago
42-
var timeUtc = utcNow() - (3600 * 3)
43-
var redeemScript = cltvCheckSigOutput(alice, bob, { utc: timeUtc })
41+
var lockTime = bip65.encode({ utc: utcNow() - (3600 * 3) })
42+
var redeemScript = cltvCheckSigOutput(alice, bob, lockTime)
4443
var scriptPubKey = bitcoin.script.scriptHash.output.encode(bitcoin.crypto.hash160(redeemScript))
4544
var address = bitcoin.address.fromOutputScript(scriptPubKey, regtest)
4645

@@ -49,7 +48,7 @@ describe('bitcoinjs-lib (transactions w/ CLTV)', function () {
4948
if (err) return done(err)
5049

5150
var txb = new bitcoin.TransactionBuilder(regtest)
52-
txb.setLockTime(timeUtc)
51+
txb.setLockTime(lockTime)
5352
txb.addInput(unspent.txId, unspent.vout, 0xfffffffe)
5453
txb.addOutput(regtestUtils.RANDOM_ADDRESS, 7e4)
5554

@@ -79,47 +78,55 @@ describe('bitcoinjs-lib (transactions w/ CLTV)', function () {
7978
it('can create (and broadcast via 3PBP) a Transaction where Alice can redeem the output after the expiry (in the future)', function (done) {
8079
this.timeout(30000)
8180

82-
// 50 blocks from now
83-
var time
84-
var redeemScript = cltvCheckSigOutput(alice, bob, timeUtc)
85-
var scriptPubKey = bitcoin.script.scriptHash.output.encode(bitcoin.crypto.hash160(redeemScript))
86-
var address = bitcoin.address.fromOutputScript(scriptPubKey, regtest)
87-
88-
// fund the P2SH(CLTV) address
89-
regtestUtils.faucet(address, 1e5, function (err, unspent) {
81+
regtestUtils.height(function (err, height) {
9082
if (err) return done(err)
9183

92-
var txb = new bitcoin.TransactionBuilder(regtest)
93-
txb.setLockTime(timeUtc)
94-
txb.addInput(unspent.txId, unspent.vout, 0xfffffffe)
95-
txb.addOutput(regtestUtils.RANDOM_ADDRESS, 7e4)
96-
97-
// {Alice's signature} OP_TRUE
98-
var tx = txb.buildIncomplete()
99-
var signatureHash = tx.hashForSignature(0, redeemScript, hashType)
100-
var redeemScriptSig = bitcoin.script.scriptHash.input.encode([
101-
alice.sign(signatureHash).toScriptSignature(hashType),
102-
bitcoin.opcodes.OP_TRUE
103-
], redeemScript)
104-
tx.setInputScript(0, redeemScriptSig)
84+
// 50 blocks from now
85+
var lockTime = bip65.encode({ blocks: height + 50 })
86+
var redeemScript = cltvCheckSigOutput(alice, bob, lockTime)
87+
var scriptPubKey = bitcoin.script.scriptHash.output.encode(bitcoin.crypto.hash160(redeemScript))
88+
var address = bitcoin.address.fromOutputScript(scriptPubKey, regtest)
10589

106-
regtestUtils.broadcast(tx.toHex(), function (err) {
90+
// fund the P2SH(CLTV) address
91+
regtestUtils.faucet(address, 1e5, function (err, unspent) {
10792
if (err) return done(err)
10893

109-
// fails before the expiry
110-
assert.throws(function () {
111-
if (err) throw err
112-
}, /Error: 64: non-final/)
113-
114-
// into the future!
115-
regtestUtils.mine(
116-
117-
regtestUtils.verify({
118-
txId: tx.getId(),
119-
address: regtestUtils.RANDOM_ADDRESS,
120-
vout: 0,
121-
value: 7e4
122-
}, done)
94+
var txb = new bitcoin.TransactionBuilder(regtest)
95+
txb.setLockTime(lockTime)
96+
txb.addInput(unspent.txId, unspent.vout, 0xfffffffe)
97+
txb.addOutput(regtestUtils.RANDOM_ADDRESS, 7e4)
98+
99+
// {Alice's signature} OP_TRUE
100+
var tx = txb.buildIncomplete()
101+
var signatureHash = tx.hashForSignature(0, redeemScript, hashType)
102+
var redeemScriptSig = bitcoin.script.scriptHash.input.encode([
103+
alice.sign(signatureHash).toScriptSignature(hashType),
104+
bitcoin.opcodes.OP_TRUE
105+
], redeemScript)
106+
tx.setInputScript(0, redeemScriptSig)
107+
108+
regtestUtils.broadcast(tx.toHex(), function (err) {
109+
// fails before the expiry
110+
assert.throws(function () {
111+
if (err) throw err
112+
}, /Error: 64: non-final/)
113+
114+
// into the future!
115+
regtestUtils.mine(51, function (err) {
116+
if (err) return done(err)
117+
118+
regtestUtils.broadcast(tx.toHex(), function (err) {
119+
if (err) return done(err)
120+
121+
regtestUtils.verify({
122+
txId: tx.getId(),
123+
address: regtestUtils.RANDOM_ADDRESS,
124+
vout: 0,
125+
value: 7e4
126+
}, done)
127+
})
128+
})
129+
})
123130
})
124131
})
125132
})

0 commit comments

Comments
 (0)