-
Notifications
You must be signed in to change notification settings - Fork 6
/
setupRootStore.ts
225 lines (186 loc) · 7.92 KB
/
setupRootStore.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/**
* This file is where we do "rehydration" of your RootStore from AsyncStorage.
* This lets you persist your state between app launches.
*
* Navigation state persistence is handled in navigationUtilities.tsx.
*
* Note that Fast Refresh doesn't play well with this file, so if you edit this,
* do a full refresh of your app instead.
*
* @refresh reset
*/
import {
applySnapshot,
IDisposer,
onSnapshot,
} from 'mobx-state-tree'
import * as Sentry from '@sentry/react-native'
import RNExitApp from 'react-native-exit-app'
import type {RootStore} from '../RootStore'
import {KeyChain, MinibitsClient, MMKVStorage, NostrClient} from '../../services'
import {Database} from '../../services'
import { log } from '../../services/logService'
import { rootStoreModelVersion } from '../RootStore'
import AppError, { Err } from '../../utils/AppError'
import { MINIBITS_NIP05_DOMAIN } from '@env'
import { LogLevel } from '../../services/log/logTypes'
import { MintStatus } from '../Mint'
/**
* The key we'll be saving our state as within storage.
*/
const ROOT_STORAGE_KEY = 'minibits-root-storage'
/**
* Setup the root state.
*/
let _disposer: IDisposer
export async function setupRootStore(rootStore: RootStore) {
let restoredState: any
// let latestSnapshot: any
try {
// Give an option to encrypt storage as it might slow down app start on some Android devices
// User settings are mastered in sqlite so we can get the encryption setting before loading root store
const userSettings = Database.getUserSettings()
// random identificator of an app installation for bugs and crash reporting
if(userSettings.walletId) {
Sentry.setUser({ id: userSettings.walletId })
}
if (userSettings.isStorageEncrypted) {
try {
await MMKVStorage.initEncryption()
} catch (e: any) {
log.error('[setupRootStore]', 'Encryption init failed', {error: e})
if (e && typeof e === 'object') {
const errString = JSON.stringify(e)
const isBackPressed = errString.includes('code: 10')
const isCancellPressed = errString.includes('code: 13')
const isIOSCancel = 'code' in e && String(e.code) === '-128'
// In case user cancels / fails the fingerprint auth, empty app state is loaded.
// If a user updates the empty app state, it is stored unencrypted and returned after restart as primary one,
// making encrypted data inaccessible or later overwritten.
// Therefore this ugly app exit on unsuccessful auth.
if(isCancellPressed || isBackPressed || isIOSCancel) {
RNExitApp.exitApp()
}
}
}
}
// load the last known state from storage
restoredState = MMKVStorage.load(ROOT_STORAGE_KEY) || {}
applySnapshot(rootStore, restoredState)
} catch (e: any) {
log.error('[setupRootStore]', Err.STORAGE_ERROR, e.message, e.params)
}
// stop tracking state changes if we've already setup
if (_disposer) {
_disposer()
}
// track changes & save to storage // TODO defering and batching of writes to storage
_disposer = onSnapshot(rootStore, snapshot =>
MMKVStorage.save(ROOT_STORAGE_KEY, snapshot),
)
// run migrations if needed, needs to be after onSnapshot to be persisted
try {
log.debug('[setupRootStore]', `Device rootStore.version is: ${rootStore.version}`)
if(rootStore.version < rootStoreModelVersion) {
await _runMigrations(rootStore)
}
} catch (e: any) {
log.error(Err.STORAGE_ERROR, e.message)
}
const unsubscribe = () => {
_disposer()
_disposer = undefined
}
return {rootStore, restoredState, unsubscribe}
}
/**
* Migrations code to execute based on code and on device model version.
*/
async function _runMigrations(rootStore: RootStore) {
const {
userSettingsStore,
walletProfileStore,
relaysStore,
contactsStore,
mintsStore,
} = rootStore
let currentVersion = rootStore.version
try {
if(currentVersion < 3) {
log.trace(`Starting rootStore migrations from version v${currentVersion} -> v3`)
if(walletProfileStore.pubkey) {
walletProfileStore.setNip05(walletProfileStore.name+MINIBITS_NIP05_DOMAIN)
log.info(`Completed rootStore migrations to the version v${rootStoreModelVersion}`)
rootStore.setVersion(rootStoreModelVersion)
}
}
if(currentVersion < 4) {
log.trace(`Starting rootStore migrations from version v${currentVersion} -> v4`)
if(walletProfileStore.pubkey) {
walletProfileStore.setWalletId(userSettingsStore.walletId as string)
// publish profile to relays
await walletProfileStore.publishToRelays()
log.info(`Completed rootStore migrations to the version v${rootStoreModelVersion}`)
rootStore.setVersion(rootStoreModelVersion)
}
}
if(currentVersion < 5) {
log.trace(`Starting rootStore migrations from version v${currentVersion} -> v5`)
if(contactsStore.publicRelay) {
relaysStore.addOrUpdateRelay({
url: contactsStore.publicRelay,
status: WebSocket.CLOSED
})
}
log.info(`Completed rootStore migrations to the version v${rootStoreModelVersion}`)
rootStore.setVersion(rootStoreModelVersion)
}
if(currentVersion < 6) {
log.trace(`Starting rootStore migrations from version v${currentVersion} -> v6`)
userSettingsStore.setLogLevel(LogLevel.ERROR)
log.info(`Completed rootStore migrations to the version v${rootStoreModelVersion}`)
rootStore.setVersion(rootStoreModelVersion)
}
if(currentVersion < 7) {
log.trace(`Starting rootStore migrations from version v${currentVersion} -> v7`)
for (const mint of mintsStore.allMints) {
mint.setStatus(MintStatus.ONLINE)
}
log.info(`Completed rootStore migrations to the version v${rootStoreModelVersion}`)
rootStore.setVersion(rootStoreModelVersion)
}
if(currentVersion < 8) {
log.trace(`Starting rootStore migrations from version v${currentVersion} -> v8`)
const seedHash = await KeyChain.loadSeedHash()
if(seedHash && walletProfileStore.pubkey) {
await MinibitsClient.migrateSeedHash(
walletProfileStore.pubkey,
{
seedHash
}
)
walletProfileStore.setSeedHash(seedHash)
log.info(`Completed rootStore migrations to the version v${rootStoreModelVersion}`)
rootStore.setVersion(rootStoreModelVersion)
}
}
if(currentVersion < 9) {
log.trace(`Starting rootStore migrations from version v${currentVersion} -> v9`)
for (const mint of mintsStore.allMints) {
try {
await mint.setShortname()
} catch (e: any) {
continue
}
}
log.info(`Completed rootStore migrations to the version v${rootStoreModelVersion}`)
rootStore.setVersion(rootStoreModelVersion)
}
} catch (e: any) {
throw new AppError(
Err.STORAGE_ERROR,
'Error when executing rootStore migrations',
e.message,
)
}
}