Skip to content

Commit

Permalink
Merge pull request #47 from argentlabs/fix/add-custom-provider
Browse files Browse the repository at this point in the history
fix: allow custom provider for injected connectors
  • Loading branch information
bluecco authored Dec 13, 2023
2 parents 4c997a6 + e951459 commit 34755db
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 18 deletions.
15 changes: 14 additions & 1 deletion src/connectors/argentMobile/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type {
AccountChangeEventHandler,
StarknetWindowObject,
} from "get-starknet-core"
import type { AccountInterface } from "starknet"
import type { AccountInterface, ProviderInterface } from "starknet"
import { constants } from "starknet"
import { DEFAULT_ARGENT_MOBILE_ICON, DEFAULT_PROJECT_ID } from "./constants"
import {
Expand All @@ -26,6 +26,7 @@ export interface ArgentMobileConnectorOptions {
description?: string
url?: string
icons?: string[]
provider?: ProviderInterface
}

export class ArgentMobileConnector extends Connector {
Expand Down Expand Up @@ -153,6 +154,10 @@ 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",
}

if (projectId === DEFAULT_PROJECT_ID) {
Expand All @@ -171,6 +176,14 @@ 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
Expand Down
9 changes: 2 additions & 7 deletions src/connectors/argentMobile/modal/starknet/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type {
ProviderInterface,
SignerInterface,
} from "starknet"
import { Provider, constants } from "starknet"
import { RpcProvider, constants } from "starknet"

import type { NamespaceAdapterOptions } from "../adapter"
import { NamespaceAdapter } from "../adapter"
Expand Down Expand Up @@ -84,12 +84,7 @@ export class StarknetAdapter

this.remoteSigner = new StarknetRemoteSigner(this.walletRpc)

if (rpcUrl) {
this.provider = new Provider({ rpc: { nodeUrl: rpcUrl } })
} else {
const network = this.getNetworkName(this.chainId)
this.provider = new Provider({ sequencer: { network } })
}
this.provider = new RpcProvider({ nodeUrl: rpcUrl })
this.account = new StarknetRemoteAccount(
this.provider,
"",
Expand Down
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
2 changes: 1 addition & 1 deletion src/connectors/webwallet/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ 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/goerli/rpc/v0.5"
"https://cloud.argent-api.com/v1/starknet/mainnet/rpc/v0.5"
10 changes: 8 additions & 2 deletions src/connectors/webwallet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type {
AccountChangeEventHandler,
StarknetWindowObject,
} from "get-starknet-core"
import type { AccountInterface } from "starknet"
import type { AccountInterface, ProviderInterface } from "starknet"
import {
Connector,
type ConnectorData,
Expand All @@ -23,6 +23,7 @@ let _wallet: StarknetWindowObject | null = null

interface WebWalletConnectorOptions {
url?: string
provider?: ProviderInterface
}

export class WebWalletConnector extends Connector {
Expand Down Expand Up @@ -163,11 +164,16 @@ export class WebWalletConnector extends Connector {

private async ensureWallet(): Promise<void> {
const origin = this._options.url || DEFAULT_WEBWALLET_URL
const provider = this._options.provider
setPopupOptions({
origin,
location: "/interstitialLogin",
})
const wallet = await getWebWalletStarknetObject(origin, trpcProxyClient({}))
const wallet = await getWebWalletStarknetObject(
origin,
trpcProxyClient({}),
provider,
)

_wallet = wallet ?? null
this._wallet = _wallet
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { CreateTRPCProxyClient } from "@trpc/client"
import { RpcProvider } from "starknet"
import { ProviderInterface, RpcProvider } from "starknet"

import { mapTargetUrlToNodeUrl } from "../helpers/mapTargetUrlToNodeUrl"
import type { AppRouter } from "../helpers/trpc"
Expand All @@ -9,14 +9,15 @@ import { getArgentStarknetWindowObject } from "./argentStarknetWindowObject"
export const getWebWalletStarknetObject = async (
target: string,
proxyLink: CreateTRPCProxyClient<AppRouter>,
provider?: ProviderInterface,
): Promise<WebWalletStarknetWindowObject> => {
const globalWindow = typeof window !== "undefined" ? window : undefined
if (!globalWindow) {
throw new Error("window is not defined")
}

const nodeUrl = mapTargetUrlToNodeUrl(target)
const defaultProvider = new RpcProvider({ nodeUrl })
const defaultProvider = provider ?? new RpcProvider({ nodeUrl })
const starknetWindowObject = getArgentStarknetWindowObject(
{
host: globalWindow.location.origin,
Expand Down
15 changes: 11 additions & 4 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,25 +10,31 @@ 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 } }),
)
}

defaultConnectors.push(new ArgentMobileConnector(argentMobileOptions))
defaultConnectors.push(new WebWalletConnector({ url: webWalletUrl }))
defaultConnectors.push(
new ArgentMobileConnector({ ...argentMobileOptions, provider }),
)
defaultConnectors.push(
new WebWalletConnector({ url: webWalletUrl, provider }),
)

return defaultConnectors
}
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 34755db

Please sign in to comment.