From 8f6fdabfd7609d7341c7551c772b0e713af27620 Mon Sep 17 00:00:00 2001 From: Pedro Gomes Date: Thu, 15 Jul 2021 18:28:19 +0200 Subject: [PATCH] refactor types --- packages/client/src/client.ts | 8 ++-- packages/client/src/controllers/engine.ts | 7 ++- packages/client/src/controllers/pairing.ts | 10 ++-- packages/client/src/controllers/session.ts | 5 +- packages/types/src/pairing.ts | 54 +++++++++++++++------- packages/types/src/sequence.ts | 24 +++------- packages/types/src/session.ts | 36 +++++++++++++-- 7 files changed, 91 insertions(+), 53 deletions(-) diff --git a/packages/client/src/client.ts b/packages/client/src/client.ts index f31903bad..9bc1fea59 100644 --- a/packages/client/src/client.ts +++ b/packages/client/src/client.ts @@ -271,8 +271,11 @@ export class Client extends IClient { } protected async onPairingSettled(pairing: PairingTypes.Settled) { - if (pairing.permissions.controller.publicKey === pairing.self.publicKey) { - this.pairing.update({ topic: pairing.topic, state: { metadata: this.metadata } }); + if ( + pairing.permissions.controller.publicKey === pairing.self.publicKey && + typeof pairing.state.metadata === "undefined" + ) { + await this.pairing.update({ topic: pairing.topic, state: { metadata: this.metadata } }); } } // ---------- Private ----------------------------------------------- // @@ -399,7 +402,6 @@ function formatPairingProposal(uri: string): PairingTypes.Proposal { signal: { method: PAIRING_SIGNAL_METHOD_URI, params: { uri } }, permissions: { jsonrpc: { methods: [SESSION_JSONRPC.propose] }, - blockchain: { chains: [] }, notifications: { types: [] }, }, ttl: PAIRING_DEFAULT_TTL, diff --git a/packages/client/src/controllers/engine.ts b/packages/client/src/controllers/engine.ts index 18b2270b7..ce7ed4d5c 100644 --- a/packages/client/src/controllers/engine.ts +++ b/packages/client/src/controllers/engine.ts @@ -34,7 +34,9 @@ import { } from "../constants"; export class Engine extends IEngine { - constructor(public sequence: ISequence) { + public sequence: ISequence; + + constructor(sequence: any) { super(sequence); this.sequence = sequence; this.registerEventListeners(); @@ -46,18 +48,21 @@ export class Engine extends IEngine { return this.sequence.values.filter((settled: SequenceTypes.Settled) => { let isCompatible = false; if ( + settled.permissions?.jsonrpc && permissions.jsonrpc?.methods && hasOverlap(permissions.jsonrpc.methods, settled.permissions.jsonrpc.methods) ) { isCompatible = true; } if ( + settled.permissions?.blockchain && permissions.blockchain?.chains && hasOverlap(permissions.blockchain.chains, settled.permissions.blockchain.chains) ) { isCompatible = true; } if ( + settled.permissions?.notifications && permissions.notifications?.types && hasOverlap(permissions.notifications.types, settled.permissions.notifications.types) ) { diff --git a/packages/client/src/controllers/pairing.ts b/packages/client/src/controllers/pairing.ts index 38d9db509..2668cb45b 100644 --- a/packages/client/src/controllers/pairing.ts +++ b/packages/client/src/controllers/pairing.ts @@ -1,7 +1,7 @@ import { EventEmitter } from "events"; import { Logger } from "pino"; import { generateChildLogger } from "@pedrouid/pino-utils"; -import { PairingTypes, IClient, IPairing } from "@walletconnect/types"; +import { PairingTypes, IClient, IPairing, ISequence, IEngine } from "@walletconnect/types"; import { formatUri } from "@walletconnect/utils"; import { JsonRpcPayload } from "@json-rpc-tools/utils"; @@ -33,7 +33,7 @@ export class Pairing extends IPairing { jsonrpc: PAIRING_JSONRPC, }; - public engine: Engine; + public engine: PairingTypes.Engine; constructor(public client: IClient, public logger: Logger) { super(client, logger); @@ -49,7 +49,7 @@ export class Pairing extends IPairing { this.config.status.settled, ); this.history = new JsonRpcHistory(client, this.logger); - this.engine = new Engine(this); + this.engine = new Engine(this) as PairingTypes.Engine; } public async init(): Promise { @@ -147,7 +147,6 @@ export class Pairing extends IPairing { ...(upgrade.permissions.jsonrpc?.methods || []), ], }, - blockchain: { chains: [] }, notifications: { types: [ ...settled.permissions.notifications?.types, @@ -196,9 +195,6 @@ export class Pairing extends IPairing { jsonrpc: { methods: [SESSION_JSONRPC.propose], }, - blockchain: { - chains: [], - }, notifications: { types: [], }, diff --git a/packages/client/src/controllers/session.ts b/packages/client/src/controllers/session.ts index 6c2119917..1fa1d585c 100644 --- a/packages/client/src/controllers/session.ts +++ b/packages/client/src/controllers/session.ts @@ -37,8 +37,7 @@ export class Session extends ISession { jsonrpc: SESSION_JSONRPC, }; - // TODO: fix type casting as any - public engine: any; + public engine: SessionTypes.Engine; constructor(public client: IClient, public logger: Logger) { super(client, logger); @@ -54,7 +53,7 @@ export class Session extends ISession { this.config.status.settled, ); this.history = new JsonRpcHistory(client, this.logger); - this.engine = new Engine(this); + this.engine = new Engine(this) as SessionTypes.Engine; } public async init(): Promise { diff --git a/packages/types/src/pairing.ts b/packages/types/src/pairing.ts index 0a2b0a3ad..ee948ea13 100644 --- a/packages/types/src/pairing.ts +++ b/packages/types/src/pairing.ts @@ -1,5 +1,6 @@ import { SequenceTypes, ISequence } from "./sequence"; import { AppMetadata, SignalTypes } from "./misc"; +import { IEngine } from "./engine"; export declare namespace PairingTypes { export type Status = SequenceTypes.Status; @@ -12,13 +13,13 @@ export declare namespace PairingTypes { export type Relay = SequenceTypes.Relay; - export type BasePermissions = SequenceTypes.BasePermissions; + export type BasePermissions = Omit; - export type ProposedPermissions = SequenceTypes.ProposedPermissions; + export type ProposedPermissions = Omit; - export type SettledPermissions = SequenceTypes.SettledPermissions; + export type SettledPermissions = Omit; - export type Permissions = SequenceTypes.Permissions; + export type Permissions = Omit; export type ProposeParams = SequenceTypes.ProposeParams; @@ -31,7 +32,7 @@ export declare namespace PairingTypes { export type ProposedPeer = SequenceTypes.ProposedPeer; - export type Proposal = SequenceTypes.Proposal; + export type Proposal = SequenceTypes.Proposal; export type ProposedStatus = SequenceTypes.ProposedStatus; @@ -39,27 +40,27 @@ export declare namespace PairingTypes { export type PendingStatus = SequenceTypes.PendingStatus; - export type BasePending = SequenceTypes.BasePending; + export type BasePending = SequenceTypes.BasePending; export type ProposedPending = SequenceTypes.ProposedPending; export type RespondedPending = SequenceTypes.RespondedPending; - export type Pending = SequenceTypes.Pending; + export type Pending = SequenceTypes.Pending; - export type RespondParams = SequenceTypes.RespondParams; + export type RespondParams = SequenceTypes.RespondParams; - export type SettleParams = SequenceTypes.SettleParams; + export type SettleParams = SequenceTypes.SettleParams; - export type UpgradeParams = SequenceTypes.UpgradeParams; + export type UpgradeParams = SequenceTypes.UpgradeParams; - export type UpdateParams = SequenceTypes.UpdateParams; + export type UpdateParams = SequenceTypes.UpdateParams; export type RequestParams = SequenceTypes.RequestParams; - export type Upgrade = SequenceTypes.Upgrade; + export type Upgrade = SequenceTypes.Upgrade; - export type Update = SequenceTypes.Update; + export type Update = SequenceTypes.Update; export type Request = SequenceTypes.Request; @@ -71,15 +72,15 @@ export declare namespace PairingTypes { export type DeleteParams = SequenceTypes.DeleteParams; - export type Settled = SequenceTypes.Settled; + export type Settled = SequenceTypes.Settled; - export type Created = SequenceTypes.Created; + export type Created = SequenceTypes.Created; - export type Success = SequenceTypes.Success; + export type Success = SequenceTypes.Success; export type Failed = SequenceTypes.Failed; - export type Outcome = SequenceTypes.Outcome; + export type Outcome = SequenceTypes.Outcome; export interface State { metadata?: AppMetadata; @@ -92,9 +93,28 @@ export declare namespace PairingTypes { export type NotificationEvent = SequenceTypes.NotificationEvent; export type NotifyParams = SequenceTypes.NotifyParams; + + export type Engine = IEngine< + Pending, + Settled, + Upgrade, + Update, + CreateParams, + RespondParams, + RequestParams, + UpgradeParams, + UpdateParams, + DeleteParams, + ProposeParams, + SettleParams, + NotifyParams, + Participant, + Permissions + >; } export abstract class IPairing extends ISequence< + PairingTypes.Engine, PairingTypes.Config, PairingTypes.Pending, PairingTypes.Settled, diff --git a/packages/types/src/sequence.ts b/packages/types/src/sequence.ts index 15b4057a8..be06b468a 100644 --- a/packages/types/src/sequence.ts +++ b/packages/types/src/sequence.ts @@ -63,10 +63,11 @@ export declare namespace SequenceTypes { export type Relay = RelayerTypes.ProtocolOptions; export interface BasePermissions { jsonrpc: JsonRpcPermissions; - blockchain: BlockchainTypes.Permissions; + blockchain?: BlockchainTypes.Permissions; notifications?: NotificationPermissions; } export interface ProposedPermissions extends BasePermissions { + blockchain: BlockchainTypes.Permissions; notifications: NotificationPermissions; } @@ -247,9 +248,12 @@ export declare namespace SequenceTypes { } export type NotifyParams = NotificationEvent; + + export type Engine = IEngine; } export abstract class ISequence< + Engine = SequenceTypes.Engine, Config = SequenceTypes.Config, Pending = SequenceTypes.Pending, Settled = SequenceTypes.Settled, @@ -292,23 +296,7 @@ export abstract class ISequence< public abstract config: Config; // sequence protocol engine - public abstract engine: IEngine< - Pending, - Settled, - Upgrade, - Update, - CreateParams, - RespondParams, - RequestParams, - UpgradeParams, - UpdateParams, - DeleteParams, - ProposeParams, - SettleParams, - NotifyParams, - Participant, - Permissions - >; + public abstract engine: Engine; constructor(public client: IClient, public logger: Logger) { super(); diff --git a/packages/types/src/session.ts b/packages/types/src/session.ts index 42d6ccfc9..fe96cd1bd 100644 --- a/packages/types/src/session.ts +++ b/packages/types/src/session.ts @@ -1,7 +1,9 @@ import { JsonRpcRequest, JsonRpcResponse } from "@json-rpc-tools/types"; import { ISequence, SequenceTypes } from "./sequence"; -import { SignalTypes, BlockchainTypes, AppMetadata } from "./misc"; +import { SignalTypes, BlockchainTypes, AppMetadata, NotificationPermissions } from "./misc"; +import { CryptoTypes } from "./crypto"; +import { IEngine } from "./engine"; export declare namespace SessionTypes { export type Status = SequenceTypes.Status; @@ -14,11 +16,18 @@ export declare namespace SessionTypes { export type Relay = SequenceTypes.Relay; - export type BasePermissions = SequenceTypes.BasePermissions; + export interface BasePermissions extends SequenceTypes.BasePermissions { + blockchain: BlockchainTypes.Permissions; + } - export type ProposedPermissions = SequenceTypes.ProposedPermissions; + export interface ProposedPermissions extends SequenceTypes.ProposedPermissions { + blockchain: BlockchainTypes.Permissions; + notifications: NotificationPermissions; + } - export type SettledPermissions = SequenceTypes.SettledPermissions; + export interface SettledPermissions extends SequenceTypes.SettledPermissions { + controller: CryptoTypes.Participant; + } export type Permissions = SettledPermissions; @@ -123,9 +132,28 @@ export declare namespace SessionTypes { export type NotificationEvent = SequenceTypes.NotificationEvent; export type NotifyParams = SequenceTypes.NotifyParams; + + export type Engine = IEngine< + Pending, + Settled, + Upgrade, + Update, + CreateParams, + RespondParams, + RequestParams, + UpgradeParams, + UpdateParams, + DeleteParams, + ProposeParams, + SettleParams, + NotifyParams, + Participant, + Permissions + >; } export abstract class ISession extends ISequence< + SessionTypes.Engine, SessionTypes.Config, SessionTypes.Pending, SessionTypes.Settled,