Skip to content

Commit

Permalink
Merge pull request #133 from liquality/wallet-error
Browse files Browse the repository at this point in the history
Wallet errors and fixes
  • Loading branch information
monokh authored Jan 28, 2019
2 parents 2360bb8 + 86cb23e commit 67458e8
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
31 changes: 26 additions & 5 deletions src/providers/LedgerProvider.js
Original file line number Diff line number Diff line change
@@ -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 () {
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/providers/bitcoin/BitcoinLedgerProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
19 changes: 11 additions & 8 deletions src/providers/ethereum/EthereumMetaMaskProvider.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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
}

Expand All @@ -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 } })
}

Expand Down

0 comments on commit 67458e8

Please sign in to comment.