All files / evm-wallet-experiment/src/lib userop.ts

94.73% Statements 18/19
50% Branches 12/24
87.5% Functions 7/8
94.44% Lines 17/18

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                                        3x           3x                   3x                   7x       6x               7x                       1x                 3x                           5x 5x 5x                             5x         5x                                                                       4x         4x                                                                                   4x                                                                                     4x                                    
import {
  encodeAbiParameters,
  parseAbiParameters,
  encodePacked,
  keccak256,
  toHex,
} from 'viem';
 
import type {
  Address,
  Delegation,
  Execution,
  Hex,
  UserOperation,
} from '../types.ts';
 
/**
 * ERC-4337 EntryPoint v0.7 address (deployed at deterministic address).
 */
export const ENTRY_POINT_V07: Address =
  '0x0000000071727de22e5e9d8baf0edac6f37da032';
 
/**
 * Default gas limits for UserOperations.
 * These are conservative defaults; use `estimateUserOpGas` for accurate values.
 */
const DEFAULT_GAS_LIMITS = {
  callGasLimit: '0x50000' as Hex,
  verificationGasLimit: '0x60000' as Hex,
  preVerificationGas: '0x10000' as Hex,
} as const;
 
/**
 * ABI tuple type for a single Delegation struct.
 */
const DELEGATION_TUPLE =
  '(address delegate, address delegator, bytes32 authority, (address enforcer, bytes terms)[] caveats, uint256 salt, bytes signature)';
 
/**
 * Encode a delegation chain into the permission context bytes
 * expected by `DelegationManager.redeemDelegations`.
 *
 * @param delegations - The delegation chain (leaf to root order).
 * @returns The ABI-encoded permission context.
 */
export function encodeDelegationChain(delegations: Delegation[]): Hex {
  const tuples = delegations.map((del) => ({
    delegate: del.delegate,
    delegator: del.delegator,
    authority: del.authority,
    caveats: del.caveats.map((cav) => ({
      enforcer: cav.enforcer,
      terms: cav.terms,
    })),
    salt: BigInt(del.salt),
    signature: del.signature ?? '0x',
  }));
 
  return encodeAbiParameters(parseAbiParameters(`${DELEGATION_TUPLE}[]`), [
    tuples,
  ] as never);
}
 
/**
 * Encode an Execution struct for use in callData.
 *
 * @param execution - The execution to encode.
 * @returns The ABI-encoded execution.
 */
export function encodeExecution(execution: Execution): Hex {
  return encodeAbiParameters(
    parseAbiParameters('address target, uint256 value, bytes callData'),
    [execution.target, BigInt(execution.value), execution.callData],
  );
}
 
/**
 * Function selector for `redeemDelegations(bytes[],uint256[],bytes[])`.
 */
const REDEEM_DELEGATIONS_SELECTOR = '0x38c86720' as Hex;
 
/**
 * 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.
 * @returns The encoded callData.
 */
export function buildRedeemCallData(options: {
  delegations: Delegation[];
  execution: Execution;
}): Hex {
  const permissionContexts = [encodeDelegationChain(options.delegations)];
  const modes = [0n]; // SingleDefault
  const executions = [
    encodeAbiParameters(
      parseAbiParameters('(address target, uint256 value, bytes callData)[]'),
      [
        [
          {
            target: options.execution.target,
            value: BigInt(options.execution.value),
            callData: options.execution.callData,
          },
        ],
      ] as never,
    ),
  ];
 
  const args = encodeAbiParameters(
    parseAbiParameters('bytes[], uint256[], bytes[]'),
    [permissionContexts, modes, executions],
  );
 
  return (REDEEM_DELEGATIONS_SELECTOR + args.slice(2)) as Hex;
}
 
/**
 * Build an unsigned UserOperation for delegation redemption.
 *
 * @param options - Options.
 * @param options.sender - The smart account address.
 * @param options.nonce - The account nonce.
 * @param options.delegations - The delegation chain.
 * @param options.execution - The execution to perform.
 * @param options.maxFeePerGas - Max fee per gas.
 * @param options.maxPriorityFeePerGas - Max priority fee per gas.
 * @param options.gasLimits - Optional gas limit overrides.
 * @param options.gasLimits.callGasLimit - Override for call gas limit.
 * @param options.gasLimits.verificationGasLimit - Override for verification gas limit.
 * @param options.gasLimits.preVerificationGas - Override for pre-verification gas.
 * @param options.factory - Optional factory address for account deployment.
 * @param options.factoryData - Optional factory data for account deployment.
 * @returns An unsigned UserOperation.
 */
