-
Notifications
You must be signed in to change notification settings - Fork 927
/
Copy pathwallet_reducer.ts
198 lines (178 loc) · 5.1 KB
/
wallet_reducer.ts
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/* 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/. */
import { Reducer } from 'redux'
// Constant
import { types } from '../constants/rewards_types'
import { generateQR } from '../utils'
const createWallet = (state: Rewards.State) => {
state.walletCreated = true
state.enabledMain = true
state.enabledAds = true
state.enabledContribute = true
state.createdTimestamp = new Date().getTime()
chrome.send('brave_rewards.getReconcileStamp', [])
chrome.send('brave_rewards.getAddresses', [])
chrome.send('brave_rewards.getContributionAmount', [])
return state
}
const walletReducer: Reducer<Rewards.State | undefined> = (state: Rewards.State, action) => {
switch (action.type) {
case types.CREATE_WALLET:
state = { ...state }
state.walletCreateFailed = false
state.walletCreated = false
chrome.send('brave_rewards.createWalletRequested', [])
break
case types.WALLET_CREATED:
state = { ...state }
state = createWallet(state)
break
case types.WALLET_CREATE_FAILED:
state = { ...state }
state.walletCreateFailed = true
break
case types.GET_WALLET_PROPERTIES:
chrome.send('brave_rewards.getWalletProperties', [])
break
case types.ON_WALLET_PROPERTIES:
{
state = { ...state }
let ui = state.ui
// TODO NZ check why enum can't be used inside Rewards namespace
if (action.payload.properties.status === 1) {
ui.walletServerProblem = true
} else {
// TODO NZ don't just assign directly
state.walletInfo = action.payload.properties.wallet
ui.walletServerProblem = false
}
state = {
...state,
ui
}
break
}
case types.GET_WALLLET_PASSPHRASE:
chrome.send('brave_rewards.getWalletPassphrase', [])
break
case types.ON_WALLLET_PASSPHRASE:
const value = action.payload.pass
if (value && value.length > 0) {
state = { ...state }
state.recoveryKey = value
}
break
case types.RECOVER_WALLET:
if (!action.payload.key || action.payload.key.length === 0) {
let ui = state.ui
ui.walletRecoverySuccess = false
state = {
...state,
ui
}
break
}
chrome.send('brave_rewards.recoverWallet', [action.payload.key])
break
case types.ON_RECOVER_WALLET_DATA:
{
state = { ...state }
const result = action.payload.properties.result
const balance = action.payload.properties.balance
const grants = action.payload.properties.grants
let ui = state.ui
let walletInfo = state.walletInfo
// TODO NZ check why enum can't be used inside Rewards namespace
ui.walletRecoverySuccess = result === 0
if (result === 0) {
walletInfo.balance = balance
walletInfo.grants = grants || []
chrome.send('brave_rewards.getWalletPassphrase', [])
chrome.send('brave_rewards.getAddresses', [])
ui.emptyWallet = balance <= 0
ui.modalBackup = false
}
state = {
...state,
ui,
walletInfo
}
break
}
case types.ON_ADDRESSES:
{
if (!action.payload.addresses) {
break
}
state = { ...state }
state.addresses = {
BAT: {
address: action.payload.addresses.BAT,
qr: null
},
BTC: {
address: action.payload.addresses.BTC,
qr: null
},
ETH: {
address: action.payload.addresses.ETH,
qr: null
},
LTC: {
address: action.payload.addresses.LTC,
qr: null
}
}
generateQR(action.payload.addresses)
break
}
case types.ON_QR_GENERATED:
{
const type = action.payload.type
if (!type) {
break
}
state = { ...state }
const addresses = state.addresses
if (!addresses || !addresses[type] || !addresses[type].address) {
break
}
addresses[type].qr = action.payload.image
state = {
...state,
addresses
}
break
}
case types.ON_BALANCE_REPORTS:
{
state = { ...state }
state.reports = action.payload.reports
break
}
case types.CHECK_WALLET_EXISTENCE:
{
chrome.send('brave_rewards.checkWalletExistence')
break
}
case types.ON_WALLET_EXISTS:
{
if (!action.payload.exists || state.walletCreated) {
break
}
state = { ...state }
state = createWallet(state)
state.firstLoad = false
break
}
case types.ON_CONTRIBUTION_AMOUNT:
{
state = { ...state }
state.contributionMonthly = action.payload.amount
break
}
}
return state
}
export default walletReducer