Skip to content

Commit

Permalink
Merge pull request #517 from bitcoinjs/locktimetests
Browse files Browse the repository at this point in the history
tests/integration: amend lockTime examples
  • Loading branch information
dcousens committed Jan 5, 2016
2 parents 531e624 + b6d986d commit d47ed2c
Showing 1 changed file with 85 additions and 56 deletions.
141 changes: 85 additions & 56 deletions test/integration/advanced.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global describe, it */
/* global describe, it, beforeEach */

var assert = require('assert')
var bitcoin = require('../../')
Expand Down Expand Up @@ -40,80 +40,109 @@ describe('bitcoinjs-lib (advanced)', function () {
tx.addInput(unspent.txId, unspent.vout)
tx.addOutput(dataScript, 1000)
tx.sign(0, keyPair)
var txRaw = tx.build()

var txBuilt = tx.build()
blockchain.t.transactions.propagate(txRaw.toHex(), done)
})
})

describe('can create transactions using OP_CHECKLOCKTIMEVERIFY', function (done) {
var network = bitcoin.networks.testnet
var alice = bitcoin.ECPair.fromWIF('cScfkGjbzzoeewVWmU2hYPUHeVGJRDdFt7WhmrVVGkxpmPP8BHWe', network)
var bob = bitcoin.ECPair.fromWIF('cMkopUXKWsEzAjfa1zApksGRwjVpJRB3831qM9W4gKZsLwjHXA9x', network)

// now - 3 hours
var threeHoursAgo = Math.floor(Date.now() / 1000) - (3600 * 3)
var redeemScript = bitcoin.script.compile([
bitcoin.opcodes.OP_IF,

bitcoin.script.number.encode(threeHoursAgo),
bitcoin.opcodes.OP_CHECKLOCKTIMEVERIFY,
bitcoin.opcodes.OP_DROP,

bitcoin.opcodes.OP_ELSE,

bob.getPublicKeyBuffer(),
bitcoin.opcodes.OP_CHECKSIGVERIFY,

blockchain.t.transactions.propagate(txBuilt.toHex(), function (err) {
bitcoin.opcodes.OP_ENDIF,

alice.getPublicKeyBuffer(),
bitcoin.opcodes.OP_CHECKSIG
])
var scriptPubKey = bitcoin.script.scriptHashOutput(bitcoin.crypto.hash160(redeemScript))

var txId
beforeEach(function (done) {
this.timeout(10000)

blockchain.t.faucet(alice.getAddress(), 2e4, function (err, unspents) {
if (err) return done(err)

// check that the transaction was propagated
blockchain.t.transactions.get(txBuilt.getId(), function (err, transaction) {
if (err) return done(err)
// use the oldest unspent
var unspent = unspents.pop()

// build the transaction
var tx = new bitcoin.TransactionBuilder(network)
tx.addInput(unspent.txId, unspent.vout)
tx.addOutput(scriptPubKey, 1e4)
tx.sign(0, alice)
var txRaw = tx.build()

var actual = bitcoin.Transaction.fromHex(transaction.txHex)
var actualScript = actual.outs[0].script
assert.deepEqual(actualScript, dataScript)
txId = txRaw.getId()

done()
})
blockchain.t.transactions.propagate(txRaw.toHex(), done)
})
})
})

it('can create a transaction using OP_CHECKLOCKTIMEVERIFY', function (done) {
this.timeout(30000)
// expiry past, {Alice's signature} OP_TRUE
it('where Alice can redeem after the expiry is past', function (done) {
this.timeout(30000)

var network = bitcoin.networks.testnet
var keyPair = bitcoin.ECPair.makeRandom({ network: network })
var address = keyPair.getAddress()
var tx2 = new bitcoin.TransactionBuilder(network)
// tx2.setLockTime(threeHoursAgo) // TODO
tx2.addInput(txId, 0, 0xfffffffe)
tx2.addOutput(alice.getAddress(), 1000)

blockchain.t.faucet(address, 2e4, function (err, unspents) {
if (err) return done(err)
var tx2Raw = tx2.buildIncomplete()
tx2Raw.locktime = threeHoursAgo // TODO

// use the oldest unspent
var unspent = unspents.pop()
var tx = new bitcoin.TransactionBuilder(network)
var hashType = bitcoin.Transaction.SIGHASH_ALL
var signatureHash = tx2Raw.hashForSignature(0, redeemScript, hashType)
var signature = alice.sign(signatureHash)

// now + 2 hours
var hodlDate = Math.floor((Date.now() + new Date(0).setSeconds(7200)) / 1000)
var hodlLockTimeBuffer = new Buffer(4)
hodlLockTimeBuffer.writeInt32LE(hodlDate | 0, 0)

// {signature} {signature} or
// OP_0 {signature} after 1 month
var hodlScript = bitcoin.script.compile([
bitcoin.opcodes.OP_IF,
hodlLockTimeBuffer,
bitcoin.opcodes.OP_CHECKLOCKTIMEVERIFY,
bitcoin.opcodes.OP_DROP,
bitcoin.opcodes.OP_ELSE,
keyPair.getPublicKeyBuffer(),
bitcoin.opcodes.OP_CHECKSIGVERIFY,
bitcoin.opcodes.OP_ENDIF,
keyPair.getPublicKeyBuffer(),
bitcoin.opcodes.OP_CHECKSIG
])
var redeemScriptSig = bitcoin.script.scriptHashInput([
signature.toScriptSignature(hashType), bitcoin.opcodes.OP_TRUE
], redeemScript)

tx.addInput(unspent.txId, unspent.vout)
tx.addOutput(hodlScript, 1000)
tx.sign(0, keyPair)
tx2Raw.setInputScript(0, redeemScriptSig)

var txBuilt = tx.build()
blockchain.t.transactions.propagate(tx2Raw.toHex(), done)
})

blockchain.t.transactions.propagate(txBuilt.toHex(), function (err) {
if (err) return done(err)
// {Bob's signature} {Alice's signature} OP_FALSE
it('where Alice and Bob can redeem at any time', function (done) {
this.timeout(30000)

// check that the transaction was propagated
blockchain.t.transactions.get(txBuilt.getId(), function (err, transaction) {
if (err) return done(err)
var tx2 = new bitcoin.TransactionBuilder(network)
// tx2.setLockTime(threeHoursAgo) // TODO
tx2.addInput(txId, 0, 0xfffffffe)
tx2.addOutput(alice.getAddress(), 1000)

var actual = bitcoin.Transaction.fromHex(transaction.txHex)
var actualScript = actual.outs[0].script
assert.deepEqual(actualScript, hodlScript)
var tx2Raw = tx2.buildIncomplete()
tx2Raw.locktime = threeHoursAgo // TODO

done()
})
})
var hashType = bitcoin.Transaction.SIGHASH_ALL
var signatureHash = tx2Raw.hashForSignature(0, redeemScript, hashType)
var signatureA = alice.sign(signatureHash)
var signatureB = bob.sign(signatureHash)
var redeemScriptSig = bitcoin.script.scriptHashInput([
signatureA.toScriptSignature(hashType), signatureB.toScriptSignature(hashType), bitcoin.opcodes.OP_FALSE
], redeemScript)

tx2Raw.setInputScript(0, redeemScriptSig)

blockchain.t.transactions.propagate(tx2Raw.toHex(), done)
})
})
})

0 comments on commit d47ed2c

Please sign in to comment.