From 37706d05ffefdb768adc4c40d3b2aaa5f9de425d Mon Sep 17 00:00:00 2001 From: "Shane (Sosho) Chang" Date: Tue, 13 Nov 2018 23:16:26 -0800 Subject: [PATCH] fixHighLvlTryCatch (#206) * fixHighLvlTryCatch * upversion to 1.4.6 * remove version number from package.json bc we are setting version in manifest.json --- package.json | 1 - .../controllers/accountController.ts | 12 ++++--- src/models/Wallet.ts | 31 ++++++++++++++++--- static/manifest.json | 2 +- 4 files changed, 35 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index e691e0bf..d776fffa 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,5 @@ { "name": "qrypto", - "version": "1.4.2-beta", "private": true, "scripts": { "clean": "rm -rf dist", diff --git a/src/background/controllers/accountController.ts b/src/background/controllers/accountController.ts index 5509bb49..c4ff38a9 100644 --- a/src/background/controllers/accountController.ts +++ b/src/background/controllers/accountController.ts @@ -436,23 +436,27 @@ export default class AccountController extends IController { }); } - private handleMessage = (request: any, _: chrome.runtime.MessageSender, sendResponse: (response: any) => void) => { + private handleMessage = async ( + request: any, + _: chrome.runtime.MessageSender, + sendResponse: (response: any) => void, + ) => { try { switch (request.type) { case MESSAGE_TYPE.LOGIN: this.login(request.password); break; case MESSAGE_TYPE.IMPORT_MNEMONIC: - this.importMnemonic(request.accountName, request.mnemonicPrivateKey); + await this.importMnemonic(request.accountName, request.mnemonicPrivateKey); break; case MESSAGE_TYPE.IMPORT_PRIVATE_KEY: - this.importPrivateKey(request.accountName, request.mnemonicPrivateKey); + await this.importPrivateKey(request.accountName, request.mnemonicPrivateKey); break; case MESSAGE_TYPE.SAVE_TO_FILE: this.saveToFile(request.accountName, request.mnemonicPrivateKey); break; case MESSAGE_TYPE.ACCOUNT_LOGIN: - this.loginAccount(request.selectedWalletName); + await this.loginAccount(request.selectedWalletName); break; case MESSAGE_TYPE.SEND_TOKENS: this.sendTokens(request.receiverAddress, request.amount, request.transactionSpeed); diff --git a/src/models/Wallet.ts b/src/models/Wallet.ts index c1154e62..7a8f5b28 100644 --- a/src/models/Wallet.ts +++ b/src/models/Wallet.ts @@ -23,12 +23,33 @@ export default class Wallet implements ISigner { if (!this.qjsWallet) { console.error('Cannot updateInfo without qjsWallet instance.'); } - const newInfo = await this.qjsWallet!.getInfo(); - // if they are not equal, then the balance has changed - if (!deepEqual(this.info, newInfo)) { - this.info = newInfo; - return true; + /** + * We add a timeout promise to handle if qjsWallet hangs when executing getInfo. + * (This happens if the insight api is down) + */ + let timedOut = false; + const timeoutPromise = new Promise((_, reject) => { + const wait = setTimeout(() => { + clearTimeout(wait); + timedOut = true; + reject(Error('wallet.getInfo failed, insight api may be down')); + }, 30000); + }); + + const getInfoPromise = this.qjsWallet!.getInfo(); + const promises = [timeoutPromise, getInfoPromise]; + let newInfo: any; + try { + newInfo = await Promise.race(promises); + + // if they are not equal, then the balance has changed + if (!timedOut && !deepEqual(this.info, newInfo)) { + this.info = newInfo; + return true; + } + } catch (e) { + throw(Error(e)); } return false; diff --git a/static/manifest.json b/static/manifest.json index fa4479d6..82abfc0c 100644 --- a/static/manifest.json +++ b/static/manifest.json @@ -1,7 +1,7 @@ { "name": "Qrypto", "description": "Qtum light wallet and transaction signing client.", - "version": "1.4.5", + "version": "1.4.6", "manifest_version": 2, "icons": { "16": "images/logo-main-16.png",