-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathWalletConnectScreen.tsx
299 lines (265 loc) · 8.3 KB
/
WalletConnectScreen.tsx
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import WalletConnect from "@walletconnect/client";
import * as React from "react";
import { StyleSheet, View } from "react-native";
import { LogView, useLogViewData } from "../components/LogView";
import { usePrompt } from "../components/Prompt";
import { ScrollContainer } from "../components/ScrollContainer";
import type { TWalletConnectScreenProps } from "../navigation";
import { useWalletQuery } from "../turnkey/TurnkeyQuery";
import { useTurnkeyWalletContext } from "../turnkey/TurnkeyWalletContext";
import {
getEtherscanUrl,
getNetworkDisplayValue,
truncateAddress,
} from "../utils";
// See https://github.com/WalletConnect/walletconnect-monorepo/blob/c94c1d608e75ef7f0e77572a8627d9412ade24c3/packages/helpers/utils/src/constants.ts
const WALLETCONNECT_RESERVED_EVENTS = [
"session_request",
"session_update",
"exchange_key",
"connect",
"disconnect",
"display_uri",
"modal_closed",
"transport_open",
"transport_close",
"transport_error",
] as const;
const WALLETCONNECT_SIGNING_METHODS = [
"eth_sendTransaction",
"eth_signTransaction",
"eth_sign",
"eth_signTypedData",
"eth_signTypedData_v1",
"eth_signTypedData_v2",
"eth_signTypedData_v3",
"eth_signTypedData_v4",
"personal_sign",
"wallet_addEthereumChain",
"wallet_switchEthereumChain",
"wallet_getPermissions",
"wallet_requestPermissions",
"wallet_registerOnboarding",
"wallet_watchAsset",
"wallet_scanQRCode",
] as const;
const WALLETCONNECT_STATE_METHODS = [
"eth_accounts",
"eth_chainId",
"net_version",
] as const;
export function WalletConnectScreen(props: TWalletConnectScreenProps) {
const { uri } = props.route.params;
const { logList } = useWalletConnectSubscription({ uri });
return (
<ScrollContainer>
<View style={styles.root}>
<LogView logList={logList} />
</View>
</ScrollContainer>
);
}
// Get a (QR) link from https://example.walletconnect.org/
//
// More references:
// - https://docs.walletconnect.com/1.0/quick-start/wallets/react-native
// - https://docs.walletconnect.com/client-api
// - https://github.com/WalletConnect/walletconnect-monorepo/tree/v1.0/packages/clients/
function useWalletConnectSubscription(input: { uri: string }) {
const { uri } = input;
const { showPrompt } = usePrompt();
const walletQuery = useWalletQuery();
const { eip1193, network } = useTurnkeyWalletContext();
const { logList, appendLog } = useLogViewData();
const address = walletQuery.data?.address;
const chainId = walletQuery.data?.chainId;
const lastConnectedUri = React.useRef<string | null>(null);
React.useEffect(() => {
if (address == null || chainId == null || eip1193 == null) {
return;
}
if (lastConnectedUri.current === uri) {
return;
}
lastConnectedUri.current = uri;
try {
appendLog({
label: "WalletConnect",
data: `Establishing connection (from ${truncateAddress(
address
)} on ${getNetworkDisplayValue(network)})`,
});
const connector = new WalletConnect({
uri,
});
[
...WALLETCONNECT_RESERVED_EVENTS,
...WALLETCONNECT_SIGNING_METHODS,
...WALLETCONNECT_STATE_METHODS,
].forEach((eventName) => {
connector.on(eventName, async (error, payload) => {
const label = "`" + eventName + "`";
if (error != null) {
appendLog({
label,
data: `Error: ${error.message}`,
});
return;
}
switch (eventName) {
case "session_request": {
appendLog({
label,
data: `Handshaking with ${
payload?.params?.[0]?.peerMeta?.url ?? "<unknown>"
}`,
});
const userInput = await showPrompt({
title: "WalletConnect Session Request",
message: `Would you like to connect to the WalletConnect session?`,
actionList: [
{
id: "APPROVE",
title: "Approve",
type: "default",
},
{
id: "REJECT",
title: "Reject",
type: "cancel",
},
],
});
if (userInput.id === "APPROVE") {
appendLog({
label: "User action",
data: "Approved connection request",
});
connector.approveSession({
chainId,
accounts: [address],
});
} else if (userInput.id === "REJECT") {
appendLog({
label: "User action",
data: "Rejected connection request",
});
connector.rejectSession({
message: "User rejected request",
});
}
return;
}
case "connect": {
appendLog({
label,
data: `Connected to ${
payload?.params?.[0]?.peerMeta?.url ?? "<unknown>"
}`,
});
return;
}
case "disconnect": {
appendLog({
label,
data: "Session disconnected",
});
return;
}
case "eth_sendTransaction": {
appendLog({
label,
data: payload,
});
const userInput = await showPrompt({
title: `Request: ${label}`,
message: `Would you like to approve this request?`,
actionList: [
{
id: "APPROVE",
title: "Approve",
type: "default",
},
{
id: "REJECT",
title: "Reject",
type: "cancel",
},
],
});
if (userInput.id === "APPROVE") {
appendLog({
label: "User action",
data: "Approved request",
});
const { method, params } = payload;
let txHash: string;
try {
txHash = await eip1193.send(
method,
cleanUpSendTxParams(params)
);
} catch (error) {
appendLog({
label,
data: `Error: ${(error as Error).message}`,
});
return;
}
connector.approveRequest({
id: payload.id,
result: txHash,
});
const etherscanLink = getEtherscanUrl(`/tx/${txHash}`, network);
appendLog({
label: "Transaction sent",
data: etherscanLink,
});
} else if (userInput.id === "REJECT") {
appendLog({
label: "User action",
data: "Rejected request",
});
connector.rejectRequest({
id: payload.id,
error: {
message: "User rejected request",
},
});
}
return;
}
}
appendLog({
label: label + " (unimplemented)",
data: payload,
});
});
});
} catch (error) {
appendLog({
label: "Failed to connect",
data: `Error: ${(error as Error).message}`,
});
}
}, [uri, showPrompt, address, chainId, appendLog, network, eip1193]);
return {
logList,
};
}
const styles = StyleSheet.create({
root: {
flex: 1,
},
});
// Can't pass `gas` and `from` as-is to `Eip1193Bridge`
// See https://github.com/ethers-io/ethers.js/issues/1683
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function cleanUpSendTxParams(params: any): any {
const clonedParam = { ...params[0] };
// TODO: Ask user for gas input
delete clonedParam.gas;
// TODO: verify that `from` matches the wallet's address
delete clonedParam.from;
return [clonedParam];
}