diff --git a/package.json b/package.json index 2fd829cd7..b02c0b3f1 100644 --- a/package.json +++ b/package.json @@ -57,7 +57,8 @@ "async": "^0.9.0", "browserify": "^10.0.0", "bs58": "^2.0.1", - "cb-helloblock": "^0.4.13", + "cb-blockr": "^3.1.1", + "cb-insight": "git://github.com/weilu/cb-insight", "coveralls": "^2.11.2", "istanbul": "^0.3.5", "mocha": "^2.2.0", diff --git a/test/integration/advanced.js b/test/integration/advanced.js index e510f4f7c..81998c442 100644 --- a/test/integration/advanced.js +++ b/test/integration/advanced.js @@ -2,7 +2,9 @@ var assert = require('assert') var bitcoin = require('../../') -var blockchain = new (require('cb-helloblock'))('testnet') +var blockchain = new (require('cb-insight'))('https://test-insight.bitpay.com') +var faucetWithdraw = require('./utils').faucetWithdraw +var pollUnspent = require('./utils').pollUnspent describe('bitcoinjs-lib (advanced)', function () { it('can sign a Bitcoin message', function () { @@ -29,10 +31,10 @@ describe('bitcoinjs-lib (advanced)', function () { }) var address = keyPair.getAddress().toString() - blockchain.addresses.__faucetWithdraw(address, 2e4, function (err) { + faucetWithdraw(address, 2e4, function (err) { if (err) return done(err) - blockchain.addresses.unspents(address, function (err, unspents) { + pollUnspent(blockchain, address, function (err, unspents) { if (err) return done(err) var tx = new bitcoin.TransactionBuilder() diff --git a/test/integration/crypto.js b/test/integration/crypto.js index 60e2cea17..3d8f3654f 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-helloblock'))('bitcoin') +var blockchain = new (require('cb-blockr'))('bitcoin') var crypto = require('crypto') describe('bitcoinjs-lib (crypto)', function () { diff --git a/test/integration/multisig.js b/test/integration/multisig.js index 4e2a8c078..15d5a737d 100644 --- a/test/integration/multisig.js +++ b/test/integration/multisig.js @@ -2,7 +2,10 @@ var assert = require('assert') var bitcoin = require('../../') -var blockchain = new (require('cb-helloblock'))('testnet') +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 describe('bitcoinjs-lib (multisig)', function () { it('can create a 2-of-3 multisig P2SH address', function () { @@ -37,11 +40,11 @@ describe('bitcoinjs-lib (multisig)', function () { var address = bitcoin.Address.fromOutputScript(scriptPubKey, bitcoin.networks.testnet).toString() // Attempt to send funds to the source address - blockchain.addresses.__faucetWithdraw(address, 2e4, function (err) { + faucetWithdraw(address, 2e4, function (err) { if (err) return done(err) // get latest unspents from the address - blockchain.addresses.unspents(address, function (err, unspents) { + pollUnspent(blockchain, address, function (err, unspents) { if (err) return done(err) // filter small unspents @@ -70,7 +73,7 @@ describe('bitcoinjs-lib (multisig)', function () { if (err) return done(err) // check that the funds (1e4 Satoshis) indeed arrived at the intended address - blockchain.addresses.summary(targetAddress, function (err, result) { + pollSummary(blockchain, 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 new file mode 100644 index 000000000..17c3275b3 --- /dev/null +++ b/test/integration/utils.js @@ -0,0 +1,42 @@ +var https = require('https') + +function faucetWithdraw (address, amount, done) { + var url = 'https://coconut-macaroon.herokuapp.com/withdrawal?address=' + address + '&amount=' + amount + https.get(url, function (res) { + res.statusCode === 200 ? done(null) : done(new Error('non-200 status: ' + res.statusCode)) + }).on('error', done) +} + +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 +}