Merge pull request #431 from bitcoinjs/blocktrailfaucet
use cb-http-client with the BlockTrail API
This commit is contained in:
commit
a3b0590504
6 changed files with 41 additions and 71 deletions
|
@ -57,8 +57,7 @@
|
||||||
"async": "^0.9.0",
|
"async": "^0.9.0",
|
||||||
"browserify": "^10.0.0",
|
"browserify": "^10.0.0",
|
||||||
"bs58": "^2.0.1",
|
"bs58": "^2.0.1",
|
||||||
"cb-blockr": "^3.1.1",
|
"cb-http-client": "^0.2.0",
|
||||||
"cb-insight": "git://github.com/weilu/cb-insight",
|
|
||||||
"coveralls": "^2.11.2",
|
"coveralls": "^2.11.2",
|
||||||
"httpify": "^1.0.0",
|
"httpify": "^1.0.0",
|
||||||
"istanbul": "^0.3.5",
|
"istanbul": "^0.3.5",
|
||||||
|
|
23
test/integration/_blockchain.js
Normal file
23
test/integration/_blockchain.js
Normal 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
|
||||||
|
}
|
|
@ -2,9 +2,7 @@
|
||||||
|
|
||||||
var assert = require('assert')
|
var assert = require('assert')
|
||||||
var bitcoin = require('../../')
|
var bitcoin = require('../../')
|
||||||
var blockchain = new (require('cb-insight'))('https://test-insight.bitpay.com')
|
var blockchain = require('./_blockchain')
|
||||||
var faucetWithdraw = require('./utils').faucetWithdraw
|
|
||||||
var pollUnspent = require('./utils').pollUnspent
|
|
||||||
|
|
||||||
describe('bitcoinjs-lib (advanced)', function () {
|
describe('bitcoinjs-lib (advanced)', function () {
|
||||||
it('can sign a Bitcoin message', 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 keyPair = bitcoin.ECPair.makeRandom({ network: network })
|
||||||
var address = keyPair.getAddress()
|
var address = keyPair.getAddress()
|
||||||
|
|
||||||
faucetWithdraw(address, 2e4, function (err) {
|
blockchain.t.faucet(address, 2e4, function (err) {
|
||||||
if (err) return done(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)
|
if (err) return done(err)
|
||||||
|
|
||||||
var tx = new bitcoin.TransactionBuilder(network)
|
var tx = new bitcoin.TransactionBuilder(network)
|
||||||
|
@ -48,11 +46,11 @@ describe('bitcoinjs-lib (advanced)', function () {
|
||||||
|
|
||||||
var txBuilt = tx.build()
|
var txBuilt = tx.build()
|
||||||
|
|
||||||
blockchain.transactions.propagate(txBuilt.toHex(), function (err) {
|
blockchain.t.transactions.propagate(txBuilt.toHex(), function (err) {
|
||||||
if (err) return done(err)
|
if (err) return done(err)
|
||||||
|
|
||||||
// check that the message was propagated
|
// 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)
|
if (err) return done(err)
|
||||||
|
|
||||||
var actual = bitcoin.Transaction.fromHex(transaction.txHex)
|
var actual = bitcoin.Transaction.fromHex(transaction.txHex)
|
||||||
|
|
|
@ -4,7 +4,7 @@ var assert = require('assert')
|
||||||
var async = require('async')
|
var async = require('async')
|
||||||
var bigi = require('bigi')
|
var bigi = require('bigi')
|
||||||
var bitcoin = require('../../')
|
var bitcoin = require('../../')
|
||||||
var blockchain = new (require('cb-blockr'))('bitcoin')
|
var blockchain = require('./_blockchain')
|
||||||
var crypto = require('crypto')
|
var crypto = require('crypto')
|
||||||
|
|
||||||
describe('bitcoinjs-lib (crypto)', function () {
|
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) {
|
it('can recover a private key from duplicate R values', function (done) {
|
||||||
|
this.timeout(10000)
|
||||||
|
|
||||||
var inputs = [
|
var inputs = [
|
||||||
{
|
{
|
||||||
txId: 'f4c16475f2a6e9c602e4a287f9db3040e319eb9ece74761a4b84bc820fbeef50',
|
txId: 'f4c16475f2a6e9c602e4a287f9db3040e319eb9ece74761a4b84bc820fbeef50',
|
||||||
|
@ -105,7 +107,7 @@ describe('bitcoinjs-lib (crypto)', function () {
|
||||||
var txIds = inputs.map(function (x) { return x.txId })
|
var txIds = inputs.map(function (x) { return x.txId })
|
||||||
|
|
||||||
// first retrieve the relevant transactions
|
// first retrieve the relevant transactions
|
||||||
blockchain.transactions.get(txIds, function (err, results) {
|
blockchain.m.transactions.get(txIds, function (err, results) {
|
||||||
assert.ifError(err)
|
assert.ifError(err)
|
||||||
|
|
||||||
var transactions = {}
|
var transactions = {}
|
||||||
|
@ -125,7 +127,7 @@ describe('bitcoinjs-lib (crypto)', function () {
|
||||||
var prevVout = transaction.ins[input.vout].index
|
var prevVout = transaction.ins[input.vout].index
|
||||||
|
|
||||||
tasks.push(function (callback) {
|
tasks.push(function (callback) {
|
||||||
blockchain.transactions.get(prevOutTxId, function (err, result) {
|
blockchain.m.transactions.get(prevOutTxId, function (err, result) {
|
||||||
if (err) return callback(err)
|
if (err) return callback(err)
|
||||||
|
|
||||||
var prevOut = bitcoin.Transaction.fromHex(result.txHex)
|
var prevOut = bitcoin.Transaction.fromHex(result.txHex)
|
||||||
|
|
|
@ -2,10 +2,7 @@
|
||||||
|
|
||||||
var assert = require('assert')
|
var assert = require('assert')
|
||||||
var bitcoin = require('../../')
|
var bitcoin = require('../../')
|
||||||
var blockchain = new (require('cb-insight'))('https://test-insight.bitpay.com')
|
var blockchain = require('./_blockchain')
|
||||||
var faucetWithdraw = require('./utils').faucetWithdraw
|
|
||||||
var pollUnspent = require('./utils').pollUnspent
|
|
||||||
var pollSummary = require('./utils').pollSummary
|
|
||||||
|
|
||||||
describe('bitcoinjs-lib (multisig)', function () {
|
describe('bitcoinjs-lib (multisig)', function () {
|
||||||
it('can create a 2-of-3 multisig P2SH address', 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 scriptPubKey = bitcoin.scripts.scriptHashOutput(redeemScript.getHash())
|
||||||
var address = bitcoin.Address.fromOutputScript(scriptPubKey, bitcoin.networks.testnet).toString()
|
var address = bitcoin.Address.fromOutputScript(scriptPubKey, bitcoin.networks.testnet).toString()
|
||||||
|
|
||||||
// Attempt to send funds to the source address
|
// attempt to send funds to the source address
|
||||||
faucetWithdraw(address, 2e4, function (err) {
|
blockchain.t.faucet(address, 2e4, function (err) {
|
||||||
if (err) return done(err)
|
if (err) return done(err)
|
||||||
|
|
||||||
// get latest unspents from the address
|
// 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)
|
if (err) return done(err)
|
||||||
|
|
||||||
// filter small unspents
|
// filter small unspents
|
||||||
unspents = unspents.filter(function (unspent) {
|
unspents = unspents.filter(function (unspent) {
|
||||||
return unspent.value > 1e4
|
return unspent.value > 1e4
|
||||||
})
|
})
|
||||||
|
@ -69,11 +66,11 @@ describe('bitcoinjs-lib (multisig)', function () {
|
||||||
txb.sign(0, keyPairs[2], redeemScript)
|
txb.sign(0, keyPairs[2], redeemScript)
|
||||||
|
|
||||||
// broadcast our transaction
|
// 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)
|
if (err) return done(err)
|
||||||
|
|
||||||
// check that the funds (1e4 Satoshis) indeed arrived at the intended address
|
// 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)
|
if (err) return done(err)
|
||||||
|
|
||||||
assert.strictEqual(result.balance, 1e4)
|
assert.strictEqual(result.balance, 1e4)
|
||||||
|
|
|
@ -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
|
|
||||||
}
|
|
Loading…
Add table
Reference in a new issue