Merge pull request #517 from bitcoinjs/locktimetests
tests/integration: amend lockTime examples
This commit is contained in:
commit
d47ed2c3ba
1 changed files with 87 additions and 58 deletions
|
@ -1,4 +1,4 @@
|
||||||
/* global describe, it */
|
/* global describe, it, beforeEach */
|
||||||
|
|
||||||
var assert = require('assert')
|
var assert = require('assert')
|
||||||
var bitcoin = require('../../')
|
var bitcoin = require('../../')
|
||||||
|
@ -40,80 +40,109 @@ describe('bitcoinjs-lib (advanced)', function () {
|
||||||
tx.addInput(unspent.txId, unspent.vout)
|
tx.addInput(unspent.txId, unspent.vout)
|
||||||
tx.addOutput(dataScript, 1000)
|
tx.addOutput(dataScript, 1000)
|
||||||
tx.sign(0, keyPair)
|
tx.sign(0, keyPair)
|
||||||
|
var txRaw = tx.build()
|
||||||
|
|
||||||
var txBuilt = tx.build()
|
blockchain.t.transactions.propagate(txRaw.toHex(), done)
|
||||||
|
|
||||||
blockchain.t.transactions.propagate(txBuilt.toHex(), function (err) {
|
|
||||||
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)
|
|
||||||
|
|
||||||
var actual = bitcoin.Transaction.fromHex(transaction.txHex)
|
|
||||||
var actualScript = actual.outs[0].script
|
|
||||||
assert.deepEqual(actualScript, dataScript)
|
|
||||||
|
|
||||||
done()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('can create a transaction using OP_CHECKLOCKTIMEVERIFY', function (done) {
|
describe('can create transactions using OP_CHECKLOCKTIMEVERIFY', function (done) {
|
||||||
this.timeout(30000)
|
|
||||||
|
|
||||||
var network = bitcoin.networks.testnet
|
var network = bitcoin.networks.testnet
|
||||||
var keyPair = bitcoin.ECPair.makeRandom({ network: network })
|
var alice = bitcoin.ECPair.fromWIF('cScfkGjbzzoeewVWmU2hYPUHeVGJRDdFt7WhmrVVGkxpmPP8BHWe', network)
|
||||||
var address = keyPair.getAddress()
|
var bob = bitcoin.ECPair.fromWIF('cMkopUXKWsEzAjfa1zApksGRwjVpJRB3831qM9W4gKZsLwjHXA9x', network)
|
||||||
|
|
||||||
blockchain.t.faucet(address, 2e4, function (err, unspents) {
|
// now - 3 hours
|
||||||
if (err) return done(err)
|
var threeHoursAgo = Math.floor(Date.now() / 1000) - (3600 * 3)
|
||||||
|
var redeemScript = bitcoin.script.compile([
|
||||||
|
bitcoin.opcodes.OP_IF,
|
||||||
|
|
||||||
// use the oldest unspent
|
bitcoin.script.number.encode(threeHoursAgo),
|
||||||
var unspent = unspents.pop()
|
bitcoin.opcodes.OP_CHECKLOCKTIMEVERIFY,
|
||||||
var tx = new bitcoin.TransactionBuilder(network)
|
bitcoin.opcodes.OP_DROP,
|
||||||
|
|
||||||
// now + 2 hours
|
bitcoin.opcodes.OP_ELSE,
|
||||||
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
|
bob.getPublicKeyBuffer(),
|
||||||
// OP_0 {signature} after 1 month
|
bitcoin.opcodes.OP_CHECKSIGVERIFY,
|
||||||
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
|
|
||||||
])
|
|
||||||
|
|
||||||
tx.addInput(unspent.txId, unspent.vout)
|
bitcoin.opcodes.OP_ENDIF,
|
||||||
tx.addOutput(hodlScript, 1000)
|
|
||||||
tx.sign(0, keyPair)
|
|
||||||
|
|
||||||
var txBuilt = tx.build()
|
alice.getPublicKeyBuffer(),
|
||||||
|
bitcoin.opcodes.OP_CHECKSIG
|
||||||
|
])
|
||||||
|
var scriptPubKey = bitcoin.script.scriptHashOutput(bitcoin.crypto.hash160(redeemScript))
|
||||||
|
|
||||||
blockchain.t.transactions.propagate(txBuilt.toHex(), function (err) {
|
var txId
|
||||||
|
beforeEach(function (done) {
|
||||||
|
this.timeout(10000)
|
||||||
|
|
||||||
|
blockchain.t.faucet(alice.getAddress(), 2e4, function (err, unspents) {
|
||||||
if (err) return done(err)
|
if (err) return done(err)
|
||||||
|
|
||||||
// check that the transaction was propagated
|
// use the oldest unspent
|
||||||
blockchain.t.transactions.get(txBuilt.getId(), function (err, transaction) {
|
var unspent = unspents.pop()
|
||||||
if (err) return done(err)
|
|
||||||
|
|
||||||
var actual = bitcoin.Transaction.fromHex(transaction.txHex)
|
// build the transaction
|
||||||
var actualScript = actual.outs[0].script
|
var tx = new bitcoin.TransactionBuilder(network)
|
||||||
assert.deepEqual(actualScript, hodlScript)
|
tx.addInput(unspent.txId, unspent.vout)
|
||||||
|
tx.addOutput(scriptPubKey, 1e4)
|
||||||
|
tx.sign(0, alice)
|
||||||
|
var txRaw = tx.build()
|
||||||
|
|
||||||
done()
|
txId = txRaw.getId()
|
||||||
})
|
|
||||||
|
blockchain.t.transactions.propagate(txRaw.toHex(), done)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// expiry past, {Alice's signature} OP_TRUE
|
||||||
|
it('where Alice can redeem after the expiry is past', function (done) {
|
||||||
|
this.timeout(30000)
|
||||||
|
|
||||||
|
var tx2 = new bitcoin.TransactionBuilder(network)
|
||||||
|
// tx2.setLockTime(threeHoursAgo) // TODO
|
||||||
|
tx2.addInput(txId, 0, 0xfffffffe)
|
||||||
|
tx2.addOutput(alice.getAddress(), 1000)
|
||||||
|
|
||||||
|
var tx2Raw = tx2.buildIncomplete()
|
||||||
|
tx2Raw.locktime = threeHoursAgo // TODO
|
||||||
|
|
||||||
|
var hashType = bitcoin.Transaction.SIGHASH_ALL
|
||||||
|
var signatureHash = tx2Raw.hashForSignature(0, redeemScript, hashType)
|
||||||
|
var signature = alice.sign(signatureHash)
|
||||||
|
|
||||||
|
var redeemScriptSig = bitcoin.script.scriptHashInput([
|
||||||
|
signature.toScriptSignature(hashType), bitcoin.opcodes.OP_TRUE
|
||||||
|
], redeemScript)
|
||||||
|
|
||||||
|
tx2Raw.setInputScript(0, redeemScriptSig)
|
||||||
|
|
||||||
|
blockchain.t.transactions.propagate(tx2Raw.toHex(), done)
|
||||||
|
})
|
||||||
|
|
||||||
|
// {Bob's signature} {Alice's signature} OP_FALSE
|
||||||
|
it('where Alice and Bob can redeem at any time', function (done) {
|
||||||
|
this.timeout(30000)
|
||||||
|
|
||||||
|
var tx2 = new bitcoin.TransactionBuilder(network)
|
||||||
|
// tx2.setLockTime(threeHoursAgo) // TODO
|
||||||
|
tx2.addInput(txId, 0, 0xfffffffe)
|
||||||
|
tx2.addOutput(alice.getAddress(), 1000)
|
||||||
|
|
||||||
|
var tx2Raw = tx2.buildIncomplete()
|
||||||
|
tx2Raw.locktime = threeHoursAgo // TODO
|
||||||
|
|
||||||
|
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)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Reference in a new issue