Repository: bitcoinjs/bitcoinjs-lib
Pull request #431 (2015-08-07 06:35:25)

use cb-http-client with the BlockTrail API
Reduces the amount of boilerplate code we have here for just querying the blockchain.
Will merge once the quirk RE the test failing is found.


Patch:
diff --git a/package.json b/package.json
index 136e7a29..afcac401 100644
--- a/package.json
+++ b/package.json
@@ -57,8 +57,7 @@
     "async": "^0.9.0",
     "browserify": "^10.0.0",
     "bs58": "^2.0.1",
-    "cb-blockr": "^3.1.1",
-    "cb-insight": "git://github.com/weilu/cb-insight",
+    "cb-http-client": "^0.2.0",
     "coveralls": "^2.11.2",
     "httpify": "^1.0.0",
     "istanbul": "^0.3.5",


Test patch:
diff --git a/test/integration/_blockchain.js b/test/integration/_blockchain.js
new file mode 100644
index 00000000..7d215ec2
--- /dev/null
+++ b/test/integration/_blockchain.js
@@ -0,0 +1,23 @@
+var Blockchain = require('cb-http-client')
+var httpify = require('httpify')
+
+var BLOCKTRAIL_API_KEY = process.env.BLOCKTRAIL_API_KEY || 'c0bd8155c66e3fb148bb1664adc1e4dacd872548'
+
+var mainnet = new Blockchain('https://api.blocktrail.com/cb/v0.2.1/BTC', { api_key: BLOCKTRAIL_API_KEY })
+var testnet = new Blockchain('https://api.blocktrail.com/cb/v0.2.1/tBTC', { api_key: BLOCKTRAIL_API_KEY })
+testnet.faucet = function faucet (address, amount, callback) {
+  httpify({
+    method: 'POST',
+    url: 'https://api.blocktrail.com/v1/tBTC/faucet/withdrawl?api_key=' + BLOCKTRAIL_API_KEY,
+    headers: { 'Content-Type': 'application/json' },
+    body: JSON.stringify({
+      address: address,
+      amount: amount
+    })
+  }, callback)
+}
+
+module.exports = {
+  m: mainnet,
+  t: testnet
+}
diff --git a/test/integration/advanced.js b/test/integration/advanced.js
index d919b742..366c4f39 100644
--- a/test/integration/advanced.js
+++ b/test/integration/advanced.js
@@ -2,9 +2,7 @@
 
 var assert = require('assert')
 var bitcoin = require('../../')
