Skip to content

Commit ee4e52b

Browse files
Merge pull request #1068 from near/1060-defect-cant-connect-using-wallet-connect
fix: Make methods and events configurable on setupWalletConnect options.
2 parents 6d4be31 + ce01ecf commit ee4e52b

File tree

6 files changed

+66
-6
lines changed

6 files changed

+66
-6
lines changed

examples/angular/src/app/pages/wallet-selector/wallet-selector.component.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@ export class WalletSelectorComponent implements OnInit {
7474
}),
7575
setupWalletConnect({
7676
projectId: "c8cb6204543639c31aef44ea4837a554", // Replace this with your own projectId form WalletConnect.
77+
// Overrides the default methods on wallet-connect.ts
78+
// the near_signMessage and near_verifyOwner are missing here.
79+
methods: [
80+
"near_signIn",
81+
"near_signOut",
82+
"near_getAccounts",
83+
"near_signTransaction",
84+
"near_signTransactions",
85+
],
7786
metadata: {
7887
name: "NEAR Wallet Selector",
7988
description: "Example dApp used by NEAR Wallet Selector",

packages/modal-ui-js/src/lib/render-modal.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,10 @@ export async function connectToWallet(
160160
modalState.emitter.emit("onHide", { hideReason: "wallet-navigation" });
161161
} catch (err) {
162162
const { name } = module.metadata;
163-
const message = err instanceof Error ? err.message : "Something went wrong";
163+
const message =
164+
err && typeof err === "object" && "message" in err
165+
? (err as { message: string }).message
166+
: "Something went wrong";
164167

165168
await renderWalletConnectionFailed(
166169
module,

packages/modal-ui/src/lib/components/DerivationPath.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,9 @@ export const DerivationPath: React.FC<DerivationPathProps> = ({
144144
} catch (err) {
145145
setConnecting(false);
146146
const message =
147-
err instanceof Error ? err.message : "Something went wrong";
147+
err && typeof err === "object" && "message" in err
148+
? (err as { message: string }).message
149+
: "Something went wrong";
148150

149151
onError(message, wallet);
150152
} finally {
@@ -174,7 +176,9 @@ export const DerivationPath: React.FC<DerivationPathProps> = ({
174176
} catch (err) {
175177
setConnecting(false);
176178
const message =
177-
err instanceof Error ? err.message : "Something went wrong";
179+
err && typeof err === "object" && "message" in err
180+
? (err as { message: string }).message
181+
: "Something went wrong";
178182
onError(message, hardwareWallet!);
179183
} finally {
180184
setConnecting(false);

packages/modal-ui/src/lib/components/Modal.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,9 @@ export const Modal: React.FC<ModalProps> = ({
227227
const { name } = module.metadata;
228228

229229
const message =
230-
err instanceof Error ? err.message : "Something went wrong";
230+
err && typeof err === "object" && "message" in err
231+
? (err as { message: string }).message
232+
: "Something went wrong";
231233

232234
setAlertMessage(`Failed to sign in with ${name}: ${message}`);
233235
setRoute({

packages/wallet-connect/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ const walletConnect = setupWalletConnect({
3737
},
3838
chainId: "near:testnet",
3939
iconUrl: "https://<Wallet Icon URL Here>",
40+
// Please note that the 'methods' option is discretionary;
41+
// if omitted, all methods are included by default.
42+
// Use it solely to override the default configuration.
43+
methods: [
44+
"near_signIn",
45+
"near_signOut",
46+
"near_getAccounts",
47+
"near_signTransaction",
48+
"near_signTransactions",
49+
"near_verifyOwner",
50+
"near_signMessage",
51+
]
4052
});
4153

4254
const selector = await setupWalletSelector({
@@ -58,6 +70,22 @@ Project ID is required for wallet connect, please obtain it from [walletconnect.
5870
- `relayUrl` (`string?`): Relay URL for requests. Defaults to `"wss://relay.walletconnect.com"`.
5971
- `iconUrl` (`string?`): Image URL for the icon shown in the modal. This can also be a relative path or base64 encoded image. Defaults to `./assets/wallet-connect-icon.png`.
6072
- `deprecated`: (`boolean?`): Deprecated is optional. Default is `false`.
73+
- `methods`: (`Array<string>?`): Methods is optional overrides default WC_METHODS. Defaults to `undefined`.
74+
- `events`: (`Array<string>?`): Events is optional overrides default WC_EVENTS. Defaults to `undefined`.
75+
76+
## Supported methods
77+
- `near_signIn`
78+
- `near_signOut`
79+
- `near_getAccounts`
80+
- `near_signTransaction`
81+
- `near_signTransactions`
82+
- `near_verifyOwner`
83+
- `near_signMessage`
84+
85+
## Supported events
86+
87+
- `chainChanged`
88+
- `accountsChanged`
6189

6290
## Assets
6391

packages/wallet-connect/src/lib/wallet-connect.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,17 @@ export interface WalletConnectParams {
2828
iconUrl?: string;
2929
chainId?: string;
3030
deprecated?: boolean;
31+
methods?: Array<string>;
32+
events?: Array<string>;
3133
}
3234

3335
interface WalletConnectExtraOptions {
3436
chainId?: string;
3537
projectId: string;
3638
metadata: SignClientTypes.Metadata;
3739
relayUrl: string;
40+
methods?: Array<string>;
41+
events?: Array<string>;
3842
}
3943

4044
interface LimitedAccessKeyPair {
@@ -64,6 +68,8 @@ interface ConnectParams {
6468
chainId: string;
6569
qrCodeModal: boolean;
6670
projectId: string;
71+
methods?: Array<string>;
72+
events?: Array<string>;
6773
}
6874

6975
const WC_METHODS = [
@@ -114,14 +120,16 @@ const connect = async ({
114120
chainId,
115121
qrCodeModal,
116122
projectId,
123+
methods,
124+
events,
117125
}: ConnectParams) => {
118126
return await state.client.connect(
119127
{
120128
requiredNamespaces: {
121129
near: {
122130
chains: [chainId],
123-
methods: WC_METHODS,
124-
events: WC_EVENTS,
131+
methods: methods || WC_METHODS,
132+
events: events || WC_EVENTS,
125133
},
126134
},
127135
},
@@ -544,6 +552,8 @@ const WalletConnect: WalletBehaviourFactory<
544552
chainId,
545553
qrCodeModal,
546554
projectId: params.projectId,
555+
methods: params.methods,
556+
events: params.events,
547557
});
548558

549559
await requestSignIn({ receiverId: contractId, methodNames });
@@ -697,6 +707,8 @@ export function setupWalletConnect({
697707
relayUrl = "wss://relay.walletconnect.com",
698708
iconUrl = icon,
699709
deprecated = false,
710+
methods,
711+
events,
700712
}: WalletConnectParams): WalletModuleFactory<BridgeWallet> {
701713
return async () => {
702714
return {
@@ -717,6 +729,8 @@ export function setupWalletConnect({
717729
metadata,
718730
relayUrl,
719731
chainId,
732+
methods,
733+
events,
720734
},
721735
});
722736
},

0 commit comments

Comments
 (0)