bitcoinjs-lib/test/integration/_regtest.js

122 lines
2.8 KiB
JavaScript
Raw Permalink Normal View History

2018-06-25 08:25:12 +02:00
const assert = require('assert')
const bitcoin = require('../../')
const dhttp = require('dhttp/200')
2018-06-25 08:25:12 +02:00
const APIPASS = process.env.APIPASS || 'satoshi'
const APIURL = 'https://regtest.bitbank.cc/1'
const NETWORK = bitcoin.networks.testnet
function broadcast (txHex, callback) {
dhttp({
method: 'POST',
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)
}
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)
const unspents = results.filter(x => x.txId === txId)
if (unspents.length === 0) return callback(new Error('Missing unspent'))
callback(null, unspents.pop())
})
})
}
function faucetComplex (output, value, callback) {
const keyPair = bitcoin.ECPair.makeRandom({ network: NETWORK })
const p2pkh = bitcoin.payments.p2pkh({ pubkey: keyPair.publicKey, network: NETWORK })
faucet(p2pkh.address, value * 2, (err, unspent) => {
if (err) return callback(err)
const txvb = new bitcoin.TransactionBuilder(NETWORK)
txvb.addInput(unspent.txId, unspent.vout, null, p2pkh.output)
txvb.addOutput(output, value)
txvb.sign(0, keyPair)
const txv = txvb.build()
broadcast(txv.toHex(), function (err) {
if (err) return callback(err)
return callback(null, {
txId: txv.getId(),
vout: 0,
value
})
})
})
}
function fetch (txId, callback) {
dhttp({
method: 'GET',
url: APIURL + '/t/' + txId + '/json'
}, callback)
}
function unspents (address, callback) {
dhttp({
method: 'GET',
url: APIURL + '/a/' + address + '/unspents'
}, callback)
}
function verify (txo, callback) {
fetch(txo.txId, function (err, tx) {
if (err) return callback(err)
const txoActual = tx.outs[txo.vout]
if (txo.address) assert.strictEqual(txoActual.address, txo.address)
if (txo.value) assert.strictEqual(txoActual.value, txo.value)
callback()
})
}
2018-05-22 09:43:25 +02:00
function getAddress (node, network) {
2018-06-27 05:54:47 +02:00
return bitcoin.payments.p2pkh({ pubkey: node.publicKey, network }).address
2018-05-22 09:43:25 +02:00
}
function randomAddress () {
2018-05-22 09:43:25 +02:00
return getAddress(bitcoin.ECPair.makeRandom({
network: bitcoin.networks.testnet
2018-05-22 09:43:25 +02:00
}), bitcoin.networks.testnet)
}
module.exports = {
broadcast,
faucet,
faucetComplex,
fetch,
height,
mine,
network: NETWORK,
unspents,
verify,
randomAddress,
RANDOM_ADDRESS: randomAddress()
}