Skip to content

Commit

Permalink
write tests and fix rpc method
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrouid committed Feb 8, 2022
1 parent a5cb261 commit 3ddd4e0
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 7 deletions.
8 changes: 4 additions & 4 deletions packages/client/src/controllers/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,9 @@ export class Engine extends IEngine {
this.sequence.logger.error(error.message);
throw new Error(error.message);
}
const extension = { expiry: calcExpiry(params.ttl) };
const upgrade = await this.handleExtension(params.topic, extension, participant);
const request = formatJsonRpcRequest(this.sequence.config.jsonrpc.upgrade, upgrade);
let extension = { expiry: calcExpiry(params.ttl) };
extension = await this.handleExtension(params.topic, extension, participant);
const request = formatJsonRpcRequest(this.sequence.config.jsonrpc.extend, extension);
await this.send(settled.topic, request);
return settled;
}
Expand Down Expand Up @@ -730,7 +730,7 @@ export class Engine extends IEngine {
this.sequence.logger.error(error.message);
throw new Error(error.message);
}
await this.sequence.mergeExtension(topic, extension);
extension = await this.sequence.mergeExtension(topic, extension);
await this.sequence.settled.update(settled.topic, extension);
return extension;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/controllers/pairing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export class Pairing extends IPairing {

public async mergeExtension(topic: string, extension: PairingTypes.Extension) {
const settled = await this.settled.get(topic);
if (settled.expiry >= extension.expiry) {
if (extension.expiry <= settled.expiry) {
const error = ERROR.INVALID_EXTEND_REQUEST.format({ context: this.name });
this.logger.error(error.message);
throw new Error(error.message);
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/controllers/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export class Session extends ISession {

public async mergeExtension(topic: string, extension: SessionTypes.Extension) {
const settled = await this.settled.get(topic);
if (settled.expiry >= extension.expiry) {
if (extension.expiry <= settled.expiry) {
const error = ERROR.INVALID_EXTEND_REQUEST.format({ context: this.name });
this.logger.error(error.message);
throw new Error(error.message);
Expand Down
60 changes: 59 additions & 1 deletion packages/client/test/session.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ import {
TEST_TIMEOUT_DURATION,
testJsonRpcRequest,
TEST_SESSION_TTL,
TEST_TIMEOUT_SAFEGUARD,
} from "./shared";
import { CLIENT_EVENTS } from "../src";
import { CLIENT_EVENTS, ONE_DAY, SEVEN_DAYS, THIRTY_DAYS } from "../src";
import { ErrorResponse, formatJsonRpcResult } from "@walletconnect/jsonrpc-utils";
import { delay } from "@walletconnect/timestamp";

describe("Session", function() {
it("A proposes session and B approves", async () => {
Expand Down Expand Up @@ -340,4 +342,60 @@ describe("Session (with timeout)", function() {
});
// clock.tick(TEST_TIMEOUT_DURATION);
});
it("B extends expiry and A receives event", async () => {
const ttl = SEVEN_DAYS;
const { setup, clients } = await setupClientsForTesting();
const topic = await testApproveSession(setup, clients);
const { expiry } = await clients.a.session.get(topic);
await Promise.all([
new Promise<void>(async (resolve, reject) => {
clients.a.on(CLIENT_EVENTS.session.extended, async (session: SessionTypes.Settled) => {
if (session.expiry <= expiry) {
return reject(new Error(`Upgraded session expiry missing new value: ${expiry}`));
}
const savedSession = await clients.a.session.get(session.topic);
if (savedSession.expiry <= expiry) {
return reject(new Error(`Saved session expiry missing new value: ${expiry}`));
}
resolve();
});
}),
new Promise<void>(async (resolve, reject) => {
try {
clock.tick(TEST_TIMEOUT_SAFEGUARD);
await clients.b.extend({ topic, ttl });
resolve();
} catch (e) {
reject(e);
}
}),
]);
});
it("B fails to extend expiry if higher than default ttl", async () => {
const { setup, clients } = await setupClientsForTesting();
const topic = await testApproveSession(setup, clients);
const ttl = THIRTY_DAYS;
clock.tick(TEST_TIMEOUT_SAFEGUARD);
await expect(clients.b.extend({ topic, ttl })).to.eventually.be.rejectedWith(
`Invalid session extend request`,
);
});
it("B fails to extend expiry if smaller than current expiry", async () => {
const { setup, clients } = await setupClientsForTesting();
const topic = await testApproveSession(setup, clients);
const ttl = ONE_DAY;
clock.tick(TEST_TIMEOUT_SAFEGUARD);
await expect(clients.b.extend({ topic, ttl })).to.eventually.be.rejectedWith(
`Invalid session extend request`,
);
});
it("A fails to extend expiry as non-controller", async () => {
const { setup, clients } = await setupClientsForTesting();
const topic = await testApproveSession(setup, clients);
const ttl = SEVEN_DAYS;
clock.tick(TEST_TIMEOUT_SAFEGUARD);
await expect(clients.a.extend({ topic, ttl })).to.eventually.be.rejectedWith(
`Unauthorized session extend request`,
);
});
});

0 comments on commit 3ddd4e0

Please sign in to comment.