|
| 1 | +/* global describe, it */ |
| 2 | + |
| 3 | +var assert = require('assert') |
| 4 | +var bitcoin = require('../../') |
| 5 | +var blockchain = require('./_blockchain') |
| 6 | + |
| 7 | +var network = bitcoin.networks.testnet |
| 8 | +var alice = bitcoin.ECPair.fromWIF('cScfkGjbzzoeewVWmU2hYPUHeVGJRDdFt7WhmrVVGkxpmPP8BHWe', network) |
| 9 | +var bob = bitcoin.ECPair.fromWIF('cMkopUXKWsEzAjfa1zApksGRwjVpJRB3831qM9W4gKZsLwjHXA9x', network) |
| 10 | + |
| 11 | +describe('bitcoinjs-lib (CLTV)', function () { |
| 12 | + var hashType = bitcoin.Transaction.SIGHASH_ALL |
| 13 | + |
| 14 | + function cltvCheckSigInput (aQ, bQ, utcSeconds) { |
| 15 | + return bitcoin.script.compile([ |
| 16 | + bitcoin.opcodes.OP_IF, |
| 17 | + bitcoin.script.number.encode(utcSeconds), |
| 18 | + bitcoin.opcodes.OP_CHECKLOCKTIMEVERIFY, |
| 19 | + bitcoin.opcodes.OP_DROP, |
| 20 | + |
| 21 | + bitcoin.opcodes.OP_ELSE, |
| 22 | + bQ.getPublicKeyBuffer(), |
| 23 | + bitcoin.opcodes.OP_CHECKSIGVERIFY, |
| 24 | + bitcoin.opcodes.OP_ENDIF, |
| 25 | + |
| 26 | + aQ.getPublicKeyBuffer(), |
| 27 | + bitcoin.opcodes.OP_CHECKSIG |
| 28 | + ]) |
| 29 | + } |
| 30 | + |
| 31 | + function utcNow () { |
| 32 | + return Math.floor(Date.now() / 1000) |
| 33 | + } |
| 34 | + |
| 35 | + // expiry past, {Alice's signature} OP_TRUE |
| 36 | + it('where Alice can redeem after the expiry is past', function (done) { |
| 37 | + this.timeout(30000) |
| 38 | + |
| 39 | + // three hours ago |
| 40 | + var timeUtc = utcNow() - (3600 * 3) |
| 41 | + var redeemScript = cltvCheckSigInput(alice, bob, timeUtc) |
| 42 | + var scriptPubKey = bitcoin.script.scriptHashOutput(bitcoin.crypto.hash160(redeemScript)) |
| 43 | + var address = bitcoin.address.fromOutputScript(scriptPubKey, network) |
| 44 | + |
| 45 | + // fund the P2SH(CLTV) address |
| 46 | + blockchain.t.faucet(address, 2e4, function (err, unspent) { |
| 47 | + if (err) return done(err) |
| 48 | + |
| 49 | + var tx = new bitcoin.TransactionBuilder(network) |
| 50 | + tx.setLockTime(timeUtc) |
| 51 | + tx.addInput(unspent.txId, 0, 0xfffffffe) |
| 52 | + tx.addOutput(alice.getAddress(), 1000) |
| 53 | + |
| 54 | + var txRaw = tx.buildIncomplete() |
| 55 | + var signatureHash = txRaw.hashForSignature(0, redeemScript, hashType) |
| 56 | + |
| 57 | + // {Alice's signature} OP_TRUE |
| 58 | + var redeemScriptSig = bitcoin.script.scriptHashInput([ |
| 59 | + alice.sign(signatureHash).toScriptSignature(hashType), |
| 60 | + bitcoin.opcodes.OP_TRUE |
| 61 | + ], redeemScript) |
| 62 | + |
| 63 | + txRaw.setInputScript(0, redeemScriptSig) |
| 64 | + |
| 65 | + blockchain.t.transactions.propagate(txRaw.toHex(), done) |
| 66 | + }) |
| 67 | + }) |
| 68 | + |
| 69 | + // expiry ignored, {Bob's signature} {Alice's signature} OP_FALSE |
| 70 | + it('where Alice and Bob can redeem at any time', function (done) { |
| 71 | + this.timeout(30000) |
| 72 | + |
| 73 | + // two hours ago |
| 74 | + var timeUtc = utcNow() - (3600 * 2) |
| 75 | + var redeemScript = cltvCheckSigInput(alice, bob, timeUtc) |
| 76 | + var scriptPubKey = bitcoin.script.scriptHashOutput(bitcoin.crypto.hash160(redeemScript)) |
| 77 | + var address = bitcoin.address.fromOutputScript(scriptPubKey, network) |
| 78 | + |
| 79 | + // fund the P2SH(CLTV) address |
| 80 | + blockchain.t.faucet(address, 2e4, function (err, unspent) { |
| 81 | + if (err) return done(err) |
| 82 | + |
| 83 | + var tx = new bitcoin.TransactionBuilder(network) |
| 84 | + tx.addInput(unspent.txId, 0, 0xfffffffe) |
| 85 | + tx.addOutput(alice.getAddress(), 1000) |
| 86 | + |
| 87 | + var txRaw = tx.buildIncomplete() |
| 88 | + var signatureHash = txRaw.hashForSignature(0, redeemScript, hashType) |
| 89 | + var redeemScriptSig = bitcoin.script.scriptHashInput([ |
| 90 | + alice.sign(signatureHash).toScriptSignature(hashType), |
| 91 | + bob.sign(signatureHash).toScriptSignature(hashType), |
| 92 | + bitcoin.opcodes.OP_FALSE |
| 93 | + ], redeemScript) |
| 94 | + |
| 95 | + txRaw.setInputScript(0, redeemScriptSig) |
| 96 | + |
| 97 | + blockchain.t.transactions.propagate(txRaw.toHex(), done) |
| 98 | + }) |
| 99 | + }) |
| 100 | + |
| 101 | + // expiry in the future, {Alice's signature} OP_TRUE |
| 102 | + it('fails when still time-locked', function (done) { |
| 103 | + this.timeout(30000) |
| 104 | + |
| 105 | + // two hours from now |
| 106 | + var timeUtc = utcNow() + (3600 * 2) |
| 107 | + var redeemScript = cltvCheckSigInput(alice, bob, timeUtc) |
| 108 | + var scriptPubKey = bitcoin.script.scriptHashOutput(bitcoin.crypto.hash160(redeemScript)) |
| 109 | + var address = bitcoin.address.fromOutputScript(scriptPubKey, network) |
| 110 | + |
| 111 | + // fund the P2SH(CLTV) address |
| 112 | + blockchain.t.faucet(address, 2e4, function (err, unspent) { |
| 113 | + if (err) return done(err) |
| 114 | + |
| 115 | + var tx = new bitcoin.TransactionBuilder(network) |
| 116 | + tx.setLockTime(timeUtc) |
| 117 | + tx.addInput(unspent.txId, 0, 0xfffffffe) |
| 118 | + tx.addOutput(alice.getAddress(), 1000) |
| 119 | + |
| 120 | + var txRaw = tx.buildIncomplete() |
| 121 | + var signatureHash = txRaw.hashForSignature(0, redeemScript, hashType) |
| 122 | + |
| 123 | + // {Alice's signature} OP_TRUE |
| 124 | + var redeemScriptSig = bitcoin.script.scriptHashInput([ |
| 125 | + alice.sign(signatureHash).toScriptSignature(hashType), |
| 126 | + bitcoin.opcodes.OP_TRUE |
| 127 | + ], redeemScript) |
| 128 | + |
| 129 | + txRaw.setInputScript(0, redeemScriptSig) |
| 130 | + |
| 131 | + blockchain.t.transactions.propagate(txRaw.toHex(), function (err) { |
| 132 | + assert.throws(function () { |
| 133 | + if (err) throw err |
| 134 | + }, /Error: 64: non-final/) |
| 135 | + |
| 136 | + done() |
| 137 | + }) |
| 138 | + }) |
| 139 | + }) |
| 140 | +}) |
0 commit comments