Skip to content

Commit

Permalink
[FIX] Improves handling of missing WCv2 Project ID (#6587)
Browse files Browse the repository at this point in the history
* Improves handling of missing WCv2 Project ID

* removes unneded try/catch

---------

Co-authored-by: Christopher Ferreira <104831203+christopherferreira9@users.noreply.github.com>
  • Loading branch information
andreahaku and christopherferreira9 authored Jul 11, 2023
1 parent 57671ff commit 33b2e33
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
12 changes: 8 additions & 4 deletions app/components/Nav/App/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ import EthSignFriction from '../../../components/Views/Settings/AdvancedSettings
import WalletActions from '../../Views/WalletActions';
import NetworkSelector from '../../../components/Views/NetworkSelector';
import EditAccountName from '../../Views/EditAccountName/EditAccountName';
import WC2Manager from '../../../../app/core/WalletConnect/WalletConnectV2';
import WC2Manager, {
isWC2Enabled,
} from '../../../../app/core/WalletConnect/WalletConnectV2';

const clearStackNavigatorOptions = {
headerShown: false,
Expand Down Expand Up @@ -360,9 +362,11 @@ const App = ({ userLoggedIn }) => {
}, [navigator]);

useEffect(() => {
WC2Manager.init().catch((err) => {
console.error(`Cannot initialize WalletConnect Manager.`, err);
});
if (isWC2Enabled) {
WC2Manager.init().catch((err) => {
console.error('Cannot initialize WalletConnect Manager.', err);
});
}
}, []);

useEffect(() => {
Expand Down
14 changes: 10 additions & 4 deletions app/components/Views/WalletConnectSessions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import Logger from '../../../util/Logger';
import { WALLETCONNECT_SESSIONS } from '../../../constants/storage';
import { ThemeContext, mockTheme } from '../../../util/theme';
import PropTypes from 'prop-types';
import WC2Manager from '../../../../app/core/WalletConnect/WalletConnectV2';
import WC2Manager, {
isWC2Enabled,
} from '../../../../app/core/WalletConnect/WalletConnectV2';

const createStyles = (colors) =>
StyleSheet.create({
Expand Down Expand Up @@ -112,13 +114,17 @@ export default class WalletConnectSessions extends PureComponent {

loadSessions = async () => {
let sessions = [];
let sessionsV2 = [];

const sessionData = await AsyncStorage.getItem(WALLETCONNECT_SESSIONS);
if (sessionData) {
sessions = JSON.parse(sessionData);
}

// Add wallet connect v2 sessions to the list
const sessionsV2 = (await WC2Manager.getInstance())?.getSessions() || [];
if (isWC2Enabled) {
// Add wallet connect v2 sessions to the list
sessionsV2 = (await WC2Manager.getInstance())?.getSessions() || [];
}

this.setState({ ready: true, sessions, sessionsV2 });
};
Expand Down Expand Up @@ -148,7 +154,7 @@ export default class WalletConnectSessions extends PureComponent {
killSession = async () => {
const isV2 = this.sessionToRemove.peerId === undefined;
try {
if (isV2) {
if (isV2 && isWC2Enabled) {
await (
await WC2Manager.getInstance()
)?.removeSession(this.sessionToRemove);
Expand Down
18 changes: 13 additions & 5 deletions app/core/WalletConnect/WalletConnectV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ import parseWalletConnectUri from './wc-utils';
import AsyncStorage from '@react-native-async-storage/async-storage';
import METHODS_TO_REDIRECT from './wc-config';

const { PROJECT_ID } = AppConstants.WALLET_CONNECT;
export const isWC2Enabled =
typeof PROJECT_ID === 'string' && PROJECT_ID?.length > 0;

const ERROR_MESSAGES = {
INVALID_CHAIN: 'Invalid chainId',
MANUAL_DISCONNECT: 'Manual disconnect',
Expand Down Expand Up @@ -366,12 +370,16 @@ export class WC2Manager {

let core;
try {
core = new Core({
projectId: AppConstants.WALLET_CONNECT.PROJECT_ID,
// logger: 'debug',
});
if (typeof PROJECT_ID === 'string') {
core = new Core({
projectId: PROJECT_ID,
// logger: 'debug',
});
} else {
throw new Error('WC2::init Init Missing projectId');
}
} catch (err) {
console.warn(`WC2::init Init failed due to missing key: ${err}`);
console.warn(`WC2::init Init failed due to ${err}`);
}

let web3Wallet;
Expand Down

0 comments on commit 33b2e33

Please sign in to comment.