Skip to content

Commit

Permalink
feat: implements static decrypt notifications & getMetadata utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Gancho Radkov committed Nov 29, 2023
1 parent 5b78ef9 commit 2e6a0a2
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 2 deletions.
11 changes: 11 additions & 0 deletions packages/web3wallet/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import EventEmitter from "events";
import { CLIENT_CONTEXT } from "./constants";
import { Engine } from "./controllers";
import { IWeb3Wallet, Web3WalletTypes } from "./types";
import { Notifications } from "./utils";

export class Web3Wallet extends IWeb3Wallet {
public name: IWeb3Wallet["name"];
Expand All @@ -10,6 +11,7 @@ export class Web3Wallet extends IWeb3Wallet {
public events: IWeb3Wallet["events"] = new EventEmitter();
public engine: IWeb3Wallet["engine"];
public metadata: IWeb3Wallet["metadata"];
public static notifications: Web3WalletTypes.INotifications = Notifications;

static async init(opts: Web3WalletTypes.Options) {
const client = new Web3Wallet(opts);
Expand Down Expand Up @@ -173,6 +175,15 @@ export class Web3Wallet extends IWeb3Wallet {
}
};

public registerDeviceToken: IWeb3Wallet["registerDeviceToken"] = (params) => {
try {
return this.engine.registerDeviceToken(params);
} catch (error: any) {
this.logger.error(error.message);
throw error;
}
};

// ---------- Private ----------------------------------------------- //

private async initialize() {
Expand Down
5 changes: 5 additions & 0 deletions packages/web3wallet/src/controllers/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ export class Engine extends IWeb3WalletEngine {
return this.authClient.formatMessage(params, iss);
};

// Push //
public registerDeviceToken: IWeb3WalletEngine["registerDeviceToken"] = (params) => {
return this.client.core.echo.registerDeviceToken(params);
};

// ---------- Private ----------------------------------------------- //

private onSessionRequest = (event: Web3WalletTypes.SessionRequest) => {
Expand Down
21 changes: 19 additions & 2 deletions packages/web3wallet/src/types/client.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import EventEmmiter, { EventEmitter } from "events";
import { ICore, ProposalTypes, Verify } from "@walletconnect/types";
import { ICore, CoreTypes, ProposalTypes, Verify } from "@walletconnect/types";
import { AuthClientTypes } from "@walletconnect/auth-client";
import { IWeb3WalletEngine } from "./engine";
import { Logger } from "@walletconnect/logger";
import { JsonRpcPayload, JsonRpcRequest } from "@walletconnect/jsonrpc-utils";

export declare namespace Web3WalletTypes {
type Event = "session_proposal" | "session_request" | "session_delete" | "auth_request";
Expand Down Expand Up @@ -41,7 +42,21 @@ export declare namespace Web3WalletTypes {
name?: string;
}

type Metadata = AuthClientTypes.Metadata;
type Metadata = CoreTypes.Metadata;

interface INotifications {
decryptMessage: (params: {
topic: string;
encryptedMessage: string;
storageOptions?: CoreTypes.Options["storageOptions"];
storage?: CoreTypes.Options["storage"];
}) => Promise<JsonRpcPayload>;
getMetadata: (params: {
topic: string;
storageOptions?: CoreTypes.Options["storageOptions"];
storage?: CoreTypes.Options["storage"];
}) => Promise<CoreTypes.Metadata>;
}
}

export abstract class IWeb3WalletEvents extends EventEmmiter {
Expand Down Expand Up @@ -104,6 +119,8 @@ export abstract class IWeb3Wallet {
public abstract respondAuthRequest: IWeb3WalletEngine["respondAuthRequest"];
public abstract getPendingAuthRequests: IWeb3WalletEngine["getPendingAuthRequests"];
public abstract formatMessage: IWeb3WalletEngine["formatMessage"];
// push
public abstract registerDeviceToken: IWeb3WalletEngine["registerDeviceToken"];

// ---------- Event Handlers ----------------------------------------------- //
public abstract on: <E extends Web3WalletTypes.Event>(
Expand Down
8 changes: 8 additions & 0 deletions packages/web3wallet/src/types/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,12 @@ export abstract class IWeb3WalletEngine {

// format payload to message string
public abstract formatMessage(payload: AuthEngineTypes.CacaoRequestPayload, iss: string): string;

// ---------- Push ------------------------------------------------- //
public abstract registerDeviceToken(params: {
clientId: string;
token: string;
notificationType: "fcm" | "apns" | "apns-sandbox" | "noop";
enableAlwaysDecrypted?: boolean;
}): Promise<void>;
}
1 change: 1 addition & 0 deletions packages/web3wallet/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./notifications";
24 changes: 24 additions & 0 deletions packages/web3wallet/src/utils/notifications.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Core } from "@walletconnect/core";
import { Web3WalletTypes } from "../types";
import { SessionStore } from "@walletconnect/sign-client";

export const Notifications: Web3WalletTypes.INotifications = {
decryptMessage: async (params) => {
const core = new Core({
storageOptions: params.storageOptions,
storage: params.storage,
});
await core.crypto.init();
return core.crypto.decode(params.topic, params.encryptedMessage);
},
getMetadata: async (params) => {
const core = new Core({
storageOptions: params.storageOptions,
storage: params.storage,
});
const sessionStore = new SessionStore(core, core.logger);
await sessionStore.init();
const session = sessionStore.get(params.topic);
return session?.peer.metadata;
},
};

0 comments on commit 2e6a0a2

Please sign in to comment.