From 21ceb2b0a99ab4982eadb52ae648647bca0c8b3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Renaudeau?= Date: Mon, 4 Jun 2018 19:20:31 +0200 Subject: [PATCH] Fix getAddress --- src/commands/getAddress.js | 3 ++- src/commands/isCurrencyAppOpened.js | 4 ++-- src/helpers/getAddressForCurrency/btc.js | 3 ++- src/helpers/getAddressForCurrency/ethereum.js | 3 ++- src/helpers/getAddressForCurrency/index.js | 17 ++++++++--------- src/helpers/getAddressForCurrency/ripple.js | 3 ++- 6 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/commands/getAddress.js b/src/commands/getAddress.js index 2e0ae740..fd4c3a73 100644 --- a/src/commands/getAddress.js +++ b/src/commands/getAddress.js @@ -1,5 +1,6 @@ // @flow +import { getCryptoCurrencyById } from '@ledgerhq/live-common/lib/helpers/currencies' import { createCommand, Command } from 'helpers/ipc' import { fromPromise } from 'rxjs/observable/fromPromise' import { withDevice } from 'helpers/deviceAccess' @@ -24,7 +25,7 @@ const cmd: Command = createCommand( ({ currencyId, devicePath, path, ...options }) => fromPromise( withDevice(devicePath)(transport => - getAddressForCurrency(currencyId)(transport, currencyId, path, options), + getAddressForCurrency(transport, getCryptoCurrencyById(currencyId), path, options), ), ), ) diff --git a/src/commands/isCurrencyAppOpened.js b/src/commands/isCurrencyAppOpened.js index 378c9467..baeae613 100644 --- a/src/commands/isCurrencyAppOpened.js +++ b/src/commands/isCurrencyAppOpened.js @@ -26,9 +26,9 @@ const cmd: Command = createCommand( // First, we check if the app can derivates on the currency try { - await getAddress(currencyId)( + await getAddress( transport, - currencyId, + currency, standardDerivation({ currency, segwit: false, x: 0 }), { segwit: false }, ) diff --git a/src/helpers/getAddressForCurrency/btc.js b/src/helpers/getAddressForCurrency/btc.js index e623dfa2..a1a448cf 100644 --- a/src/helpers/getAddressForCurrency/btc.js +++ b/src/helpers/getAddressForCurrency/btc.js @@ -1,11 +1,12 @@ // @flow +import type { CryptoCurrency } from '@ledgerhq/live-common/lib/types' import Btc from '@ledgerhq/hw-app-btc' import type Transport from '@ledgerhq/hw-transport' export default async ( transport: Transport<*>, - currencyId: string, + currency: CryptoCurrency, path: string, { segwit = true, diff --git a/src/helpers/getAddressForCurrency/ethereum.js b/src/helpers/getAddressForCurrency/ethereum.js index 0f3f6180..4835c26d 100644 --- a/src/helpers/getAddressForCurrency/ethereum.js +++ b/src/helpers/getAddressForCurrency/ethereum.js @@ -1,12 +1,13 @@ // @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<*>, - currencyId: string, + currency: CryptoCurrency, path: string, { verify = false }: *, ) => { diff --git a/src/helpers/getAddressForCurrency/index.js b/src/helpers/getAddressForCurrency/index.js index 09e46319..b3d843d6 100644 --- a/src/helpers/getAddressForCurrency/index.js +++ b/src/helpers/getAddressForCurrency/index.js @@ -1,5 +1,6 @@ // @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' @@ -8,7 +9,7 @@ import ripple from './ripple' type Resolver = ( transport: Transport<*>, - currencyId: string, + currency: CryptoCurrency, path: string, options: { segwit?: boolean, @@ -16,18 +17,16 @@ type Resolver = ( }, ) => Promise<{ address: string, path: string, publicKey: string }> -type Module = (currencyId: string) => Resolver - -const perFamily = { +const perFamily: { [_: string]: Resolver } = { bitcoin, ethereum, ripple, } -const getAddressForCurrency: Module = (currencyId: string) => { - const getAddress = perFamily[currencyId] - invariant(getAddress, `getAddress not implemented for ${currencyId}`) - return getAddress +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 getAddressForCurrency +export default proxy diff --git a/src/helpers/getAddressForCurrency/ripple.js b/src/helpers/getAddressForCurrency/ripple.js index 5278d1da..8e98b42f 100644 --- a/src/helpers/getAddressForCurrency/ripple.js +++ b/src/helpers/getAddressForCurrency/ripple.js @@ -2,10 +2,11 @@ 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<*>, - currencyId: string, + currency: CryptoCurrency, path: string, { verify = false }: *, ) => {