Merge pull request #431 from bitcoinjs/blocktrailfaucet

use cb-http-client with the BlockTrail API
This commit is contained in:
Daniel Cousens 2015-08-07 21:13:41 +10:00
commit a3b0590504
6 changed files with 41 additions and 71 deletions

View file

@ -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",

View file

@ -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
}

View file

@ -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)

View file

@ -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)

View file

@ -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,12 +36,12 @@ 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
@ -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)

View file

@ -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
}