export function buildDelegationUserOp(options: {
  sender: Address;
  nonce: Hex;
  delegations: Delegation[];
  execution: Execution;
  maxFeePerGas: Hex;
  maxPriorityFeePerGas: Hex;
  factory?: Address;
  factoryData?: Hex;
  gasLimits?: {
    callGasLimit?: Hex;
    verificationGasLimit?: Hex;
    preVerificationGas?: Hex;
  };
}): UserOperation {
  const callData = buildRedeemCallData({
    delegations: options.delegations,
    execution: options.execution,
  });
 
  return {
    sender: options.sender,
    nonce: options.nonce,
    ...(options.factory ? { factory: options.factory } : {}),
    ...(options.factoryData ? { factoryData: options.factoryData } : {}),
    callData,
    callGasLimit:
      options.gasLimits?.callGasLimit ?? DEFAULT_GAS_LIMITS.callGasLimit,
    verificationGasLimit:
      options.gasLimits?.verificationGasLimit ??
      DEFAULT_GAS_LIMITS.verificationGasLimit,
    preVerificationGas:
      options.gasLimits?.preVerificationGas ??
      DEFAULT_GAS_LIMITS.preVerificationGas,
    maxFeePerGas: options.maxFeePerGas,
    maxPriorityFeePerGas: options.maxPriorityFeePerGas,
    signature: '0x' as Hex,
  };
}
 
/**
 * Compute the hash of a UserOperation for signing (ERC-4337 v0.7).
 *
 * @param userOp - The UserOperation.
 * @param entryPoint - The EntryPoint address.
 * @param chainId - The chain ID.
 * @returns The hash to sign.
 */
export function computeUserOpHash(
  userOp: UserOperation,
  entryPoint: Address,
  chainId: number,
): Hex {
  // ERC-4337 v0.7 uses abi.encode for the inner hash (8 fields) and the
  // outer hash. Sub-fields accountGasLimits and gasFees are packed via
  // encodePacked(uint128, uint128) into bytes32.
  // The inner fields match PackedUserOperation:
  //   abi.encode(sender, nonce, hashInitCode, hashCallData,
  //              accountGasLimits, preVerificationGas, gasFees,
  //              hashPaymasterAndData)
  // where accountGasLimits = encodePacked(verificationGasLimit, callGasLimit)
  // and gasFees = encodePacked(maxPriorityFeePerGas, maxFeePerGas).
  const packed = keccak256(
    encodeAbiParameters(
      parseAbiParameters(
        'address, uint256, bytes32, bytes32, bytes32, uint256, bytes32, bytes32',
      ),
      [
        userOp.sender,
        BigInt(userOp.nonce),
        keccak256(
          userOp.factory && userOp.factoryData
            ? encodePacked(
                ['address', 'bytes'],
                [userOp.factory, userOp.factoryData],
              )
            : '0x',
        ),
        keccak256(userOp.callData),
        encodePacked(
          ['uint128', 'uint128'],
          [BigInt(userOp.verificationGasLimit), BigInt(userOp.callGasLimit)],
        ),
        BigInt(userOp.preVerificationGas),
        encodePacked(
          ['uint128', 'uint128'],
          [BigInt(userOp.maxPriorityFeePerGas), BigInt(userOp.maxFeePerGas)],
        ),
        keccak256(
          userOp.paymaster
            ? encodePacked(
                ['address', 'uint128', 'uint128', 'bytes'],
                [
                  userOp.paymaster,
                  BigInt(userOp.paymasterVerificationGasLimit ?? '0x0'),
                  BigInt(userOp.paymasterPostOpGasLimit ?? '0x0'),
                  userOp.paymasterData ?? '0x',
                ],
              )
            : '0x',
        ),
      ],
    ),
  );
 
  return keccak256(
    encodeAbiParameters(parseAbiParameters('bytes32, address, uint256'), [
      packed,
      entryPoint,
      BigInt(chainId),
    ]),
  );
}
 
/**
 * Convert a number to a hex string.
 *
 * @param value - The number to convert.
 * @returns The hex string.
 */
export function numberToHex(value: number | bigint): Hex {
  return toHex(value);
}