@@ -6,15 +6,17 @@ import { toast } from "sonner";
66import { getContract , sendAndConfirmTransaction } from "thirdweb" ;
77import type { Chain , ThirdwebClient } from "thirdweb" ;
88import { defineChain } from "thirdweb/chains" ;
9- import { addSessionKey , isActiveSigner } from "thirdweb/extensions/erc4337" ;
9+ import {
10+ addSessionKey ,
11+ shouldUpdateSessionKey ,
12+ } from "thirdweb/extensions/erc4337" ;
1013import {
1114 ConnectButton ,
1215 ConnectEmbed ,
1316 useActiveAccount ,
1417 useActiveWallet ,
1518 useSiweAuth ,
1619} from "thirdweb/react" ;
17- import { isContractDeployed } from "thirdweb/utils" ;
1820import { type Account , inAppWallet } from "thirdweb/wallets" ;
1921import { createCode } from "../actions/create-code" ;
2022import {
@@ -163,39 +165,37 @@ async function ensureSessionKey(options: {
163165 client : options . client ,
164166 } ) ;
165167
168+ const newPermissions = {
169+ approvedTargets :
170+ typeof options . permissions [ "contracts:write" ] === "boolean"
171+ ? ( "*" as const )
172+ : options . permissions [ "contracts:write" ] ,
173+ permissionEndTimestamp : options . permissions . expiration ,
174+ nativeTokenLimitPerTransaction : options . permissions [ "native:spend" ] ,
175+ } ;
176+
177+ const needsUpdate = await shouldUpdateSessionKey ( {
178+ accountContract,
179+ sessionKeyAddress : options . sessionKeySignerAddress ,
180+ newPermissions,
181+ } ) ;
182+
166183 // check if already added
167- const accountDeployed = await isContractDeployed ( accountContract ) ;
168- if ( accountDeployed ) {
169- if (
170- await isActiveSigner ( {
171- contract : accountContract ,
172- signer : options . sessionKeySignerAddress ,
173- } )
174- ) {
175- return {
176- success : true ,
177- message : "Session key already added" ,
178- transaction : null ,
179- } ;
180- }
184+ if ( ! needsUpdate ) {
185+ return {
186+ success : true ,
187+ message : "Session key already added" ,
188+ transaction : null ,
189+ } ;
181190 }
182- // if not added, send tx to add the session key
183-
191+ // if not added or needs to be updated, send tx to add the session key
184192 const tx = await sendAndConfirmTransaction ( {
185193 account : options . account ,
186194 transaction : addSessionKey ( {
187195 account : options . account ,
188196 contract : accountContract ,
189197 sessionKeyAddress : options . sessionKeySignerAddress ,
190- // hard coded for now
191- permissions : {
192- approvedTargets :
193- typeof options . permissions [ "contracts:write" ] === "boolean"
194- ? "*"
195- : options . permissions [ "contracts:write" ] ,
196- permissionEndTimestamp : options . permissions . expiration ,
197- nativeTokenLimitPerTransaction : options . permissions [ "native:spend" ] ,
198- } ,
198+ permissions : newPermissions ,
199199 } ) ,
200200 } ) ;
201201
0 commit comments