@@ -7,206 +7,105 @@ import {
77 createSessionWithSig ,
88 isCreateSessionWithSigSupported ,
99} from "../__generated__/MinimalAccount/write/createSessionWithSig.js" ;
10- import {
11- type CallSpecInput ,
12- CallSpecRequest ,
13- ConstraintRequest ,
14- SessionSpecRequest ,
15- type TransferSpecInput ,
16- TransferSpecRequest ,
17- UsageLimitRequest ,
18- } from "./types.js" ;
19-
20- // MinimalAccount ABI - using the ABI definition from the JSON file
21- const MinimalAccountAbi = [
22- "error AllowanceExceeded(uint256 allowanceUsage, uint256 limit, uint64 period)" ,
23- "error CallPolicyViolated(address target, bytes4 selector)" ,
24- "error CallReverted()" ,
25- "error ConditionFailed(bytes32 param, bytes32 refValue, uint8 condition)" ,
26- "error InvalidDataLength(uint256 actualLength, uint256 expectedLength)" ,
27- "error InvalidSignature(address msgSender, address thisAddress)" ,
28- "error LifetimeUsageExceeded(uint256 lifetimeUsage, uint256 limit)" ,
29- "error MaxValueExceeded(uint256 value, uint256 maxValuePerUse)" ,
30- "error NoCallsToExecute()" ,
31- "error SessionExpired()" ,
32- "error SessionExpiresTooSoon()" ,
33- "error SessionZeroSigner()" ,
34- "error TransferPolicyViolated(address target)" ,
35- "error UIDAlreadyProcessed()" ,
36- "event Executed(address indexed to, uint256 value, bytes data)" ,
37- "event SessionCreated(address indexed signer, (address signer, bool isWildcard, uint256 expiresAt, (address target, bytes4 selector, uint256 maxValuePerUse, (uint8 limitType, uint256 limit, uint256 period) valueLimit, (uint8 condition, uint64 index, bytes32 refValue, (uint8 limitType, uint256 limit, uint256 period) limit)[] constraints)[] callPolicies, (address target, uint256 maxValuePerUse, (uint8 limitType, uint256 limit, uint256 period) valueLimit)[] transferPolicies, bytes32 uid) sessionSpec)" ,
38- "function createSessionWithSig((address signer, bool isWildcard, uint256 expiresAt, (address target, bytes4 selector, uint256 maxValuePerUse, (uint8 limitType, uint256 limit, uint256 period) valueLimit, (uint8 condition, uint64 index, bytes32 refValue, (uint8 limitType, uint256 limit, uint256 period) limit)[] constraints)[] callPolicies, (address target, uint256 maxValuePerUse, (uint8 limitType, uint256 limit, uint256 period) valueLimit)[] transferPolicies, bytes32 uid) sessionSpec, bytes signature)" ,
39- "function eip712Domain() view returns (bytes1 fields, string name, string version, uint256 chainId, address verifyingContract, bytes32 salt, uint256[] extensions)" ,
40- "function execute((address target, uint256 value, bytes data)[] calls) payable" ,
41- "function executeWithSig(((address target, uint256 value, bytes data)[] calls, bytes32 uid) wrappedCalls, bytes signature) payable" ,
42- "function getCallPoliciesForSigner(address signer) view returns ((address target, bytes4 selector, uint256 maxValuePerUse, (uint8 limitType, uint256 limit, uint256 period) valueLimit, (uint8 condition, uint64 index, bytes32 refValue, (uint8 limitType, uint256 limit, uint256 period) limit)[] constraints)[])" ,
43- "function getSessionExpirationForSigner(address signer) view returns (uint256)" ,
44- "function getSessionStateForSigner(address signer) view returns (((uint256 remaining, address target, bytes4 selector, uint256 index)[] transferValue, (uint256 remaining, address target, bytes4 selector, uint256 index)[] callValue, (uint256 remaining, address target, bytes4 selector, uint256 index)[] callParams))" ,
45- "function getTransferPoliciesForSigner(address signer) view returns ((address target, uint256 maxValuePerUse, (uint8 limitType, uint256 limit, uint256 period) valueLimit)[])" ,
46- "function isWildcardSigner(address signer) view returns (bool)" ,
47- ] as const ;
10+ import { type CallSpecInput , type TransferSpecInput } from "./types.js" ;
4811
49- /**
50- * @extension ERC7702
51- */
5212export type CreateSessionKeyOptions = {
53- /**
54- * The Thirdweb client
55- */
5613 client : ThirdwebClient ;
57- /**
58- * The chain to create the session key on
59- */
6014 chain : Chain ;
61- /**
62- * The admin account that will perform the operation.
63- */
6415 account : Account ;
65- /**
66- * The address to add as a session key.
67- */
6816 sessionKeyAddress : string ;
69- /**
70- * How long the session key should be valid for, in seconds.
71- */
72- durationInSeconds : number ;
73- /**
74- * Whether to grant full execution permissions to the session key.
75- */
17+ durationInSeconds ?: number ;
7618 grantFullPermissions ?: boolean ;
77- /**
78- * Smart contract interaction policies to apply to the session key, ignored if grantFullPermissions is true.
79- */
8019 callPolicies ?: CallSpecInput [ ] ;
81- /**
82- * Value transfer policies to apply to the session key, ignored if grantFullPermissions is true.
83- */
8420 transferPolicies ?: TransferSpecInput [ ] ;
8521} ;
8622
8723/**
88- * Creates session key permissions for a specified address.
89- * @param options - The options for the createSessionKey function.
90- * @returns The transaction object to be sent.
24+ * Creates a session key for the minimal account.
25+ * This allows delegation of specific permissions to another address.
26+ *
27+ * @param options - The options for creating the session key
28+ * @returns A prepared transaction to create the session key
29+ *
9130 * @example
9231 * ```ts
93- * import { createSessionKey } from ' thirdweb/wallets/in-app' ;
94- * import { sendTransaction } from ' thirdweb' ;
32+ * import { createSessionKey } from " thirdweb/extensions/erc7702/account" ;
33+ * import { defineChain } from " thirdweb/chains" ;
9534 *
9635 * const transaction = createSessionKey({
97- * client,
98- * chain,
99- * account: account,
100- * sessionKeyAddress: TEST_ACCOUNT_A.address,
101- * durationInSeconds: 86400, // 1 day
102- * grantFullPermissions: true
103- *})
104- *
105- * await sendTransaction({ transaction, account });
36+ * client,
37+ * chain: defineChain(1),
38+ * account,
39+ * sessionKeyAddress: "0x...",
40+ * durationInSeconds: 3600, // 1 hour
41+ * grantFullPermissions: true,
42+ * });
10643 * ```
107- * @extension ERC7702
10844 */
10945export function createSessionKey ( options : CreateSessionKeyOptions ) {
11046 const {
11147 client,
11248 chain,
11349 account,
11450 sessionKeyAddress,
115- durationInSeconds,
116- grantFullPermissions,
117- callPolicies,
118- transferPolicies,
51+ durationInSeconds = 86400 , // 24 hours default
52+ grantFullPermissions = false ,
53+ callPolicies = [ ] ,
54+ transferPolicies = [ ] ,
11955 } = options ;
12056
121- if ( durationInSeconds <= 0 ) {
122- throw new Error ( "durationInSeconds must be positive" ) ;
123- }
124-
125- // Create contract from account.address with MinimalAccount ABI
57+ // Create the contract using account.address - ABI will be auto-resolved by thirdweb
12658 const contract = getContract ( {
12759 address : account . address ,
12860 chain,
12961 client,
130- abi : MinimalAccountAbi ,
13162 } ) ;
13263
133- return createSessionWithSig ( {
134- async asyncParams ( ) {
135- const req = {
136- callPolicies : ( callPolicies || [ ] ) . map ( ( policy ) => ( {
64+ // Create the session spec object directly
65+ const sessionSpec = {
66+ signer : sessionKeyAddress ,
67+ isWildcard : grantFullPermissions ,
68+ expiresAt : BigInt ( Math . floor ( Date . now ( ) / 1000 ) + durationInSeconds ) ,
69+ callPolicies : grantFullPermissions
70+ ? [ ]
71+ : callPolicies . map ( ( policy ) => ( {
72+ target : policy . target ,
73+ selector : policy . selector ,
74+ maxValuePerUse : policy . maxValuePerUse || 0n ,
75+ valueLimit : policy . valueLimit || {
76+ limitType : 0 ,
77+ limit : 0n ,
78+ period : 0n ,
79+ } ,
13780 constraints : ( policy . constraints || [ ] ) . map ( ( constraint ) => ( {
138- condition : Number ( constraint . condition ) ,
139- index : constraint . index || BigInt ( 0 ) ,
140- limit : constraint . limit
141- ? {
142- limit : constraint . limit . limit ,
143- limitType : Number ( constraint . limit . limitType ) ,
144- period : constraint . limit . period ,
145- }
146- : {
147- limit : BigInt ( 0 ) ,
148- limitType : 0 ,
149- period : BigInt ( 0 ) ,
150- } ,
151- refValue : constraint . refValue || "0x" ,
81+ condition : constraint . condition ,
82+ index : constraint . index ,
83+ refValue : constraint . refValue ,
84+ limit : constraint . limit || {
85+ limitType : 0 ,
86+ limit : 0n ,
87+ period : 0n ,
88+ } ,
15289 } ) ) ,
153- maxValuePerUse : policy . maxValuePerUse || BigInt ( 0 ) ,
154- selector : policy . selector ,
155- target : policy . target ,
156- valueLimit : policy . valueLimit
157- ? {
158- limit : policy . valueLimit . limit ,
159- limitType : Number ( policy . valueLimit . limitType ) ,
160- period : policy . valueLimit . period ,
161- }
162- : {
163- limit : BigInt ( 0 ) ,
164- limitType : 0 ,
165- period : BigInt ( 0 ) ,
166- } ,
16790 } ) ) ,
168- expiresAt : BigInt ( Math . floor ( Date . now ( ) / 1000 ) + durationInSeconds ) ,
169- isWildcard : grantFullPermissions ?? true ,
170- signer : sessionKeyAddress ,
171- transferPolicies : ( transferPolicies || [ ] ) . map ( ( policy ) => ( {
172- maxValuePerUse : policy . maxValuePerUse || BigInt ( 0 ) ,
91+ transferPolicies : grantFullPermissions
92+ ? [ ]
93+ : transferPolicies . map ( ( policy ) => ( {
17394 target : policy . target ,
174- valueLimit : policy . valueLimit
175- ? {
176- limit : policy . valueLimit . limit ,
177- limitType : Number ( policy . valueLimit . limitType ) ,
178- period : policy . valueLimit . period ,
179- }
180- : {
181- limit : BigInt ( 0 ) ,
182- limitType : 0 ,
183- period : BigInt ( 0 ) ,
184- } ,
95+ maxValuePerUse : policy . maxValuePerUse || 0n ,
96+ valueLimit : policy . valueLimit || {
97+ limitType : 0 ,
98+ limit : 0n ,
99+ period : 0n ,
100+ } ,
185101 } ) ) ,
186- uid : await randomBytesHex ( ) ,
187- } ;
102+ uid : randomBytesHex ( 32 ) ,
103+ } ;
188104
189- const signature = await account . signTypedData ( {
190- domain : {
191- chainId : contract . chain . id ,
192- name : "MinimalAccount" ,
193- verifyingContract : contract . address ,
194- version : "1" ,
195- } ,
196- message : req ,
197- primaryType : "SessionSpec" ,
198- types : {
199- CallSpec : CallSpecRequest ,
200- Constraint : ConstraintRequest ,
201- SessionSpec : SessionSpecRequest ,
202- TransferSpec : TransferSpecRequest ,
203- UsageLimit : UsageLimitRequest ,
204- } ,
205- } ) ;
206-
207- return { sessionSpec : req , signature } ;
208- } ,
105+ return createSessionWithSig ( {
209106 contract,
107+ sessionSpec,
108+ signature : "0x" ,
210109 } ) ;
211110}
212111
0 commit comments