-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstore.ts
More file actions
124 lines (107 loc) · 3.52 KB
/
store.ts
File metadata and controls
124 lines (107 loc) · 3.52 KB
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
import type {
KernelAccountClient,
KernelSmartAccountImplementation,
} from '@zerodev/sdk'
import type {
ZeroDevWalletSDK,
ZeroDevWalletSession,
} from '@zerodev/wallet-core'
import type { LocalAccount } from 'viem'
import type { SmartAccount } from 'viem/account-abstraction'
import { create } from 'zustand'
import {
createJSONStorage,
persist,
type StateStorage,
subscribeWithSelector,
} from 'zustand/middleware'
// Internal OAuth config stored in the state (derived from connector params)
type InternalOAuthConfig = {
backendUrl: string
projectId: string
}
export type ZeroDevWalletState = {
// Core
wallet: ZeroDevWalletSDK | null
eoaAccount: LocalAccount | null
session: ZeroDevWalletSession | null
// Multi-chain support
activeChainId: number | null
kernelAccounts: Map<number, SmartAccount<KernelSmartAccountImplementation>>
kernelClients: Map<number, KernelAccountClient>
// Session expiry
isExpiring: boolean
// OAuth config (derived from connector params)
oauthConfig: InternalOAuthConfig | null
// Actions
setWallet: (wallet: ZeroDevWalletSDK) => void
setEoaAccount: (account: LocalAccount | null) => void
setKernelAccount: (
chainId: number,
account: SmartAccount<KernelSmartAccountImplementation>,
) => void
setKernelClient: (chainId: number, client: KernelAccountClient) => void
setSession: (session: ZeroDevWalletSession | null) => void
setActiveChainId: (chainId: number | null) => void
setIsExpiring: (isExpiring: boolean) => void
setOAuthConfig: (config: InternalOAuthConfig | null) => void
clear: () => void
}
export type CreateStoreOptions = {
storage?: StateStorage<void>
}
export const createZeroDevWalletStore = (options?: CreateStoreOptions) =>
create<ZeroDevWalletState>()(
subscribeWithSelector(
persist(
(set, get) => ({
// Initial state
wallet: null,
eoaAccount: null,
session: null,
activeChainId: null,
kernelAccounts: new Map(),
kernelClients: new Map(),
isExpiring: false,
oauthConfig: null,
// Actions
setWallet: (wallet) => set({ wallet }),
setEoaAccount: (account) => set({ eoaAccount: account }),
setKernelAccount: (chainId, account) => {
const accounts = new Map(get().kernelAccounts)
accounts.set(chainId, account)
set({ kernelAccounts: accounts })
},
setKernelClient: (chainId, client) => {
const clients = new Map(get().kernelClients)
clients.set(chainId, client)
set({ kernelClients: clients })
},
setSession: (session) => set({ session }),
setActiveChainId: (chainId) => set({ activeChainId: chainId }),
setIsExpiring: (isExpiring) => set({ isExpiring }),
setOAuthConfig: (config) => set({ oauthConfig: config }),
clear: () =>
set({
eoaAccount: null,
session: null,
kernelAccounts: new Map(),
kernelClients: new Map(),
isExpiring: false,
activeChainId: null,
}),
}),
{
name: 'zerodev-wallet',
// Only persist session data, not clients or accounts
partialize: (state) => ({
session: state.session,
activeChainId: state.activeChainId,
}),
...(options?.storage && {
storage: createJSONStorage(() => options.storage!),
}),
},
),
),
)