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', () =>
fromPromise(
withLibcore(ledgerCore => {
withLibcore(async ledgerCore => {
const core = new ledgerCore.NJSLedgerCore()
const stringVersion = core.getStringVersion()
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 { 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<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 WALLET_IDENTIFIER = await getWalletIdentifier({

16
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 }
}

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'
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<AccountRaw> {
@ -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)

Loading…
Cancel
Save