From ca19ee08dbad1302706c43c789394ff16f6442ea Mon Sep 17 00:00:00 2001 From: Archit Date: Thu, 22 Feb 2024 23:34:11 +0530 Subject: [PATCH 1/4] Adds plugin events --- demo/react-app/src/services/web3auth.tsx | 22 ++++++ package-lock.json | 74 +++++++++++++++++++ .../package.json | 1 + .../src/plugin.ts | 14 +++- .../wallet-services-plugin/package.json | 1 + .../wallet-services-plugin/src/plugin.ts | 17 ++++- 6 files changed, 122 insertions(+), 7 deletions(-) diff --git a/demo/react-app/src/services/web3auth.tsx b/demo/react-app/src/services/web3auth.tsx index dfe455bc4..9f5d0323d 100644 --- a/demo/react-app/src/services/web3auth.tsx +++ b/demo/react-app/src/services/web3auth.tsx @@ -7,6 +7,7 @@ import { SolanaPrivateKeyProvider } from "@web3auth/solana-provider"; import { createContext, FunctionComponent, ReactNode, useCallback, useContext, useEffect, useState } from "react"; import { CHAIN_CONFIG, CHAIN_CONFIG_TYPE } from "../config/chainConfig"; import { getWalletProvider, IWalletProvider } from "./walletProvider"; +import { PLUGIN_EVENTS } from "@web3auth/base-plugin"; export interface IWeb3AuthContext { web3Auth: Web3Auth | null; @@ -107,6 +108,26 @@ export const Web3AuthProvider: FunctionComponent = ({ children, }); }; + const subscribePluginEvents = (plugin: WalletServicesPlugin) => { + // Can subscribe to all PLUGIN_EVENTS and LOGIN_MODAL_EVENTS + plugin.on(PLUGIN_EVENTS.CONNECTED, (data: unknown) => { + console.log("Yeah!, you are successfully logged in to plugin"); + }); + + plugin.on(PLUGIN_EVENTS.CONNECTING, () => { + console.log("connecting plugin"); + }); + + plugin.on(PLUGIN_EVENTS.DISCONNECTED, () => { + console.log("plugin disconnected"); + setUser(null); + }); + + plugin.on(PLUGIN_EVENTS.ERRORED, (error) => { + console.error("some error on plugin login", error); + }); + }; + async function init() { try { const currentChainConfig = CHAIN_CONFIG[chain]; @@ -195,6 +216,7 @@ export const Web3AuthProvider: FunctionComponent = ({ children, wsEmbedOpts: {}, walletInitOptions: { whiteLabel: { showWidgetButton: true } }, }); + subscribePluginEvents(walletServicesPlugin); setWalletServicesPlugin(walletServicesPlugin); web3AuthInstance.addPlugin(walletServicesPlugin); } diff --git a/package-lock.json b/package-lock.json index 998af1c77..af2ec4825 100644 --- a/package-lock.json +++ b/package-lock.json @@ -28695,6 +28695,7 @@ "version": "8.0.0", "license": "ISC", "dependencies": { + "@toruslabs/openlogin-jrpc": "^7.0.0", "@toruslabs/solana-embed": "^2.0.0", "@web3auth/base": "^8.0.0", "@web3auth/base-plugin": "^8.0.0", @@ -28712,11 +28713,48 @@ "@babel/runtime": "^7.x" } }, + "packages/plugins/solana-wallet-connector-plugin/node_modules/@toruslabs/openlogin-jrpc": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/@toruslabs/openlogin-jrpc/-/openlogin-jrpc-7.0.0.tgz", + "integrity": "sha512-5alUJPwaGniUVUsbN7Y5T3DVfrtOfBNTXy8NhXxO8bszEYxbl/EXChe1FwmZ40LjHhGmRF3MCvZxjA3BkWZoGQ==", + "dependencies": { + "@metamask/rpc-errors": "^6.1.0", + "end-of-stream": "^1.4.4", + "events": "^3.3.0", + "fast-safe-stringify": "^2.1.1", + "once": "^1.4.0", + "pump": "^3.0.0", + "readable-stream": "^4.5.2" + }, + "engines": { + "node": ">=18.x", + "npm": ">=9.x" + }, + "peerDependencies": { + "@babel/runtime": "7.x" + } + }, + "packages/plugins/solana-wallet-connector-plugin/node_modules/readable-stream": { + "version": "4.5.2", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-4.5.2.tgz", + "integrity": "sha512-yjavECdqeZ3GLXNgRXgeQEdz9fvDDkNKyHnbHRFtOr7/LcfgBcmct7t/ET+HaCTqfh06OzoAxrkN/IfjJBVe+g==", + "dependencies": { + "abort-controller": "^3.0.0", + "buffer": "^6.0.3", + "events": "^3.3.0", + "process": "^0.11.10", + "string_decoder": "^1.3.0" + }, + "engines": { + "node": "^12.22.0 || ^14.17.0 || >=16.0.0" + } + }, "packages/plugins/wallet-services-plugin": { "name": "@web3auth/wallet-services-plugin", "version": "8.0.0", "license": "ISC", "dependencies": { + "@toruslabs/openlogin-jrpc": "^7.0.0", "@web3auth/base": "^8.0.0", "@web3auth/base-plugin": "^8.0.0", "@web3auth/no-modal": "^8.0.0", @@ -28736,6 +28774,42 @@ "@web3auth/openlogin-adapter": "^8.x" } }, + "packages/plugins/wallet-services-plugin/node_modules/@toruslabs/openlogin-jrpc": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/@toruslabs/openlogin-jrpc/-/openlogin-jrpc-7.0.0.tgz", + "integrity": "sha512-5alUJPwaGniUVUsbN7Y5T3DVfrtOfBNTXy8NhXxO8bszEYxbl/EXChe1FwmZ40LjHhGmRF3MCvZxjA3BkWZoGQ==", + "dependencies": { + "@metamask/rpc-errors": "^6.1.0", + "end-of-stream": "^1.4.4", + "events": "^3.3.0", + "fast-safe-stringify": "^2.1.1", + "once": "^1.4.0", + "pump": "^3.0.0", + "readable-stream": "^4.5.2" + }, + "engines": { + "node": ">=18.x", + "npm": ">=9.x" + }, + "peerDependencies": { + "@babel/runtime": "7.x" + } + }, + "packages/plugins/wallet-services-plugin/node_modules/readable-stream": { + "version": "4.5.2", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-4.5.2.tgz", + "integrity": "sha512-yjavECdqeZ3GLXNgRXgeQEdz9fvDDkNKyHnbHRFtOr7/LcfgBcmct7t/ET+HaCTqfh06OzoAxrkN/IfjJBVe+g==", + "dependencies": { + "abort-controller": "^3.0.0", + "buffer": "^6.0.3", + "events": "^3.3.0", + "process": "^0.11.10", + "string_decoder": "^1.3.0" + }, + "engines": { + "node": "^12.22.0 || ^14.17.0 || >=16.0.0" + } + }, "packages/providers/base-provider": { "name": "@web3auth/base-provider", "version": "8.0.0", diff --git a/packages/plugins/solana-wallet-connector-plugin/package.json b/packages/plugins/solana-wallet-connector-plugin/package.json index 73cbc922a..6228728f4 100644 --- a/packages/plugins/solana-wallet-connector-plugin/package.json +++ b/packages/plugins/solana-wallet-connector-plugin/package.json @@ -35,6 +35,7 @@ "@metamask/rpc-errors": "^6.1.0" }, "dependencies": { + "@toruslabs/openlogin-jrpc": "^7.0.0", "@toruslabs/solana-embed": "^2.0.0", "@web3auth/base": "^8.0.0", "@web3auth/base-plugin": "^8.0.0", diff --git a/packages/plugins/solana-wallet-connector-plugin/src/plugin.ts b/packages/plugins/solana-wallet-connector-plugin/src/plugin.ts index 5bfe7c7eb..0486797c5 100644 --- a/packages/plugins/solana-wallet-connector-plugin/src/plugin.ts +++ b/packages/plugins/solana-wallet-connector-plugin/src/plugin.ts @@ -1,7 +1,8 @@ import type { JsonRpcError } from "@metamask/rpc-errors"; +import { SafeEventEmitter } from "@toruslabs/openlogin-jrpc"; import TorusEmbed, { NetworkInterface, PAYMENT_PROVIDER_TYPE, PaymentParams, TorusCtorArgs, TorusParams } from "@toruslabs/solana-embed"; import { ADAPTER_EVENTS, CustomChainConfig, SafeEventEmitterProvider, UserInfo, WALLET_ADAPTERS } from "@web3auth/base"; -import { IPlugin, PLUGIN_NAMESPACES } from "@web3auth/base-plugin"; +import { IPlugin, PLUGIN_EVENTS, PLUGIN_NAMESPACES, WALLET_PLUGINS } from "@web3auth/base-plugin"; import type { Web3AuthNoModal } from "@web3auth/no-modal"; import log from "loglevel"; @@ -12,7 +13,7 @@ export type ProviderInfo = { userInfo?: Omit; }; -export class SolanaWalletConnectorPlugin implements IPlugin { +export class SolanaWalletConnectorPlugin extends SafeEventEmitter implements IPlugin { name = "SOLANA_WALLET_CONNECTOR_PLUGIN"; readonly SUPPORTED_ADAPTERS = [WALLET_ADAPTERS.OPENLOGIN]; @@ -32,6 +33,7 @@ export class SolanaWalletConnectorPlugin implements IPlugin { private walletInitOptions: TorusParams | null = null; constructor(options: { torusWalletOpts?: TorusCtorArgs; walletInitOptions: Partial }) { + super(); const { torusWalletOpts = {}, walletInitOptions } = options; // const whiteLabel = walletInitOptions?.whiteLabel; @@ -80,6 +82,7 @@ export class SolanaWalletConnectorPlugin implements IPlugin { showTorusButton: false, }); this.isInitialized = true; + this.emit(PLUGIN_EVENTS.READY, WALLET_PLUGINS.SOLANA); } async initWithProvider(provider: SafeEventEmitterProvider, userInfo: UserInfo): Promise { @@ -92,12 +95,14 @@ export class SolanaWalletConnectorPlugin implements IPlugin { this.userInfo = userInfo; await this.torusWalletInstance.init(this.walletInitOptions || {}); this.isInitialized = true; + this.emit(PLUGIN_EVENTS.READY); } async connect(): Promise { // if web3auth is being used and connected to unsupported adapter throw error if (this.web3auth && this.web3auth.connectedAdapterName !== WALLET_ADAPTERS.OPENLOGIN) throw SolanaWalletPluginError.unsupportedAdapter(); if (!this.isInitialized) throw SolanaWalletPluginError.notInitialized(); + this.emit(PLUGIN_EVENTS.CONNECTING); // Not connected yet to openlogin if (!this.provider) { if (this.web3auth?.provider) { @@ -132,8 +137,10 @@ export class SolanaWalletConnectorPlugin implements IPlugin { }); this.torusWalletInstance.showTorusButton(); this.subscribeToProviderEvents(this.provider); - } catch (error) { + this.emit(PLUGIN_EVENTS.CONNECTED); + } catch (error: unknown) { log.error(error); + this.emit(PLUGIN_EVENTS.ERRORED, { error: (error as Error).message || "Something went wrong" }); } } @@ -147,6 +154,7 @@ export class SolanaWalletConnectorPlugin implements IPlugin { if (this.web3auth?.connectedAdapterName !== WALLET_ADAPTERS.OPENLOGIN) throw SolanaWalletPluginError.unsupportedAdapter(); if (this.torusWalletInstance.isLoggedIn) { await this.torusWalletInstance.logout(); + this.emit(PLUGIN_EVENTS.DISCONNECTED); } else { throw new Error("Torus Wallet plugin is not connected"); } diff --git a/packages/plugins/wallet-services-plugin/package.json b/packages/plugins/wallet-services-plugin/package.json index ad02a704e..0325e1f90 100644 --- a/packages/plugins/wallet-services-plugin/package.json +++ b/packages/plugins/wallet-services-plugin/package.json @@ -35,6 +35,7 @@ "@web3auth/openlogin-adapter": "^8.0.0" }, "dependencies": { + "@toruslabs/openlogin-jrpc": "^7.0.0", "@web3auth/base": "^8.0.0", "@web3auth/base-plugin": "^8.0.0", "@web3auth/no-modal": "^8.0.0", diff --git a/packages/plugins/wallet-services-plugin/src/plugin.ts b/packages/plugins/wallet-services-plugin/src/plugin.ts index 967888953..08f98b07f 100644 --- a/packages/plugins/wallet-services-plugin/src/plugin.ts +++ b/packages/plugins/wallet-services-plugin/src/plugin.ts @@ -1,6 +1,7 @@ import type { EthereumProviderConfig } from "@toruslabs/ethereum-controllers"; +import { SafeEventEmitter } from "@toruslabs/openlogin-jrpc"; import { ADAPTER_EVENTS, ADAPTER_STATUS, CustomChainConfig, SafeEventEmitterProvider, WALLET_ADAPTERS } from "@web3auth/base"; -import { IPlugin, PLUGIN_NAMESPACES } from "@web3auth/base-plugin"; +import { IPlugin, PLUGIN_EVENTS, PLUGIN_NAMESPACES } from "@web3auth/base-plugin"; import type { Web3AuthNoModal } from "@web3auth/no-modal"; import type { OpenloginAdapter } from "@web3auth/openlogin-adapter"; import WsEmbed, { CtorArgs, WsEmbedParams } from "@web3auth/ws-embed"; @@ -8,7 +9,7 @@ import log from "loglevel"; import { WalletServicesPluginError } from "./errors"; -export class WalletServicesPlugin implements IPlugin { +export class WalletServicesPlugin extends SafeEventEmitter implements IPlugin { name = "WALLET_SERVICES_PLUGIN"; readonly SUPPORTED_ADAPTERS = [WALLET_ADAPTERS.OPENLOGIN]; @@ -28,6 +29,7 @@ export class WalletServicesPlugin implements IPlugin { constructor( options: { wsEmbedOpts?: Partial; walletInitOptions?: Omit } = {} ) { + super(); const { wsEmbedOpts, walletInitOptions } = options; // we fake these checks here and get them from web3auth instance this.wsEmbedInstance = new WsEmbed((wsEmbedOpts || {}) as CtorArgs); @@ -74,6 +76,7 @@ export class WalletServicesPlugin implements IPlugin { }, }); this.isInitialized = true; + this.emit(PLUGIN_EVENTS.READY); } initWithProvider(): Promise { @@ -83,8 +86,11 @@ export class WalletServicesPlugin implements IPlugin { async connect(): Promise { // if web3auth is being used and connected to unsupported adapter throw error if (this.web3auth && this.web3auth.connectedAdapterName !== WALLET_ADAPTERS.OPENLOGIN) throw WalletServicesPluginError.unsupportedAdapter(); - const { connectedAdapterName } = this.web3auth; if (!this.isInitialized) throw WalletServicesPluginError.notInitialized(); + this.emit(PLUGIN_EVENTS.CONNECTING); + + const { connectedAdapterName } = this.web3auth; + // Not connected yet to openlogin let sessionId: string | null = null; let sessionNamespace: string = ""; @@ -110,8 +116,10 @@ export class WalletServicesPlugin implements IPlugin { if (this.walletInitOptions?.whiteLabel?.showWidgetButton) this.wsEmbedInstance.showTorusButton(); this.subscribeToProviderEvents(this.provider); this.subscribeToWalletEvents(); - } catch (error) { + this.emit(PLUGIN_EVENTS.CONNECTED); + } catch (error: unknown) { log.error(error); + this.emit(PLUGIN_EVENTS.ERRORED, { error: (error as Error).message || "Something went wrong" }); } } @@ -135,6 +143,7 @@ export class WalletServicesPlugin implements IPlugin { if (this.web3auth?.connectedAdapterName !== WALLET_ADAPTERS.OPENLOGIN) throw WalletServicesPluginError.unsupportedAdapter(); if (this.wsEmbedInstance.isLoggedIn) { await this.wsEmbedInstance.logout(); + this.emit(PLUGIN_EVENTS.DISCONNECTED); } else { throw new Error("Wallet Services plugin is not connected"); } From 3096c1c69b48fb38e2b5aeef0e2bd4682c029cea Mon Sep 17 00:00:00 2001 From: Archit Date: Fri, 23 Feb 2024 16:34:21 +0530 Subject: [PATCH 2/4] adds plugin events --- packages/plugins/base-plugin/src/IPlugin.ts | 25 +++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/packages/plugins/base-plugin/src/IPlugin.ts b/packages/plugins/base-plugin/src/IPlugin.ts index 972d7029f..37e78aac0 100644 --- a/packages/plugins/base-plugin/src/IPlugin.ts +++ b/packages/plugins/base-plugin/src/IPlugin.ts @@ -5,8 +5,33 @@ export const PLUGIN_NAMESPACES = { MULTICHAIN: "multichain", } as const; +export const PLUGIN_STATUS = { + READY: "ready", + CONNECTING: "connecting", + CONNECTED: "connected", + DISCONNECTED: "disconnected", + ERRORED: "errored", +} as const; + +export const PLUGIN_EVENTS = { + ...PLUGIN_STATUS, +} as const; + export type PluginNamespace = (typeof PLUGIN_NAMESPACES)[keyof typeof PLUGIN_NAMESPACES]; +export const EVM_PLUGINS = { + WALLET_SERVICES: "wallet-services", +} as const; + +export const SOLANA_PLUGINS = { + SOLANA: "solana", +} as const; + +export const WALLET_PLUGINS = { + ...EVM_PLUGINS, + ...SOLANA_PLUGINS, +} as const; + export interface IPlugin { name: string; SUPPORTED_ADAPTERS: WALLET_ADAPTER_TYPE[]; From 7c28c3084965022fbf3053ed2ac1c52ea7b7090c Mon Sep 17 00:00:00 2001 From: Archit Date: Fri, 23 Feb 2024 17:13:36 +0530 Subject: [PATCH 3/4] removes extra data params --- packages/plugins/solana-wallet-connector-plugin/src/plugin.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/plugins/solana-wallet-connector-plugin/src/plugin.ts b/packages/plugins/solana-wallet-connector-plugin/src/plugin.ts index 0486797c5..5c33195fc 100644 --- a/packages/plugins/solana-wallet-connector-plugin/src/plugin.ts +++ b/packages/plugins/solana-wallet-connector-plugin/src/plugin.ts @@ -2,7 +2,7 @@ import type { JsonRpcError } from "@metamask/rpc-errors"; import { SafeEventEmitter } from "@toruslabs/openlogin-jrpc"; import TorusEmbed, { NetworkInterface, PAYMENT_PROVIDER_TYPE, PaymentParams, TorusCtorArgs, TorusParams } from "@toruslabs/solana-embed"; import { ADAPTER_EVENTS, CustomChainConfig, SafeEventEmitterProvider, UserInfo, WALLET_ADAPTERS } from "@web3auth/base"; -import { IPlugin, PLUGIN_EVENTS, PLUGIN_NAMESPACES, WALLET_PLUGINS } from "@web3auth/base-plugin"; +import { IPlugin, PLUGIN_EVENTS, PLUGIN_NAMESPACES } from "@web3auth/base-plugin"; import type { Web3AuthNoModal } from "@web3auth/no-modal"; import log from "loglevel"; @@ -82,7 +82,7 @@ export class SolanaWalletConnectorPlugin extends SafeEventEmitter implements IPl showTorusButton: false, }); this.isInitialized = true; - this.emit(PLUGIN_EVENTS.READY, WALLET_PLUGINS.SOLANA); + this.emit(PLUGIN_EVENTS.READY); } async initWithProvider(provider: SafeEventEmitterProvider, userInfo: UserInfo): Promise { From f78b19a5b335b6c9cb2237364a65c4c59a6bffa1 Mon Sep 17 00:00:00 2001 From: Archit Date: Fri, 23 Feb 2024 17:18:12 +0530 Subject: [PATCH 4/4] dont set user null on plugin disconnect --- demo/react-app/src/services/web3auth.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/demo/react-app/src/services/web3auth.tsx b/demo/react-app/src/services/web3auth.tsx index 679ff8886..380152abd 100644 --- a/demo/react-app/src/services/web3auth.tsx +++ b/demo/react-app/src/services/web3auth.tsx @@ -120,7 +120,6 @@ export const Web3AuthProvider: FunctionComponent = ({ children, plugin.on(PLUGIN_EVENTS.DISCONNECTED, () => { console.log("plugin disconnected"); - setUser(null); }); plugin.on(PLUGIN_EVENTS.ERRORED, (error) => {