Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update walletconnect se-sdk #10103

Merged
merged 11 commits into from
Jul 17, 2024
4 changes: 3 additions & 1 deletion app/core/RPCMethods/wallet_addEthereumChain.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ const wallet_addEthereumChain = async ({
);
}

if (Object.values(ChainId).find((value) => value === _chainId)) {
//TODO: Remove aurora from default chains in @metamask/controller-utils
const actualChains = { ...ChainId, aurora: undefined };
if (Object.values(actualChains).find((value) => value === _chainId)) {
throw rpcErrors.invalidParams(`May not specify default MetaMask chain.`);
}

Expand Down
80 changes: 71 additions & 9 deletions app/core/WalletConnect/WalletConnectV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
import Routes from '../../../app/constants/navigation/Routes';
import ppomUtil from '../../../app/lib/ppom/ppom-util';
import { WALLET_CONNECT_ORIGIN } from '../../../app/util/walletconnect';
import { selectChainId } from '../../selectors/networkController';
import {
selectChainId,
selectNetworkConfigurations,
} from '../../selectors/networkController';
import { store } from '../../store';
import AsyncStorage from '../../store/async-storage-wrapper';
import Device from '../../util/device';
Expand All @@ -41,6 +44,7 @@
hideWCLoadingState,
showWCLoadingState,
} from './wc-utils';
import { getDefaultNetworkByChainId } from '../../util/networks';

const { PROJECT_ID } = AppConstants.WALLET_CONNECT;
export const isWC2Enabled =
Expand All @@ -65,6 +69,9 @@
private navigation?: NavigationContainerRef;
private web3Wallet: Client;
private deeplink: boolean;
// timeoutRef is used on android to prevent automatic redirect on switchChain and wait for wallet_addEthereumChain.
// If addEthereumChain is not received after 3 seconds, it will redirect.
private timeoutRef: NodeJS.Timeout | null = null;
private session: SessionTypes.Struct;
private requestsToRedirect: { [request: string]: boolean } = {};
private topicByRequestId: { [requestId: string]: string } = {};
Expand Down Expand Up @@ -184,11 +191,11 @@
this.deeplink = deeplink;
};

