Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
fix(wallet): only generate new address after use
Browse files Browse the repository at this point in the history
  • Loading branch information
mrfelton committed May 11, 2020
1 parent 4a10e8e commit 4fd938d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 44 deletions.
28 changes: 10 additions & 18 deletions renderer/reducers/transaction.js
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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
// ------------------------------------
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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({
Expand All @@ -138,7 +138,7 @@ export const receiveTransactions = (transactions, updateOnly = false) => (dispat
}
})

// fetch new balance
// Fetch updated balance.
dispatch(fetchBalance())
}

Expand Down Expand Up @@ -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())
}

// ------------------------------------
Expand All @@ -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) {
Expand Down
17 changes: 0 additions & 17 deletions test/unit/reducers/__snapshots__/transaction.spec.js.snap
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 0 additions & 9 deletions test/unit/reducers/transaction.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import reducer, {
TRANSACTION_SUCCESSFUL,
TRANSACTION_FAILED,
TRANSACTION_COMPLETE,
ADD_TRANSACTION,
} from 'reducers/transaction'

describe('reducers', () => {
Expand Down Expand Up @@ -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()
})
})
})

0 comments on commit 4fd938d

Please sign in to comment.