From 1acba22ba1fb2fbdfaa885ffbfbb9b5adb331c28 Mon Sep 17 00:00:00 2001 From: monokh Date: Tue, 22 Jan 2019 11:42:49 +0000 Subject: [PATCH 1/4] Rethrow generic wallet errors --- src/errors.js | 1 + src/providers/LedgerProvider.js | 31 ++++++++++++++++--- .../ethereum/EthereumMetaMaskProvider.js | 12 +++---- 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/src/errors.js b/src/errors.js index 5f63b6e59..ad3e488b4 100644 --- a/src/errors.js +++ b/src/errors.js @@ -14,3 +14,4 @@ export const NoProviderError = createError('NoProviderError') export const UnsupportedMethodError = createError('UnsupportedMethodError') export const UnimplementedMethodError = createError('UnimplementedMethodError') export const InvalidProviderResponseError = createError('InvalidProviderResponseError') +export const WalletError = createError('WalletError') diff --git a/src/providers/LedgerProvider.js b/src/providers/LedgerProvider.js index 25461d63f..84f1f319e 100644 --- a/src/providers/LedgerProvider.js +++ b/src/providers/LedgerProvider.js @@ -1,7 +1,6 @@ -import Provider from '../Provider' - import Transport from '@ledgerhq/hw-transport-node-hid' -// import bitcoinjs from 'bitcoinjs-lib' +import Provider from '../Provider' +import { WalletError } from '../errors' export default class LedgerProvider extends Provider { static isSupported () { @@ -26,11 +25,33 @@ export default class LedgerProvider extends Provider { } } + errorProxy (target, func) { + if (Object.getOwnPropertyNames(target).includes(func)) { + const method = target[func] + return async (...args) => { + try { + const result = await method.bind(target)(...args) + return result + } catch (e) { + const { name, ...errorNoName } = e + throw new WalletError(e.toString(), errorNoName) + } + } + } else { + return target[func] + } + } + async getApp () { - await this.createTransport() + try { + await this.createTransport() + } catch (e) { + const { name, ...errorNoName } = e + throw new WalletError(e.toString(), errorNoName) + } if (!this._appInstance) { - this._appInstance = new this._App(LedgerProvider.transport) + this._appInstance = new Proxy(new this._App(LedgerProvider.transport), { get: this.errorProxy }) } return this._appInstance diff --git a/src/providers/ethereum/EthereumMetaMaskProvider.js b/src/providers/ethereum/EthereumMetaMaskProvider.js index 12124d8b1..ce7ec4113 100644 --- a/src/providers/ethereum/EthereumMetaMaskProvider.js +++ b/src/providers/ethereum/EthereumMetaMaskProvider.js @@ -1,8 +1,8 @@ -import Provider from '../../Provider' - import { isFunction } from 'lodash' -import { formatEthResponse, ensureHexEthFormat, ensureHexStandardFormat } from './EthereumUtil' import { BigNumber } from 'bignumber.js' +import Provider from '../../Provider' +import { formatEthResponse, ensureHexEthFormat, ensureHexStandardFormat } from './EthereumUtil' +import { WalletError } from '../../errors' export default class EthereumMetaMaskProvider extends Provider { constructor (metamaskProvider, network) { @@ -21,17 +21,17 @@ export default class EthereumMetaMaskProvider extends Provider { ._metamaskProvider .sendAsync({ method, params }, (err, data) => { if (err) { - reject(err) + reject(new WalletError(err.toString(), err)) return } if (!data) { - reject(new Error('Something went wrong')) + reject(new WalletError('Metamask response was empty')) return } if (typeof data.result === 'undefined') { - reject(new Error('Something went wrong')) + reject(new WalletError('Metamask response was empty')) return } From b38345d9a4c62baaadc41e8ff6368371e7d10f5f Mon Sep 17 00:00:00 2001 From: monokh Date: Wed, 23 Jan 2019 11:17:04 +0000 Subject: [PATCH 2/4] Metamask provider: throw error when no addresses available --- src/providers/ethereum/EthereumMetaMaskProvider.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/providers/ethereum/EthereumMetaMaskProvider.js b/src/providers/ethereum/EthereumMetaMaskProvider.js index ce7ec4113..187cade3f 100644 --- a/src/providers/ethereum/EthereumMetaMaskProvider.js +++ b/src/providers/ethereum/EthereumMetaMaskProvider.js @@ -44,7 +44,9 @@ export default class EthereumMetaMaskProvider extends Provider { async getAddresses () { const addresses = await this._toMM('eth_accounts') - + if (addresses.length === 0) { + throw new WalletError('Metamask: No addresses available from wallet') + } return addresses.map((address) => { return { address: address } }) } From 4b066c5fc350bd1c996023c3a26bf3c56feee975 Mon Sep 17 00:00:00 2001 From: monokh Date: Wed, 23 Jan 2019 11:45:25 +0000 Subject: [PATCH 3/4] BitcoinLedgerProvider: xpubkey - cache the key not the promise --- src/providers/bitcoin/BitcoinLedgerProvider.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/providers/bitcoin/BitcoinLedgerProvider.js b/src/providers/bitcoin/BitcoinLedgerProvider.js index eee0a99e8..bba224ac9 100644 --- a/src/providers/bitcoin/BitcoinLedgerProvider.js +++ b/src/providers/bitcoin/BitcoinLedgerProvider.js @@ -65,7 +65,7 @@ export default class BitcoinLedgerProvider extends LedgerProvider { return this._extendedPubKeyCache[path] } - const extendedPubKey = this._getAddressExtendedPubKey(path) + const extendedPubKey = await this._getAddressExtendedPubKey(path) this._extendedPubKeyCache[path] = extendedPubKey return extendedPubKey } From 02e96f1944def5ad56b35557f5a3cc6888d2373d Mon Sep 17 00:00:00 2001 From: monokh Date: Wed, 23 Jan 2019 12:27:58 +0000 Subject: [PATCH 4/4] EthereumMetaMaskProvider: Fix error not being retrieved --- src/providers/ethereum/EthereumMetaMaskProvider.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/providers/ethereum/EthereumMetaMaskProvider.js b/src/providers/ethereum/EthereumMetaMaskProvider.js index 187cade3f..6572f7f51 100644 --- a/src/providers/ethereum/EthereumMetaMaskProvider.js +++ b/src/providers/ethereum/EthereumMetaMaskProvider.js @@ -20,8 +20,9 @@ export default class EthereumMetaMaskProvider extends Provider { this ._metamaskProvider .sendAsync({ method, params }, (err, data) => { - if (err) { - reject(new WalletError(err.toString(), err)) + const error = err || data.error + if (error) { + reject(new WalletError(error.toString(), error)) return }