@unisat/wallet-state 1.0.4 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. package/LICENSE +0 -5
  2. package/lib/index.d.mts +2012 -200
  3. package/lib/index.d.ts +2012 -200
  4. package/lib/index.js +6951 -570
  5. package/lib/index.js.map +1 -1
  6. package/lib/index.mjs +6754 -517
  7. package/lib/index.mjs.map +1 -1
  8. package/lib/types/index.d.mts +1 -1
  9. package/lib/types/index.d.ts +1 -1
  10. package/package.json +19 -15
  11. package/src/.DS_Store +0 -0
  12. package/src/context/ApprovalContext.tsx +27 -0
  13. package/src/context/DeviceContext.tsx +36 -0
  14. package/src/context/I18nContext.tsx +14 -172
  15. package/src/context/NavigationContext.tsx +305 -0
  16. package/src/context/PriceContext.tsx +2 -2
  17. package/src/context/StorageContext.tsx +393 -0
  18. package/src/context/ToolsContext.tsx +50 -0
  19. package/src/context/WalletContext.tsx +119 -108
  20. package/src/context/index.ts +17 -2
  21. package/src/hooks/accounts.ts +11 -5
  22. package/src/hooks/browser.ts +11 -0
  23. package/src/hooks/global.ts +170 -7
  24. package/src/hooks/index.ts +1 -2
  25. package/src/hooks/settings.ts +28 -37
  26. package/src/hooks/transactions.ts +28 -155
  27. package/src/hooks/ui.ts +232 -36
  28. package/src/index.ts +33 -24
  29. package/src/reducers/accounts.ts +19 -2
  30. package/src/reducers/browser.ts +223 -0
  31. package/src/reducers/global.ts +67 -1
  32. package/src/reducers/index.ts +1 -0
  33. package/src/reducers/transactions.ts +0 -9
  34. package/src/reducers/ui.ts +127 -8
  35. package/src/types/index.ts +1 -1
  36. package/src/ui-hooks/index.ts +107 -0
  37. package/src/ui-hooks/useActionOverviewSectionLogic.ts +150 -0
  38. package/src/ui-hooks/useAddressTypeScreenLogic.ts +160 -0
  39. package/src/ui-hooks/useAlkanesBalanceCardLogic.ts +41 -0
  40. package/src/ui-hooks/useAlkanesCollectionListLogic.ts +68 -0
  41. package/src/ui-hooks/useAlkanesListLogic.ts +69 -0
  42. package/src/ui-hooks/useAlkanesNFTListLogic.ts +42 -0
  43. package/src/ui-hooks/useAlkanesNFTScreenLogic.ts +45 -0
  44. package/src/ui-hooks/useAlkanesTokenScreenLogic.ts +130 -0
  45. package/src/ui-hooks/useAmountInputLogic.ts +80 -0
  46. package/src/ui-hooks/useAnnouncementCardLogic.ts +91 -0
  47. package/src/ui-hooks/useBRC20BalanceCardLogic.ts +115 -0
  48. package/src/ui-hooks/useBRC20InscribeTransferLogic.ts +398 -0
  49. package/src/ui-hooks/useBRC20ListLogic.ts +75 -0
  50. package/src/ui-hooks/useBRC20ProgListLogic.ts +77 -0
  51. package/src/ui-hooks/useBRC20SendScreenLogic.ts +411 -0
  52. package/src/ui-hooks/useBRC20SingleStepScreenLogic.ts +208 -0
  53. package/src/ui-hooks/useBRC20TokenScreenLogic.ts +469 -0
  54. package/src/ui-hooks/useBalanceCardLogic.ts +164 -0
  55. package/src/ui-hooks/useBtcDisplayLogic.ts +16 -0
  56. package/src/ui-hooks/useCAT20BalanceCardLogic.ts +35 -0
  57. package/src/ui-hooks/useCAT20ListLogic.ts +83 -0
  58. package/src/ui-hooks/useCAT20TokenScreenLogic.ts +122 -0
  59. package/src/ui-hooks/useCAT721ListLogic.ts +68 -0
  60. package/src/ui-hooks/useCAT721NFTScreenLogic.ts +37 -0
  61. package/src/ui-hooks/useCreatePasswordScreenLogic.ts +92 -0
  62. package/src/ui-hooks/useCreateWalletLogicImportWordsStep.ts +299 -0
  63. package/src/ui-hooks/useEditAccountNameScreenLogic.ts +71 -0
  64. package/src/ui-hooks/useEditContactScreenLogic.ts +162 -0
  65. package/src/ui-hooks/useEditWalletNameScreenLogic.ts +58 -0
  66. package/src/ui-hooks/useExportMnemonicsScreenLogic.ts +75 -0
  67. package/src/ui-hooks/useExportPrivateKeyScreenLogic.ts +64 -0
  68. package/src/ui-hooks/useFeeRateBarLogic.ts +303 -0
  69. package/src/ui-hooks/useInfiniteList.ts +85 -0
  70. package/src/ui-hooks/useInscriptionListLogic.ts +68 -0
  71. package/src/ui-hooks/useLockTimePageLogic.ts +43 -0
  72. package/src/ui-hooks/useNotificationsLogic.ts +115 -0
  73. package/src/ui-hooks/useOrdinalsInscriptionScreenLogic.ts +130 -0
  74. package/src/ui-hooks/useRunesBalanceCardLogic.ts +44 -0
  75. package/src/ui-hooks/useRunesListLogic.ts +74 -0
  76. package/src/ui-hooks/useRunesTokenScreenLogic.ts +149 -0
  77. package/src/ui-hooks/useSecurityCardLogic.ts +0 -0
  78. package/src/ui-hooks/useSendAlkanesNFTScreenLogic.ts +138 -0
  79. package/src/ui-hooks/useSendAlkanesScreenLogic.ts +192 -0
  80. package/src/ui-hooks/useSendCAT20ScreenLogic.ts +297 -0
  81. package/src/ui-hooks/useSendCAT721ScreenLogic.ts +205 -0
  82. package/src/ui-hooks/useSendOrdinalsInscriptionScreenLogic.ts +137 -0
  83. package/src/ui-hooks/useSendRunesScreenLogic.ts +172 -0
  84. package/src/ui-hooks/useSettingsTabScreenLogic.ts +211 -0
  85. package/src/ui-hooks/useSignMessageLogic.ts +302 -0
  86. package/src/ui-hooks/useSignPsbtLogic.ts +517 -0
  87. package/src/ui-hooks/useSplitOrdinalsInscriptionScreenLogic.ts +95 -0
  88. package/src/ui-hooks/useTxConfirmScreenLogic.ts +47 -0
  89. package/src/ui-hooks/useTxCreateScreenLogic.ts +161 -0
  90. package/src/ui-hooks/useTxFailScreenLogic.ts +26 -0
  91. package/src/ui-hooks/useTxSuccessScreenLogic.ts +33 -0
  92. package/src/updater/accounts.ts +11 -11
  93. package/src/utils/bitcoin-utils.ts +17 -8
  94. package/src/utils/eventBus.ts +2 -1
  95. package/src/utils/password-utils.ts +78 -0
  96. package/src/utils/ui-utils.ts +28 -0
  97. package/src/hooks/approval.ts +0 -72
  98. package/src/hooks/i18n.ts +0 -53
  99. package/src/utils/i18n.ts +0 -41
