Browse Source

Fix using getOrCreateWallet for all access to getWallet

master
Gaëtan Renaudeau 7 years ago
parent
commit
187e21f099
No known key found for this signature in database GPG Key ID: 7B66B85F042E5451
  1. 5
      src/bridge/LibcoreBridge.js
  2. 28
      src/commands/libcoreGetFees.js
  3. 14
      src/commands/libcoreSignAndBroadcast.js
  4. 2
      src/helpers/libcore.js

5
src/bridge/LibcoreBridge.js

@ -9,7 +9,7 @@ import FeesBitcoinKind from 'components/FeesField/BitcoinKind'
import libcoreScanAccounts from 'commands/libcoreScanAccounts'
import libcoreSyncAccount from 'commands/libcoreSyncAccount'
import libcoreSignAndBroadcast from 'commands/libcoreSignAndBroadcast'
import libcoreGetFees from 'commands/libcoreGetFees'
import libcoreGetFees, { extractGetFeesInputFromAccount } from 'commands/libcoreGetFees'
import libcoreValidAddress from 'commands/libcoreValidAddress'
import { NotEnoughBalance } from 'config/errors'
import type { WalletBridge, EditProps } from './types'
@ -85,8 +85,7 @@ const getFees = async (a, transaction) => {
if (promise) return promise
promise = libcoreGetFees
.send({
accountId: a.id,
accountIndex: a.index,
...extractGetFeesInputFromAccount(a),
transaction: serializeTransaction(transaction),
})
.toPromise()

28
src/commands/libcoreGetFees.js

@ -4,9 +4,17 @@ import { Observable } from 'rxjs'
import { BigNumber } from 'bignumber.js'
import withLibcore from 'helpers/withLibcore'
import { createCommand, Command } from 'helpers/ipc'
import type { Account } from '@ledgerhq/live-common/lib/types'
import * as accountIdHelper from 'helpers/accountId'
import { isValidAddress, libcoreAmountToBigNumber, bigNumberToLibcoreAmount } from 'helpers/libcore'
import {
isValidAddress,
libcoreAmountToBigNumber,
bigNumberToLibcoreAmount,
getOrCreateWallet,
} from 'helpers/libcore'
import { isSegwitPath, isUnsplitPath } from 'helpers/bip32'
import { InvalidAddress } from 'config/errors'
import { splittedCurrencies } from 'config/cryptocurrencies'
type BitcoinLikeTransaction = {
// TODO we rename this Transaction concept into transactionInput
@ -19,20 +27,34 @@ type Input = {
accountId: string,
accountIndex: number,
transaction: BitcoinLikeTransaction,
currencyId: string,
isSegwit: boolean,
isUnsplit: boolean,
}
export const extractGetFeesInputFromAccount = (a: Account) => {
const currencyId = a.currency.id
return {
accountId: a.id,
accountIndex: a.index,
currencyId,
isSegwit: isSegwitPath(a.freshAddressPath),
isUnsplit: isUnsplitPath(a.freshAddressPath, splittedCurrencies[currencyId]),
}
}
type Result = { totalFees: string }
const cmd: Command<Input, Result> = createCommand(
'libcoreGetFees',
({ accountId, accountIndex, transaction }) =>
({ accountId, currencyId, isSegwit, isUnsplit, accountIndex, transaction }) =>
Observable.create(o => {
let unsubscribed = false
const isCancelled = () => unsubscribed
withLibcore(async core => {
const { walletName } = accountIdHelper.decode(accountId)
const njsWallet = await core.getPoolInstance().getWallet(walletName)
const njsWallet = await getOrCreateWallet(core, walletName, currencyId, isSegwit, isUnsplit)
if (isCancelled()) return
const njsAccount = await njsWallet.getAccount(accountIndex)
if (isCancelled()) return

14
src/commands/libcoreSignAndBroadcast.js

@ -6,8 +6,13 @@ import type { OperationRaw } from '@ledgerhq/live-common/lib/types'
import Btc from '@ledgerhq/hw-app-btc'
import { Observable } from 'rxjs'
import { getCryptoCurrencyById } from '@ledgerhq/live-common/lib/helpers/currencies'
import { isSegwitPath } from 'helpers/bip32'
import { libcoreAmountToBigNumber, bigNumberToLibcoreAmount } from 'helpers/libcore'
import { isSegwitPath, isUnsplitPath } from 'helpers/bip32'
import {
libcoreAmountToBigNumber,
bigNumberToLibcoreAmount,
getOrCreateWallet,
} from 'helpers/libcore'
import { splittedCurrencies } from 'config/cryptocurrencies'
import withLibcore from 'helpers/withLibcore'
import { createCommand, Command } from 'helpers/ipc'
@ -187,7 +192,10 @@ export async function doSignAndBroadcast({
onOperationBroadcasted: (optimisticOp: $Exact<OperationRaw>) => void,
}): Promise<void> {
const { walletName } = accountIdHelper.decode(accountId)
const njsWallet = await core.getPoolInstance().getWallet(walletName)
const isSegwit = isSegwitPath(freshAddressPath)
const isUnsplit = isUnsplitPath(freshAddressPath, splittedCurrencies[currencyId])
const njsWallet = await getOrCreateWallet(core, walletName, currencyId, isSegwit, isUnsplit)
if (isCancelled()) return
const njsAccount = await njsWallet.getAccount(index)
if (isCancelled()) return

2
src/helpers/libcore.js

@ -321,7 +321,7 @@ const createWalletConfig = (core, configMap = {}) => {
return config
}
async function getOrCreateWallet(
export async function getOrCreateWallet(
core: *,
walletName: string,
currencyId: string,

Loading…
Cancel
Save