-
Notifications
You must be signed in to change notification settings - Fork 741
/
Copy pathindex.ts
438 lines (406 loc) · 13.3 KB
/
index.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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
import WalletConnect from "@walletconnect/client";
import QRCodeModal from "@walletconnect/qrcode-modal";
import HttpConnection from "@walletconnect/http-connection";
import { payloadId, signingMethods, parsePersonalSign, getRpcUrl } from "@walletconnect/utils";
import {
IRPCMap,
IConnector,
IJsonRpcRequest,
IJsonRpcResponseSuccess,
IWalletConnectProviderOptions,
IQRCodeModalOptions,
} from "@walletconnect/types";
const ProviderEngine = require("web3-provider-engine");
const CacheSubprovider = require("web3-provider-engine/subproviders/cache");
const FixtureSubprovider = require("web3-provider-engine/subproviders/fixture");
const FilterSubprovider = require("web3-provider-engine/subproviders/filters");
const HookedWalletSubprovider = require("web3-provider-engine/subproviders/hooked-wallet");
const NonceSubprovider = require("web3-provider-engine/subproviders/nonce-tracker");
const SubscriptionsSubprovider = require("web3-provider-engine/subproviders/subscriptions");
class WalletConnectProvider extends ProviderEngine {
public bridge = "https://bridge.walletconnect.org";
public qrcode = true;
public qrcodeModal = QRCodeModal;
public qrcodeModalOptions: IQRCodeModalOptions | undefined = undefined;
public rpc: IRPCMap | null = null;
public infuraId = "";
public http: HttpConnection | null = null;
public wc: IConnector;
public isConnecting = false;
public connected = false;
public connectCallbacks: any[] = [];
public accounts: string[] = [];
public chainId = 1;
public rpcUrl = "";
constructor(opts: IWalletConnectProviderOptions) {
super({ pollingInterval: opts.pollingInterval || 8000 });
this.bridge = opts.connector
? opts.connector.bridge
: opts.bridge || "https://bridge.walletconnect.org";
this.qrcode = typeof opts.qrcode === "undefined" || opts.qrcode !== false;
this.qrcodeModal = opts.qrcodeModal || this.qrcodeModal;
this.qrcodeModalOptions = opts.qrcodeModalOptions;
this.wc =
opts.connector ||
new WalletConnect({
bridge: this.bridge,
qrcodeModal: this.qrcode ? this.qrcodeModal : undefined,
qrcodeModalOptions: this.qrcodeModalOptions,
storageId: opts?.storageId,
signingMethods: opts?.signingMethods,
clientMeta: opts?.clientMeta,
});
this.rpc = opts.rpc || null;
if (
!this.rpc &&
(!opts.infuraId || typeof opts.infuraId !== "string" || !opts.infuraId.trim())
) {
throw new Error("Missing one of the required parameters: rpc or infuraId");
}
this.infuraId = opts.infuraId || "";
this.chainId = opts?.chainId || this.chainId;
this.initialize();
}
get isWalletConnect() {
return true;
}
get connector() {
return this.wc;
}
get walletMeta() {
return this.wc.peerMeta;
}
// Connect with a wallet and return the addresses of all available
// accounts.
enable = async (): Promise<string[]> => {
const wc = await this.getWalletConnector();
if (wc) {
this.start();
this.subscribeWalletConnector();
return wc.accounts;
} else {
throw new Error("Failed to connect to WalleConnect");
}
};
request = async (payload: any): Promise<any> => {
return this.send(payload);
};
send = async (payload: any, callback?: any): Promise<any> => {
// Web3 1.0 beta.38 (and above) calls `send` with method and parameters
if (typeof payload === "string") {
const method = payload;
let params = callback;
// maintaining the previous behavior where personal_sign could be non-hex string
if (method === "personal_sign") {
params = parsePersonalSign(params);
}
return this.sendAsyncPromise(method, params);
}
// ensure payload includes id and jsonrpc
payload = { id: payloadId(), jsonrpc: "2.0", ...payload };
// maintaining the previous behavior where personal_sign could be non-hex string
if (payload.method === "personal_sign") {
payload.params = parsePersonalSign(payload.params);
}
// Web3 1.0 beta.37 (and below) uses `send` with a callback for async queries
if (callback) {
this.sendAsync(payload, callback);
return;
}
return this.sendAsyncPromise(payload.method, payload.params);
};
onConnect = (callback: any) => {
this.connectCallbacks.push(callback);
};
triggerConnect = (result: any) => {
if (this.connectCallbacks && this.connectCallbacks.length) {
this.connectCallbacks.forEach(callback => callback(result));
}
};
async disconnect() {
this.close();
}
async close() {
const wc = await this.getWalletConnector({ disableSessionCreation: true });
await wc.killSession();
await this.onDisconnect();
}
async handleRequest(payload: any) {
try {
let response;
let result: any = null;
const wc = await this.getWalletConnector();
switch (payload.method) {
case "wc_killSession":
await this.close();
result = null;
break;
case "eth_accounts":
result = wc.accounts;
break;
case "eth_coinbase":
result = wc.accounts[0];
break;
case "eth_chainId":
result = wc.chainId;
break;
case "net_version":
result = wc.chainId;
break;
case "eth_uninstallFilter":
this.sendAsync(payload, (_: any) => _);
result = true;
break;
default:
response = await this.handleOtherRequests(payload);
}
if (response) {
return response;
}
return this.formatResponse(payload, result);
} catch (error) {
this.emit("error", error);
throw error;
}
}
async handleOtherRequests(payload: any): Promise<IJsonRpcResponseSuccess> {
if (!signingMethods.includes(payload.method) && payload.method.startsWith("eth_")) {
return this.handleReadRequests(payload);
}
const wc = await this.getWalletConnector();
const result = await wc.sendCustomRequest(payload);
return this.formatResponse(payload, result);
}
async handleReadRequests(payload: any): Promise<IJsonRpcResponseSuccess> {
if (!this.http) {
const error = new Error("HTTP Connection not available");
this.emit("error", error);
throw error;
}
return this.http.send(payload);
}
formatResponse(payload: any, result: any) {
return {
id: payload.id,
jsonrpc: payload.jsonrpc,
result: result,
};
}
// disableSessionCreation - if true, getWalletConnector won't try to create a new session
// in case the connector is disconnected
getWalletConnector(opts: { disableSessionCreation?: boolean } = {}): Promise<IConnector> {
const { disableSessionCreation = false } = opts;
return new Promise((resolve, reject) => {
const wc = this.wc;
if (this.isConnecting) {
this.onConnect((x: any) => resolve(x));
} else if (!wc.connected && !disableSessionCreation) {
this.isConnecting = true;
wc.on("modal_closed", () => {
reject(new Error("User closed modal"));
});
wc.createSession({ chainId: this.chainId })
.then(() => {
wc.on("connect", (error, payload) => {
if (error) {
this.isConnecting = false;
return reject(error);
}
this.isConnecting = false;
this.connected = true;
if (payload) {
// Handle session update
this.updateState(payload.params[0]);
}
// Emit connect event
this.emit("connect");
this.triggerConnect(wc);
resolve(wc);
});
})
.catch(error => {
this.isConnecting = false;
reject(error);
});
} else {
if (!this.connected) {
this.connected = true;
this.updateState(wc.session);
}
resolve(wc);
}
});
}
async subscribeWalletConnector() {
const wc = await this.getWalletConnector();
wc.on("disconnect", error => {
if (error) {
this.emit("error", error);
return;
}
this.onDisconnect();
});
wc.on("session_update", (error, payload) => {
if (error) {
this.emit("error", error);
return;
}
// Handle session update
this.updateState(payload.params[0]);
});
}
async onDisconnect() {
// tslint:disable-next-line:await-promise
await this.stop();
this.emit("close", 1000, "Connection closed");
this.emit("disconnect", 1000, "Connection disconnected");
this.connected = false;
}
async updateState(sessionParams: any) {
const { accounts, chainId, networkId, rpcUrl } = sessionParams;
// Check if accounts changed and trigger event
if (!this.accounts || (accounts && this.accounts !== accounts)) {
this.accounts = accounts;
this.emit("accountsChanged", accounts);
}
// Check if chainId changed and trigger event
if (!this.chainId || (chainId && this.chainId !== chainId)) {
this.chainId = chainId;
this.emit("chainChanged", chainId);
}
// Check if networkId changed and trigger event
if (!this.networkId || (networkId && this.networkId !== networkId)) {
this.networkId = networkId;
this.emit("networkChanged", networkId);
}
// Handle rpcUrl update
this.updateRpcUrl(this.chainId, rpcUrl || "");
}
updateRpcUrl(chainId: number, rpcUrl: string | undefined = "") {
const rpc = { infuraId: this.infuraId, custom: this.rpc || undefined };
rpcUrl = rpcUrl || getRpcUrl(chainId, rpc);
if (rpcUrl) {
this.rpcUrl = rpcUrl;
this.updateHttpConnection();
} else {
this.emit("error", new Error(`No RPC Url available for chainId: ${chainId}`));
}
}
updateHttpConnection() {
if (this.rpcUrl) {
this.http = new HttpConnection(this.rpcUrl);
this.http.on("payload", payload => this.emit("payload", payload));
this.http.on("error", error => this.emit("error", error));
}
}
sendAsyncPromise(method: string, params: any): Promise<any> {
return new Promise((resolve, reject) => {
this.sendAsync(
{
id: payloadId(),
jsonrpc: "2.0",
method,
params: params || [],
},
(error: any, response: any) => {
if (error) {
reject(error);
return;
}
resolve(response.result);
},
);
});
}
private initialize() {
this.updateRpcUrl(this.chainId);
this.addProvider(
new FixtureSubprovider({
eth_hashrate: "0x00",
eth_mining: false,
eth_syncing: true,
net_listening: true,
web3_clientVersion: `WalletConnect/v1.x.x/javascript`,
}),
);
this.addProvider(new CacheSubprovider());
this.addProvider(new SubscriptionsSubprovider());
this.addProvider(new FilterSubprovider());
this.addProvider(new NonceSubprovider());
this.addProvider(new HookedWalletSubprovider(this.configWallet()));
this.addProvider({
handleRequest: async (payload: IJsonRpcRequest, next: any, end: any) => {
try {
const { error, result } = await this.handleRequest(payload);
end(error, result);
} catch (error) {
end(error);
}
},
setEngine: (_: any) => _,
});
}
private configWallet() {
return {
getAccounts: async (cb: any) => {
try {
const wc = await this.getWalletConnector();
const accounts = wc.accounts;
if (accounts && accounts.length) {
cb(null, accounts);
} else {
cb(new Error("Failed to get accounts"));
}
} catch (error) {
cb(error);
}
},
processMessage: async (msgParams: { from: string; data: string }, cb: any) => {
try {
const wc = await this.getWalletConnector();
const result = await wc.signMessage([msgParams.from, msgParams.data]);
cb(null, result);
} catch (error) {
cb(error);
}
},
processPersonalMessage: async (msgParams: { from: string; data: string }, cb: any) => {
try {
const wc = await this.getWalletConnector();
const result = await wc.signPersonalMessage([msgParams.data, msgParams.from]);
cb(null, result);
} catch (error) {
cb(error);
}
},
processSignTransaction: async (txParams: any, cb: any) => {
try {
const wc = await this.getWalletConnector();
const result = await wc.signTransaction(txParams);
cb(null, result);
} catch (error) {
cb(error);
}
},
processTransaction: async (txParams: any, cb: any) => {
try {
const wc = await this.getWalletConnector();
const result = await wc.sendTransaction(txParams);
cb(null, result);
} catch (error) {
cb(error);
}
},
processTypedMessage: async (msgParams: { from: string; data: string }, cb: any) => {
try {
const wc = await this.getWalletConnector();
const result = await wc.signTypedData([msgParams.from, msgParams.data]);
cb(null, result);
} catch (error) {
cb(error);
}
},
};
}
}
export default WalletConnectProvider;