From 3ddd4e0ea6e6a09a74f82248a7fc0b3bfbcfbc9f Mon Sep 17 00:00:00 2001 From: Pedro Gomes Date: Tue, 8 Feb 2022 14:43:47 +0100 Subject: [PATCH] write tests and fix rpc method --- packages/client/src/controllers/engine.ts | 8 +-- packages/client/src/controllers/pairing.ts | 2 +- packages/client/src/controllers/session.ts | 2 +- packages/client/test/session.spec.ts | 60 +++++++++++++++++++++- 4 files changed, 65 insertions(+), 7 deletions(-) diff --git a/packages/client/src/controllers/engine.ts b/packages/client/src/controllers/engine.ts index d02646790..8908ad54c 100644 --- a/packages/client/src/controllers/engine.ts +++ b/packages/client/src/controllers/engine.ts @@ -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; } @@ -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; } diff --git a/packages/client/src/controllers/pairing.ts b/packages/client/src/controllers/pairing.ts index 010aeb5dc..ad2ce5f09 100644 --- a/packages/client/src/controllers/pairing.ts +++ b/packages/client/src/controllers/pairing.ts @@ -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); diff --git a/packages/client/src/controllers/session.ts b/packages/client/src/controllers/session.ts index 1bdbd03f3..d614f5e72 100644 --- a/packages/client/src/controllers/session.ts +++ b/packages/client/src/controllers/session.ts @@ -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); diff --git a/packages/client/test/session.spec.ts b/packages/client/test/session.spec.ts index fa1837136..b3fac7ef9 100644 --- a/packages/client/test/session.spec.ts +++ b/packages/client/test/session.spec.ts @@ -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 () => { @@ -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(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(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`, + ); + }); });