testing/integration/examples: isolate to addresses/transactions

examples, use public broadcast endpoints
This commit is contained in:
Daniel Cousens 2017-08-09 16:03:12 +10:00 committed by Daniel Cousens
commit a4fe3d3139
12 changed files with 358 additions and 295 deletions
test/integration

View file

@ -2,15 +2,16 @@
var assert = require('assert')
var bitcoin = require('../../')
var blockchain = require('./_blockchain')
var testnetUtils = require('./_testnet')
var network = bitcoin.networks.testnet
var alice = bitcoin.ECPair.fromWIF('cScfkGjbzzoeewVWmU2hYPUHeVGJRDdFt7WhmrVVGkxpmPP8BHWe', network)
var bob = bitcoin.ECPair.fromWIF('cMkopUXKWsEzAjfa1zApksGRwjVpJRB3831qM9W4gKZsLwjHXA9x', network)
var testnet = bitcoin.networks.testnet
var alice = bitcoin.ECPair.fromWIF('cScfkGjbzzoeewVWmU2hYPUHeVGJRDdFt7WhmrVVGkxpmPP8BHWe', testnet)
var bob = bitcoin.ECPair.fromWIF('cMkopUXKWsEzAjfa1zApksGRwjVpJRB3831qM9W4gKZsLwjHXA9x', testnet)
describe('bitcoinjs-lib (CLTV)', function () {
describe('bitcoinjs-lib (transactions w/ CLTV)', function () {
var hashType = bitcoin.Transaction.SIGHASH_ALL
// IF MTP > utcSeconds, aQ can redeem, ELSE bQ, aQ joint redeem
function cltvCheckSigOutput (aQ, bQ, utcSeconds) {
return bitcoin.script.compile([
bitcoin.opcodes.OP_IF,
@ -33,23 +34,23 @@ describe('bitcoinjs-lib (CLTV)', function () {
}
// expiry past, {Alice's signature} OP_TRUE
it('where Alice can redeem after the expiry is past', function (done) {
it('can create (and broadcast via 3PBP) a Transaction where Alice can redeem the output after the expiry', function (done) {
this.timeout(30000)
// three hours ago
var timeUtc = utcNow() - (3600 * 3)
var redeemScript = cltvCheckSigOutput(alice, bob, timeUtc)
var scriptPubKey = bitcoin.script.scriptHash.output.encode(bitcoin.crypto.hash160(redeemScript))
var address = bitcoin.address.fromOutputScript(scriptPubKey, network)
var address = bitcoin.address.fromOutputScript(scriptPubKey, testnet)
// fund the P2SH(CLTV) address
blockchain.t.faucet(address, 2e4, function (err, unspent) {
testnetUtils.faucet(address, 2e4, function (err, unspent) {
if (err) return done(err)
var tx = new bitcoin.TransactionBuilder(network)
var tx = new bitcoin.TransactionBuilder(testnet)
tx.setLockTime(timeUtc)
tx.addInput(unspent.txId, 0, 0xfffffffe)
tx.addOutput(blockchain.t.RETURN, 1e4)
tx.addOutput(testnetUtils.RETURN_ADDRESS, 1e4)
var txRaw = tx.buildIncomplete()
var signatureHash = txRaw.hashForSignature(0, redeemScript, hashType)
@ -62,27 +63,28 @@ describe('bitcoinjs-lib (CLTV)', function () {
txRaw.setInputScript(0, redeemScriptSig)
blockchain.t.transactions.propagate(txRaw.toHex(), done)
testnetUtils.transactions.propagate(txRaw.toHex(), done)
})
})
// expiry ignored, {Bob's signature} {Alice's signature} OP_FALSE
it('where Alice and Bob can redeem at any time', function (done) {
it('can create (and broadcast via 3PBP) a Transaction where Alice and Bob can redeem the output at any time', function (done) {
this.timeout(30000)
// two hours ago
var timeUtc = utcNow() - (3600 * 2)
var redeemScript = cltvCheckSigOutput(alice, bob, timeUtc)
var scriptPubKey = bitcoin.script.scriptHash.output.encode(bitcoin.crypto.hash160(redeemScript))
var address = bitcoin.address.fromOutputScript(scriptPubKey, network)
var address = bitcoin.address.fromOutputScript(scriptPubKey, testnet)
// fund the P2SH(CLTV) address
blockchain.t.faucet(address, 2e4, function (err, unspent) {
testnetUtils.faucet(address, 2e4, function (err, unspent) {
if (err) return done(err)
var tx = new bitcoin.TransactionBuilder(network)
var tx = new bitcoin.TransactionBuilder(testnet)
tx.setLockTime(timeUtc)
tx.addInput(unspent.txId, 0, 0xfffffffe)
tx.addOutput(blockchain.t.RETURN, 1e4)
tx.addOutput(testnetUtils.RETURN_ADDRESS, 1e4)
var txRaw = tx.buildIncomplete()
var signatureHash = txRaw.hashForSignature(0, redeemScript, hashType)
@ -94,28 +96,28 @@ describe('bitcoinjs-lib (CLTV)', function () {
txRaw.setInputScript(0, redeemScriptSig)
blockchain.t.transactions.propagate(txRaw.toHex(), done)
testnetUtils.transactions.propagate(txRaw.toHex(), done)
})
})
// expiry in the future, {Alice's signature} OP_TRUE
it('fails when still time-locked', function (done) {
it('can create (but fail to broadcast via 3PBP) a Transaction where Alice attempts to redeem before the expiry', function (done) {
this.timeout(30000)
// two hours from now
var timeUtc = utcNow() + (3600 * 2)
var redeemScript = cltvCheckSigOutput(alice, bob, timeUtc)
var scriptPubKey = bitcoin.script.scriptHash.output.encode(bitcoin.crypto.hash160(redeemScript))
var address = bitcoin.address.fromOutputScript(scriptPubKey, network)
var address = bitcoin.address.fromOutputScript(scriptPubKey, testnet)
// fund the P2SH(CLTV) address
blockchain.t.faucet(address, 2e4, function (err, unspent) {
testnetUtils.faucet(address, 2e4, function (err, unspent) {
if (err) return done(err)
var tx = new bitcoin.TransactionBuilder(network)
var tx = new bitcoin.TransactionBuilder(testnet)
tx.setLockTime(timeUtc)
tx.addInput(unspent.txId, 0, 0xfffffffe)
tx.addOutput(blockchain.t.RETURN, 1e4)
tx.addOutput(testnetUtils.RETURN_ADDRESS, 1e4)
var txRaw = tx.buildIncomplete()
var signatureHash = txRaw.hashForSignature(0, redeemScript, hashType)
@ -128,7 +130,7 @@ describe('bitcoinjs-lib (CLTV)', function () {
txRaw.setInputScript(0, redeemScriptSig)
blockchain.t.transactions.propagate(txRaw.toHex(), function (err) {
testnetUtils.transactions.propagate(txRaw.toHex(), function (err) {
assert.throws(function () {
if (err) throw err
}, /Error: 64: non-final/)