Skip to content

Commit a38dbf5

Browse files
patchy-the-botPatchy Bot
andauthored
refactor: rename getDelegationHashOffchain to hashDelegation (#162)
Co-authored-by: Patchy Bot <patchy@localhost>
1 parent b456564 commit a38dbf5

15 files changed

Lines changed: 60 additions & 77 deletions

packages/delegator-e2e/test/actions/erc7710sendTransactionWithDelegation.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {
2929
} from 'viem';
3030
import {
3131
encodeDelegations,
32-
getDelegationHashOffchain,
32+
hashDelegation,
3333
} from '@metamask/smart-accounts-kit/utils';
3434

3535
import CounterMetadata from '../utils/counter/metadata.json';
@@ -147,7 +147,7 @@ test('Bob redelegates to Carol, who redeems the delegation to call increment() o
147147
const redelegation: Delegation = {
148148
delegate: carol.address,
149149
delegator: bob.address,
150-
authority: getDelegationHashOffchain(signedDelegation),
150+
authority: hashDelegation(signedDelegation),
151151
salt: '0x0',
152152
caveats: [],
153153
signature: '0x',

packages/delegator-e2e/test/caveats/erc20TransferAmount.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
encodeExecutionCalldatas,
44
encodeDelegations,
55
createCaveatBuilder,
6-
getDelegationHashOffchain,
6+
hashDelegation,
77
} from '@metamask/smart-accounts-kit/utils';
88
import {
99
createExecution,
@@ -357,7 +357,7 @@ describe('ERC20TransferAmountEnforcer Utilities E2E Tests', () => {
357357
};
358358

359359
// Get delegation hash and delegation manager
360-
const delegationHash = getDelegationHashOffchain(signedDelegation);
360+
const delegationHash = hashDelegation(signedDelegation);
361361
const delegationManager = aliceSmartAccount.environment.DelegationManager;
362362

363363
// Check spent amount before any transfers (should be 0)
@@ -526,7 +526,7 @@ describe('ERC20TransferAmountEnforcer Utilities E2E Tests', () => {
526526
};
527527

528528
// Get delegation hash and delegation manager
529-
const delegationHash = getDelegationHashOffchain(signedDelegation);
529+
const delegationHash = hashDelegation(signedDelegation);
530530
const delegationManager = aliceSmartAccount.environment.DelegationManager;
531531

532532
// Check spent amount before transfer (should be 0)
@@ -655,7 +655,7 @@ describe('ERC20TransferAmountEnforcer Utilities E2E Tests', () => {
655655
throw new Error('No caveats found in delegation');
656656
}
657657

658-
const delegationHash = getDelegationHashOffchain(signedDelegation);
658+
const delegationHash = hashDelegation(signedDelegation);
659659
const delegationManager = aliceSmartAccount.environment.DelegationManager;
660660

661661
// Test getTermsInfo utility vs manual decoding

packages/delegator-e2e/test/caveats/limitedCalls.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
createCaveatBuilder,
1313
encodeExecutionCalldatas,
1414
encodeDelegations,
15-
getDelegationHashOffchain,
15+
hashDelegation,
1616
} from '@metamask/smart-accounts-kit/utils';
1717
import { LimitedCallsEnforcer } from '@metamask/smart-accounts-kit/contracts';
1818

@@ -209,7 +209,7 @@ describe('LimitedCallsEnforcer Contract Read Methods', () => {
209209
};
210210

211211
// Calculate delegation hash
212-
const delegationHash = getDelegationHashOffchain(delegation);
212+
const delegationHash = hashDelegation(delegation);
213213

214214
const callCount = await LimitedCallsEnforcer.read.callCounts({
215215
client: publicClient,
@@ -244,7 +244,7 @@ describe('LimitedCallsEnforcer Contract Read Methods', () => {
244244
};
245245

246246
// Calculate delegation hash
247-
const delegationHash = getDelegationHashOffchain(delegation);
247+
const delegationHash = hashDelegation(delegation);
248248

249249
// Check initial call count
250250
let callCount = await LimitedCallsEnforcer.read.callCounts({
@@ -342,8 +342,8 @@ describe('LimitedCallsEnforcer Contract Read Methods', () => {
342342
};
343343

344344
// Calculate delegation hashes
345-
const delegationHash1 = getDelegationHashOffchain(delegation1);
346-
const delegationHash2 = getDelegationHashOffchain(delegation2);
345+
const delegationHash1 = hashDelegation(delegation1);
346+
const delegationHash2 = hashDelegation(delegation2);
347347

348348
// Redeem delegation1 once
349349
const newCount = hexToBigInt(randomBytes(32));
@@ -432,7 +432,7 @@ describe('LimitedCallsEnforcer Contract Read Methods', () => {
432432
expect(decodedLimit).toBe(BigInt(limit));
433433

434434
// Calculate delegation hash
435-
const delegationHash = getDelegationHashOffchain(delegation);
435+
const delegationHash = hashDelegation(delegation);
436436

437437
// Test callCounts with real delegation hash
438438
const initialCallCount = await LimitedCallsEnforcer.read.callCounts({

packages/delegator-e2e/test/caveats/nativeTokenPayment.test.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { beforeEach, test, expect } from 'vitest';
22
import {
33
encodeDelegations,
44
encodeExecutionCalldatas,
5-
getDelegationHashOffchain,
5+
hashDelegation,
66
createCaveatBuilder,
77
} from '@metamask/smart-accounts-kit/utils';
88
import {
@@ -92,9 +92,7 @@ test('maincase: Bob redeems the delegation with a permissions context allowing p
9292
signature: '0x',
9393
};
9494

95-
const delegationHash = getDelegationHashOffchain(
96-
delegationRequiringNativeTokenPayment,
97-
);
95+
const delegationHash = hashDelegation(delegationRequiringNativeTokenPayment);
9896

9997
const args = concat([delegationHash, bobSmartAccount.address]);
10098

@@ -228,9 +226,7 @@ test('Bob attempts to redeem with invalid terms length', async () => {
228226
signature: '0x',
229227
};
230228

231-
const delegationHash = getDelegationHashOffchain(
232-
delegationRequiringNativeTokenPayment,
233-
);
229+
const delegationHash = hashDelegation(delegationRequiringNativeTokenPayment);
234230

235231
const args = concat([delegationHash, bobSmartAccount.address]);
236232

packages/delegator-e2e/test/caveats/nativeTokenTransferAmount.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
encodeExecutionCalldatas,
44
encodeDelegations,
55
createCaveatBuilder,
6-
getDelegationHashOffchain,
6+
hashDelegation,
77
} from '@metamask/smart-accounts-kit/utils';
88
import {
99
createExecution,
@@ -471,7 +471,7 @@ test('Utility: getSpentAmount should track spending correctly before and after t
471471
}),
472472
};
473473

474-
const delegationHash = getDelegationHashOffchain(signedDelegation);
474+
const delegationHash = hashDelegation(signedDelegation);
475475

476476
// Check initial spent amount
477477
const initialSpent =

packages/delegator-e2e/test/caveats/specificActionERC20TransferBatch.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
encodeExecutionCalldatas,
44
encodeDelegations,
55
createCaveatBuilder,
6-
getDelegationHashOffchain,
6+
hashDelegation,
77
} from '@metamask/smart-accounts-kit/utils';
88
import {
99
Implementation,
@@ -676,7 +676,7 @@ describe('SpecificActionERC20TransferBatchEnforcer Contract Read Methods', () =>
676676
signature: '0x',
677677
};
678678

679-
const delegationHash = getDelegationHashOffchain(delegation);
679+
const delegationHash = hashDelegation(delegation);
680680

681681
const isUsed =
682682
await SpecificActionERC20TransferBatchEnforcer.read.usedDelegations({
@@ -738,7 +738,7 @@ describe('SpecificActionERC20TransferBatchEnforcer Contract Read Methods', () =>
738738
},
739739
];
740740

741-
const delegationHash = getDelegationHashOffchain(delegation);
741+
const delegationHash = hashDelegation(delegation);
742742

743743
// Check that delegation is unused before execution
744744
let isUsed =
@@ -827,8 +827,8 @@ describe('SpecificActionERC20TransferBatchEnforcer Contract Read Methods', () =>
827827
signature: '0x',
828828
};
829829

830-
const delegationHash1 = getDelegationHashOffchain(delegation1);
831-
const delegationHash2 = getDelegationHashOffchain(delegation2);
830+
const delegationHash1 = hashDelegation(delegation1);
831+
const delegationHash2 = hashDelegation(delegation2);
832832

833833
// Both delegations should be unused initially
834834
const isUsed1 =

packages/delegator-e2e/test/delegationManagement.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
import {
1111
encodeDelegations,
1212
encodeExecutionCalldatas,
13-
getDelegationHashOffchain,
13+
hashDelegation,
1414
} from '@metamask/smart-accounts-kit/utils';
1515
import {
1616
DeleGatorCore,
@@ -93,7 +93,7 @@ test('delegation management lifecycle: create, disable, enable, and check status
9393
signature: await aliceSmartAccount.signDelegation({ delegation }),
9494
};
9595

96-
const delegationHash = getDelegationHashOffchain(delegation);
96+
const delegationHash = hashDelegation(delegation);
9797

9898
// Step 2: Verify Bob can use the delegation initially
9999
const countBefore = await aliceCounter.read.count();
@@ -370,7 +370,7 @@ test('disabling non-existent delegation should succeed silently', async () => {
370370
expectUserOperationToSucceed(receipt);
371371

372372
// Verify the delegation is now disabled
373-
const delegationHash = getDelegationHashOffchain(delegation);
373+
const delegationHash = hashDelegation(delegation);
374374
const isDisabled = await DelegationManager.read.disabledDelegations({
375375
client: publicClient,
376376
contractAddress: aliceSmartAccount.environment.DelegationManager,
@@ -404,8 +404,8 @@ test('can check delegation status using disabledDelegations', async () => {
404404
},
405405
});
406406

407-
const delegationHash1 = getDelegationHashOffchain(delegation1);
408-
const delegationHash2 = getDelegationHashOffchain(delegation2);
407+
const delegationHash1 = hashDelegation(delegation1);
408+
const delegationHash2 = hashDelegation(delegation2);
409409

410410
// Initially, both delegations should not be disabled
411411
let isDisabled1 = await DelegationManager.read.disabledDelegations({

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Address, Hex, Client } from 'viem';
22

3-
import { getDelegationHashOffchain } from '../delegation';
3+
import { hashDelegation } from '../delegation';
44
import * as ERC20PeriodTransferEnforcer from '../DelegationFramework/ERC20PeriodTransferEnforcer';
55
import * as ERC20StreamingEnforcer from '../DelegationFramework/ERC20StreamingEnforcer';
66
import * as MultiTokenPeriodEnforcer from '../DelegationFramework/MultiTokenPeriodEnforcer';
@@ -133,7 +133,7 @@ export async function getErc20PeriodTransferEnforcerAvailableAmount(
133133
environment,
134134
});
135135

136-
const delegationHash = getDelegationHashOffchain(params.delegation);
136+
const delegationHash = hashDelegation(params.delegation);
137137
const { terms } = findMatchingCaveat({
138138
delegation: params.delegation,
139139
enforcerAddress,
@@ -169,7 +169,7 @@ export async function getErc20StreamingEnforcerAvailableAmount(
169169
environment,
170170
});
171171

172-
const delegationHash = getDelegationHashOffchain(params.delegation);
172+
const delegationHash = hashDelegation(params.delegation);
173173
const { terms } = findMatchingCaveat({
174174
delegation: params.delegation,
175175
enforcerAddress,
@@ -205,7 +205,7 @@ export async function getMultiTokenPeriodEnforcerAvailableAmount(
205205
environment,
206206
});
207207

208-
const delegationHash = getDelegationHashOffchain(params.delegation);
208+
const delegationHash = hashDelegation(params.delegation);
209209
const { terms, args } = findMatchingCaveat({
210210
delegation: params.delegation,
211211
enforcerAddress,
@@ -242,7 +242,7 @@ export async function getNativeTokenPeriodTransferEnforcerAvailableAmount(
242242
environment,
243243
});
244244

245-
const delegationHash = getDelegationHashOffchain(params.delegation);
245+
const delegationHash = hashDelegation(params.delegation);
246246
const { terms } = findMatchingCaveat({
247247
delegation: params.delegation,
248248
enforcerAddress,
@@ -278,7 +278,7 @@ export async function getNativeTokenStreamingEnforcerAvailableAmount(
278278
environment,
279279
});
280280

281-
const delegationHash = getDelegationHashOffchain(params.delegation);
281+
const delegationHash = hashDelegation(params.delegation);
282282
const { terms } = findMatchingCaveat({
283283
delegation: params.delegation,
284284
enforcerAddress,

packages/smart-accounts-kit/src/delegation.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
decodeDelegations as decodeDelegationsCore,
44
encodeDelegation as encodeDelegationCore,
55
decodeDelegation as decodeDelegationCore,
6-
hashDelegation,
6+
hashDelegation as hashDelegationCore,
77
ANY_BENEFICIARY,
88
DELEGATION_TYPEHASH,
99
CAVEAT_TYPEHASH,
@@ -189,10 +189,10 @@ export const prepDelegationHashForPasskeySign = (delegationHash: Hex) => {
189189
* @param input - The delegation to get the hash for.
190190
* @returns The hash of the delegation parameters.
191191
*/
192-
export const getDelegationHashOffchain = (input: Delegation): Hex => {
192+
export const hashDelegation = (input: Delegation): Hex => {
193193
const delegationStruct = toDelegationStruct(input);
194194

195-
return hashDelegation(delegationStruct);
195+
return hashDelegationCore(delegationStruct);
196196
};
197197

198198
type BaseCreateDelegationOptions = {
@@ -231,7 +231,7 @@ export const resolveAuthority = (parentDelegation?: Delegation | Hex): Hex => {
231231
return parentDelegation;
232232
}
233233

234-
return getDelegationHashOffchain(parentDelegation);
234+
return hashDelegation(parentDelegation);
235235
};
236236

237237
/**

packages/smart-accounts-kit/src/experimental/delegationStorage.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { type Hex, toHex } from 'viem';
22

3-
import { getDelegationHashOffchain } from '../delegation';
3+
import { hashDelegation } from '../delegation';
44
import type { Delegation } from '../types';
55

66
type ErrorResponse = {
@@ -102,7 +102,7 @@ export class DelegationStorageClient {
102102
const leafDelegationHash =
103103
typeof leafDelegationOrDelegationHash === 'string'
104104
? leafDelegationOrDelegationHash
105-
: getDelegationHashOffchain(leafDelegationOrDelegationHash);
105+
: hashDelegation(leafDelegationOrDelegationHash);
106106

107107
const response = await this.#fetcher(
108108
`${this.#apiUrl}/delegation/chain/${leafDelegationHash}`,
@@ -171,7 +171,7 @@ export class DelegationStorageClient {
171171
throw new Error('Delegation must be signed to be stored');
172172
}
173173

174-
const delegationHash = getDelegationHashOffchain(delegation);
174+
const delegationHash = hashDelegation(delegation);
175175

176176
const body = JSON.stringify(
177177
{

0 commit comments

Comments
 (0)