Skip to content

Commit

Permalink
Merge pull request WalletConnect#717 from WalletConnect/extend-expiry…
Browse files Browse the repository at this point in the history
…-method

Extend method
  • Loading branch information
pedrouid authored Feb 8, 2022
2 parents e5aef1f + 3ddd4e0 commit 979c957
Show file tree
Hide file tree
Showing 16 changed files with 298 additions and 54 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 15 additions & 2 deletions packages/client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,16 @@ export class Client extends IClient {
this.logger.trace({ type: "method", method: "reject", pending });
}

public async update(params: ClientTypes.UpdateParams): Promise<void> {
await this.session.update(params);
}

public async upgrade(params: ClientTypes.UpgradeParams): Promise<void> {
await this.session.upgrade(params);
}

public async update(params: ClientTypes.UpdateParams): Promise<void> {
await this.session.update(params);
public async extend(params: ClientTypes.ExtendParams): Promise<void> {
await this.session.extend(params);
}

public async request(params: ClientTypes.RequestParams): Promise<any> {
Expand Down Expand Up @@ -427,6 +431,15 @@ export class Client extends IClient {
this.events.emit(eventName, session, upgrade);
},
);
this.session.on(
SESSION_EVENTS.extended,
(session: SessionTypes.Settled, extension: Partial<SessionTypes.Settled>) => {
const eventName = CLIENT_EVENTS.session.extended;
this.logger.info(`Emitting ${eventName}`);
this.logger.debug({ type: "event", event: eventName, data: session, extension });
this.events.emit(eventName, session, extension);
},
);
this.session.on(
SESSION_EVENTS.deleted,
(session: SessionTypes.Settled, reason: ErrorResponse) => {
Expand Down
2 changes: 2 additions & 0 deletions packages/client/src/constants/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const CLIENT_EVENTS = {
proposal: "pairing_proposal",
updated: "pairing_updated",
upgraded: "pairing_upgraded",
extended: "pairing_extended",
created: "pairing_created",
deleted: "pairing_deleted",
sync: "pairing_sync",
Expand All @@ -22,6 +23,7 @@ export const CLIENT_EVENTS = {
proposal: "session_proposal",
updated: "session_updated",
upgraded: "session_upgraded",
extended: "session_extended",
created: "session_created",
deleted: "session_deleted",
notification: "session_notification",
Expand Down
2 changes: 2 additions & 0 deletions packages/client/src/constants/pairing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const PAIRING_JSONRPC: PairingTypes.JsonRpc = {
reject: "wc_pairingReject",
update: "wc_pairingUpdate",
upgrade: "wc_pairingUpgrade",
extend: "wc_pairingExtend",
delete: "wc_pairingDelete",
payload: "wc_pairingPayload",
ping: "wc_pairingPing",
Expand All @@ -32,6 +33,7 @@ export const PAIRING_EVENTS: PairingTypes.Events = {
settled: "pairing_settled",
updated: "pairing_updated",
upgraded: "pairing_upgraded",
extended: "pairing_extended",
deleted: "pairing_deleted",
request: "pairing_request",
response: "pairing_response",
Expand Down
2 changes: 2 additions & 0 deletions packages/client/src/constants/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const SESSION_JSONRPC: SessionTypes.JsonRpc = {
reject: "wc_sessionReject",
update: "wc_sessionUpdate",
upgrade: "wc_sessionUpgrade",
extend: "wc_sessionExtend",
delete: "wc_sessionDelete",
payload: "wc_sessionPayload",
ping: "wc_sessionPing",
Expand All @@ -33,6 +34,7 @@ export const SESSION_EVENTS: SessionTypes.Events = {
settled: "session_settled",
updated: "session_updated",
upgraded: "session_upgraded",
extended: "session_extended",
deleted: "session_deleted",
request: "session_request",
response: "session_response",
Expand Down
72 changes: 72 additions & 0 deletions packages/client/src/controllers/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,23 @@ export class Engine extends IEngine {
return settled;
}

public async extend(params: SequenceTypes.ExtendParams): Promise<SequenceTypes.Settled> {
this.sequence.logger.debug(`Extend ${this.sequence.context}`);
this.sequence.logger.trace({ type: "method", method: "extend", params });
const settled = await this.sequence.settled.get(params.topic);
const participant: SequenceTypes.Participant = { publicKey: settled.self.publicKey };
if (params.ttl > (await this.sequence.getDefaultTTL())) {
const error = ERROR.INVALID_EXTEND_REQUEST.format({ context: this.sequence.name });
this.sequence.logger.error(error.message);
throw new Error(error.message);
}
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;
}

public async request(params: SequenceTypes.RequestParams): Promise<any> {
return new Promise(async (resolve, reject) => {
try {
Expand Down Expand Up @@ -519,6 +536,9 @@ export class Engine extends IEngine {
case this.sequence.config.jsonrpc.upgrade:
await this.onUpgrade(payloadEvent);
break;
case this.sequence.config.jsonrpc.extend:
await this.onExtension(payloadEvent);
break;
case this.sequence.config.jsonrpc.notification:
await this.onNotification(payloadEvent);
break;
Expand Down Expand Up @@ -603,6 +623,24 @@ export class Engine extends IEngine {
}
}

public async onExtension(payloadEvent: RelayerTypes.PayloadEvent): Promise<void> {
const { topic, payload } = payloadEvent;
this.sequence.logger.debug(`Receiving ${this.sequence.context} extension`);
this.sequence.logger.trace({ type: "method", method: "onExtension", topic, payload });
const request = payloadEvent.payload as JsonRpcRequest;
const settled = await this.sequence.settled.get(payloadEvent.topic);
try {
const participant: SequenceTypes.Participant = { publicKey: settled.peer.publicKey };
await this.handleExtension(topic, request.params, participant);
const response = formatJsonRpcResult(request.id, true);
await this.send(settled.topic, response);
} catch (e) {
this.sequence.logger.error(e as any);
const response = formatJsonRpcError(request.id, (e as any).message);
await this.send(settled.topic, response);
}
}

protected async onNotification(payloadEvent: RelayerTypes.PayloadEvent) {
const { params: notification } = payloadEvent.payload as JsonRpcRequest<
SessionTypes.Notification
Expand Down Expand Up @@ -673,6 +711,29 @@ export class Engine extends IEngine {
await this.sequence.settled.update(settled.topic, { permissions });
return upgrade;
}

public async handleExtension(
topic: string,
extension: SequenceTypes.Extension,
participant: SequenceTypes.Participant,
): Promise<SequenceTypes.Extension> {
if (typeof extension.expiry === "undefined") {
const error = ERROR.INVALID_EXTEND_REQUEST.format({ context: this.sequence.name });
this.sequence.logger.error(error.message);
throw new Error(error.message);
}
const settled = await this.sequence.settled.get(topic);
if (participant.publicKey !== settled.permissions.controller.publicKey) {
const error = ERROR.UNAUTHORIZED_EXTEND_REQUEST.format({
context: this.sequence.name,
});
this.sequence.logger.error(error.message);
throw new Error(error.message);
}
extension = await this.sequence.mergeExtension(topic, extension);
await this.sequence.settled.update(settled.topic, extension);
return extension;
}
// ---------- Private ----------------------------------------------- //

private async isJsonRpcAuthorized(
Expand Down Expand Up @@ -907,6 +968,17 @@ export class Engine extends IEngine {
upgrade,
});
this.sequence.events.emit(eventName, settled, upgrade);
} else if (typeof update.expiry !== "undefined") {
const eventName = this.sequence.config.events.extended;
const extension = update;
this.sequence.logger.info(`Emitting ${eventName}`);
this.sequence.logger.debug({
type: "event",
event: eventName,
sequence: settled,
extension,
});
this.sequence.events.emit(eventName, settled, extension);
}
},
);
Expand Down
32 changes: 23 additions & 9 deletions packages/client/src/controllers/pairing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Logger } from "pino";
import { generateChildLogger, getLoggerContext } from "@walletconnect/logger";
import { PairingTypes, IClient, IPairing } from "@walletconnect/types";
import { JsonRpcPayload } from "@walletconnect/jsonrpc-utils";
import { formatUri, mergeArrays } from "@walletconnect/utils";
import { ERROR, formatUri, mergeArrays } from "@walletconnect/utils";

import { Store } from "./store";
import { Engine } from "./engine";
Expand Down Expand Up @@ -88,31 +88,35 @@ export class Pairing extends IPairing {
}

public create(params?: PairingTypes.CreateParams): Promise<PairingTypes.Settled> {
return this.engine.create(params);
return this.engine.create(params as any) as any;
}

public respond(params: PairingTypes.RespondParams): Promise<PairingTypes.Pending> {
return this.engine.respond(params);
return this.engine.respond(params as any) as any;
}

public update(params: PairingTypes.UpdateParams): Promise<PairingTypes.Settled> {
return this.engine.update(params as any) as any;
}

public upgrade(params: PairingTypes.UpgradeParams): Promise<PairingTypes.Settled> {
return this.engine.upgrade(params);
return this.engine.upgrade(params as any) as any;
}

public update(params: PairingTypes.UpdateParams): Promise<PairingTypes.Settled> {
return this.engine.update(params);
public extend(params: PairingTypes.ExtendParams): Promise<PairingTypes.Settled> {
return this.engine.extend(params as any) as any;
}

public request(params: PairingTypes.RequestParams): Promise<any> {
return this.engine.request(params);
return this.engine.request(params as any) as any;
}

public delete(params: PairingTypes.DeleteParams): Promise<void> {
return this.engine.delete(params);
return this.engine.delete(params as any) as any;
}

public notify(params: PairingTypes.NotificationEvent): Promise<void> {
return this.engine.notify(params);
return this.engine.notify(params as any) as any;
}

public on(event: string, listener: any): void {
Expand Down Expand Up @@ -159,6 +163,16 @@ export class Pairing extends IPairing {
return permissions;
}

public async mergeExtension(topic: string, extension: PairingTypes.Extension) {
const settled = await this.settled.get(topic);
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);
}
return extension;
}

public async validateRespond(params?: PairingTypes.RespondParams) {
// nothing to validate
}
Expand Down
28 changes: 20 additions & 8 deletions packages/client/src/controllers/session.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EventEmitter } from "events";
import { Logger } from "pino";
import { generateChildLogger, getLoggerContext } from "@walletconnect/logger";
import { IClient, ISession, SessionTypes } from "@walletconnect/types";
import { IClient, ISession, SequenceTypes, SessionTypes } from "@walletconnect/types";
import { JsonRpcPayload } from "@walletconnect/jsonrpc-utils";
import {
validateSessionProposeParams,
Expand Down Expand Up @@ -100,26 +100,28 @@ export class Session extends ISession {
return this.engine.respond(params as any) as any;
}

public update(params: SessionTypes.UpdateParams): Promise<SessionTypes.Settled> {
return this.engine.update(params as any) as any;
}

public upgrade(params: SessionTypes.UpgradeParams): Promise<SessionTypes.Settled> {
// TODO: fix type casting as any
return this.engine.upgrade(params as any) as any;
}

public update(params: SessionTypes.UpdateParams): Promise<SessionTypes.Settled> {
// TODO: fix type casting as any
return this.engine.update(params as any) as any;
public extend(params: SessionTypes.ExtendParams): Promise<SessionTypes.Settled> {
return this.engine.extend(params as any) as any;
}

public request(params: SessionTypes.RequestParams): Promise<any> {
return this.engine.request(params);
return this.engine.request(params as any) as any;
}

public delete(params: SessionTypes.DeleteParams): Promise<void> {
return this.engine.delete(params);
return this.engine.delete(params as any) as any;
}

public notify(params: SessionTypes.NotificationEvent): Promise<void> {
return this.engine.notify(params);
return this.engine.notify(params as any) as any;
}

public on(event: string, listener: any): void {
Expand Down Expand Up @@ -172,6 +174,16 @@ export class Session extends ISession {
return permissions;
}

public async mergeExtension(topic: string, extension: SessionTypes.Extension) {
const settled = await this.settled.get(topic);
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);
}
return extension;
}

public async validateRespond(params?: SessionTypes.RespondParams) {
if (typeof params === "undefined") {
const error = ERROR.MISSING_OR_INVALID.format({ name: "respond params" });
Expand Down
Loading

0 comments on commit 979c957

Please sign in to comment.