Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
Fixes transition for disabled wallets
Browse files Browse the repository at this point in the history
Resolves #11611

Auditors:

Test Plan:
  • Loading branch information
NejcZdovc committed Oct 23, 2017
1 parent c18ba1d commit 8bedda4
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 15 deletions.
17 changes: 11 additions & 6 deletions app/browser/api/ledger.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const appActions = require('../../../js/actions/appActions')
// State
const ledgerState = require('../../common/state/ledgerState')
const pageDataState = require('../../common/state/pageDataState')
const migrationState = require('../../common/state/migrationState')

// Constants
const settings = require('../../../js/constants/settings')
Expand Down Expand Up @@ -160,7 +161,7 @@ const notifications = {
return state
}

checkBtcBatMigrated(state, enabled)
state = checkBtcBatMigrated(state, enabled)

if (hasFunds(state)) {
// Don't bother processing the rest, which are only notifications.
Expand All @@ -175,7 +176,9 @@ const notifications = {
// - wallet has been transitioned
// - notification has not already been shown yet
// (see https://github.com/brave/browser-laptop/issues/11021)
const hasBeenNotified = state.getIn(['migrations', 'batMercuryTimestamp']) !== state.getIn(['migrations', 'btc2BatNotifiedTimestamp'])
const isNewInstall = migrationState.isNewInstall(state)
const hasUpgradedWallet = migrationState.hasUpgradedWallet(state)
const hasBeenNotified = migrationState.hasBeenNotified(state)
if (!isNewInstall && hasUpgradedWallet && !hasBeenNotified) {
notifications.showBraveWalletUpdated()
}
Expand Down Expand Up @@ -1302,7 +1305,7 @@ const initSynopsis = (state) => {

const enable = (state, paymentsEnabled) => {
if (paymentsEnabled) {
checkBtcBatMigrated(state, paymentsEnabled)
state = checkBtcBatMigrated(state, paymentsEnabled)

if (!getSetting(settings.PAYMENTS_NOTIFICATION_TRY_PAYMENTS_DISMISSED)) {
appActions.changeSetting(settings.PAYMENTS_NOTIFICATION_TRY_PAYMENTS_DISMISSED, true)
Expand Down Expand Up @@ -2267,12 +2270,14 @@ const checkBtcBatMigrated = (state, status) => {
}

// One time conversion of wallet
const isNewInstall = state.get('firstRunTimestamp') === state.getIn(['migrations', 'batMercuryTimestamp'])
const hasUpgradedWallet = state.getIn(['migrations', 'batMercuryTimestamp']) !== state.getIn(['migrations', 'btc2BatTimestamp'])
const isNewInstall = migrationState.isNewInstall(state)
const hasUpgradedWallet = migrationState.hasUpgradedWallet(state)
if (!isNewInstall && !hasUpgradedWallet) {
state = state.setIn(['migrations', 'btc2BatTransitionPending'], true)
state = migrationState.setTransitionStatus(state, true)
module.exports.transitionWalletToBat()
}

return state
}

let newClient = null
Expand Down
13 changes: 7 additions & 6 deletions app/browser/reducers/ledgerReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const settings = require('../../../js/constants/settings')

// State
const ledgerState = require('../../common/state/ledgerState')
const migrationState = require('../../common/state/migrationState')

// Utils
const ledgerApi = require('../../browser/api/ledger')
Expand Down Expand Up @@ -239,8 +240,8 @@ const ledgerReducer = (state, action, immutableAction) => {
case appConstants.APP_ON_LEDGER_WALLET_CREATE:
{
ledgerApi.boot()
state = state.setIn(['migrations', 'btc2BatTimestamp'], new Date().getTime())
state = state.setIn(['migrations', 'btc2BatTransitionPending'], false)
state = migrationState.setConversionTimestamp(state, new Date().getTime())
state = migrationState.setTransitionStatus(state, false)
break
}
case appConstants.APP_ON_BOOT_STATE_FILE:
Expand Down Expand Up @@ -316,18 +317,18 @@ const ledgerReducer = (state, action, immutableAction) => {
}
case appConstants.APP_ON_BTC_TO_BAT_NOTIFIED:
{
state = state.setIn(['migrations', 'btc2BatNotifiedTimestamp'], new Date().getTime())
state = migrationState.setNotifiedTimestamp(state, new Date().getTime())
break
}
case appConstants.APP_ON_BTC_TO_BAT_BEGIN_TRANSITION:
{
state = state.setIn(['migrations', 'btc2BatTransitionPending'], true)
state = migrationState.setTransitionStatus(state, true)
break
}
case appConstants.APP_ON_BTC_TO_BAT_TRANSITIONED:
{
state = state.setIn(['migrations', 'btc2BatTimestamp'], new Date().getTime())
state = state.setIn(['migrations', 'btc2BatTransitionPending'], false)
state = migrationState.setConversionTimestamp(state, new Date().getTime())
state = migrationState.setTransitionStatus(state, false)
break
}
case appConstants.APP_ON_LEDGER_QR_GENERATED:
Expand Down
60 changes: 60 additions & 0 deletions app/common/state/migrationState.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

const assert = require('assert')

const {makeImmutable, isMap} = require('../../common/state/immutableUtil')

const validateState = function (state) {
state = makeImmutable(state)
assert.ok(isMap(state), 'state must be an Immutable.Map')
assert.ok(isMap(state.get('migrations')), 'state must contain an Immutable.Map of migrations')
return state
}

const migrationState = {
setTransitionStatus: (state, value) => {
state = validateState(state)
if (value == null) {
return state
}

return state.setIn(['migrations', 'btc2BatTransitionPending'], value)
},

setConversionTimestamp: (state, value) => {
state = validateState(state)
if (value == null) {
return state
}

return state.setIn(['migrations', 'btc2BatTimestamp'], value)
},

setNotifiedTimestamp: (state, value) => {
state = validateState(state)
if (value == null) {
return state
}

return state.setIn(['migrations', 'btc2BatNotifiedTimestamp'], value)
},

isNewInstall: (state) => {
state = validateState(state)
return state.get('firstRunTimestamp') === state.getIn(['migrations', 'batMercuryTimestamp'])
},

hasUpgradedWallet: (state) => {
state = validateState(state)
return state.getIn(['migrations', 'batMercuryTimestamp']) !== state.getIn(['migrations', 'btc2BatTimestamp'])
},

hasBeenNotified: (state) => {
state = validateState(state)
return state.getIn(['migrations', 'batMercuryTimestamp']) !== state.getIn(['migrations', 'btc2BatNotifiedTimestamp'])
}
}

module.exports = migrationState
4 changes: 2 additions & 2 deletions test/unit/app/browser/api/ledgerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ const settings = require('../../../../../js/constants/settings')
const appActions = require('../../../../../js/actions/appActions')

const defaultAppState = Immutable.fromJS({
ledger: {
}
ledger: {},
migrations: {}
})

describe('ledger api unit tests', function () {
Expand Down
3 changes: 2 additions & 1 deletion test/unit/app/browser/reducers/ledgerReducerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ describe('ledgerReducer unit tests', function () {
ledgerReducer = require('../../../../../app/browser/reducers/ledgerReducer')

appState = Immutable.fromJS({
ledger: {}
ledger: {},
migrations: {}
})
})

Expand Down

0 comments on commit 8bedda4

Please sign in to comment.