-
Notifications
You must be signed in to change notification settings - Fork 370
/
Copy pathuseSmartAccounts.ts
123 lines (111 loc) · 3.73 KB
/
useSmartAccounts.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
import { EIP155Chain } from '@/data/EIP155Data'
import SettingsStore, {
SAFE_SMART_ACCOUNTS_ENABLED_KEY,
ZERO_DEV_SMART_ACCOUNTS_ENABLED_KEY,
BICONOMY_SMART_ACCOUNTS_ENABLED_KEY
} from '@/store/SettingsStore'
import {
createOrRestoreBiconomySmartAccount,
createOrRestoreKernelSmartAccount,
createOrRestoreSafeSmartAccount,
smartAccountWallets
} from '@/utils/SmartAccountUtil'
import { styledToast } from '@/utils/HelperUtil'
import { useSnapshot } from 'valtio'
export default function useSmartAccounts() {
const {
smartAccountEnabled,
kernelSmartAccountEnabled,
safeSmartAccountEnabled,
biconomySmartAccountEnabled
} = useSnapshot(SettingsStore.state)
const initializeSmartAccounts = async (privateKey: string) => {
if (smartAccountEnabled) {
try {
const promises = []
if (kernelSmartAccountEnabled) {
promises.push(
(async () => {
try {
const address = await createOrRestoreKernelSmartAccount(privateKey)
SettingsStore.setKernelSmartAccountAddress(address)
} catch (error) {
SettingsStore.state.kernelSmartAccountEnabled = false
localStorage.removeItem(ZERO_DEV_SMART_ACCOUNTS_ENABLED_KEY)
styledToast('Kernel smart account initialization failed', 'warning')
}
})()
)
}
if (safeSmartAccountEnabled) {
promises.push(
(async () => {
try {
const address = await createOrRestoreSafeSmartAccount(privateKey)
SettingsStore.setSafeSmartAccountAddress(address)
} catch (error) {
SettingsStore.state.safeSmartAccountEnabled = false
localStorage.removeItem(SAFE_SMART_ACCOUNTS_ENABLED_KEY)
styledToast('Safe smart account initialization failed', 'warning')
}
})()
)
}
if (biconomySmartAccountEnabled) {
promises.push(
(async () => {
try {
const address = await createOrRestoreBiconomySmartAccount(privateKey)
SettingsStore.setBiconomySmartAccountAddress(address)
} catch (error) {
SettingsStore.state.biconomySmartAccountEnabled = false
localStorage.removeItem(BICONOMY_SMART_ACCOUNTS_ENABLED_KEY)
styledToast('Biconomy smart account initialization failed', 'warning')
}
})()
)
}
await Promise.all(promises)
} catch (error) {
console.error('Error initializing smart accounts:', error)
styledToast('Error initializing smart accounts', 'error')
}
}
}
const getAvailableSmartAccounts = () => {
if (!smartAccountEnabled) {
return []
}
const accounts = []
for (const [key, lib] of Object.entries(smartAccountWallets)) {
accounts.push({
address: key.split(':')[1],
type: lib.type,
chain: lib.chain
})
}
return accounts
}
const getAvailableSmartAccountsOnNamespaceChains = (chains: string[] | undefined) => {
if (!smartAccountEnabled || chains === undefined) {
return []
}
const accounts = []
for (const [key, lib] of Object.entries(smartAccountWallets)) {
accounts.push({
address: key.split(':')[1],
type: lib.type,
chain: lib.chain
})
}
const filteredAccounts = accounts.filter(account =>
chains.some(chain => chain && parseInt(chain.split(':')[1]) === account.chain.id)
)
return filteredAccounts
}
return {
initializeSmartAccounts,
getAvailableSmartAccounts,
getAvailableSmartAccountsOnNamespaceChains
}
}