Skip to content

Commit

Permalink
fix: allow custom provider for injected connectors
Browse files Browse the repository at this point in the history
  • Loading branch information
bluecco committed Dec 13, 2023
1 parent 4c997a6 commit acdbf1c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
11 changes: 10 additions & 1 deletion src/connectors/injected/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { StarknetWindowObject } from "get-starknet-core"
import { AccountInterface, constants } from "starknet"
import { AccountInterface, ProviderInterface, constants } from "starknet"
import {
ConnectorNotConnectedError,
ConnectorNotFoundError,
Expand All @@ -23,6 +23,8 @@ export interface InjectedConnectorOptions {
name?: string
/** Wallet icons. */
icon?: ConnectorIcons
/** Provider */
provider?: ProviderInterface
}

export class InjectedConnector extends Connector {
Expand Down Expand Up @@ -211,6 +213,13 @@ export class InjectedConnector extends Connector {
const installed = getAvailableWallets(globalThis)
const wallet = installed.filter((w) => w.id === this._options.id)[0]
if (wallet) {
const { provider } = this._options
if (provider) {
Object.assign(wallet, {
provider,
})
}

this._wallet = wallet
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/helpers/defaultConnectors.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ProviderInterface } from "starknet"
import { type Connector } from "../connectors"
import {
ArgentMobileConnector,
Expand All @@ -9,20 +10,22 @@ import { WebWalletConnector } from "../connectors/webwallet"
export const defaultConnectors = ({
argentMobileOptions,
webWalletUrl,
provider,
}: {
argentMobileOptions?: ArgentMobileConnectorOptions
webWalletUrl?: string
provider?: ProviderInterface
}): Connector[] => {
const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent)

const defaultConnectors: Connector[] = []

if (!isSafari) {
defaultConnectors.push(
new InjectedConnector({ options: { id: "argentX" } }),
new InjectedConnector({ options: { id: "argentX", provider } }),
)
defaultConnectors.push(
new InjectedConnector({ options: { id: "braavos" } }),
new InjectedConnector({ options: { id: "braavos", provider } }),
)
}

Expand Down
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const connect = async ({
webWalletUrl = DEFAULT_WEBWALLET_URL,
argentMobileOptions,
connectors = [],
provider,
...restOptions
}: ConnectOptions = {}): Promise<StarknetWindowObject | null> => {
// force null in case it was disconnected from mobile app
Expand All @@ -40,6 +41,7 @@ export const connect = async ({
? defaultConnectors({
argentMobileOptions,
webWalletUrl,
provider,
})
: connectors

Expand Down
2 changes: 2 additions & 0 deletions src/types/modal.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { GetWalletOptions } from "get-starknet-core"
import type { Connector, ConnectorIcons } from "../connectors/connector"
import type { ArgentMobileConnectorOptions } from "../connectors/argentMobile"
import { ProviderInterface } from "starknet"

export type StoreVersion = "chrome" | "firefox" | "edge"

Expand All @@ -12,6 +13,7 @@ export interface ConnectOptions extends GetWalletOptions {
modalTheme?: "light" | "dark" | "system"
storeVersion?: StoreVersion | null
webWalletUrl?: string
provider?: ProviderInterface
}

export type ModalWallet = {
Expand Down

0 comments on commit acdbf1c

Please sign in to comment.