diff --git a/lib/blockchain/install.js b/lib/blockchain/install.js index c2932383..16e2dd4e 100644 --- a/lib/blockchain/install.js +++ b/lib/blockchain/install.js @@ -9,6 +9,8 @@ const _ = require('lodash/fp') const { utils: coinUtils } = require('lamassu-coins') const options = require('../options') +const settingsLoader = require('../new-settings-loader') +const wallet = require('../wallet') const common = require('./common') const doVolume = require('./do-volume') @@ -112,9 +114,19 @@ function plugin (crypto) { return plugin } -function getSyncedBlockchains (cryptoList) { +function getBlockchainSyncStatus (cryptoList) { const installedCryptos = _.reduce((acc, value) => ({ ...acc, [value.cryptoCode]: isInstalledSoftware(value) && isInstalledVolume(value) }), {}, cryptoList) - const syncedBlockchains = plugin() + + return settingsLoader.loadLatest() + .then(settings => { + const blockchainStatuses = _.reduce((acc, value) => + _.includes(value.cryptoCode, _.keys(installedCryptos) ? ({ ...acc, [value.cryptoCode]: wallet.checkBlockchainStatus(settings, value.cryptoCode) }) : ({ ...acc })), + {}, + cryptoList + ) + + return blockchainStatuses + }) } function run () { @@ -137,14 +149,17 @@ function run () { questions.push({ type: 'checkbox', name: 'crypto', - message: 'Which cryptocurrencies would you like to install?\nTo prevent server resource overloading, only TWO coins should be installed simultaneously.\nMore coins can be installed after this process is over.', + message: 'Which cryptocurrencies would you like to install?\nTo prevent server resource overloading, only TWO coins should be syncing simultaneously.\nMore coins can be installed after this process is over.', choices, validate: (a) => { - getSyncedBlockchains(cryptos) - return _.size(a) > 0 && _.size(a) <= 2 + return getBlockchainSyncStatus(cryptos) + .then(chains => { + const result = _.reduce((acc, value) => ({ ...acc, [value]: _.isNil() ? 1 : acc[value] + 1 }), {}, _.values(chains)) + return result.syncing > 0 || _.size(a) > 0 && _.size(a) <= 2 + }) } }) inquirer.prompt(questions) - .then(answers => console.log('answers', answers) /* processCryptos(answers.crypto) */) + .then(answers => processCryptos(answers.crypto)) } diff --git a/lib/plugins/wallet/bitcoincashd/bitcoincashd.js b/lib/plugins/wallet/bitcoincashd/bitcoincashd.js index 00a37f73..c969cc71 100644 --- a/lib/plugins/wallet/bitcoincashd/bitcoincashd.js +++ b/lib/plugins/wallet/bitcoincashd/bitcoincashd.js @@ -123,6 +123,12 @@ function supportsBatching (cryptoCode) { .then(() => SUPPORTS_BATCHING) } +function checkBlockchainStatus (cryptoCode) { + return checkCryptoCode(cryptoCode) + .then(() => fetch('getblockchaininfo')) + .then(res => !!res['initialblockdownload'] ? 'ready' : 'syncing') +} + module.exports = { balance, sendCoins, @@ -130,5 +136,6 @@ module.exports = { getStatus, newFunding, cryptoNetwork, - supportsBatching + supportsBatching, + checkBlockchainStatus } diff --git a/lib/plugins/wallet/bitcoind/bitcoind.js b/lib/plugins/wallet/bitcoind/bitcoind.js index 3b9a4e0a..f08acb46 100644 --- a/lib/plugins/wallet/bitcoind/bitcoind.js +++ b/lib/plugins/wallet/bitcoind/bitcoind.js @@ -192,5 +192,6 @@ module.exports = { fetchRBF, estimateFee, sendCoinsBatch, - supportsBatching + supportsBatching, + checkBlockchainStatus } diff --git a/lib/plugins/wallet/bitgo/bitgo.js b/lib/plugins/wallet/bitgo/bitgo.js index 34621840..c7c22abd 100644 --- a/lib/plugins/wallet/bitgo/bitgo.js +++ b/lib/plugins/wallet/bitgo/bitgo.js @@ -164,6 +164,11 @@ function supportsBatching (cryptoCode) { .then(() => SUPPORTS_BATCHING) } +function checkBlockchainStatus (cryptoCode) { + return checkCryptoCode(cryptoCode) + .then(() => Promise.resolve('ready')) +} + module.exports = { NAME, balance, @@ -172,5 +177,6 @@ module.exports = { getStatus, newFunding, cryptoNetwork, - supportsBatching + supportsBatching, + checkBlockchainStatus } diff --git a/lib/plugins/wallet/dashd/dashd.js b/lib/plugins/wallet/dashd/dashd.js index bb5d848a..c9e6a656 100644 --- a/lib/plugins/wallet/dashd/dashd.js +++ b/lib/plugins/wallet/dashd/dashd.js @@ -118,11 +118,18 @@ function supportsBatching (cryptoCode) { .then(() => SUPPORTS_BATCHING) } +function checkBlockchainStatus (cryptoCode) { + return checkCryptoCode(cryptoCode) + .then(() => fetch('getblockchaininfo')) + .then(res => !!res['initialblockdownload'] ? 'ready' : 'syncing') +} + module.exports = { balance, sendCoins, newAddress, getStatus, newFunding, - supportsBatching + supportsBatching, + checkBlockchainStatus } diff --git a/lib/plugins/wallet/geth/base.js b/lib/plugins/wallet/geth/base.js index 26b3a50d..64b890c1 100644 --- a/lib/plugins/wallet/geth/base.js +++ b/lib/plugins/wallet/geth/base.js @@ -32,7 +32,8 @@ module.exports = { privateKey, isStrictAddress, connect, - supportsBatching + supportsBatching, + checkBlockchainStatus } function connect (url) { @@ -230,3 +231,9 @@ function supportsBatching (cryptoCode) { return checkCryptoCode(cryptoCode) .then(() => SUPPORTS_BATCHING) } + +function checkBlockchainStatus (cryptoCode) { + return checkCryptoCode(cryptoCode) + .then(pify(web3.eth.isSyncing)) + .then(res => _.isObject(res) ? 'syncing' : 'ready') +} diff --git a/lib/plugins/wallet/litecoind/litecoind.js b/lib/plugins/wallet/litecoind/litecoind.js index 7b238f90..452a2553 100644 --- a/lib/plugins/wallet/litecoind/litecoind.js +++ b/lib/plugins/wallet/litecoind/litecoind.js @@ -118,11 +118,18 @@ function supportsBatching (cryptoCode) { .then(() => SUPPORTS_BATCHING) } +function checkBlockchainStatus (cryptoCode) { + return checkCryptoCode(cryptoCode) + .then(() => fetch('getblockchaininfo')) + .then(res => !!res['initialblockdownload'] ? 'ready' : 'syncing') +} + module.exports = { balance, sendCoins, newAddress, getStatus, newFunding, - supportsBatching + supportsBatching, + checkBlockchainStatus } diff --git a/lib/plugins/wallet/mock-wallet/mock-wallet.js b/lib/plugins/wallet/mock-wallet/mock-wallet.js index 5850fefc..9b17e4ad 100644 --- a/lib/plugins/wallet/mock-wallet/mock-wallet.js +++ b/lib/plugins/wallet/mock-wallet/mock-wallet.js @@ -115,6 +115,11 @@ function supportsBatching (cryptoCode) { return Promise.resolve(_.includes(cryptoCode, BATCHABLE_COINS)) } +function checkBlockchainStatus (cryptoCode) { + return checkCryptoCode(cryptoCode) + .then(() => Promise.resolve('ready')) +} + module.exports = { NAME, balance, @@ -123,5 +128,6 @@ module.exports = { newAddress, getStatus, newFunding, - supportsBatching + supportsBatching, + checkBlockchainStatus } diff --git a/lib/plugins/wallet/monerod/monerod.js b/lib/plugins/wallet/monerod/monerod.js index f28eca0f..1c9f8561 100644 --- a/lib/plugins/wallet/monerod/monerod.js +++ b/lib/plugins/wallet/monerod/monerod.js @@ -205,6 +205,27 @@ function supportsBatching (cryptoCode) { .then(() => SUPPORTS_BATCHING) } +function checkBlockchainStatus (cryptoCode) { + return checkCryptoCode(cryptoCode) + .then(() => { + try { + const config = jsonRpc.parseConf(configPath) + + // Daemon uses a different connection of the wallet + const rpcConfig = { + username: config['rpc-login'].split(':')[0], + password: config['rpc-login'].split(':')[1], + port: cryptoRec.defaultPort + } + + return jsonRpc.fetchDigest(rpcConfig, 'get_info') + .then(res => !!res.synchronized ? 'ready' : 'syncing') + } catch (err) { + throw new Error('XMR daemon is currently not installed') + } + }) +} + module.exports = { balance, sendCoins, @@ -212,5 +233,6 @@ module.exports = { getStatus, newFunding, cryptoNetwork, - supportsBatching + supportsBatching, + checkBlockchainStatus } diff --git a/lib/plugins/wallet/zcashd/zcashd.js b/lib/plugins/wallet/zcashd/zcashd.js index 5979cbe8..bc8c30c4 100644 --- a/lib/plugins/wallet/zcashd/zcashd.js +++ b/lib/plugins/wallet/zcashd/zcashd.js @@ -144,11 +144,18 @@ function supportsBatching (cryptoCode) { .then(() => SUPPORTS_BATCHING) } +function checkBlockchainStatus (cryptoCode) { + return checkCryptoCode(cryptoCode) + .then(() => fetch('getblockchaininfo')) + .then(res => !!res['initial_block_download_complete'] ? 'ready' : 'syncing') +} + module.exports = { balance, sendCoins, newAddress, getStatus, newFunding, - supportsBatching + supportsBatching, + checkBlockchainStatus } diff --git a/lib/wallet.js b/lib/wallet.js index 13e972fa..2b39f260 100644 --- a/lib/wallet.js +++ b/lib/wallet.js @@ -237,6 +237,11 @@ function supportsBatching (settings, cryptoCode) { .then(r => r.wallet.supportsBatching(cryptoCode)) } +function checkBlockchainStatus (settings, cryptoCode) { + return fetchWallet(settings, cryptoCode) + .then(r => r.wallet.checkBlockchainStatus(cryptoCode)) +} + const coinFilter = ['ETH'] const balance = (settings, cryptoCode) => { @@ -265,5 +270,6 @@ module.exports = { isHd, newFunding, cryptoNetwork, - supportsBatching + supportsBatching, + checkBlockchainStatus }