Skip to content

Commit 23f4964

Browse files
authored
Update WalletConnect integration to support optional namespaces (#2842)
* Update WalletConnect integration to support optional namespaces and enhance CSP for verification endpoints * Refactor WalletConnect session proposal handling to merge required and optional namespaces for chia capabilities * Fix error handling in processSessionDelete by correcting variable reference
1 parent 3988a10 commit 23f4964

2 files changed

Lines changed: 20 additions & 7 deletions

File tree

packages/gui/src/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
default-src 'self';
1010
script-src 'self';
1111
style-src 'self' 'unsafe-inline';
12-
connect-src 'self' https://download.chia.net wss://relay.walletconnect.org wss://relay.walletconnect.com;
12+
connect-src 'self' https://download.chia.net wss://relay.walletconnect.org wss://relay.walletconnect.com https://verify.walletconnect.org https://verify.walletconnect.com;
1313
frame-src 'self' https://verify.walletconnect.org/ https://verify.walletconnect.com/;
1414
object-src 'none';
1515
img-src 'self' data: blob: cache:;

packages/gui/src/util/walletConnect.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,14 @@ export async function processSessionProposal(
4444
icons?: string[];
4545
};
4646
};
47-
requiredNamespaces: {
48-
chia: {
47+
requiredNamespaces?: {
48+
chia?: {
49+
chains: string[];
50+
methods: string[];
51+
};
52+
};
53+
optionalNamespaces?: {
54+
chia?: {
4955
chains: string[];
5056
methods: string[];
5157
};
@@ -64,19 +70,26 @@ export async function processSessionProposal(
6470
pairingTopic,
6571
proposer: { metadata: proposerMetadata },
6672
requiredNamespaces,
73+
optionalNamespaces,
6774
},
6875
} = event;
6976

7077
if (!pairingTopic) {
7178
throw new Error('Pairing topic not found');
7279
}
7380

74-
const requiredNamespace = requiredNamespaces.chia;
75-
if (!requiredNamespace) {
81+
// WalletConnect SDK v2.17+ deprecated requiredNamespaces and moves them
82+
// to optionalNamespaces, so check both. When a dApp provides chia in both,
83+
// merge them so the approved session advertises all supported capabilities.
84+
const requiredChia = requiredNamespaces?.chia;
85+
const optionalChia = optionalNamespaces?.chia;
86+
87+
if (!requiredChia && !optionalChia) {
7688
throw new Error('Missing required chia namespace');
7789
}
7890

79-
const { chains, methods } = requiredNamespace;
91+
const chains = [...new Set([...(requiredChia?.chains ?? []), ...(optionalChia?.chains ?? [])])];
92+
const methods = [...new Set([...(requiredChia?.methods ?? []), ...(optionalChia?.methods ?? [])])];
8093
const chain = chains.find((item) => ['chia:testnet', 'chia:mainnet'].includes(item));
8194
if (!chain) {
8295
throw new Error('Chain not supported');
@@ -168,7 +181,7 @@ export async function processSessionDelete(client: Client, pairs: Pairs, event:
168181
} catch (error) {
169182
// session was deleted we are not sending any response
170183
log('Session delete error', error);
171-
processError(e as Error);
184+
processError(error as Error);
172185
}
173186
}
174187

0 commit comments

Comments
 (0)