@@ -0,0 +1,393 @@
1
+ /* eslint-disable indent */
2
+ import { VersionDetail } from '@unisat/wallet-shared'
3
+ import React, { useContext } from 'react'
4
+
5
+ enum StorageType {
6
+ ENABLE_FACEID = 'FACEIDDEFAULT',
7
+ AUTO_LOCK_TIME = 'AutomaticLockTime',
8
+ ENABLE_EXPLORE = 'ENABLE_EXPLORE',
9
+ UTXO_MANAGE_VISITED = 'UTXO_MANAGE_VISITED',
10
+ BALANCE_HIDDEN = 'HIDE_BALANCE',
11
+ ANNOUNCEMENT_DISMISSED_IDS_KEY = '@announcement_dismissed_ids',
12
+
13
+ I18N_SAVED_LANG = 'i18nextLng',
14
+
15
+ WEB_SEARCH_HISTORY = 'historyList',
16
+ WEB_RISK_WARNING = 'RiskWarning',
17
+
18
+ UNBACKUP_KEYRINGS = 'unbackedup_keyrings',
19
+
20
+ VERSION_DETAIL = 'version_detail',
21
+
22
+ THEME_MODE = 'PREFERENCES',
23
+ }
24
+
25
+ type AutoLockTime = {
26
+ id: number
27
+ time: number
28
+ label: string
29
+ }
30
+
31
+ type WebSearchHistoryItem = string
32
+
33
+ type WebRiskWarningItem = {
34
+ url: string
35
+ }
36
+
37
+ const defaultVersionDetail: VersionDetail = {
38
+ version: '',
39
+ title: '',
40
+ changelogs: [],
41
+ notice: '',
42
+ }
43
+
44
+ type ThemeMode = 'light' | 'dark'
45
+ const defaultThemeMode = 'light'
46
+
47
+ export interface BaseStorageProvider {
48
+ // storage should json.parse value after get
49
+ get: (key: string) => Promise<any>
50
+
51
+ // storage should json.stringify value before set
52
+ set: (key: string, value: any) => Promise<void>
53
+ }
54
+
55
+ export interface StorageContextType {
56
+ get: (key: string) => Promise<string>
57
+ set: (key: string, value: string) => Promise<void>
58
+ clear: () => Promise<void>
59
+
60
+ getAutomaticLockTime: () => Promise<AutoLockTime>
61
+ clearAutomaticLockTime: () => Promise<void>
62
+
63
+ getAnnouncementDismissedIds: () => Promise<string[]>
64
+ setAnnouncementDismissedIds: (ids: string[]) => Promise<void>
65
+ clearAnnouncementDismissedIds: () => Promise<void>
66
+
67
+ getI18nSavedLang: () => Promise<string>
68
+ setI18nSavedLang: (lang: string) => Promise<void>
69
+ clearI18nSavedLang: () => Promise<void>
70
+
71
+ getWebSearchHistory: () => Promise<WebSearchHistoryItem[]>
72
+ setWebSearchHistory: (history: WebSearchHistoryItem[]) => Promise<void>
73
+ clearWebSearchHistory: () => Promise<void>
74
+
75
+ getWebRiskWarning: () => Promise<WebRiskWarningItem[]>
76
+ setWebRiskWarning: (warnings: WebRiskWarningItem[]) => Promise<void>
77
+ clearWebRiskWarning: () => Promise<void>
78
+
79
+ getEnableExplore: () => Promise<boolean>
80
+ setEnableExplore: (enable: boolean) => Promise<void>
81
+ clearEnableExplore: () => Promise<void>
82
+
83
+ getUnbackupKeyringIds: () => Promise<string[]>
84
+ setUnbackupKeyringIds: (keyrings: string[]) => Promise<void>
85
+ clearUnbackupKeyringIds: () => Promise<void>
86
+
87
+ getEnableFaceId: () => Promise<boolean>
88
+ setEnableFaceId: (enable: boolean) => Promise<void>
89
+ clearEnableFaceId: () => Promise<void>
90
+
91
+ getBalanceHidden: () => Promise<boolean>
92
+ setBalanceHidden: (enable: boolean) => Promise<void>
93
+ clearBalanceHidden: () => Promise<void>
94
+
95
+ getVersionDetail: () => Promise<VersionDetail>
96
+ setVersionDetail: (detail: VersionDetail) => Promise<void>
97
+ clearVersionDetail: () => Promise<void>
98
+
99
+ getThemeMode: () => Promise<string>
100
+ setThemeMode: (mode: string) => Promise<void>
101
+ clearThemeMode: () => Promise<void>
102
+
103
+ getUtxoManageVisited: () => Promise<boolean>
104
+ setUtxoManageVisited: () => Promise<void>
105
+ clearUtxoManageVisited: () => Promise<void>
106
+ }
107
+
108
+ const defaultContext: StorageContextType = {
109
+ async get(key: string) {
110
+ return ''
111
+ },
112
+ async set(key: string, content: string) {},
113
+
114
+ async clear() {},
115
+
116
+ async getAutomaticLockTime() {
117
+ return null
118
+ },
119
+ async clearAutomaticLockTime() {},
120
+
121
+ async getAnnouncementDismissedIds() {
122
+ return []
123
+ },
124
+ async setAnnouncementDismissedIds(ids: string[]) {},
125
+ async clearAnnouncementDismissedIds() {},
126
+
127
+ async getI18nSavedLang() {
128
+ return ''
129
+ },
130
+ async setI18nSavedLang(lang: string) {},
131
+ async clearI18nSavedLang() {},
132
+
133
+ async getWebSearchHistory() {
134
+ return []
135
+ },
136
+ async setWebSearchHistory(history: WebSearchHistoryItem[]) {},
137
+ async clearWebSearchHistory() {},
138
+
139
+ async getWebRiskWarning() {
140
+ return []
141
+ },
142
+ async setWebRiskWarning(warnings: WebRiskWarningItem[]) {},
143
+ async clearWebRiskWarning() {},
144
+
145
+ async getEnableExplore() {
146
+ return false
147
+ },
148
+ async setEnableExplore(enable: boolean) {},
149
+ async clearEnableExplore() {},
150
+
151
+ async getUnbackupKeyringIds() {
152
+ return []
153
+ },
154
+ async setUnbackupKeyringIds(keyringIds: string[]) {},
155
+ async clearUnbackupKeyringIds() {},
156
+
157
+ async getEnableFaceId() {
158
+ return false
159
+ },
160
+ async setEnableFaceId(enable: boolean) {},
161
+ async clearEnableFaceId() {},
162
+
163
+ async getBalanceHidden() {
164
+ return false
165
+ },
166
+ async setBalanceHidden(enable) {},
167
+ async clearBalanceHidden() {},
168
+
169
+ async getVersionDetail() {
170
+ return defaultVersionDetail
171
+ },
172
+ async setVersionDetail(detail: VersionDetail) {},
173
+ async clearVersionDetail() {},
174
+
175
+ async getThemeMode() {
176
+ return defaultThemeMode
177
+ },
178
+ async setThemeMode(mode: string) {},
179
+ async clearThemeMode() {},
180
+
181
+ async getUtxoManageVisited() {
182
+ return false
183
+ },
184
+ async setUtxoManageVisited() {},
185
+ async clearUtxoManageVisited() {},
186
+ }
187
+
188
+ function legacyParseObjectValue(val: any) {
189
+ let parsedObj = null
190
+ if (val && typeof val !== 'string') {
191
+ return val
192
+ }
193
+
194
+ if (typeof val === 'string' && val.length > 0) {
195
+ try {
196
+ parsedObj = JSON.parse(val)
197
+ } catch (e) {
198
+ parsedObj = null
199
+ }
200
+ }
201
+
202
+ // legacy double stringify
203
+ val = parsedObj
204
+ if (val && typeof val === 'string' && val.length > 0) {
205
+ try {
206
+ parsedObj = JSON.parse(val)
207
+ } catch (e) {
208
+ parsedObj = null
209
+ }
210
+ }
211
+
212
+ return parsedObj
213
+ }
214
+
215
+ function isLegacyStringifiedObject(val: any) {
216
+ if (typeof val === 'string' && val.length > 0) {
217
+ try {
218
+ const parsedObj = JSON.parse(val)
219
+ if (parsedObj && typeof parsedObj === 'string' && parsedObj.length > 0) {
220
+ try {
221
+ JSON.parse(parsedObj)
222
+ return true
223
+ } catch (e) {
224
+ return false
225
+ }
226
+ }
227
+ } catch (e) {
228
+ return false
229
+ }
230
+ }
231
+ return false
232
+ }
233
+
234
+ export function createStorageProvider(base: BaseStorageProvider): StorageContextType {
235
+ const funcs = {
236
+ ...base,
237
+
238
+ async getAutomaticLockTime(): Promise<AutoLockTime | null> {
239
+ const val = await base.get(StorageType.AUTO_LOCK_TIME)
240
+ return legacyParseObjectValue(val)
241
+ },
242
+ async clearAutomaticLockTime() {
243
+ await base.set(StorageType.AUTO_LOCK_TIME, null)
244
+ },
245
+
246
+ async getAnnouncementDismissedIds(): Promise<string[]> {
247
+ const val = await base.get(StorageType.ANNOUNCEMENT_DISMISSED_IDS_KEY)
248
+ return legacyParseObjectValue(val) || []
249
+ },
250
+ async setAnnouncementDismissedIds(ids: string[]) {
251
+ await base.set(StorageType.ANNOUNCEMENT_DISMISSED_IDS_KEY, ids)
252
+ },
253
+ async clearAnnouncementDismissedIds() {
254
+ await base.set(StorageType.ANNOUNCEMENT_DISMISSED_IDS_KEY, [])
255
+ },
256
+
257
+ async getI18nSavedLang() {
258
+ const val = await base.get(StorageType.I18N_SAVED_LANG)
259
+ return val || ''
260
+ },
261
+ async setI18nSavedLang(lang: string) {
262
+ await base.set(StorageType.I18N_SAVED_LANG, lang)
263
+ },
264
+ async clearI18nSavedLang() {
265
+ await base.set(StorageType.I18N_SAVED_LANG, '')
266
+ },
267
+
268
+ async getWebSearchHistory(): Promise<WebSearchHistoryItem[]> {
269
+ const val = await base.get(StorageType.WEB_SEARCH_HISTORY)
270
+ return legacyParseObjectValue(val) || []
271
+ },
272
+ async setWebSearchHistory(history: WebSearchHistoryItem[]) {
273
+ await base.set(StorageType.WEB_SEARCH_HISTORY, history)
274
+ },
275
+ async clearWebSearchHistory() {
276
+ await base.set(StorageType.WEB_SEARCH_HISTORY, [])
277
+ },
278
+
279
+ async getWebRiskWarning(): Promise<WebRiskWarningItem[]> {
280
+ const val = await base.get(StorageType.WEB_RISK_WARNING)
281
+ return legacyParseObjectValue(val) || []
282
+ },
283
+ async setWebRiskWarning(warnings: WebRiskWarningItem[]) {
284
+ await base.set(StorageType.WEB_RISK_WARNING, warnings)
285
+ },
286
+ async clearWebRiskWarning() {
287
+ await base.set(StorageType.WEB_RISK_WARNING, [])
288
+ },
289
+
290
+ async getEnableExplore() {
291
+ const val = await base.get(StorageType.ENABLE_EXPLORE)
292
+ return val === 'true'
293
+ },
294
+ async setEnableExplore(enable: boolean) {
295
+ await base.set(StorageType.ENABLE_EXPLORE, String(enable))
296
+ },
297
+ async clearEnableExplore() {
298
+ await base.set(StorageType.ENABLE_EXPLORE, 'false')
299
+ },
300
+
301
+ async getUnbackupKeyringIds(): Promise<string[]> {
302
+ const val = await base.get(StorageType.UNBACKUP_KEYRINGS)
303
+ return legacyParseObjectValue(val) || []
304
+ },
305
+ async setUnbackupKeyringIds(keyringIds: string[]) {
306
+ await base.set(StorageType.UNBACKUP_KEYRINGS, keyringIds)
307
+ },
308
+ async clearUnbackupKeyringIds() {
309
+ await base.set(StorageType.UNBACKUP_KEYRINGS, [])
310
+ },
311
+
312
+ //
313
+ async getEnableFaceId() {
314
+ const val = await base.get(StorageType.ENABLE_FACEID)
315
+ return val === 'true' || val == true
316
+ },
317
+ async setEnableFaceId(enable: boolean) {
318
+ await base.set(StorageType.ENABLE_FACEID, String(enable))
319
+ },
320
+ async clearEnableFaceId() {
321
+ await base.set(StorageType.ENABLE_FACEID, 'false')
322
+ },
323
+
324
+ async getBalanceHidden() {
325
+ const val = await base.get(StorageType.BALANCE_HIDDEN)
326
+ return val === 'true'
327
+ },
328
+ async setBalanceHidden(enable: boolean) {
329
+ await base.set(StorageType.BALANCE_HIDDEN, String(enable))
330
+ },
331
+ async clearBalanceHidden() {
332
+ await base.set(StorageType.BALANCE_HIDDEN, 'false')
333
+ },
334
+
335
+ async getVersionDetail() {
336
+ const val = await base.get(StorageType.VERSION_DETAIL)
337
+ return legacyParseObjectValue(val) || defaultVersionDetail
338
+ },
339
+ async setVersionDetail(detail: VersionDetail) {
340
+ await base.set(StorageType.VERSION_DETAIL, detail)
341
+ },
342
+ async clearVersionDetail() {
343
+ await base.set(StorageType.VERSION_DETAIL, defaultVersionDetail)
344
+ },
345
+
346
+ async getThemeMode() {
347
+ const val = await base.get(StorageType.THEME_MODE)
348
+ return val || defaultThemeMode
349
+ },
350
+ async setThemeMode(mode: string) {
351
+ await base.set(StorageType.THEME_MODE, mode)
352
+ },
353
+ async clearThemeMode() {
354
+ await base.set(StorageType.THEME_MODE, defaultThemeMode)
355
+ },
356
+
357
+ async getUtxoManageVisited() {
358
+ const val = await base.get(StorageType.UTXO_MANAGE_VISITED)
359
+ return val === 'true'
360
+ },
361
+ async setUtxoManageVisited() {
362
+ await base.set(StorageType.UTXO_MANAGE_VISITED, 'true')
363
+ },
364
+ async clearUtxoManageVisited() {
365
+ await base.set(StorageType.UTXO_MANAGE_VISITED, 'false')
366
+ },
367
+ }
368
+
369
+ return {
370
+ ...funcs,
371
+ async clear() {
372
+ await funcs.clearAutomaticLockTime()
373
+ await funcs.clearAnnouncementDismissedIds()
374
+ await funcs.clearI18nSavedLang()
375
+ await funcs.clearWebSearchHistory()
376
+ await funcs.clearWebRiskWarning()
377
+ await funcs.clearEnableExplore()
378
+ await funcs.clearUnbackupKeyringIds()
379
+ await funcs.clearEnableFaceId()
380
+ await funcs.clearBalanceHidden()
381
+ await funcs.clearVersionDetail()
382
+ await funcs.clearThemeMode()
383
+ await funcs.clearUtxoManageVisited()
384
+ },
385
+ }
386
+ }
387
+
388
+ export const StorageContext = React.createContext<StorageContextType>(defaultContext)
389
+
390
+ export function useStorage() {
391
+ const ctx = useContext(StorageContext)
392
+ return ctx
393
+ }
@@ -0,0 +1,50 @@
1
+ /* eslint-disable indent */
2
+ import React, { useCallback, useContext, useRef, useState } from 'react'
3
+
4
+ type ToastFunction = (content: string) => void
5
+ type LoadingFunction = (visible: boolean, content?: string) => void
6
+
7
+ export interface ToolsContextType {
8
+ toast: ToastFunction
9
+ toastSuccess: ToastFunction
10
+ toastError: ToastFunction
11
+ toastWarning: ToastFunction
12
+ showLoading: LoadingFunction
13
+ showTip: ToastFunction
14
+ copyToClipboard: (text: string) => void
15
+ openUrl: (url: string) => void
16
+ }
17
+
18
+ const initContext = {
19
+ toast: (content: string) => {
20
+ // todo
21
+ },
22
+ toastSuccess: (content: string) => {
23
+ // todo
24
+ },
25
+ toastError: (content: string) => {
26
+ // todo
27
+ },
28
+ toastWarning: (content: string) => {
29
+ // todo
30
+ },
31
+ showLoading: () => {
32
+ // todo
33
+ },
34
+ showTip: (content: string) => {
35
+ // todo
36
+ },
37
+ copyToClipboard: (text: string) => {
38
+ // todo
39
+ },
40
+ openUrl(url: string) {
41
+ // todo
42
+ },
43
+ }
44
+
45
+ export const ToolsContext = React.createContext<ToolsContextType>(initContext)
46
+
47
+ export function useTools() {
48
+ const ctx = useContext(ToolsContext)
49
+ return ctx
50
+ }