-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathindex.ts
137 lines (127 loc) · 4.25 KB
/
index.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
import { PayloadAction } from '@reduxjs/toolkit'
import { createSlice } from 'utils/@reduxjs/toolkit'
import {
FinishV0MigrationPayload,
PersistedRootState,
PersistState,
SetUnlockedRootStatePayload,
StringifiedKeyWithSalt,
UpdatePasswordPayload,
} from './types'
import { RootState } from 'types'
import { runtimeIs } from 'config'
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { createPersistedRootReducer } from 'store/reducers'
export const STORAGE_FIELD = 'oasis_wallet_persist_v1'
/** Syncing tabs is only needed in web app, not in extension. */
export const needsSyncingTabs = runtimeIs === 'webapp'
// Simulate with `delete window.BroadcastChannel`
export const isSyncingTabsSupported = typeof BroadcastChannel === 'function'
export function getInitialState(): PersistState {
return {
hasPersistedProfiles: !!window.localStorage.getItem(STORAGE_FIELD),
hasV0StorageToMigrate: false,
// Disable persistence if tabs would override each other.
isPersistenceUnsupported: needsSyncingTabs && !isSyncingTabsSupported,
loading: false,
stringifiedEncryptionKey: localStorage.getItem(STORAGE_FIELD) ? undefined : 'skipped',
enteredWrongPassword: false,
}
}
export const persistSlice = createSlice({
name: 'persist',
initialState: () => getInitialState(),
reducers: {
setWrongPassword(state) {
state.enteredWrongPassword = true
state.loading = false
},
resetWrongPassword(state) {
state.enteredWrongPassword = false
},
setUnlockedRootState(state, action: PayloadAction<SetUnlockedRootStatePayload>) {
/**
* Handled in {@link createPersistedRootReducer} and {@link receivePersistedRootState}.
* Sets `state.loading = false` and `state.stringifiedEncryptionKey`.
*/
return
},
resetRootState(state) {
/**
* Handled in {@link createPersistedRootReducer}.
* Sets `state.loading = false`.
*/
return
},
skipUnlocking(state) {
state.stringifiedEncryptionKey = 'skipped'
},
setHasV0StorageToMigrate(state, action: PayloadAction<boolean>) {
state.hasV0StorageToMigrate = action.payload
},
// Handled in saga
// ---------------
/**
* Set a password to encrypt state with and begin storing it to localStorage.
*/
setPasswordAsync(state, action: PayloadAction<{ password: string }>) {
state.loading = true
},
/**
* Load encrypted state from localStorage and decrypt, or set {@link PersistState.enteredWrongPassword}.
*/
unlockAsync(state, action: PayloadAction<{ password: string }>) {
state.loading = true
},
/**
* Remove everything from state, especially {@link PersistState.stringifiedEncryptionKey}.
* UnlockGate should stop any components relying on missing state.
*/
lockAsync(state) {
state.loading = true
},
/**
* Remove encrypted state from localStorage and reload.
*/
deleteProfileAsync(state) {
state.loading = true
},
updatePasswordAsync(state, action: PayloadAction<UpdatePasswordPayload>) {
return
},
finishV0Migration(state, action: PayloadAction<FinishV0MigrationPayload>) {
return
},
},
})
export const persistActions = persistSlice.actions
/**
* When persisted state is unlocked use these state slices.
*/
export function receivePersistedRootState(
prevState: RootState,
persistedRootState: PersistedRootState,
stringifiedEncryptionKey: StringifiedKeyWithSalt,
): RootState {
return {
// Explicitly list every field instead of `...prevState` to force developers
// to consider if their new slice should be persisted.
account: prevState.account,
createWallet: prevState.createWallet,
fatalError: prevState.fatalError,
fiatOnramp: prevState.fiatOnramp,
importAccounts: prevState.importAccounts,
paraTimes: prevState.paraTimes,
staking: prevState.staking,
transaction: prevState.transaction,
contacts: persistedRootState.contacts,
evmAccounts: persistedRootState.evmAccounts,
theme: persistedRootState.theme,
wallet: persistedRootState.wallet,
network: persistedRootState.network,
persist: {
...getInitialState(),
stringifiedEncryptionKey: stringifiedEncryptionKey,
},
}
}