From d2fee96c7ae44d167160b5b20c54d453f17f463e Mon Sep 17 00:00:00 2001 From: Florin Dzeladini Date: Wed, 6 Nov 2024 16:23:41 +0100 Subject: [PATCH 01/12] chore: add changeset for MetaMask dynamic loading fix --- .changeset/quiet-eggs-tap.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .changeset/quiet-eggs-tap.md diff --git a/.changeset/quiet-eggs-tap.md b/.changeset/quiet-eggs-tap.md new file mode 100644 index 00000000..e536ed6b --- /dev/null +++ b/.changeset/quiet-eggs-tap.md @@ -0,0 +1,7 @@ +--- +"@starknet-io/get-starknet-core": patch +--- + +Fix loading MetaMask Virtual Wallet dynamically in WalletAccount and add support +for RPC APIs (wallet_supportedWalletApi and wallet_supportedSpecs) in +get-starknet v4 integration. From 600b4a77fc3348c0b987117c850ebff55ee0ea99 Mon Sep 17 00:00:00 2001 From: Florin Dzeladini Date: Wed, 6 Nov 2024 16:39:42 +0100 Subject: [PATCH 02/12] chore: fix pnpm-lock --- pnpm-lock.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 932bc38d..0d6dffae 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -73,7 +73,7 @@ importers: specifier: ^0.7.7 version: 0.7.7 async-mutex: - specifier: 0.5.0 + specifier: ^0.5.0 version: 0.5.0 devDependencies: c8: From 25326a77ff0700f2ec4c36064fc7776ca6a920e1 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Fri, 15 Nov 2024 03:14:46 +0000 Subject: [PATCH 03/12] chore: version bump --- .changeset/quiet-eggs-tap.md | 7 ------- example/CHANGELOG.md | 6 ++++++ example/package.json | 2 +- packages/core/CHANGELOG.md | 8 ++++++++ packages/core/package.json | 2 +- packages/ui/CHANGELOG.md | 7 +++++++ packages/ui/package.json | 2 +- 7 files changed, 24 insertions(+), 10 deletions(-) delete mode 100644 .changeset/quiet-eggs-tap.md diff --git a/.changeset/quiet-eggs-tap.md b/.changeset/quiet-eggs-tap.md deleted file mode 100644 index e536ed6b..00000000 --- a/.changeset/quiet-eggs-tap.md +++ /dev/null @@ -1,7 +0,0 @@ ---- -"@starknet-io/get-starknet-core": patch ---- - -Fix loading MetaMask Virtual Wallet dynamically in WalletAccount and add support -for RPC APIs (wallet_supportedWalletApi and wallet_supportedSpecs) in -get-starknet v4 integration. diff --git a/example/CHANGELOG.md b/example/CHANGELOG.md index 31d512d7..bbfcaaa9 100644 --- a/example/CHANGELOG.md +++ b/example/CHANGELOG.md @@ -1,5 +1,11 @@ # @starknet-io/get-starknet-example +## 4.0.4 + +### Patch Changes + +- @starknet-io/get-starknet@4.0.4 + ## 4.0.3 ### Patch Changes diff --git a/example/package.json b/example/package.json index 91a61e50..12789838 100644 --- a/example/package.json +++ b/example/package.json @@ -1,6 +1,6 @@ { "name": "@starknet-io/get-starknet-example", - "version": "4.0.3", + "version": "4.0.4", "private": true, "type": "module", "scripts": { diff --git a/packages/core/CHANGELOG.md b/packages/core/CHANGELOG.md index e8fddd43..a0763b81 100644 --- a/packages/core/CHANGELOG.md +++ b/packages/core/CHANGELOG.md @@ -1,5 +1,13 @@ # @starknet-io/get-starknet-core +## 4.0.4 + +### Patch Changes + +- d2fee96: Fix loading MetaMask Virtual Wallet dynamically in WalletAccount and + add support for RPC APIs (wallet_supportedWalletApi and wallet_supportedSpecs) + in get-starknet v4 integration. + ## 4.0.3 ### Patch Changes diff --git a/packages/core/package.json b/packages/core/package.json index 7f9595bd..73b85a33 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -1,6 +1,6 @@ { "name": "@starknet-io/get-starknet-core", - "version": "4.0.3", + "version": "4.0.4", "keywords": [ "starknet", "starkware", diff --git a/packages/ui/CHANGELOG.md b/packages/ui/CHANGELOG.md index 55536fba..5545bee3 100644 --- a/packages/ui/CHANGELOG.md +++ b/packages/ui/CHANGELOG.md @@ -1,5 +1,12 @@ # @starknet-io/get-starknet +## 4.0.4 + +### Patch Changes + +- Updated dependencies [d2fee96] + - @starknet-io/get-starknet-core@4.0.4 + ## 4.0.3 ### Patch Changes diff --git a/packages/ui/package.json b/packages/ui/package.json index 0e93a957..9c5dbfa3 100644 --- a/packages/ui/package.json +++ b/packages/ui/package.json @@ -1,6 +1,6 @@ { "name": "@starknet-io/get-starknet", - "version": "4.0.3", + "version": "4.0.4", "keywords": [ "starknet", "starkware", From 34e44b6c1eb94124747347babdd93de441b8a5aa Mon Sep 17 00:00:00 2001 From: Florin Dzeladini Date: Thu, 5 Dec 2024 10:55:23 +0100 Subject: [PATCH 04/12] fix: allows discovery of virtual wallet support independently --- packages/core/src/main.ts | 9 +++++++++ packages/core/src/types.ts | 1 + packages/core/src/wallet/virtualWallets/index.ts | 10 ++++++---- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/packages/core/src/main.ts b/packages/core/src/main.ts index b6c70695..41119b4e 100644 --- a/packages/core/src/main.ts +++ b/packages/core/src/main.ts @@ -13,6 +13,7 @@ import { sortBy } from "./wallet/sort" import { initiateVirtualWallets, resolveVirtualWallet, + virtualWallets, } from "./wallet/virtualWallets" import { Permission, type StarknetWindowObject } from "@starknet-io/types-js" @@ -116,6 +117,14 @@ export function getStarknet( return firstAuthorizedWallet }, + discoverVirtualWallets: async () => { + virtualWallets.forEach(async (virtualWallet) => { + const hasSupport = await virtualWallet.hasSupport(windowObject) + if (hasSupport) { + windowObject[virtualWallet.windowKey] = virtualWallet + } + }) + }, enable: async (inputWallet, options) => { let wallet: StarknetWindowObject if (isVirtualWallet(inputWallet)) { diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index 3fb0c9e9..49e81bfb 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -69,6 +69,7 @@ export interface GetStarknetResult { ) => Promise // Returns only preauthorized wallets available in the window object getDiscoveryWallets: (options?: GetWalletOptions) => Promise // Returns all wallets in existence (from discovery file) getLastConnectedWallet: () => Promise // Returns the last wallet connected when it's still connected + discoverVirtualWallets: () => Promise // Discovers the virtual wallets by calling their hasSupport methods enable: ( wallet: StarknetWindowObject | VirtualWallet, options?: RequestAccountsParameters, diff --git a/packages/core/src/wallet/virtualWallets/index.ts b/packages/core/src/wallet/virtualWallets/index.ts index a7d5288f..5924500c 100644 --- a/packages/core/src/wallet/virtualWallets/index.ts +++ b/packages/core/src/wallet/virtualWallets/index.ts @@ -6,9 +6,11 @@ const virtualWallets: VirtualWallet[] = [metaMaskVirtualWallet] function initiateVirtualWallets(windowObject: Record) { virtualWallets.forEach(async (virtualWallet) => { - const hasSupport = await virtualWallet.hasSupport(windowObject) - if (hasSupport) { - windowObject[virtualWallet.windowKey] = virtualWallet + if (!(virtualWallet.windowKey in windowObject)) { + const hasSupport = await virtualWallet.hasSupport(windowObject) + if (hasSupport) { + windowObject[virtualWallet.windowKey] = virtualWallet + } } }) } @@ -28,4 +30,4 @@ async function resolveVirtualWallet( return wallet } -export { initiateVirtualWallets, resolveVirtualWallet } +export { initiateVirtualWallets, resolveVirtualWallet, virtualWallets } From 0263b88530db48ba3f517c7f54c541c26972cc99 Mon Sep 17 00:00:00 2001 From: Florin Dzeladini Date: Thu, 5 Dec 2024 16:29:16 +0100 Subject: [PATCH 05/12] chore: added changeset --- .changeset/eleven-buttons-unite.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/eleven-buttons-unite.md diff --git a/.changeset/eleven-buttons-unite.md b/.changeset/eleven-buttons-unite.md new file mode 100644 index 00000000..9e06eade --- /dev/null +++ b/.changeset/eleven-buttons-unite.md @@ -0,0 +1,5 @@ +--- +"@starknet-io/get-starknet-core": patch +--- + +Decouple Virtual Wallet Discovery for async workflow From 7144af2c574fd88536efc839f0cbf97efca617a2 Mon Sep 17 00:00:00 2001 From: Florin Dzeladini Date: Fri, 6 Dec 2024 09:58:23 +0100 Subject: [PATCH 06/12] fix: await for all hasSupport to haapen --- packages/core/src/main.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/packages/core/src/main.ts b/packages/core/src/main.ts index 41119b4e..bcbe8f28 100644 --- a/packages/core/src/main.ts +++ b/packages/core/src/main.ts @@ -117,13 +117,15 @@ export function getStarknet( return firstAuthorizedWallet }, - discoverVirtualWallets: async () => { - virtualWallets.forEach(async (virtualWallet) => { - const hasSupport = await virtualWallet.hasSupport(windowObject) - if (hasSupport) { - windowObject[virtualWallet.windowKey] = virtualWallet - } - }) + discoverVirtualWallets: async (): Promise => { + await Promise.all( + virtualWallets.map(async (virtualWallet) => { + const hasSupport = await virtualWallet.hasSupport(windowObject) + if (hasSupport) { + windowObject[virtualWallet.windowKey] = virtualWallet + } + }), + ) }, enable: async (inputWallet, options) => { let wallet: StarknetWindowObject From 2cccbbbbff75304b7b86ec962764ad02e26f0ad6 Mon Sep 17 00:00:00 2001 From: Florin Dzeladini Date: Fri, 6 Dec 2024 10:06:06 +0100 Subject: [PATCH 07/12] feat: allow filtering by ids or names --- packages/core/src/main.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/core/src/main.ts b/packages/core/src/main.ts index bcbe8f28..242f11a1 100644 --- a/packages/core/src/main.ts +++ b/packages/core/src/main.ts @@ -117,9 +117,22 @@ export function getStarknet( return firstAuthorizedWallet }, - discoverVirtualWallets: async (): Promise => { + discoverVirtualWallets: async ( + walletNamesOrIds: string[] = [], + ): Promise => { + const walletNamesOrIdsSet = new Set(walletNamesOrIds) + + const virtualWalletToDiscover = + walletNamesOrIdsSet.size > 0 + ? virtualWallets.filter( + (virtualWallet) => + walletNamesOrIdsSet.has(virtualWallet.name) || + walletNamesOrIdsSet.has(virtualWallet.id), + ) + : virtualWallets + await Promise.all( - virtualWallets.map(async (virtualWallet) => { + virtualWalletToDiscover.map(async (virtualWallet) => { const hasSupport = await virtualWallet.hasSupport(windowObject) if (hasSupport) { windowObject[virtualWallet.windowKey] = virtualWallet From 356a6fd25a3709c68d9eb17937c530bb3f67eafe Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Mon, 9 Dec 2024 14:44:25 +0000 Subject: [PATCH 08/12] chore: version bump --- .changeset/eleven-buttons-unite.md | 5 ----- example/CHANGELOG.md | 6 ++++++ example/package.json | 2 +- packages/core/CHANGELOG.md | 6 ++++++ packages/core/package.json | 2 +- packages/ui/CHANGELOG.md | 7 +++++++ packages/ui/package.json | 2 +- 7 files changed, 22 insertions(+), 8 deletions(-) delete mode 100644 .changeset/eleven-buttons-unite.md diff --git a/.changeset/eleven-buttons-unite.md b/.changeset/eleven-buttons-unite.md deleted file mode 100644 index 9e06eade..00000000 --- a/.changeset/eleven-buttons-unite.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -"@starknet-io/get-starknet-core": patch ---- - -Decouple Virtual Wallet Discovery for async workflow diff --git a/example/CHANGELOG.md b/example/CHANGELOG.md index bbfcaaa9..138cab76 100644 --- a/example/CHANGELOG.md +++ b/example/CHANGELOG.md @@ -1,5 +1,11 @@ # @starknet-io/get-starknet-example +## 4.0.5 + +### Patch Changes + +- @starknet-io/get-starknet@4.0.5 + ## 4.0.4 ### Patch Changes diff --git a/example/package.json b/example/package.json index 12789838..966b9222 100644 --- a/example/package.json +++ b/example/package.json @@ -1,6 +1,6 @@ { "name": "@starknet-io/get-starknet-example", - "version": "4.0.4", + "version": "4.0.5", "private": true, "type": "module", "scripts": { diff --git a/packages/core/CHANGELOG.md b/packages/core/CHANGELOG.md index a0763b81..87f7ab6f 100644 --- a/packages/core/CHANGELOG.md +++ b/packages/core/CHANGELOG.md @@ -1,5 +1,11 @@ # @starknet-io/get-starknet-core +## 4.0.5 + +### Patch Changes + +- 0263b88: Decouple Virtual Wallet Discovery for async workflow + ## 4.0.4 ### Patch Changes diff --git a/packages/core/package.json b/packages/core/package.json index 73b85a33..2fc242e0 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -1,6 +1,6 @@ { "name": "@starknet-io/get-starknet-core", - "version": "4.0.4", + "version": "4.0.5", "keywords": [ "starknet", "starkware", diff --git a/packages/ui/CHANGELOG.md b/packages/ui/CHANGELOG.md index 5545bee3..705b018c 100644 --- a/packages/ui/CHANGELOG.md +++ b/packages/ui/CHANGELOG.md @@ -1,5 +1,12 @@ # @starknet-io/get-starknet +## 4.0.5 + +### Patch Changes + +- Updated dependencies [0263b88] + - @starknet-io/get-starknet-core@4.0.5 + ## 4.0.4 ### Patch Changes diff --git a/packages/ui/package.json b/packages/ui/package.json index 9c5dbfa3..9cf24b99 100644 --- a/packages/ui/package.json +++ b/packages/ui/package.json @@ -1,6 +1,6 @@ { "name": "@starknet-io/get-starknet", - "version": "4.0.4", + "version": "4.0.5", "keywords": [ "starknet", "starkware", From 1818adf171bc464b901058b24b52114cf9659d84 Mon Sep 17 00:00:00 2001 From: stanleyyuen <102275989+stanleyyconsensys@users.noreply.github.com> Date: Wed, 11 Dec 2024 15:11:53 +0800 Subject: [PATCH 09/12] feat: add event support on MM --- .../virtualWallets/metaMaskVirtualWallet.ts | 146 ++++++++++++++---- 1 file changed, 112 insertions(+), 34 deletions(-) diff --git a/packages/core/src/wallet/virtualWallets/metaMaskVirtualWallet.ts b/packages/core/src/wallet/virtualWallets/metaMaskVirtualWallet.ts index 5ef1d637..900de719 100644 --- a/packages/core/src/wallet/virtualWallets/metaMaskVirtualWallet.ts +++ b/packages/core/src/wallet/virtualWallets/metaMaskVirtualWallet.ts @@ -1,6 +1,11 @@ import { VirtualWallet } from "../../types" import { init, loadRemote } from "@module-federation/runtime" -import { RpcMessage, StarknetWindowObject } from "@starknet-io/types-js" +import { + RequestFnCall, + RpcMessage, + StarknetWindowObject, + WalletEventHandlers, +} from "@starknet-io/types-js" import { Mutex } from "async-mutex" interface MetaMaskProvider { @@ -87,17 +92,8 @@ export type Eip6963SupportedWallet = { provider: MetaMaskProvider | null } -export type EmptyVirtualWallet = { - swo: StarknetWindowObject | null - on(): void - off(): void - request( - call: Omit, - ): Promise -} - class MetaMaskVirtualWallet - implements VirtualWallet, Eip6963SupportedWallet, EmptyVirtualWallet + implements VirtualWallet, Eip6963SupportedWallet, StarknetWindowObject { id: string = "metamask" name: string = "MetaMask" @@ -106,13 +102,39 @@ class MetaMaskVirtualWallet provider: MetaMaskProvider | null = null swo: StarknetWindowObject | null = null lock: Mutex + version: string = "v2.0.0" constructor() { this.lock = new Mutex() } + /** + * Load and resolve the `StarknetWindowObject`. + * + * @param windowObject The window object. + * @returns A promise to resolve a `StarknetWindowObject`. + */ async loadWallet( windowObject: Record, + ): Promise { + // Using `this.#loadSwoSafe` to prevent the wallet is loading in a racing condition + + await this.#loadSwoSafe(windowObject) + // Whenever trgger function call to `request` / `on` / `off`, + // it will load the wallet into the `this.swo` object and forward the function call to the `this.swo` object. + // Therefore the `MetaMaskVirtualWallet` object actually act as a proxy to the `this.swo` object. + // Thus, to standardize the behavior, we should return the `MetaMaskVirtualWallet` object here, instead of the `this.swo` object. + return this + } + + /** + * Load the remote `StarknetWindowObject` with module federation. + * + * @param windowObject The window object. + * @returns A promise to resolve a `StarknetWindowObject`. + */ + async #loadSwo( + windowObject: Record, ): Promise { if (!this.provider) { this.provider = await detectMetamaskSupport(windowObject) @@ -125,7 +147,7 @@ class MetaMaskVirtualWallet name: "MetaMaskStarknetSnapWallet", alias: "MetaMaskStarknetSnapWallet", entry: - "https://snaps.consensys.io/starknet/get-starknet/v1/remoteEntry.js", //"http://localhost:8082/remoteEntry.js", + "https://snaps.consensys.io/starknet/get-starknet/v1/remoteEntry.js", }, ], }) @@ -149,44 +171,100 @@ class MetaMaskVirtualWallet ) } + /** + * Verify if the hosting machine is support the Wallet or not without loading the wallet itself. + * + * @param windowObject The window object. + * @returns A promise to resolve a boolean value to indicate the support status. + */ async hasSupport(windowObject: Record) { this.provider = await detectMetamaskSupport(windowObject) return this.provider !== null } + /** + * Proxy the RPC request to the `this.swo` object. + * Load the `this.swo` if not loaded. + * + * @param call The RPC API arguments. + * @returns A promise to resolve a response of the proxy RPC API. + */ async request( - arg: Omit, + call: Omit, ): Promise { - const { type } = arg - // `wallet_supportedWalletApi` and `wallet_supportedSpecs` should enabled even if the wallet is not loaded/connected - switch (type) { - case "wallet_supportedWalletApi": - return ["0.7"] as unknown as Data["result"] - case "wallet_supportedSpecs": - return ["0.7"] as unknown as Data["result"] - default: - return this.#handleRequest(arg) - } + return this.#loadSwoSafe().then((swo: StarknetWindowObject) => { + // Forward the request to the `this.swo` object. + // Except RPC `wallet_supportedSpecs` and `wallet_getPermissions`, others API will trigger the Snap to install if not installed + return swo.request( + call as unknown as RequestFnCall, + ) as unknown as Data["result"] + }) } - async #handleRequest( - arg: Omit, - ): Promise { - // Using lock to ensure the load wallet operation is not fall into a racing condirtion + /** + * Subscribe the `accountsChanged` or `networkChanged` event. + * Proxy the subscription to the `this.swo` object. + * Load the `this.swo` if not loaded. + * + * @param event - The event name. + * @param handleEvent - The event handler function. + */ + on( + event: Event, + handleEvent: WalletEventHandlers[Event], + ): void { + this.#loadSwoSafe().then((swo: StarknetWindowObject) => + swo.on(event, handleEvent), + ) + } + + /** + * Un-subscribe the `accountsChanged` or `networkChanged` event for a given handler. + * Proxy the un-subscribe request to the `this.swo` object. + * Load the `this.swo` if not loaded. + * + * @param event - The event name. + * @param handleEvent - The event handler function. + */ + off( + event: Event, + handleEvent: WalletEventHandlers[Event], + ): void { + this.#loadSwoSafe().then((swo: StarknetWindowObject) => + swo.off(event, handleEvent), + ) + } + + /** + * Load the `StarknetWindowObject` safely with lock. + * And prevent the loading operation fall into a racing condirtion. + * + * @returns A promise to resolve a `StarknetWindowObject`. + */ + async #loadSwoSafe( + windowObject: Record = window, + ): Promise { return this.lock.runExclusive(async () => { // Using `this.swo` to prevent the wallet is loaded multiple times if (!this.swo) { - this.swo = await this.loadWallet(window) + this.swo = await this.#loadSwo(windowObject) + this.#bindSwoProperties() } - // forward the request to the actual connect wallet object - // it will also trigger the Snap to install if not installed - return this.swo.request(arg) as unknown as Data["result"] + return this.swo }) } - // MetaMask Snap Wallet does not support `on` and `off` method - on() {} - off() {} + /** + * Bind properties to `MetaMaskVirtualWallet` from `this.swo`. + */ + #bindSwoProperties(): void { + if (this.swo) { + this.version = this.swo.version + this.name = this.swo.name + this.id = this.swo.id + this.icon = this.swo.icon as string + } + } } const metaMaskVirtualWallet = new MetaMaskVirtualWallet() From d35f27c806b398bdfd0718793810eea642652536 Mon Sep 17 00:00:00 2001 From: stanleyyuen <102275989+stanleyyconsensys@users.noreply.github.com> Date: Wed, 11 Dec 2024 15:25:28 +0800 Subject: [PATCH 10/12] chore: update comment text --- .../core/src/wallet/virtualWallets/metaMaskVirtualWallet.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/core/src/wallet/virtualWallets/metaMaskVirtualWallet.ts b/packages/core/src/wallet/virtualWallets/metaMaskVirtualWallet.ts index 900de719..f7922686 100644 --- a/packages/core/src/wallet/virtualWallets/metaMaskVirtualWallet.ts +++ b/packages/core/src/wallet/virtualWallets/metaMaskVirtualWallet.ts @@ -117,8 +117,7 @@ class MetaMaskVirtualWallet async loadWallet( windowObject: Record, ): Promise { - // Using `this.#loadSwoSafe` to prevent the wallet is loading in a racing condition - + // Using `this.#loadSwoSafe` to prevent the wallet is loading in a racing condition. await this.#loadSwoSafe(windowObject) // Whenever trgger function call to `request` / `on` / `off`, // it will load the wallet into the `this.swo` object and forward the function call to the `this.swo` object. From 5887ee629bd08047a9e5fe0d757569c0fea4c18b Mon Sep 17 00:00:00 2001 From: stanleyyuen <102275989+stanleyyconsensys@users.noreply.github.com> Date: Wed, 11 Dec 2024 15:52:29 +0800 Subject: [PATCH 11/12] chore: fix comment text --- .../core/src/wallet/virtualWallets/metaMaskVirtualWallet.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/src/wallet/virtualWallets/metaMaskVirtualWallet.ts b/packages/core/src/wallet/virtualWallets/metaMaskVirtualWallet.ts index f7922686..e7feb107 100644 --- a/packages/core/src/wallet/virtualWallets/metaMaskVirtualWallet.ts +++ b/packages/core/src/wallet/virtualWallets/metaMaskVirtualWallet.ts @@ -193,7 +193,7 @@ class MetaMaskVirtualWallet ): Promise { return this.#loadSwoSafe().then((swo: StarknetWindowObject) => { // Forward the request to the `this.swo` object. - // Except RPC `wallet_supportedSpecs` and `wallet_getPermissions`, others API will trigger the Snap to install if not installed + // Except RPCs `wallet_supportedSpecs` and `wallet_getPermissions`, other RPCs will trigger the Snap to install if not installed. return swo.request( call as unknown as RequestFnCall, ) as unknown as Data["result"] From 3f030eb3e9c93aa1898d3866ce8f6e4c8e426869 Mon Sep 17 00:00:00 2001 From: stanleyyuen <102275989+stanleyyconsensys@users.noreply.github.com> Date: Fri, 13 Dec 2024 08:01:08 +0800 Subject: [PATCH 12/12] chore: resolve comment --- .../virtualWallets/metaMaskVirtualWallet.ts | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/packages/core/src/wallet/virtualWallets/metaMaskVirtualWallet.ts b/packages/core/src/wallet/virtualWallets/metaMaskVirtualWallet.ts index e7feb107..15558e50 100644 --- a/packages/core/src/wallet/virtualWallets/metaMaskVirtualWallet.ts +++ b/packages/core/src/wallet/virtualWallets/metaMaskVirtualWallet.ts @@ -117,12 +117,13 @@ class MetaMaskVirtualWallet async loadWallet( windowObject: Record, ): Promise { - // Using `this.#loadSwoSafe` to prevent the wallet is loading in a racing condition. + // Using `this.#loadSwoSafe` to prevent race condition when the wallet is loading. await this.#loadSwoSafe(windowObject) - // Whenever trgger function call to `request` / `on` / `off`, - // it will load the wallet into the `this.swo` object and forward the function call to the `this.swo` object. - // Therefore the `MetaMaskVirtualWallet` object actually act as a proxy to the `this.swo` object. - // Thus, to standardize the behavior, we should return the `MetaMaskVirtualWallet` object here, instead of the `this.swo` object. + // The `MetaMaskVirtualWallet` object acts as a proxy for the `this.swo` object. + // When `request`, `on`, or `off` is called, the wallet is loaded into `this.swo`, + // and the function call is forwarded to it. + // To maintain consistent behaviour, the `MetaMaskVirtualWallet` + // object (`this`) is returned instead of `this.swo`. return this } @@ -171,10 +172,10 @@ class MetaMaskVirtualWallet } /** - * Verify if the hosting machine is support the Wallet or not without loading the wallet itself. + * Verify if the hosting machine supports the Wallet or not without loading the wallet itself. * * @param windowObject The window object. - * @returns A promise to resolve a boolean value to indicate the support status. + * @returns A promise that resolves to a boolean value to indicate the support status. */ async hasSupport(windowObject: Record) { this.provider = await detectMetamaskSupport(windowObject) @@ -236,7 +237,7 @@ class MetaMaskVirtualWallet /** * Load the `StarknetWindowObject` safely with lock. - * And prevent the loading operation fall into a racing condirtion. + * And prevent the loading operation fall into a racing condition. * * @returns A promise to resolve a `StarknetWindowObject`. */