feat: backport remote BTC node support

This commit is contained in:
Sérgio Salgado 2023-02-01 15:16:35 +00:00
parent 24473de6d5
commit 1b6ba5e6dc
15 changed files with 272 additions and 55 deletions

View file

@ -7,6 +7,8 @@ const request = require('request-promise')
const { utils: coinUtils } = require('@lamassu/coins')
const logger = require('../../logger')
const { isRemoteNode, isRemoteWallet } = require('../../environment-helper')
const { isEnvironmentValid } = require('../../blockchain/install')
const BLOCKCHAIN_DIR = process.env.BLOCKCHAIN_DIR
@ -28,7 +30,7 @@ function fetch (account = {}, method, params) {
if (_.isNil(account.port)) throw new Error('port attribute required for jsonRpc')
const url = _.defaultTo(`http://localhost:${account.port}`, account.url)
const url = _.defaultTo(`http://${account.host}:${account.port}`, account.url)
return axios({
method: 'post',
@ -109,15 +111,30 @@ function parseConf (confPath) {
function rpcConfig (cryptoRec) {
try {
if (isRemoteWallet(cryptoRec) && isEnvironmentValid(cryptoRec)) {
return {
username: process.env[`${cryptoRec.cryptoCode}_NODE_USER`],
password: process.env[`${cryptoRec.cryptoCode}_NODE_PASSWORD`],
host: process.env[`${cryptoRec.cryptoCode}_NODE_RPC_HOST`],
port: process.env[`${cryptoRec.cryptoCode}_NODE_RPC_PORT`]
}
}
const configPath = coinUtils.configPath(cryptoRec, BLOCKCHAIN_DIR)
const config = parseConf(configPath)
return {
username: config.rpcuser,
password: config.rpcpassword,
host: 'localhost',
port: config.rpcport || cryptoRec.defaultPort
}
} catch (err) {
logger.error('Wallet is currently not installed!')
if (!isEnvironmentValid(cryptoRec)) {
logger.error('Environment is not correctly setup for remote wallet usage!')
} else {
logger.error('Wallet is currently not installed!')
}
return {
port: cryptoRec.defaultPort
}

View file

@ -5,6 +5,7 @@ const BN = require('../../../bn')
const E = require('../../../error')
const logger = require('../../../logger')
const { utils: coinUtils } = require('@lamassu/coins')
const { isDevMode } = require('../../../environment-helper')
const cryptoRec = coinUtils.getCryptoCurrency('BTC')
const unitScale = cryptoRec.unitScale
@ -36,15 +37,15 @@ function checkCryptoCode (cryptoCode) {
function accountBalance (cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getwalletinfo'))
.then(({ balance }) => new BN(balance).shiftedBy(unitScale).decimalPlaces(0))
.then(() => fetch('getbalances'))
.then(({ mine }) => new BN(mine.trusted).shiftedBy(unitScale).decimalPlaces(0))
.catch(errorHandle)
}
function accountUnconfirmedBalance (cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getwalletinfo'))
.then(({ unconfirmed_balance: balance }) => new BN(balance).shiftedBy(unitScale).decimalPlaces(0))
.then(() => fetch('getbalances'))
.then(({ mine }) => new BN(mine.untrusted_pending).plus(mine.immature).shiftedBy(unitScale).decimalPlaces(0))
.catch(errorHandle)
}
@ -62,7 +63,7 @@ function estimateFee () {
function calculateFeeDiscount (feeMultiplier) {
// 0 makes bitcoind do automatic fee selection
const AUTOMATIC_FEE = 0
const AUTOMATIC_FEE = isDevMode() ? 0.01 : 0
if (!feeMultiplier || feeMultiplier.eq(1)) return AUTOMATIC_FEE
return estimateFee()
.then(estimatedFee => {

View file

@ -3,7 +3,6 @@ const BN = require('../../../bn')
const E = require('../../../error')
const _ = require('lodash/fp')
const ENV = process.env.NODE_ENV === undefined || process.env.NODE_ENV === 'development' ? 'development' : 'production'
const SUPPORTED_COINS = ['BTC']
const axios = require('axios').create({