Skip to content

Commit

Permalink
Merge branch 'refs/heads/feat/argent-one-button-connector' into beta
Browse files Browse the repository at this point in the history
  • Loading branch information
Cussone committed Nov 15, 2024
2 parents 1ca776b + e991c89 commit 43fed59
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 55 deletions.
12 changes: 3 additions & 9 deletions src/connectors/argent/argentMobile/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { removeStarknetLastConnectedWallet } from "../../../helpers/lastConnecte
import { getRandomPublicRPCNode } from "../../../helpers/publicRcpNodes"
import { resetWalletConnect } from "../../../helpers/resetWalletConnect"
import {
ConnectArgs,
Connector,
type ConnectorData,
type ConnectorIcons,
Expand Down Expand Up @@ -97,22 +98,15 @@ export class ArgentMobileBaseConnector extends Connector {
return this._wallet
}

async connect(
props:
| {
onlyQRCode?: boolean
}
| undefined,
): Promise<ConnectorData> {
await this.ensureWallet({ onlyQRCode: props?.onlyQRCode })
async connect(_args: ConnectArgs = {}): Promise<ConnectorData> {
await this.ensureWallet({ onlyQRCode: _args?.onlyQRCode })

if (!this._wallet) {
throw new ConnectorNotFoundError()
}

const accounts = await this._wallet.request({
type: "wallet_requestAccounts",
params: { silent_mode: false }, // explicit to show the modal
})

const chainId = await this.chainId()
Expand Down
3 changes: 2 additions & 1 deletion src/connectors/braavosMobile/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from "@starknet-io/types-js"
import { AccountInterface, ProviderInterface, ProviderOptions } from "starknet"
import {
ConnectArgs,
Connector,
type ConnectorData,
type ConnectorIcons,
Expand Down Expand Up @@ -52,7 +53,7 @@ export class BraavosMobileBaseConnector extends Connector {
throw new Error("not implemented")
}

async connect(): Promise<ConnectorData> {
async connect(_args: ConnectArgs = {}): Promise<ConnectorData> {
await this.ensureWallet()

// will return empty data, connect will only open braavos mobile app
Expand Down
6 changes: 3 additions & 3 deletions src/connectors/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ export interface ConnectorEvents {
disconnect(): void
}

export type ConnectOptions = {
silent_mode: boolean
export type ConnectArgs = {
chainIdHint?: bigint
onlyQRCode?: boolean
}

Expand All @@ -46,7 +46,7 @@ export abstract class Connector extends EventEmitter<ConnectorEvents> {
/** Whether connector is already authorized */
abstract ready(): Promise<boolean>
/** Connect wallet. */
abstract connect(params?: ConnectOptions): Promise<ConnectorData>
abstract connect(params?: ConnectArgs): Promise<ConnectorData>
/** Disconnect wallet. */
abstract disconnect(): Promise<void>
/** Get current account silently. Return null if the account is not authorized */
Expand Down
17 changes: 5 additions & 12 deletions src/connectors/injected/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
} from "../../errors"
import { removeStarknetLastConnectedWallet } from "../../helpers/lastConnected"
import {
ConnectOptions,
ConnectArgs,
Connector,
type ConnectorData,
type ConnectorIcons,
Expand Down Expand Up @@ -141,7 +141,7 @@ export class InjectedConnector extends Connector {
return new Account(provider, accounts[0], "")
}

async connect(params: ConnectOptions): Promise<ConnectorData> {
async connect(_args: ConnectArgs = {}): Promise<ConnectorData> {
this.ensureWallet()

if (!this._wallet) {
Expand All @@ -150,16 +150,9 @@ export class InjectedConnector extends Connector {

let accounts: string[]
try {
accounts = await this.request(
params
? {
type: "wallet_requestAccounts",
params,
}
: {
type: "wallet_requestAccounts",
},
)
accounts = await this.request({
type: "wallet_requestAccounts",
})
} catch {
throw new UserRejectedRequestError()
}
Expand Down
3 changes: 2 additions & 1 deletion src/connectors/webwallet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
import { getStarknetChainId } from "../../helpers/getStarknetChainId"
import { removeStarknetLastConnectedWallet } from "../../helpers/lastConnected"
import {
ConnectArgs,
Connector,
type ConnectorData,
type ConnectorIcons,
Expand Down Expand Up @@ -100,7 +101,7 @@ export class WebWalletConnector extends Connector {
return "Powered by Argent"
}

async connect(): Promise<ConnectorData> {
async connect(_args: ConnectArgs = {}): Promise<ConnectorData> {
await this.ensureWallet()

if (!this._wallet) {
Expand Down
27 changes: 3 additions & 24 deletions src/helpers/defaultConnectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,12 @@ import {
StarknetkitCompoundConnector,
StarknetkitConnector,
} from "../connectors"
import {
type ArgentMobileConnectorOptions,
} from "../connectors/argent/argentMobile"
import { type ArgentMobileConnectorOptions } from "../connectors/argent/argentMobile"
import { BraavosMobileBaseConnector } from "../connectors/braavosMobile"
import { WebWalletConnector } from "../connectors/webwallet"
import { Braavos } from "../connectors/injected/braavos"
import { Argent } from "../connectors/argent"

const isMobileDevice = () => {
// Primary method: User Agent + Touch support check
const userAgent = navigator.userAgent.toLowerCase()
const isMobileUA =
/android|webos|iphone|ipad|ipod|blackberry|windows phone/.test(userAgent)
const hasTouchSupport =
"ontouchstart" in window || navigator.maxTouchPoints > 0

// Backup method: Screen size
const isSmallScreen = window.innerWidth <= 768

// Combine checks: Must match user agent AND (touch support OR small screen)
return isMobileUA && (hasTouchSupport || isSmallScreen)
}
import { isMobileDevice, isSafari } from "./navigator"

export const defaultConnectors = ({
argentMobileOptions,
Expand All @@ -32,19 +16,14 @@ export const defaultConnectors = ({
argentMobileOptions: ArgentMobileConnectorOptions
webWalletUrl?: string
}): (StarknetkitConnector | StarknetkitCompoundConnector)[] => {
const isSafari =
typeof window !== "undefined"
? /^((?!chrome|android).)*safari/i.test(navigator.userAgent)
: false

const defaultConnectors: (
| StarknetkitConnector
| StarknetkitCompoundConnector
)[] = []

defaultConnectors.push(new Argent({ mobile: argentMobileOptions }))

if (!isSafari) {
if (!isSafari()) {
defaultConnectors.push(new Braavos())
}

Expand Down
19 changes: 19 additions & 0 deletions src/helpers/navigator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export const isMobileDevice = () => {
// Primary method: User Agent + Touch support check
const userAgent = navigator.userAgent.toLowerCase()
const isMobileUA =
/android|webos|iphone|ipad|ipod|blackberry|windows phone/.test(userAgent)
const hasTouchSupport =
"ontouchstart" in window || navigator.maxTouchPoints > 0

// Backup method: Screen size
const isSmallScreen = window.innerWidth <= 768

// Combine checks: Must match user agent AND (touch support OR small screen)
return isMobileUA && (hasTouchSupport || isSmallScreen)
}

export const isSafari = () =>
typeof window !== "undefined"
? /^((?!chrome|android).)*safari/i.test(navigator.userAgent)
: false
3 changes: 0 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ export const connect = async ({

if (connector && resultType === "wallet") {
connectorData = await connector.connect({
silent_mode: true,
onlyQRCode: true,
})
}
Expand Down Expand Up @@ -129,7 +128,6 @@ export const connect = async ({
connectorData =
(await connector?.connect({
onlyQRCode: true,
silent_mode: false,
})) ?? null
}

Expand Down Expand Up @@ -189,7 +187,6 @@ export const connect = async ({
const connectorData =
(await selectedConnector?.connect({
onlyQRCode: true,
silent_mode: false,
})) ?? null
if (selectedConnector !== null) {
setStarknetLastConnectedWallet(selectedConnector.id)
Expand Down
8 changes: 6 additions & 2 deletions src/modal/Modal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@
if (isInAppBrowser) {
try {
void callback(getModalWallet(new ArgentX()))
setTimeout(() => {
void callback(getModalWallet(new ArgentX()))
})
} catch (e) {
console.error(e)
}
Expand All @@ -72,7 +74,9 @@
if (isBraavosMobileApp) {
try {
void callback(getModalWallet(new Braavos()))
setTimeout(() => {
void callback(getModalWallet(new Braavos()))
})
} catch (e) {
console.error(e)
}
Expand Down

0 comments on commit 43fed59

Please sign in to comment.