2015-11-06 00:49:06 +01:00
|
|
|
var async = require('async')
|
2015-08-07 06:47:07 +02:00
|
|
|
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 })
|
2015-11-06 00:49:06 +01:00
|
|
|
|
|
|
|
testnet.faucet = function faucet (address, amount, done) {
|
|
|
|
var unspents = []
|
|
|
|
|
|
|
|
async.whilst(
|
|
|
|
function condition () { return unspents.length === 0 },
|
|
|
|
function f (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
|
|
|
|
})
|
|
|
|
}, function (err) {
|
|
|
|
if (err) return callback(err)
|
|
|
|
|
|
|
|
testnet.addresses.unspents(address, function (err, result) {
|
|
|
|
if (err) return callback(err)
|
|
|
|
|
|
|
|
// filter small unspents
|
|
|
|
unspents = result.filter(function (unspent) {
|
|
|
|
return unspent.value > 1e3
|
|
|
|
})
|
|
|
|
|
|
|
|
callback()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
function (err) {
|
|
|
|
if (err) return done(err)
|
|
|
|
|
|
|
|
done(null, unspents)
|
|
|
|
}
|
|
|
|
)
|
2015-08-07 06:47:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
m: mainnet,
|
|
|
|
t: testnet
|
|
|
|
}
|