@@ -2,47 +2,47 @@ import type { BaseTransactionOptions } from "../../../transaction/types.js";
22import { randomBytesHex } from "../../../utils/random.js" ;
33import type { Account } from "../../../wallets/interfaces/wallet.js" ;
44import {
5- createSessionWithSig ,
6- isCreateSessionWithSigSupported ,
5+ createSessionWithSig ,
6+ isCreateSessionWithSigSupported ,
77} from "../__generated__/MinimalAccount/write/createSessionWithSig.js" ;
88import {
9- type CallSpecInput ,
10- CallSpecRequest ,
11- ConstraintRequest ,
12- SessionSpecRequest ,
13- type TransferSpecInput ,
14- TransferSpecRequest ,
15- UsageLimitRequest ,
9+ type CallSpecInput ,
10+ CallSpecRequest ,
11+ ConstraintRequest ,
12+ SessionSpecRequest ,
13+ type TransferSpecInput ,
14+ TransferSpecRequest ,
15+ UsageLimitRequest ,
1616} from "./types.js" ;
1717
1818/**
1919 * @extension ERC7702
2020 */
2121export type CreateSessionKeyOptions = {
22- /**
23- * The admin account that will perform the operation.
24- */
25- account : Account ;
26- /**
27- * The address to add as a session key.
28- */
29- sessionKeyAddress : string ;
30- /**
31- * How long the session key should be valid for, in seconds.
32- */
33- durationInSeconds : number ;
34- /**
35- * Whether to grant full execution permissions to the session key.
36- */
37- grantFullPermissions ?: boolean ;
38- /**
39- * Smart contract interaction policies to apply to the session key, ignored if grantFullPermissions is true.
40- */
41- callPolicies ?: CallSpecInput [ ] ;
42- /**
43- * Value transfer policies to apply to the session key, ignored if grantFullPermissions is true.
44- */
45- transferPolicies ?: TransferSpecInput [ ] ;
22+ /**
23+ * The admin account that will perform the operation.
24+ */
25+ account : Account ;
26+ /**
27+ * The address to add as a session key.
28+ */
29+ sessionKeyAddress : string ;
30+ /**
31+ * How long the session key should be valid for, in seconds.
32+ */
33+ durationInSeconds : number ;
34+ /**
35+ * Whether to grant full execution permissions to the session key.
36+ */
37+ grantFullPermissions ?: boolean ;
38+ /**
39+ * Smart contract interaction policies to apply to the session key, ignored if grantFullPermissions is true.
40+ */
41+ callPolicies ?: CallSpecInput [ ] ;
42+ /**
43+ * Value transfer policies to apply to the session key, ignored if grantFullPermissions is true.
44+ */
45+ transferPolicies ?: TransferSpecInput [ ] ;
4646} ;
4747
4848/**
@@ -68,94 +68,100 @@ export type CreateSessionKeyOptions = {
6868 * @extension ERC7702
6969 */
7070export function createSessionKey (
71- options : BaseTransactionOptions < CreateSessionKeyOptions > ,
71+ options : BaseTransactionOptions < CreateSessionKeyOptions > ,
7272) {
73- const {
74- contract,
75- account,
76- sessionKeyAddress,
77- durationInSeconds,
78- grantFullPermissions,
79- callPolicies,
80- transferPolicies,
81- } = options ;
73+ const {
74+ contract,
75+ account,
76+ sessionKeyAddress,
77+ durationInSeconds,
78+ grantFullPermissions,
79+ callPolicies,
80+ transferPolicies,
81+ } = options ;
8282
83- if ( durationInSeconds <= 0 ) {
84- throw new Error ( "durationInSeconds must be positive" ) ;
85- }
83+ if ( durationInSeconds <= 0 ) {
84+ throw new Error ( "durationInSeconds must be positive" ) ;
85+ }
8686
87- return createSessionWithSig ( {
88- async asyncParams ( ) {
89- const req = {
90- callPolicies : ( callPolicies || [ ] ) . map ( ( policy ) => ( {
91- constraints : ( policy . constraints || [ ] ) . map ( ( constraint ) => ( {
92- condition : Number ( constraint . condition ) ,
93- index : constraint . index || BigInt ( 0 ) ,
94- limit : constraint . limit ? {
95- limit : constraint . limit . limit ,
96- limitType : Number ( constraint . limit . limitType ) ,
97- period : constraint . limit . period ,
98- } : {
99- limit : BigInt ( 0 ) ,
100- limitType : 0 ,
101- period : BigInt ( 0 ) ,
102- } ,
103- refValue : constraint . refValue || "0x" ,
104- } ) ) ,
105- maxValuePerUse : policy . maxValuePerUse || BigInt ( 0 ) ,
106- selector : policy . selector ,
107- target : policy . target ,
108- valueLimit : policy . valueLimit ? {
109- limit : policy . valueLimit . limit ,
110- limitType : Number ( policy . valueLimit . limitType ) ,
111- period : policy . valueLimit . period ,
112- } : {
113- limit : BigInt ( 0 ) ,
114- limitType : 0 ,
115- period : BigInt ( 0 ) ,
116- } ,
117- } ) ) ,
118- expiresAt : BigInt ( Math . floor ( Date . now ( ) / 1000 ) + durationInSeconds ) ,
119- isWildcard : grantFullPermissions ?? true ,
120- signer : sessionKeyAddress ,
121- transferPolicies : ( transferPolicies || [ ] ) . map ( ( policy ) => ( {
122- maxValuePerUse : policy . maxValuePerUse || BigInt ( 0 ) ,
123- target : policy . target ,
124- valueLimit : policy . valueLimit ? {
125- limit : policy . valueLimit . limit ,
126- limitType : Number ( policy . valueLimit . limitType ) ,
127- period : policy . valueLimit . period ,
128- } : {
129- limit : BigInt ( 0 ) ,
130- limitType : 0 ,
131- period : BigInt ( 0 ) ,
132- } ,
133- } ) ) ,
134- uid : await randomBytesHex ( ) ,
135- } ;
87+ return createSessionWithSig ( {
88+ async asyncParams ( ) {
89+ const req = {
90+ callPolicies : ( callPolicies || [ ] ) . map ( ( policy ) => ( {
91+ constraints : ( policy . constraints || [ ] ) . map ( ( constraint ) => ( {
92+ condition : Number ( constraint . condition ) ,
93+ index : constraint . index || BigInt ( 0 ) ,
94+ limit : constraint . limit
95+ ? {
96+ limit : constraint . limit . limit ,
97+ limitType : Number ( constraint . limit . limitType ) ,
98+ period : constraint . limit . period ,
99+ }
100+ : {
101+ limit : BigInt ( 0 ) ,
102+ limitType : 0 ,
103+ period : BigInt ( 0 ) ,
104+ } ,
105+ refValue : constraint . refValue || "0x" ,
106+ } ) ) ,
107+ maxValuePerUse : policy . maxValuePerUse || BigInt ( 0 ) ,
108+ selector : policy . selector ,
109+ target : policy . target ,
110+ valueLimit : policy . valueLimit
111+ ? {
112+ limit : policy . valueLimit . limit ,
113+ limitType : Number ( policy . valueLimit . limitType ) ,
114+ period : policy . valueLimit . period ,
115+ }
116+ : {
117+ limit : BigInt ( 0 ) ,
118+ limitType : 0 ,
119+ period : BigInt ( 0 ) ,
120+ } ,
121+ } ) ) ,
122+ expiresAt : BigInt ( Math . floor ( Date . now ( ) / 1000 ) + durationInSeconds ) ,
123+ isWildcard : grantFullPermissions ?? true ,
124+ signer : sessionKeyAddress ,
125+ transferPolicies : ( transferPolicies || [ ] ) . map ( ( policy ) => ( {
126+ maxValuePerUse : policy . maxValuePerUse || BigInt ( 0 ) ,
127+ target : policy . target ,
128+ valueLimit : policy . valueLimit
129+ ? {
130+ limit : policy . valueLimit . limit ,
131+ limitType : Number ( policy . valueLimit . limitType ) ,
132+ period : policy . valueLimit . period ,
133+ }
134+ : {
135+ limit : BigInt ( 0 ) ,
136+ limitType : 0 ,
137+ period : BigInt ( 0 ) ,
138+ } ,
139+ } ) ) ,
140+ uid : await randomBytesHex ( ) ,
141+ } ;
136142
137- const signature = await account . signTypedData ( {
138- domain : {
139- chainId : contract . chain . id ,
140- name : "MinimalAccount" ,
141- verifyingContract : contract . address ,
142- version : "1" ,
143- } ,
144- message : req ,
145- primaryType : "SessionSpec" ,
146- types : {
147- CallSpec : CallSpecRequest ,
148- Constraint : ConstraintRequest ,
149- SessionSpec : SessionSpecRequest ,
150- TransferSpec : TransferSpecRequest ,
151- UsageLimit : UsageLimitRequest ,
152- } ,
153- } ) ;
143+ const signature = await account . signTypedData ( {
144+ domain : {
145+ chainId : contract . chain . id ,
146+ name : "MinimalAccount" ,
147+ verifyingContract : contract . address ,
148+ version : "1" ,
149+ } ,
150+ message : req ,
151+ primaryType : "SessionSpec" ,
152+ types : {
153+ CallSpec : CallSpecRequest ,
154+ Constraint : ConstraintRequest ,
155+ SessionSpec : SessionSpecRequest ,
156+ TransferSpec : TransferSpecRequest ,
157+ UsageLimit : UsageLimitRequest ,
158+ } ,
159+ } ) ;
154160
155- return { sessionSpec : req , signature } ;
156- } ,
157- contract,
158- } ) ;
161+ return { sessionSpec : req , signature } ;
162+ } ,
163+ contract,
164+ } ) ;
159165}
160166
161167/**
@@ -171,5 +177,5 @@ export function createSessionKey(
171177 * ```
172178 */
173179export function isCreateSessionKeySupported ( availableSelectors : string [ ] ) {
174- return isCreateSessionWithSigSupported ( availableSelectors ) ;
180+ return isCreateSessionWithSigSupported ( availableSelectors ) ;
175181}
0 commit comments