|
| 1 | +# @metamask/delegation-core |
| 2 | + |
| 3 | +Core utilities for creating and managing delegation terms and caveats in the MetaMask Delegation Framework. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```bash |
| 8 | +npm install @metamask/delegation-core |
| 9 | +``` |
| 10 | + |
| 11 | +## Overview |
| 12 | + |
| 13 | +This package provides utilities for: |
| 14 | +- Creating caveat terms for various delegation constraints |
| 15 | +- Encoding and decoding delegations |
| 16 | +- Type definitions for delegation structures |
| 17 | + |
| 18 | +## API Reference |
| 19 | + |
| 20 | +### Caveat Terms Builders |
| 21 | + |
| 22 | +Caveat terms builders create encoded parameters for different types of delegation constraints. |
| 23 | + |
| 24 | +#### `createValueLteTerms(terms, options?)` |
| 25 | + |
| 26 | +Creates terms for a ValueLte caveat that limits the maximum value of native tokens that can be spent. |
| 27 | + |
| 28 | +**Parameters:** |
| 29 | +- `terms: ValueLteTerms` |
| 30 | + - `maxValue: bigint` - The maximum value allowed for the transaction |
| 31 | +- `options?: EncodingOptions` - Optional encoding options (`'hex'` | `'bytes'`) |
| 32 | + |
| 33 | +**Returns:** `Hex | Uint8Array` - 32-byte encoded terms |
| 34 | + |
| 35 | +**Example:** |
| 36 | +```typescript |
| 37 | +import { createValueLteTerms } from '@metamask/delegation-core'; |
| 38 | + |
| 39 | +// Limit to 1 ETH maximum |
| 40 | +const terms = createValueLteTerms({ |
| 41 | + maxValue: 1000000000000000000n // 1 ETH in wei |
| 42 | +}); |
| 43 | +// Returns: '0x0000000000000000000000000000000000000000000000000de0b6b3a7640000' |
| 44 | + |
| 45 | +// Get as Uint8Array |
| 46 | +const bytesTerms = createValueLteTerms({ |
| 47 | + maxValue: 1000000000000000000n |
| 48 | +}, { out: 'bytes' }); |
| 49 | +``` |
| 50 | + |
| 51 | +#### `createTimestampTerms(terms, options?)` |
| 52 | + |
| 53 | +Creates terms for a Timestamp caveat that enforces time-based constraints on delegation usage. |
| 54 | + |
| 55 | +**Parameters:** |
| 56 | +- `terms: TimestampTerms` |
| 57 | + - `timestampAfterThreshold: number` - Timestamp (seconds) after which delegation can be used |
| 58 | + - `timestampBeforeThreshold: number` - Timestamp (seconds) before which delegation can be used |
| 59 | +- `options?: EncodingOptions` - Optional encoding options |
| 60 | + |
| 61 | +**Returns:** `Hex | Uint8Array` - 32-byte encoded terms (16 bytes per timestamp) |
| 62 | + |
| 63 | +**Example:** |
| 64 | +```typescript |
| 65 | +import { createTimestampTerms } from '@metamask/delegation-core'; |
| 66 | + |
| 67 | +// Valid between Jan 1, 2022 and Jan 1, 2023 |
| 68 | +const terms = createTimestampTerms({ |
| 69 | + timestampAfterThreshold: 1640995200, // 2022-01-01 00:00:00 UTC |
| 70 | + timestampBeforeThreshold: 1672531200 // 2023-01-01 00:00:00 UTC |
| 71 | +}); |
| 72 | + |
| 73 | +// Only valid after a certain time (no end time) |
| 74 | +const openEndedTerms = createTimestampTerms({ |
| 75 | + timestampAfterThreshold: 1640995200, |
| 76 | + timestampBeforeThreshold: 0 |
| 77 | +}); |
| 78 | +``` |
| 79 | + |
| 80 | +#### `createExactCalldataTerms(terms, options?)` |
| 81 | + |
| 82 | +Creates terms for an ExactCalldata caveat that ensures execution calldata matches exactly. |
| 83 | + |
| 84 | +**Parameters:** |
| 85 | +- `terms: ExactCalldataTerms` |
| 86 | + - `callData: BytesLike` - The expected calldata (hex string or Uint8Array) |
| 87 | +- `options?: EncodingOptions` - Optional encoding options |
| 88 | + |
| 89 | +**Returns:** `Hex | Uint8Array` - The calldata itself (variable length) |
| 90 | + |
| 91 | +**Example:** |
| 92 | +```typescript |
| 93 | +import { createExactCalldataTerms } from '@metamask/delegation-core'; |
| 94 | + |
| 95 | +// Exact calldata for a specific function call |
| 96 | +const terms = createExactCalldataTerms({ |
| 97 | + callData: '0xa9059cbb000000000000000000000000742d35cc6634c0532925a3b8d40ec49b0e8baa5e0000000000000000000000000000000000000000000000000de0b6b3a7640000' |
| 98 | +}); |
| 99 | + |
| 100 | +// From Uint8Array |
| 101 | +const terms2 = createExactCalldataTerms({ |
| 102 | + callData: new Uint8Array([0xa9, 0x05, 0x9c, 0xbb, /* ... */]) |
| 103 | +}); |
| 104 | +``` |
| 105 | + |
| 106 | +#### `createNativeTokenPeriodTransferTerms(terms, options?)` |
| 107 | + |
| 108 | +Creates terms for periodic native token transfer limits with time-based resets. |
| 109 | + |
| 110 | +**Parameters:** |
| 111 | +- `terms: NativeTokenPeriodTransferTerms` |
| 112 | + - `periodAmount: bigint` - Maximum amount transferable per period (wei) |
| 113 | + - `periodDuration: number` - Duration of each period (seconds) |
| 114 | + - `startDate: number` - Unix timestamp when first period begins |
| 115 | +- `options?: EncodingOptions` - Optional encoding options |
| 116 | + |
| 117 | +**Returns:** `Hex | Uint8Array` - 96-byte encoded terms (32 bytes per parameter) |
| 118 | + |
| 119 | +**Example:** |
| 120 | +```typescript |
| 121 | +import { createNativeTokenPeriodTransferTerms } from '@metamask/delegation-core'; |
| 122 | + |
| 123 | +// Allow 1 ETH per day starting from a specific date |
| 124 | +const terms = createNativeTokenPeriodTransferTerms({ |
| 125 | + periodAmount: 1000000000000000000n, // 1 ETH in wei |
| 126 | + periodDuration: 86400, // 24 hours in seconds |
| 127 | + startDate: 1640995200 // 2022-01-01 00:00:00 UTC |
| 128 | +}); |
| 129 | +``` |
| 130 | + |
| 131 | +#### `createNativeTokenStreamingTerms(terms, options?)` |
| 132 | + |
| 133 | +Creates terms for linear streaming allowance of native tokens. |
| 134 | + |
| 135 | +**Parameters:** |
| 136 | +- `terms: NativeTokenStreamingTerms` |
| 137 | + - `initialAmount: bigint` - Amount available immediately (wei) |
| 138 | + - `maxAmount: bigint` - Maximum total transferable amount (wei) |
| 139 | + - `amountPerSecond: bigint` - Rate of allowance increase per second (wei) |
| 140 | + - `startTime: number` - Unix timestamp when streaming begins |
| 141 | +- `options?: EncodingOptions` - Optional encoding options |
| 142 | + |
| 143 | +**Returns:** `Hex | Uint8Array` - 128-byte encoded terms (32 bytes per parameter) |
| 144 | + |
| 145 | +**Example:** |
| 146 | +```typescript |
| 147 | +import { createNativeTokenStreamingTerms } from '@metamask/delegation-core'; |
| 148 | + |
| 149 | +// Stream 0.5 ETH per second, starting with 1 ETH, max 10 ETH |
| 150 | +const terms = createNativeTokenStreamingTerms({ |
| 151 | + initialAmount: 1000000000000000000n, // 1 ETH available immediately |
| 152 | + maxAmount: 10000000000000000000n, // 10 ETH maximum |
| 153 | + amountPerSecond: 500000000000000000n, // 0.5 ETH per second |
| 154 | + startTime: 1640995200 // Start streaming at this time |
| 155 | +}); |
| 156 | +``` |
| 157 | + |
| 158 | +#### `createERC20StreamingTerms(terms, options?)` |
| 159 | + |
| 160 | +Creates terms for linear streaming allowance of ERC20 tokens. |
| 161 | + |
| 162 | +**Parameters:** |
| 163 | +- `terms: ERC20StreamingTerms` |
| 164 | + - `tokenAddress: string` - ERC20 token contract address |
| 165 | + - `initialAmount: bigint` - Amount available immediately |
| 166 | + - `maxAmount: bigint` - Maximum total transferable amount |
| 167 | + - `amountPerSecond: bigint` - Rate of allowance increase per second |
| 168 | + - `startTime: number` - Unix timestamp when streaming begins |
| 169 | +- `options?: EncodingOptions` - Optional encoding options |
| 170 | + |
| 171 | +**Returns:** `Hex | Uint8Array` - 148-byte encoded terms (20 bytes + 32 bytes × 4 parameters) |
| 172 | + |
| 173 | +**Example:** |
| 174 | +```typescript |
| 175 | +import { createERC20StreamingTerms } from '@metamask/delegation-core'; |
| 176 | + |
| 177 | +// Stream USDC tokens |
| 178 | +const terms = createERC20StreamingTerms({ |
| 179 | + tokenAddress: '0xA0b86a33E6441E74C65c6BF2A6d73B895B9b34A2', |
| 180 | + initialAmount: 1000000n, // 1 USDC (6 decimals) |
| 181 | + maxAmount: 10000000n, // 10 USDC maximum |
| 182 | + amountPerSecond: 100000n, // 0.1 USDC per second |
| 183 | + startTime: 1640995200 |
| 184 | +}); |
| 185 | +``` |
| 186 | + |
| 187 | +### Delegation Utilities |
| 188 | + |
| 189 | +#### `encodeDelegations(delegations)` |
| 190 | + |
| 191 | +Encodes an array of delegations into a format suitable for on-chain submission. |
| 192 | + |
| 193 | +**Parameters:** |
| 194 | +- `delegations: Delegation[]` - Array of delegation objects |
| 195 | + |
| 196 | +**Returns:** Encoded delegation data |
| 197 | + |
| 198 | +**Example:** |
| 199 | +```typescript |
| 200 | +import { encodeDelegations } from '@metamask/delegation-core'; |
| 201 | + |
| 202 | +const delegations = [ |
| 203 | + { |
| 204 | + delegate: '0x742d35Cc6634C0532925a3b8d40EC49b0e8BaA5e', |
| 205 | + delegator: '0x...', |
| 206 | + authority: '0x...', |
| 207 | + caveats: [], |
| 208 | + salt: 0n, |
| 209 | + signature: '0x...' |
| 210 | + } |
| 211 | +]; |
| 212 | + |
| 213 | +const encoded = encodeDelegations(delegations); |
| 214 | +``` |
| 215 | + |
| 216 | +#### `decodeDelegations(data)` |
| 217 | + |
| 218 | +Decodes encoded delegation data back into delegation objects. |
| 219 | + |
| 220 | +**Parameters:** |
| 221 | +- `data` - Encoded delegation data |
| 222 | + |
| 223 | +**Returns:** `Delegation[]` - Array of decoded delegation objects |
| 224 | + |
| 225 | +**Example:** |
| 226 | +```typescript |
| 227 | +import { decodeDelegations } from '@metamask/delegation-core'; |
| 228 | + |
| 229 | +const delegations = decodeDelegations(encodedData); |
| 230 | +``` |
| 231 | + |
| 232 | +#### `ROOT_AUTHORITY` |
| 233 | + |
| 234 | +A constant representing the root authority in the delegation hierarchy. |
| 235 | + |
| 236 | +**Example:** |
| 237 | +```typescript |
| 238 | +import { ROOT_AUTHORITY } from '@metamask/delegation-core'; |
| 239 | + |
| 240 | +console.log(ROOT_AUTHORITY); // Root authority identifier |
| 241 | +``` |
| 242 | + |
| 243 | +## Type Definitions |
| 244 | + |
| 245 | +### Core Types |
| 246 | + |
| 247 | +```typescript |
| 248 | +export type Hex = `0x${string}`; |
| 249 | + |
| 250 | +export type Delegation = { |
| 251 | + delegate: string; |
| 252 | + delegator: string; |
| 253 | + authority: string; |
| 254 | + caveats: Caveat[]; |
| 255 | + salt: bigint; |
| 256 | + signature: string; |
| 257 | +}; |
| 258 | + |
| 259 | +export type Caveat = { |
| 260 | + enforcer: string; |
| 261 | + terms: string; |
| 262 | +}; |
| 263 | +``` |
| 264 | + |
| 265 | +### Caveat Terms Types |
| 266 | + |
| 267 | +```typescript |
| 268 | +export type ValueLteTerms = { |
| 269 | + maxValue: bigint; |
| 270 | +}; |
| 271 | + |
| 272 | +export type TimestampTerms = { |
| 273 | + timestampAfterThreshold: number; |
| 274 | + timestampBeforeThreshold: number; |
| 275 | +}; |
| 276 | + |
| 277 | +export type ExactCalldataTerms = { |
| 278 | + callData: BytesLike; |
| 279 | +}; |
| 280 | + |
| 281 | +export type NativeTokenPeriodTransferTerms = { |
| 282 | + periodAmount: bigint; |
| 283 | + periodDuration: number; |
| 284 | + startDate: number; |
| 285 | +}; |
| 286 | + |
| 287 | +export type NativeTokenStreamingTerms = { |
| 288 | + initialAmount: bigint; |
| 289 | + maxAmount: bigint; |
| 290 | + amountPerSecond: bigint; |
| 291 | + startTime: number; |
| 292 | +}; |
| 293 | + |
| 294 | +export type ERC20StreamingTerms = { |
| 295 | + tokenAddress: BytesLike; |
| 296 | + initialAmount: bigint; |
| 297 | + maxAmount: bigint; |
| 298 | + amountPerSecond: bigint; |
| 299 | + startTime: number; |
| 300 | +}; |
| 301 | +``` |
| 302 | + |
| 303 | +## Error Handling |
| 304 | + |
| 305 | +All caveat builders include validation and will throw descriptive errors: |
| 306 | + |
| 307 | +```typescript |
| 308 | +try { |
| 309 | + const terms = createValueLteTerms({ |
| 310 | + maxValue: -1n // Invalid: negative value |
| 311 | + }); |
| 312 | +} catch (error) { |
| 313 | + console.error(error.message); // "Invalid maxValue: must be greater than or equal to zero" |
| 314 | +} |
| 315 | +``` |
| 316 | + |
| 317 | +## Related Packages |
| 318 | + |
| 319 | +- `@metamask/delegation-toolkit` - Higher-level utilities for delegation management |
| 320 | +- `@metamask/delegation-abis` - Contract ABIs for the delegation framework |
| 321 | +- `@metamask/delegation-deployments` - Contract deployment addresses |
| 322 | + |
| 323 | +## Links |
| 324 | + |
| 325 | +- [MetaMask Delegation Framework](https://github.com/MetaMask/delegation-framework) |
| 326 | +- [EIP-7715: Delegated Authorization](https://eips.ethereum.org/EIPS/eip-7715) |
0 commit comments