From 7572e3fe7bc58c912d1e560f4c6331d083c37223 Mon Sep 17 00:00:00 2001 From: Gancho Radkov Date: Wed, 10 Jan 2024 09:41:09 +0200 Subject: [PATCH 1/3] feat: adds optional `expiry` argument when sending request from the providers --- providers/ethereum-provider/src/EthereumProvider.ts | 7 ++++--- providers/universal-provider/src/UniversalProvider.ts | 5 ++++- providers/universal-provider/src/types/misc.ts | 1 + 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/providers/ethereum-provider/src/EthereumProvider.ts b/providers/ethereum-provider/src/EthereumProvider.ts index e175ae85c..92ef81834 100644 --- a/providers/ethereum-provider/src/EthereumProvider.ts +++ b/providers/ethereum-provider/src/EthereumProvider.ts @@ -236,15 +236,16 @@ export class EthereumProvider implements IEthereumProvider { return provider; } - public async request(args: RequestArguments): Promise { - return await this.signer.request(args, this.formatChainId(this.chainId)); + public async request(args: RequestArguments, expiry?: number): Promise { + return await this.signer.request(args, this.formatChainId(this.chainId), expiry); } public sendAsync( args: RequestArguments, callback: (error: Error | null, response: JsonRpcResult) => void, + expiry?: number, ): void { - this.signer.sendAsync(args, callback, this.formatChainId(this.chainId)); + this.signer.sendAsync(args, callback, this.formatChainId(this.chainId), expiry); } get connected(): boolean { diff --git a/providers/universal-provider/src/UniversalProvider.ts b/providers/universal-provider/src/UniversalProvider.ts index 91b6b6179..aabda0778 100644 --- a/providers/universal-provider/src/UniversalProvider.ts +++ b/providers/universal-provider/src/UniversalProvider.ts @@ -71,6 +71,7 @@ export class UniversalProvider implements IUniversalProvider { public async request( args: RequestArguments, chain?: string | undefined, + expiry?: number | undefined, ): Promise { const [namespace, chainId] = this.validateChain(chain); @@ -84,6 +85,7 @@ export class UniversalProvider implements IUniversalProvider { }, chainId: `${namespace}:${chainId}`, topic: this.session.topic, + expiry, }); } @@ -91,9 +93,10 @@ export class UniversalProvider implements IUniversalProvider { args: RequestArguments, callback: (error: Error | null, response: JsonRpcResult) => void, chain?: string | undefined, + expiry?: number | undefined, ): void { const id = new Date().getTime(); - this.request(args, chain) + this.request(args, chain, expiry) .then((response) => callback(null, formatJsonRpcResult(id, response))) .catch((error) => callback(error, undefined as any)); } diff --git a/providers/universal-provider/src/types/misc.ts b/providers/universal-provider/src/types/misc.ts index f1cc6c19d..c4bab0b4c 100644 --- a/providers/universal-provider/src/types/misc.ts +++ b/providers/universal-provider/src/types/misc.ts @@ -67,6 +67,7 @@ export interface RequestParams { request: RequestArguments; chainId: string; id?: number; + expiry?: number; } export interface RequestArguments { From 0e086814da0bc0b851f7f3038bfa0df32de2ef1a Mon Sep 17 00:00:00 2001 From: Gancho Radkov Date: Wed, 10 Jan 2024 09:41:24 +0200 Subject: [PATCH 2/3] feat: tests --- .../ethereum-provider/test/index.spec.ts | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/providers/ethereum-provider/test/index.spec.ts b/providers/ethereum-provider/test/index.spec.ts index 7bdcf0a3e..3474a12a8 100644 --- a/providers/ethereum-provider/test/index.spec.ts +++ b/providers/ethereum-provider/test/index.spec.ts @@ -3,7 +3,7 @@ import Web3 from "web3"; import { BigNumber, providers, utils } from "ethers"; import { TestNetwork } from "ethereum-test-network"; -import { SignClient } from "@walletconnect/sign-client"; +import { SESSION_REQUEST_EXPIRY_BOUNDARIES, SignClient } from "@walletconnect/sign-client"; import { ERC20Token__factory, @@ -48,7 +48,7 @@ describe("EthereumProvider", function () { qrModalOptions: { themeMode: "dark", themeVariables: { - "--w3m-z-index": "99", + "--wcm-z-index": "99", }, }, disableProviderPing: true, @@ -165,6 +165,26 @@ describe("EthereumProvider", function () { }), ]); }); + + describe("validation", () => { + it("should reject when lower than min expiry is used", async () => { + const expiryToTest = SESSION_REQUEST_EXPIRY_BOUNDARIES.min - 1; + await expect( + provider.request({ method: "personal_sign" }, expiryToTest), + ).rejects.toThrowError( + `Missing or invalid. request() expiry: ${expiryToTest}. Expiry must be a number (in seconds) between ${SESSION_REQUEST_EXPIRY_BOUNDARIES.min} and ${SESSION_REQUEST_EXPIRY_BOUNDARIES.max}`, + ); + }); + it("should reject when low expiry is used", async () => { + const expiryToTest = SESSION_REQUEST_EXPIRY_BOUNDARIES.max + 1; + await expect( + provider.request({ method: "personal_sign" }, expiryToTest), + ).rejects.toThrowError( + `Missing or invalid. request() expiry: ${expiryToTest}. Expiry must be a number (in seconds) between ${SESSION_REQUEST_EXPIRY_BOUNDARIES.min} and ${SESSION_REQUEST_EXPIRY_BOUNDARIES.max}`, + ); + }); + }); + describe("eip155", () => { describe("Web3", () => { let web3: Web3; From aa1e1e0ab16eb200e8eab5d011c2cd892541590d Mon Sep 17 00:00:00 2001 From: Gancho Radkov Date: Wed, 10 Jan 2024 09:47:22 +0200 Subject: [PATCH 3/3] refactor: test name --- providers/ethereum-provider/test/index.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/providers/ethereum-provider/test/index.spec.ts b/providers/ethereum-provider/test/index.spec.ts index 3474a12a8..48c0bb6e1 100644 --- a/providers/ethereum-provider/test/index.spec.ts +++ b/providers/ethereum-provider/test/index.spec.ts @@ -175,7 +175,7 @@ describe("EthereumProvider", function () { `Missing or invalid. request() expiry: ${expiryToTest}. Expiry must be a number (in seconds) between ${SESSION_REQUEST_EXPIRY_BOUNDARIES.min} and ${SESSION_REQUEST_EXPIRY_BOUNDARIES.max}`, ); }); - it("should reject when low expiry is used", async () => { + it("should reject when higher than max expiry is used", async () => { const expiryToTest = SESSION_REQUEST_EXPIRY_BOUNDARIES.max + 1; await expect( provider.request({ method: "personal_sign" }, expiryToTest),