generalize bitcoind json-rpc
This commit is contained in:
parent
f70211d774
commit
2c525c1e5c
11 changed files with 468 additions and 121 deletions
|
|
@ -1,52 +1,24 @@
|
|||
const path = require('path')
|
||||
const os = require('os')
|
||||
const RpcClient = require('bitcoind-rpc')
|
||||
const fs = require('fs')
|
||||
const pify = require('pify')
|
||||
const jsonRpc = require('../../common/json-rpc')
|
||||
|
||||
const path = require('path')
|
||||
const options = require('../../../options')
|
||||
const BN = require('../../../bn')
|
||||
const E = require('../../../error')
|
||||
|
||||
const NAME = 'Bitcoind'
|
||||
|
||||
const DEFAULT_PORT = 8332
|
||||
const SATOSHI_SHIFT = 8
|
||||
|
||||
const configPath = path.resolve(os.homedir(), '.bitcoin', 'bitcoin.conf')
|
||||
const pluginConfig = {
|
||||
account: '',
|
||||
bitcoindConfigurationPath: configPath
|
||||
const configPath = path.resolve(options.blockchainDir, 'bitcoin.conf')
|
||||
const config = jsonRpc.parseConf(configPath)
|
||||
|
||||
const rpcConfig = {
|
||||
username: config.rpcuser,
|
||||
password: config.rpcpassword,
|
||||
port: config.rpcport || DEFAULT_PORT
|
||||
}
|
||||
|
||||
function initRpc () {
|
||||
const bitcoindConf = parseConf(pluginConfig.bitcoindConfigurationPath)
|
||||
|
||||
const rpcConfig = {
|
||||
protocol: 'http',
|
||||
user: bitcoindConf.rpcuser,
|
||||
pass: bitcoindConf.rpcpassword
|
||||
}
|
||||
|
||||
return new RpcClient(rpcConfig)
|
||||
}
|
||||
|
||||
/*
|
||||
* initialize RpcClient
|
||||
*/
|
||||
function parseConf (confPath) {
|
||||
const conf = fs.readFileSync(confPath)
|
||||
const lines = conf.toString().split('\n')
|
||||
|
||||
const res = {}
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
const keyVal = lines[i].split('=')
|
||||
|
||||
// skip when value is empty
|
||||
if (!keyVal[1]) continue
|
||||
|
||||
res[keyVal[0]] = keyVal[1]
|
||||
}
|
||||
|
||||
return res
|
||||
function fetch (method, params) {
|
||||
return jsonRpc.fetch(rpcConfig, method, params)
|
||||
}
|
||||
|
||||
function checkCryptoCode (cryptoCode) {
|
||||
|
|
@ -54,20 +26,10 @@ function checkCryptoCode (cryptoCode) {
|
|||
return Promise.resolve()
|
||||
}
|
||||
|
||||
function accountBalance (account, cryptoCode, confirmations) {
|
||||
function accountBalance (acount, cryptoCode, confirmations) {
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => {
|
||||
const rpc = initRpc()
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
rpc.getBalance(pluginConfig.account, confirmations, (err, result) => {
|
||||
if (err) return reject(err)
|
||||
if (result.error) reject(new Error(err))
|
||||
|
||||
return resolve(BN(result.result).shift(SATOSHI_SHIFT).round())
|
||||
})
|
||||
})
|
||||
})
|
||||
.then(() => fetch('getbalance', ['', confirmations]))
|
||||
.then(r => BN(r).shift(SATOSHI_SHIFT).round())
|
||||
}
|
||||
|
||||
// We want a balance that includes all spends (0 conf) but only deposits that
|
||||
|
|
@ -77,42 +39,25 @@ function balance (account, cryptoCode) {
|
|||
}
|
||||
|
||||
function sendCoins (account, address, cryptoAtoms, cryptoCode) {
|
||||
const rpc = initRpc()
|
||||
const confirmations = 1
|
||||
const bitcoins = cryptoAtoms.shift(-SATOSHI_SHIFT).toFixed(8)
|
||||
|
||||
return checkCryptoCode(cryptoCode)
|
||||
.then(() => {
|
||||
return new Promise((resolve, reject) => {
|
||||
rpc.sendFrom(pluginConfig.account, address, bitcoins, confirmations, (err, result) => {
|
||||
if (err) {
|
||||
if (err.code === -6) return reject(new E.InsufficientFundsError())
|
||||
return reject(err)
|
||||
}
|
||||
|
||||
resolve(result.result)
|
||||
})
|
||||
})
|
||||
.then(() => fetch('sendfrom', [address, bitcoins, confirmations]))
|
||||
.catch(err => {
|
||||
if (err.code === -6) throw new E.InsufficientFundsError()
|
||||
throw err
|
||||
})
|
||||
}
|
||||
|
||||
function newAddress (account, info) {
|
||||
return checkCryptoCode(info.cryptoCode)
|
||||
.then(() => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const rpc = initRpc()
|
||||
rpc.getNewAddress((err, result) => {
|
||||
if (err) return reject(err)
|
||||
resolve(result.result)
|
||||
})
|
||||
})
|
||||
})
|
||||
.then(() => fetch('getnewaddress'))
|
||||
}
|
||||
|
||||
function addressBalance (address, confs) {
|
||||
const rpc = initRpc()
|
||||
return pify(rpc.getReceivedByAddress.bind(rpc))(address, confs)
|
||||
.then(r => BN(r.result).shift(SATOSHI_SHIFT).round())
|
||||
return fetch('getreceivedbyaddress', [address, confs])
|
||||
.then(r => BN(r).shift(SATOSHI_SHIFT).round())
|
||||
}
|
||||
|
||||
function confirmedBalance (address, cryptoCode) {
|
||||
|
|
@ -159,10 +104,7 @@ function newFunding (account, cryptoCode) {
|
|||
}
|
||||
|
||||
module.exports = {
|
||||
NAME,
|
||||
balance,
|
||||
pendingBalance,
|
||||
confirmedBalance,
|
||||
sendCoins,
|
||||
newAddress,
|
||||
getStatus,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue