Skip to content

Commit

Permalink
Merge pull request #136 from argentlabs/develop
Browse files Browse the repository at this point in the history
Release v2.3.2
  • Loading branch information
bluecco authored Oct 2, 2024
2 parents d4ba2cf + 9dfd304 commit 957350c
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 31 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "starknetkit",
"version": "2.3.0",
"version": "2.3.2",
"repository": "github:argentlabs/starknetkit",
"private": false,
"browser": {
Expand Down
24 changes: 14 additions & 10 deletions src/connectors/argentMobile/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { type AccountChangeEventHandler } from "@starknet-io/get-starknet-core"
import {
AccountInterface,
ProviderInterface,
ProviderOptions,
WalletAccount,
constants,
} from "starknet"
import {
Permission,
RequestFnCall,
RpcMessage,
RpcTypeToMessageMap,
type StarknetWindowObject,
} from "@starknet-io/types-js"
import {
Account,
AccountInterface,
ProviderInterface,
ProviderOptions,
constants,
} from "starknet"
import {
ConnectorNotConnectedError,
ConnectorNotFoundError,
Expand All @@ -27,10 +27,10 @@ import {
type ConnectorData,
type ConnectorIcons,
} from "../connector"
import { InjectedConnector, InjectedConnectorOptions } from "../injected"
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"
import type { StarknetAdapter } from "./modal/starknet/adapter"

export interface ArgentMobileConnectorOptions {
dappName: string
Expand Down Expand Up @@ -131,8 +131,12 @@ export class ArgentMobileBaseConnector extends Connector {
if (!this._wallet) {
throw new ConnectorNotConnectedError()
}
const accounts = await this._wallet.request({
type: "wallet_requestAccounts",
params: { silent_mode: true },
})

return new WalletAccount(provider, this._wallet)
return new Account(provider, accounts[0], "")
}

async chainId(): Promise<bigint> {
Expand Down
6 changes: 5 additions & 1 deletion src/connectors/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export interface ConnectorEvents {
disconnect(): void
}

export type ConnectOptions = {
silent_mode: boolean
}

export abstract class Connector extends EventEmitter<ConnectorEvents> {
/** Unique connector id. */
abstract get id(): string
Expand All @@ -41,7 +45,7 @@ export abstract class Connector extends EventEmitter<ConnectorEvents> {
/** Whether connector is already authorized */
abstract ready(): Promise<boolean>
/** Connect wallet. */
abstract connect(): Promise<ConnectorData>
abstract connect(params?: ConnectOptions): Promise<ConnectorData>
/** Disconnect wallet. */
abstract disconnect(): Promise<void>
/** Get current account silently. Return null if the account is not authorized */
Expand Down
35 changes: 24 additions & 11 deletions src/connectors/injected/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import {
AccountInterface,
ProviderInterface,
ProviderOptions,
WalletAccount,
} from "starknet"
import {
Permission,
RequestFnCall,
RpcMessage,
RpcTypeToMessageMap,
type StarknetWindowObject,
} from "@starknet-io/types-js"
import {
Account,
AccountInterface,
ProviderInterface,
ProviderOptions,
} from "starknet"
import {
ConnectorNotConnectedError,
ConnectorNotFoundError,
UserRejectedRequestError,
} from "../../errors"
import { removeStarknetLastConnectedWallet } from "../../helpers/lastConnected"
import {
ConnectOptions,
Connector,
type ConnectorData,
type ConnectorIcons,
Expand Down Expand Up @@ -130,10 +131,15 @@ export class InjectedConnector extends Connector {
throw new ConnectorNotConnectedError()
}

return new WalletAccount(provider, this._wallet)
const accounts = await this.request({
type: "wallet_requestAccounts",
params: { silent_mode: true },
})

return new Account(provider, accounts[0], "")
}

async connect(): Promise<ConnectorData> {
async connect(params: ConnectOptions): Promise<ConnectorData> {
this.ensureWallet()

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

let accounts: string[]
try {
accounts = await this.request({
type: "wallet_requestAccounts",
})
accounts = await this.request(
params
? {
type: "wallet_requestAccounts",
params,
}
: {
type: "wallet_requestAccounts",
},
)
} catch {
throw new UserRejectedRequestError()
}
Expand Down
14 changes: 12 additions & 2 deletions src/connectors/webwallet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import {
type StarknetWindowObject,
} from "@starknet-io/types-js"
import {
Account,
AccountInterface,
ProviderInterface,
ProviderOptions,
WalletAccount,
} from "starknet"
import {
ConnectorNotConnectedError,
Expand All @@ -30,6 +30,7 @@ import { setPopupOptions } from "./helpers/trpc"
import type { WebWalletStarknetWindowObject } from "./starknetWindowObject/argentStarknetWindowObject"

let _wallet: StarknetWindowObject | null = null
let _address: string | null = null

interface WebWalletConnectorOptions {
url?: string
Expand All @@ -51,6 +52,7 @@ export class WebWalletConnector extends Connector {
async ready(): Promise<boolean> {
if (!_wallet) {
this._wallet = null
_address = null
return false
}

Expand Down Expand Up @@ -116,6 +118,8 @@ export class WebWalletConnector extends Connector {

const hexChainId = getStarknetChainId(chainId)

_address = account[0]

return {
account: account[0],
chainId: BigInt(hexChainId),
Expand Down Expand Up @@ -145,6 +149,7 @@ export class WebWalletConnector extends Connector {
}

_wallet = null
_address = null
this._wallet = _wallet
removeStarknetLastConnectedWallet()
}
Expand All @@ -158,7 +163,11 @@ export class WebWalletConnector extends Connector {
throw new ConnectorNotConnectedError()
}

return new WalletAccount(provider, this._wallet)
if (!_address) {
throw new ConnectorNotConnectedError()
}

return new Account(provider, _address, "")
}

async chainId(): Promise<bigint> {
Expand Down Expand Up @@ -192,6 +201,7 @@ export class WebWalletConnector extends Connector {
this._wallet.off("accountsChanged", accountChangeCb)

_wallet = null
_address = null
this._wallet = null
}

Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useStarknetkitConnectModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ type UseStarknetkitConnectors = {
}

const useStarknetkitConnectModal = (
options: ConnectOptionsWithConnectors,
options?: ConnectOptionsWithConnectors,
): UseStarknetkitConnectors => {
const starknetkitConnectModal = async (): Promise<ModalResult> => {
return await connect({
...options,
resultType: options.resultType ?? "connector",
resultType: options?.resultType ?? "connector",
})
}

Expand Down
9 changes: 5 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ export const connect = async ({
let connectorData: ConnectorData | null = null

if (connector && resultType === "wallet") {
connectorData = await connector.connect()
connectorData = await connector.connect({
silent_mode: true,
})
}

return {
Expand Down Expand Up @@ -139,9 +141,8 @@ export const connect = async ({
callback: async (connector: StarknetkitConnector | null) => {
try {
selectedConnector = connector
const connectorData = (await connector?.connect()) ?? null

if (resultType === "wallet") {
const connectorData = (await connector?.connect()) ?? null
if (connector !== null) {
setStarknetLastConnectedWallet(connector.id)
}
Expand All @@ -155,7 +156,7 @@ export const connect = async ({
resolve({
connector,
wallet: null,
connectorData,
connectorData: null,
})
}
} catch (error) {
Expand Down

0 comments on commit 957350c

Please sign in to comment.