Skip to content

Add @metamask/delegation-core package #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 26, 2025
Merged

Conversation

jeffsmale90
Copy link
Collaborator

@jeffsmale90 jeffsmale90 commented Jun 24, 2025

📝 Description

Creates a new package @metamask/delegation-core that provides low-level delegation functionality, with minimal dependencies.

🔄 What Changed?

  • Adds @metamask/delegation-core package with minimal dependencies (@metamask/utils and @metamask/abi-utils)
  • Supports terms encoding for limited set of caveats used in Readable Permissions
  • Supports Delegations encoding and decoding
  • Supports input / output of bytes as either Hex or Uint8Array
  • Consumes these functions in @metamask/delegation-utils

🚀 Why?

This adds a lower level utils library that can be used across the extension and snaps without including a dependency on Viem, or the full Delegation Toolkit. This is designed to do a lot of the lower level encoding required in the Delegation Toolkit, to avoid duplication of logic.

🧪 How to Test?

There is comprehensive unit test coverage for the functionality added to @metamask/delegation-core, and the impacted functionality in @metamask/delegation-toolkit.

⚠️ Breaking Changes

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

📋 Checklist

Check off completed items:

  • Code follows the project's coding standards
  • Self-review completed
  • Documentation updated (if needed)
  • Tests added/updated
  • Changelog updated (if needed)
  • All CI checks pass

Additional comments

I've kept each change in the PR clearly isolated, so it's probably best to review by changeset.

Copy link

socket-security bot commented Jun 24, 2025

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Added@​metamask/​abi-utils@​3.0.010010010085100

View full report

Copy link

socket-security bot commented Jun 24, 2025

All alerts resolved. Learn more about Socket for GitHub.

This PR previously contained dependency changes with security issues that have been resolved, removed, or ignored.

View full report

@jeffsmale90 jeffsmale90 force-pushed the feat/delegation-core branch 10 times, most recently from 74d6801 to 6d01af9 Compare June 25, 2025 07:23
@jeffsmale90 jeffsmale90 marked this pull request as ready for review June 25, 2025 07:28
@jeffsmale90 jeffsmale90 requested a review from a team as a code owner June 25, 2025 07:28
@jeffsmale90 jeffsmale90 force-pushed the feat/delegation-core branch 3 times, most recently from 0a9c099 to 86cbf28 Compare June 25, 2025 07:59
@jeffsmale90 jeffsmale90 force-pushed the feat/delegation-core branch from 86cbf28 to 60a476f Compare June 25, 2025 19:20
@jeffsmale90 jeffsmale90 requested a review from Copilot June 25, 2025 20:30
Copy link

@Copilot Copilot AI left a comment

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 adds a new package (@metamask/delegation-core) that provides low‐level delegation functionality to support encoding/decoding of caveat terms and delegations while minimizing external dependencies. Key changes include:

  • Introducing several caveat term builders (e.g. for value limits, timestamps, token streaming, and exact calldata) that replace manual encoding logic.
  • Adding comprehensive unit tests for the new functionality and integrating the new core functions into existing toolkit utilities.
  • Updating package configuration and build settings for the new package.

Reviewed Changes

Copilot reviewed 37 out of 38 changed files in this pull request and generated 1 comment.

File Description
packages/delegator-e2e/package.json Updated vitest dependency version for e2e testing.
packages/delegation-toolkit/** Updated caveat builders to delegate functionality to delegation-core.
packages/delegation-core/** New package implementation including caveat builders, encoding/decoding functions, types, and unit tests.

Comment on lines +1 to +8
export const toHexString = ({
value,
size,
}: {
value: bigint | number;
size: number;
}): string => {
return value.toString(16).padStart(size * 2, '0');
Copy link
Preview

Copilot AI Jun 25, 2025

Choose a reason for hiding this comment

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

[nitpick] Consider documenting that toHexString intentionally omits the '0x' prefix so that callers know to add it later – alternatively, including the prefix directly might simplify usage and improve consistency with other utilities.

Suggested change
export const toHexString = ({
value,
size,
}: {
value: bigint | number;
size: number;
}): string => {
return value.toString(16).padStart(size * 2, '0');
/**
* Converts a number or bigint to a hexadecimal string with a `0x` prefix.
* Pads the result to the specified size (in bytes) with leading zeros.
*
* @param value - The number or bigint to convert.
* @param size - The size (in bytes) to pad the result to.
* @returns The hexadecimal string with a `0x` prefix.
*/
export const toPrefixedHexString = ({
value,
size,
}: {
value: bigint | number;
size: number;
}): string => {
return '0x' + value.toString(16).padStart(size * 2, '0');

Copilot uses AI. Check for mistakes.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good call - I think adding a comment to the function makes sense. This exists because @metamask/utils only provides functionality to convert to prefixed Hex strings, while we need unprefixed.

Copy link
Member

@V00D00-child V00D00-child left a comment

Choose a reason for hiding this comment

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

Looks good! Just a suggestion regarding how we expose caveats:

The package is currently defined as:

Core utilities for creating and managing delegation terms and caveats in the MetaMask Delegation Framework.

However, the package only exposes some caveats, while the core caveatBuilder remains in the @metamask/delegation-toolkit package.

My suggestion is to move the caveatBuilder into this new @metamask/delegation-core package as well.

If the goal is to provide a single core package that exposes delegation and caveat functionality to consumers, having caveat creation split between @metamask/delegation-core and @metamask/delegation-toolkit could be confusing for users.

@jeffsmale90
Copy link
Collaborator Author

My suggestion is to move the caveatBuilder into this new @metamask/delegation-core package as well.

The intention is to implement all caveats eventually, but for now, only those that are needed for readable permssions.

I don't intend for external developers to have to import delegation-core directly. @metamask/delegation-toolkit provides the (slightly) higher-level abstraction, and depends on delegation-core. This is intended to be used internally in snaps and extension. Developers who want the (slightly) higher level abstractions, and more friendly API, can use @metamask/delegation-toolkit.

Implementing caveatbuilder abstraction within delegation-core brings a lot with it, including DelegatorEnvironment etc. I want to keep delegation-core as minimal and low level as possible.

@jeffsmale90 jeffsmale90 force-pushed the feat/delegation-core branch from ecf867d to f4f5906 Compare June 26, 2025 18:59
@V00D00-child
Copy link
Member

The intention is to implement all caveats eventually, but for now, only those that are needed for readable permssions.

Got it, I'll go ahead and approve this so we can push this forward.

@jeffsmale90 jeffsmale90 merged commit 5ab1045 into main Jun 26, 2025
14 checks passed
@jeffsmale90 jeffsmale90 deleted the feat/delegation-core branch June 26, 2025 20:36
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.

2 participants