Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 | 2x 2x 23x 1x 2x 1x 1x 1x 7x 7x 7x 1x 28x 28x 31x 10x 2x 2x 16x 16x 16x 16x 8x 8x 8x 4x 7x 4x 4x 14x 7x 2x 2x 35x 19x 16x 1x 15x 15x 15x 15x 15x 20x 20x | /**
* SDK adapter — single integration point for `@metamask/smart-accounts-kit`.
*
* All other files import from this module, never directly from the SDK.
* This isolates the external dependency and maps between our types and SDK types.
*
* @module lib/sdk
*/
import {
createExecution as sdkCreateExecution,
getSmartAccountsEnvironment,
Implementation,
ExecutionMode,
contracts,
} from '@metamask/smart-accounts-kit';
import type {
SmartAccountsEnvironment,
Delegation as SdkDelegation,
} from '@metamask/smart-accounts-kit';
import {
encodeDelegations as sdkEncodeDelegations,
getCounterfactualAccountData,
} from '@metamask/smart-accounts-kit/utils';
import { toPackedUserOperation } from 'viem/account-abstraction';
import type {
Address,
CaveatType,
Delegation,
Eip712TypedData,
Execution,
Hex,
} from '../types.ts';
/**
* EIP-712 type definitions for PackedUserOperation signing.
* Used by HybridDeleGator's validateUserOp.
*/
const SIGNABLE_USER_OP_TYPED_DATA = {
PackedUserOperation: [
{ name: 'sender', type: 'address' },
{ name: 'nonce', type: 'uint256' },
{ name: 'initCode', type: 'bytes' },
{ name: 'callData', type: 'bytes' },
{ name: 'accountGasLimits', type: 'bytes32' },
{ name: 'preVerificationGas', type: 'uint256' },
{ name: 'gasFees', type: 'bytes32' },
{ name: 'paymasterAndData', type: 'bytes' },
{ name: 'entryPoint', type: 'address' },
],
} as const;
const harden = globalThis.harden ?? (<T>(value: T): T => value);
// ---------------------------------------------------------------------------
// Re-exports for external callers
// ---------------------------------------------------------------------------
export { Implementation, ExecutionMode };
export type { SmartAccountsEnvironment, SdkDelegation };
// ---------------------------------------------------------------------------
// Environment resolution
// ---------------------------------------------------------------------------
/**
* Resolve the SDK environment for a chain ID.
*
* @param chainId - The chain ID.
* @returns The SDK environment.
*/
export function resolveEnvironment(chainId: number): SmartAccountsEnvironment {
return getSmartAccountsEnvironment(chainId);
}
/**
* Get the DelegationManager address from the SDK environment.
*
* @param chainId - The chain ID.
* @returns The DelegationManager address.
*/
export function getDelegationManagerAddress(chainId: number): Address {
return resolveEnvironment(chainId).DelegationManager;
}
/**
* Map SDK caveat enforcer names to our CaveatType keys.
*/
const SDK_ENFORCER_KEYS: Record<CaveatType, string> = {
allowedTargets: 'AllowedTargetsEnforcer',
allowedMethods: 'AllowedMethodsEnforcer',
valueLte: 'ValueLteEnforcer',
nativeTokenTransferAmount: 'NativeTokenTransferAmountEnforcer',
erc20TransferAmount: 'ERC20TransferAmountEnforcer',
limitedCalls: 'LimitedCallsEnforcer',
timestamp: 'TimestampEnforcer',
};
/**
* Get enforcer addresses from the SDK environment.
*
* @param chainId - The chain ID.
* @returns A mapping of CaveatType to enforcer address.
*/
export function getEnforcerAddresses(
chainId: number,
): Record<CaveatType, Address> {
const env = resolveEnvironment(chainId);
const enforcers = {} as Record<CaveatType, Address>;
for (const [caveatType, sdkKey] of Object.entries(SDK_ENFORCER_KEYS)) {
const addr = env.caveatEnforcers[sdkKey];
Eif (addr) {
enforcers[caveatType as CaveatType] = addr;
}
}
return harden(enforcers);
}
// ---------------------------------------------------------------------------
// Internal helpers
// ---------------------------------------------------------------------------
/**
* Wrap inner callData in a `DeleGatorCore.execute` call targeting the
* DelegationManager for the given chain.
*
* @param innerCallData - The encoded call to route through the DelegationManager.
* @param chainId - The chain ID (for DelegationManager address resolution).
* @returns The wrapped callData.
*/
function wrapInDelegationManagerExecute(
innerCallData: Hex,
chainId: number,
): Hex {
const env = getSmartAccountsEnvironment(chainId);
return contracts.DeleGatorCore.encode.execute({
execution: sdkCreateExecution({
target: env.DelegationManager,
value: 0n,
callData: innerCallData,
}),
});
}
// ---------------------------------------------------------------------------
// Type mapping: our types ↔ SDK types
// ---------------------------------------------------------------------------
/**
* Convert our Delegation type to the SDK's Delegation format.
*
* @param delegation - Our delegation.
* @returns The SDK delegation.
*/
export function toSdkDelegation(delegation: Delegation): SdkDelegation {
return {
delegate: delegation.delegate,
delegator: delegation.delegator,
authority: delegation.authority,
caveats: delegation.caveats.map((caveat) => ({
enforcer: caveat.enforcer,
terms: caveat.terms,
args: '0x' as Hex,
})),
salt: delegation.salt,
signature: delegation.signature ?? ('0x' as Hex),
};
}
// ---------------------------------------------------------------------------
// Delegation operations
// ---------------------------------------------------------------------------
/**
* Encode an array of delegations into ABI-encoded bytes.
*
* @param delegations - Our delegation objects.
* @returns ABI-encoded hex string.
*/
export function encodeSdkDelegations(delegations: Delegation[]): Hex {
const sdkDelegations = delegations.map(toSdkDelegation);
return sdkEncodeDelegations(sdkDelegations);
}
/**
* Build the callData for `DelegationManager.redeemDelegations`.
*
* @param options - Options.
* @param options.delegations - The delegation chain (leaf to root).
* @param options.execution - The execution to perform.
* @param options.chainId - The chain ID (for DelegationManager address resolution).
* @returns The encoded callData.
*/
export function buildSdkRedeemCallData(options: {
delegations: Delegation[];
execution: Execution;
chainId: number;
}): Hex {
const sdkDelegations = options.delegations.map(toSdkDelegation);
const sdkExecution = sdkCreateExecution({
target: options.execution.target,
value: BigInt(options.execution.value),
callData: options.execution.callData,
});
// Build the redeemDelegations callData for the DelegationManager
const redeemCallData = contracts.DelegationManager.encode.redeemDelegations({
delegations: [sdkDelegations],
modes: [ExecutionMode.SingleDefault],
executions: [[sdkExecution]],
});
// Wrap in a DeleGatorCore.execute call so the smart account routes
// the call to the DelegationManager. The UserOp callData must target
// the smart account's own execute function, not the DelegationManager
// directly.
return wrapInDelegationManagerExecute(redeemCallData, options.chainId);
}
/**
* Build the callData for `DelegationManager.disableDelegation`, wrapped in a
* `DeleGatorCore.execute` call so it can be submitted as a UserOp from the
* delegator's smart account.
*
* @param options - Options.
* @param options.delegation - The delegation to disable on-chain.
* @param options.chainId - The chain ID (for DelegationManager address resolution).
* @returns The encoded callData.
*/
export function buildSdkDisableCallData(options: {
delegation: Delegation;
chainId: number;
}): Hex {
const sdkDelegation = toSdkDelegation(options.delegation);
const disableCallData = contracts.DelegationManager.encode.disableDelegation({
delegation: sdkDelegation,
});
return wrapInDelegationManagerExecute(disableCallData, options.chainId);
}
/**
* Build batch callData for `DelegationManager.redeemDelegations` with
* multiple executions in a single UserOp, wrapped in
* `DeleGatorCore.execute`.
*
* @param options - Options.
* @param options.delegations - The delegation chain (leaf to root).
* @param options.executions - The executions to perform as a batch.
* @param options.chainId - The chain ID (for DelegationManager address resolution).
* @returns The encoded callData.
*/
export function buildSdkBatchRedeemCallData(options: {
delegations: Delegation[];
executions: Execution[];
chainId: number;
}): Hex {
const sdkDelegations = options.delegations.map(toSdkDelegation);
const sdkExecutions = options.executions.map((exec) =>
sdkCreateExecution({
target: exec.target,
value: BigInt(exec.value),
callData: exec.callData,
}),
);
const redeemCallData = contracts.DelegationManager.encode.redeemDelegations({
delegations: [sdkDelegations],
modes: [ExecutionMode.BatchDefault],
executions: [sdkExecutions],
});
return wrapInDelegationManagerExecute(redeemCallData, options.chainId);
}
/**
* Build batch callData for `DeleGatorCore.executeWithMode` to execute
* multiple operations in a single UserOp without delegation redemption.
*
* @param options - Options.
* @param options.executions - The executions to batch.
* @returns The encoded callData.
*/
export function buildBatchExecuteCallData(options: {
executions: Execution[];
}): Hex {
const sdkExecutions = options.executions.map((exec) =>
sdkCreateExecution({
target: exec.target,
value: BigInt(exec.value),
callData: exec.callData,
}),
);
return contracts.DeleGatorCore.encode.executeWithMode({
mode: ExecutionMode.BatchDefault,
executions: sdkExecutions,
});
}
// ---------------------------------------------------------------------------
// Execution
// ---------------------------------------------------------------------------
/**
* Create an SDK execution struct.
*
* @param options - Execution options.
* @param options.target - The target contract address.
* @param options.value - The native token amount in wei.
* @param options.callData - The encoded function data.
* @returns The SDK execution struct.
*/
export function createSdkExecution(options: {
target: Address;
value?: bigint;
callData?: Hex;
}): { target: Address; value: bigint; callData: Hex } {
const execution = sdkCreateExecution({
target: options.target,
value: options.value ?? 0n,
callData: options.callData ?? ('0x' as Hex),
});
return execution as { target: Address; value: bigint; callData: Hex };
}
// ---------------------------------------------------------------------------
// Smart account
// ---------------------------------------------------------------------------
/**
* Compute the counterfactual address for a Hybrid smart account.
*
* This is a pure CREATE2 computation — no network calls needed.
*
* @param options - Options for address derivation.
* @param options.owner - The EOA owner address.
* @param options.deploySalt - The deployment salt.
* @param options.chainId - The chain ID (for environment resolution).
* @returns The counterfactual address and factory data.
*/
export async function computeSmartAccountAddress(options: {
owner: Address;
deploySalt: Hex;
chainId: number;
}): Promise<{ address: Address; factoryData: Hex }> {
const env = resolveEnvironment(options.chainId);
const deployParams: [Hex, string[], bigint[], bigint[]] = [
options.owner,
[],
[],
[],
];
const result = await getCounterfactualAccountData({
factory: env.SimpleFactory,
implementations: env.implementations,
implementation: Implementation.Hybrid,
deployParams,
deploySalt: options.deploySalt,
});
return {
address: result.address,
factoryData: result.factoryData,
};
}
// ---------------------------------------------------------------------------
// EIP-7702 status check
// ---------------------------------------------------------------------------
/**
* Check whether on-chain code indicates an active EIP-7702 delegation
* to the expected Stateless7702 DeleGator implementation.
*
* EIP-7702 designator format: `0xef0100` + 20-byte address.
*
* @param code - The result of `eth_getCode` for the EOA.
* @param chainId - The chain ID (for environment resolution).
* @returns `true` if the code points at the expected 7702 implementation.
*/
export function isEip7702Delegated(code: string, chainId: number): boolean {
// 0xef0100 (6 chars) + 40 hex chars address = 46 chars + "0x" prefix = 48
if (!code || code === '0x' || code.length !== 48) {
return false;
}
if (!code.toLowerCase().startsWith('0xef0100')) {
return false;
}
const addr = `0x${code.slice(8)}`;
const env = resolveEnvironment(chainId);
const expectedImpl = (
env.implementations as Record<string, string | undefined>
).EIP7702StatelessDeleGatorImpl;
Iif (!expectedImpl) {
return false;
}
return addr.toLowerCase() === expectedImpl.toLowerCase();
}
// ---------------------------------------------------------------------------
// UserOp signing (EIP-712 typed data for DeleGator smart accounts)
// ---------------------------------------------------------------------------
/**
* Prepare EIP-712 typed data for signing a UserOperation.
*
* DeleGator smart accounts (both HybridDeleGator and EIP7702StatelessDeleGator)
* validate UserOp signatures as EIP-712 typed data. This function produces the
* typed data payload that the EOA owner must sign. The domain name defaults to
* 'HybridDeleGator' but can be overridden via `smartAccountName`.
*
* @param options - Options.
* @param options.userOp - The UserOperation to sign.
* @param options.entryPoint - The EntryPoint address.
* @param options.chainId - The chain ID.
* @param options.smartAccountAddress - The smart account address (verifyingContract).
* @param options.smartAccountName - The smart account name for the EIP-712 domain.
* @returns The EIP-712 typed data for signing.
*/
export function prepareUserOpTypedData(options: {
userOp: Record<string, unknown>;
entryPoint: Address;
chainId: number;
smartAccountAddress: Address;
smartAccountName?: string;
}): Eip712TypedData {
const packed = toPackedUserOperation({
...options.userOp,
signature: '0x',
} as never);
return {
domain: {
chainId: options.chainId,
name: options.smartAccountName ?? 'HybridDeleGator',
version: '1',
verifyingContract: options.smartAccountAddress,
},
types: SIGNABLE_USER_OP_TYPED_DATA as unknown as Record<
string,
{ name: string; type: string }[]
>,
primaryType: 'PackedUserOperation',
message: {
...packed,
entryPoint: options.entryPoint,
},
};
}
|