-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnector.ts
More file actions
117 lines (100 loc) · 3.05 KB
/
connector.ts
File metadata and controls
117 lines (100 loc) · 3.05 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
import type { CreateConnectorFn } from '@wagmi/core'
import type {
ZeroDevProvider,
ZeroDevWalletConnectorParams,
} from '@zerodev/wallet-react'
import { zeroDevWallet } from '@zerodev/wallet-react'
import type { AuthConfig } from './auth/types'
import { createStore } from './store.js'
import type { Request, RequestMethod } from './types.js'
const DEFAULT_SIGNING_PROMPT_METHODS: RequestMethod[] = [
'eth_sendTransaction',
'wallet_sendTransaction',
'wallet_sendCalls',
'personal_sign',
'eth_signTypedData_v4',
]
export type SigningConfig =
| { mode: 'background' }
| { mode: 'prompt'; methods?: RequestMethod[] }
export type ZeroDevKitConfig = {
signing?: SigningConfig
auth?: AuthConfig
}
export type ZeroDevKitConnectorParams = ZeroDevWalletConnectorParams & {
config?: ZeroDevKitConfig
}
function requireUserConfirmation(
store: ReturnType<typeof createStore>,
request: Request,
): Promise<void> {
return new Promise<void>((resolve, reject) => {
store.getState().addPendingRequest({
id: crypto.randomUUID(),
...request,
resolve,
reject,
})
})
}
export function zeroDevKitWallet(
params: ZeroDevKitConnectorParams,
): CreateConnectorFn {
const baseFactory = zeroDevWallet(params)
const store = createStore()
// Initialize auth config if provided
if (params.config?.auth) {
store.getState().auth.initialize(params.config.auth)
}
return (wagmiConfig) => {
const connector = baseFactory(wagmiConfig)
return {
...connector,
async disconnect() {
await connector.disconnect?.()
// Reset auth state on disconnect
if (params.config?.auth) {
store.getState().auth.reset()
store.getState().auth.initialize(params.config.auth)
}
},
async setup() {
await connector.setup?.()
const signing = params.config?.signing
if (signing?.mode === 'background') return
const methods =
(signing?.mode === 'prompt' && signing.methods) ||
DEFAULT_SIGNING_PROMPT_METHODS
const provider = (await connector.getProvider()) as ZeroDevProvider
const baseRequest = provider.request.bind(provider)
// todo: move Request type to core package and use as the method and params type here
// then we can remove as RequestMethod and spread in return baseRequest
provider.request = async ({
method,
params: reqParams,
}: {
method: string
params?: unknown[]
}) => {
const { userConfirmationListenerActive } = store.getState()
if (
userConfirmationListenerActive &&
methods.includes(method as RequestMethod)
) {
await requireUserConfirmation(store, {
method: method as RequestMethod,
params: reqParams,
} as Request)
}
return baseRequest({
method,
...(reqParams && { params: reqParams }),
})
}
},
getKitStore() {
return store
},
}
}
}