Skip to content

Commit

Permalink
use shouldUpdateSessionKey in login flow
Browse files Browse the repository at this point in the history
  • Loading branch information
joaquim-verges committed Feb 26, 2025
1 parent 84d1623 commit f336ee7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 27 deletions.
54 changes: 27 additions & 27 deletions apps/login/src/components/login-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ import { toast } from "sonner";
import { getContract, sendAndConfirmTransaction } from "thirdweb";
import type { Chain, ThirdwebClient } from "thirdweb";
import { defineChain } from "thirdweb/chains";
import { addSessionKey, isActiveSigner } from "thirdweb/extensions/erc4337";
import {
addSessionKey,
shouldUpdateSessionKey,
} from "thirdweb/extensions/erc4337";
import {
ConnectButton,
ConnectEmbed,
useActiveAccount,
useActiveWallet,
useSiweAuth,
} from "thirdweb/react";
import { isContractDeployed } from "thirdweb/utils";
import { type Account, inAppWallet } from "thirdweb/wallets";
import { createCode } from "../actions/create-code";
import {
Expand Down Expand Up @@ -163,39 +165,37 @@ async function ensureSessionKey(options: {
client: options.client,
});

const newPermissions = {
approvedTargets:
typeof options.permissions["contracts:write"] === "boolean"
? ("*" as const)
: options.permissions["contracts:write"],
permissionEndTimestamp: options.permissions.expiration,
nativeTokenLimitPerTransaction: options.permissions["native:spend"],
};

const needsUpdate = await shouldUpdateSessionKey({
accountContract,
sessionKeyAddress: options.sessionKeySignerAddress,
newPermissions,
});

// check if already added
const accountDeployed = await isContractDeployed(accountContract);
if (accountDeployed) {
if (
await isActiveSigner({
contract: accountContract,
signer: options.sessionKeySignerAddress,
})
) {
return {
success: true,
message: "Session key already added",
transaction: null,
};
}
if (!needsUpdate) {
return {
success: true,
message: "Session key already added",
transaction: null,
};
}
// if not added, send tx to add the session key

// if not added or needs to be updated, send tx to add the session key
const tx = await sendAndConfirmTransaction({
account: options.account,
transaction: addSessionKey({
account: options.account,
contract: accountContract,
sessionKeyAddress: options.sessionKeySignerAddress,
// hard coded for now
permissions: {
approvedTargets:
typeof options.permissions["contracts:write"] === "boolean"
? "*"
: options.permissions["contracts:write"],
permissionEndTimestamp: options.permissions.expiration,
nativeTokenLimitPerTransaction: options.permissions["native:spend"],
},
permissions: newPermissions,
}),
});

Expand Down
5 changes: 5 additions & 0 deletions packages/thirdweb/test/globalSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,8 @@ export default async function globalSetup() {
// await shutdownOdyssey();
};
}

process.on("unhandledRejection", (reason) => {
console.error(`Vitest unhandledRejection: ${reason}`);
throw reason;
});
5 changes: 5 additions & 0 deletions packages/thirdweb/test/reactSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ import { afterEach } from "vitest";
afterEach(() => {
cleanup();
});

process.on("unhandledRejection", (reason) => {
console.error(`Vitest unhandledRejection: ${reason}`);
throw reason;
});

0 comments on commit f336ee7

Please sign in to comment.