11import {
22 Permission ,
3+ type AccountChangeEventHandler ,
34 type RequestFnCall ,
45 type RpcMessage ,
56 type RpcTypeToMessageMap ,
6- type AccountChangeEventHandler ,
77 type StarknetWindowObject ,
88 type TypedData ,
99} from "@starknet-io/types-js"
10+ import type { TRPCClientError } from "@trpc/client"
1011import {
1112 Account ,
1213 type AccountInterface ,
@@ -27,15 +28,16 @@ import {
2728 type ConnectorIcons ,
2829} from "../connector"
2930import { DEFAULT_WEBWALLET_ICON , DEFAULT_WEBWALLET_URL } from "./constants"
31+ import { ConnectAndSignSessionError , WebwalletError } from "./errors"
3032import { openWebwallet } from "./helpers/openWebwallet"
3133import { setPopupOptions } from "./helpers/trpc"
3234import {
3335 type Theme ,
3436 type WebWalletStarknetWindowObject ,
3537} from "./starknetWindowObject/argentStarknetWindowObject"
3638import type { ApprovalRequest } from "./starknetWindowObject/types"
37- import type { TRPCClientError } from "@trpc/client"
38- import { ConnectAndSignSessionError } from "./errors "
39+
40+ const WEBWALLET_LOGOUT_EVENT = "webwallet_logout "
3941
4042let _wallet : StarknetWindowObject | null = null
4143let _address : string | null = null
@@ -209,8 +211,29 @@ export class WebWalletConnector extends Connector {
209211 }
210212 try {
211213 return await this . _wallet . request ( call )
212- } catch ( e ) {
213- console . error ( e )
214+ } catch ( error ) {
215+ if (
216+ error instanceof Error &&
217+ ( error . constructor . name === "TRPCClientError" ||
218+ error . name === "TRPCClientError" )
219+ ) {
220+ const trpcError = error as TRPCClientError < any >
221+
222+ const message =
223+ trpcError . shape . data . webwalletErrorMessage || trpcError . message
224+ const code =
225+ trpcError . shape . data . webwalletErrorCode || trpcError . shape . message
226+
227+ if ( code === "USER_LOGGED_OUT" ) {
228+ _wallet = null
229+ _address = null
230+ this . _wallet = null
231+ document . dispatchEvent ( new Event ( WEBWALLET_LOGOUT_EVENT ) )
232+ }
233+
234+ throw new WebwalletError ( message , code )
235+ }
236+
214237 throw new UserRejectedRequestError ( )
215238 }
216239 }
@@ -290,5 +313,16 @@ export class WebWalletConnector extends Connector {
290313 }
291314}
292315
293- export type { WebWalletStarknetWindowObject , ApprovalRequest }
294- export { ConnectAndSignSessionError }
316+ const handleWebwalletLogoutEvent = ( callback : ( ) => void ) => {
317+ document . addEventListener ( WEBWALLET_LOGOUT_EVENT , ( ) => {
318+ callback ( )
319+ } )
320+ }
321+
322+ export {
323+ ConnectAndSignSessionError ,
324+ handleWebwalletLogoutEvent ,
325+ WEBWALLET_LOGOUT_EVENT ,
326+ WebwalletError ,
327+ }
328+ export type { ApprovalRequest , WebWalletStarknetWindowObject }
0 commit comments