-
Notifications
You must be signed in to change notification settings - Fork 579
/
index.tsx
1073 lines (926 loc) Β· 33.9 KB
/
index.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
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import React from 'react';
import { InteractionManager } from 'react-native';
import { SignClientTypes, SessionTypes } from '@walletconnect/types';
import { getSdkError, parseUri, buildApprovedNamespaces } from '@walletconnect/utils';
import { WC_PROJECT_ID } from 'react-native-dotenv';
import Minimizer from 'react-native-minimizer';
import { isAddress, getAddress } from '@ethersproject/address';
import { formatJsonRpcResult, formatJsonRpcError } from '@json-rpc-tools/utils';
import { gretch } from 'gretchen';
import messaging from '@react-native-firebase/messaging';
import { Core } from '@walletconnect/core';
import { Web3Wallet, Web3WalletTypes } from '@walletconnect/web3wallet';
import { isHexString } from '@ethersproject/bytes';
import { toUtf8String } from '@ethersproject/strings';
import { logger, RainbowError } from '@/logger';
import { WalletconnectApprovalSheetRouteParams } from '@/redux/walletconnect';
import Navigation, { getActiveRoute } from '@/navigation/Navigation';
import Routes from '@/navigation/routesNames';
import { analyticsV2 as analytics } from '@/analytics';
import { maybeSignUri } from '@/handlers/imgix';
import Alert from '@/components/alerts/Alert';
import * as lang from '@/languages';
import store from '@/redux/store';
import { findWalletWithAccount } from '@/helpers/findWalletWithAccount';
import WalletTypes from '@/helpers/walletTypes';
import ethereumUtils, { getNetworkFromChainId } from '@/utils/ethereumUtils';
import { getRequestDisplayDetails } from '@/parsers/requests';
import { WalletconnectRequestData, REQUESTS_UPDATE_REQUESTS_TO_APPROVE, removeRequest } from '@/redux/requests';
import { saveLocalRequests } from '@/handlers/localstorage/walletconnectRequests';
import { events } from '@/handlers/appEvents';
import { getFCMToken } from '@/notifications/tokens';
import { IS_DEV, IS_ANDROID, IS_IOS } from '@/env';
import { loadWallet } from '@/model/wallet';
import * as portal from '@/screens/Portal';
import * as explain from '@/screens/Explain';
import { Box } from '@/design-system';
import { AuthRequestAuthenticateSignature, AuthRequestResponseErrorReason, RPCMethod, RPCPayload } from '@/walletConnect/types';
import { AuthRequest } from '@/walletConnect/sheets/AuthRequest';
import { getProviderForNetwork } from '@/handlers/web3';
import { RainbowNetworks } from '@/networks';
import { uniq } from 'lodash';
import { fetchDappMetadata } from '@/resources/metadata/dapp';
import { DAppStatus } from '@/graphql/__generated__/metadata';
import { handleWalletConnectRequest } from '@/utils/requestNavigationHandlers';
import { PerformanceMetrics } from '@/performance/tracking/types/PerformanceMetrics';
import { PerformanceTracking } from '@/performance/tracking';
const SUPPORTED_EVM_CHAIN_IDS = RainbowNetworks.filter(({ features }) => features.walletconnect).map(({ id }) => id);
const SUPPORTED_SESSION_EVENTS = ['chainChanged', 'accountsChanged'];
const T = lang.l.walletconnect;
/**
* Indicates that the app should redirect or go back after the next action
* completes
*
* This is a hack to get around having to muddy the scopes of our event
* listeners. BE CAREFUL WITH THIS.
*/
let hasDeeplinkPendingRedirect = false;
/**
* Set `hasDeeplinkPendingRedirect` to a boolean, indicating that the app
* should redirect or go back after the next action completes
*
* This is a hack to get around having to muddy the scopes of our event
* listeners. BE CAREFUL WITH THIS.
*/
export function setHasPendingDeeplinkPendingRedirect(value: boolean) {
logger.info(`setHasPendingDeeplinkPendingRedirect`, { value });
hasDeeplinkPendingRedirect = value;
}
/**
* Called in case we're on mobile and have a pending redirect
*/
export function maybeGoBackAndClearHasPendingRedirect({ delay = 0 }: { delay?: number } = {}) {
if (hasDeeplinkPendingRedirect) {
InteractionManager.runAfterInteractions(() => {
setTimeout(() => {
setHasPendingDeeplinkPendingRedirect(false);
if (!IS_IOS) {
Minimizer.goBack();
}
}, delay);
});
}
}
/**
* MAY BE UNDEFINED if WC v2 hasn't been instantiated yet
*/
let syncWeb3WalletClient: Awaited<ReturnType<(typeof Web3Wallet)['init']>> | undefined;
const walletConnectCore = new Core({ projectId: WC_PROJECT_ID });
const web3WalletClient = Web3Wallet.init({
core: walletConnectCore,
metadata: {
name: 'π Rainbow',
description: 'Rainbow makes exploring Ethereum fun and accessible π',
url: 'https://rainbow.me',
icons: ['https://avatars2.githubusercontent.com/u/48327834?s=200&v=4'],
redirect: {
native: 'rainbow://wc',
universal: 'https://rnbwapp.com/wc',
},
},
});
let initPromise: ReturnType<(typeof Web3Wallet)['init']> | null = null;
// this function ensures we only initialize the client once
export async function getWeb3WalletClient() {
if (!syncWeb3WalletClient) {
if (!initPromise) {
initPromise = web3WalletClient.then(client => {
syncWeb3WalletClient = client;
return client;
});
}
// Wait for the initialization promise to resolve
return initPromise;
} else {
return syncWeb3WalletClient;
}
}
/**
* For RPC requests that have [address, message] tuples (order may change),
* return { address, message } and JSON.parse the value if it's from a typed
* data request
*/
export function parseRPCParams({ method, params }: RPCPayload): {
address?: string;
message?: string;
} {
switch (method) {
case RPCMethod.PersonalSign: {
const [address, message] = params.sort(a => (isAddress(a) ? -1 : 1));
const isHex = isHexString(message);
let decodedMessage = message;
try {
if (isHex) {
decodedMessage = toUtf8String(message);
}
} catch (err) {
logger.debug('WC v2: parsing RPC params unable to decode hex message to UTF8 string', {}, logger.DebugContext.walletconnect);
}
return {
address: getAddress(address),
message: decodedMessage,
};
}
/**
* @see https://eips.ethereum.org/EIPS/eip-712#specification-of-the-eth_signtypeddata-json-rpc
* @see https://docs.metamask.io/guide/signing-data.html#a-brief-history
*/
case RPCMethod.SignTypedData:
case RPCMethod.SignTypedDataV1:
case RPCMethod.SignTypedDataV3:
case RPCMethod.SignTypedDataV4: {
const [address, message] = params;
return {
address: getAddress(address),
message: JSON.parse(message),
};
}
case RPCMethod.SendTransaction: {
const [tx] = params;
return {
address: getAddress(tx.from),
};
}
default:
return {};
}
}
/**
* Better signature for this type of function
*
* @see https://docs.walletconnect.com/2.0/web/web3wallet/wallet-usage#-namespaces-builder-util
*/
export function getApprovedNamespaces(props: Parameters<typeof buildApprovedNamespaces>[0]):
| {
success: true;
result: ReturnType<typeof buildApprovedNamespaces>;
error: undefined;
}
| {
success: false;
result: undefined;
error: Error;
} {
try {
const namespaces = buildApprovedNamespaces(props);
if (!namespaces.eip155.accounts.length) {
return {
success: false,
result: undefined,
error: new Error(lang.t(T.errors.no_accounts_found)),
};
}
return {
success: true,
result: namespaces,
error: undefined,
};
} catch (e: any) {
logger.error(new RainbowError(`WC v2: buildApprovedNamespaces threw an error`), {
message: e.toString(),
});
return {
success: false,
result: undefined,
error: e,
};
}
}
const SUPPORTED_SIGNING_METHODS = [
RPCMethod.Sign,
RPCMethod.PersonalSign,
RPCMethod.SignTypedData,
RPCMethod.SignTypedDataV1,
RPCMethod.SignTypedDataV3,
RPCMethod.SignTypedDataV4,
];
const SUPPORTED_TRANSACTION_METHODS = [RPCMethod.SendTransaction];
export function isSupportedSigningMethod(method: RPCMethod) {
return SUPPORTED_SIGNING_METHODS.includes(method);
}
export function isSupportedTransactionMethod(method: RPCMethod) {
return SUPPORTED_TRANSACTION_METHODS.includes(method);
}
export function isSupportedMethod(method: RPCMethod) {
return isSupportedSigningMethod(method) || isSupportedTransactionMethod(method);
}
export function isSupportedChain(chainId: number) {
return !!RainbowNetworks.find(({ id, features }) => id === chainId && features.walletconnect);
}
/**
* Navigates to `ExplainSheet` by way of `WalletConnectApprovalSheet`, and
* shows the text configured by the `reason` string, which is a key of the
* `explainers` object in `ExplainSheet`
*/
function showErrorSheet({
title,
body,
cta,
onClose,
sheetHeight,
}: {
title: string;
body: string;
cta?: string;
onClose?: () => void;
sheetHeight?: number;
}) {
explain.open(
() => (
<>
<explain.Title>{title}</explain.Title>
<explain.Body>{body}</explain.Body>
<Box paddingTop="8px">
<explain.Button
label={cta || lang.t(T.errors.go_back)}
onPress={() => {
explain.close();
onClose?.();
}}
/>
</Box>
</>
),
{ sheetHeight }
);
}
async function rejectProposal({
proposal,
reason,
}: {
proposal: Web3WalletTypes.SessionProposal;
reason: Parameters<typeof getSdkError>[0];
}) {
logger.warn(`WC v2: session approval denied`, {
reason,
proposal,
});
const client = await getWeb3WalletClient();
const { id, proposer } = proposal.params;
await client.rejectSession({ id, reason: getSdkError(reason) });
analytics.track(analytics.event.wcNewSessionRejected, {
dappName: proposer.metadata.name,
dappUrl: proposer.metadata.url,
});
}
export async function pair({ uri, connector }: { uri: string; connector?: string }) {
logger.debug(`WC v2: pair`, { uri }, logger.DebugContext.walletconnect);
/**
* Make sure this is cleared if we get multiple pairings in rapid succession
*/
const { topic, ...rest } = parseUri(uri);
const client = await getWeb3WalletClient();
logger.debug(`WC v2: pair: parsed uri`, { topic, rest });
// listen for THIS topic pairing, and clear timeout if received
function handler(proposal: Web3WalletTypes.SessionProposal | Web3WalletTypes.AuthRequest) {
logger.debug(`WC v2: pair: handler`, { proposal });
const { metadata } =
(proposal as Web3WalletTypes.SessionProposal).params.proposer || (proposal as Web3WalletTypes.AuthRequest).params.requester;
analytics.track(analytics.event.wcNewPairing, {
dappName: metadata.name,
dappUrl: metadata.url,
connector,
});
}
// CAN get fired on subsequent pairs, so need to make sure we clean up
client.on('session_proposal', handler);
client.on('auth_request', handler);
// init pairing
await client.core.pairing.pair({ uri });
}
export async function initListeners() {
PerformanceTracking.startMeasuring(PerformanceMetrics.initializeWalletconnect);
const client = await getWeb3WalletClient();
PerformanceTracking.finishMeasuring(PerformanceMetrics.initializeWalletconnect);
syncWeb3WalletClient = client;
logger.debug(`WC v2: web3WalletClient initialized, initListeners`, {}, logger.DebugContext.walletconnect);
client.on('session_proposal', onSessionProposal);
client.on('session_request', onSessionRequest);
client.on('auth_request', onAuthRequest);
client.on('session_delete', () => {
logger.debug(`WC v2: session_delete`, {}, logger.DebugContext.walletconnect);
setTimeout(() => {
events.emit('walletConnectV2SessionDeleted');
}, 500);
});
try {
const token = await getFCMToken();
if (token) {
const client_id = await client.core.crypto.getClientId();
// initial subscription
await subscribeToEchoServer({ token, client_id });
/**
* Ensure that if the FCM token changes we update the echo server
*/
messaging().onTokenRefresh(async token => {
await subscribeToEchoServer({ token, client_id });
});
} else {
if (!IS_DEV) {
/*
* If we failed to fetch an FCM token, this will fail too. You should
* see these errors increase proportionally if something goes wrong,
* which could be due to network flakiness, SSL server error (has
* happened), etc. Things out of our control.
*/
logger.warn(`WC v2: FCM token not found, push notifications will not be received`);
}
}
} catch (e) {
logger.error(new RainbowError(`WC v2: initListeners failed`), { error: e });
}
}
async function subscribeToEchoServer({ client_id, token }: { client_id: string; token: string }) {
const res = await gretch(`https://wcpush.p.rainbow.me/clients`, {
method: 'POST',
json: {
type: 'FCM',
client_id,
token,
},
}).json();
if (res.error) {
/*
* Most of these appear to be network errors and timeouts. So our backend
* should report these to Datadog, and we can leave this as a warn to
* continue to monitor.
*/
logger.warn(`WC v2: echo server subscription failed`, {
error: res.error,
});
}
}
export async function onSessionProposal(proposal: Web3WalletTypes.SessionProposal) {
try {
logger.debug(`WC v2: session_proposal`, { proposal }, logger.DebugContext.walletconnect);
const verifiedData = proposal.verifyContext.verified;
const receivedTimestamp = Date.now();
const { proposer, requiredNamespaces, optionalNamespaces } = proposal.params;
const requiredChains = requiredNamespaces?.eip155?.chains || [];
const optionalChains = optionalNamespaces?.eip155?.chains || [];
const chains = uniq([...requiredChains, ...optionalChains]);
// we already checked for eip155 namespace above
const chainIds = chains?.map(chain => parseInt(chain.split('eip155:')[1]));
const supportedChainIds = chainIds.filter(isSupportedChain);
const peerMeta = proposer.metadata;
const metadata = await fetchDappMetadata({ url: peerMeta.url, status: true });
const dappName = metadata?.appName || peerMeta.name || lang.t(lang.l.walletconnect.unknown_dapp);
const dappImage = metadata?.appLogo || peerMeta?.icons?.[0];
const routeParams: WalletconnectApprovalSheetRouteParams = {
receivedTimestamp,
meta: {
chainIds: supportedChainIds,
dappName,
dappScheme: 'unused in WC v2', // only used for deeplinks from WC v1
dappUrl: peerMeta.url || lang.t(lang.l.walletconnect.unknown_url),
imageUrl: maybeSignUri(dappImage, { w: 200 }),
peerId: proposer.publicKey,
isWalletConnectV2: true,
},
verifiedData,
timedOut: false,
callback: async (approved, approvedChainId, accountAddress) => {
const client = await getWeb3WalletClient();
const { id, proposer, requiredNamespaces } = proposal.params;
if (approved) {
logger.debug(
`WC v2: session approved`,
{
approved,
approvedChainId,
accountAddress,
},
logger.DebugContext.walletconnect
);
// we only support EVM chains rn
const supportedEvents = requiredNamespaces?.eip155?.events || SUPPORTED_SESSION_EVENTS;
/** @see https://chainagnostic.org/CAIPs/caip-2 */
const caip2ChainIds = SUPPORTED_EVM_CHAIN_IDS.map(id => `eip155:${id}`);
const namespaces = getApprovedNamespaces({
proposal: proposal.params,
supportedNamespaces: {
eip155: {
chains: caip2ChainIds,
methods: [...SUPPORTED_SIGNING_METHODS, ...SUPPORTED_TRANSACTION_METHODS],
events: supportedEvents,
accounts: caip2ChainIds.map(id => `${id}:${accountAddress}`),
},
},
});
logger.debug(`WC v2: session approved namespaces`, { namespaces }, logger.DebugContext.walletconnect);
try {
if (namespaces.success) {
/**
* This is equivalent handling of setPendingRequest and
* walletConnectApproveSession, since setPendingRequest is only used
* within the /redux/walletconnect handlers
*
* WC v2 stores existing _pairings_ itself, so we don't need to persist
* ourselves
*/
await client.approveSession({
id,
namespaces: namespaces.result,
});
// let the ConnectedDappsSheet know we've got a new one
events.emit('walletConnectV2SessionCreated');
logger.debug(`WC v2: session created`, {}, logger.DebugContext.walletconnect);
analytics.track(analytics.event.wcNewSessionApproved, {
dappName: proposer.metadata.name,
dappUrl: proposer.metadata.url,
});
maybeGoBackAndClearHasPendingRedirect();
if (IS_IOS) {
Navigation.handleAction(Routes.WALLET_CONNECT_REDIRECT_SHEET, {
type: 'connect',
});
}
} else {
await rejectProposal({
proposal,
reason: 'INVALID_SESSION_SETTLE_REQUEST',
});
showErrorSheet({
title: lang.t(T.errors.generic_title),
body: `${lang.t(T.errors.namespaces_invalid)} \n \n ${namespaces.error.message}`,
sheetHeight: 400,
onClose: maybeGoBackAndClearHasPendingRedirect,
});
}
} catch (e) {
setHasPendingDeeplinkPendingRedirect(false);
Alert({
buttons: [
{
style: 'cancel',
text: lang.t(lang.l.walletconnect.go_back),
},
],
message: lang.t(lang.l.walletconnect.failed_to_connect_to, {
appName: dappName,
}),
title: lang.t(lang.l.walletconnect.connection_failed),
});
logger.error(new RainbowError(`WC v2: session approval failed`), {
error: (e as Error).message,
});
}
} else if (!approved) {
await rejectProposal({ proposal, reason: 'USER_REJECTED' });
}
},
};
/**
* We might see this at any point in the app, so only use `replace`
* sometimes if the user is already looking at the approval sheet.
*/
Navigation.handleAction(
Routes.WALLET_CONNECT_APPROVAL_SHEET,
routeParams,
getActiveRoute()?.name === Routes.WALLET_CONNECT_APPROVAL_SHEET
);
} catch (error) {
logger.error(
new RainbowError(`WC v2: session request catch all`, {
...(error as Error),
})
);
}
}
// For WC v2
export async function onSessionRequest(event: SignClientTypes.EventArguments['session_request']) {
setHasPendingDeeplinkPendingRedirect(true);
const client = await getWeb3WalletClient();
logger.debug(`WC v2: session_request`, {}, logger.DebugContext.walletconnect);
const { id, topic } = event;
const { method, params } = event.params.request;
logger.debug(`WC v2: session_request method`, { method, params }, logger.DebugContext.walletconnect);
// we allow eth sign for connections but we dont want to support actual singing
if (method === RPCMethod.Sign) {
await client.respondSessionRequest({
topic,
response: formatJsonRpcError(id, `Rainbow does not support legacy eth_sign`),
});
showErrorSheet({
title: lang.t(T.errors.generic_title),
body: lang.t(T.errors.eth_sign),
sheetHeight: 270,
onClose: maybeGoBackAndClearHasPendingRedirect,
});
return;
}
if (isSupportedMethod(method as RPCMethod)) {
const isSigningMethod = isSupportedSigningMethod(method as RPCMethod);
const { address, message } = parseRPCParams({
method: method as RPCMethod,
params,
});
if (!address) {
logger.error(new RainbowError('No Address in the RPC Params'));
return;
}
const allWallets = store.getState().wallets.wallets;
logger.debug(`WC v2: session_request method is supported`, { method, params, address, message }, logger.DebugContext.walletconnect);
if (isSigningMethod) {
logger.debug(`WC v2: validating session_request signing method`);
if (!address || !message) {
logger.error(new RainbowError(`WC v2: session_request exited, signing request had no address and/or messsage`), {
address,
message,
});
await client.respondSessionRequest({
topic,
response: formatJsonRpcError(id, `Invalid RPC params`),
});
showErrorSheet({
title: lang.t(T.errors.generic_title),
body: lang.t(T.errors.request_invalid),
sheetHeight: 270,
onClose: maybeGoBackAndClearHasPendingRedirect,
});
return;
}
// for TS only, should never happen
if (!allWallets) {
logger.error(new RainbowError(`WC v2: allWallets is null, this should never happen`));
return;
}
const selectedWallet = findWalletWithAccount(allWallets, address);
const isReadOnly = selectedWallet?.type === WalletTypes.readOnly;
if (!selectedWallet || isReadOnly) {
logger.error(new RainbowError(`WC v2: session_request exited, selectedWallet was falsy or read only`), {
selectedWalletType: selectedWallet?.type,
});
const errorMessageBody = isReadOnly ? lang.t(T.errors.read_only_wallet_on_signing_method) : lang.t(T.errors.generic_error);
await client.respondSessionRequest({
topic,
response: formatJsonRpcError(id, `Wallet is read-only`),
});
showErrorSheet({
title: lang.t(T.errors.generic_title),
body: errorMessageBody,
sheetHeight: 270,
onClose: maybeGoBackAndClearHasPendingRedirect,
});
return;
}
}
const session = Object.values(client.getActiveSessions() || {}).find(s => {
return s.topic === topic;
});
// mostly a TS guard, pry won't happen
if (!session) {
logger.error(new RainbowError(`WC v2: session_request topic was not found`));
await client.respondSessionRequest({
topic,
response: formatJsonRpcError(id, `Session not found`),
});
return;
}
const { nativeCurrency, network } = store.getState().settings;
const chainId = Number(event.params.chainId.split(':')[1]);
logger.debug(`WC v2: getting session for topic`, { session });
logger.debug(`WC v2: handling request`, {}, logger.DebugContext.walletconnect);
const dappNetwork = ethereumUtils.getNetworkFromChainId(chainId);
const displayDetails = getRequestDisplayDetails(event.params.request, nativeCurrency, dappNetwork);
const peerMeta = session.peer.metadata;
const metadata = await fetchDappMetadata({ url: peerMeta.url, status: true });
const dappName = metadata?.appName || peerMeta.name || lang.t(lang.l.walletconnect.unknown_url);
const dappImage = metadata?.appLogo || peerMeta?.icons?.[0];
const request: WalletconnectRequestData = {
clientId: session.topic, // I don't think this is used
peerId: session.topic, // I don't think this is used
requestId: event.id,
dappName,
dappScheme: 'unused in WC v2', // only used for deeplinks from WC v1
dappUrl: peerMeta.url || 'Unknown URL',
displayDetails,
imageUrl: maybeSignUri(dappImage, { w: 200 }),
address,
network: getNetworkFromChainId(chainId),
payload: event.params.request,
walletConnectV2RequestValues: {
sessionRequestEvent: event,
address,
chainId,
onComplete(type: string) {
if (IS_IOS) {
Navigation.handleAction(Routes.WALLET_CONNECT_REDIRECT_SHEET, {
type,
});
}
maybeGoBackAndClearHasPendingRedirect({ delay: 300 });
},
},
};
const { requests: pendingRequests } = store.getState().requests;
if (!pendingRequests[request.requestId]) {
const updatedRequests = {
...pendingRequests,
[request.requestId]: request,
};
store.dispatch({
payload: updatedRequests,
type: REQUESTS_UPDATE_REQUESTS_TO_APPROVE,
});
saveLocalRequests(updatedRequests, address, network);
logger.debug(`WC v2: navigating to CONFIRM_REQUEST sheet`, {}, logger.DebugContext.walletconnect);
handleWalletConnectRequest(request);
analytics.track(analytics.event.wcShowingSigningRequest, {
dappName: request.dappName,
dappUrl: request.dappUrl,
});
}
} else {
logger.error(new RainbowError(`WC v2: received unsupported session_request RPC method`), {
method,
});
try {
await client.respondSessionRequest({
topic,
response: formatJsonRpcError(id, `Method ${method} not supported`),
});
} catch (e) {
logger.error(new RainbowError(`WC v2: error rejecting session_request`), {
error: (e as Error).message,
});
}
showErrorSheet({
title: lang.t(T.errors.generic_title),
body: lang.t(T.errors.request_unsupported_methods),
sheetHeight: 250,
onClose: maybeGoBackAndClearHasPendingRedirect,
});
}
}
/**
* Handles the result created on our confirmation screen and sends it along to the dapp via WC
*/
export async function handleSessionRequestResponse(
{
sessionRequestEvent,
}: {
sessionRequestEvent: SignClientTypes.EventArguments['session_request'];
},
{ result, error }: { result: string | null; error: any }
) {
logger.info(`WC v2: handleSessionRequestResponse`, {
success: Boolean(result),
});
const client = await getWeb3WalletClient();
const { topic, id } = sessionRequestEvent;
if (result) {
const payload = {
topic,
response: formatJsonRpcResult(id, result),
};
logger.debug(`WC v2: handleSessionRequestResponse success`, {}, logger.DebugContext.walletconnect);
await client.respondSessionRequest(payload);
} else {
const payload = {
topic,
response: formatJsonRpcError(id, error),
};
logger.debug(`WC v2: handleSessionRequestResponse reject`, {}, logger.DebugContext.walletconnect);
await client.respondSessionRequest(payload);
}
store.dispatch(removeRequest(sessionRequestEvent.id));
}
export async function onAuthRequest(event: Web3WalletTypes.AuthRequest) {
const client = await getWeb3WalletClient();
logger.debug(`WC v2: auth_request`, { event }, logger.DebugContext.walletconnect);
const authenticate: AuthRequestAuthenticateSignature = async ({ address }) => {
try {
const { wallets } = store.getState().wallets;
const selectedWallet = findWalletWithAccount(wallets!, address);
const isHardwareWallet = selectedWallet?.type === WalletTypes.bluetooth;
const iss = `did:pkh:eip155:1:${address}`;
// exit early if possible
if (selectedWallet?.type === WalletTypes.readOnly) {
await client.respondAuthRequest(
{
id: event.id,
error: {
code: 0,
message: `Wallet is read-only`,
},
},
iss
);
return {
success: false,
reason: AuthRequestResponseErrorReason.ReadOnly,
};
}
/**
* Locally scoped to this `authenticate` function. Simply here to
* encapsulate reused code.
*/
const loadWalletAndSignMessage = async () => {
const provider = await getProviderForNetwork();
const wallet = await loadWallet(address, false, provider);
if (!wallet) {
logger.error(new RainbowError(`WC v2: could not loadWallet to sign auth_request`));
return undefined;
}
const message = client.formatMessage(event.params.cacaoPayload, iss);
// prompt the user to sign the message
return wallet.signMessage(message);
};
// Get signature either directly, or via hardware wallet flow
const signature = await (isHardwareWallet
? new Promise<Awaited<ReturnType<typeof loadWalletAndSignMessage>>>((y, n) => {
Navigation.handleAction(Routes.HARDWARE_WALLET_TX_NAVIGATOR, {
async submit() {
try {
y(loadWalletAndSignMessage());
} catch (e) {
n(e);
}
},
});
})
: loadWalletAndSignMessage());
if (!signature) {
return {
success: false,
reason: AuthRequestResponseErrorReason.Unknown,
};
}
// respond to WC
await client.respondAuthRequest(
{
id: event.id,
signature: {
s: signature,
t: 'eip191',
},
},
iss
);
// only handled on success
maybeGoBackAndClearHasPendingRedirect({ delay: 300 });
return { success: true };
} catch (e: any) {
logger.error(new RainbowError(`WC v2: an unknown error occurred when signing auth_request`), {
message: e.message,
});
return { success: false, reason: AuthRequestResponseErrorReason.Unknown };
}
};
// need to prefetch dapp metadata since portal is static
const url =
// @ts-ignore Web3WalletTypes.AuthRequest type is missing VerifyContext
event?.verifyContext?.verifyUrl || event.params.requester.metadata.url;
const metadata = await fetchDappMetadata({ url, status: true });
const isScam = metadata.status === DAppStatus.Scam;
portal.open(
() =>
AuthRequest({
authenticate,
requesterMeta: event.params.requester.metadata,
// @ts-ignore Web3WalletTypes.AuthRequest type is missing VerifyContext
verifiedData: event?.verifyContext,
}),
{ sheetHeight: IS_ANDROID ? 560 : 520 + (isScam ? 40 : 0) }
);
}
/**
* Returns all active settings in a type-safe manner.
*/
export async function getAllActiveSessions() {
const client = await getWeb3WalletClient();
return Object.values(client?.getActiveSessions() || {}) || [];
}
/**
* Synchronous version of `getAllActiveSessions`. Returns all active settings
* in a type-safe manner.
*/
export function getAllActiveSessionsSync() {
return Object.values(syncWeb3WalletClient?.getActiveSessions() || {}) || [];
}
/**
* Adds an account to an existing session
*/
export async function addAccountToSession(session: SessionTypes.Struct, { address }: { address?: string }) {
try {
const client = await getWeb3WalletClient();
const namespaces: Parameters<typeof client.updateSession>[0]['namespaces'] = {};
for (const [key, value] of Object.entries(session.requiredNamespaces)) {
/**
* The `namespace` that corresponds to the `requiredNamespace` that was
* requested when connecting the session. The `requiredNamespace` does
* not have `accounts`, since it merely requests chains and methods, so
* we need to referernce the connected `namespace` for existing `account`
* values.
*/
const ns = session.namespaces[key];
namespaces[key] = {
accounts: ns.accounts || [],
methods: value.methods,
events: value.events,
};
if (value.chains) {
for (const chain of value.chains) {
const chainId = parseInt(chain.split(`${key}:`)[1]);
const account = `${key}:${chainId}:${address}`;
/*
* Add latest selected accounts to beginning of array so that our
* switch wallet sheet shows that one as selected
*/
if (!namespaces[key].accounts.includes(account)) {
namespaces[key].accounts.unshift(account);
} else {
// remove and re-add to start of array
namespaces[key].accounts.splice(namespaces[key].accounts.indexOf(account), 1);
namespaces[key].accounts.unshift(account);
}
}
} else {
logger.error(new RainbowError(`WC v2: namespace is missing chains prop when updating`), {
requiredNamespaces: session.requiredNamespaces,
});
}
}