Skip to content

Add hashDelegation function to @metamask/delegation-core #13

Merged
jeffsmale90 merged 5 commits into
mainfrom
feat/get-delegation-hash-in-core
Jul 1, 2025
Merged

Add hashDelegation function to @metamask/delegation-core #13
jeffsmale90 merged 5 commits into
mainfrom
feat/get-delegation-hash-in-core

Conversation

@jeffsmale90

@jeffsmale90 jeffsmale90 commented Jun 26, 2025

Copy link
Copy Markdown
Collaborator

📝 Description

Adds a function hashDelegation to @metamask/delegation-core, along with hash encoding constants. Consumes these within @metamask/delegation-toolkit.

🔄 What Changed?

  • Adds hashDelegation and related tests to @metamask/delegation-core
  • Adds DELEGATION_TYPEHASH, CAVEAT_TYPEHASH, and ANY_BENEFICIARY (that last one is only loosely related to the change) constants to @metamask/delegation-core
  • Updates getDelegationHashOffchain to use delegation-core implementation
  • @metamask/delegation-toolkit re-exports ANY_BENEFICIARY, DELEGATION_TYPEHASH, CAVEAT_TYPEHASH, and ROOT_AUTHORITY from @metamask/delegation-core rather than re-defining them. Removes ANY_DELEGATE which is a duplicate of ANY_BENEFICIARY (and was exported from the API as ANY_BENEFICIARY).

🚀 Why?

Delegation hashing is required in snaps and extension, so moving this implementation into core allows these low level consumers access to this functionality, without adding @metamask/delegation-toolkit as a dependency.

🧪 How to Test?

New unit tests are added to @metamask/delegation-core covering the new functionality, in addition to the existing unit tests in @metamask/delegation-toolkit and integration tests in delegator-e2e.

⚠️ Breaking Changes

List any breaking changes:

  • No breaking changes
  • Breaking changes (describe below):

@jeffsmale90 jeffsmale90 requested a review from a team as a code owner June 26, 2025 23:01
@jeffsmale90 jeffsmale90 force-pushed the feat/get-delegation-hash-in-core branch from 81b4fee to edda70a Compare June 26, 2025 23:15
@jeffsmale90 jeffsmale90 force-pushed the feat/get-delegation-hash-in-core branch from edda70a to 905f873 Compare June 26, 2025 23:24
@jeffsmale90 jeffsmale90 requested a review from Copilot June 26, 2025 23:24

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors and consolidates delegation hashing logic by introducing a new getDelegationHash function in delegation-core. It updates related tests and re-exports delegation constants in delegation-toolkit while renaming types for clarity.

  • Updated import and export paths in delegation-toolkit and delegation-core.
  • Introduced getDelegationHash and related helper functions in delegation-core.
  • Renamed types (Delegation and Caveat → DelegationStruct and CaveatStruct) and removed duplicate constant definitions.

Reviewed Changes

Copilot reviewed 12 out of 13 changed files in this pull request and generated no comments.

Show a summary per file
File Description
packages/delegation-toolkit/test/experimental/delegationStorageTestData.ts Updated import style for delegation functions and constants.
packages/delegation-toolkit/test/delegation.test.ts Adjusted test imports and usage to match new delegation-core exports and type renamings.
packages/delegation-toolkit/src/index.ts Re-ordered exports to remove duplicates and re-export delegation-core constants.
packages/delegation-toolkit/src/delegation.ts Replaced local getDelegationHashOffchain with delegation-core’s getDelegationHash.
packages/delegation-toolkit/src/constants.ts Removed duplicate constant definitions (ANY_DELEGATE) in favor of ANY_BENEFICIARY.
packages/delegation-toolkit/src/DelegationFramework/*/constants.ts Updated exports to reference delegation-core instead of legacy constants.
packages/delegation-core/test/delegation.test.ts Added extensive tests for getDelegationHash and updated type usage.
packages/delegation-core/src/types.ts Renamed Delegation and Caveat types to DelegationStruct and CaveatStruct.
packages/delegation-core/src/index.ts Updated re-exports to reflect new type names and added getDelegationHash export.
packages/delegation-core/src/delegation.ts Added getDelegationHash with improved encoding and hashing logic using @noble/hashes.
packages/delegation-core/package.json Added dependency on @noble/hashes for keccak256 implementation.
Comments suppressed due to low confidence (3)

packages/delegation-core/src/types.ts:12

  • [nitpick] Consider updating the type documentation to reflect the new naming convention from 'Caveat' to 'CaveatStruct' to improve clarity for future developers.
export type CaveatStruct<TBytes extends BytesLike = BytesLike> = {

packages/delegation-core/src/types.ts:28

  • [nitpick] Consider updating the documentation to clarify the renaming from 'Delegation' to 'DelegationStruct', ensuring consistency in type usage across the codebase.
export type DelegationStruct<TBytes extends BytesLike = BytesLike> = {

packages/delegation-core/src/delegation.ts:237

  • [nitpick] Consider adding a comment clarifying that the 'args' field is intentionally excluded from the caveat hash computation, as supported by the associated test cases.
function getCaveatsArrayHash(caveats: CaveatStruct[]): Uint8Array {

@jeffsmale90 jeffsmale90 marked this pull request as draft June 27, 2025 00:01
… validated against current version of @metamask/delegation-toolkit
@jeffsmale90 jeffsmale90 marked this pull request as ready for review June 27, 2025 04:32
@jeffsmale90 jeffsmale90 changed the title Add getDelegationHash function to @metamask/delegation-core Add hashDelegation function to @metamask/delegation-core Jun 30, 2025
Comment thread packages/delegation-core/src/delegation.ts
Comment thread packages/delegation-core/src/delegation.ts Outdated
hanzel98
hanzel98 previously approved these changes Jun 30, 2025
- Updated description of ANY_BENEFICIARY to be more descriptive
- Fixed incorrect definition of DELEGATION_TYPEHASH, which excluded the caveat suffix

Co-authored-by: Hanzel Anchia Mena <33629234+hanzel98@users.noreply.github.com>
@jeffsmale90 jeffsmale90 merged commit f71db8f into main Jul 1, 2025
15 checks passed
@jeffsmale90 jeffsmale90 deleted the feat/get-delegation-hash-in-core branch July 1, 2025 01:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants