From f0bdd11833b711268e3fcf5a5ee13f5513dbad43 Mon Sep 17 00:00:00 2001 From: jhw Date: Fri, 29 Mar 2024 18:34:19 +0800 Subject: [PATCH] fix: sessionKey parsing fail --- src/waas/hooks/useSessionPermission.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/waas/hooks/useSessionPermission.ts b/src/waas/hooks/useSessionPermission.ts index 21b28ab..bdadbde 100644 --- a/src/waas/hooks/useSessionPermission.ts +++ b/src/waas/hooks/useSessionPermission.ts @@ -19,6 +19,10 @@ import { useValidator } from ".."; import { getEntryPoint } from "../utils/entryPoint"; import { getPermissionId } from "../utils/mock/getPermissionId"; +type SessionKeys = { + [permissionId: string]: `0x${string}`; +}; + export type SessionPermissionKey = [ key: string, params: { @@ -72,9 +76,13 @@ export function setSessionKey( permissionId: `0x${string}`, sessionKey: `0x${string}` ) { - const sessionKeys = JSON.parse( - localStorage.getItem(`kernel_session_key`) || "{}" - ); + let sessionKeys: SessionKeys = {}; + try { + sessionKeys = JSON.parse( + localStorage.getItem(`kernel_session_key`) || "{}" + ); + } catch (err) {} + sessionKeys[permissionId] = sessionKey; localStorage.setItem(`kernel_session_key`, JSON.stringify(sessionKeys)); }