From aa3e5faad251bf1706e807c6e45b3f4d89a52567 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Fri, 7 Aug 2015 14:47:07 +1000 Subject: [PATCH] use cb-http-client with BlockTrail --- package.json | 3 +- test/integration/_blockchain.js | 23 ++++++++++++++++ test/integration/advanced.js | 12 ++++---- test/integration/crypto.js | 8 ++++-- test/integration/multisig.js | 17 +++++------- test/integration/utils.js | 49 --------------------------------- 6 files changed, 41 insertions(+), 71 deletions(-) create mode 100644 test/integration/_blockchain.js delete mode 100644 test/integration/utils.js diff --git a/package.json b/package.json index 136e7a2..afcac40 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", diff --git a/test/integration/_blockchain.js b/test/integration/_blockchain.js new file mode 100644 index 0000000..7d215ec --- /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 d919b74..366c4f3 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 172e6a1..7c97cd0 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 f56b368..abe1e8d 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 bbeae80..0000000 --- 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 -}