From fc32b3c459c8be165bc226f01c1d52def2c5ef83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Renaudeau?= Date: Tue, 29 May 2018 17:24:53 +0200 Subject: [PATCH] better flowtype for crypto types --- package.json | 4 +-- src/components/EnsureDeviceApp/index.js | 2 ++ src/helpers/SettingsDefaults.js | 4 +-- src/helpers/explorers.js | 30 +++++++++++++++---- src/helpers/getAddressForCurrency/ethereum.js | 2 +- src/helpers/getAddressForCurrency/index.js | 28 +++++++++++++---- src/helpers/getAddressForCurrency/ripple.js | 2 +- yarn.lock | 22 +++++++++++--- 8 files changed, 73 insertions(+), 21 deletions(-) diff --git a/package.json b/package.json index 1ef4d994..d9b19027 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,7 @@ "@ledgerhq/hw-transport": "^4.12.0", "@ledgerhq/hw-transport-node-hid": "^4.12.0", "@ledgerhq/ledger-core": "^1.3.0", - "@ledgerhq/live-common": "2.12.0", + "@ledgerhq/live-common": "2.14.0", "axios": "^0.18.0", "babel-runtime": "^6.26.0", "bcryptjs": "^2.4.3", @@ -91,8 +91,8 @@ "ripple-lib": "^1.0.0-beta.0", "rxjs": "^6.2.0", "rxjs-compat": "^6.1.0", - "semaphore": "^1.1.0", "secp256k1": "3.3.1", + "semaphore": "^1.1.0", "smooth-scrollbar": "^8.2.7", "source-map": "0.7.2", "source-map-support": "^0.5.4", diff --git a/src/components/EnsureDeviceApp/index.js b/src/components/EnsureDeviceApp/index.js index bce04b25..1e6690d7 100644 --- a/src/components/EnsureDeviceApp/index.js +++ b/src/components/EnsureDeviceApp/index.js @@ -126,6 +126,8 @@ class EnsureDeviceApp extends PureComponent { if (appOptions) { const { address } = await getAddress.send(appOptions).toPromise() if (account && account.freshAddress !== address) { + console.log(account) + console.warn(account.freshAddress, address) throw new Error('Account address is different than device address') } } else { diff --git a/src/helpers/SettingsDefaults.js b/src/helpers/SettingsDefaults.js index 8c8456d8..97a1e4b0 100644 --- a/src/helpers/SettingsDefaults.js +++ b/src/helpers/SettingsDefaults.js @@ -1,6 +1,6 @@ // @flow -import type { CryptoCurrency } from '@ledgerhq/live-common/lib/types' +import type { CryptoCurrencyConfig, CryptoCurrency } from '@ledgerhq/live-common/lib/types' type ConfirmationDefaults = { confirmationsNb: ?{ @@ -12,7 +12,7 @@ type ConfirmationDefaults = { // This is approximated to be a 30mn confirmation in number of blocks on blockchains // to disable the confirmations feature simply set 0. -const confirmationsNbPerCoin = { +const confirmationsNbPerCoin: CryptoCurrencyConfig = { bitcoin: 2, ethereum: 120, ripple: 0, diff --git a/src/helpers/explorers.js b/src/helpers/explorers.js index 03d4abe8..1df1fa32 100644 --- a/src/helpers/explorers.js +++ b/src/helpers/explorers.js @@ -1,7 +1,11 @@ // @flow -import type { Account, Operation } from '@ledgerhq/live-common/lib/types' +import type { Account, Operation, CryptoCurrencyConfig } from '@ledgerhq/live-common/lib/types' -const txExplorers: { [_: string]: (Operation) => string } = { +type Explorer = Operation => ?string + +const fallback = () => null + +const txExplorers: CryptoCurrencyConfig = { bitcoin_cash: op => `https://bitcoincash.blockexplorer.com/tx/${op.hash}`, bitcoin_gold: op => `https://btgexplorer.com/tx/${op.hash}`, bitcoin_testnet: op => `https://testnet.blockchain.info/tx/${op.hash}`, @@ -10,9 +14,23 @@ const txExplorers: { [_: string]: (Operation) => string } = { ethereum: op => `https://etherscan.io/tx/${op.hash}`, ripple: op => `https://bithomp.com/explorer/${op.hash}`, zcash: op => `https://explorer.zcha.in/transactions/${op.hash}`, -} -export const getTxURL = (account: Account, operation: Operation): ?string => { - const f = txExplorers[account.currency.id] - return f ? f(operation) : null + viacoin: fallback, + vertcoin: fallback, + stratis: fallback, + stealthcoin: fallback, + qtum: fallback, + pivx: fallback, + peercoin: fallback, + komodo: fallback, + hshare: fallback, + dogecoin: fallback, + digibyte: fallback, + dash: fallback, + zencash: fallback, + litecoin: fallback, + ethereum_classic: fallback, } + +export const getTxURL = (account: Account, operation: Operation): ?string => + txExplorers[account.currency.id](operation) diff --git a/src/helpers/getAddressForCurrency/ethereum.js b/src/helpers/getAddressForCurrency/ethereum.js index 6f4d8021..0f3f6180 100644 --- a/src/helpers/getAddressForCurrency/ethereum.js +++ b/src/helpers/getAddressForCurrency/ethereum.js @@ -8,7 +8,7 @@ export default async ( transport: Transport<*>, currencyId: string, path: string, - { verify = false }: { verify: boolean }, + { verify = false }: *, ) => { const eth = new Eth(transport) const r = await eth.getAddress(path, verify) diff --git a/src/helpers/getAddressForCurrency/index.js b/src/helpers/getAddressForCurrency/index.js index eb15d6f6..532e227d 100644 --- a/src/helpers/getAddressForCurrency/index.js +++ b/src/helpers/getAddressForCurrency/index.js @@ -1,5 +1,6 @@ // @flow +import type { CryptoCurrencyConfig } from '@ledgerhq/live-common/lib/types' import type Transport from '@ledgerhq/hw-transport' import btc from './btc' import ethereum from './ethereum' @@ -9,7 +10,10 @@ type Resolver = ( transport: Transport<*>, currencyId: string, path: string, - options: *, + options: { + segwit?: boolean, + verify?: boolean, + }, ) => Promise<{ address: string, path: string, publicKey: string }> type Module = (currencyId: string) => Resolver @@ -17,10 +21,12 @@ type Module = (currencyId: string) => Resolver const fallback: string => Resolver = currencyId => () => Promise.reject(new Error(`${currencyId} device support not implemented`)) -const all = { +const all: CryptoCurrencyConfig = { bitcoin: btc, bitcoin_testnet: btc, + litecoin: btc, + zcash: btc, bitcoin_cash: btc, bitcoin_gold: btc, @@ -29,12 +35,24 @@ const all = { ethereum, ethereum_testnet: ethereum, ethereum_classic: ethereum, - ethereum_classic_testnet: ethereum, ripple, + + // TODO port of all these + viacoin: fallback('viacoin'), + vertcoin: fallback('vertcoin'), + stratis: fallback('stratis'), + stealthcoin: fallback('stealthcoin'), + qtum: fallback('qtum'), + pivx: fallback('pivx'), + peercoin: fallback('peercoin'), + komodo: fallback('komodo'), + hshare: fallback('hshare'), + dogecoin: fallback('dogecoin'), + digibyte: fallback('digibyte'), + dash: fallback('dash'), } -const getAddressForCurrency: Module = (currencyId: string) => - all[currencyId] || fallback(currencyId) +const getAddressForCurrency: Module = (currencyId: string) => all[currencyId] export default getAddressForCurrency diff --git a/src/helpers/getAddressForCurrency/ripple.js b/src/helpers/getAddressForCurrency/ripple.js index 61398a58..5278d1da 100644 --- a/src/helpers/getAddressForCurrency/ripple.js +++ b/src/helpers/getAddressForCurrency/ripple.js @@ -7,7 +7,7 @@ export default async ( transport: Transport<*>, currencyId: string, path: string, - { verify = false }: { verify: boolean }, + { verify = false }: *, ) => { const xrp = new Xrp(transport) const { address, publicKey } = await xrp.getAddress(path, verify) diff --git a/yarn.lock b/yarn.lock index 4b330f02..6e034a75 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1464,9 +1464,9 @@ npm "^5.7.1" prebuild-install "^2.2.2" -"@ledgerhq/live-common@2.12.0": - version "2.12.0" - resolved "https://registry.yarnpkg.com/@ledgerhq/live-common/-/live-common-2.12.0.tgz#7c60cb3d7829a0f55b6763efff96c8f4937b05f2" +"@ledgerhq/live-common@2.14.0": + version "2.14.0" + resolved "https://registry.yarnpkg.com/@ledgerhq/live-common/-/live-common-2.14.0.tgz#9487e825826e7a626a4005cc45a82743bdb0a298" dependencies: axios "^0.18.0" invariant "^2.2.2" @@ -10846,7 +10846,7 @@ prando@^3.0.1: version "3.0.3" resolved "https://registry.yarnpkg.com/prando/-/prando-3.0.3.tgz#1248d6d3c07e70bd645ef7bdaaa21c35d0d6d2e0" -prebuild-install@^2.2.2: +prebuild-install@^2.0.0, prebuild-install@^2.2.2: version "2.5.3" resolved "https://registry.yarnpkg.com/prebuild-install/-/prebuild-install-2.5.3.tgz#9f65f242782d370296353710e9bc843490c19f69" dependencies: @@ -12260,6 +12260,20 @@ scoped-regex@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/scoped-regex/-/scoped-regex-1.0.0.tgz#a346bb1acd4207ae70bd7c0c7ca9e566b6baddb8" +secp256k1@3.3.1: + version "3.3.1" + resolved "https://registry.yarnpkg.com/secp256k1/-/secp256k1-3.3.1.tgz#d1d325519db714789c11ec0450d4b9a3aa01eb1a" + dependencies: + bindings "^1.2.1" + bip66 "^1.1.3" + bn.js "^4.11.3" + create-hash "^1.1.2" + drbg.js "^1.0.1" + elliptic "^6.2.3" + nan "^2.2.1" + prebuild-install "^2.0.0" + safe-buffer "^5.1.0" + secp256k1@^3.0.1: version "3.5.0" resolved "https://registry.yarnpkg.com/secp256k1/-/secp256k1-3.5.0.tgz#677d3b8a8e04e1a5fa381a1ae437c54207b738d0"