Browse Source

better flowtype for crypto types

master
Gaëtan Renaudeau 7 years ago
parent
commit
fc32b3c459
  1. 4
      package.json
  2. 2
      src/components/EnsureDeviceApp/index.js
  3. 4
      src/helpers/SettingsDefaults.js
  4. 30
      src/helpers/explorers.js
  5. 2
      src/helpers/getAddressForCurrency/ethereum.js
  6. 28
      src/helpers/getAddressForCurrency/index.js
  7. 2
      src/helpers/getAddressForCurrency/ripple.js
  8. 22
      yarn.lock

4
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",

2
src/components/EnsureDeviceApp/index.js

@ -126,6 +126,8 @@ class EnsureDeviceApp extends PureComponent<Props, State> {
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 {

4
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<number> = {
bitcoin: 2,
ethereum: 120,
ripple: 0,

30
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<Explorer> = {
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)

2
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)

28
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<Resolver> = {
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

2
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)

22
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"

Loading…
Cancel
Save