-var blockchain = new (require('cb-insight'))('https://test-insight.bitpay.com')
-var faucetWithdraw = require('./utils').faucetWithdraw
-var pollUnspent = require('./utils').pollUnspent
+var blockchain = require('./_blockchain')
 
 describe('bitcoinjs-lib (advanced)', function () {
   it('can sign a Bitcoin message', function () {
@@ -30,10 +28,10 @@ describe('bitcoinjs-lib (advanced)', function () {
     var keyPair = bitcoin.ECPair.makeRandom({ network: network })
     var address = keyPair.getAddress()
 
-    faucetWithdraw(address, 2e4, function (err) {
+    blockchain.t.faucet(address, 2e4, function (err) {
       if (err) return done(err)
 
-      pollUnspent(blockchain, address, function (err, unspents) {
+      blockchain.t.addresses.unspents(address, function (err, unspents) {
         if (err) return done(err)
 
         var tx = new bitcoin.TransactionBuilder(network)
@@ -48,11 +46,11 @@ describe('bitcoinjs-lib (advanced)', function () {
 
         var txBuilt = tx.build()
 
-        blockchain.transactions.propagate(txBuilt.toHex(), function (err) {
+        blockchain.t.transactions.propagate(txBuilt.toHex(), function (err) {
           if (err) return done(err)
 
           // check that the message was propagated
-          blockchain.transactions.get(txBuilt.getId(), function (err, transaction) {
+          blockchain.t.transactions.get(txBuilt.getId(), function (err, transaction) {
             if (err) return done(err)
 
             var actual = bitcoin.Transaction.fromHex(transaction.txHex)
diff --git a/test/integration/crypto.js b/test/integration/crypto.js
index 172e6a11..7c97cd06 100644
--- a/test/integration/crypto.js
+++ b/test/integration/crypto.js
@@ -4,7 +4,7 @@ var assert = require('assert')
 var async = require('async')
 var bigi = require('bigi')
 var bitcoin = require('../../')
-var blockchain = new (require('cb-blockr'))('bitcoin')
+var blockchain = require('./_blockchain')
 var crypto = require('crypto')
 
 describe('bitcoinjs-lib (crypto)', function () {
@@ -91,6 +91,8 @@ describe('bitcoinjs-lib (crypto)', function () {
   })
 
   it('can recover a private key from duplicate R values', function (done) {
+    this.timeout(10000)
+
     var inputs = [
       {
         txId: 'f4c16475f2a6e9c602e4a287f9db3040e319eb9ece74761a4b84bc820fbeef50',
@@ -105,7 +107,7 @@ describe('bitcoinjs-lib (crypto)', function () {
     var txIds = inputs.map(function (x) { return x.txId })
 
     // first retrieve the relevant transactions
-    blockchain.transactions.get(txIds, function (err, results) {
+    blockchain.m.transactions.get(txIds, function (err, results) {
       assert.ifError(err)
 
       var transactions = {}
@@ -125,7 +127,7 @@ describe('bitcoinjs-lib (crypto)', function () {
         var prevVout = transaction.ins[input.vout].index
 
         tasks.push(function (callback) {
-          blockchain.transactions.get(prevOutTxId, function (err, result) {
+          blockchain.m.transactions.get(prevOutTxId, function (err, result) {
             if (err) return callback(err)
 
             var prevOut = bitcoin.Transaction.fromHex(result.txHex)
diff --git a/test/integration/multisig.js b/test/integration/multisig.js
index f56b368b..abe1e8d1 100644
--- a/test/integration/multisig.js
+++ b/test/integration/multisig.js
@@ -2,10 +2,7 @@
 
 var assert = require('assert')
 var bitcoin = require('../../')
-var blockchain = new (require('cb-insight'))('https://test-insight.bitpay.com')
-var faucetWithdraw = require('./utils').faucetWithdraw
-var pollUnspent = require('./utils').pollUnspent
-var pollSummary = require('./utils').pollSummary
+var blockchain = require('./_blockchain')
 
 describe('bitcoinjs-lib (multisig)', function () {
   it('can create a 2-of-3 multisig P2SH address', function () {
@@ -39,15 +36,15 @@ describe('bitcoinjs-lib (multisig)', function () {
     var scriptPubKey = bitcoin.scripts.scriptHashOutput(redeemScript.getHash())
     var address = bitcoin.Address.fromOutputScript(scriptPubKey, bitcoin.networks.testnet).toString()
 
-    // Attempt to send funds to the source address
-    faucetWithdraw(address, 2e4, function (err) {
+    // attempt to send funds to the source address
+    blockchain.t.faucet(address, 2e4, function (err) {
       if (err) return done(err)
 
       // get latest unspents from the address
-      pollUnspent(blockchain, address, function (err, unspents) {
+      blockchain.t.addresses.unspents(address, function (err, unspents) {
         if (err) return done(err)
 
-          // filter small unspents
+        // filter small unspents
         unspents = unspents.filter(function (unspent) {
           return unspent.value > 1e4
         })
@@ -69,11 +66,11 @@ describe('bitcoinjs-lib (multisig)', function () {
         txb.sign(0, keyPairs[2], redeemScript)
 
         // broadcast our transaction
-        blockchain.transactions.propagate(txb.build().toHex(), function (err) {
+        blockchain.t.transactions.propagate(txb.build().toHex(), function (err) {
           if (err) return done(err)
 
           // check that the funds (1e4 Satoshis) indeed arrived at the intended address
-          pollSummary(blockchain, targetAddress, function (err, result) {
+          blockchain.t.addresses.summary(targetAddress, function (err, result) {
             if (err) return done(err)
 
             assert.strictEqual(result.balance, 1e4)
diff --git a/test/integration/utils.js b/test/integration/utils.js
deleted file mode 100644
index bbeae805..00000000
--- a/test/integration/utils.js
+++ /dev/null
@@ -1,49 +0,0 @@
-var httpify = require('httpify')
-
-var BLOCKTRAIL_API_KEY = process.env.BLOCKTRAIL_API_KEY || 'c0bd8155c66e3fb148bb1664adc1e4dacd872548'
-
-function faucetWithdraw (address, amount, callback) {
-  httpify({
-    method: 'POST',
-    url: 'https://api.blocktrail.com/v1/tBTC/faucet/withdrawl?api_key=' + BLOCKTRAIL_API_KEY,
-    headers: { 'Content-Type': 'application/json' },
-    body: JSON.stringify({
-      address: address,
-      amount: amount
-    })
-  }, callback)
-}
-
-function pollUnspent (blockchain, address, done) {
-  blockchain.addresses.unspents(address, function (err, unspents) {
-    if (err) return done(err)
-
-    if (!unspents || unspents.length === 0) {
-      return setTimeout(function () {
-        pollUnspent(blockchain, address, done)
-      }, 200)
-    }
-
-    done(null, unspents)
-  })
-}
-
-function pollSummary (blockchain, address, done) {
-  blockchain.addresses.summary(address, function (err, result) {
-    if (err) return done(err)
-
-    if (result.balance === 0) {
-      return setTimeout(function () {
-        pollSummary(blockchain, address, done)
-      }, 200)
-    }
-
-    done(null, result)
-  })
-}
-
-module.exports = {
-  faucetWithdraw: faucetWithdraw,
-  pollUnspent: pollUnspent,
-  pollSummary: pollSummary
-}