|
|
@ -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 |
|
|
|