From 07cf0bd057b65ead6d16a5e0c9e8f9d9d3968f87 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Thu, 17 Dec 2015 16:01:51 +1100 Subject: [PATCH 1/4] tests/integration: use actual=expected consistently for asserts --- test/integration/advanced.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/integration/advanced.js b/test/integration/advanced.js index 8321da6..bff0db3 100644 --- a/test/integration/advanced.js +++ b/test/integration/advanced.js @@ -21,7 +21,7 @@ describe('bitcoinjs-lib (advanced)', function () { assert(bitcoin.message.verify(address, signature, message)) }) - it('can create an OP_RETURN transaction', function (done) { + it('can create a transaction using OP_RETURN', function (done) { this.timeout(30000) var network = bitcoin.networks.testnet @@ -51,8 +51,8 @@ describe('bitcoinjs-lib (advanced)', function () { if (err) return done(err) var actual = bitcoin.Transaction.fromHex(transaction.txHex) - var dataScript2 = actual.outs[0].script - var data2 = bitcoin.script.decompile(dataScript2)[1] + var actualScript = actual.outs[0].script + assert.deepEqual(actualScript, dataScript) assert.deepEqual(dataScript, dataScript2) assert.deepEqual(data, data2) From cab3022719df2e75762eaf8fc3c663b401dfe2d7 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Thu, 17 Dec 2015 16:10:13 +1100 Subject: [PATCH 2/4] add OP_CHECKLOCKTIMEVERIFY and integration tests --- src/opcodes.json | 2 ++ test/integration/advanced.js | 58 ++++++++++++++++++++++++++++++++++-- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/opcodes.json b/src/opcodes.json index 04fca75..f53ec2f 100644 --- a/src/opcodes.json +++ b/src/opcodes.json @@ -113,6 +113,8 @@ "OP_NOP1": 176, "OP_NOP2": 177, + "OP_CHECKLOCKTIMEVERIFY": 177, + "OP_NOP3": 178, "OP_NOP4": 179, "OP_NOP5": 180, diff --git a/test/integration/advanced.js b/test/integration/advanced.js index bff0db3..796ba98 100644 --- a/test/integration/advanced.js +++ b/test/integration/advanced.js @@ -54,8 +54,62 @@ describe('bitcoinjs-lib (advanced)', function () { var actualScript = actual.outs[0].script assert.deepEqual(actualScript, dataScript) - assert.deepEqual(dataScript, dataScript2) - assert.deepEqual(data, data2) + done() + }) + }) + }) + }) + + it('can create a transaction using OP_CHECKLOCKTIMEVERIFY', function (done) { + this.timeout(30000) + + var network = bitcoin.networks.testnet + var keyPair = bitcoin.ECPair.makeRandom({ network: network }) + var address = keyPair.getAddress() + + blockchain.t.faucet(address, 2e4, function (err, unspents) { + if (err) return done(err) + + // use the oldest unspent + var unspent = unspents.pop() + var tx = new bitcoin.TransactionBuilder(network) + + // now + 1 month + var hodlDate = Math.floor((Date.now() + new Date(0).setMonth(1)) / 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 + ]) + + tx.addInput(unspent.txId, unspent.vout) + tx.addOutput(hodlScript, 1000) + tx.sign(0, keyPair) + + var txBuilt = tx.build() + + blockchain.t.transactions.propagate(txBuilt.toHex(), function (err) { + if (err) return done(err) + + // check that the message 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, hodlScript) done() }) From b5d1b4a0da94b6983d9751807280669f43534287 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Thu, 17 Dec 2015 16:13:29 +1100 Subject: [PATCH 3/4] tests/integration: s/message/transaction --- test/integration/advanced.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/integration/advanced.js b/test/integration/advanced.js index 796ba98..df0ead6 100644 --- a/test/integration/advanced.js +++ b/test/integration/advanced.js @@ -46,7 +46,7 @@ describe('bitcoinjs-lib (advanced)', function () { blockchain.t.transactions.propagate(txBuilt.toHex(), function (err) { if (err) return done(err) - // check that the message was propagated + // check that the transaction was propagated blockchain.t.transactions.get(txBuilt.getId(), function (err, transaction) { if (err) return done(err) @@ -103,7 +103,7 @@ describe('bitcoinjs-lib (advanced)', function () { blockchain.t.transactions.propagate(txBuilt.toHex(), function (err) { if (err) return done(err) - // check that the message was propagated + // check that the transaction was propagated blockchain.t.transactions.get(txBuilt.getId(), function (err, transaction) { if (err) return done(err) From b077d7089daef7754f72428f51765128bd5ac68b Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Tue, 22 Dec 2015 14:17:41 +1100 Subject: [PATCH 4/4] tests/integration: change hodlDate to T+2 hours --- test/integration/advanced.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/integration/advanced.js b/test/integration/advanced.js index df0ead6..1af554d 100644 --- a/test/integration/advanced.js +++ b/test/integration/advanced.js @@ -74,8 +74,8 @@ describe('bitcoinjs-lib (advanced)', function () { var unspent = unspents.pop() var tx = new bitcoin.TransactionBuilder(network) - // now + 1 month - var hodlDate = Math.floor((Date.now() + new Date(0).setMonth(1)) / 1000) + // 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)