From a2b4558a0b423c0462098a9c4a7c8bcc75e844bf Mon Sep 17 00:00:00 2001 From: Wei Lu Date: Sat, 30 May 2015 18:58:07 +0800 Subject: [PATCH 1/5] replace helloblock with insight and blockr --- package.json | 3 ++- test/integration/advanced.js | 5 +++-- test/integration/crypto.js | 2 +- test/integration/multisig.js | 5 +++-- test/integration/utils.js | 12 ++++++++++++ 5 files changed, 21 insertions(+), 6 deletions(-) create mode 100644 test/integration/utils.js 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..65e9a5b35 100644 --- a/test/integration/advanced.js +++ b/test/integration/advanced.js @@ -2,7 +2,8 @@ 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 describe('bitcoinjs-lib (advanced)', function () { it('can sign a Bitcoin message', function () { @@ -29,7 +30,7 @@ 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) { 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..414d3afa4 100644 --- a/test/integration/multisig.js +++ b/test/integration/multisig.js @@ -2,7 +2,8 @@ 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 describe('bitcoinjs-lib (multisig)', function () { it('can create a 2-of-3 multisig P2SH address', function () { @@ -37,7 +38,7 @@ 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 diff --git a/test/integration/utils.js b/test/integration/utils.js new file mode 100644 index 000000000..d481973c1 --- /dev/null +++ b/test/integration/utils.js @@ -0,0 +1,12 @@ +var https = require('https') + +function faucetWithdraw(address, amount, done) { + var url = "https://coconut-macaroon.herokuapp.com/bitcoin/testnet/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) +} + +module.exports = { + faucetWithdraw: faucetWithdraw +} From 713f038583725bb97dfada9a750af9194b5cb094 Mon Sep 17 00:00:00 2001 From: Wei Lu Date: Sat, 30 May 2015 19:17:42 +0800 Subject: [PATCH 2/5] poll address unspent until non-empty or timeout --- test/integration/advanced.js | 3 ++- test/integration/multisig.js | 3 ++- test/integration/utils.js | 18 +++++++++++++++++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/test/integration/advanced.js b/test/integration/advanced.js index 65e9a5b35..81998c442 100644 --- a/test/integration/advanced.js +++ b/test/integration/advanced.js @@ -4,6 +4,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 describe('bitcoinjs-lib (advanced)', function () { it('can sign a Bitcoin message', function () { @@ -33,7 +34,7 @@ describe('bitcoinjs-lib (advanced)', function () { 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/multisig.js b/test/integration/multisig.js index 414d3afa4..5a9d88a01 100644 --- a/test/integration/multisig.js +++ b/test/integration/multisig.js @@ -4,6 +4,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 describe('bitcoinjs-lib (multisig)', function () { it('can create a 2-of-3 multisig P2SH address', function () { @@ -42,7 +43,7 @@ describe('bitcoinjs-lib (multisig)', function () { 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 diff --git a/test/integration/utils.js b/test/integration/utils.js index d481973c1..db9f68d76 100644 --- a/test/integration/utils.js +++ b/test/integration/utils.js @@ -7,6 +7,22 @@ function faucetWithdraw(address, amount, done) { }).on('error', done) } +function pollUnspent(blockchain, address, done) { + blockchain.addresses.unspents(address, function (err, unspents) { + if (err) return done(err) + + if(unspents == null || unspents.length === 0) { + return setTimeout(function() { + pollUnspent(blockchain, address, done) + }, 200) + } + + done(null, unspents) + }) + +} + module.exports = { - faucetWithdraw: faucetWithdraw + faucetWithdraw: faucetWithdraw, + pollUnspent: pollUnspent } From 70d423f59637b29bb96d09d1c87c07dfe67bb027 Mon Sep 17 00:00:00 2001 From: Wei Lu Date: Sat, 30 May 2015 20:20:04 +0800 Subject: [PATCH 3/5] update faucet url --- test/integration/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/utils.js b/test/integration/utils.js index db9f68d76..b31f3ed5b 100644 --- a/test/integration/utils.js +++ b/test/integration/utils.js @@ -1,7 +1,7 @@ var https = require('https') function faucetWithdraw(address, amount, done) { - var url = "https://coconut-macaroon.herokuapp.com/bitcoin/testnet/withdrawal?address=" + address + "&amount=" + amount + 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) From 9a921ad1818af4431ee5898f7eaeef72d85804a4 Mon Sep 17 00:00:00 2001 From: Wei Lu Date: Sat, 30 May 2015 21:19:57 +0800 Subject: [PATCH 4/5] poll address summary until balance non-zero or timeout --- test/integration/multisig.js | 3 ++- test/integration/utils.js | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/test/integration/multisig.js b/test/integration/multisig.js index 5a9d88a01..15d5a737d 100644 --- a/test/integration/multisig.js +++ b/test/integration/multisig.js @@ -5,6 +5,7 @@ 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 describe('bitcoinjs-lib (multisig)', function () { it('can create a 2-of-3 multisig P2SH address', function () { @@ -72,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 index b31f3ed5b..91aafafd9 100644 --- a/test/integration/utils.js +++ b/test/integration/utils.js @@ -19,10 +19,24 @@ function pollUnspent(blockchain, address, done) { 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 + pollUnspent: pollUnspent, + pollSummary: pollSummary } From 4028bd50f27e8a6d2ac2a2fb04f4cdb15ac3e479 Mon Sep 17 00:00:00 2001 From: Wei Lu Date: Sat, 30 May 2015 21:30:37 +0800 Subject: [PATCH 5/5] comply with project coding style --- test/integration/utils.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/test/integration/utils.js b/test/integration/utils.js index 91aafafd9..17c3275b3 100644 --- a/test/integration/utils.js +++ b/test/integration/utils.js @@ -1,18 +1,18 @@ 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)) +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) { +function pollUnspent (blockchain, address, done) { blockchain.addresses.unspents(address, function (err, unspents) { if (err) return done(err) - if(unspents == null || unspents.length === 0) { - return setTimeout(function() { + if (!unspents || unspents.length === 0) { + return setTimeout(function () { pollUnspent(blockchain, address, done) }, 200) } @@ -21,12 +21,12 @@ function pollUnspent(blockchain, address, done) { }) } -function pollSummary(blockchain, address, done) { +function pollSummary (blockchain, address, done) { blockchain.addresses.summary(address, function (err, result) { if (err) return done(err) - if(result.balance == 0) { - return setTimeout(function() { + if (result.balance === 0) { + return setTimeout(function () { pollSummary(blockchain, address, done) }, 200) }