Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: provider request set custom expiry #4119

Merged
merged 4 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions providers/ethereum-provider/src/EthereumProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,15 +236,16 @@ export class EthereumProvider implements IEthereumProvider {
return provider;
}

public async request<T = unknown>(args: RequestArguments): Promise<T> {
return await this.signer.request(args, this.formatChainId(this.chainId));
public async request<T = unknown>(args: RequestArguments, expiry?: number): Promise<T> {
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 {
Expand Down
24 changes: 22 additions & 2 deletions providers/ethereum-provider/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -48,7 +48,7 @@ describe("EthereumProvider", function () {
qrModalOptions: {
themeMode: "dark",
themeVariables: {
"--w3m-z-index": "99",
"--wcm-z-index": "99",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was left over from when we updated the wcm types, resolving it so ts is satisfied

},
},
disableProviderPing: true,
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 4 additions & 1 deletion providers/universal-provider/src/UniversalProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export class UniversalProvider implements IUniversalProvider {
public async request<T = unknown>(
args: RequestArguments,
chain?: string | undefined,
expiry?: number | undefined,
): Promise<T> {
const [namespace, chainId] = this.validateChain(chain);

Expand All @@ -84,16 +85,18 @@ export class UniversalProvider implements IUniversalProvider {
},
chainId: `${namespace}:${chainId}`,
topic: this.session.topic,
expiry,
});
}

public sendAsync(
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));
}
Expand Down
1 change: 1 addition & 0 deletions providers/universal-provider/src/types/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export interface RequestParams {
request: RequestArguments;
chainId: string;
id?: number;
expiry?: number;
}

export interface RequestArguments {
Expand Down
Loading