Skip to content

Commit dbfa5e1

Browse files
patchy-the-botPatchy Bot
andauthored
Add DelegationManager validation for EIP-7710 actions (#150) (#156)
Co-authored-by: Patchy Bot <patchy@localhost>
1 parent cae5943 commit dbfa5e1

2 files changed

Lines changed: 74 additions & 2 deletions

File tree

packages/smart-accounts-kit/src/actions/erc7710RedeemDelegationAction.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,20 @@ export async function sendTransactionWithDelegationAction<
5959
);
6060
}
6161

62+
// Validate DelegationManager address
63+
const chainId = client.chain?.id;
64+
if (!chainId) {
65+
throw new Error('Chain ID is not set');
66+
}
67+
68+
const { DelegationManager: expectedDelegationManager } =
69+
getSmartAccountsEnvironment(chainId);
70+
if (!isAddressEqual(args.delegationManager, expectedDelegationManager)) {
71+
throw new Error(
72+
`Invalid DelegationManager: expected ${expectedDelegationManager} for chain ${chainId}, but got ${args.delegationManager}`,
73+
);
74+
}
75+
6276
const executions = [
6377
createExecution({
6478
target: args.to,

packages/smart-accounts-kit/test/actions/erc7710RedeemDelegationAction.test.ts

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,14 +277,19 @@ describe('erc7710RedeemDelegationAction', () => {
277277

278278
const sendTransaction = stub(walletClient, 'sendTransaction');
279279

280+
const expectedDelegationManager = randomAddress();
281+
overrideDeployedEnvironment(chain.id, '1.3.0', {
282+
DelegationManager: expectedDelegationManager,
283+
} as any as SmartAccountsEnvironment);
284+
280285
const args: SendTransactionWithDelegationParameters = {
281286
account,
282287
chain,
283288
to: randomAddress(),
284289
value: 0n,
285290
data: randomBytes(128),
286291
permissionsContext: randomBytes(128),
287-
delegationManager: randomAddress(),
292+
delegationManager: expectedDelegationManager,
288293
};
289294

290295
await extendedWalletClient.sendTransactionWithDelegation(args);
@@ -349,14 +354,19 @@ describe('erc7710RedeemDelegationAction', () => {
349354

350355
const sendTransaction = stub(walletClient, 'sendTransaction');
351356

357+
const expectedDelegationManager = randomAddress();
358+
overrideDeployedEnvironment(chain.id, '1.3.0', {
359+
DelegationManager: expectedDelegationManager,
360+
} as any as SmartAccountsEnvironment);
361+
352362
const args: SendTransactionWithDelegationParameters = {
353363
account,
354364
chain,
355365
to: randomAddress(),
356366
value: 100n,
357367
data: randomBytes(128),
358368
permissionsContext: randomBytes(128),
359-
delegationManager: randomAddress(),
369+
delegationManager: expectedDelegationManager,
360370
};
361371

362372
await extendedWalletClient.sendTransactionWithDelegation(args);
@@ -371,5 +381,53 @@ describe('erc7710RedeemDelegationAction', () => {
371381
undefined,
372382
);
373383
});
384+
385+
it('should throw an error when DelegationManager does not match expected address for the chain', async () => {
386+
const extendedWalletClient = walletClient.extend(erc7710WalletActions());
387+
388+
const expectedDelegationManager = randomAddress();
389+
const invalidDelegationManager = randomAddress();
390+
391+
overrideDeployedEnvironment(chain.id, '1.3.0', {
392+
DelegationManager: expectedDelegationManager,
393+
} as any as SmartAccountsEnvironment);
394+
395+
await expect(
396+
extendedWalletClient.sendTransactionWithDelegation({
397+
account,
398+
chain,
399+
to: randomAddress(),
400+
value: 0n,
401+
data: randomBytes(128),
402+
permissionsContext: randomBytes(128),
403+
delegationManager: invalidDelegationManager,
404+
}),
405+
).rejects.toThrow(
406+
`Invalid DelegationManager: expected ${expectedDelegationManager} for chain ${chain.id}, but got ${invalidDelegationManager}`,
407+
);
408+
});
409+
410+
it('should throw an error when chain ID is not set', async () => {
411+
const walletClientWithoutChain = createWalletClient({
412+
account,
413+
transport: custom({ request: async () => '0x' }),
414+
});
415+
416+
const extendedWalletClient = walletClientWithoutChain.extend(
417+
erc7710WalletActions(),
418+
);
419+
420+
await expect(
421+
extendedWalletClient.sendTransactionWithDelegation({
422+
account,
423+
chain: undefined,
424+
to: randomAddress(),
425+
value: 0n,
426+
data: randomBytes(128),
427+
permissionsContext: randomBytes(128),
428+
delegationManager: randomAddress(),
429+
}),
430+
).rejects.toThrow('Chain ID is not set');
431+
});
374432
});
375433
});

0 commit comments

Comments
 (0)