@@ -5,11 +5,12 @@ import {
55 type RpcTypeToMessageMap ,
66 type AccountChangeEventHandler ,
77 type StarknetWindowObject ,
8+ type TypedData ,
89} from "@starknet-io/types-js"
910import {
1011 Account ,
11- AccountInterface ,
12- ProviderInterface ,
12+ type AccountInterface ,
13+ type ProviderInterface ,
1314 type ProviderOptions ,
1415} from "starknet"
1516import {
@@ -28,10 +29,13 @@ import {
2829import { DEFAULT_WEBWALLET_ICON , DEFAULT_WEBWALLET_URL } from "./constants"
2930import { openWebwallet } from "./helpers/openWebwallet"
3031import { setPopupOptions } from "./helpers/trpc"
31- import type {
32- Theme ,
33- WebWalletStarknetWindowObject ,
32+ import {
33+ type Theme ,
34+ type WebWalletStarknetWindowObject ,
3435} from "./starknetWindowObject/argentStarknetWindowObject"
36+ import type { ApprovalRequest } from "./starknetWindowObject/types"
37+ import type { TRPCClientError } from "@trpc/client"
38+ import { ConnectAndSignSessionError } from "./errors"
3539
3640let _wallet : StarknetWindowObject | null = null
3741let _address : string | null = null
@@ -57,21 +61,25 @@ export class WebWalletConnector extends Connector {
5761 }
5862
5963 async ready ( ) : Promise < boolean > {
60- if ( ! _wallet ) {
61- this . _wallet = null
62- _address = null
63- return false
64+ if ( ! this . _wallet ) {
65+ await this . ensureWallet ( )
6466 }
6567
66- this . _wallet = _wallet
67- try {
68- const permissions = await this . _wallet . request ( {
69- type : "wallet_getPermissions" ,
70- } )
68+ if ( ! this . _wallet ) {
69+ this . _wallet = null
70+ _address = null
7171
72- return ( permissions as Permission [ ] ) . includes ( Permission . ACCOUNTS )
73- } catch {
7472 return false
73+ } else {
74+ try {
75+ const permissions = await this . _wallet . request ( {
76+ type : "wallet_getPermissions" ,
77+ } )
78+
79+ return ( permissions as Permission [ ] ) . includes ( Permission . ACCOUNTS )
80+ } catch {
81+ return false
82+ }
7583 }
7684 }
7785
@@ -107,6 +115,48 @@ export class WebWalletConnector extends Connector {
107115 return "Powered by Argent"
108116 }
109117
118+ async connectAndSignSession ( {
119+ callbackData,
120+ approvalRequests,
121+ sessionTypedData,
122+ } : {
123+ callbackData ?: string
124+ approvalRequests : ApprovalRequest [ ]
125+ sessionTypedData : TypedData
126+ } ) {
127+ await this . ensureWallet ( )
128+
129+ if ( ! this . _wallet ) {
130+ throw new ConnectorNotFoundError ( )
131+ }
132+
133+ try {
134+ return await (
135+ this . _wallet as WebWalletStarknetWindowObject
136+ ) . connectAndSignSession ( {
137+ callbackData,
138+ approvalRequests,
139+ sessionTypedData,
140+ } )
141+ } catch ( error ) {
142+ if (
143+ error instanceof Error &&
144+ ( error . constructor . name === "TRPCClientError" ||
145+ error . name === "TRPCClientError" )
146+ ) {
147+ const trpcError = error as TRPCClientError < any >
148+
149+ const message =
150+ trpcError . shape . data . webwalletErrorMessage || trpcError . message
151+ const code =
152+ trpcError . shape . data . webwalletErrorCode || trpcError . shape . message
153+
154+ throw new ConnectAndSignSessionError ( message , code )
155+ }
156+ throw new Error ( error instanceof Error ? error . message : String ( error ) )
157+ }
158+ }
159+
110160 async connect ( _args : ConnectArgs = { } ) : Promise < ConnectorData > {
111161 await this . ensureWallet ( )
112162
@@ -240,4 +290,5 @@ export class WebWalletConnector extends Connector {
240290 }
241291}
242292
243- export type { WebWalletStarknetWindowObject }
293+ export type { WebWalletStarknetWindowObject , ApprovalRequest }
294+ export { ConnectAndSignSessionError }
0 commit comments