Skip to content

Commit f711ed2

Browse files
authored
Merge pull request #198 from argentlabs/feat/webwallet-logout
feat: handle webwallet logout
2 parents d6ce8ef + 4e6692a commit f711ed2

File tree

2 files changed

+51
-7
lines changed

2 files changed

+51
-7
lines changed

src/connectors/webwallet/errors/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,13 @@ export class ConnectAndSignSessionError extends Error {
77
this.code = code
88
}
99
}
10+
11+
export class WebwalletError extends Error {
12+
code: string
13+
14+
constructor(message: string, code: string) {
15+
super(message)
16+
this.name = "WebwalletError"
17+
this.code = code
18+
}
19+
}

src/connectors/webwallet/index.ts

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import {
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"
1011
import {
1112
Account,
1213
type AccountInterface,
@@ -27,15 +28,16 @@ import {
2728
type ConnectorIcons,
2829
} from "../connector"
2930
import { DEFAULT_WEBWALLET_ICON, DEFAULT_WEBWALLET_URL } from "./constants"
31+
import { ConnectAndSignSessionError, WebwalletError } from "./errors"
3032
import { openWebwallet } from "./helpers/openWebwallet"
3133
import { setPopupOptions } from "./helpers/trpc"
3234
import {
3335
type Theme,
3436
type WebWalletStarknetWindowObject,
3537
} from "./starknetWindowObject/argentStarknetWindowObject"
3638
import 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

4042
let _wallet: StarknetWindowObject | null = null
4143
let _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

Comments
 (0)