Skip to content

Commit

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

Auditors:

Test Plan:
  • Loading branch information
NejcZdovc committed Oct 23, 2017
1 parent 153f110 commit 3e53054
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 20 deletions.
40 changes: 29 additions & 11 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 @@ -155,17 +156,12 @@ const notifications = {
}, notifications.pollingInterval, state)
},
onLaunch: (state) => {
if (!getSetting(settings.PAYMENTS_ENABLED)) {
const enabled = getSetting(settings.PAYMENTS_ENABLED)
if (!enabled) {
return state
}

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

if (hasFunds(state)) {
// Don't bother processing the rest, which are only notifications.
Expand All @@ -180,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 @@ -1306,8 +1304,12 @@ const initSynopsis = (state) => {
}

const enable = (state, paymentsEnabled) => {
if (paymentsEnabled && !getSetting(settings.PAYMENTS_NOTIFICATION_TRY_PAYMENTS_DISMISSED)) {
appActions.changeSetting(settings.PAYMENTS_NOTIFICATION_TRY_PAYMENTS_DISMISSED, true)
if (paymentsEnabled) {
state = checkBtcBatMigrated(state, paymentsEnabled)

if (!getSetting(settings.PAYMENTS_NOTIFICATION_TRY_PAYMENTS_DISMISSED)) {
appActions.changeSetting(settings.PAYMENTS_NOTIFICATION_TRY_PAYMENTS_DISMISSED, true)
}
}

if (synopsis) {
Expand Down Expand Up @@ -2262,6 +2264,22 @@ const yoDawg = (stateState) => {
return stateState
}

const checkBtcBatMigrated = (state, status) => {
if (!status) {
return state
}

// One time conversion of wallet
const isNewInstall = migrationState.isNewInstall(state)
const hasUpgradedWallet = migrationState.hasUpgradedWallet(state)
if (!isNewInstall && !hasUpgradedWallet) {
state = migrationState.setTransitionStatus(state, true)
module.exports.transitionWalletToBat()
}

return state
}

let newClient = null
const transitionWalletToBat = () => {
let newPaymentId, result
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 3e53054

Please sign in to comment.