From c23b9c5ba93fa7c5354555d4711e72b4e21d72df Mon Sep 17 00:00:00 2001 From: bluecco Date: Wed, 18 Oct 2023 16:51:00 +0200 Subject: [PATCH 1/7] fix: manage dapp disconnection with mobile connector --- src/connectors/argentMobile/index.ts | 17 ++++++++ .../argentMobile/modal/argentModal.ts | 5 ++- src/main.ts | 42 +++++++++++++++---- 3 files changed, 54 insertions(+), 10 deletions(-) diff --git a/src/connectors/argentMobile/index.ts b/src/connectors/argentMobile/index.ts index d21edfc..ab216c6 100644 --- a/src/connectors/argentMobile/index.ts +++ b/src/connectors/argentMobile/index.ts @@ -12,6 +12,8 @@ import { } from "../../errors" import { resetWalletConnect } from "../../helpers/resetWalletConnect" import { Connector } from "../connector" +import type { StarknetAdapter } from "./modal/starknet/adapter" +import { removeStarknetLastConnectedWallet } from "../../helpers/lastConnected" export interface ArgentMobileConnectorOptions { dappName?: string @@ -85,6 +87,8 @@ export class ArgentMobileConnector extends Connector { } async disconnect(): Promise { + // wallet connect rpc enable + await (this._wallet as StarknetAdapter).disable() resetWalletConnect() if (!this.available() && !this._wallet) { @@ -162,5 +166,18 @@ export class ArgentMobileConnector extends Connector { const _wallet = await getStarknetWindowObject(options) this._wallet = _wallet + + // wallet connect rpc enable + const snProvider = this._wallet as StarknetAdapter + await snProvider.enable() + snProvider.client.on("session_delete", () => { + // Session was deleted -> reset the dapp state, clean up from user session, etc. + // not calling disconnect(), because .disable() is already done by the mobile app + resetWalletConnect() + this._wallet = null + removeStarknetLastConnectedWallet() + // dapp should listen to this event and update the UI accordingly + document.dispatchEvent(new Event("snDappDisconnectedFromMobile")) + }) } } diff --git a/src/connectors/argentMobile/modal/argentModal.ts b/src/connectors/argentMobile/modal/argentModal.ts index cb7a309..2c96661 100644 --- a/src/connectors/argentMobile/modal/argentModal.ts +++ b/src/connectors/argentMobile/modal/argentModal.ts @@ -70,10 +70,11 @@ class ArgentModal { public showConnectionModal(wcUri: string) { const wcParam = encodeURIComponent(wcUri) + const href = encodeURIComponent(window.location.href) this.showModal({ desktop: `${this.bridgeUrl}?wc=${wcParam}`, - ios: `${this.mobileUrl}app/wc?uri=${wcParam}`, - android: `${this.mobileUrl}app/wc?uri=${wcParam}`, + ios: `${this.mobileUrl}app/wc?uri=${wcParam}&href=${href}`, + android: `${this.mobileUrl}app/wc?uri=${wcParam}&href=${href}`, }) } diff --git a/src/main.ts b/src/main.ts index e966625..e024129 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,21 +1,28 @@ -import type { DisconnectOptions, StarknetWindowObject } from "get-starknet-core" +import type { + ConnectedStarknetWindowObject, + DisconnectOptions, + DisconnectedStarknetWindowObject, + StarknetWindowObject, +} from "get-starknet-core" import sn from "get-starknet-core" import { getStoreVersionFromBrowser } from "./helpers/getStoreVersionFromBrowser" import { DEFAULT_WEBWALLET_URL } from "./connectors/webwallet/constants" import { defaultConnectors } from "./helpers/defaultConnectors" -import { mapModalWallets } from "./helpers/mapModalWallets" -import { resetWalletConnect } from "./helpers/resetWalletConnect" -import Modal from "./modal/Modal.svelte" -import type { ConnectOptions, ModalWallet } from "./types/modal" import { removeStarknetLastConnectedWallet, setStarknetLastConnectedWallet, } from "./helpers/lastConnected" +import { mapModalWallets } from "./helpers/mapModalWallets" +import Modal from "./modal/Modal.svelte" +import type { ConnectOptions, ModalWallet } from "./types/modal" +import { ArgentMobileConnector, Connector } from "./connectors" import css from "./theme.css?inline" +let selectedConnector: Connector | null = null + export const connect = async ({ modalMode = "canAsk", storeVersion = getStoreVersionFromBrowser(), @@ -26,6 +33,8 @@ export const connect = async ({ connectors = [], ...restOptions }: ConnectOptions = {}): Promise => { + // force null in case it was disconnected from mobile app + selectedConnector = null const availableConnectors = !connectors || connectors.length === 0 ? defaultConnectors({ @@ -38,7 +47,8 @@ export const connect = async ({ if (modalMode === "neverAsk") { const connector = availableConnectors.find((c) => c.id === lastWalletId) await connector?.connect() - return connector?.wallet || null + selectedConnector = connector ?? null + return connector?.wallet ?? null } const installedWallets = await sn.getAvailableWallets(restOptions) @@ -58,6 +68,7 @@ export const connect = async ({ if (wallet) { const connector = availableConnectors.find((c) => c.id === lastWalletId) await connector?.connect() + selectedConnector = connector return wallet } // otherwise fallback to modal } @@ -85,6 +96,9 @@ export const connect = async ({ if (value.id !== "argentWebWallet") { setStarknetLastConnectedWallet(value.id) } + selectedConnector = + availableConnectors.find((c) => c.id === value.id) ?? null + resolve(value) } finally { setTimeout(() => modal.$destroy()) @@ -97,8 +111,20 @@ export const connect = async ({ }) } -export function disconnect(options: DisconnectOptions = {}): Promise { - resetWalletConnect() +export const disconnect = async (options: DisconnectOptions = {}) => { removeStarknetLastConnectedWallet() + if (selectedConnector.id === "argentMobile") { + const mobileConnector = selectedConnector as ArgentMobileConnector + await mobileConnector.disconnect() + } + selectedConnector = null + return sn.disconnect(options) } + +export type { + ConnectedStarknetWindowObject, + DisconnectOptions, + DisconnectedStarknetWindowObject, + StarknetWindowObject, +} From 7ff92c80c1d05853368eae6a2f50abf0238ce533 Mon Sep 17 00:00:00 2001 From: bluecco Date: Wed, 18 Oct 2023 17:51:27 +0200 Subject: [PATCH 2/7] chore: update event name --- src/connectors/argentMobile/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/connectors/argentMobile/index.ts b/src/connectors/argentMobile/index.ts index ab216c6..56ce1ec 100644 --- a/src/connectors/argentMobile/index.ts +++ b/src/connectors/argentMobile/index.ts @@ -177,7 +177,7 @@ export class ArgentMobileConnector extends Connector { this._wallet = null removeStarknetLastConnectedWallet() // dapp should listen to this event and update the UI accordingly - document.dispatchEvent(new Event("snDappDisconnectedFromMobile")) + document.dispatchEvent(new Event("wallet_disconnected")) }) } } From bc65125fcd4c16c6dca13ba84c2ac9f232c3a07d Mon Sep 17 00:00:00 2001 From: bluecco Date: Wed, 18 Oct 2023 18:29:15 +0200 Subject: [PATCH 3/7] fix: avoid safari popup blocking --- src/connectors/argentMobile/modal/argentModal.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/connectors/argentMobile/modal/argentModal.ts b/src/connectors/argentMobile/modal/argentModal.ts index 2c96661..f955a17 100644 --- a/src/connectors/argentMobile/modal/argentModal.ts +++ b/src/connectors/argentMobile/modal/argentModal.ts @@ -120,7 +120,11 @@ class ArgentModal { } if (device === "android" || device === "ios") { - window.open(urls[device]) + const toMobileApp = document.createElement("a") + toMobileApp.setAttribute("href", urls[device]) + toMobileApp.setAttribute("target", "_blank") + toMobileApp.click() + return } if (this.type === "window") { From 99b2b1fde9c598a646b99713bf461a273d866ecc Mon Sep 17 00:00:00 2001 From: bluecco Date: Thu, 19 Oct 2023 13:32:57 +0200 Subject: [PATCH 4/7] fix: getters and update disconnect method --- src/connectors/argentMobile/index.ts | 1 - src/connectors/injected/index.ts | 3 --- src/connectors/webwallet/index.ts | 1 - src/main.ts | 6 +----- 4 files changed, 1 insertion(+), 10 deletions(-) diff --git a/src/connectors/argentMobile/index.ts b/src/connectors/argentMobile/index.ts index 56ce1ec..94f9b10 100644 --- a/src/connectors/argentMobile/index.ts +++ b/src/connectors/argentMobile/index.ts @@ -69,7 +69,6 @@ export class ArgentMobileConnector extends Connector { } get wallet(): StarknetWindowObject { - this.ensureWallet() if (!this._wallet) { throw new ConnectorNotConnectedError() } diff --git a/src/connectors/injected/index.ts b/src/connectors/injected/index.ts index 36a9921..1709049 100644 --- a/src/connectors/injected/index.ts +++ b/src/connectors/injected/index.ts @@ -102,7 +102,6 @@ export class InjectedConnector extends Connector { } get name(): string { - this.ensureWallet() if (!this._wallet) { throw new ConnectorNotConnectedError() } @@ -110,7 +109,6 @@ export class InjectedConnector extends Connector { } get icon(): string { - this.ensureWallet() if (!this._wallet) { throw new ConnectorNotConnectedError() } @@ -118,7 +116,6 @@ export class InjectedConnector extends Connector { } get wallet(): StarknetWindowObject { - this.ensureWallet() if (!this._wallet) { throw new ConnectorNotConnectedError() } diff --git a/src/connectors/webwallet/index.ts b/src/connectors/webwallet/index.ts index 3aa293e..1c990c8 100644 --- a/src/connectors/webwallet/index.ts +++ b/src/connectors/webwallet/index.ts @@ -72,7 +72,6 @@ export class WebWalletConnector extends Connector { } get wallet(): StarknetWindowObject { - this.ensureWallet() if (!this._wallet) { throw new ConnectorNotConnectedError() } diff --git a/src/main.ts b/src/main.ts index e024129..5799138 100644 --- a/src/main.ts +++ b/src/main.ts @@ -98,7 +98,6 @@ export const connect = async ({ } selectedConnector = availableConnectors.find((c) => c.id === value.id) ?? null - resolve(value) } finally { setTimeout(() => modal.$destroy()) @@ -113,10 +112,7 @@ export const connect = async ({ export const disconnect = async (options: DisconnectOptions = {}) => { removeStarknetLastConnectedWallet() - if (selectedConnector.id === "argentMobile") { - const mobileConnector = selectedConnector as ArgentMobileConnector - await mobileConnector.disconnect() - } + await selectedConnector.disconnect() selectedConnector = null return sn.disconnect(options) From 9a4101a90871c9c392a5437475a5f0e0ddbcfa7f Mon Sep 17 00:00:00 2001 From: bluecco Date: Thu, 19 Oct 2023 14:06:18 +0200 Subject: [PATCH 5/7] chore: add device to argent modal urls --- src/connectors/argentMobile/modal/argentModal.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/connectors/argentMobile/modal/argentModal.ts b/src/connectors/argentMobile/modal/argentModal.ts index f955a17..027583d 100644 --- a/src/connectors/argentMobile/modal/argentModal.ts +++ b/src/connectors/argentMobile/modal/argentModal.ts @@ -71,10 +71,11 @@ class ArgentModal { public showConnectionModal(wcUri: string) { const wcParam = encodeURIComponent(wcUri) const href = encodeURIComponent(window.location.href) + this.showModal({ - desktop: `${this.bridgeUrl}?wc=${wcParam}`, - ios: `${this.mobileUrl}app/wc?uri=${wcParam}&href=${href}`, - android: `${this.mobileUrl}app/wc?uri=${wcParam}&href=${href}`, + desktop: `${this.bridgeUrl}?wc=${wcParam}&device=desktop`, + ios: `${this.mobileUrl}app/wc?uri=${wcParam}&href=${href}&device=mobile`, + android: `${this.mobileUrl}app/wc?uri=${wcParam}&href=${href}&device=mobile`, }) } @@ -95,9 +96,9 @@ class ArgentModal { this should be ignored and not considered valid as it's only used for automatically redirecting the users to approve or reject a signing request. */ this.showModal({ - desktop: `${this.bridgeUrl}?action=sign`, - ios: `${this.mobileUrl}app/wc?uri=${href}`, - android: `${this.mobileUrl}app/wc?uri=${href}`, + desktop: `${this.bridgeUrl}?action=sign&device=desktop`, + ios: `${this.mobileUrl}app/wc?uri=${href}&device=mobile`, + android: `${this.mobileUrl}app/wc?uri=${href}&device=mobile`, }) } From 4c31f023f370aad2db0c2a116f72b48465dbc800 Mon Sep 17 00:00:00 2001 From: bluecco Date: Thu, 19 Oct 2023 14:40:34 +0200 Subject: [PATCH 6/7] fix: update request modal urls --- src/connectors/argentMobile/modal/argentModal.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/connectors/argentMobile/modal/argentModal.ts b/src/connectors/argentMobile/modal/argentModal.ts index 027583d..9721d31 100644 --- a/src/connectors/argentMobile/modal/argentModal.ts +++ b/src/connectors/argentMobile/modal/argentModal.ts @@ -97,8 +97,8 @@ class ArgentModal { */ this.showModal({ desktop: `${this.bridgeUrl}?action=sign&device=desktop`, - ios: `${this.mobileUrl}app/wc?uri=${href}&device=mobile`, - android: `${this.mobileUrl}app/wc?uri=${href}&device=mobile`, + ios: `${this.mobileUrl}app/wc/request?href=${href}&device=mobile`, + android: `${this.mobileUrl}app/wc/request?href=${href}&device=mobile`, }) } From 42395d7619168e512b2d3bb903f62d82f32aadce Mon Sep 17 00:00:00 2001 From: semantic-release-bot Date: Fri, 20 Oct 2023 09:33:41 +0000 Subject: [PATCH 7/7] chore(release): 1.0.10 [skip ci] ## [1.0.10](https://github.com/argentlabs/starknetkit/compare/v1.0.9...v1.0.10) (2023-10-20) ### Bug Fixes * avoid safari popup blocking ([bc65125](https://github.com/argentlabs/starknetkit/commit/bc65125fcd4c16c6dca13ba84c2ac9f232c3a07d)) * getters and update disconnect method ([99b2b1f](https://github.com/argentlabs/starknetkit/commit/99b2b1fde9c598a646b99713bf461a273d866ecc)) * manage dapp disconnection with mobile connector ([c23b9c5](https://github.com/argentlabs/starknetkit/commit/c23b9c5ba93fa7c5354555d4711e72b4e21d72df)) * update request modal urls ([4c31f02](https://github.com/argentlabs/starknetkit/commit/4c31f023f370aad2db0c2a116f72b48465dbc800)) --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 90024b7..35d85fe 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "starknetkit", - "version": "1.0.9", + "version": "1.0.10", "repository": "github:argentlabs/starknetkit", "private": false, "browser": {