2016-04-14 00:28:44 +02:00
|
|
|
const extend = require('xtend')
|
|
|
|
const actions = require('../actions')
|
2017-08-18 13:11:26 +02:00
|
|
|
const MetamascaraPlatform = require('../../../app/scripts/platforms/window')
|
2018-04-17 08:03:47 +02:00
|
|
|
const { getEnvironmentType } = require('../../../app/scripts/lib/util')
|
|
|
|
const { ENVIRONMENT_TYPE_POPUP } = require('../../../app/scripts/lib/enums')
|
2018-04-12 23:17:36 +02:00
|
|
|
const { OLD_UI_NETWORK_TYPE } = require('../../../app/scripts/controllers/network/enums')
|
2016-04-14 00:28:44 +02:00
|
|
|
|
|
|
|
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,
|
2017-10-16 07:28:25 +02:00
|
|
|
isAccountMenuOpen: false,
|
2017-08-18 13:11:26 +02:00
|
|
|
isMascara: window.platform instanceof MetamascaraPlatform,
|
2018-04-17 08:03:47 +02:00
|
|
|
isPopup: getEnvironmentType(window.location.href) === ENVIRONMENT_TYPE_POPUP,
|
2016-04-14 00:28:44 +02:00
|
|
|
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: [],
|
2017-09-06 12:17:49 +02:00
|
|
|
selectedTokenAddress: null,
|
2018-04-16 05:37:42 +02:00
|
|
|
contractExchangeRates: {},
|
2017-09-13 10:25:39 +02:00
|
|
|
tokenExchangeRates: {},
|
2017-10-03 19:25:52 +02:00
|
|
|
tokens: [],
|
2018-05-20 08:04:19 +02:00
|
|
|
pendingTokens: {},
|
2017-10-12 18:42:14 +02:00
|
|
|
send: {
|
|
|
|
gasLimit: null,
|
|
|
|
gasPrice: null,
|
2017-10-17 19:22:23 +02:00
|
|
|
gasTotal: null,
|
2017-10-30 18:48:50 +01:00
|
|
|
tokenBalance: null,
|
2017-10-17 19:22:23 +02:00
|
|
|
from: '',
|
|
|
|
to: '',
|
|
|
|
amount: '0x0',
|
|
|
|
memo: '',
|
2017-10-17 22:13:20 +02:00
|
|
|
errors: {},
|
2017-11-16 19:14:25 +01:00
|
|
|
maxModeOn: false,
|
2017-11-08 17:44:48 +01:00
|
|
|
editingTransactionId: null,
|
2018-03-09 04:49:26 +01:00
|
|
|
forceGasMin: null,
|
2018-03-26 15:30:54 +02:00
|
|
|
toNickname: '',
|
2017-10-12 18:42:14 +02:00
|
|
|
},
|
2017-09-25 23:51:49 +02:00
|
|
|
coinOptions: {},
|
2017-11-24 02:33:44 +01:00
|
|
|
useBlockie: false,
|
2017-11-14 17:04:55 +01:00
|
|
|
featureFlags: {},
|
2017-12-22 19:43:02 +01:00
|
|
|
networkEndpointType: OLD_UI_NETWORK_TYPE,
|
2018-03-03 03:14:05 +01:00
|
|
|
isRevealingSeedWords: false,
|
2018-03-08 18:31:21 +01:00
|
|
|
welcomeScreenSeen: false,
|
2018-03-23 17:59:59 +01:00
|
|
|
currentLocale: '',
|
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:
|
2018-03-03 03:14:05 +01:00
|
|
|
newState = extend(metamaskState, {
|
|
|
|
isRevealingSeedWords: false,
|
|
|
|
})
|
2016-06-21 22:56:04 +02:00
|
|
|
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
|
|
|
|
|
2017-11-08 17:44:48 +01:00
|
|
|
case actions.EDIT_TX:
|
|
|
|
return extend(metamaskState, {
|
|
|
|
send: {
|
|
|
|
...metamaskState.send,
|
|
|
|
editingTransactionId: action.value,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2018-03-03 03:14:05 +01:00
|
|
|
|
2016-06-21 22:18:32 +02:00
|
|
|
case actions.SHOW_NEW_VAULT_SEED:
|
|
|
|
return extend(metamaskState, {
|
2018-03-03 03:14:05 +01:00
|
|
|
isRevealingSeedWords: true,
|
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
|
|
|
|
|
2017-09-06 12:17:49 +02:00
|
|
|
case actions.SET_SELECTED_TOKEN:
|
|
|
|
return extend(metamaskState, {
|
|
|
|
selectedTokenAddress: action.value,
|
|
|
|
})
|
|
|
|
|
2018-04-19 05:33:51 +02:00
|
|
|
case actions.SET_ACCOUNT_LABEL:
|
2016-06-21 22:18:32 +02:00
|
|
|
const account = action.value.account
|
|
|
|
const name = action.value.label
|
2017-11-08 20:18:27 +01:00
|
|
|
const id = {}
|
2016-06-21 22:18:32 +02:00
|
|
|
id[account] = extend(metamaskState.identities[account], { name })
|
2017-11-08 20:18:27 +01:00
|
|
|
const identities = extend(metamaskState.identities, id)
|
2016-06-21 22:18:32 +02:00
|
|
|
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
|
|
|
})
|
|
|
|
|
2017-10-03 19:25:52 +02:00
|
|
|
case actions.UPDATE_TOKENS:
|
|
|
|
return extend(metamaskState, {
|
|
|
|
tokens: action.newTokens,
|
|
|
|
})
|
|
|
|
|
2017-10-17 19:22:23 +02:00
|
|
|
// metamask.send
|
2017-10-12 18:42:14 +02:00
|
|
|
case actions.UPDATE_GAS_LIMIT:
|
|
|
|
return extend(metamaskState, {
|
|
|
|
send: {
|
|
|
|
...metamaskState.send,
|
|
|
|
gasLimit: action.value,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
case actions.UPDATE_GAS_PRICE:
|
|
|
|
return extend(metamaskState, {
|
|
|
|
send: {
|
|
|
|
...metamaskState.send,
|
|
|
|
gasPrice: action.value,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2017-10-16 07:28:25 +02:00
|
|
|
case actions.TOGGLE_ACCOUNT_MENU:
|
|
|
|
return extend(metamaskState, {
|
|
|
|
isAccountMenuOpen: !metamaskState.isAccountMenuOpen,
|
|
|
|
})
|
|
|
|
|
2017-10-17 19:22:23 +02:00
|
|
|
case actions.UPDATE_GAS_TOTAL:
|
|
|
|
return extend(metamaskState, {
|
|
|
|
send: {
|
|
|
|
...metamaskState.send,
|
|
|
|
gasTotal: action.value,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2017-10-30 18:48:50 +01:00
|
|
|
case actions.UPDATE_SEND_TOKEN_BALANCE:
|
|
|
|
return extend(metamaskState, {
|
|
|
|
send: {
|
|
|
|
...metamaskState.send,
|
|
|
|
tokenBalance: action.value,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2017-10-17 19:22:23 +02:00
|
|
|
case actions.UPDATE_SEND_FROM:
|
|
|
|
return extend(metamaskState, {
|
|
|
|
send: {
|
|
|
|
...metamaskState.send,
|
|
|
|
from: action.value,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
case actions.UPDATE_SEND_TO:
|
|
|
|
return extend(metamaskState, {
|
|
|
|
send: {
|
|
|
|
...metamaskState.send,
|
2018-03-26 15:30:54 +02:00
|
|
|
to: action.value.to,
|
|
|
|
toNickname: action.value.nickname,
|
2017-10-17 19:22:23 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
case actions.UPDATE_SEND_AMOUNT:
|
|
|
|
return extend(metamaskState, {
|
|
|
|
send: {
|
|
|
|
...metamaskState.send,
|
|
|
|
amount: action.value,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
case actions.UPDATE_SEND_MEMO:
|
|
|
|
return extend(metamaskState, {
|
|
|
|
send: {
|
|
|
|
...metamaskState.send,
|
|
|
|
memo: action.value,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2017-10-17 22:13:20 +02:00
|
|
|
case actions.UPDATE_SEND_ERRORS:
|
|
|
|
return extend(metamaskState, {
|
|
|
|
send: {
|
|
|
|
...metamaskState.send,
|
|
|
|
errors: {
|
|
|
|
...metamaskState.send.errors,
|
|
|
|
...action.value,
|
2017-11-02 13:15:59 +01:00
|
|
|
},
|
2017-10-17 22:13:20 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2017-11-16 19:14:25 +01:00
|
|
|
case actions.UPDATE_MAX_MODE:
|
|
|
|
return extend(metamaskState, {
|
|
|
|
send: {
|
|
|
|
...metamaskState.send,
|
|
|
|
maxModeOn: action.value,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2017-11-10 04:49:16 +01:00
|
|
|
case actions.UPDATE_SEND:
|
|
|
|
return extend(metamaskState, {
|
|
|
|
send: {
|
|
|
|
...metamaskState.send,
|
|
|
|
...action.value,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2017-10-21 04:05:22 +02:00
|
|
|
case actions.CLEAR_SEND:
|
|
|
|
return extend(metamaskState, {
|
|
|
|
send: {
|
|
|
|
gasLimit: null,
|
|
|
|
gasPrice: null,
|
|
|
|
gasTotal: null,
|
2017-11-10 04:49:16 +01:00
|
|
|
tokenBalance: null,
|
2017-10-21 04:05:22 +02:00
|
|
|
from: '',
|
|
|
|
to: '',
|
|
|
|
amount: '0x0',
|
|
|
|
memo: '',
|
|
|
|
errors: {},
|
2017-11-10 04:49:16 +01:00
|
|
|
editingTransactionId: null,
|
2018-03-09 04:49:26 +01:00
|
|
|
forceGasMin: null,
|
2017-10-21 04:05:22 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2017-11-08 17:44:48 +01:00
|
|
|
case actions.UPDATE_TRANSACTION_PARAMS:
|
2017-11-08 20:18:27 +01:00
|
|
|
const { id: txId, value } = action
|
2017-11-08 17:44:48 +01:00
|
|
|
let { selectedAddressTxList } = metamaskState
|
|
|
|
selectedAddressTxList = selectedAddressTxList.map(tx => {
|
2017-11-08 20:18:27 +01:00
|
|
|
if (tx.id === txId) {
|
2017-11-08 17:44:48 +01:00
|
|
|
tx.txParams = value
|
|
|
|
}
|
|
|
|
return tx
|
|
|
|
})
|
|
|
|
|
|
|
|
return extend(metamaskState, {
|
|
|
|
selectedAddressTxList,
|
|
|
|
})
|
|
|
|
|
2017-09-25 23:51:49 +02:00
|
|
|
case actions.PAIR_UPDATE:
|
|
|
|
const { value: { marketinfo: pairMarketInfo } } = action
|
|
|
|
return extend(metamaskState, {
|
|
|
|
tokenExchangeRates: {
|
|
|
|
...metamaskState.tokenExchangeRates,
|
|
|
|
[pairMarketInfo.pair]: pairMarketInfo,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
case actions.SHAPESHIFT_SUBVIEW:
|
2017-10-25 01:50:25 +02:00
|
|
|
const { value: { marketinfo: ssMarketInfo, coinOptions } } = action
|
2017-09-25 23:51:49 +02:00
|
|
|
return extend(metamaskState, {
|
|
|
|
tokenExchangeRates: {
|
|
|
|
...metamaskState.tokenExchangeRates,
|
2017-11-15 18:39:03 +01:00
|
|
|
[ssMarketInfo.pair]: ssMarketInfo,
|
2017-09-25 23:51:49 +02:00
|
|
|
},
|
|
|
|
coinOptions,
|
|
|
|
})
|
|
|
|
|
2017-11-24 18:35:17 +01:00
|
|
|
case actions.SET_USE_BLOCKIE:
|
|
|
|
return extend(metamaskState, {
|
2017-11-25 23:47:34 +01:00
|
|
|
useBlockie: action.value,
|
2017-11-24 18:35:17 +01:00
|
|
|
})
|
2017-11-24 02:33:44 +01:00
|
|
|
|
2017-11-14 17:04:55 +01:00
|
|
|
case actions.UPDATE_FEATURE_FLAGS:
|
|
|
|
return extend(metamaskState, {
|
|
|
|
featureFlags: action.value,
|
|
|
|
})
|
|
|
|
|
2017-12-22 19:43:02 +01:00
|
|
|
case actions.UPDATE_NETWORK_ENDPOINT_TYPE:
|
|
|
|
return extend(metamaskState, {
|
|
|
|
networkEndpointType: action.value,
|
|
|
|
})
|
|
|
|
|
2018-03-08 18:31:21 +01:00
|
|
|
case actions.CLOSE_WELCOME_SCREEN:
|
|
|
|
return extend(metamaskState, {
|
|
|
|
welcomeScreenSeen: true,
|
|
|
|
})
|
|
|
|
|
2018-04-14 01:14:23 +02:00
|
|
|
case actions.SET_CURRENT_LOCALE:
|
2018-03-16 01:29:45 +01:00
|
|
|
return extend(metamaskState, {
|
|
|
|
currentLocale: action.value,
|
|
|
|
})
|
|
|
|
|
2018-05-20 08:04:19 +02:00
|
|
|
case actions.SET_PENDING_TOKENS:
|
|
|
|
return extend(metamaskState, {
|
|
|
|
pendingTokens: { ...action.payload },
|
|
|
|
})
|
|
|
|
|
|
|
|
case actions.CLEAR_PENDING_TOKENS: {
|
|
|
|
return extend(metamaskState, {
|
|
|
|
pendingTokens: {},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-06-21 22:18:32 +02:00
|
|
|
default:
|
|
|
|
return metamaskState
|
2016-04-14 00:28:44 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|