aboutsummaryrefslogtreecommitdiffstats
path: root/ui/app/reducers/metamask.js
blob: 7af805f065c9c823452e2eadabdd8a3b7bf3b4db (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const extend = require('xtend')
const actions = require('../actions')

module.exports = reduceMetamask

function reduceMetamask (state, action) {
  let newState

  // clone + defaults
  var metamaskState = extend({
    isInitialized: false,
    isUnlocked: false,
    isEthConfirmed: false,
    currentDomain: 'example.com',
    rpcTarget: 'https://rawtestrpc.metamask.io/',
    identities: {},
    unconfTxs: {},
    currentFiat: 'USD',
    conversionRate: 'N/A',
    conversionDate: 'N/A',
  }, state.metamask)

  switch (action.type) {

    case actions.SHOW_ACCOUNTS_PAGE:
      newState = extend(metamaskState)
      delete newState.seedWords
      return newState

    case actions.UPDATE_METAMASK_STATE:
      return extend(metamaskState, action.value)

    case actions.AGREE_TO_DISCLAIMER:
      return extend(metamaskState, {
        isConfirmed: true,
      })

    case actions.AGREE_TO_ETH_WARNING:
      return extend(metamaskState, {
        isEthConfirmed: !metamaskState.isEthConfirmed,
      })

    case actions.UNLOCK_METAMASK:
      return extend(metamaskState, {
        isUnlocked: true,
        isInitialized: true,
        selectedAccount: action.value,
      })

    case actions.LOCK_METAMASK:
      return extend(metamaskState, {
        isUnlocked: false,
      })

    case actions.SET_RPC_TARGET:
      return extend(metamaskState, {
        provider: {
          type: 'rpc',
          rpcTarget: action.value,
        },
      })

    case actions.SET_PROVIDER_TYPE:
      return extend(metamaskState, {
        provider: {
          type: action.value,
        },
      })

    case actions.COMPLETED_TX:
      var stringId = String(action.id)
      newState = extend(metamaskState, {
        unconfTxs: {},
        unconfMsgs: {},
      })
      for (const id in metamaskState.unconfTxs) {
        if (id !== stringId) {
          newState.unconfTxs[id] = metamaskState.unconfTxs[id]
        }
      }
      for (const id in metamaskState.unconfMsgs) {
        if (id !== stringId) {
          newState.unconfMsgs[id] = metamaskState.unconfMsgs[id]
        }
      }
      return newState

    case actions.SHOW_NEW_VAULT_SEED:
      return extend(metamaskState, {
        isUnlocked: true,
        isInitialized: false,
      })

    case actions.CLEAR_SEED_WORD_CACHE:
      newState = extend(metamaskState, {
        isUnlocked: true,
        isInitialized: true,
        selectedAccount: action.value,
      })
      delete newState.seedWords
      return newState

    case actions.SHOW_ACCOUNT_DETAIL:
      newState = extend(metamaskState, {
        isUnlocked: true,
        isInitialized: true,
        selectedAccount: action.value,
        selectedAddress: action.value,
      })
      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 })

    case actions.SET_CURRENT_FIAT:
      return extend(metamaskState, {
        currentFiat: action.value.currentFiat,
        conversionRate: action.value.conversionRate,
        conversionDate: action.value.conversionDate,
      })

    default:
      return metamaskState

  }
}