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 40fe92f20..7a5436f0b 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/bitcoin/BitcoinLedgerProvider.js b/src/providers/bitcoin/BitcoinLedgerProvider.js index 1591332f3..ef00132e0 100644 --- a/src/providers/bitcoin/BitcoinLedgerProvider.js +++ b/src/providers/bitcoin/BitcoinLedgerProvider.js @@ -29,7 +29,7 @@ export default class BitcoinLedgerProvider extends LedgerProvider { return this._walletPublicKeyCache[path] } - const walletPublicKey = this._getWalletPublicKey(path) + const walletPublicKey = await this._getWalletPublicKey(path) this._walletPublicKeyCache[path] = walletPublicKey return walletPublicKey } diff --git a/src/providers/ethereum/EthereumMetaMaskProvider.js b/src/providers/ethereum/EthereumMetaMaskProvider.js index 12124d8b1..6572f7f51 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) { @@ -20,18 +20,19 @@ export default class EthereumMetaMaskProvider extends Provider { this ._metamaskProvider .sendAsync({ method, params }, (err, data) => { - if (err) { - reject(err) + const error = err || data.error + if (error) { + reject(new WalletError(error.toString(), error)) 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 } @@ -44,7 +45,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 } }) }