-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Expand file tree
/
Copy pathEmbeddedWalletConnector.ts
More file actions
195 lines (175 loc) · 6.77 KB
/
EmbeddedWalletConnector.ts
File metadata and controls
195 lines (175 loc) · 6.77 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import { EmbeddedWalletProvider, Listener, embeddedWalletProvider } from 'connection/EmbeddedWalletProvider'
import { getEmbeddedWalletState } from 'state/embeddedWallet/store'
import {
ProviderConnectInfo,
ResourceUnavailableRpcError,
RpcError,
SwitchChainError,
UserRejectedRequestError,
getAddress,
} from 'viem'
import { ChainNotConfiguredError, createConnector } from 'wagmi'
interface EmbeddedWalletParameters {
onConnect?(): void
}
export function embeddedWallet(_parameters: EmbeddedWalletParameters = {}) {
type Provider = EmbeddedWalletProvider
type Properties = {
onConnect(connectInfo: ProviderConnectInfo): void
}
type StorageItem = { 'embeddedUniswapWallet.disconnected': true }
return createConnector<Provider, Properties, StorageItem>((config) => ({
id: 'embeddedUniswapWalletConnector',
name: 'Uniswap Embedded Wallet',
type: 'embeddedUniswapWallet',
async setup() {
const provider = await this.getProvider()
provider.on('connect', this.onConnect.bind(this) as Listener)
},
async getProvider() {
return embeddedWalletProvider
},
async connect({ chainId } = {}) {
const { walletAddress, isConnected } = getEmbeddedWalletState()
if (!walletAddress) {
throw new ResourceUnavailableRpcError(new Error('No accounts available'))
}
if (!isConnected) {
throw new ResourceUnavailableRpcError(
new Error(
'Embedded wallet isConnected state must be updated to true before attempting connection. See useSignInWithPasskey hook.',
),
)
}
const provider = await this.getProvider()
let accounts: readonly `0x${string}`[] = []
accounts = await this.getAccounts().catch(() => [])
try {
provider.removeListener('connect', this.onConnect.bind(this) as Listener)
provider.on('accountsChanged', this.onAccountsChanged.bind(this) as Listener)
provider.on('chainChanged', this.onChainChanged as Listener)
provider.on('disconnect', this.onDisconnect.bind(this) as Listener)
// Switch to chain if provided
let currentChainId = (await this.getChainId()) as number
if (chainId && currentChainId !== chainId) {
const chain = await this.switchChain!({ chainId }).catch((error) => {
if (error.code === UserRejectedRequestError.code) {
throw error
}
return { id: currentChainId }
})
currentChainId = chain.id
}
await config.storage?.removeItem('embeddedUniswapWallet.disconnected')
if (accounts.length === 0) {
throw new ResourceUnavailableRpcError(new Error('No accounts available'))
}
return { accounts, chainId: currentChainId }
} catch (err) {
const error = err as RpcError
if (error.code === UserRejectedRequestError.code) {
throw new UserRejectedRequestError(error)
}
if (error.code === ResourceUnavailableRpcError.code) {
throw new ResourceUnavailableRpcError(error)
}
throw error
}
},
async disconnect() {
const provider = await this.getProvider()
provider.removeListener('accountsChanged', this.onAccountsChanged.bind(this))
provider.removeListener('chainChanged', this.onChainChanged)
provider.removeListener('disconnect', this.onDisconnect.bind(this))
provider.on('connect', this.onConnect.bind(this) as Listener)
config.storage?.setItem('embeddedUniswapWallet.disconnected', true)
},
async getAccounts() {
const provider = await this.getProvider()
const accounts = (await provider.request({
method: 'eth_accounts',
})) as string[]
return accounts.map((x) => getAddress(x))
},
async getChainId() {
const provider = await this.getProvider()
const chainId = provider.getChainId()
return Number(chainId)
},
async isAuthorized() {
try {
const accounts = await this.getAccounts()
return !!accounts.length
} catch {
return false
}
},
async switchChain({ chainId }) {
const provider = await this.getProvider()
const chain = config.chains.find((x) => x.id === chainId)
if (!chain) {
throw new SwitchChainError(new ChainNotConfiguredError())
}
try {
await provider.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId }],
})
return chain
} catch (err) {
const error = err as RpcError
if (error.code === UserRejectedRequestError.code) {
throw new UserRejectedRequestError(error)
}
throw new SwitchChainError(error)
}
},
async onAccountsChanged(accounts) {
// Disconnect if there are no accounts
if (accounts.length === 0) {
this.onDisconnect()
}
// Connect if emitter is listening for connect event (e.g. is disconnected and connects through wallet interface)
else if (config.emitter.listenerCount('connect')) {
const chainId = (await this.getChainId()).toString()
this.onConnect({ chainId })
await config.storage?.removeItem('embeddedUniswapWallet.disconnected')
}
// Regular change event
else {
config.emitter.emit('change', {
accounts: accounts.map((x) => getAddress(x)),
})
}
},
onChainChanged(chain) {
const chainId = Number(chain)
config.emitter.emit('change', { chainId })
},
async onConnect(connectInfo) {
const accounts = await this.getAccounts()
if (accounts.length === 0) {
return
}
const chainId = Number(connectInfo.chainId)
config.emitter.emit('connect', { accounts, chainId })
const provider = await this.getProvider()
provider.removeListener('connect', this.onConnect.bind(this))
provider.on('accountsChanged', this.onAccountsChanged.bind(this) as any)
provider.on('chainChanged', this.onChainChanged as any)
provider.on('disconnect', this.onDisconnect.bind(this) as any)
},
// this can accept an `error` argument if needed.
async onDisconnect() {
const provider = await this.getProvider()
// No need to remove 'metaMaskSDK.disconnected' from storage because `onDisconnect` is typically
// only called when the wallet is disconnected through the wallet's interface, meaning the wallet
// actually disconnected and we don't need to simulate it.
config.emitter.emit('disconnect')
provider.removeListener('accountsChanged', this.onAccountsChanged.bind(this))
provider.removeListener('chainChanged', this.onChainChanged)
provider.removeListener('disconnect', this.onDisconnect.bind(this))
provider.on('connect', this.onConnect.bind(this) as any)
},
}))
}