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

wc: improvements #5616

Merged
merged 5 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ PODS:
- React-Core
- react-native-cloud-fs (2.6.1):
- React
- react-native-compat (2.11.2):
- react-native-compat (2.12.2):
- RCT-Folly (= 2021.07.22.00)
- React-Core
- react-native-get-random-values (1.5.0):
Expand Down Expand Up @@ -1313,7 +1313,7 @@ SPEC CHECKSUMS:
react-native-cameraroll: aff50ec1df9d054626dceca9336e6644e153d32f
react-native-change-icon: ea9bb7255b09e89f41cbf282df16eade91ab1833
react-native-cloud-fs: e7103d1f693c57b481f820fa5e6b6b0522a5a31e
react-native-compat: e31d4e4ba7db54f2649c4b34b9b594029b51b5e2
react-native-compat: c741279b875109d2d9fc8da22e80c00ef91311a6
react-native-get-random-values: 1404bd5cc0ab0e287f75ee1c489555688fc65f89
react-native-ios-context-menu: e529171ba760a1af7f2ef0729f5a7f4d226171c5
react-native-mail: a864fb211feaa5845c6c478a3266de725afdce89
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,12 @@
"@unstoppabledomains/resolution": "7.1.4",
"@wagmi/chains": "1.8.0",
"@walletconnect/client": "1.8.0",
"@walletconnect/core": "2.11.2",
"@walletconnect/core": "2.12.2",
"@walletconnect/legacy-utils": "2.0.0",
"@walletconnect/react-native-compat": "2.11.2",
"@walletconnect/types": "2.11.2",
"@walletconnect/utils": "2.11.2",
"@walletconnect/web3wallet": "1.10.2",
"@walletconnect/react-native-compat": "2.12.2",
"@walletconnect/types": "2.12.2",
"@walletconnect/utils": "2.12.2",
"@walletconnect/web3wallet": "1.11.2",
"assert": "1.5.0",
"async-mutex": "0.3.2",
"asyncstorage-down": "4.2.0",
Expand Down
1 change: 1 addition & 0 deletions src/performance/tracking/types/PerformanceMetrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const PerformanceMetrics = {
loadRootAppComponent: 'Performance Time To Load Root App Component',
timeToInteractive: 'Performance Time To Interactive',
useInitializeWallet: 'Performance Wallet Initialize Time',
initializeWalletconnect: 'Performance WalletConnect Initialize Time',
} as const;

export type PerformanceMetricsType = (typeof PerformanceMetrics)[keyof typeof PerformanceMetrics];
47 changes: 35 additions & 12 deletions src/walletConnect/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ 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);

Expand Down Expand Up @@ -95,7 +97,7 @@ let syncWeb3WalletClient: Awaited<ReturnType<(typeof Web3Wallet)['init']>> | und

const walletConnectCore = new Core({ projectId: WC_PROJECT_ID });

