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

feat: upgrade argent webwallet connect with one call #127

Merged
merged 1 commit into from
Sep 11, 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
2 changes: 1 addition & 1 deletion src/connectors/webwallet/helpers/openWebwallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const openWebwallet = async (
const iframeTrpcProxyClient = trpcProxyClient({
iframe: iframe.contentWindow ?? undefined,
})
await iframeTrpcProxyClient.authorize.mutate()

const starknetWindowObject = await getWebWalletStarknetObject(
origin,
iframeTrpcProxyClient,
Expand Down
10 changes: 9 additions & 1 deletion src/connectors/webwallet/helpers/trpc.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Permission } from "@starknet-io/types-js"
import type { CreateTRPCProxyClient } from "@trpc/client"
import { createTRPCProxyClient, loggerLink, splitLink } from "@trpc/client"
import { initTRPC } from "@trpc/server"
Expand All @@ -10,7 +11,6 @@ import {
deployAccountContractSchema,
} from "../../../types/window"
import { DEFAULT_WEBWALLET_URL } from "../constants"
import { Permission } from "@starknet-io/types-js"

const t = initTRPC.create({
isServer: false,
Expand Down Expand Up @@ -60,6 +60,14 @@ const appRouter = t.router({
return true
}),
connect: t.procedure.mutation(async () => ""),
connectWebwallet: t.procedure
.output(
z.object({
account: z.string().array().optional(),
chainId: z.string().optional(),
}),
)
.mutation(async () => ({})),
enable: t.procedure.output(z.string()).mutation(async () => ""),
execute: t.procedure
.input(StarknetMethodArgumentsSchemas.execute)
Expand Down
46 changes: 23 additions & 23 deletions src/connectors/webwallet/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
import {
AccountInterface,
ProviderInterface,
ProviderOptions,
WalletAccount,
} from "starknet"
import {
Permission,
RequestFnCall,
Expand All @@ -12,6 +6,12 @@ import {
type AccountChangeEventHandler,
type StarknetWindowObject,
} from "@starknet-io/types-js"
import {
AccountInterface,
ProviderInterface,
ProviderOptions,
WalletAccount,
} from "starknet"
import {
ConnectorNotConnectedError,
ConnectorNotFoundError,
Expand All @@ -27,6 +27,7 @@ import {
import { DEFAULT_WEBWALLET_ICON, DEFAULT_WEBWALLET_URL } from "./constants"
import { openWebwallet } from "./helpers/openWebwallet"
import { setPopupOptions } from "./helpers/trpc"
import type { WebWalletStarknetWindowObject } from "./starknetWindowObject/argentStarknetWindowObject"

let _wallet: StarknetWindowObject | null = null

Expand Down Expand Up @@ -104,26 +105,23 @@ export class WebWalletConnector extends Connector {
throw new ConnectorNotFoundError()
}

let accounts: string[]

try {
accounts = await this._wallet.request({
type: "wallet_requestAccounts",
params: { silent_mode: false }, // explicit to show the modal
})
} catch {
throw new UserRejectedRequestError()
}
const { account, chainId } = await (
this._wallet as WebWalletStarknetWindowObject
).connectWebwallet()

// Prevent trpc from throwing an error (closed prematurely)
// this happens when 2 requests to webwallet are made in a row (trpc-browser is closing the first popup and requesting a new one right after)
// won't be needed with chrome iframes will be enabled again (but still needed for other browsers)
await new Promise((r) => setTimeout(r, 200))
const chainId = await this.chainId()
if (!account || !chainId) {
return {}
}

return {
account: accounts[0],
chainId,
const hexChainId = getStarknetChainId(chainId)

return {
account: account[0],
chainId: BigInt(hexChainId),
}
} catch {
throw new UserRejectedRequestError()
}
}

Expand Down Expand Up @@ -209,3 +207,5 @@ export class WebWalletConnector extends Connector {
this._wallet = _wallet
}
}

export type { WebWalletStarknetWindowObject }
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { CreateTRPCProxyClient } from "@trpc/client"
import type { constants } from "starknet"
import type {
AccountChangeEventHandler,
NetworkChangeEventHandler,
RpcTypeToMessageMap,
StarknetWindowObject,
WalletEvents,
} from "@starknet-io/types-js"
import type { CreateTRPCProxyClient } from "@trpc/client"
import type { constants } from "starknet"
import {
EXECUTE_POPUP_HEIGHT,
EXECUTE_POPUP_WIDTH,
Expand Down Expand Up @@ -35,7 +35,12 @@ export type LoginStatus = {

export type WebWalletStarknetWindowObject = StarknetWindowObject & {
getLoginStatus(): Promise<LoginStatus>
connectWebwallet(): Promise<{
account?: string[]
chainId?: string
}>
}

export const getArgentStarknetWindowObject = (
options: GetArgentStarknetWindowObject,
proxyLink: CreateTRPCProxyClient<AppRouter>,
Expand All @@ -45,6 +50,9 @@ export const getArgentStarknetWindowObject = (
getLoginStatus: () => {
return proxyLink.getLoginStatus.mutate()
},
connectWebwallet: () => {
return proxyLink.connectWebwallet.mutate()
},
async request(call) {
switch (call.type) {
case "wallet_requestAccounts": {
Expand Down