diff --git a/src/helpers/db.js b/src/helpers/db.js index 19b5e636..499b7011 100644 --- a/src/helpers/db.js +++ b/src/helpers/db.js @@ -4,9 +4,7 @@ import Store from 'electron-store' import set from 'lodash/set' import get from 'lodash/get' -import { getDefaultUnitByCoinType, getCurrencyByCoinType } from '@ledgerhq/currencies' - -import type { Accounts } from 'types/common' +import { serializeAccounts, deserializeAccounts } from 'reducers/accounts' type DBKey = 'settings' | 'accounts' @@ -25,46 +23,6 @@ export function setEncryptionKey(key: DBKey, value?: string) { encryptionKey[key] = value } -export function serializeAccounts(accounts: Array) { - return accounts.map((account, key) => { - const a = { - id: account.id, - address: account.address, - addresses: account.addresses, - balance: account.balance, - coinType: account.coinType, - currency: getCurrencyByCoinType(account.coinType), - index: account.index, - name: account.name || `${key}`, - path: account.path, - unit: account.unit || getDefaultUnitByCoinType(account.coinType), - } - - return { - ...a, - transactions: account.transactions.map(t => ({ - ...t, - account: a, - })), - } - }) -} - -export function deserializeAccounts(accounts: Accounts) { - return accounts.map(account => ({ - id: account.id, - address: account.address, - addresses: account.addresses, - balance: account.balance, - coinType: account.coinType, - index: account.index, - name: account.name, - path: account.path, - transactions: account.transactions.map(({ account, ...t }) => t), - unit: account.unit, - })) -} - function middleware(type, key, data: any) { if (key === 'accounts') { if (type === 'get') { diff --git a/src/internals/usb/wallet/accounts.js b/src/internals/usb/wallet/accounts.js index 7c0ab3cb..74fd3f74 100644 --- a/src/internals/usb/wallet/accounts.js +++ b/src/internals/usb/wallet/accounts.js @@ -7,7 +7,7 @@ import bs58check from 'bs58check' import Btc from '@ledgerhq/hw-app-btc' import { getAccount, getHDNode, networks } from 'helpers/btc' -import { serializeAccounts } from 'helpers/db' +import { serializeAccounts } from 'reducers/accounts' type CoinType = 0 | 1 diff --git a/src/reducers/accounts.js b/src/reducers/accounts.js index eb31368f..06ef3eac 100644 --- a/src/reducers/accounts.js +++ b/src/reducers/accounts.js @@ -6,6 +6,8 @@ import every from 'lodash/every' import get from 'lodash/get' import reduce from 'lodash/reduce' +import { getDefaultUnitByCoinType, getCurrencyByCoinType } from '@ledgerhq/currencies' + import type { State } from 'reducers' import type { Account, Accounts } from 'types/common' @@ -96,4 +98,44 @@ export function canCreateAccount(state: State): boolean { return every(getAccounts(state), a => get(a, 'transactions.length', 0) > 0) } +export function serializeAccounts(accounts: Array) { + return accounts.map((account, key) => { + const a = { + id: account.id, + address: account.address, + addresses: account.addresses, + balance: account.balance, + coinType: account.coinType, + currency: getCurrencyByCoinType(account.coinType), + index: account.index, + name: account.name || `${key}`, + path: account.path, + unit: account.unit || getDefaultUnitByCoinType(account.coinType), + } + + return { + ...a, + transactions: account.transactions.map(t => ({ + ...t, + account: a, + })), + } + }) +} + +export function deserializeAccounts(accounts: Accounts) { + return accounts.map(account => ({ + id: account.id, + address: account.address, + addresses: account.addresses, + balance: account.balance, + coinType: account.coinType, + index: account.index, + name: account.name, + path: account.path, + transactions: account.transactions.map(({ account, ...t }) => t), + unit: account.unit, + })) +} + export default handleActions(handlers, state)