Skip to content

Commit

Permalink
refactor types
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrouid committed Jul 15, 2021
1 parent bacc870 commit 8f6fdab
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 53 deletions.
8 changes: 5 additions & 3 deletions packages/client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ----------------------------------------------- //
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 6 additions & 1 deletion packages/client/src/controllers/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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)
) {
Expand Down
10 changes: 3 additions & 7 deletions packages/client/src/controllers/pairing.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 } 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";

Expand Down Expand Up @@ -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);
Expand All @@ -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<void> {
Expand Down Expand Up @@ -147,7 +147,6 @@ export class Pairing extends IPairing {
...(upgrade.permissions.jsonrpc?.methods || []),
],
},
blockchain: { chains: [] },
notifications: {
types: [
...settled.permissions.notifications?.types,
Expand Down Expand Up @@ -196,9 +195,6 @@ export class Pairing extends IPairing {
jsonrpc: {
methods: [SESSION_JSONRPC.propose],
},
blockchain: {
chains: [],
},
notifications: {
types: [],
},
Expand Down
5 changes: 2 additions & 3 deletions packages/client/src/controllers/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<void> {
Expand Down
54 changes: 37 additions & 17 deletions packages/types/src/pairing.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,13 +13,13 @@ export declare namespace PairingTypes {

export type Relay = SequenceTypes.Relay;

export type BasePermissions = SequenceTypes.BasePermissions;
export type BasePermissions = Omit<SequenceTypes.BasePermissions, "blockchain">;

export type ProposedPermissions = SequenceTypes.ProposedPermissions;
export type ProposedPermissions = Omit<SequenceTypes.ProposedPermissions, "blockchain">;

export type SettledPermissions = SequenceTypes.SettledPermissions;
export type SettledPermissions = Omit<SequenceTypes.SettledPermissions, "blockchain">;

export type Permissions = SequenceTypes.Permissions;
export type Permissions = Omit<SequenceTypes.Permissions, "blockchain">;

export type ProposeParams = SequenceTypes.ProposeParams;

Expand All @@ -31,35 +32,35 @@ export declare namespace PairingTypes {

export type ProposedPeer = SequenceTypes.ProposedPeer;

export type Proposal = SequenceTypes.Proposal<Signal>;
export type Proposal = SequenceTypes.Proposal<Signal, ProposedPeer, ProposedPermissions>;

export type ProposedStatus = SequenceTypes.ProposedStatus;

export type RespondedStatus = SequenceTypes.RespondedStatus;

export type PendingStatus = SequenceTypes.PendingStatus;

export type BasePending = SequenceTypes.BasePending<Proposal>;
export type BasePending = SequenceTypes.BasePending<Participant, Proposal>;

export type ProposedPending = SequenceTypes.ProposedPending<Participant, Proposal>;

export type RespondedPending = SequenceTypes.RespondedPending<Participant, Proposal, State>;

export type Pending = SequenceTypes.Pending;
export type Pending = SequenceTypes.Pending<Participant, Proposal, State>;

export type RespondParams = SequenceTypes.RespondParams;
export type RespondParams = SequenceTypes.RespondParams<Proposal>;

export type SettleParams = SequenceTypes.SettleParams;
export type SettleParams = SequenceTypes.SettleParams<State, Participant, Permissions>;

export type UpgradeParams = SequenceTypes.UpgradeParams;
export type UpgradeParams = SequenceTypes.UpgradeParams<Permissions>;

export type UpdateParams = SequenceTypes.UpdateParams;
export type UpdateParams = SequenceTypes.UpdateParams<State>;

export type RequestParams = SequenceTypes.RequestParams;

export type Upgrade = SequenceTypes.Upgrade;
export type Upgrade = SequenceTypes.Upgrade<Permissions>;

export type Update = SequenceTypes.Update;
export type Update = SequenceTypes.Update<State>;

export type Request = SequenceTypes.Request;

Expand All @@ -71,15 +72,15 @@ export declare namespace PairingTypes {

export type DeleteParams = SequenceTypes.DeleteParams;

export type Settled = SequenceTypes.Settled;
export type Settled = SequenceTypes.Settled<State, Participant, Permissions>;

export type Created = SequenceTypes.Created;
export type Created = SequenceTypes.Created<State, Participant, Permissions>;

export type Success = SequenceTypes.Success;
export type Success = SequenceTypes.Success<State, Participant>;

export type Failed = SequenceTypes.Failed;

export type Outcome = SequenceTypes.Outcome;
export type Outcome = SequenceTypes.Outcome<State, Participant>;

export interface State {
metadata?: AppMetadata;
Expand All @@ -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,
Expand Down
24 changes: 6 additions & 18 deletions packages/types/src/sequence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand Down
36 changes: 32 additions & 4 deletions packages/types/src/session.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 8f6fdab

Please sign in to comment.