From a72a67473efd505403760bf19f13a29339970909 Mon Sep 17 00:00:00 2001 From: Sergei Novikov Date: Mon, 22 May 2023 16:12:25 +0300 Subject: [PATCH] solution: create shadow entry when adding new coin --- .../src/create-wallet/CreateWalletScreen.tsx | 16 ++++----- packages/store/src/accounts/sagas.ts | 34 +++++++++++++++++-- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/packages/react-app/src/create-wallet/CreateWalletScreen.tsx b/packages/react-app/src/create-wallet/CreateWalletScreen.tsx index 6fe44bd51..e5ef2baf4 100644 --- a/packages/react-app/src/create-wallet/CreateWalletScreen.tsx +++ b/packages/react-app/src/create-wallet/CreateWalletScreen.tsx @@ -16,7 +16,7 @@ import { connect } from 'react-redux'; import CreateWalletWizard from './CreateWalletWizard'; import { Result, isLedger, isPk, isPkJson, isPkRaw, isSeedCreate, isSeedSelected } from './flow/types'; -type NewEntry = AddEntry & { additional: boolean }; +type NewEntry = AddEntry & { shadow: boolean }; type StateProps = { blockchains: IBlockchain[]; @@ -39,7 +39,7 @@ function entriesForBlockchains( indexes: HDPathIndexes, ): NewEntry[] { const entries = blockchains.map((blockchain) => ({ - additional: false, + shadow: false, blockchain: blockchainCodeToId(blockchain), key: { address: addresses[blockchain], @@ -62,7 +62,7 @@ function entriesForBlockchains( if (entry != null) { additionalEntries.push({ ...entry, - additional: true, + shadow: true, blockchain: blockchainCodeToId(blockchain === BlockchainCode.ETC ? BlockchainCode.ETH : BlockchainCode.ETC), }); } @@ -129,7 +129,7 @@ export default connect( entries.push({ ...pkEntry, - additional: false, + shadow: false, blockchain: blockchainCodeToId(blockchainCode), }); } else if (isPkRaw(type)) { @@ -143,7 +143,7 @@ export default connect( entries.push({ ...pkEntry, - additional: false, + shadow: false, blockchain: blockchainCodeToId(blockchainCode), }); } @@ -156,7 +156,7 @@ export default connect( entries.push({ ...pkEntry, blockchain, - additional: true, + shadow: true, }); } } else if (isLedger(type)) { @@ -171,8 +171,8 @@ export default connect( exactEntries: AddEntry[]; receiveDisabled: Array<{ blockchain: number; address?: string }>; }>( - (carry, { additional, ...entry }) => { - if (additional) { + (carry, { shadow, ...entry }) => { + if (shadow) { return { exactEntries: [...carry.exactEntries, entry], receiveDisabled: [ diff --git a/packages/store/src/accounts/sagas.ts b/packages/store/src/accounts/sagas.ts index fe168ce10..d09a0decc 100644 --- a/packages/store/src/accounts/sagas.ts +++ b/packages/store/src/accounts/sagas.ts @@ -8,6 +8,7 @@ import { } from '@emeraldpay/emerald-vault-core'; import { BitcoinEntry, isBitcoinEntry, isEthereumEntry } from '@emeraldpay/emerald-vault-core/lib/types'; import { + BlockchainCode, Blockchains, IpcCommands, PersistentState, @@ -204,14 +205,41 @@ function* createHdAddress(vault: IEmeraldVault, action: ICreateHdEntry): SagaIte return; } - ipcRenderer.send(IpcCommands.BALANCE_SUBSCRIBE, blockchain, entry.id, [entry.address.value]); - const tokenRegistry = new TokenRegistry(action.tokens); const tokens = tokenRegistry.byBlockchain(blockchain); - yield put(requestTokensBalances(blockchain, tokens, entry.address.value)); + if (blockchain === BlockchainCode.ETC || blockchain === BlockchainCode.ETH) { + const addShadowEntry = { + ...addEntry, + blockchain: blockchainCodeToId(blockchain === BlockchainCode.ETH ? BlockchainCode.ETC : BlockchainCode.ETH), + }; + + const shadowEntryId = yield call(vault.addEntry, walletId, addShadowEntry); + + yield call(vault.setEntryReceiveDisabled, shadowEntryId, true); + + wallet = yield call(vault.getWallet, walletId); + + const shadowEntry = wallet.entries.find(({ id }) => id === shadowEntryId); + + if (shadowEntry?.address?.value != null) { + const { value: shadowAddress } = shadowEntry.address; + + ipcRenderer.send(IpcCommands.BALANCE_SUBSCRIBE, blockchain, shadowEntryId, [shadowAddress]); + + yield put(requestTokensBalances(blockchain, tokens, shadowAddress)); + yield put(hdAccountCreated(wallet.id, shadowEntry)); + } + } + + const { value: entryAddress } = entry.address; + + ipcRenderer.send(IpcCommands.BALANCE_SUBSCRIBE, blockchain, entry.id, [entryAddress]); + + yield put(requestTokensBalances(blockchain, tokens, entryAddress)); yield put(hdAccountCreated(wallet.id, entry)); + yield put(screen.actions.gotoScreen(screen.Pages.WALLET, wallet.id)); } catch (error) { if (error instanceof Error) {