redirect = () => {
redirect = (context?: string) => {
DevLogger.log(
`WC2::redirect isDeeplink=${this.deeplink} navigation=${
this.navigation !== undefined
}`,
`WC2::redirect context=${context} isDeeplink=${
this.deeplink
} navigation=${this.navigation !== undefined}`,
);
if (!this.deeplink) return;

Expand All @@ -208,7 +215,7 @@
needsRedirect = (id: string) => {
if (this.requestsToRedirect[id]) {
delete this.requestsToRedirect[id];
this.redirect();
this.redirect(`needsRedirect_${id}`);
}
};

Expand Down Expand Up @@ -368,6 +375,10 @@
);
this.topicByRequestId[requestEvent.id] = requestEvent.topic;
this.requestByRequestId[requestEvent.id] = requestEvent;
if (this.timeoutRef) {
// Always clear the timeout ref on new message, it is only used for wallet_switchEthereumChain auto reject on android
clearTimeout(this.timeoutRef);
}

hideWCLoadingState({ navigation: this.navigation });
const verified = requestEvent.verifyContext?.verified;
Expand All @@ -390,13 +401,62 @@
const selectedChainId = parseInt(selectChainId(store.getState()));

if (selectedChainId !== chainId) {
DevLogger.log(
`rejectRequest due to invalid chainId ${chainId} (selectedChainId=${selectedChainId})`,
);
await this.web3Wallet.rejectRequest({
id: chainId,
id: requestEvent.id,
topic: this.session.topic,
error: { code: 1, message: ERROR_MESSAGES.INVALID_CHAIN },
});
}

// Android specific logic to prevent automatic redirect on switchChain and let the dapp call wallet_addEthereumChain on error.
if (
method.toLowerCase() === RPC_WALLET_SWITCHETHEREUMCHAIN.toLowerCase() &&
Device.isAndroid()
) {
// extract first chainId param from request array
const params = requestEvent.params.request.params as [
{ chainId?: string },
];
const _chainId = params[0]?.chainId;
DevLogger.log(
`formatting chainId=>${chainId} ==> 0x${chainId.toString(16)}`,
);
const networkConfigurations = selectNetworkConfigurations(
store.getState(),
);
const existingNetworkDefault = getDefaultNetworkByChainId(_chainId);
const existingEntry = Object.entries(networkConfigurations).find(
([, networkConfiguration]) => networkConfiguration.chainId === _chainId,
);
DevLogger.log(
`rpcMiddleWare -- check for auto rejection (_chainId=${_chainId}) networkConfigurations=${JSON.stringify(
networkConfigurations,
)} existingEntry=${existingEntry} existingNetworkDefault=${existingNetworkDefault}`,
);
if (!existingEntry && !existingNetworkDefault) {
DevLogger.log(
`SKIP rpcMiddleWare -- auto rejection is detected android (_chainId=${_chainId})`,
);
await this.web3Wallet.rejectRequest({
id: requestEvent.id,
topic: requestEvent.topic,
error: { code: 32603, message: ERROR_MESSAGES.INVALID_CHAIN },
});

showWCLoadingState({ navigation: this.navigation });
this.timeoutRef = setTimeout(() => {
DevLogger.log(`wc2::timeoutRef redirecting...`);
hideWCLoadingState({ navigation: this.navigation });
// Redirect or do nothing if timer gets cleared upon receiving wallet_addEthereumChain after automatic reject
this.redirect('handleRequestTimeout');
}, 3000);
return;
}
}

// Manage redirects
if (METHODS_TO_REDIRECT[method]) {
this.requestsToRedirect[requestEvent.id] = true;
Expand Down Expand Up @@ -475,7 +535,9 @@
this.deeplinkSessions = deeplinkSessions;
this.navigation = navigation;

const sessions = web3Wallet.getActiveSessions() || {};
const sessions = web3Wallet.getActiveSessions
? web3Wallet.getActiveSessions()
: {};

DevLogger.log(`WC2Manager::constructor()`, navigation);

Expand All @@ -484,7 +546,7 @@
web3Wallet.on(
'session_delete',
async (event: SingleEthereumTypes.SessionDelete) => {
const session = sessions[event.topic];

Check failure on line 549 in app/core/WalletConnect/WalletConnectV2.ts

View workflow job for this annotation

GitHub Actions / scripts (lint:tsc)

Object is possibly 'undefined'.
if (session && deeplinkSessions[session?.pairingTopic]) {
delete deeplinkSessions[session.pairingTopic];
await AsyncStorage.setItem(
Expand Down Expand Up @@ -514,9 +576,9 @@
}
).PermissionController;

Object.keys(sessions).forEach(async (sessionKey) => {

Check failure on line 579 in app/core/WalletConnect/WalletConnectV2.ts

View workflow job for this annotation

GitHub Actions / scripts (lint:tsc)

No overload matches this call.
try {
const session = sessions[sessionKey];

Check failure on line 581 in app/core/WalletConnect/WalletConnectV2.ts

View workflow job for this annotation

GitHub Actions / scripts (lint:tsc)

Object is possibly 'undefined'.

this.sessions[sessionKey] = new WalletConnect2Session({
web3Wallet,
Expand Down Expand Up @@ -860,7 +922,7 @@

this.sessions[activeSession.topic] = session;
if (deeplink) {
session.redirect();
session.redirect('onSessionProposal');
}
} catch (err) {
console.error(`invalid wallet status`, err);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@
"@walletconnect/core": "2.13.0",
"@walletconnect/jsonrpc-types": "^1.0.2",
"@walletconnect/react-native-compat": "2.13.0",
"@walletconnect/se-sdk": "1.8.0",
"@walletconnect/se-sdk": "1.8.1",
"@walletconnect/utils": "2.13.0",
"@xmldom/xmldom": "^0.8.10",
"appium-adb": "^9.11.4",
Expand Down
101 changes: 78 additions & 23 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9874,7 +9874,7 @@
"@walletconnect/types" "^1.8.0"
"@walletconnect/utils" "^1.8.0"

"@walletconnect/core@2.13.0", "@walletconnect/core@^2.10.1":
"@walletconnect/core@2.13.0":
version "2.13.0"
resolved "https://registry.yarnpkg.com/@walletconnect/core/-/core-2.13.0.tgz#6b79b039930643e8ee85a0f512b143a35fdb8b52"
integrity sha512-blDuZxQenjeXcVJvHxPznTNl6c/2DO4VNrFnus+qHmO6OtT5lZRowdMtlCaCNb1q0OxzgrmBDcTOCbFcCpio/g==
Expand All @@ -9897,6 +9897,29 @@
lodash.isequal "4.5.0"
uint8arrays "3.1.0"

"@walletconnect/core@2.13.3", "@walletconnect/core@^2.10.1":
version "2.13.3"
resolved "https://registry.yarnpkg.com/@walletconnect/core/-/core-2.13.3.tgz#d98fccefe36c6b365812fd0f7237a0f9634bafb6"
integrity sha512-TdF+rC6rONJGyOUtt/nLkbyQWjnkwbD3kXq3ZA0Q7+tYtmSjTDE4wbArlLbHIbtf69g+9/DpEVEQimWWcEOn2g==
dependencies:
"@walletconnect/heartbeat" "1.2.2"
"@walletconnect/jsonrpc-provider" "1.0.14"
"@walletconnect/jsonrpc-types" "1.0.4"
"@walletconnect/jsonrpc-utils" "1.0.8"
"@walletconnect/jsonrpc-ws-connection" "1.0.14"
"@walletconnect/keyvaluestorage" "1.1.1"
"@walletconnect/logger" "2.1.2"
"@walletconnect/relay-api" "1.0.10"
"@walletconnect/relay-auth" "1.0.4"
"@walletconnect/safe-json" "1.0.2"
"@walletconnect/time" "1.0.2"
"@walletconnect/types" "2.13.3"
"@walletconnect/utils" "2.13.3"
events "3.3.0"
isomorphic-unfetch "3.1.0"
lodash.isequal "4.5.0"
uint8arrays "3.1.0"

"@walletconnect/core@^1.8.0":
version "1.8.0"
resolved "https://registry.yarnpkg.com/@walletconnect/core/-/core-1.8.0.tgz#6b2748b90c999d9d6a70e52e26a8d5e8bfeaa81e"
Expand Down Expand Up @@ -10063,26 +10086,26 @@
dependencies:
tslib "1.14.1"

"@walletconnect/se-sdk@1.8.0":
version "1.8.0"
resolved "https://registry.yarnpkg.com/@walletconnect/se-sdk/-/se-sdk-1.8.0.tgz#109cd817d243f798f33dec089a542f8c4ef7fe49"
integrity sha512-vsSMxcO8Jp4rJ5P/T9FG1AnApZfTRSAyChyJJzDUyi+6bPv+t3Y+7rKMvntQFc9LrnzSdXpB6Fb5LwI390eksw==
"@walletconnect/se-sdk@1.8.1":
version "1.8.1"
resolved "https://registry.yarnpkg.com/@walletconnect/se-sdk/-/se-sdk-1.8.1.tgz#a4755e8a27dd9de84c0a30b3a99dae7231e6635b"
integrity sha512-bTbr3EHPY6TcGzfGWYm1lR8JXK9hfLAo8bMTeSQ5X1DSsVi8Xq7rKH04Z2vWGKbG+ma53/NSkH+BNyIUaU8/IA==
dependencies:
"@walletconnect/web3wallet" "1.12.0"
"@walletconnect/web3wallet" "1.12.3"

"@walletconnect/sign-client@2.13.0":
version "2.13.0"
resolved "https://registry.yarnpkg.com/@walletconnect/sign-client/-/sign-client-2.13.0.tgz#f59993f082aec1ca5498b9519027e764c1e6d28b"
integrity sha512-En7KSvNUlQFx20IsYGsFgkNJ2lpvDvRsSFOT5PTdGskwCkUfOpB33SQJ6nCrN19gyoKPNvWg80Cy6MJI0TjNYA==
"@walletconnect/sign-client@2.13.3":
version "2.13.3"
resolved "https://registry.yarnpkg.com/@walletconnect/sign-client/-/sign-client-2.13.3.tgz#9f8c826000bf3d6ea782f7325bc87e9f260e71ce"
integrity sha512-3Pcq6trHWdBZn5X0VUFQ3zJaaqyEbMW9WNVKcZ2SakIpQAwySd08Mztvq48G98jfucdgP3tjGPbBvzHX9vJX7w==
dependencies:
"@walletconnect/core" "2.13.0"
"@walletconnect/core" "2.13.3"
"@walletconnect/events" "1.0.1"
"@walletconnect/heartbeat" "1.2.2"
"@walletconnect/jsonrpc-utils" "1.0.8"
"@walletconnect/logger" "2.1.2"
"@walletconnect/time" "1.0.2"
"@walletconnect/types" "2.13.0"
"@walletconnect/utils" "2.13.0"
"@walletconnect/types" "2.13.3"
"@walletconnect/utils" "2.13.3"
events "3.3.0"

"@walletconnect/socket-transport@^1.8.0":
Expand All @@ -10101,7 +10124,7 @@
dependencies:
tslib "1.14.1"

"@walletconnect/types@2.13.0", "@walletconnect/types@^2.9.0":
"@walletconnect/types@2.13.0":
version "2.13.0"
resolved "https://registry.yarnpkg.com/@walletconnect/types/-/types-2.13.0.tgz#cdac083651f5897084fe9ed62779f11810335ac6"
integrity sha512-MWaVT0FkZwzYbD3tvk8F+2qpPlz1LUSWHuqbINUtMXnSzJtXN49Y99fR7FuBhNFtDalfuWsEK17GrNA+KnAsPQ==
Expand All @@ -10113,12 +10136,24 @@
"@walletconnect/logger" "2.1.2"
events "3.3.0"

"@walletconnect/types@2.13.3", "@walletconnect/types@^2.9.0":
version "2.13.3"
resolved "https://registry.yarnpkg.com/@walletconnect/types/-/types-2.13.3.tgz#0280b5c64df9a2e07752c4121eeb81dc4a59b2c2"
integrity sha512-9UdtLoQqwGFfepCPprUAXeUbKg9zyDarPRmEJVco51OWXHCOpvRgroWk54fQHDhCUIfDELjObY6XNAzNrmNYUA==
dependencies:
"@walletconnect/events" "1.0.1"
"@walletconnect/heartbeat" "1.2.2"
"@walletconnect/jsonrpc-types" "1.0.4"
"@walletconnect/keyvaluestorage" "1.1.1"
"@walletconnect/logger" "2.1.2"
events "3.3.0"

"@walletconnect/types@^1.8.0":
version "1.8.0"
resolved "https://registry.yarnpkg.com/@walletconnect/types/-/types-1.8.0.tgz#3f5e85b2d6b149337f727ab8a71b8471d8d9a195"
integrity sha512-Cn+3I0V0vT9ghMuzh1KzZvCkiAxTq+1TR2eSqw5E5AVWfmCtECFkVZBP6uUJZ8YjwLqXheI+rnjqPy7sVM4Fyg==

"@walletconnect/utils@2.13.0", "@walletconnect/utils@^2.10.1":
"@walletconnect/utils@2.13.0":
version "2.13.0"
resolved "https://registry.yarnpkg.com/@walletconnect/utils/-/utils-2.13.0.tgz#1fc1fbff0d26db0830e65d1ba8cfe1a13a0616ad"
integrity sha512-q1eDCsRHj5iLe7fF8RroGoPZpdo2CYMZzQSrw1iqL+2+GOeqapxxuJ1vaJkmDUkwgklfB22ufqG6KQnz78sD4w==
Expand All @@ -10138,6 +10173,26 @@
query-string "7.1.3"
uint8arrays "3.1.0"

"@walletconnect/utils@2.13.3", "@walletconnect/utils@^2.10.1":
version "2.13.3"
resolved "https://registry.yarnpkg.com/@walletconnect/utils/-/utils-2.13.3.tgz#500d88342c193ce92ab9d2fae3bd343be71821b2"
integrity sha512-hjyyNhnhTCezGNr6OCfKRzqRsiak+p+YP57iRo1Tsf222fsj/9JD++MP97YiDwc4e4xXaZp/boiLB+8hJHsCog==
dependencies:
"@stablelib/chacha20poly1305" "1.0.1"
"@stablelib/hkdf" "1.0.1"
"@stablelib/random" "1.0.2"
"@stablelib/sha256" "1.0.1"
"@stablelib/x25519" "1.0.3"
"@walletconnect/relay-api" "1.0.10"
"@walletconnect/safe-json" "1.0.2"
"@walletconnect/time" "1.0.2"
"@walletconnect/types" "2.13.3"
"@walletconnect/window-getters" "1.0.1"
"@walletconnect/window-metadata" "1.0.1"
detect-browser "5.3.0"
query-string "7.1.3"
uint8arrays "3.1.0"

"@walletconnect/utils@^1.8.0":
version "1.8.0"
resolved "https://registry.yarnpkg.com/@walletconnect/utils/-/utils-1.8.0.tgz#2591a197c1fa7429941fe428876088fda6632060"
Expand All @@ -10151,19 +10206,19 @@
js-sha3 "0.8.0"
query-string "6.13.5"

"@walletconnect/web3wallet@1.12.0":
version "1.12.0"
resolved "https://registry.yarnpkg.com/@walletconnect/web3wallet/-/web3wallet-1.12.0.tgz#bac10a755ddf23aacf4775c0be04b4d9df145536"
integrity sha512-u+krMeatqnlm086fG+587pHide9LOGd8gATA0EGYNnc7nVUWc3+xjhKR8C7YcWNBTGOH0cWdh/OFWMzUPnHGtw==
"@walletconnect/web3wallet@1.12.3":
version "1.12.3"
resolved "https://registry.yarnpkg.com/@walletconnect/web3wallet/-/web3wallet-1.12.3.tgz#26765f52cb653bd3696198ce4cc871df2061a01e"
integrity sha512-HBzYDjf3ZVzyTCqycyMjJnn/1sQtRe0beGc3qtq9bLX/dmezkO6Oc1lg1WlvxT7KtTqKx41usw5tjiwfNZ2+ug==
dependencies:
"@walletconnect/auth-client" "2.1.2"
"@walletconnect/core" "2.13.0"
"@walletconnect/core" "2.13.3"
"@walletconnect/jsonrpc-provider" "1.0.14"
"@walletconnect/jsonrpc-utils" "1.0.8"
"@walletconnect/logger" "2.1.2"
"@walletconnect/sign-client" "2.13.0"
"@walletconnect/types" "2.13.0"
"@walletconnect/utils" "2.13.0"
"@walletconnect/sign-client" "2.13.3"
"@walletconnect/types" "2.13.3"
"@walletconnect/utils" "2.13.3"

"@walletconnect/window-getters@1.0.0":
version "1.0.0"
Expand Down
Loading