From 4fd938d91a8680e576d5017fc0efdea5d5529066 Mon Sep 17 00:00:00 2001 From: Tom Kirkpatrick Date: Sun, 10 May 2020 23:59:36 +0200 Subject: [PATCH] fix(wallet): only generate new address after use --- renderer/reducers/transaction.js | 28 +++++++------------ .../__snapshots__/transaction.spec.js.snap | 17 ----------- test/unit/reducers/transaction.spec.js | 9 ------ 3 files changed, 10 insertions(+), 44 deletions(-) diff --git a/renderer/reducers/transaction.js b/renderer/reducers/transaction.js index 632fdc4c994..0f9fa35b1ca 100644 --- a/renderer/reducers/transaction.js +++ b/renderer/reducers/transaction.js @@ -1,7 +1,8 @@ import { createSelector } from 'reselect' import uniqBy from 'lodash/uniqBy' +import uniq from 'lodash/uniq' import last from 'lodash/last' -import find from 'lodash' +import find from 'lodash/find' import createReducer from '@zap/utils/createReducer' import { showSystemNotification } from '@zap/utils/notifications' import { convert } from '@zap/utils/btc' @@ -14,9 +15,10 @@ import { grpc } from 'workers' import { addressSelectors, newAddress } from './address' import { fetchBalance } from './balance' import { fetchChannels, channelsSelectors, getChannelData } from './channels' -import { settingsSelectors } from './settings' import messages from './messages' +let usedAddresses = [] + // ------------------------------------ // Initial State // ------------------------------------ @@ -37,7 +39,6 @@ export const SEND_TRANSACTION = 'SEND_TRANSACTION' export const TRANSACTION_SUCCESSFUL = 'TRANSACTION_SUCCESSFUL' export const TRANSACTION_FAILED = 'TRANSACTION_FAILED' export const TRANSACTION_COMPLETE = 'TRANSACTION_COMPLETE' -export const ADD_TRANSACTION = 'ADD_TRANSACTION' // ------------------------------------ // Helpers @@ -109,7 +110,6 @@ export const receiveTransactions = (transactions, updateOnly = false) => (dispat const state = getState() const currentAddresses = addressSelectors.currentAddresses(state) - let usedAddresses = [] // index of the last tx in `transactions` // that is newer(or equal) than the last tx from the state. @@ -123,7 +123,7 @@ export const receiveTransactions = (transactions, updateOnly = false) => (dispat lastKnownTxIndex = index } // Keep track of used addresses. - usedAddresses = usedAddresses.concat(destAddresses) + usedAddresses = uniq(usedAddresses.concat(destAddresses)) }) dispatch({ @@ -138,7 +138,7 @@ export const receiveTransactions = (transactions, updateOnly = false) => (dispat } }) - // fetch new balance + // Fetch updated balance. dispatch(fetchBalance()) } @@ -239,27 +239,22 @@ export const receiveTransactionData = transaction => (dispatch, getState) => { const state = getState() const intl = getIntl() - if (state.transaction.transactions.find(tx => tx.txHash === transaction.txHash)) { - dispatch(receiveTransactions([transaction], true)) - } else { - dispatch({ type: ADD_TRANSACTION, transaction }) + dispatch(receiveTransactions([transaction])) + if (!state.transaction.transactions.find(tx => tx.txHash === transaction.txHash)) { // HTML 5 desktop notification for the new transaction if (CoinBig(transaction.amount).gt(0)) { showSystemNotification(intl.formatMessage(messages.transaction_received_title), { body: intl.formatMessage(messages.transaction_received_body), }) - // Generate a new address - dispatch(newAddress(settingsSelectors.currentConfig(state).address)) + // Fetch updated channels. + dispatch(fetchChannels()) } else { showSystemNotification(intl.formatMessage(messages.transaction_sent_title), { body: intl.formatMessage(messages.transaction_sent_body), }) } } - - // fetch updated channels - dispatch(fetchChannels()) } // ------------------------------------ @@ -277,9 +272,6 @@ const ACTION_HANDLERS = { state.transactionLoading = false state.transactions = uniqBy(state.transactions.concat(transactions), 'txHash') }, - [ADD_TRANSACTION]: (state, { transaction }) => { - state.transactions.unshift(transaction) - }, [TRANSACTION_SUCCESSFUL]: (state, { internalId }) => { const txIndex = state.transactionsSending.findIndex(item => item.internalId === internalId) if (txIndex >= 0) { diff --git a/test/unit/reducers/__snapshots__/transaction.spec.js.snap b/test/unit/reducers/__snapshots__/transaction.spec.js.snap index ef7578a3069..ef14ba403f2 100644 --- a/test/unit/reducers/__snapshots__/transaction.spec.js.snap +++ b/test/unit/reducers/__snapshots__/transaction.spec.js.snap @@ -1,22 +1,5 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`reducers transactionReducer should handle ADD_TRANSACTION 1`] = ` -Snapshot Diff: -- First value -+ Second value - - Object { - "transactionLoading": false, -- "transactions": Array [], -+ "transactions": Array [ -+ Object { -+ "some": "data", -+ }, -+ ], - "transactionsSending": Array [], - } -`; - exports[`reducers transactionReducer should handle GET_TRANSACTIONS 1`] = ` Snapshot Diff: - First value diff --git a/test/unit/reducers/transaction.spec.js b/test/unit/reducers/transaction.spec.js index 3a4eb70e02a..ede18515431 100644 --- a/test/unit/reducers/transaction.spec.js +++ b/test/unit/reducers/transaction.spec.js @@ -6,7 +6,6 @@ import reducer, { TRANSACTION_SUCCESSFUL, TRANSACTION_FAILED, TRANSACTION_COMPLETE, - ADD_TRANSACTION, } from 'reducers/transaction' describe('reducers', () => { @@ -59,13 +58,5 @@ describe('reducers', () => { } expect(snapshotDiff(reducer(undefined, {}), reducer(undefined, action))).toMatchSnapshot() }) - - it('should handle ADD_TRANSACTION', () => { - const action = { - type: ADD_TRANSACTION, - transaction: { some: 'data' }, - } - expect(snapshotDiff(reducer(undefined, {}), reducer(undefined, action))).toMatchSnapshot() - }) }) })