Merge pull request #1175 from chaotixkilla/fix-btc-wallet-creation
Fix BTC wallet creation
This commit is contained in:
commit
2a2c1fccc8
7 changed files with 91 additions and 34 deletions
|
|
@ -23,8 +23,6 @@ module.exports = {
|
||||||
|
|
||||||
const BINARIES = {
|
const BINARIES = {
|
||||||
BTC: {
|
BTC: {
|
||||||
defaultUrl: 'https://bitcoincore.org/bin/bitcoin-core-0.20.1/bitcoin-0.20.1-x86_64-linux-gnu.tar.gz',
|
|
||||||
defaultDir: 'bitcoin-0.20.1/bin',
|
|
||||||
url: 'https://bitcoincore.org/bin/bitcoin-core-22.0/bitcoin-22.0-x86_64-linux-gnu.tar.gz',
|
url: 'https://bitcoincore.org/bin/bitcoin-core-22.0/bitcoin-22.0-x86_64-linux-gnu.tar.gz',
|
||||||
dir: 'bitcoin-22.0/bin'
|
dir: 'bitcoin-22.0/bin'
|
||||||
},
|
},
|
||||||
|
|
@ -56,7 +54,7 @@ const BINARIES = {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const coinsUpdateDependent = ['BTC']
|
const coinsUpdateDependent = []
|
||||||
|
|
||||||
function firewall (ports) {
|
function firewall (ports) {
|
||||||
if (!ports || ports.length === 0) throw new Error('No ports supplied')
|
if (!ports || ports.length === 0) throw new Error('No ports supplied')
|
||||||
|
|
|
||||||
|
|
@ -41,11 +41,11 @@ function fetch (account = {}, method, params) {
|
||||||
return r.data.result
|
return r.data.result
|
||||||
})
|
})
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
throw new Error(_.join(' ', [
|
throw new Error(JSON.stringify({
|
||||||
'json-rpc::axios error:',
|
responseMessage: _.get('message', err),
|
||||||
JSON.stringify(_.get('message', err, '')),
|
message: _.get('response.data.error.message', err),
|
||||||
JSON.stringify(_.get('response.data.error', err, ''))
|
code: _.get('response.data.error.code', err)
|
||||||
]))
|
}))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,16 @@ function fetch (method, params) {
|
||||||
return jsonRpc.fetch(rpcConfig, method, params)
|
return jsonRpc.fetch(rpcConfig, method, params)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function errorHandle (e) {
|
||||||
|
const err = JSON.parse(e.message)
|
||||||
|
switch (err.code) {
|
||||||
|
case -6:
|
||||||
|
throw new E.InsufficientFundsError()
|
||||||
|
default:
|
||||||
|
throw e
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function checkCryptoCode (cryptoCode) {
|
function checkCryptoCode (cryptoCode) {
|
||||||
if (cryptoCode !== 'BCH') return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
|
if (cryptoCode !== 'BCH') return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
|
||||||
return Promise.resolve()
|
return Promise.resolve()
|
||||||
|
|
@ -50,10 +60,7 @@ function sendCoins (account, tx, settings, operatorId) {
|
||||||
txid: pickedObj.txid
|
txid: pickedObj.txid
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(err => {
|
.catch(errorHandle)
|
||||||
if (err.code === -6) throw new E.InsufficientFundsError()
|
|
||||||
throw err
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function newAddress (account, info, tx, settings, operatorId) {
|
function newAddress (account, info, tx, settings, operatorId) {
|
||||||
|
|
|
||||||
|
|
@ -17,21 +17,55 @@ function fetch (method, params) {
|
||||||
return jsonRpc.fetch(rpcConfig, method, params)
|
return jsonRpc.fetch(rpcConfig, method, params)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function errorHandle (e) {
|
||||||
|
const err = JSON.parse(e.message)
|
||||||
|
switch (err.code) {
|
||||||
|
case -4:
|
||||||
|
return loadWallet()
|
||||||
|
case -5:
|
||||||
|
return logger.error(`${err}`)
|
||||||
|
case -6:
|
||||||
|
throw new E.InsufficientFundsError()
|
||||||
|
case -18:
|
||||||
|
return createWallet()
|
||||||
|
case -35:
|
||||||
|
// Wallet is already loaded, just return
|
||||||
|
return
|
||||||
|
default:
|
||||||
|
throw e
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function checkCryptoCode (cryptoCode) {
|
function checkCryptoCode (cryptoCode) {
|
||||||
if (cryptoCode !== 'BTC') return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
|
if (cryptoCode !== 'BTC') return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
|
||||||
return Promise.resolve()
|
return Promise.resolve().then(loadWallet)
|
||||||
|
}
|
||||||
|
|
||||||
|
function createWallet () {
|
||||||
|
return fetch('createwallet', ['wallet'])
|
||||||
|
.then(loadWallet)
|
||||||
|
}
|
||||||
|
|
||||||
|
function loadWallet () {
|
||||||
|
return fetch('loadwallet', ['wallet', true])
|
||||||
|
// Catching the error here to suppress error code -35
|
||||||
|
// This improves UX on the initial wallet load and serves as error sink
|
||||||
|
// for wallet creation/loading related issues before actual business logic runs
|
||||||
|
.catch(errorHandle)
|
||||||
}
|
}
|
||||||
|
|
||||||
function accountBalance (cryptoCode) {
|
function accountBalance (cryptoCode) {
|
||||||
return checkCryptoCode(cryptoCode)
|
return checkCryptoCode(cryptoCode)
|
||||||
.then(() => fetch('getwalletinfo'))
|
.then(() => fetch('getwalletinfo'))
|
||||||
.then(({ balance }) => new BN(balance).shiftedBy(unitScale).decimalPlaces(0))
|
.then(({ balance }) => new BN(balance).shiftedBy(unitScale).decimalPlaces(0))
|
||||||
|
.catch(errorHandle)
|
||||||
}
|
}
|
||||||
|
|
||||||
function accountUnconfirmedBalance (cryptoCode) {
|
function accountUnconfirmedBalance (cryptoCode) {
|
||||||
return checkCryptoCode(cryptoCode)
|
return checkCryptoCode(cryptoCode)
|
||||||
.then(() => fetch('getwalletinfo'))
|
.then(() => fetch('getwalletinfo'))
|
||||||
.then(({ unconfirmed_balance: balance }) => new BN(balance).shiftedBy(unitScale).decimalPlaces(0))
|
.then(({ unconfirmed_balance: balance }) => new BN(balance).shiftedBy(unitScale).decimalPlaces(0))
|
||||||
|
.catch(errorHandle)
|
||||||
}
|
}
|
||||||
|
|
||||||
// We want a balance that includes all spends (0 conf) but only deposits that
|
// We want a balance that includes all spends (0 conf) but only deposits that
|
||||||
|
|
@ -75,10 +109,7 @@ function sendCoins (account, tx, settings, operatorId, feeMultiplier) {
|
||||||
txid: pickedObj.txid
|
txid: pickedObj.txid
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(err => {
|
.catch(errorHandle)
|
||||||
if (err.code === -6) throw new E.InsufficientFundsError()
|
|
||||||
throw err
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function sendCoinsBatch (account, txs, cryptoCode, feeMultiplier) {
|
function sendCoinsBatch (account, txs, cryptoCode, feeMultiplier) {
|
||||||
|
|
@ -98,20 +129,19 @@ function sendCoinsBatch (account, txs, cryptoCode, feeMultiplier) {
|
||||||
fee: new BN(pickedObj.fee).abs().shiftedBy(unitScale).decimalPlaces(0),
|
fee: new BN(pickedObj.fee).abs().shiftedBy(unitScale).decimalPlaces(0),
|
||||||
txid: pickedObj.txid
|
txid: pickedObj.txid
|
||||||
}))
|
}))
|
||||||
.catch(err => {
|
.catch(errorHandle)
|
||||||
if (err.code === -6) throw new E.InsufficientFundsError()
|
|
||||||
throw err
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function newAddress (account, info, tx, settings, operatorId) {
|
function newAddress (account, info, tx, settings, operatorId) {
|
||||||
return checkCryptoCode(info.cryptoCode)
|
return checkCryptoCode(info.cryptoCode)
|
||||||
.then(() => fetch('getnewaddress'))
|
.then(() => fetch('getnewaddress'))
|
||||||
|
.catch(errorHandle)
|
||||||
}
|
}
|
||||||
|
|
||||||
function addressBalance (address, confs) {
|
function addressBalance (address, confs) {
|
||||||
return fetch('getreceivedbyaddress', [address, confs])
|
return fetch('getreceivedbyaddress', [address, confs])
|
||||||
.then(r => new BN(r).shiftedBy(unitScale).decimalPlaces(0))
|
.then(r => new BN(r).shiftedBy(unitScale).decimalPlaces(0))
|
||||||
|
.catch(errorHandle)
|
||||||
}
|
}
|
||||||
|
|
||||||
function confirmedBalance (address, cryptoCode) {
|
function confirmedBalance (address, cryptoCode) {
|
||||||
|
|
@ -156,6 +186,7 @@ function newFunding (account, cryptoCode, settings, operatorId) {
|
||||||
fundingConfirmedBalance,
|
fundingConfirmedBalance,
|
||||||
fundingAddress
|
fundingAddress
|
||||||
}))
|
}))
|
||||||
|
.catch(errorHandle)
|
||||||
}
|
}
|
||||||
|
|
||||||
function cryptoNetwork (account, cryptoCode, settings, operatorId) {
|
function cryptoNetwork (account, cryptoCode, settings, operatorId) {
|
||||||
|
|
@ -169,7 +200,7 @@ function fetchRBF (txId) {
|
||||||
return [txId, res['bip125-replaceable']]
|
return [txId, res['bip125-replaceable']]
|
||||||
})
|
})
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
if (err.code === -5) logger.error(`${err.message}`)
|
errorHandle(err)
|
||||||
return [txId, true]
|
return [txId, true]
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,16 @@ function fetch (method, params) {
|
||||||
return jsonRpc.fetch(rpcConfig, method, params)
|
return jsonRpc.fetch(rpcConfig, method, params)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function errorHandle (e) {
|
||||||
|
const err = JSON.parse(e.message)
|
||||||
|
switch (err.code) {
|
||||||
|
case -6:
|
||||||
|
throw new E.InsufficientFundsError()
|
||||||
|
default:
|
||||||
|
throw e
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function checkCryptoCode (cryptoCode) {
|
function checkCryptoCode (cryptoCode) {
|
||||||
if (cryptoCode !== 'DASH') return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
|
if (cryptoCode !== 'DASH') return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
|
||||||
return Promise.resolve()
|
return Promise.resolve()
|
||||||
|
|
@ -52,10 +62,7 @@ function sendCoins (account, tx, settings, operatorId) {
|
||||||
txid: pickedObj.txid
|
txid: pickedObj.txid
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(err => {
|
.catch(errorHandle)
|
||||||
if (err.code === -6) throw new E.InsufficientFundsError()
|
|
||||||
throw err
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function newAddress (account, info, tx, settings, operatorId) {
|
function newAddress (account, info, tx, settings, operatorId) {
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,16 @@ function fetch (method, params) {
|
||||||
return jsonRpc.fetch(rpcConfig, method, params)
|
return jsonRpc.fetch(rpcConfig, method, params)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function errorHandle (e) {
|
||||||
|
const err = JSON.parse(e.message)
|
||||||
|
switch (err.code) {
|
||||||
|
case -6:
|
||||||
|
throw new E.InsufficientFundsError()
|
||||||
|
default:
|
||||||
|
throw e
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function checkCryptoCode (cryptoCode) {
|
function checkCryptoCode (cryptoCode) {
|
||||||
if (cryptoCode !== 'LTC') return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
|
if (cryptoCode !== 'LTC') return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
|
||||||
return Promise.resolve()
|
return Promise.resolve()
|
||||||
|
|
@ -52,10 +62,7 @@ function sendCoins (account, tx, settings, operatorId) {
|
||||||
txid: pickedObj.txid
|
txid: pickedObj.txid
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(err => {
|
.catch(errorHandle)
|
||||||
if (err.code === -6) throw new E.InsufficientFundsError()
|
|
||||||
throw err
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function newAddress (account, info, tx, settings, operatorId) {
|
function newAddress (account, info, tx, settings, operatorId) {
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,16 @@ function fetch (method, params) {
|
||||||
return jsonRpc.fetch(rpcConfig, method, params)
|
return jsonRpc.fetch(rpcConfig, method, params)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function errorHandle (e) {
|
||||||
|
const err = JSON.parse(e.message)
|
||||||
|
switch (err.code) {
|
||||||
|
case -6:
|
||||||
|
throw new E.InsufficientFundsError()
|
||||||
|
default:
|
||||||
|
throw e
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function checkCryptoCode (cryptoCode) {
|
function checkCryptoCode (cryptoCode) {
|
||||||
if (cryptoCode !== 'ZEC') return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
|
if (cryptoCode !== 'ZEC') return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
|
||||||
return Promise.resolve()
|
return Promise.resolve()
|
||||||
|
|
@ -78,10 +88,7 @@ function sendCoins (account, tx, settings, operatorId) {
|
||||||
txid: pickedObj.txid
|
txid: pickedObj.txid
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.catch(err => {
|
.catch(errorHandle)
|
||||||
if (err.code === -6) throw new E.InsufficientFundsError()
|
|
||||||
throw err
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function newAddress (account, info, tx, settings, operatorId) {
|
function newAddress (account, info, tx, settings, operatorId) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue