Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: argent mobile connector based on in app browser #119

Merged
merged 2 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/connectors/argentMobile/helpers/inAppBrowser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import type { StarknetWindowObject } from "@starknet-io/types-js"

export const isInArgentMobileAppBrowser = (): boolean => {
if (typeof window === "undefined") {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prevent ssr errors

return false
}

if (!window?.starknet_argentX) {
return false
}
Expand Down
26 changes: 24 additions & 2 deletions src/connectors/argentMobile/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import {
} from "../connector"
import { DEFAULT_ARGENT_MOBILE_ICON, DEFAULT_PROJECT_ID } from "./constants"
import type { StarknetAdapter } from "./modal/starknet/adapter"
import { isInArgentMobileAppBrowser } from "./helpers"
import { InjectedConnector, InjectedConnectorOptions } from "../injected"

export interface ArgentMobileConnectorOptions {
dappName?: string
Expand All @@ -40,7 +42,7 @@ export interface ArgentMobileConnectorOptions {
rpcUrl?: string
}

export class ArgentMobileConnector extends Connector {
export class ArgentMobileBaseConnector extends Connector {
private _wallet: StarknetWindowObject | null = null
private _options: ArgentMobileConnectorOptions

Expand Down Expand Up @@ -242,4 +244,24 @@ export class ArgentMobileConnector extends Connector {
}
}

export { isInArgentMobileAppBrowser } from "./helpers"
export interface ArgentMobileConnectorInitParams {
options: ArgentMobileConnectorOptions
inAppBrowserOptions: Omit<InjectedConnectorOptions, "id">
}

export class ArgentMobileConnector {
static init({
options,
inAppBrowserOptions,
}: ArgentMobileConnectorInitParams): Connector {
if (isInArgentMobileAppBrowser()) {
return new InjectedConnector({
options: { id: "argentX", ...inAppBrowserOptions },
})
} else {
return new ArgentMobileBaseConnector(options)
}
}
}

export { isInArgentMobileAppBrowser }
4 changes: 2 additions & 2 deletions src/helpers/defaultConnectors.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { StarknetkitConnector } from "../connectors"
import {
ArgentMobileConnector,
ArgentMobileBaseConnector,
type ArgentMobileConnectorOptions,
} from "../connectors/argentMobile"
import { InjectedConnector } from "../connectors/injected"
Expand Down Expand Up @@ -29,7 +29,7 @@ export const defaultConnectors = ({
)
}

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

return defaultConnectors
Expand Down