@@ -18,8 +18,6 @@ import {
1818 createOpenDelegation ,
1919 decodeDelegations ,
2020 encodeDelegations ,
21- type CreateDelegationOptions ,
22- type CreateOpenDelegationOptions ,
2321} from '../delegation' ;
2422import type {
2523 Delegation ,
@@ -33,10 +31,8 @@ export type RedelegatePermissionContextParameters = {
3331 account ?: Account | Address ;
3432 /** Environment configuration */
3533 environment : SmartAccountsEnvironment ;
36- /** The permission context to redelegate from (from ERC-7715 response) */
34+ /** The permission context to redelegate from (i.e., from ERC-7715 response) */
3735 permissionContext : PermissionContext ;
38- /** The address of the delegation manager contract */
39- delegationManager : Address ;
4036 /** The chain ID for the signature */
4137 chainId : number ;
4238 /** Optional scope - if not provided, inherits from parent */
@@ -45,16 +41,9 @@ export type RedelegatePermissionContextParameters = {
4541 caveats ?: Caveats ;
4642 /** Optional salt for uniqueness */
4743 salt ?: Hex ;
48- /** Name of the DelegationManager contract */
49- name ?: string ;
50- /** Version of the DelegationManager contract */
51- version ?: string ;
52- /** Whether to allow insecure unrestricted delegation */
53- allowInsecureUnrestrictedDelegation ?: boolean ;
54- } & (
55- | { delegate : Address } // Specific delegate
56- | { delegate ?: never } // Open delegation
57- ) ;
44+ /** The address of the delegate to redelegate to */
45+ to ?: Address ;
46+ } ;
5847
5948export type RedelegatePermissionContextReturnType = {
6049 /** The signed delegation that was created */
@@ -127,14 +116,11 @@ export async function redelegatePermissionContext<
127116 account : accountParam = client . account ,
128117 environment,
129118 permissionContext,
130- delegationManager,
131119 chainId,
132120 scope,
133121 caveats,
134122 salt,
135- name = 'DelegationManager' ,
136- version = '1' ,
137- allowInsecureUnrestrictedDelegation = false ,
123+ to,
138124 } = parameters ;
139125
140126 if ( ! accountParam ) {
@@ -145,72 +131,57 @@ export async function redelegatePermissionContext<
145131
146132 trackSmartAccountsKitFunctionCall ( 'redelegatePermissionContext' , {
147133 chainId,
148- hasDelegate : 'delegate' in parameters && parameters . delegate !== undefined ,
134+ hasDelegate : Boolean ( parameters . to ) ,
149135 hasScope : scope !== undefined ,
150136 hasCaveats : caveats !== undefined ,
151137 } ) ;
152138
153- // Decode the permission context to get the delegation chain
154139 const delegations = decodeDelegations ( permissionContext ) ;
155140
156- if ( delegations . length === 0 ) {
141+ const parentDelegation = delegations [ 0 ] ;
142+
143+ if ( ! parentDelegation ) {
157144 throw new BaseError (
158145 'Permission context must contain at least one delegation' ,
159146 ) ;
160147 }
161148
162- // The leaf delegation is the first element (chain ordered leaf to root)
163- const leafDelegation = delegations [ 0 ] ;
164-
165- // Create the unsigned delegation
166- // We always pass parentDelegation as the leaf delegation
167- // TypeScript struggles with the discriminated union, so we build the object explicitly
168- let unsignedDelegation : Omit < Delegation , 'signature' > ;
169-
170- const commonOptions = {
149+ const createDelegationOptions = {
171150 environment,
172151 from : account . address ,
173- parentDelegation : leafDelegation as Delegation | Hex ,
174- ... ( scope !== undefined && { scope } ) ,
175- ... ( caveats !== undefined && { caveats } ) ,
176- ... ( salt !== undefined && { salt } ) ,
152+ scope ,
153+ caveats ,
154+ parentDelegation ,
155+ salt,
177156 } ;
178157
179- if ( 'delegate' in parameters && parameters . delegate ) {
180- unsignedDelegation = createDelegation ( {
181- ...commonOptions ,
182- to : parameters . delegate ,
183- } as CreateDelegationOptions ) ;
184- } else {
185- unsignedDelegation = createOpenDelegation (
186- commonOptions as CreateOpenDelegationOptions ,
187- ) ;
188- }
158+ const unsignedDelegation = to
159+ ? createDelegation ( {
160+ ...createDelegationOptions ,
161+ to,
162+ } )
163+ : createOpenDelegation ( createDelegationOptions ) ;
189164
190- // Sign the delegation
191165 const signature = await signDelegation ( client , {
192- account : accountParam ,
166+ account,
193167 delegation : unsignedDelegation ,
194- delegationManager,
168+ delegationManager : environment . DelegationManager ,
195169 chainId,
196- name,
197- version,
198- allowInsecureUnrestrictedDelegation,
199170 } ) ;
200171
201- // Create the signed delegation
202172 const signedDelegation : Delegation = {
203173 ...unsignedDelegation ,
204174 signature,
205175 } ;
206176
207- // Prepend the new delegation to create the new chain
208- const newDelegationChain = [ signedDelegation , ...delegations ] ;
177+ const newPermissionContext = encodeDelegations ( [
178+ signedDelegation ,
179+ ...delegations ,
180+ ] ) ;
209181
210- // Return both the delegation and the encoded permission context
211182 return {
212183 delegation : signedDelegation ,
213- permissionContext : encodeDelegations ( newDelegationChain ) ,
184+ permissionContext : newPermissionContext ,
214185 } ;
215186}
216187
@@ -227,10 +198,9 @@ export async function redelegatePermissionContext<
227198 *
228199 * // Now you can call it directly on the client
229200 * const result = await walletClient.redelegatePermissionContext({
230- * permissionContext: erc7715Response.context,
231- * delegate: charlie.address,
232201 * environment,
233- * delegationManager: environment.DelegationManager,
202+ * permissionContext: erc7715Response.context,
203+ * to: charlie.address,
234204 * });
235205 * ```
236206 */
0 commit comments