Skip to content

Commit d51e824

Browse files
committed
fix: the wallet_getCapabilities cache in universal-provider now takes the chainIds as well as the address to decide if the request should be sent to the wallet
1 parent 9e404ee commit d51e824

File tree

3 files changed

+61
-5
lines changed

3 files changed

+61
-5
lines changed

.changeset/twenty-poems-accept.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
"@walletconnect/universal-provider": patch
3+
"@walletconnect/core": patch
4+
"@walletconnect/react-native-compat": patch
5+
"@walletconnect/sign-client": patch
6+
"@walletconnect/types": patch
7+
"@walletconnect/utils": patch
8+
"@walletconnect/ethereum-provider": patch
9+
"@walletconnect/signer-connection": patch
10+
---
11+
12+
the wallet_getCapabilities cache in universal-provider now takes the chainIds as well as the address to decide if the request should be sent to the wallet

providers/universal-provider/src/providers/eip155.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,18 @@ class Eip155Provider implements IProvider {
177177
private async getCapabilities(args: RequestParams) {
178178
// if capabilities are stored in the session, return them, else send the request to the wallet
179179
const address = args.request?.params?.[0];
180+
const chainIds: string[] = args.request?.params?.[1] || [];
181+
182+
// cache key is address + chainIds to allow requests to be made to different chains
183+
const capabilitiesKey = `${address}${chainIds.join(",")}`;
180184
if (!address) throw new Error("Missing address parameter in `wallet_getCapabilities` request");
181185
const session = this.client.session.get(args.topic);
182186
const sessionCapabilities = session?.sessionProperties?.capabilities || {};
183-
if (sessionCapabilities?.[address]) {
184-
return sessionCapabilities?.[address];
187+
188+
if (sessionCapabilities?.[capabilitiesKey]) {
189+
return sessionCapabilities?.[capabilitiesKey];
185190
}
191+
186192
// intentionally omit catching errors/rejection during `request` to allow the error to bubble up
187193
const capabilities = await this.client.request(args as EngineTypes.RequestParams);
188194
try {
@@ -192,7 +198,7 @@ class Eip155Provider implements IProvider {
192198
...(session.sessionProperties || {}),
193199
capabilities: {
194200
...(sessionCapabilities || {}),
195-
[address]: capabilities,
201+
[capabilitiesKey]: capabilities,
196202
} as any, // by spec sessionProperties should be <string, string> but here are used as objects?
197203
},
198204
});

providers/universal-provider/test/index.spec.ts

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,7 +1455,7 @@ describe("UniversalProvider", function () {
14551455
}),
14561456
]);
14571457
});
1458-
it("should cache `wallet_getCapabilities` request", async () => {
1458+
it("should cache `wallet_getCapabilities` request with different chainIds", async () => {
14591459
const dapp = await UniversalProvider.init({
14601460
...TEST_PROVIDER_OPTS,
14611461
name: "dapp",
@@ -1498,7 +1498,7 @@ describe("UniversalProvider", function () {
14981498
};
14991499
await Promise.all([
15001500
new Promise<void>((resolve) => {
1501-
wallet.client.on("session_request", async (event) => {
1501+
wallet.client.once("session_request", async (event) => {
15021502
await wallet.client.respond({
15031503
topic: event.topic,
15041504
response: formatJsonRpcResult(event.id, sessionPropertiesResponse),
@@ -1525,6 +1525,44 @@ describe("UniversalProvider", function () {
15251525
params: [walletAddress],
15261526
});
15271527
expect(cachedResult).to.eql(sessionPropertiesResponse);
1528+
1529+
const secondCapabilitiesResult = {
1530+
"0x1": {
1531+
atomicBatch: {
1532+
supported: true,
1533+
},
1534+
},
1535+
};
1536+
1537+
await Promise.all([
1538+
new Promise<void>((resolve) => {
1539+
wallet.client.once("session_request", async (event) => {
1540+
await wallet.client.respond({
1541+
topic: event.topic,
1542+
response: formatJsonRpcResult(event.id, secondCapabilitiesResult),
1543+
});
1544+
resolve();
1545+
});
1546+
}),
1547+
new Promise<void>(async (resolve) => {
1548+
const result = await dapp.request({
1549+
method: "wallet_getCapabilities",
1550+
// this req has additional chainId param so it should not use cached result but make a new request to the wallet
1551+
params: [walletAddress, ["0x1"]],
1552+
});
1553+
expect(result).to.eql(secondCapabilitiesResult);
1554+
resolve();
1555+
}),
1556+
]);
1557+
1558+
const updatedSession2 = dapp.client.session.get(sessionA.topic);
1559+
expect(updatedSession2.sessionProperties).to.exist;
1560+
expect(updatedSession2.sessionProperties?.capabilities).to.exist;
1561+
expect(Object.keys(updatedSession2.sessionProperties?.capabilities || {}).length).to.eql(2);
1562+
expect(updatedSession2.sessionProperties?.capabilities[`${walletAddress}0x1`]).to.eql(
1563+
secondCapabilitiesResult,
1564+
);
1565+
15281566
await deleteProviders({ A: dapp, B: wallet });
15291567
});
15301568
});

0 commit comments

Comments
 (0)