diff --git a/src/bridge/LibcoreBridge.js b/src/bridge/LibcoreBridge.js index 642788d1..c9b9535d 100644 --- a/src/bridge/LibcoreBridge.js +++ b/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() diff --git a/src/commands/libcoreGetFees.js b/src/commands/libcoreGetFees.js index ab96ff30..58e8512c 100644 --- a/src/commands/libcoreGetFees.js +++ b/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 = 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 diff --git a/src/commands/libcoreSignAndBroadcast.js b/src/commands/libcoreSignAndBroadcast.js index 83d36a2b..d1fbd814 100644 --- a/src/commands/libcoreSignAndBroadcast.js +++ b/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) => void, }): Promise { 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 diff --git a/src/helpers/libcore.js b/src/helpers/libcore.js index 5f7f20e5..395b3b64 100644 --- a/src/helpers/libcore.js +++ b/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,