Browse Source

Merge pull request #1505 from gre/fix-dgb

Fix using getOrCreateWallet for all access to getWallet
master
Thibaut 7 years ago
committed by GitHub
parent
commit
0cbbab39d7
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  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 libcoreScanAccounts from 'commands/libcoreScanAccounts'
import libcoreSyncAccount from 'commands/libcoreSyncAccount' import libcoreSyncAccount from 'commands/libcoreSyncAccount'
import libcoreSignAndBroadcast from 'commands/libcoreSignAndBroadcast' import libcoreSignAndBroadcast from 'commands/libcoreSignAndBroadcast'
import libcoreGetFees from 'commands/libcoreGetFees' import libcoreGetFees, { extractGetFeesInputFromAccount } from 'commands/libcoreGetFees'
import libcoreValidAddress from 'commands/libcoreValidAddress' import libcoreValidAddress from 'commands/libcoreValidAddress'
import { NotEnoughBalance } from 'config/errors' import { NotEnoughBalance } from 'config/errors'
import type { WalletBridge, EditProps } from './types' import type { WalletBridge, EditProps } from './types'
@ -85,8 +85,7 @@ const getFees = async (a, transaction) => {
if (promise) return promise if (promise) return promise
promise = libcoreGetFees promise = libcoreGetFees
.send({ .send({
accountId: a.id, ...extractGetFeesInputFromAccount(a),
accountIndex: a.index,
transaction: serializeTransaction(transaction), transaction: serializeTransaction(transaction),
}) })
.toPromise() .toPromise()

28
src/commands/libcoreGetFees.js

@ -4,9 +4,17 @@ import { Observable } from 'rxjs'
import { BigNumber } from 'bignumber.js' import { BigNumber } from 'bignumber.js'
import withLibcore from 'helpers/withLibcore' import withLibcore from 'helpers/withLibcore'
import { createCommand, Command } from 'helpers/ipc' import { createCommand, Command } from 'helpers/ipc'
import type { Account } from '@ledgerhq/live-common/lib/types'
import * as accountIdHelper from 'helpers/accountId' 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 { InvalidAddress } from 'config/errors'
import { splittedCurrencies } from 'config/cryptocurrencies'
type BitcoinLikeTransaction = { type BitcoinLikeTransaction = {
// TODO we rename this Transaction concept into transactionInput // TODO we rename this Transaction concept into transactionInput
@ -19,20 +27,34 @@ type Input = {
accountId: string, accountId: string,
accountIndex: number, accountIndex: number,
transaction: BitcoinLikeTransaction, 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 } type Result = { totalFees: string }
const cmd: Command<Input, Result> = createCommand( const cmd: Command<Input, Result> = createCommand(
'libcoreGetFees', 'libcoreGetFees',
({ accountId, accountIndex, transaction }) => ({ accountId, currencyId, isSegwit, isUnsplit, accountIndex, transaction }) =>
Observable.create(o => { Observable.create(o => {
let unsubscribed = false let unsubscribed = false
const isCancelled = () => unsubscribed const isCancelled = () => unsubscribed
withLibcore(async core => { withLibcore(async core => {
const { walletName } = accountIdHelper.decode(accountId) const { walletName } = accountIdHelper.decode(accountId)
const njsWallet = await core.getPoolInstance().getWallet(walletName) const njsWallet = await getOrCreateWallet(core, walletName, currencyId, isSegwit, isUnsplit)
if (isCancelled()) return if (isCancelled()) return
const njsAccount = await njsWallet.getAccount(accountIndex) const njsAccount = await njsWallet.getAccount(accountIndex)
if (isCancelled()) return 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 Btc from '@ledgerhq/hw-app-btc'
import { Observable } from 'rxjs' import { Observable } from 'rxjs'
import { getCryptoCurrencyById } from '@ledgerhq/live-common/lib/helpers/currencies' import { getCryptoCurrencyById } from '@ledgerhq/live-common/lib/helpers/currencies'
import { isSegwitPath } from 'helpers/bip32' import { isSegwitPath, isUnsplitPath } from 'helpers/bip32'
import { libcoreAmountToBigNumber, bigNumberToLibcoreAmount } from 'helpers/libcore' import {
libcoreAmountToBigNumber,
bigNumberToLibcoreAmount,
getOrCreateWallet,
} from 'helpers/libcore'
import { splittedCurrencies } from 'config/cryptocurrencies'
import withLibcore from 'helpers/withLibcore' import withLibcore from 'helpers/withLibcore'
import { createCommand, Command } from 'helpers/ipc' import { createCommand, Command } from 'helpers/ipc'
@ -187,7 +192,10 @@ export async function doSignAndBroadcast({
onOperationBroadcasted: (optimisticOp: $Exact<OperationRaw>) => void, onOperationBroadcasted: (optimisticOp: $Exact<OperationRaw>) => void,
}): Promise<void> { }): Promise<void> {
const { walletName } = accountIdHelper.decode(accountId) 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 if (isCancelled()) return
const njsAccount = await njsWallet.getAccount(index) const njsAccount = await njsWallet.getAccount(index)
if (isCancelled()) return if (isCancelled()) return

2
src/helpers/libcore.js

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

Loading…
Cancel
Save