-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
StorageUtil.ts
146 lines (125 loc) · 4.06 KB
/
StorageUtil.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
/* eslint-disable no-console */
import { SafeLocalStorage, SafeLocalStorageKeys, type ChainNamespace } from '@reown/appkit-common'
import type { WcWallet, ConnectorType, SocialProvider, ConnectionStatus } from './TypeUtil.js'
import { ChainController } from '../controllers/ChainController.js'
// -- Utility -----------------------------------------------------------------
export const StorageUtil = {
setWalletConnectDeepLink({ name, href }: { href: string; name: string }) {
try {
SafeLocalStorage.setItem(SafeLocalStorageKeys.DEEPLINK_CHOICE, JSON.stringify({ href, name }))
} catch {
console.info('Unable to set WalletConnect deep link')
}
},
getWalletConnectDeepLink() {
try {
const deepLink = SafeLocalStorage.getItem(SafeLocalStorageKeys.DEEPLINK_CHOICE)
if (deepLink) {
return JSON.parse(deepLink)
}
} catch {
console.info('Unable to get WalletConnect deep link')
}
return undefined
},
deleteWalletConnectDeepLink() {
try {
SafeLocalStorage.removeItem(SafeLocalStorageKeys.DEEPLINK_CHOICE)
} catch {
console.info('Unable to delete WalletConnect deep link')
}
},
setAppKitRecent(wallet: WcWallet) {
try {
const recentWallets = StorageUtil.getRecentWallets()
const exists = recentWallets.find(w => w.id === wallet.id)
if (!exists) {
recentWallets.unshift(wallet)
if (recentWallets.length > 2) {
recentWallets.pop()
}
SafeLocalStorage.setItem(SafeLocalStorageKeys.RECENT_WALLETS, JSON.stringify(recentWallets))
}
} catch {
console.info('Unable to set AppKit recent')
}
},
getRecentWallets(): WcWallet[] {
try {
const recent = SafeLocalStorage.getItem(SafeLocalStorageKeys.RECENT_WALLETS)
return recent ? JSON.parse(recent) : []
} catch {
console.info('Unable to get AppKit recent')
}
return []
},
setConnectedConnector(connectorType: ConnectorType) {
try {
SafeLocalStorage.setItem(SafeLocalStorageKeys.CONNECTED_CONNECTOR, connectorType)
} catch {
console.info('Unable to set Connected Connector')
}
},
setConnectedNamespace(namespace: ChainNamespace) {
try {
SafeLocalStorage.setItem(SafeLocalStorageKeys.CONNECTED_NAMESPACE, namespace)
} catch {
console.info('Unable to set Connected Namespace')
}
},
getConnectedConnector() {
try {
return SafeLocalStorage.getItem(SafeLocalStorageKeys.CONNECTED_CONNECTOR) as ConnectorType
} catch {
console.info('Unable to get Connected Connector')
}
return undefined
},
setConnectedSocialProvider(socialProvider: SocialProvider) {
try {
SafeLocalStorage.setItem(SafeLocalStorageKeys.CONNECTED_SOCIAL, socialProvider)
} catch {
console.info('Unable to set Connected Social Provider')
}
},
getConnectedSocialProvider() {
try {
return SafeLocalStorage.getItem(SafeLocalStorageKeys.CONNECTED_SOCIAL)
} catch {
console.info('Unable to get Connected Social Provider')
}
return undefined
},
getConnectedSocialUsername() {
try {
return SafeLocalStorage.getItem(SafeLocalStorageKeys.CONNECTED_SOCIAL_USERNAME)
} catch {
console.info('Unable to get Connected Social Username')
}
return undefined
},
getStoredActiveCaipNetwork() {
const storedCaipNetworkId = SafeLocalStorage.getItem(
SafeLocalStorageKeys.ACTIVE_CAIP_NETWORK_ID
)
const allRequestedCaipNetworks = ChainController.getAllRequestedCaipNetworks()
const storedCaipNetwork = allRequestedCaipNetworks?.find(
c => c.caipNetworkId === storedCaipNetworkId
)
return storedCaipNetwork
},
setConnectionStatus(status: ConnectionStatus) {
try {
SafeLocalStorage.setItem(SafeLocalStorageKeys.CONNECTION_STATUS, status)
} catch {
console.info('Unable to set Connection Status')
}
},
getConnectionStatus() {
try {
return SafeLocalStorage.getItem(SafeLocalStorageKeys.CONNECTION_STATUS) as ConnectionStatus
} catch {
return undefined
}
}
}