Skip to content

Commit

Permalink
Set expiresAt based on selected duration
Browse files Browse the repository at this point in the history
  • Loading branch information
JunichiSugiura committed Dec 18, 2024
1 parent b984983 commit 63ba1b5
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions packages/keychain/src/components/connect/CreateSession.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useCallback, useEffect, useMemo, useState } from "react";
import { useConnection } from "hooks/connection";
import { ControllerErrorAlert } from "components/ErrorAlert";
import { SessionConsent } from "components/connect";
import { DEFAULT_SESSION_DURATION, SESSION_EXPIRATION } from "const";
import { DEFAULT_SESSION_DURATION } from "const";
import { Upgrade } from "./Upgrade";
import { ErrorCode } from "@cartridge/account-wasm";
import { TypedDataPolicy } from "@cartridge/presets";
Expand Down Expand Up @@ -35,18 +35,19 @@ export function CreateSession({
const [isDisabled, setIsDisabled] = useState(false);
const [isConsent, setIsConsent] = useState(false);
const [duration, setDuration] = useState<bigint>(DEFAULT_SESSION_DURATION);
const expiresAt = useMemo(() => {
return SESSION_EXPIRATION;
}, []);
const expiresAt = useMemo(
() => duration + BigInt(Math.floor(Date.now() / 1000)),
[duration],
);
const [maxFee] = useState<BigNumberish>();
const [error, setError] = useState<ControllerError | Error>();

useEffect(() => {
if (!chainId) return;
const normalizedChainId = normalizeChainId(chainId);

const violatingPolicy = policies?.messages?.find(
(policy) =>
const violatingPolicy = policies.messages?.find(
(policy: TypedDataPolicy) =>
"domain" in policy &&
(!policy.domain.chainId ||
normalizeChainId(policy.domain.chainId) !== normalizedChainId),
Expand All @@ -55,11 +56,9 @@ export function CreateSession({
if (violatingPolicy) {
setError({
code: ErrorCode.PolicyChainIdMismatch,
message: `Policy for ${
(violatingPolicy as TypedDataPolicy).domain.name
}.${
(violatingPolicy as TypedDataPolicy).primaryType
} has mismatched chain ID.`,
message: `Policy for ${(violatingPolicy as TypedDataPolicy).domain.name
}.${(violatingPolicy as TypedDataPolicy).primaryType
} has mismatched chain ID.`,
});
setIsDisabled(true);
} else {
Expand Down Expand Up @@ -164,7 +163,7 @@ export function CreateSession({
<Select
value={duration.toString()}
onValueChange={(val) => {
console.log(val);
setDuration(BigInt(val));
}}
>
<SelectTrigger className="w-28">
Expand Down

0 comments on commit 63ba1b5

Please sign in to comment.