From 603f720049ca07407ba63261ff4992481a57a165 Mon Sep 17 00:00:00 2001 From: Tarrence van As Date: Fri, 10 Jan 2025 08:59:34 -0500 Subject: [PATCH] Check session on execute fallback (#1268) --- packages/keychain/src/components/home.tsx | 52 ++++++++++++--- .../transaction/ConfirmTransaction.tsx | 66 +------------------ packages/keychain/src/hooks/connection.ts | 2 - .../keychain/src/utils/connection/index.ts | 4 +- .../keychain/src/utils/connection/probe.ts | 2 +- packages/keychain/src/utils/cookie.ts | 11 ---- 6 files changed, 49 insertions(+), 88 deletions(-) delete mode 100644 packages/keychain/src/utils/cookie.ts diff --git a/packages/keychain/src/components/home.tsx b/packages/keychain/src/components/home.tsx index 68cfeb197..37739239e 100644 --- a/packages/keychain/src/components/home.tsx +++ b/packages/keychain/src/components/home.tsx @@ -3,7 +3,7 @@ import { useEffect, useState } from "react"; import { usePostHog } from "posthog-js/react"; import { ResponseCodes } from "@cartridge/controller"; import { useConnection } from "@/hooks/connection"; -import { DeployCtx, SignMessageCtx } from "@/utils/connection"; +import { DeployCtx, ExecuteCtx, SignMessageCtx } from "@/utils/connection"; import { ConfirmTransaction } from "./transaction/ConfirmTransaction"; import { CreateController, CreateSession, Logout, Upgrade } from "./connect"; import { LoginMode } from "./connect/types"; @@ -13,9 +13,11 @@ import { PurchaseCredits } from "./funding/PurchaseCredits"; import { Settings } from "./settings"; import { SignMessage } from "./SignMessage"; import { PageLoading } from "./Loading"; +import { execute } from "@/utils/connection/execute"; export function Home() { - const { context, controller, error, policies, upgrade } = useConnection(); + const { context, setContext, controller, error, policies, upgrade } = + useConnection(); const [hasSessionForPolicies, setHasSessionForPolicies] = useState< boolean | undefined >(undefined); @@ -60,8 +62,9 @@ export function Home() { return ; } - if (!upgrade.isSynced) { - return <>; + if (!upgrade.isSynced || hasSessionForPolicies === undefined) { + // This is likely never observable in a real application but just in case. + return ; } if (upgrade.available) { @@ -87,10 +90,7 @@ export function Home() { return <>; } - if (hasSessionForPolicies === undefined) { - // This is likely never observable in a real application but just in case. - return ; - } else if (hasSessionForPolicies) { + if (hasSessionForPolicies) { context.resolve({ code: ResponseCodes.SUCCESS, address: controller.address, @@ -113,6 +113,7 @@ export function Home() { /> ); } + case "logout": { posthog?.capture("Call Logout"); @@ -136,11 +137,46 @@ export function Home() { /> ); } + case "execute": { posthog?.capture("Call Execute"); + const ctx = context as ExecuteCtx; + if (!hasSessionForPolicies) { + return ( + { + const res = await execute({ + setContext: (nextCtx) => { + setContext(nextCtx); + }, + })( + ctx.transactions, + ctx.abis || [], + ctx.transactionsDetail, + false, + ); + + setHasSessionForPolicies(true); + + if ("transaction_hash" in res) { + // resets execute ui + setContext(undefined); + return ctx.resolve?.({ + code: ResponseCodes.SUCCESS, + transaction_hash: res.transaction_hash, + }); + } + }} + /> + ); + } + return ; } + case "deploy": { posthog?.capture("Call Deploy"); diff --git a/packages/keychain/src/components/transaction/ConfirmTransaction.tsx b/packages/keychain/src/components/transaction/ConfirmTransaction.tsx index ed186cfcc..eeb23e83a 100644 --- a/packages/keychain/src/components/transaction/ConfirmTransaction.tsx +++ b/packages/keychain/src/components/transaction/ConfirmTransaction.tsx @@ -1,19 +1,14 @@ -import { useEffect, useMemo, useState } from "react"; import { ResponseCodes, toArray } from "@cartridge/controller"; import { Content, FOOTER_MIN_HEIGHT } from "@/components/layout"; import { TransactionDuoIcon } from "@cartridge/ui-next"; import { useConnection } from "@/hooks/connection"; import { TransactionSummary } from "@/components/transaction/TransactionSummary"; import { ExecuteCtx } from "@/utils/connection"; -import { getChecksumAddress, num } from "starknet"; +import { num } from "starknet"; import { ExecutionContainer } from "@/components/ExecutionContainer"; -import { CreateSession } from "../connect"; -import { ParsedSessionPolicies } from "@/hooks/session"; export function ConfirmTransaction() { - const { controller, context, origin, policies, setContext } = useConnection(); - const [policiesUpdated, setIsPoliciesUpdated] = useState(false); - const [updateSession, setUpdateSession] = useState(false); + const { controller, context, origin, setContext } = useConnection(); const ctx = context as ExecuteCtx; const account = controller; const transactions = toArray(ctx.transactions); @@ -34,63 +29,6 @@ export function ConfirmTransaction() { setContext(undefined); }; - const callPolicies = useMemo(() => { - const contracts = new Map(); - - transactions.forEach((c) => { - const address = getChecksumAddress(c.contractAddress); - if (contracts.has(address)) { - // Extend methods if contract already exists - const existing = contracts.get(address); - existing.methods.push({ entrypoint: c.entrypoint }); - } else { - // Add new contract entry - contracts.set(address, { methods: [{ entrypoint: c.entrypoint }] }); - } - }); - - return { - contracts: Object.fromEntries(contracts), - verified: false, - }; - }, [ctx.transactions]); - - useEffect(() => { - if (policiesUpdated) { - setUpdateSession(false); - return; - } - - const entries = Object.entries(callPolicies.contracts || {}); - const txnsApproved = entries.every(([target, policy]) => { - const contract = policies?.contracts?.[target]; - if (!contract) return false; - return policy.methods.every((method) => - contract.methods.some((m) => m.entrypoint === method.entrypoint), - ); - }); - - // If calls are approved by dapp specified policies but not stored session - // then prompt user to update session. This also accounts for expired sessions. - if (txnsApproved && account) { - account.isRequestedSession(callPolicies).then((isRequestedSession) => { - setUpdateSession(!isRequestedSession); - }); - } else { - setUpdateSession(false); - } - }, [policiesUpdated, policies, account]); - - if (updateSession && policies) { - return ( - setIsPoliciesUpdated(true)} - /> - ); - } - return ( { diff --git a/packages/keychain/src/utils/connection/index.ts b/packages/keychain/src/utils/connection/index.ts index 702bf934a..bad2c033d 100644 --- a/packages/keychain/src/utils/connection/index.ts +++ b/packages/keychain/src/utils/connection/index.ts @@ -5,7 +5,7 @@ import Controller from "@/utils/controller"; import { connect } from "./connect"; import { execute } from "./execute"; import { estimateDeclareFee, estimateInvokeFee } from "./estimate"; -import { probeFactory } from "./probe"; +import { probe } from "./probe"; import { signMessageFactory } from "./sign"; import { fetchControllers } from "./fetchControllers"; import { ConnectionCtx } from "./types"; @@ -41,7 +41,7 @@ export function connectToController({ execute: () => execute({ setContext }), estimateDeclareFee: () => estimateDeclareFee, estimateInvokeFee: () => estimateInvokeFee, - probe: normalize(probeFactory({ setController, setRpcUrl })), + probe: normalize(probe({ setController, setRpcUrl })), signMessage: () => signMessageFactory(setContext), openSettings: () => openSettingsFactory(setContext), reset: () => () => setContext(undefined), diff --git a/packages/keychain/src/utils/connection/probe.ts b/packages/keychain/src/utils/connection/probe.ts index af334e722..cb9b03f1d 100644 --- a/packages/keychain/src/utils/connection/probe.ts +++ b/packages/keychain/src/utils/connection/probe.ts @@ -1,7 +1,7 @@ import { ProbeReply, ResponseCodes } from "@cartridge/controller"; import Controller from "@/utils/controller"; -export function probeFactory({ +export function probe({ setController, setRpcUrl, }: { diff --git a/packages/keychain/src/utils/cookie.ts b/packages/keychain/src/utils/cookie.ts deleted file mode 100644 index 08c71ebaa..000000000 --- a/packages/keychain/src/utils/cookie.ts +++ /dev/null @@ -1,11 +0,0 @@ -export function isSignedUp() { - return document.cookie.search("controller=1") >= 0; -} - -export function setIsSignedUp() { - if (isSignedUp()) { - return; - } - - document.cookie = "controller=1; SameSite=None; Secure"; -}