diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index e69de29..529109a 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -0,0 +1,71 @@ +# Starknetkit Contribution Guide + +Thank you for investing your time in contributing to Starknetkit! + +We love pull requests and this guide aims to provide an overview of the contribution workflow to help us make the contribution process effective for everyone involved. + +If you want to contribute but don’t know what to do, take a look at issues labelled `good first issue`. + +## Getting started + +You can contribute to this repo in many ways: + +- Solve open issues +- Report bugs or feature requests +- Add new features such as new connectors +- Improve the documentation + +Contributions are made via Issues and Pull Requests (PRs). A few general guidelines for contributions: + +- Search for existing Issues and PRs before creating your own. +- If you're running into an error, please give context. Explain what you're trying to do and how to reproduce the error. +- Please use the same formatting in the code repository. You can configure your IDE to do it by using the prettier / linting config files included in each package. +- If applicable, please edit the README.md file to reflect the changes. + +### Issues + +Issues should be used to report problems, request a new feature, or discuss potential changes before a PR is created. + +#### Solve an issue + +Scan through our existing issues to find one that interests you. + +If a contributor is working on the issue, they will be assigned to the individual. If you find an issue to work on, you are welcome to assign it to yourself and open a PR with a fix for it. + +### Pull Requests + +#### Pull Request Process + +We follow the ["fork-and-pull" Git workflow](https://github.com/susam/gitpr) + +1. Fork the repo +2. Clone the project +3. Create a new branch with a descriptive name +4. Commit your changes to the new branch +5. Push changes to your fork +6. Open a PR in our repository and tag one of the maintainers to review your PR + +Here are some tips for a high-quality pull request: + +- Create a title for the PR that accurately defines the work done. +- Structure the description neatly to make it easy to consume by the readers. For example, you can include bullet points and screenshots instead of having one large paragraph. +- Add the link to the issue if applicable. +- Have a good commit message that summarises the work done. + +Once you submit your PR: + +- We may ask questions, request additional information or ask for changes to be made before a PR can be merged. Please note that these are to make the PR clear for everyone involved and aims to create a frictionless interaction process. +- As you update your PR and apply changes, mark each conversation resolved. +- Once approved, your PR will be merged. + +#### Pull request targets +For the most common pull requests such as bug fixes, feature additions, documentation changes, etc., target the develop branch. + +### Other notes +- If you have commit access to the repository and want to make a big change or are unsure about something, make a new branch and open a pull request. +- We’re using Prettier to format code, so don’t worry much about code formatting. +- Don’t commit generated files, like minified JavaScript. +- Don’t change the version number or changelog. + +### Need help? +If you want to contribute but have any questions, concerns or doubts, feel free to ping maintainers. Ideally create a pull request with WIP (Work in progress) in its title and ask questions in the pull request description. diff --git a/package.json b/package.json index 5c8efdb..1a0eee3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "starknetkit", - "version": "1.0.29", + "version": "1.1.3", "repository": "github:argentlabs/starknetkit", "private": false, "browser": { diff --git a/src/connectors/argentMobile/index.ts b/src/connectors/argentMobile/index.ts index a3e0524..50fb6f7 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 { @@ -18,6 +19,7 @@ import { } from "../connector" import type { StarknetAdapter } from "./modal/starknet/adapter" import { removeStarknetLastConnectedWallet } from "../../helpers/lastConnected" +import { getRandomPublicRPCNode } from "../../helpers/publicRcpNodes" export interface ArgentMobileConnectorOptions { dappName?: string @@ -27,6 +29,7 @@ export interface ArgentMobileConnectorOptions { url?: string icons?: string[] provider?: ProviderInterface + rpcUrl?: string } export class ArgentMobileConnector extends Connector { @@ -43,6 +46,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 } @@ -145,8 +151,24 @@ 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 publicRPCNode = getRandomPublicRPCNode() + const providerRpcUrl = + rpcUrl ?? + (!chainId || chainId === constants.NetworkName.SN_MAIN + ? publicRPCNode.mainnet + : publicRPCNode.testnet) + const options = { chainId: chainId ?? constants.NetworkName.SN_MAIN, name: dappName, @@ -154,10 +176,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,11 +197,9 @@ export class ArgentMobileConnector extends Connector { const _wallet = await getStarknetWindowObject(options) - const { provider } = this._options - if (provider) { - Object.assign(_wallet, { - provider, - }) + // getStarknetWindowObject returns null when the user rejects the connection + if (!_wallet) { + throw new UserRejectedRequestError() } this._wallet = _wallet 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..66709de 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, "", @@ -127,7 +127,8 @@ export class StarknetAdapter } async isPreauthorized(): Promise { - throw new Error("Not implemented: .isPreauthorized()") + // check if wc session is valid, if so, return true + return Boolean(this.client.session.getAll().find(this.isValidSession)) } on: ConnectedStarknetWindowObject["on"] = (event, handleEvent) => { diff --git a/src/connectors/injected/index.ts b/src/connectors/injected/index.ts index e90ee8b..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, @@ -153,7 +154,7 @@ export class InjectedConnector extends Connector { async disconnect(): Promise { this.ensureWallet() - + removeStarknetLastConnectedWallet() if (!this.available()) { throw new ConnectorNotFoundError() } 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/connectors/webwallet/index.ts b/src/connectors/webwallet/index.ts index 7e53c8f..2546bb7 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 @@ -119,6 +120,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 14b6659..db5cc2f 100644 --- a/src/helpers/defaultConnectors.ts +++ b/src/helpers/defaultConnectors.ts @@ -16,7 +16,10 @@ export const defaultConnectors = ({ webWalletUrl?: string provider?: ProviderInterface }): 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/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] +} 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/main.ts b/src/main.ts index d7a752b..c7218ae 100644 --- a/src/main.ts +++ b/src/main.ts @@ -16,9 +16,9 @@ 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 { Connector } from "./connectors" +import { type Connector } from "./connectors" import css from "./theme.css?inline" let selectedConnector: Connector | null = null @@ -31,9 +31,10 @@ export const connect = async ({ webWalletUrl = DEFAULT_WEBWALLET_URL, argentMobileOptions, connectors = [], + resultType = "wallet", provider, ...restOptions -}: ConnectOptions = {}): Promise => { +}: ConnectOptions = {}): Promise => { // force null in case it was disconnected from mobile app selectedConnector = null const availableConnectors = @@ -48,9 +49,17 @@ export const connect = async ({ const lastWalletId = localStorage.getItem("starknetLastConnectedWallet") if (modalMode === "neverAsk") { const connector = availableConnectors.find((c) => c.id === lastWalletId) - await connector?.connect() + + if (resultType === "wallet") { + await connector?.connect() + } + selectedConnector = connector ?? null - return connector?.wallet ?? null + + return { + connector, + wallet: connector?.wallet ?? null, + } } const installedWallets = await sn.getAvailableWallets(restOptions) @@ -69,11 +78,19 @@ export const connect = async ({ if (wallet) { const connector = availableConnectors.find((c) => c.id === lastWalletId) - await connector?.connect() + + if (resultType === "wallet") { + await connector?.connect() + } + if (connector) { selectedConnector = connector } - return wallet + + return { + connector, + wallet: connector?.wallet ?? null, + } } // otherwise fallback to modal } @@ -108,21 +125,33 @@ export const connect = async ({ return target } - return new Promise((resolve) => { + return new Promise((resolve, reject) => { const modal = new Modal({ target: getTarget(), 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 (resultType === "wallet") { + await connector?.connect() + + if (connector !== null && connector.id !== "argentWebWallet") { + setStarknetLastConnectedWallet(connector.id) + } + + resolve({ + connector, + wallet: connector?.wallet ?? null, + }) + } else { + resolve({ + connector, + }) } - selectedConnector = - availableConnectors.find( - (c) => value !== null && c.id === value.id, - ) ?? null - resolve(value) + } catch (error) { + reject(error) } finally { setTimeout(() => modal.$destroy()) } @@ -150,7 +179,11 @@ export const disconnect = async (options: DisconnectOptions = {}) => { export type { ConnectedStarknetWindowObject, + Connector, DisconnectOptions, DisconnectedStarknetWindowObject, StarknetWindowObject, + defaultConnectors as starknetkitDefaultConnectors, } + +export { useStarknetkitConnectModal } from "./hooks/useStarknetkitConnectModal" diff --git a/src/modal/Modal.svelte b/src/modal/Modal.svelte index e8f2343..a58569a 100644 --- a/src/modal/Modal.svelte +++ b/src/modal/Modal.svelte @@ -1,15 +1,15 @@