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/connectors account #132

Merged
merged 3 commits into from
Sep 30, 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
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
19 changes: 12 additions & 7 deletions src/connectors/injected/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
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,
Expand Down Expand Up @@ -130,7 +130,12 @@ 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> {
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
5 changes: 2 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,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 +154,7 @@ export const connect = async ({
resolve({
connector,
wallet: null,
connectorData,
connectorData: null,
})
}
} catch (error) {
Expand Down