Browse Source

Flow fixes and typecheck account id encode/decode

master
meriadec 7 years ago
parent
commit
b43f673300
No known key found for this signature in database GPG Key ID: 1D2FC2305E2CB399
  1. 2
      src/commands/libcoreGetVersion.js
  2. 15
      src/commands/libcoreSignAndBroadcast.js
  3. 16
      src/helpers/accountId.js
  4. 33
      src/helpers/libcore.js

2
src/commands/libcoreGetVersion.js

@ -11,7 +11,7 @@ type Result = { stringVersion: string, intVersion: number }
const cmd: Command<Input, Result> = createCommand('libcoreGetVersion', () => const cmd: Command<Input, Result> = createCommand('libcoreGetVersion', () =>
fromPromise( fromPromise(
withLibcore(ledgerCore => { withLibcore(async ledgerCore => {
const core = new ledgerCore.NJSLedgerCore() const core = new ledgerCore.NJSLedgerCore()
const stringVersion = core.getStringVersion() const stringVersion = core.getStringVersion()
const intVersion = core.getIntVersion() const intVersion = core.getIntVersion()

15
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 Btc from '@ledgerhq/hw-app-btc'
import { fromPromise } from 'rxjs/observable/fromPromise' import { fromPromise } from 'rxjs/observable/fromPromise'
import { getCryptoCurrencyById } from '@ledgerhq/live-common/lib/helpers/currencies' import { getCryptoCurrencyById } from '@ledgerhq/live-common/lib/helpers/currencies'
import type Transport from '@ledgerhq/hw-transport'
import withLibcore from 'helpers/withLibcore' import withLibcore from 'helpers/withLibcore'
import { createCommand, Command } from 'helpers/ipc' import { createCommand, Command } from 'helpers/ipc'
@ -42,7 +43,19 @@ const cmd: Command<Input, Result> = 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 hwApp = new Btc(transport)
const WALLET_IDENTIFIER = await getWalletIdentifier({ const WALLET_IDENTIFIER = await getWalletIdentifier({

16
src/helpers/accountId.js

@ -1,16 +1,22 @@
// @flow // @flow
import invariant from 'invariant'
type Params = { type Params = {
type: string, type: string,
version: string,
xpub: string, xpub: string,
walletName: string, walletName: string,
} }
export function encode({ type, xpub, walletName }: Params) { export function encode({ type, version, xpub, walletName }: Params) {
return `${type}:${xpub}:${walletName}` return `${type}:${version}:${xpub}:${walletName}`
} }
export function decode(accountId: string) { export function decode(accountId: string): Params {
const [type, xpub, walletName] = accountId.split(':') invariant(typeof accountId === 'string', 'accountId is not a string')
return { type, xpub, walletName } const splitted = accountId.split(':')
invariant(splitted.length === 4, 'invalid size for accountId')
const [type, version, xpub, walletName] = splitted
return { type, version, xpub, walletName }
} }

33
src/helpers/libcore.js

@ -10,7 +10,7 @@ import type { NJSAccount, NJSOperation } from '@ledgerhq/ledger-core/src/ledgerc
import * as accountIdHelper from 'helpers/accountId' import * as accountIdHelper from 'helpers/accountId'
type Props = { type Props = {
core: Object, core: *,
devicePath: string, devicePath: string,
currencyId: string, currencyId: string,
onAccountScanned: AccountRaw => void, onAccountScanned: AccountRaw => void,
@ -82,7 +82,7 @@ async function scanAccountsOnDeviceBySegwit({
isSegwit, isSegwit,
showNewAccount, showNewAccount,
}: { }: {
core: Object, core: *,
hwApp: Object, hwApp: Object,
currencyId: string, currencyId: string,
onAccountScanned: AccountRaw => void, onAccountScanned: AccountRaw => void,
@ -118,7 +118,7 @@ async function scanAccountsOnDeviceBySegwit({
async function scanNextAccount(props: { async function scanNextAccount(props: {
// $FlowFixMe // $FlowFixMe
wallet: NJSWallet, wallet: NJSWallet,
core: Object, core: *,
hwApp: Object, hwApp: Object,
currencyId: string, currencyId: string,
accountsCount: number, accountsCount: number,
@ -183,7 +183,7 @@ async function scanNextAccount(props: {
} }
async function getOrCreateWallet( async function getOrCreateWallet(
core: Object, core: *,
WALLET_IDENTIFIER: string, WALLET_IDENTIFIER: string,
currencyId: string, currencyId: string,
isSegwit: boolean, isSegwit: boolean,
@ -220,7 +220,7 @@ async function buildAccountRaw({
wallet: NJSWallet, wallet: NJSWallet,
currencyId: string, currencyId: string,
accountIndex: number, accountIndex: number,
core: Object, core: *,
// $FlowFixMe // $FlowFixMe
ops: NJSOperation[], ops: NJSOperation[],
}): Promise<AccountRaw> { }): Promise<AccountRaw> {
@ -262,7 +262,12 @@ async function buildAccountRaw({
} }
const rawAccount: AccountRaw = { const rawAccount: AccountRaw = {
id: accountIdHelper.encode({ type: 'libcore', xpub, walletName: wallet.getName() }), id: accountIdHelper.encode({
type: 'libcore',
version: '1',
xpub,
walletName: wallet.getName(),
}),
xpub, xpub,
path: walletPath, path: walletPath,
name, name,
@ -288,7 +293,7 @@ function buildOperationRaw({
op, op,
xpub, xpub,
}: { }: {
core: Object, core: *,
op: NJSOperation, op: NJSOperation,
xpub: string, xpub: string,
}): OperationRaw { }): OperationRaw {
@ -322,15 +327,15 @@ function buildOperationRaw({
} }
export async function getNJSAccount({ export async function getNJSAccount({
account, accountRaw,
njsWalletPool, njsWalletPool,
}: { }: {
accountId: string, accountRaw: AccountRaw,
njsWalletPool: any, njsWalletPool: *,
}) { }) {
const decodedAccountId = accountIdHelper.decode(account.id) const decodedAccountId = accountIdHelper.decode(accountRaw.id)
const njsWallet = await njsWalletPool.getWallet(decodedAccountId.walletName) const njsWallet = await njsWalletPool.getWallet(decodedAccountId.walletName)
const njsAccount = await njsWallet.getAccount(account.index) const njsAccount = await njsWallet.getAccount(accountRaw.index)
return njsAccount return njsAccount
} }
@ -339,9 +344,9 @@ export async function syncAccount({
core, core,
njsWalletPool, njsWalletPool,
}: { }: {
core: Object, core: *,
rawAccount: AccountRaw, rawAccount: AccountRaw,
njsWalletPool: Object, njsWalletPool: *,
}) { }) {
const decodedAccountId = accountIdHelper.decode(rawAccount.id) const decodedAccountId = accountIdHelper.decode(rawAccount.id)
const njsWallet = await njsWalletPool.getWallet(decodedAccountId.walletName) const njsWallet = await njsWalletPool.getWallet(decodedAccountId.walletName)

Loading…
Cancel
Save