2018-06-25 08:25:12 +02:00
|
|
|
const assert = require('assert')
|
|
|
|
const bitcoin = require('../../')
|
|
|
|
const dhttp = require('dhttp/200')
|
2018-01-31 02:46:35 +01:00
|
|
|
|
2018-06-25 08:25:12 +02:00
|
|
|
const APIPASS = process.env.APIPASS || 'satoshi'
|
|
|
|
const APIURL = 'https://api.dcousens.cloud/1'
|
2018-01-31 02:46:35 +01:00
|
|
|
|
|
|
|
function broadcast (txHex, callback) {
|
|
|
|
dhttp({
|
|
|
|
method: 'PUT',
|
|
|
|
url: APIURL + '/t/push',
|
|
|
|
body: txHex
|
|
|
|
}, callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
function mine (count, callback) {
|
|
|
|
dhttp({
|
|
|
|
method: 'POST',
|
|
|
|
url: APIURL + '/r/generate?count=' + count + '&key=' + APIPASS
|
|
|
|
}, callback)
|
|
|
|
}
|
|
|
|
|
2018-02-01 02:54:20 +01:00
|
|
|
function height (callback) {
|
|
|
|
dhttp({
|
|
|
|
method: 'GET',
|
|
|
|
url: APIURL + '/b/best/height'
|
|
|
|
}, callback)
|
|
|
|
}
|
|
|
|
|
2018-01-31 02:46:35 +01:00
|
|
|
function faucet (address, value, callback) {
|
|
|
|
dhttp({
|
|
|
|
method: 'POST',
|
|
|
|
url: APIURL + '/r/faucet?address=' + address + '&value=' + value + '&key=' + APIPASS
|
|
|
|
}, function (err, txId) {
|
|
|
|
if (err) return callback(err)
|
|
|
|
|
|
|
|
unspents(address, function (err, results) {
|
|
|
|
if (err) return callback(err)
|
|
|
|
|
|
|
|
callback(null, results.filter(x => x.txId === txId).pop())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function fetch (txId, callback) {
|
|
|
|
dhttp({
|
|
|
|
method: 'GET',
|
2018-02-01 03:14:34 +01:00
|
|
|
url: APIURL + '/t/' + txId + '/json'
|
2018-01-31 02:46:35 +01:00
|
|
|
}, callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
function unspents (address, callback) {
|
|
|
|
dhttp({
|
|
|
|
method: 'GET',
|
|
|
|
url: APIURL + '/a/' + address + '/unspents'
|
|
|
|
}, callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
function verify (txo, callback) {
|
2018-02-01 03:14:34 +01:00
|
|
|
fetch(txo.txId, function (err, tx) {
|
2018-01-31 02:46:35 +01:00
|
|
|
if (err) return callback(err)
|
|
|
|
|
2018-06-25 08:37:45 +02:00
|
|
|
const txoActual = tx.outs[txo.vout]
|
2018-02-01 03:14:34 +01:00
|
|
|
if (txo.address) assert.strictEqual(txoActual.address, txo.address)
|
|
|
|
if (txo.value) assert.strictEqual(txoActual.value, txo.value)
|
2018-01-31 02:46:35 +01:00
|
|
|
callback()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-05-22 09:43:25 +02:00
|
|
|
// TODO: remove
|
2018-06-25 08:24:37 +02:00
|
|
|
const baddress = bitcoin.address
|
|
|
|
const bcrypto = bitcoin.crypto
|
2018-05-22 09:43:25 +02:00
|
|
|
function getAddress (node, network) {
|
|
|
|
network = network || bitcoin.networks.bitcoin
|
2018-05-30 03:19:46 +02:00
|
|
|
return baddress.toBase58Check(bcrypto.hash160(node.publicKey), network.pubKeyHash)
|
2018-05-22 09:43:25 +02:00
|
|
|
}
|
|
|
|
|
2018-01-31 02:46:35 +01:00
|
|
|
function randomAddress () {
|
2018-05-22 09:43:25 +02:00
|
|
|
return getAddress(bitcoin.ECPair.makeRandom({
|
2018-01-31 02:46:35 +01:00
|
|
|
network: bitcoin.networks.testnet
|
2018-05-22 09:43:25 +02:00
|
|
|
}), bitcoin.networks.testnet)
|
2018-01-31 02:46:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
broadcast: broadcast,
|
|
|
|
faucet: faucet,
|
|
|
|
fetch: fetch,
|
2018-02-01 02:54:20 +01:00
|
|
|
height: height,
|
2018-01-31 02:46:35 +01:00
|
|
|
mine: mine,
|
|
|
|
network: bitcoin.networks.testnet,
|
|
|
|
unspents: unspents,
|
|
|
|
verify: verify,
|
|
|
|
randomAddress: randomAddress,
|
|
|
|
RANDOM_ADDRESS: randomAddress()
|
|
|
|
}
|