export const web3WalletClient = Web3Wallet.init({
const web3WalletClient = Web3Wallet.init({
core: walletConnectCore,
metadata: {
name: '🌈 Rainbow',
Expand All @@ -109,6 +111,24 @@ export const web3WalletClient = Web3Wallet.init({
},
});

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
Expand Down Expand Up @@ -285,7 +305,7 @@ async function rejectProposal({
proposal,
});

const client = await web3WalletClient;
const client = await getWeb3WalletClient();
const { id, proposer } = proposal.params;

await client.rejectSession({ id, reason: getSdkError(reason) });
Expand All @@ -304,7 +324,7 @@ export async function pair({ uri, connector }: { uri: string; connector?: string
*/

const { topic, ...rest } = parseUri(uri);
const client = await web3WalletClient;
const client = await getWeb3WalletClient();

logger.debug(`WC v2: pair: parsed uri`, { topic, rest });

Expand All @@ -330,7 +350,10 @@ export async function pair({ uri, connector }: { uri: string; connector?: string
}

export async function initListeners() {
const client = await web3WalletClient;
PerformanceTracking.startMeasuring(PerformanceMetrics.initializeWalletconnect);

const client = await getWeb3WalletClient();
PerformanceTracking.finishMeasuring(PerformanceMetrics.initializeWalletconnect);

syncWeb3WalletClient = client;

Expand Down Expand Up @@ -434,7 +457,7 @@ export async function onSessionProposal(proposal: Web3WalletTypes.SessionProposa
verifiedData,
timedOut: false,
callback: async (approved, approvedChainId, accountAddress) => {
const client = await web3WalletClient;
const client = await getWeb3WalletClient();
const { id, proposer, requiredNamespaces } = proposal.params;

if (approved) {
Expand Down Expand Up @@ -558,7 +581,7 @@ export async function onSessionProposal(proposal: Web3WalletTypes.SessionProposa
// For WC v2
export async function onSessionRequest(event: SignClientTypes.EventArguments['session_request']) {
setHasPendingDeeplinkPendingRedirect(true);
const client = await web3WalletClient;
const client = await getWeb3WalletClient();

logger.debug(`WC v2: session_request`, {}, logger.DebugContext.walletconnect);

Expand Down Expand Up @@ -766,7 +789,7 @@ export async function handleSessionRequestResponse(
success: Boolean(result),
});

const client = await web3WalletClient;
const client = await getWeb3WalletClient();
const { topic, id } = sessionRequestEvent;
if (result) {
const payload = {
Expand All @@ -788,7 +811,7 @@ export async function handleSessionRequestResponse(
}

export async function onAuthRequest(event: Web3WalletTypes.AuthRequest) {
const client = await web3WalletClient;
const client = await getWeb3WalletClient();

logger.debug(`WC v2: auth_request`, { event }, logger.DebugContext.walletconnect);

Expand Down Expand Up @@ -906,7 +929,7 @@ export async function onAuthRequest(event: Web3WalletTypes.AuthRequest) {
* Returns all active settings in a type-safe manner.
*/
export async function getAllActiveSessions() {
const client = await web3WalletClient;
const client = await getWeb3WalletClient();
return Object.values(client?.getActiveSessions() || {}) || [];
}

Expand All @@ -923,7 +946,7 @@ export function getAllActiveSessionsSync() {
*/
export async function addAccountToSession(session: SessionTypes.Struct, { address }: { address?: string }) {
try {
const client = await web3WalletClient;
const client = await getWeb3WalletClient();

const namespaces: Parameters<typeof client.updateSession>[0]['namespaces'] = {};

Expand Down Expand Up @@ -984,7 +1007,7 @@ export async function addAccountToSession(session: SessionTypes.Struct, { addres

export async function changeAccount(session: SessionTypes.Struct, { address }: { address?: string }) {
try {
const client = await web3WalletClient;
const client = await getWeb3WalletClient();

/*
* Before we can effectively switch accounts, we need to add the account to
Expand Down Expand Up @@ -1032,7 +1055,7 @@ export async function changeAccount(session: SessionTypes.Struct, { address }: {
* within a dapp is handled internally by WC v2.
*/
export async function disconnectSession(session: SessionTypes.Struct) {
const client = await web3WalletClient;
const client = await getWeb3WalletClient();

await client.disconnectSession({
topic: session.topic,
Expand Down
102 changes: 55 additions & 47 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5440,24 +5440,24 @@
"@walletconnect/types" "^1.8.0"
"@walletconnect/utils" "^1.8.0"

"@walletconnect/core@2.11.2":
version "2.11.2"
resolved "https://registry.yarnpkg.com/@walletconnect/core/-/core-2.11.2.tgz#35286be92c645fa461fecc0dfe25de9f076fca8f"
integrity sha512-bB4SiXX8hX3/hyBfVPC5gwZCXCl+OPj+/EDVM71iAO3TDsh78KPbrVAbDnnsbHzZVHlsMohtXX3j5XVsheN3+g==
"@walletconnect/core@2.12.2":
version "2.12.2"
resolved "https://registry.yarnpkg.com/@walletconnect/core/-/core-2.12.2.tgz#12bd568b90daed876e58ebcc098c12843a3321e6"
integrity sha512-7Adv/b3pp9F42BkvReaaM4KS8NEvlkS7AMtwO3uF/o6aRMKtcfTJq9/jgWdKJh4RP8pPRTRFjCw6XQ/RZtT4aQ==
dependencies:
"@walletconnect/heartbeat" "1.2.1"
"@walletconnect/jsonrpc-provider" "1.0.13"
"@walletconnect/jsonrpc-types" "1.0.3"
"@walletconnect/jsonrpc-utils" "1.0.8"
"@walletconnect/jsonrpc-ws-connection" "1.0.14"
"@walletconnect/keyvaluestorage" "^1.1.1"
"@walletconnect/logger" "^2.0.1"
"@walletconnect/logger" "^2.1.2"
"@walletconnect/relay-api" "^1.0.9"
"@walletconnect/relay-auth" "^1.0.4"
"@walletconnect/safe-json" "^1.0.2"
"@walletconnect/time" "^1.0.2"
"@walletconnect/types" "2.11.2"
"@walletconnect/utils" "2.11.2"
"@walletconnect/types" "2.12.2"
"@walletconnect/utils" "2.12.2"
events "^3.3.0"
isomorphic-unfetch "3.1.0"
lodash.isequal "4.5.0"
Expand Down Expand Up @@ -5615,7 +5615,15 @@
detect-browser "^5.3.0"
query-string "^6.13.5"

"@walletconnect/logger@2.0.1", "@walletconnect/logger@^2.0.1":
"@walletconnect/logger@2.1.2", "@walletconnect/logger@^2.1.2":
version "2.1.2"
resolved "https://registry.yarnpkg.com/@walletconnect/logger/-/logger-2.1.2.tgz#813c9af61b96323a99f16c10089bfeb525e2a272"
integrity sha512-aAb28I3S6pYXZHQm5ESB+V6rDqIYfsnHaQyzFbwUUBFY4H0OXx/YtTl8lvhUNhMMfb9UxbwEBS253TlXUYJWSw==
dependencies:
"@walletconnect/safe-json" "^1.0.2"
pino "7.11.0"

"@walletconnect/logger@^2.0.1":
version "2.0.1"
resolved "https://registry.yarnpkg.com/@walletconnect/logger/-/logger-2.0.1.tgz#7f489b96e9a1ff6bf3e58f0fbd6d69718bf844a8"
integrity sha512-SsTKdsgWm+oDTBeNE/zHxxr5eJfZmE9/5yp/Ku+zJtcTAjELb3DXueWkDXmE9h8uHIbJzIb5wj5lPdzyrjT6hQ==
Expand All @@ -5633,10 +5641,10 @@
randombytes "^2.1.0"
tslib "1.14.1"

"@walletconnect/react-native-compat@2.11.2":
version "2.11.2"
resolved "https://registry.yarnpkg.com/@walletconnect/react-native-compat/-/react-native-compat-2.11.2.tgz#bd0d3ab77589050fb4cb750261e257455c09af46"
integrity sha512-Ozz0vPyWDBIKQfjHEwt3OKK6EslDMaTUVpgOaGfZjQ8gw/vuOnYOybn1IpgAQjuPfIQSkPebtE36dxlJyGPzOg==
"@walletconnect/react-native-compat@2.12.2":
version "2.12.2"
resolved "https://registry.yarnpkg.com/@walletconnect/react-native-compat/-/react-native-compat-2.12.2.tgz#3dd9e75124d7cea30c02bc0f3fcfede01e241f2e"
integrity sha512-NChpoZwftMDSy6xH0hc1d2eCvlm0Tx8BQSMtHKFaOpbxxreB90UDmbubqNIPywAGinM/1isgen0i0yvLpHRLRg==
dependencies:
events "3.3.0"
fast-text-encoding "^1.0.6"
Expand Down Expand Up @@ -5674,19 +5682,19 @@
dependencies:
tslib "1.14.1"

"@walletconnect/sign-client@2.11.2":
version "2.11.2"
resolved "https://registry.yarnpkg.com/@walletconnect/sign-client/-/sign-client-2.11.2.tgz#855609653855f0d23b0502cdbdcf43402e34c459"
integrity sha512-MfBcuSz2GmMH+P7MrCP46mVE5qhP0ZyWA0FyIH6/WuxQ6G+MgKsGfaITqakpRPsykWOJq8tXMs3XvUPDU413OQ==
"@walletconnect/sign-client@2.12.2":
version "2.12.2"
resolved "https://registry.yarnpkg.com/@walletconnect/sign-client/-/sign-client-2.12.2.tgz#10cddcba3740f726149c33ef1a9040a808d65e08"
integrity sha512-cM0ualXj6nVvLqS4BDNRk+ZWR+lubcsz/IHreH+3wYrQ2sV+C0fN6ctrd7MMGZss0C0qacWCx0pm62ZBuoKvqA==
dependencies:
"@walletconnect/core" "2.11.2"
"@walletconnect/core" "2.12.2"
"@walletconnect/events" "^1.0.1"
"@walletconnect/heartbeat" "1.2.1"
"@walletconnect/jsonrpc-utils" "1.0.8"
"@walletconnect/logger" "^2.0.1"
"@walletconnect/logger" "^2.1.2"
"@walletconnect/time" "^1.0.2"
"@walletconnect/types" "2.11.2"
"@walletconnect/utils" "2.11.2"
"@walletconnect/types" "2.12.2"
"@walletconnect/utils" "2.12.2"
events "^3.3.0"

"@walletconnect/socket-transport@^1.8.0":
Expand All @@ -5705,10 +5713,10 @@
dependencies:
tslib "1.14.1"

"@walletconnect/types@2.11.2":
version "2.11.2"
resolved "https://registry.yarnpkg.com/@walletconnect/types/-/types-2.11.2.tgz#d0359dd4106fcaa1634241a00428d3ea08d0d3c7"
integrity sha512-p632MFB+lJbip2cvtXPBQslpUdiw1sDtQ5y855bOlAGquay+6fZ4h1DcDePeKQDQM3P77ax2a9aNPZxV6y/h1Q==
"@walletconnect/types@2.11.3":
version "2.11.3"
resolved "https://registry.yarnpkg.com/@walletconnect/types/-/types-2.11.3.tgz#8ce43cb77e8fd9d5269847cdd73bcfa7cce7dd1a"
integrity sha512-JY4wA9MVosDW9dcJMTpnwliste0aJGJ1X6Q4ulLsQsgWRSEBRkLila0oUT01TDBW9Yq8uUp7uFOUTaKx6KWVAg==
dependencies:
"@walletconnect/events" "^1.0.1"
"@walletconnect/heartbeat" "1.2.1"
Expand All @@ -5717,10 +5725,10 @@
"@walletconnect/logger" "^2.0.1"
events "^3.3.0"

"@walletconnect/types@2.11.3":
version "2.11.3"
resolved "https://registry.yarnpkg.com/@walletconnect/types/-/types-2.11.3.tgz#8ce43cb77e8fd9d5269847cdd73bcfa7cce7dd1a"
integrity sha512-JY4wA9MVosDW9dcJMTpnwliste0aJGJ1X6Q4ulLsQsgWRSEBRkLila0oUT01TDBW9Yq8uUp7uFOUTaKx6KWVAg==
"@walletconnect/types@2.12.2":
version "2.12.2"
resolved "https://registry.yarnpkg.com/@walletconnect/types/-/types-2.12.2.tgz#8b64a2015a0a96972d28acb2ff317a9a994abfdb"
integrity sha512-9CmwTlPbrFTzayTL9q7xM7s3KTJkS6kYFtH2m1/fHFgALs6pIUjf1qAx1TF2E4tv7SEzLAIzU4NqgYUt2vWXTg==
dependencies:
"@walletconnect/events" "^1.0.1"
"@walletconnect/heartbeat" "1.2.1"
Expand All @@ -5734,10 +5742,10 @@
resolved "https://registry.yarnpkg.com/@walletconnect/types/-/types-1.8.0.tgz#3f5e85b2d6b149337f727ab8a71b8471d8d9a195"
integrity sha512-Cn+3I0V0vT9ghMuzh1KzZvCkiAxTq+1TR2eSqw5E5AVWfmCtECFkVZBP6uUJZ8YjwLqXheI+rnjqPy7sVM4Fyg==

"@walletconnect/utils@2.11.2":
version "2.11.2"
resolved "https://registry.yarnpkg.com/@walletconnect/utils/-/utils-2.11.2.tgz#dee0f19adf5e38543612cbe9fa4de7ed28eb7e85"
integrity sha512-LyfdmrnZY6dWqlF4eDrx5jpUwsB2bEPjoqR5Z6rXPiHJKUOdJt7az+mNOn5KTSOlRpd1DmozrBrWr+G9fFLYVw==
"@walletconnect/utils@2.11.3", "@walletconnect/utils@^2.10.1":
version "2.11.3"
resolved "https://registry.yarnpkg.com/@walletconnect/utils/-/utils-2.11.3.tgz#3731809b54902655cf202e0bf0e8f268780e8b54"
integrity sha512-jsdNkrl/IcTkzWFn0S2d0urzBXg6RxVJtUYRsUx3qI3wzOGiABP9ui3yiZ3SgZOv9aRe62PaNp1qpbYZ+zPb8Q==
dependencies:
"@stablelib/chacha20poly1305" "1.0.1"
"@stablelib/hkdf" "1.0.1"
Expand All @@ -5747,17 +5755,17 @@
"@walletconnect/relay-api" "^1.0.9"
"@walletconnect/safe-json" "^1.0.2"
"@walletconnect/time" "^1.0.2"
"@walletconnect/types" "2.11.2"
"@walletconnect/types" "2.11.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@2.11.3", "@walletconnect/utils@^2.10.1":
version "2.11.3"
resolved "https://registry.yarnpkg.com/@walletconnect/utils/-/utils-2.11.3.tgz#3731809b54902655cf202e0bf0e8f268780e8b54"
integrity sha512-jsdNkrl/IcTkzWFn0S2d0urzBXg6RxVJtUYRsUx3qI3wzOGiABP9ui3yiZ3SgZOv9aRe62PaNp1qpbYZ+zPb8Q==
"@walletconnect/utils@2.12.2":
version "2.12.2"
resolved "https://registry.yarnpkg.com/@walletconnect/utils/-/utils-2.12.2.tgz#a2c349d4effef7c1c5e72e74a5483d8dfbb10918"
integrity sha512-zf50HeS3SfoLv1N9GPl2IXTZ9TsXfet4usVAsZmX9P6/Xzq7d/7QakjVQCHH/Wk1O9XkcsfeoZoUhRxoMJ5uJw==
dependencies:
"@stablelib/chacha20poly1305" "1.0.1"
"@stablelib/hkdf" "1.0.1"
Expand All @@ -5767,7 +5775,7 @@
"@walletconnect/relay-api" "^1.0.9"
"@walletconnect/safe-json" "^1.0.2"
"@walletconnect/time" "^1.0.2"
"@walletconnect/types" "2.11.3"
"@walletconnect/types" "2.12.2"
"@walletconnect/window-getters" "^1.0.1"
"@walletconnect/window-metadata" "^1.0.1"
detect-browser "5.3.0"
Expand All @@ -5787,19 +5795,19 @@
js-sha3 "0.8.0"
query-string "6.13.5"

"@walletconnect/web3wallet@1.10.2":
version "1.10.2"
resolved "https://registry.yarnpkg.com/@walletconnect/web3wallet/-/web3wallet-1.10.2.tgz#5887642773e6e1b88d1bfb159a00ec45b31f762a"
integrity sha512-FbWsJwhihppl6poJ0+0WCkjXZDVdb11KJiS/AJt+qyNheIKQ7z6NfCnCnnKPBEpNNoUPclcFo4b/BmdFo2YlMw==
"@walletconnect/web3wallet@1.11.2":
version "1.11.2"
resolved "https://registry.yarnpkg.com/@walletconnect/web3wallet/-/web3wallet-1.11.2.tgz#0c6af495518a92036b0bb72a39e54bc10127b541"
integrity sha512-jrxXmZyg+czkHXg4d0jdhxajjfbgPvS9dW4UzdGdz12dXsX7CFgZxz+LWc/oakhLyngUtwHtyBlgaFWxamS3AQ==
dependencies:
"@walletconnect/auth-client" "2.1.2"
"@walletconnect/core" "2.11.2"
"@walletconnect/core" "2.12.2"
"@walletconnect/jsonrpc-provider" "1.0.13"
"@walletconnect/jsonrpc-utils" "1.0.8"
"@walletconnect/logger" "2.0.1"
"@walletconnect/sign-client" "2.11.2"
"@walletconnect/types" "2.11.2"
"@walletconnect/utils" "2.11.2"
"@walletconnect/logger" "2.1.2"
"@walletconnect/sign-client" "2.12.2"
"@walletconnect/types" "2.12.2"
"@walletconnect/utils" "2.12.2"

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