From b43f6733007e9b18b54e00c5d7a3721ac3c2e5b8 Mon Sep 17 00:00:00 2001 From: meriadec Date: Sun, 3 Jun 2018 12:31:07 +0200 Subject: [PATCH] Flow fixes and typecheck account id encode/decode --- src/commands/libcoreGetVersion.js | 2 +- src/commands/libcoreSignAndBroadcast.js | 15 ++++++++++- src/helpers/accountId.js | 16 ++++++++---- src/helpers/libcore.js | 33 ++++++++++++++----------- 4 files changed, 45 insertions(+), 21 deletions(-) diff --git a/src/commands/libcoreGetVersion.js b/src/commands/libcoreGetVersion.js index 43a9924f..e8fbd065 100644 --- a/src/commands/libcoreGetVersion.js +++ b/src/commands/libcoreGetVersion.js @@ -11,7 +11,7 @@ type Result = { stringVersion: string, intVersion: number } const cmd: Command = createCommand('libcoreGetVersion', () => fromPromise( - withLibcore(ledgerCore => { + withLibcore(async ledgerCore => { const core = new ledgerCore.NJSLedgerCore() const stringVersion = core.getStringVersion() const intVersion = core.getIntVersion() diff --git a/src/commands/libcoreSignAndBroadcast.js b/src/commands/libcoreSignAndBroadcast.js index 8ae4d954..22324197 100644 --- a/src/commands/libcoreSignAndBroadcast.js +++ b/src/commands/libcoreSignAndBroadcast.js @@ -4,6 +4,7 @@ import type { AccountRaw, OperationRaw } from '@ledgerhq/live-common/lib/types' import Btc from '@ledgerhq/hw-app-btc' import { fromPromise } from 'rxjs/observable/fromPromise' import { getCryptoCurrencyById } from '@ledgerhq/live-common/lib/helpers/currencies' +import type Transport from '@ledgerhq/hw-transport' import withLibcore from 'helpers/withLibcore' import { createCommand, Command } from 'helpers/ipc' @@ -42,7 +43,19 @@ const cmd: Command = createCommand( ), ) -export async function doSignAndBroadcast({ account, transaction, deviceId, core, transport }) { +export async function doSignAndBroadcast({ + account, + transaction, + deviceId, + core, + transport, +}: { + account: AccountRaw, + transaction: BitcoinLikeTransaction, + deviceId: string, + core: *, + transport: Transport<*>, +}) { const hwApp = new Btc(transport) const WALLET_IDENTIFIER = await getWalletIdentifier({ diff --git a/src/helpers/accountId.js b/src/helpers/accountId.js index b70b5cef..543a8223 100644 --- a/src/helpers/accountId.js +++ b/src/helpers/accountId.js @@ -1,16 +1,22 @@ // @flow +import invariant from 'invariant' + type Params = { type: string, + version: string, xpub: string, walletName: string, } -export function encode({ type, xpub, walletName }: Params) { - return `${type}:${xpub}:${walletName}` +export function encode({ type, version, xpub, walletName }: Params) { + return `${type}:${version}:${xpub}:${walletName}` } -export function decode(accountId: string) { - const [type, xpub, walletName] = accountId.split(':') - return { type, xpub, walletName } +export function decode(accountId: string): Params { + invariant(typeof accountId === 'string', 'accountId is not a string') + const splitted = accountId.split(':') + invariant(splitted.length === 4, 'invalid size for accountId') + const [type, version, xpub, walletName] = splitted + return { type, version, xpub, walletName } } diff --git a/src/helpers/libcore.js b/src/helpers/libcore.js index 2a09a3a4..6fd035d1 100644 --- a/src/helpers/libcore.js +++ b/src/helpers/libcore.js @@ -10,7 +10,7 @@ import type { NJSAccount, NJSOperation } from '@ledgerhq/ledger-core/src/ledgerc import * as accountIdHelper from 'helpers/accountId' type Props = { - core: Object, + core: *, devicePath: string, currencyId: string, onAccountScanned: AccountRaw => void, @@ -82,7 +82,7 @@ async function scanAccountsOnDeviceBySegwit({ isSegwit, showNewAccount, }: { - core: Object, + core: *, hwApp: Object, currencyId: string, onAccountScanned: AccountRaw => void, @@ -118,7 +118,7 @@ async function scanAccountsOnDeviceBySegwit({ async function scanNextAccount(props: { // $FlowFixMe wallet: NJSWallet, - core: Object, + core: *, hwApp: Object, currencyId: string, accountsCount: number, @@ -183,7 +183,7 @@ async function scanNextAccount(props: { } async function getOrCreateWallet( - core: Object, + core: *, WALLET_IDENTIFIER: string, currencyId: string, isSegwit: boolean, @@ -220,7 +220,7 @@ async function buildAccountRaw({ wallet: NJSWallet, currencyId: string, accountIndex: number, - core: Object, + core: *, // $FlowFixMe ops: NJSOperation[], }): Promise { @@ -262,7 +262,12 @@ async function buildAccountRaw({ } const rawAccount: AccountRaw = { - id: accountIdHelper.encode({ type: 'libcore', xpub, walletName: wallet.getName() }), + id: accountIdHelper.encode({ + type: 'libcore', + version: '1', + xpub, + walletName: wallet.getName(), + }), xpub, path: walletPath, name, @@ -288,7 +293,7 @@ function buildOperationRaw({ op, xpub, }: { - core: Object, + core: *, op: NJSOperation, xpub: string, }): OperationRaw { @@ -322,15 +327,15 @@ function buildOperationRaw({ } export async function getNJSAccount({ - account, + accountRaw, njsWalletPool, }: { - accountId: string, - njsWalletPool: any, + accountRaw: AccountRaw, + njsWalletPool: *, }) { - const decodedAccountId = accountIdHelper.decode(account.id) + const decodedAccountId = accountIdHelper.decode(accountRaw.id) const njsWallet = await njsWalletPool.getWallet(decodedAccountId.walletName) - const njsAccount = await njsWallet.getAccount(account.index) + const njsAccount = await njsWallet.getAccount(accountRaw.index) return njsAccount } @@ -339,9 +344,9 @@ export async function syncAccount({ core, njsWalletPool, }: { - core: Object, + core: *, rawAccount: AccountRaw, - njsWalletPool: Object, + njsWalletPool: *, }) { const decodedAccountId = accountIdHelper.decode(rawAccount.id) const njsWallet = await njsWalletPool.getWallet(decodedAccountId.walletName)