Gaëtan Renaudeau
6 years ago
47 changed files with 67 additions and 332 deletions
@ -1,53 +0,0 @@ |
|||
// @flow
|
|||
|
|||
// TODO we need to start porting all custom errors here.
|
|||
|
|||
import { createCustomErrorClass } from 'helpers/errors' |
|||
|
|||
export const AccountNameRequiredError = createCustomErrorClass('AccountNameRequired') |
|||
export const BtcUnmatchedApp = createCustomErrorClass('BtcUnmatchedApp') |
|||
export const CantOpenDevice = createCustomErrorClass('CantOpenDevice') |
|||
export const DeviceAppVerifyNotSupported = createCustomErrorClass('DeviceAppVerifyNotSupported') |
|||
export const DeviceGenuineSocketEarlyClose = createCustomErrorClass('DeviceGenuineSocketEarlyClose') |
|||
export const DeviceNotGenuineError = createCustomErrorClass('DeviceNotGenuine') |
|||
export const DeviceSocketFail = createCustomErrorClass('DeviceSocketFail') |
|||
export const DeviceSocketNoBulkStatus = createCustomErrorClass('DeviceSocketNoBulkStatus') |
|||
export const DeviceSocketNoHandler = createCustomErrorClass('DeviceSocketNoHandler') |
|||
export const DisconnectedDevice = createCustomErrorClass('DisconnectedDevice') |
|||
export const EnpointConfigError = createCustomErrorClass('EnpointConfig') |
|||
export const FeeEstimationFailed = createCustomErrorClass('FeeEstimationFailed') |
|||
export const HardResetFail = createCustomErrorClass('HardResetFail') |
|||
export const InvalidAddress = createCustomErrorClass('InvalidAddress') |
|||
export const LatestMCUInstalledError = createCustomErrorClass('LatestMCUInstalledError') |
|||
export const LedgerAPIError = createCustomErrorClass('LedgerAPIError') |
|||
export const LedgerAPIErrorWithMessage = createCustomErrorClass('LedgerAPIErrorWithMessage') |
|||
export const LedgerAPINotAvailable = createCustomErrorClass('LedgerAPINotAvailable') |
|||
export const ManagerAppAlreadyInstalledError = createCustomErrorClass('ManagerAppAlreadyInstalled') |
|||
export const ManagerAppRelyOnBTCError = createCustomErrorClass('ManagerAppRelyOnBTC') |
|||
export const ManagerDeviceLockedError = createCustomErrorClass('ManagerDeviceLocked') |
|||
export const ManagerNotEnoughSpaceError = createCustomErrorClass('ManagerNotEnoughSpace') |
|||
export const ManagerUninstallBTCDep = createCustomErrorClass('ManagerUninstallBTCDep') |
|||
export const NetworkDown = createCustomErrorClass('NetworkDown') |
|||
export const NoAddressesFound = createCustomErrorClass('NoAddressesFound') |
|||
export const NotEnoughBalance = createCustomErrorClass('NotEnoughBalance') |
|||
export const NotEnoughBalanceBecauseDestinationNotCreated = createCustomErrorClass( |
|||
'NotEnoughBalanceBecauseDestinationNotCreated', |
|||
) |
|||
export const PasswordsDontMatchError = createCustomErrorClass('PasswordsDontMatch') |
|||
export const PasswordIncorrectError = createCustomErrorClass('PasswordIncorrect') |
|||
export const TimeoutTagged = createCustomErrorClass('TimeoutTagged') |
|||
export const UpdateYourApp = createCustomErrorClass('UpdateYourApp') |
|||
export const UserRefusedAddress = createCustomErrorClass('UserRefusedAddress') |
|||
export const UserRefusedFirmwareUpdate = createCustomErrorClass('UserRefusedFirmwareUpdate') |
|||
export const UserRefusedOnDevice = createCustomErrorClass('UserRefusedOnDevice') // TODO rename because it's just for transaction refusal
|
|||
export const WebsocketConnectionError = createCustomErrorClass('WebsocketConnectionError') |
|||
export const WebsocketConnectionFailed = createCustomErrorClass('WebsocketConnectionFailed') |
|||
export const WrongDeviceForAccount = createCustomErrorClass('WrongDeviceForAccount') |
|||
export const ETHAddressNonEIP = createCustomErrorClass('ETHAddressNonEIP') |
|||
export const CantScanQRCode = createCustomErrorClass('CantScanQRCode') |
|||
export const FeeNotLoaded = createCustomErrorClass('FeeNotLoaded') |
|||
|
|||
// db stuff, no need to translate
|
|||
export const NoDBPathGiven = createCustomErrorClass('NoDBPathGiven') |
|||
export const DBWrongPassword = createCustomErrorClass('DBWrongPassword') |
|||
export const DBNotReset = createCustomErrorClass('DBNotReset') |
@ -1,96 +0,0 @@ |
|||
// @flow
|
|||
/* eslint-disable no-continue */ |
|||
|
|||
// TODO we need to centralize the error in one place. so all are recorded
|
|||
const errorClasses = {} |
|||
|
|||
export const createCustomErrorClass = (name: string): Class<any> => { |
|||
const C = function CustomError(message?: string, fields?: Object) { |
|||
Object.assign(this, fields) |
|||
this.name = name |
|||
this.message = message || name |
|||
this.stack = new Error().stack |
|||
} |
|||
// $FlowFixMe
|
|||
C.prototype = new Error() |
|||
|
|||
errorClasses[name] = C |
|||
// $FlowFixMe we can't easily type a subset of Error for now...
|
|||
return C |
|||
} |
|||
|
|||
// inspired from https://github.com/programble/errio/blob/master/index.js
|
|||
export const deserializeError = (object: mixed): Error => { |
|||
if (typeof object === 'object' && object) { |
|||
try { |
|||
// $FlowFixMe FIXME HACK
|
|||
const msg = JSON.parse(object.message) |
|||
if (msg.message && msg.name) { |
|||
object = msg |
|||
} |
|||
} catch (e) { |
|||
// nothing
|
|||
} |
|||
const constructor = |
|||
object.name === 'Error' |
|||
? Error |
|||
: typeof object.name === 'string' |
|||
? errorClasses[object.name] || createCustomErrorClass(object.name) |
|||
: Error |
|||
|
|||
const error = Object.create(constructor.prototype) |
|||
for (const prop in object) { |
|||
if (object.hasOwnProperty(prop)) { |
|||
error[prop] = object[prop] |
|||
} |
|||
} |
|||
if (!error.stack && Error.captureStackTrace) { |
|||
Error.captureStackTrace(error, deserializeError) |
|||
} |
|||
return error |
|||
} |
|||
return new Error(String(object)) |
|||
} |
|||
|
|||
// inspired from https://github.com/sindresorhus/serialize-error/blob/master/index.js
|
|||
export const serializeError = (value: mixed) => { |
|||
if (!value) return value |
|||
if (typeof value === 'object') { |
|||
return destroyCircular(value, []) |
|||
} |
|||
if (typeof value === 'function') { |
|||
return `[Function: ${value.name || 'anonymous'}]` |
|||
} |
|||
return value |
|||
} |
|||
|
|||
// https://www.npmjs.com/package/destroy-circular
|
|||
function destroyCircular(from: Object, seen) { |
|||
const to = {} |
|||
seen.push(from) |
|||
for (const key of Object.keys(from)) { |
|||
const value = from[key] |
|||
if (typeof value === 'function') { |
|||
continue |
|||
} |
|||
if (!value || typeof value !== 'object') { |
|||
to[key] = value |
|||
continue |
|||
} |
|||
if (seen.indexOf(from[key]) === -1) { |
|||
to[key] = destroyCircular(from[key], seen.slice(0)) |
|||
continue |
|||
} |
|||
to[key] = '[Circular]' |
|||
} |
|||
if (typeof from.name === 'string') { |
|||
to.name = from.name |
|||
} |
|||
if (typeof from.message === 'string') { |
|||
to.message = from.message |
|||
} |
|||
if (typeof from.stack === 'string') { |
|||
to.stack = from.stack |
|||
} |
|||
return to |
|||
} |
@ -1,44 +0,0 @@ |
|||
// @flow
|
|||
|
|||
import type { CryptoCurrency } from '@ledgerhq/live-common/lib/types' |
|||
import Btc from '@ledgerhq/hw-app-btc' |
|||
import type Transport from '@ledgerhq/hw-transport' |
|||
import { BtcUnmatchedApp, UpdateYourApp } from 'config/errors' |
|||
import getBitcoinLikeInfo from '../devices/getBitcoinLikeInfo' |
|||
|
|||
const oldP2SH = { |
|||
digibyte: 5, |
|||
} |
|||
|
|||
export default async ( |
|||
transport: Transport<*>, |
|||
currency: CryptoCurrency, |
|||
path: string, |
|||
{ |
|||
segwit = true, |
|||
verify = false, |
|||
}: { |
|||
segwit?: boolean, |
|||
verify?: boolean, |
|||
}, |
|||
) => { |
|||
const btc = new Btc(transport) |
|||
const { bitcoinAddress, publicKey } = await btc.getWalletPublicKey(path, verify, segwit) |
|||
|
|||
const { bitcoinLikeInfo } = currency |
|||
if (bitcoinLikeInfo) { |
|||
const { P2SH, P2PKH } = await getBitcoinLikeInfo(transport) |
|||
if (P2SH !== bitcoinLikeInfo.P2SH || P2PKH !== bitcoinLikeInfo.P2PKH) { |
|||
if ( |
|||
currency.id in oldP2SH && |
|||
P2SH === oldP2SH[currency.id] && |
|||
P2PKH === bitcoinLikeInfo.P2PKH |
|||
) { |
|||
throw new UpdateYourApp(`UpdateYourApp ${currency.id}`, currency) |
|||
} |
|||
throw new BtcUnmatchedApp(`BtcUnmatchedApp ${currency.id}`, currency) |
|||
} |
|||
} |
|||
|
|||
return { address: bitcoinAddress, path, publicKey } |
|||
} |
@ -1,18 +0,0 @@ |
|||
// @flow
|
|||
|
|||
import type { CryptoCurrency } from '@ledgerhq/live-common/lib/types' |
|||
import Eth from '@ledgerhq/hw-app-eth' |
|||
import type Transport from '@ledgerhq/hw-transport' |
|||
import eip55 from 'eip55' |
|||
|
|||
export default async ( |
|||
transport: Transport<*>, |
|||
currency: CryptoCurrency, |
|||
path: string, |
|||
{ verify = false }: *, |
|||
) => { |
|||
const eth = new Eth(transport) |
|||
const r = await eth.getAddress(path, verify) |
|||
const address = eip55.encode(r.address) |
|||
return { path, address, publicKey: r.publicKey } |
|||
} |
@ -1,32 +0,0 @@ |
|||
// @flow
|
|||
|
|||
import type { CryptoCurrency } from '@ledgerhq/live-common/lib/types' |
|||
import invariant from 'invariant' |
|||
import type Transport from '@ledgerhq/hw-transport' |
|||
import bitcoin from './btc' |
|||
import ethereum from './ethereum' |
|||
import ripple from './ripple' |
|||
|
|||
type Resolver = ( |
|||
transport: Transport<*>, |
|||
currency: CryptoCurrency, |
|||
path: string, |
|||
options: { |
|||
segwit?: boolean, |
|||
verify?: boolean, |
|||
}, |
|||
) => Promise<{ address: string, path: string, publicKey: string }> |
|||
|
|||
const perFamily: { [_: string]: Resolver } = { |
|||
bitcoin, |
|||
ethereum, |
|||
ripple, |
|||
} |
|||
|
|||
const proxy: Resolver = (transport, currency, path, options) => { |
|||
const getAddress = perFamily[currency.family] |
|||
invariant(getAddress, `getAddress not implemented for ${currency.id}`) |
|||
return getAddress(transport, currency, path, options) |
|||
} |
|||
|
|||
export default proxy |
@ -1,16 +0,0 @@ |
|||
// @flow
|
|||
|
|||
import Xrp from '@ledgerhq/hw-app-xrp' |
|||
import type Transport from '@ledgerhq/hw-transport' |
|||
import type { CryptoCurrency } from '@ledgerhq/live-common/lib/types' |
|||
|
|||
export default async ( |
|||
transport: Transport<*>, |
|||
currency: CryptoCurrency, |
|||
path: string, |
|||
{ verify = false }: *, |
|||
) => { |
|||
const xrp = new Xrp(transport) |
|||
const { address, publicKey } = await xrp.getAddress(path, verify) |
|||
return { path, address, publicKey } |
|||
} |
Loading…
Reference in new issue