|
| 1 | +var bitcoin = require('../../') |
| 2 | +var dhttp = require('dhttp/200') |
| 3 | + |
| 4 | +var APIPASS = process.env.APIPASS || 'satoshi' |
| 5 | +var APIURL = 'https://api.dcousens.cloud/1' |
| 6 | + |
| 7 | +function broadcast (txHex, callback) { |
| 8 | + dhttp({ |
| 9 | + method: 'PUT', |
| 10 | + url: APIURL + '/t/push', |
| 11 | + body: txHex |
| 12 | + }, callback) |
| 13 | +} |
| 14 | + |
| 15 | +function mine (count, callback) { |
| 16 | + dhttp({ |
| 17 | + method: 'POST', |
| 18 | + url: APIURL + '/r/generate?count=' + count + '&key=' + APIPASS |
| 19 | + }, callback) |
| 20 | +} |
| 21 | + |
| 22 | +function faucet (address, value, callback) { |
| 23 | + dhttp({ |
| 24 | + method: 'POST', |
| 25 | + url: APIURL + '/r/faucet?address=' + address + '&value=' + value + '&key=' + APIPASS |
| 26 | + }, function (err, txId) { |
| 27 | + if (err) return callback(err) |
| 28 | + |
| 29 | + unspents(address, function (err, results) { |
| 30 | + if (err) return callback(err) |
| 31 | + |
| 32 | + callback(null, results.filter(x => x.txId === txId).pop()) |
| 33 | + }) |
| 34 | + }) |
| 35 | +} |
| 36 | + |
| 37 | +function fetch (txId, callback) { |
| 38 | + dhttp({ |
| 39 | + method: 'GET', |
| 40 | + url: APIURL + '/t/' + txId |
| 41 | + }, callback) |
| 42 | +} |
| 43 | + |
| 44 | +function unspents (address, callback) { |
| 45 | + dhttp({ |
| 46 | + method: 'GET', |
| 47 | + url: APIURL + '/a/' + address + '/unspents' |
| 48 | + }, callback) |
| 49 | +} |
| 50 | + |
| 51 | +function verify (txo, callback) { |
| 52 | + let { txId } = txo |
| 53 | + |
| 54 | + fetch(txId, function (err, txHex) { |
| 55 | + if (err) return callback(err) |
| 56 | + |
| 57 | + // TODO: verify address and value |
| 58 | + callback() |
| 59 | + }) |
| 60 | +} |
| 61 | + |
| 62 | +function randomAddress () { |
| 63 | + return bitcoin.ECPair.makeRandom({ |
| 64 | + network: bitcoin.networks.testnet |
| 65 | + }).getAddress() |
| 66 | +} |
| 67 | + |
| 68 | +module.exports = { |
| 69 | + broadcast: broadcast, |
| 70 | + faucet: faucet, |
| 71 | + fetch: fetch, |
| 72 | + mine: mine, |
| 73 | + network: bitcoin.networks.testnet, |
| 74 | + unspents: unspents, |
| 75 | + verify: verify, |
| 76 | + randomAddress: randomAddress, |
| 77 | + RANDOM_ADDRESS: randomAddress() |
| 78 | +} |
0 commit comments