From 799743bdd24b5f378fd6dd1c28f4a07f2d07517a Mon Sep 17 00:00:00 2001 From: bluecco Date: Thu, 7 Dec 2023 09:52:43 +0100 Subject: [PATCH 01/17] feat: add starknetkit hook and return connector instead of wallets --- src/connectors/injected/index.ts | 3 ++- src/connectors/webwallet/index.ts | 2 ++ src/helpers/defaultConnectors.ts | 5 ++++- src/hooks/useStarknetkitConnectors.ts | 31 +++++++++++++++++++++++++++ src/main.ts | 29 +++++++++++++------------ src/modal/Modal.svelte | 12 +++++------ 6 files changed, 59 insertions(+), 23 deletions(-) create mode 100644 src/hooks/useStarknetkitConnectors.ts diff --git a/src/connectors/injected/index.ts b/src/connectors/injected/index.ts index fb29653..164d5b0 100644 --- a/src/connectors/injected/index.ts +++ b/src/connectors/injected/index.ts @@ -15,6 +15,7 @@ import { WALLET_NOT_FOUND_ICON_DARK, WALLET_NOT_FOUND_ICON_LIGHT, } from "./constants" +import { removeStarknetLastConnectedWallet } from "../../helpers/lastConnected" /** Injected connector options. */ export interface InjectedConnectorOptions { /** The wallet id. */ @@ -151,7 +152,7 @@ export class InjectedConnector extends Connector { async disconnect(): Promise { this.ensureWallet() - + removeStarknetLastConnectedWallet() if (!this.available()) { throw new ConnectorNotFoundError() } diff --git a/src/connectors/webwallet/index.ts b/src/connectors/webwallet/index.ts index 18ddfb8..e8cf694 100644 --- a/src/connectors/webwallet/index.ts +++ b/src/connectors/webwallet/index.ts @@ -18,6 +18,7 @@ import { } from "../../errors" import { DEFAULT_WEBWALLET_ICON, DEFAULT_WEBWALLET_URL } from "./constants" import { getWebWalletStarknetObject } from "./starknetWindowObject/getWebWalletStarknetObject" +import { removeStarknetLastConnectedWallet } from "../../helpers/lastConnected" let _wallet: StarknetWindowObject | null = null @@ -118,6 +119,7 @@ export class WebWalletConnector extends Connector { } _wallet = null this._wallet = _wallet + removeStarknetLastConnectedWallet() } async account(): Promise { diff --git a/src/helpers/defaultConnectors.ts b/src/helpers/defaultConnectors.ts index 5a36195..e468e3b 100644 --- a/src/helpers/defaultConnectors.ts +++ b/src/helpers/defaultConnectors.ts @@ -13,7 +13,10 @@ export const defaultConnectors = ({ argentMobileOptions?: ArgentMobileConnectorOptions webWalletUrl?: string }): Connector[] => { - const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent) + const isSafari = + typeof window !== "undefined" + ? /^((?!chrome|android).)*safari/i.test(navigator.userAgent) + : false const defaultConnectors: Connector[] = [] diff --git a/src/hooks/useStarknetkitConnectors.ts b/src/hooks/useStarknetkitConnectors.ts new file mode 100644 index 0000000..230c9eb --- /dev/null +++ b/src/hooks/useStarknetkitConnectors.ts @@ -0,0 +1,31 @@ +import { type Connector } from "../connectors" +import { defaultConnectors } from "../helpers/defaultConnectors" +import { connect } from "../main" +import { ConnectOptions } from "../types/modal" + +type UseStarknetkitConnectors = { + connectors?: Connector[] + starknetkitConnect: () => Promise +} + +const useStarknetkitConnectors = ( + options: ConnectOptions, +): UseStarknetkitConnectors => { + const starknetkitConnect = async () => { + return await connect(options) + } + + const connectors = + options?.connectors || + defaultConnectors({ + argentMobileOptions: options?.argentMobileOptions, + webWalletUrl: options?.webWalletUrl, + }) + + return { + connectors, + starknetkitConnect, + } +} + +export { useStarknetkitConnectors } diff --git a/src/main.ts b/src/main.ts index ddbb659..9c1bf68 100644 --- a/src/main.ts +++ b/src/main.ts @@ -18,7 +18,7 @@ import { mapModalWallets } from "./helpers/mapModalWallets" import Modal from "./modal/Modal.svelte" import type { ConnectOptions, ModalWallet } from "./types/modal" -import { Connector } from "./connectors" +import { type Connector } from "./connectors" import css from "./theme.css?inline" let selectedConnector: Connector | null = null @@ -32,7 +32,7 @@ export const connect = async ({ argentMobileOptions, connectors = [], ...restOptions -}: ConnectOptions = {}): Promise => { +}: ConnectOptions = {}): Promise => { // force null in case it was disconnected from mobile app selectedConnector = null const availableConnectors = @@ -46,9 +46,8 @@ export const connect = async ({ const lastWalletId = localStorage.getItem("starknetLastConnectedWallet") if (modalMode === "neverAsk") { const connector = availableConnectors.find((c) => c.id === lastWalletId) - await connector?.connect() selectedConnector = connector ?? null - return connector?.wallet ?? null + return connector } const installedWallets = await sn.getAvailableWallets(restOptions) @@ -67,11 +66,10 @@ export const connect = async ({ if (wallet) { const connector = availableConnectors.find((c) => c.id === lastWalletId) - await connector?.connect() if (connector) { selectedConnector = connector } - return wallet + return connector } // otherwise fallback to modal } @@ -93,16 +91,15 @@ export const connect = async ({ target, props: { dappName, - callback: async (value: StarknetWindowObject | null) => { + callback: async (connector: Connector | null) => { try { - if (value !== null && value.id !== "argentWebWallet") { - setStarknetLastConnectedWallet(value.id) + selectedConnector = connector + + if (connector !== null && connector.id !== "argentWebWallet") { + setStarknetLastConnectedWallet(connector.id) } - selectedConnector = - availableConnectors.find( - (c) => value !== null && c.id === value.id, - ) ?? null - resolve(value) + + resolve(connector) } finally { setTimeout(() => modal.$destroy()) } @@ -129,8 +126,12 @@ export const disconnect = async (options: DisconnectOptions = {}) => { } export type { + Connector, ConnectedStarknetWindowObject, DisconnectOptions, DisconnectedStarknetWindowObject, StarknetWindowObject, + defaultConnectors as starknetkitDefaultConnectors, } + +export { useStarknetkitConnectors } from "./hooks/useStarknetkitConnectors" diff --git a/src/modal/Modal.svelte b/src/modal/Modal.svelte index c80d88e..3819f17 100644 --- a/src/modal/Modal.svelte +++ b/src/modal/Modal.svelte @@ -5,11 +5,12 @@ import ConnectorButton from "./ConnectorButton.svelte" import type { ModalWallet } from "../types/modal" import type { Connector } from "../connectors/connector" + import { InjectedConnector } from "../connectors/injected" export let dappName: string = window?.document.title ?? "" export let modalWallets: ModalWallet[] export let callback: ( - value: StarknetWindowObject | null, + value: Connector | null, ) => Promise = async () => {} export let theme: "light" | "dark" | null = null @@ -30,8 +31,7 @@ let cb = async (connector: Connector | null) => { setLoadingItem(connector?.id ?? false) try { - await connector?.connect() - await callback(connector?.wallet ?? null) + await callback(connector ?? null) } catch (e) { console.error(e) } finally { @@ -53,8 +53,7 @@ if (isInAppBrowser && window?.starknet_argentX) { try { - const enabledValue = await sn.enable(window?.starknet_argentX) - callback(enabledValue ?? window?.starknet_argentX) + callback(new InjectedConnector({ options: { id: "argentX" } })) } catch {} return } @@ -62,8 +61,7 @@ if (emailOnly) { try { const [wallet] = modalWallets - await wallet.connector?.connect() - callback(wallet.connector.wallet) + await callback(wallet.connector) } catch (e) { console.error(e) } From 328e0156240da1b2b27f3e37499ef74c9984925a Mon Sep 17 00:00:00 2001 From: bluecco Date: Tue, 12 Dec 2023 10:36:36 +0100 Subject: [PATCH 02/17] chore: refactor useStarknetkitConnectors params --- src/hooks/useStarknetkitConnectors.ts | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/src/hooks/useStarknetkitConnectors.ts b/src/hooks/useStarknetkitConnectors.ts index 230c9eb..3719598 100644 --- a/src/hooks/useStarknetkitConnectors.ts +++ b/src/hooks/useStarknetkitConnectors.ts @@ -1,29 +1,21 @@ import { type Connector } from "../connectors" -import { defaultConnectors } from "../helpers/defaultConnectors" import { connect } from "../main" import { ConnectOptions } from "../types/modal" type UseStarknetkitConnectors = { - connectors?: Connector[] + connectors: Connector[] starknetkitConnect: () => Promise } const useStarknetkitConnectors = ( - options: ConnectOptions, + options: Omit, ): UseStarknetkitConnectors => { const starknetkitConnect = async () => { return await connect(options) } - const connectors = - options?.connectors || - defaultConnectors({ - argentMobileOptions: options?.argentMobileOptions, - webWalletUrl: options?.webWalletUrl, - }) - return { - connectors, + connectors: options.connectors, starknetkitConnect, } } From 1e5cdc6371a506737122bab386825e4734a45110 Mon Sep 17 00:00:00 2001 From: bluecco Date: Tue, 12 Dec 2023 15:28:10 +0100 Subject: [PATCH 03/17] chore: refactor useStarknetkitConnector --- src/hooks/useStarknetkitConnectModal.ts | 23 ++++++++++++ src/hooks/useStarknetkitConnectors.ts | 23 ------------ src/main.ts | 50 ++++++++++++++++++++----- src/types/modal.ts | 8 +++- 4 files changed, 70 insertions(+), 34 deletions(-) create mode 100644 src/hooks/useStarknetkitConnectModal.ts delete mode 100644 src/hooks/useStarknetkitConnectors.ts diff --git a/src/hooks/useStarknetkitConnectModal.ts b/src/hooks/useStarknetkitConnectModal.ts new file mode 100644 index 0000000..dfb894c --- /dev/null +++ b/src/hooks/useStarknetkitConnectModal.ts @@ -0,0 +1,23 @@ +import { connect } from "../main" +import { ConnectOptions, ModalResult } from "../types/modal" + +type UseStarknetkitConnectors = { + starknetkitConnectModal: () => Promise +} + +const useStarknetkitConnectModal = ( + options: Omit, +): UseStarknetkitConnectors => { + const starknetkitConnectModal = async (): Promise => { + return await connect({ + ...options, + resultType: options.resultType ?? "connector", + }) + } + + return { + starknetkitConnectModal, + } +} + +export { useStarknetkitConnectModal } diff --git a/src/hooks/useStarknetkitConnectors.ts b/src/hooks/useStarknetkitConnectors.ts deleted file mode 100644 index 3719598..0000000 --- a/src/hooks/useStarknetkitConnectors.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { type Connector } from "../connectors" -import { connect } from "../main" -import { ConnectOptions } from "../types/modal" - -type UseStarknetkitConnectors = { - connectors: Connector[] - starknetkitConnect: () => Promise -} - -const useStarknetkitConnectors = ( - options: Omit, -): UseStarknetkitConnectors => { - const starknetkitConnect = async () => { - return await connect(options) - } - - return { - connectors: options.connectors, - starknetkitConnect, - } -} - -export { useStarknetkitConnectors } diff --git a/src/main.ts b/src/main.ts index 37cd0d5..0248c65 100644 --- a/src/main.ts +++ b/src/main.ts @@ -16,7 +16,7 @@ import { } from "./helpers/lastConnected" import { mapModalWallets } from "./helpers/mapModalWallets" import Modal from "./modal/Modal.svelte" -import type { ConnectOptions, ModalWallet } from "./types/modal" +import type { ConnectOptions, ModalResult, ModalWallet } from "./types/modal" import { type Connector } from "./connectors" import css from "./theme.css?inline" @@ -31,8 +31,9 @@ export const connect = async ({ webWalletUrl = DEFAULT_WEBWALLET_URL, argentMobileOptions, connectors = [], + resultType = "wallet", ...restOptions -}: ConnectOptions = {}): Promise => { +}: ConnectOptions = {}): Promise => { // force null in case it was disconnected from mobile app selectedConnector = null const availableConnectors = @@ -46,8 +47,17 @@ export const connect = async ({ const lastWalletId = localStorage.getItem("starknetLastConnectedWallet") if (modalMode === "neverAsk") { const connector = availableConnectors.find((c) => c.id === lastWalletId) + + if (resultType === "wallet") { + await connector?.connect() + } + selectedConnector = connector ?? null - return connector + + return { + connector, + wallet: connector?.wallet ?? null, + } } const installedWallets = await sn.getAvailableWallets(restOptions) @@ -66,10 +76,19 @@ export const connect = async ({ if (wallet) { const connector = availableConnectors.find((c) => c.id === lastWalletId) + + if (resultType === "wallet") { + await connector?.connect() + } + if (connector) { selectedConnector = connector } - return connector + + return { + connector, + wallet: connector?.wallet ?? null, + } } // otherwise fallback to modal } @@ -113,11 +132,22 @@ export const connect = async ({ try { selectedConnector = connector - if (connector !== null && connector.id !== "argentWebWallet") { - setStarknetLastConnectedWallet(connector.id) + if (resultType === "wallet") { + await connector?.connect() + + if (connector !== null && connector.id !== "argentWebWallet") { + setStarknetLastConnectedWallet(connector.id) + } + + resolve({ + connector, + wallet: connector?.wallet ?? null, + }) + } else { + resolve({ + connector, + }) } - - resolve(connector) } finally { setTimeout(() => modal.$destroy()) } @@ -144,12 +174,12 @@ export const disconnect = async (options: DisconnectOptions = {}) => { } export type { - Connector, ConnectedStarknetWindowObject, + Connector, DisconnectOptions, DisconnectedStarknetWindowObject, StarknetWindowObject, defaultConnectors as starknetkitDefaultConnectors, } -export { useStarknetkitConnectors } from "./hooks/useStarknetkitConnectors" +export { useStarknetkitConnectModal } from "./hooks/useStarknetkitConnectModal" diff --git a/src/types/modal.ts b/src/types/modal.ts index 91608dd..604e668 100644 --- a/src/types/modal.ts +++ b/src/types/modal.ts @@ -1,4 +1,4 @@ -import type { GetWalletOptions } from "get-starknet-core" +import type { GetWalletOptions, StarknetWindowObject } from "get-starknet-core" import type { Connector, ConnectorIcons } from "../connectors/connector" import type { ArgentMobileConnectorOptions } from "../connectors/argentMobile" @@ -12,6 +12,7 @@ export interface ConnectOptions extends GetWalletOptions { modalTheme?: "light" | "dark" | "system" storeVersion?: StoreVersion | null webWalletUrl?: string + resultType?: "connector" | "wallet" } export type ModalWallet = { @@ -23,3 +24,8 @@ export type ModalWallet = { title?: string connector: Connector } + +export type ModalResult = { + connector: Connector + wallet?: StarknetWindowObject | null +} From 37121fbaa6b2128e9099e4660103c451edd063b6 Mon Sep 17 00:00:00 2001 From: bluecco Date: Tue, 9 Jan 2024 10:22:44 +0100 Subject: [PATCH 04/17] fix: default to mainnet for argent mobile --- src/connectors/argentMobile/constants.ts | 6 ++++ src/connectors/argentMobile/index.ts | 34 ++++++++++++------- src/connectors/argentMobile/modal/adapter.ts | 2 ++ src/connectors/argentMobile/modal/login.ts | 6 ++-- .../argentMobile/modal/starknet/adapter.ts | 6 ++-- 5 files changed, 36 insertions(+), 18 deletions(-) diff --git a/src/connectors/argentMobile/constants.ts b/src/connectors/argentMobile/constants.ts index 8d6fb77..c5919b6 100644 --- a/src/connectors/argentMobile/constants.ts +++ b/src/connectors/argentMobile/constants.ts @@ -13,3 +13,9 @@ export const DEFAULT_ARGENT_MOBILE_ICON = ` ` + +export const RPC_NODE_URL_TESTNET = + "https://api.hydrogen.argent47.net/v1/starknet/goerli/rpc/v0.5" + +export const RPC_NODE_URL_MAINNET = + "https://cloud.argent-api.com/v1/starknet/mainnet/rpc/v0.5" diff --git a/src/connectors/argentMobile/index.ts b/src/connectors/argentMobile/index.ts index a3e0524..65f5d0c 100644 --- a/src/connectors/argentMobile/index.ts +++ b/src/connectors/argentMobile/index.ts @@ -18,6 +18,7 @@ import { } from "../connector" import type { StarknetAdapter } from "./modal/starknet/adapter" import { removeStarknetLastConnectedWallet } from "../../helpers/lastConnected" +import { RPC_NODE_URL_MAINNET, RPC_NODE_URL_TESTNET } from "./constants" export interface ArgentMobileConnectorOptions { dappName?: string @@ -27,6 +28,7 @@ export interface ArgentMobileConnectorOptions { url?: string icons?: string[] provider?: ProviderInterface + rpcUrl?: string } export class ArgentMobileConnector extends Connector { @@ -145,8 +147,23 @@ export class ArgentMobileConnector extends Connector { private async ensureWallet(): Promise { const { getStarknetWindowObject } = await import("./modal") - const { chainId, projectId, dappName, description, url, icons } = - this._options + const { + chainId, + projectId, + dappName, + description, + url, + icons, + provider, + rpcUrl, + } = this._options + + const providerRpcUrl = + rpcUrl ?? + (!chainId || chainId === constants.NetworkName.SN_MAIN + ? RPC_NODE_URL_MAINNET + : RPC_NODE_URL_TESTNET) + const options = { chainId: chainId ?? constants.NetworkName.SN_MAIN, name: dappName, @@ -154,10 +171,8 @@ export class ArgentMobileConnector extends Connector { description, url, icons, - rpcUrl: - chainId === constants.NetworkName.SN_MAIN - ? "https://cloud.argent-api.com/v1/starknet/mainnet/rpc/v0.5" - : "https://api.hydrogen.argent47.net/v1/starknet/goerli/rpc/v0.5", + provider, + rpcUrl: providerRpcUrl, } if (projectId === DEFAULT_PROJECT_ID) { @@ -177,13 +192,6 @@ export class ArgentMobileConnector extends Connector { const _wallet = await getStarknetWindowObject(options) - const { provider } = this._options - if (provider) { - Object.assign(_wallet, { - provider, - }) - } - this._wallet = _wallet // wallet connect rpc enable diff --git a/src/connectors/argentMobile/modal/adapter.ts b/src/connectors/argentMobile/modal/adapter.ts index c0be7c3..0ab6503 100644 --- a/src/connectors/argentMobile/modal/adapter.ts +++ b/src/connectors/argentMobile/modal/adapter.ts @@ -14,11 +14,13 @@ import { } from "@walletconnect/utils" import type { EthereumRpcConfig } from "./starknet/adapter" +import { ProviderInterface } from "starknet" export interface NamespaceAdapterOptions { client: SignClient chainId?: string | number rpcUrl?: string + provider: ProviderInterface } export abstract class NamespaceAdapter { diff --git a/src/connectors/argentMobile/modal/login.ts b/src/connectors/argentMobile/modal/login.ts index 6d63548..1aefd7c 100644 --- a/src/connectors/argentMobile/modal/login.ts +++ b/src/connectors/argentMobile/modal/login.ts @@ -1,7 +1,7 @@ import SignClient from "@walletconnect/sign-client" import type { SignClientTypes } from "@walletconnect/types" -import { constants } from "starknet" +import { ProviderInterface, constants } from "starknet" // Using NetworkName as a value. const Network: typeof constants.NetworkName = constants.NetworkName @@ -22,6 +22,7 @@ export interface IArgentLoginOptions { mobileUrl?: string modalType?: "overlay" | "window" walletConnect?: SignClientTypes.Options + provider?: ProviderInterface } export const login = async ( @@ -37,6 +38,7 @@ export const login = async ( url, icons, walletConnect, + provider, }: IArgentLoginOptions, Adapter: new (options: NamespaceAdapterOptions) => TAdapter, ): Promise => { @@ -63,7 +65,7 @@ export const login = async ( } const client = await SignClient.init(signClientOptions) - const adapter = new Adapter({ client, chainId, rpcUrl }) + const adapter = new Adapter({ client, chainId, rpcUrl, provider }) client.on("session_event", (_) => { // Handle session events, such as "chainChanged", "accountsChanged", etc. diff --git a/src/connectors/argentMobile/modal/starknet/adapter.ts b/src/connectors/argentMobile/modal/starknet/adapter.ts index eabd13d..d0fb6f1 100644 --- a/src/connectors/argentMobile/modal/starknet/adapter.ts +++ b/src/connectors/argentMobile/modal/starknet/adapter.ts @@ -63,10 +63,10 @@ export class StarknetAdapter private walletRpc: IStarknetRpc - constructor({ client, chainId, rpcUrl }: NamespaceAdapterOptions) { + constructor({ client, chainId, rpcUrl, provider }: NamespaceAdapterOptions) { super() - this.chainId = String(chainId || "SN_GOERLI") + this.chainId = String(chainId ?? constants.NetworkName.SN_MAIN) this.rpc = { chains: chainId ? [this.formatChainId(this.chainId)] : [], methods: this.methods, @@ -84,7 +84,7 @@ export class StarknetAdapter this.remoteSigner = new StarknetRemoteSigner(this.walletRpc) - this.provider = new RpcProvider({ nodeUrl: rpcUrl }) + this.provider = provider ? provider : new RpcProvider({ nodeUrl: rpcUrl }) this.account = new StarknetRemoteAccount( this.provider, "", From 5e1f1429af0e172121fc33bd8e523784ddfa65a4 Mon Sep 17 00:00:00 2001 From: semantic-release-bot Date: Tue, 9 Jan 2024 12:02:57 +0000 Subject: [PATCH 05/17] chore(release): 1.0.30 [skip ci] ## [1.0.30](https://github.com/argentlabs/starknetkit/compare/v1.0.29...v1.0.30) (2024-01-09) ### Bug Fixes * default to mainnet for argent mobile ([37121fb](https://github.com/argentlabs/starknetkit/commit/37121fbaa6b2128e9099e4660103c451edd063b6)) --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 5c8efdb..af733cf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "starknetkit", - "version": "1.0.29", + "version": "1.0.30", "repository": "github:argentlabs/starknetkit", "private": false, "browser": { From c5f81f0bb9a495387696dc7be94c9a579188ac32 Mon Sep 17 00:00:00 2001 From: bluecco Date: Wed, 10 Jan 2024 13:28:47 +0100 Subject: [PATCH 06/17] fix: implement argent mobile connector isPreauthorized method --- src/connectors/argentMobile/index.ts | 3 +++ src/connectors/argentMobile/modal/starknet/adapter.ts | 7 ++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/connectors/argentMobile/index.ts b/src/connectors/argentMobile/index.ts index 65f5d0c..7c0b328 100644 --- a/src/connectors/argentMobile/index.ts +++ b/src/connectors/argentMobile/index.ts @@ -45,6 +45,9 @@ export class ArgentMobileConnector extends Connector { } async ready(): Promise { + // check if session is valid and retrieve the wallet + // if no sessions, it will show the login modal + await this.ensureWallet() if (!this._wallet) { return false } diff --git a/src/connectors/argentMobile/modal/starknet/adapter.ts b/src/connectors/argentMobile/modal/starknet/adapter.ts index d0fb6f1..7fb3be2 100644 --- a/src/connectors/argentMobile/modal/starknet/adapter.ts +++ b/src/connectors/argentMobile/modal/starknet/adapter.ts @@ -127,7 +127,12 @@ export class StarknetAdapter } async isPreauthorized(): Promise { - throw new Error("Not implemented: .isPreauthorized()") + // check if wc session is valid, if so, return true + const session = this.client.session.getAll().find(this.isValidSession) + if (session) { + return true + } + return false } on: ConnectedStarknetWindowObject["on"] = (event, handleEvent) => { From 4cc71576c5d266c40872c1db45eecdde757fd0a4 Mon Sep 17 00:00:00 2001 From: bluecco Date: Mon, 15 Jan 2024 09:41:57 +0100 Subject: [PATCH 07/17] chore: refactor argent mobile isPreauthorized --- src/connectors/argentMobile/modal/starknet/adapter.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/connectors/argentMobile/modal/starknet/adapter.ts b/src/connectors/argentMobile/modal/starknet/adapter.ts index 7fb3be2..e1a784d 100644 --- a/src/connectors/argentMobile/modal/starknet/adapter.ts +++ b/src/connectors/argentMobile/modal/starknet/adapter.ts @@ -128,11 +128,7 @@ export class StarknetAdapter async isPreauthorized(): Promise { // check if wc session is valid, if so, return true - const session = this.client.session.getAll().find(this.isValidSession) - if (session) { - return true - } - return false + return !!this.client.session.getAll().find(this.isValidSession) } on: ConnectedStarknetWindowObject["on"] = (event, handleEvent) => { From 51862784b7b0a8ee6178fe512ab1918681bf6ec9 Mon Sep 17 00:00:00 2001 From: bluecco Date: Mon, 15 Jan 2024 10:00:21 +0100 Subject: [PATCH 08/17] chore: refactor argent mobile isPreauthorized --- src/connectors/argentMobile/modal/starknet/adapter.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/connectors/argentMobile/modal/starknet/adapter.ts b/src/connectors/argentMobile/modal/starknet/adapter.ts index e1a784d..66709de 100644 --- a/src/connectors/argentMobile/modal/starknet/adapter.ts +++ b/src/connectors/argentMobile/modal/starknet/adapter.ts @@ -128,7 +128,7 @@ export class StarknetAdapter async isPreauthorized(): Promise { // check if wc session is valid, if so, return true - return !!this.client.session.getAll().find(this.isValidSession) + return Boolean(this.client.session.getAll().find(this.isValidSession)) } on: ConnectedStarknetWindowObject["on"] = (event, handleEvent) => { From e88e55fca9c26dc8b2ad297ddc8e4d17e04e1593 Mon Sep 17 00:00:00 2001 From: semantic-release-bot Date: Mon, 15 Jan 2024 09:05:44 +0000 Subject: [PATCH 09/17] chore(release): 1.1.0 [skip ci] # [1.1.0](https://github.com/argentlabs/starknetkit/compare/v1.0.30...v1.1.0) (2024-01-15) ### Bug Fixes * implement argent mobile connector isPreauthorized method ([c5f81f0](https://github.com/argentlabs/starknetkit/commit/c5f81f0bb9a495387696dc7be94c9a579188ac32)) ### Features * add starknetkit hook and return connector instead of wallets ([799743b](https://github.com/argentlabs/starknetkit/commit/799743bdd24b5f378fd6dd1c28f4a07f2d07517a)) --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index af733cf..334663d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "starknetkit", - "version": "1.0.30", + "version": "1.1.0", "repository": "github:argentlabs/starknetkit", "private": false, "browser": { From 21de91f0bc9e1fc051a5a43f2fb7bb510b3ecef8 Mon Sep 17 00:00:00 2001 From: bluecco Date: Wed, 17 Jan 2024 16:48:38 +0100 Subject: [PATCH 10/17] fix: throw UserRejectedRequestError for mobile connector when user reject connection --- src/connectors/argentMobile/index.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/connectors/argentMobile/index.ts b/src/connectors/argentMobile/index.ts index 7c0b328..cd28c37 100644 --- a/src/connectors/argentMobile/index.ts +++ b/src/connectors/argentMobile/index.ts @@ -9,6 +9,7 @@ import { ConnectorNotConnectedError, ConnectorNotFoundError, UserNotConnectedError, + UserRejectedRequestError, } from "../../errors" import { resetWalletConnect } from "../../helpers/resetWalletConnect" import { @@ -195,6 +196,11 @@ export class ArgentMobileConnector extends Connector { const _wallet = await getStarknetWindowObject(options) + // getStarknetWindowObject returns null when the user rejects the connection + if (!_wallet) { + throw new UserRejectedRequestError() + } + this._wallet = _wallet // wallet connect rpc enable From 79c7a1fac2943dc8ad5d4ce184ac905aaca356c4 Mon Sep 17 00:00:00 2001 From: semantic-release-bot Date: Wed, 17 Jan 2024 15:58:31 +0000 Subject: [PATCH 11/17] chore(release): 1.1.1 [skip ci] ## [1.1.1](https://github.com/argentlabs/starknetkit/compare/v1.1.0...v1.1.1) (2024-01-17) ### Bug Fixes * throw UserRejectedRequestError for mobile connector when user reject connection ([21de91f](https://github.com/argentlabs/starknetkit/commit/21de91f0bc9e1fc051a5a43f2fb7bb510b3ecef8)) --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 334663d..cdfb913 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "starknetkit", - "version": "1.1.0", + "version": "1.1.1", "repository": "github:argentlabs/starknetkit", "private": false, "browser": { From c3a5169872409bbe815ab93556b868654ac3552f Mon Sep 17 00:00:00 2001 From: bluecco Date: Fri, 19 Jan 2024 12:26:28 +0100 Subject: [PATCH 12/17] fix: use rpc public urls --- src/connectors/argentMobile/constants.ts | 6 ----- src/connectors/injected/index.ts | 2 +- src/connectors/webwallet/constants.ts | 6 ----- .../helpers/mapTargetUrlToNodeUrl.ts | 13 ++++++----- src/helpers/publicRcpNodes.ts | 22 +++++++++++++++++++ 5 files changed, 30 insertions(+), 19 deletions(-) create mode 100644 src/helpers/publicRcpNodes.ts diff --git a/src/connectors/argentMobile/constants.ts b/src/connectors/argentMobile/constants.ts index c5919b6..8d6fb77 100644 --- a/src/connectors/argentMobile/constants.ts +++ b/src/connectors/argentMobile/constants.ts @@ -13,9 +13,3 @@ export const DEFAULT_ARGENT_MOBILE_ICON = ` ` - -export const RPC_NODE_URL_TESTNET = - "https://api.hydrogen.argent47.net/v1/starknet/goerli/rpc/v0.5" - -export const RPC_NODE_URL_MAINNET = - "https://cloud.argent-api.com/v1/starknet/mainnet/rpc/v0.5" diff --git a/src/connectors/injected/index.ts b/src/connectors/injected/index.ts index d72008c..33af12a 100644 --- a/src/connectors/injected/index.ts +++ b/src/connectors/injected/index.ts @@ -6,6 +6,7 @@ import { UserNotConnectedError, UserRejectedRequestError, } from "../../errors" +import { removeStarknetLastConnectedWallet } from "../../helpers/lastConnected" import { Connector, type ConnectorData, @@ -15,7 +16,6 @@ import { WALLET_NOT_FOUND_ICON_DARK, WALLET_NOT_FOUND_ICON_LIGHT, } from "./constants" -import { removeStarknetLastConnectedWallet } from "../../helpers/lastConnected" /** Injected connector options. */ export interface InjectedConnectorOptions { /** The wallet id. */ diff --git a/src/connectors/webwallet/constants.ts b/src/connectors/webwallet/constants.ts index 4642193..d209e53 100644 --- a/src/connectors/webwallet/constants.ts +++ b/src/connectors/webwallet/constants.ts @@ -14,9 +14,3 @@ export const DEFAULT_WEBWALLET_ICON = ` ` - -export const RPC_NODE_URL_TESTNET = - "https://api.hydrogen.argent47.net/v1/starknet/goerli/rpc/v0.5" - -export const RPC_NODE_URL_MAINNET = - "https://cloud.argent-api.com/v1/starknet/mainnet/rpc/v0.5" diff --git a/src/connectors/webwallet/helpers/mapTargetUrlToNodeUrl.ts b/src/connectors/webwallet/helpers/mapTargetUrlToNodeUrl.ts index d3f83de..aff97a9 100644 --- a/src/connectors/webwallet/helpers/mapTargetUrlToNodeUrl.ts +++ b/src/connectors/webwallet/helpers/mapTargetUrlToNodeUrl.ts @@ -1,24 +1,25 @@ -import { RPC_NODE_URL_MAINNET, RPC_NODE_URL_TESTNET } from "../constants" +import { getRandomPublicRPCNode } from "../../../helpers/publicRcpNodes" export function mapTargetUrlToNodeUrl(target: string): string { + const publicRPCNode = getRandomPublicRPCNode() try { const { origin } = new URL(target) if (origin.includes("localhost") || origin.includes("127.0.0.1")) { - return RPC_NODE_URL_TESTNET + return publicRPCNode.testnet } if (origin.includes("hydrogen")) { - return RPC_NODE_URL_TESTNET + return publicRPCNode.testnet } if (origin.includes("staging")) { - return RPC_NODE_URL_MAINNET + return publicRPCNode.mainnet } if (origin.includes("argent.xyz")) { - return RPC_NODE_URL_MAINNET + return publicRPCNode.mainnet } } catch (e) { console.warn( "Could not determine rpc nodeUrl from target URL, defaulting to mainnet", ) } - return RPC_NODE_URL_MAINNET + return publicRPCNode.mainnet } diff --git a/src/helpers/publicRcpNodes.ts b/src/helpers/publicRcpNodes.ts new file mode 100644 index 0000000..72e81df --- /dev/null +++ b/src/helpers/publicRcpNodes.ts @@ -0,0 +1,22 @@ +export type PublicRpcNode = { + mainnet: string + testnet: string +} + +// Public RPC nodes +export const BLAST_RPC_NODE: PublicRpcNode = { + mainnet: "https://starknet-mainnet.public.blastapi.io", + testnet: "https://starknet-testnet.public.blastapi.io", +} as const + +export const LAVA_RPC_NODE: PublicRpcNode = { + mainnet: "https://rpc.starknet.lava.build", + testnet: "https://rpc.starknet-testnet.lava.build", +} as const + +export const PUBLIC_RPC_NODES = [BLAST_RPC_NODE, LAVA_RPC_NODE] as const + +export function getRandomPublicRPCNode() { + const randomIndex = Math.floor(Math.random() * PUBLIC_RPC_NODES.length) + return PUBLIC_RPC_NODES[randomIndex] +} From d0d150f9249e0ccde05153936165a2753221eb02 Mon Sep 17 00:00:00 2001 From: bluecco Date: Fri, 19 Jan 2024 12:28:06 +0100 Subject: [PATCH 13/17] fix: use rpc public urls for mobile connector --- src/connectors/argentMobile/index.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/connectors/argentMobile/index.ts b/src/connectors/argentMobile/index.ts index cd28c37..50fb6f7 100644 --- a/src/connectors/argentMobile/index.ts +++ b/src/connectors/argentMobile/index.ts @@ -19,7 +19,7 @@ import { } from "../connector" import type { StarknetAdapter } from "./modal/starknet/adapter" import { removeStarknetLastConnectedWallet } from "../../helpers/lastConnected" -import { RPC_NODE_URL_MAINNET, RPC_NODE_URL_TESTNET } from "./constants" +import { getRandomPublicRPCNode } from "../../helpers/publicRcpNodes" export interface ArgentMobileConnectorOptions { dappName?: string @@ -162,11 +162,12 @@ export class ArgentMobileConnector extends Connector { rpcUrl, } = this._options + const publicRPCNode = getRandomPublicRPCNode() const providerRpcUrl = rpcUrl ?? (!chainId || chainId === constants.NetworkName.SN_MAIN - ? RPC_NODE_URL_MAINNET - : RPC_NODE_URL_TESTNET) + ? publicRPCNode.mainnet + : publicRPCNode.testnet) const options = { chainId: chainId ?? constants.NetworkName.SN_MAIN, From e44760513680fd57c123712d3979f77d737fa01a Mon Sep 17 00:00:00 2001 From: semantic-release-bot Date: Fri, 19 Jan 2024 11:32:18 +0000 Subject: [PATCH 14/17] chore(release): 1.1.2 [skip ci] ## [1.1.2](https://github.com/argentlabs/starknetkit/compare/v1.1.1...v1.1.2) (2024-01-19) ### Bug Fixes * use rpc public urls ([c3a5169](https://github.com/argentlabs/starknetkit/commit/c3a5169872409bbe815ab93556b868654ac3552f)) * use rpc public urls for mobile connector ([d0d150f](https://github.com/argentlabs/starknetkit/commit/d0d150f9249e0ccde05153936165a2753221eb02)) --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index cdfb913..ea14322 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "starknetkit", - "version": "1.1.1", + "version": "1.1.2", "repository": "github:argentlabs/starknetkit", "private": false, "browser": { From f7a7c6365699ac3b07cefbe6685015dbd28a7222 Mon Sep 17 00:00:00 2001 From: bluecco Date: Sat, 20 Jan 2024 16:00:56 +0100 Subject: [PATCH 15/17] fix: manage promise reject when user reject a connection in connect method --- src/main.ts | 4 +++- src/modal/Modal.svelte | 3 --- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main.ts b/src/main.ts index c9a73e9..c7218ae 100644 --- a/src/main.ts +++ b/src/main.ts @@ -125,7 +125,7 @@ export const connect = async ({ return target } - return new Promise((resolve) => { + return new Promise((resolve, reject) => { const modal = new Modal({ target: getTarget(), props: { @@ -150,6 +150,8 @@ export const connect = async ({ connector, }) } + } catch (error) { + reject(error) } finally { setTimeout(() => modal.$destroy()) } diff --git a/src/modal/Modal.svelte b/src/modal/Modal.svelte index 0b84b0e..a58569a 100644 --- a/src/modal/Modal.svelte +++ b/src/modal/Modal.svelte @@ -1,6 +1,5 @@