2016-04-14 00:28:44 +02:00
|
|
|
const extend = require('xtend')
|
|
|
|
const actions = require('../actions')
|
|
|
|
|
|
|
|
module.exports = reduceMetamask
|
|
|
|
|
2016-06-21 22:18:32 +02:00
|
|
|
function reduceMetamask (state, action) {
|
2016-06-21 22:56:04 +02:00
|
|
|
let newState
|
|
|
|
|
2016-04-14 00:28:44 +02:00
|
|
|
// clone + defaults
|
|
|
|
var metamaskState = extend({
|
|
|
|
isInitialized: false,
|
|
|
|
isUnlocked: false,
|
|
|
|
rpcTarget: 'https://rawtestrpc.metamask.io/',
|
|
|
|
identities: {},
|
2017-01-28 01:11:59 +01:00
|
|
|
unapprovedTxs: {},
|
2016-12-07 23:34:15 +01:00
|
|
|
noActiveNotices: true,
|
|
|
|
lastUnreadNotice: undefined,
|
2017-03-08 01:01:51 +01:00
|
|
|
frequentRpcList: [],
|
2017-03-09 22:58:42 +01:00
|
|
|
addressBook: [],
|
2016-04-14 00:28:44 +02:00
|
|
|
}, state.metamask)
|
|
|
|
|
|
|
|
switch (action.type) {
|
|
|
|
|
2016-06-21 22:18:32 +02:00
|
|
|
case actions.SHOW_ACCOUNTS_PAGE:
|
2016-06-21 22:56:04 +02:00
|
|
|
newState = extend(metamaskState)
|
|
|
|
delete newState.seedWords
|
|
|
|
return newState
|
2016-06-21 22:18:32 +02:00
|
|
|
|
2016-12-07 23:34:15 +01:00
|
|
|
case actions.SHOW_NOTICE:
|
|
|
|
return extend(metamaskState, {
|
|
|
|
noActiveNotices: false,
|
|
|
|
lastUnreadNotice: action.value,
|
|
|
|
})
|
|
|
|
|
|
|
|
case actions.CLEAR_NOTICES:
|
|
|
|
return extend(metamaskState, {
|
|
|
|
noActiveNotices: true,
|
|
|
|
})
|
|
|
|
|
2016-06-21 22:18:32 +02:00
|
|
|
case actions.UPDATE_METAMASK_STATE:
|
|
|
|
return extend(metamaskState, action.value)
|
|
|
|
|
|
|
|
case actions.UNLOCK_METAMASK:
|
|
|
|
return extend(metamaskState, {
|
|
|
|
isUnlocked: true,
|
|
|
|
isInitialized: true,
|
2017-01-31 00:08:31 +01:00
|
|
|
selectedAddress: action.value,
|
2016-06-21 22:18:32 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
case actions.LOCK_METAMASK:
|
|
|
|
return extend(metamaskState, {
|
|
|
|
isUnlocked: false,
|
|
|
|
})
|
|
|
|
|
2017-03-07 19:25:50 +01:00
|
|
|
case actions.SET_RPC_LIST:
|
|
|
|
return extend(metamaskState, {
|
|
|
|
frequentRpcList: action.value,
|
|
|
|
})
|
|
|
|
|
2016-06-21 22:18:32 +02:00
|
|
|
case actions.SET_RPC_TARGET:
|
|
|
|
return extend(metamaskState, {
|
|
|
|
provider: {
|
|
|
|
type: 'rpc',
|
2017-03-07 19:25:50 +01:00
|
|
|
rpcTarget: action.value,
|
2016-06-21 22:18:32 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
case actions.SET_PROVIDER_TYPE:
|
|
|
|
return extend(metamaskState, {
|
|
|
|
provider: {
|
|
|
|
type: action.value,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
case actions.COMPLETED_TX:
|
|
|
|
var stringId = String(action.id)
|
2016-06-21 22:56:04 +02:00
|
|
|
newState = extend(metamaskState, {
|
2017-01-28 01:11:59 +01:00
|
|
|
unapprovedTxs: {},
|
|
|
|
unapprovedMsgs: {},
|
2016-06-21 22:18:32 +02:00
|
|
|
})
|
2017-01-28 01:11:59 +01:00
|
|
|
for (const id in metamaskState.unapprovedTxs) {
|
2016-06-21 22:18:32 +02:00
|
|
|
if (id !== stringId) {
|
2017-01-28 01:11:59 +01:00
|
|
|
newState.unapprovedTxs[id] = metamaskState.unapprovedTxs[id]
|
2016-06-21 22:18:32 +02:00
|
|
|
}
|
2016-04-14 00:28:44 +02:00
|
|
|
}
|
2017-01-28 01:11:59 +01:00
|
|
|
for (const id in metamaskState.unapprovedMsgs) {
|
2016-06-21 22:18:32 +02:00
|
|
|
if (id !== stringId) {
|
2017-01-28 01:11:59 +01:00
|
|
|
newState.unapprovedMsgs[id] = metamaskState.unapprovedMsgs[id]
|
2016-06-21 22:18:32 +02:00
|
|
|
}
|
2016-05-03 23:32:22 +02:00
|
|
|
}
|
2016-06-21 22:18:32 +02:00
|
|
|
return newState
|
|
|
|
|
|
|
|
case actions.SHOW_NEW_VAULT_SEED:
|
|
|
|
return extend(metamaskState, {
|
|
|
|
isUnlocked: true,
|
|
|
|
isInitialized: false,
|
2017-03-28 17:23:25 +02:00
|
|
|
seedWords: action.value,
|
2016-06-21 22:18:32 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
case actions.CLEAR_SEED_WORD_CACHE:
|
2016-06-21 22:56:04 +02:00
|
|
|
newState = extend(metamaskState, {
|
2016-06-21 22:18:32 +02:00
|
|
|
isUnlocked: true,
|
|
|
|
isInitialized: true,
|
2017-01-31 00:08:31 +01:00
|
|
|
selectedAddress: action.value,
|
2016-06-21 22:18:32 +02:00
|
|
|
})
|
|
|
|
delete newState.seedWords
|
|
|
|
return newState
|
|
|
|
|
|
|
|
case actions.SHOW_ACCOUNT_DETAIL:
|
2016-06-21 22:56:04 +02:00
|
|
|
newState = extend(metamaskState, {
|
2016-06-21 22:18:32 +02:00
|
|
|
isUnlocked: true,
|
|
|
|
isInitialized: true,
|
2017-01-31 00:08:31 +01:00
|
|
|
selectedAddress: action.value,
|
2016-06-21 22:18:32 +02:00
|
|
|
})
|
|
|
|
delete newState.seedWords
|
|
|
|
return newState
|
|
|
|
|
|
|
|
case actions.SAVE_ACCOUNT_LABEL:
|
|
|
|
const account = action.value.account
|
|
|
|
const name = action.value.label
|
|
|
|
var id = {}
|
|
|
|
id[account] = extend(metamaskState.identities[account], { name })
|
|
|
|
var identities = extend(metamaskState.identities, id)
|
|
|
|
return extend(metamaskState, { identities })
|
|
|
|
|
2016-07-21 19:15:34 +02:00
|
|
|
case actions.SET_CURRENT_FIAT:
|
2016-07-22 19:15:39 +02:00
|
|
|
return extend(metamaskState, {
|
2017-03-15 01:06:16 +01:00
|
|
|
currentCurrency: action.value.currentCurrency,
|
2016-07-21 19:15:34 +02:00
|
|
|
conversionRate: action.value.conversionRate,
|
2016-07-21 23:08:26 +02:00
|
|
|
conversionDate: action.value.conversionDate,
|
2016-07-21 19:15:34 +02:00
|
|
|
})
|
|
|
|
|
2016-06-21 22:18:32 +02:00
|
|
|
default:
|
|
|
|
return metamaskState
|
2016-04-14 00:28:44 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|