diff --git a/accounts/xpub-rest-api.js b/accounts/xpub-rest-api.js index 9d63927..b612a5b 100644 --- a/accounts/xpub-rest-api.js +++ b/accounts/xpub-rest-api.js @@ -48,6 +48,14 @@ class XPubRestApi { HttpServer.sendAuthError ) + this.httpServer.app.get( + '/xpub/:xpub/import/status', + authMgr.checkAuthentication.bind(authMgr), + this.validateArgsGetXpub.bind(this), + this.getXpubImportStatus.bind(this), + HttpServer.sendAuthError + ) + this.httpServer.app.get( '/xpub/:xpub', authMgr.checkAuthentication.bind(authMgr), @@ -202,6 +210,41 @@ class XPubRestApi { } } + /** + * Handle xPub/import/status GET request + * @param {object} req - http request object + * @param {object} res - http response object + */ + async getXpubImportStatus(req, res) { + try { + let xpub + + // Extracts arguments + const argXpub = req.params.xpub + + // Translate xpub if needed + try { + const xlatXpub = this.xlatHdAccount(argXpub) + xpub = xlatXpub.xpub + } catch(e) { + return HttpServer.sendError(res, e) + } + + const ret = { + import_in_progress: hdaService.importInProgress(xpub) + } + + HttpServer.sendOkData(res, ret) + + } catch(e) { + Logger.error(e, 'API : XpubRestApi.getXpubImportStatus()') + HttpServer.sendError(res, e) + + } finally { + debugApi && Logger.info(`API : Completed GET /xpub/${req.params.xpub}/import/status`) + } + } + /** * Handle Lock XPub POST request * @param {object} req - http request object diff --git a/lib/bitcoin/hd-accounts-service.js b/lib/bitcoin/hd-accounts-service.js index 0d2b07f..63c3307 100644 --- a/lib/bitcoin/hd-accounts-service.js +++ b/lib/bitcoin/hd-accounts-service.js @@ -171,6 +171,16 @@ class HDAccountsService { } } + /** + * Check if a xpub is currently being imported or rescanned by Dojo + * Returns true if import/rescan is in progress, otherwise returns false + * @param {string} xpub - xpub + * @returns {Promise} + */ + importInProgress(xpub) { + return remote.importInProgress(xpub) + } + /** * Check if we try to override an existing xpub * Delete the old xpub from db if it's the case diff --git a/lib/remote-importer/remote-importer.js b/lib/remote-importer/remote-importer.js index 228737d..3c4be3b 100644 --- a/lib/remote-importer/remote-importer.js +++ b/lib/remote-importer/remote-importer.js @@ -48,6 +48,16 @@ class RemoteImporter { delete this.importing[xpub] } + /** + * Check if a xpub is currently being imported or rescanned by Dojo + * Returns true if import/rescan is in progress, otherwise returns false + * @param {string} xpub - xpub + * @returns {boolean} + */ + importInProgress(xpub) { + return this.importing[xpub] ? true : false + } + /** * Process the relations between a list of transactions * @param {object[]} txs - array of transaction objects