All files / evm-wallet-experiment/src constants.ts

96.55% Statements 28/29
90% Branches 9/10
75% Functions 3/4
100% Lines 28/28

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    7x         7x             7x         7x                                   17x 17x 1x         16x           7x           7x                                   7x                                       7x           7x                     7x 56x               7x             7x                           7x                                                       80x 17x 17x 16x     64x 1x         63x                         7x               7x               7x                          
import type { Address, CaveatType, Hex } from './types.ts';
 
const harden = globalThis.harden ?? (<T>(value: T): T => value);
 
/**
 * The Sepolia testnet chain ID.
 */
export const SEPOLIA_CHAIN_ID = 11155111;
 
/**
 * Base URL for the Pimlico bundler on Sepolia.
 *
 * @deprecated Use {@link getPimlicoRpcUrl} for chain-specific URLs.
 */
export const PIMLICO_RPC_BASE_URL = 'https://api.pimlico.io/v2/sepolia/rpc';
 
/**
 * Pimlico chain slug per chain ID.
 */
const PIMLICO_CHAIN_SLUGS: Record<number, string> = harden({
  1: 'ethereum',
  10: 'optimism',
  56: 'binance',
  137: 'polygon',
  8453: 'base',
  42161: 'arbitrum',
  59144: 'linea',
  11155111: 'sepolia',
});
 
/**
 * Get the Pimlico bundler RPC URL for a given chain.
 *
 * @param chainId - The chain ID.
 * @returns The Pimlico bundler RPC URL.
 */
export function getPimlicoRpcUrl(chainId: number): string {
  const slug = PIMLICO_CHAIN_SLUGS[chainId];
  if (slug === undefined) {
    throw new Error(
      `No Pimlico bundler URL for chain ${chainId}. ` +
        `Supported chains: ${Object.keys(PIMLICO_CHAIN_SLUGS).join(', ')}.`,
    );
  }
  return `https://api.pimlico.io/v2/${slug}/rpc`;
}
 
/**
 * The default BIP-44 HD path for Ethereum accounts: m/44'/60'/0'/0/{index}.
 */
export const ETH_HD_PATH_PREFIX = "m/44'/60'/0'/0" as const;
 
/**
 * The root authority hash (no parent delegation).
 */
export const ROOT_AUTHORITY: Hex =
  '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff';
 
// ---------------------------------------------------------------------------
// Chain-specific contract addresses
// ---------------------------------------------------------------------------
 
/**
 * Contract addresses for a specific chain deployment.
 */
export type ChainContracts = {
  delegationManager: Address;
  enforcers: Record<CaveatType, Address>;
};
 
/**
 * Placeholder contract addresses used when no chain-specific deployment
 * is registered. These will not work on-chain but allow offline testing.
 */
export const PLACEHOLDER_CONTRACTS: ChainContracts = harden({
  delegationManager: '0x0000000000000000000000000000000000000000' as Address,
  enforcers: {
    allowedTargets: '0x0000000000000000000000000000000000000001' as Address,
    allowedMethods: '0x0000000000000000000000000000000000000002' as Address,
    valueLte: '0x0000000000000000000000000000000000000003' as Address,
    nativeTokenTransferAmount:
      '0x0000000000000000000000000000000000000007' as Address,
    erc20TransferAmount:
      '0x0000000000000000000000000000000000000004' as Address,
    limitedCalls: '0x0000000000000000000000000000000000000005' as Address,
    timestamp: '0x0000000000000000000000000000000000000006' as Address,
  },
});
 
// ---------------------------------------------------------------------------
// Shared contract addresses (same across all supported chains)
// ---------------------------------------------------------------------------
 
const SHARED_DELEGATION_MANAGER: Address =
  '0xdb9B1e94B5b69Df7e401DDbedE43491141047dB3' as Address;
 
/**
 * Enforcer addresses shared across all supported chains (same CREATE2
 * addresses on every chain, including Sepolia).
 */
const SHARED_ENFORCERS: Record<CaveatType, Address> = harden({
  allowedTargets: '0x7F20f61b1f09b08D970938F6fa563634d65c4EeB' as Address,
  allowedMethods: '0x2c21fD0Cb9DC8445CB3fb0DC5E7Bb0Aca01842B5' as Address,
  valueLte: '0x92Bf12322527cAA612fd31a0e810472BBB106A8F' as Address,
  nativeTokenTransferAmount:
    '0xF71af580b9c3078fbc2BBF16FbB8EEd82b330320' as Address,
  erc20TransferAmount: '0xf100b0819427117EcF76Ed94B358B1A5b5C6D2Fc' as Address,
  limitedCalls: '0x04658B29F6b82ed55274221a06Fc97D318E25416' as Address,
  timestamp: '0x1046bb45C8d673d4ea75321280DB34899413c069' as Address,
});
 
const makeChainContracts = (): ChainContracts =>
  harden({
    delegationManager: SHARED_DELEGATION_MANAGER,
    enforcers: { ...SHARED_ENFORCERS },
  });
 
/**
 * Supported chain IDs (both mainnets and testnets).
 */
export const SUPPORTED_CHAIN_IDS: readonly number[] = harden([
  1, 10, 56, 137, 8453, 42161, 59144, 11155111,
]);
 
/**
 * Human-readable chain names keyed by chain ID.
 */
export const CHAIN_NAMES: Record<number, string> = harden({
  1: 'Ethereum',
  10: 'Optimism',
  56: 'BNB Smart Chain',
  137: 'Polygon',
  8453: 'Base',
  42161: 'Arbitrum One',
  59144: 'Linea',
  11155111: 'Sepolia',
});
 
/**
 * Registry of contract addresses keyed by chain ID.
 */
export const CHAIN_CONTRACTS: Readonly<Record<number, ChainContracts>> = harden(
  {
    /** Ethereum mainnet (chain 1). */
    1: makeChainContracts(),
    /** Optimism (chain 10). */
    10: makeChainContracts(),
    /** BNB Smart Chain (chain 56). */
    56: makeChainContracts(),
    /** Polygon (chain 137). */
    137: makeChainContracts(),
    /** Base (chain 8453). */
    8453: makeChainContracts(),
    /** Arbitrum One (chain 42161). */
    42161: makeChainContracts(),
    /** Linea (chain 59144). */
    59144: makeChainContracts(),
    /** Sepolia testnet (chain 11155111). */
    [SEPOLIA_CHAIN_ID]: makeChainContracts(),
  },
);
 
/**
 * Get the contract addresses for a chain, falling back to placeholders.
 *
 * @param chainId - The chain ID to look up.
 * @returns The contract addresses.
 */
export function getChainContracts(chainId?: number): ChainContracts {
  if (chainId !== undefined) {
    const entry = CHAIN_CONTRACTS[chainId];
    if (entry !== undefined) {
      return entry;
    }
  }
  if (chainId !== undefined) {
    throw new Error(
      `No contract addresses registered for chain ${chainId}. ` +
        `Register addresses in CHAIN_CONTRACTS or provide explicit enforcer addresses.`,
    );
  }
  return PLACEHOLDER_CONTRACTS;
}
 
// ---------------------------------------------------------------------------
// Legacy exports (point to placeholders)
// ---------------------------------------------------------------------------
 
/**
 * The default DelegationManager verifying contract address.
 *
 * @deprecated Use `getChainContracts(chainId).delegationManager` instead.
 */
export const DEFAULT_DELEGATION_MANAGER: Address =
  PLACEHOLDER_CONTRACTS.delegationManager;
 
/**
 * Well-known enforcer contract addresses.
 *
 * @deprecated Use `getChainContracts(chainId).enforcers` instead.
 */
export const ENFORCER_ADDRESSES: Record<CaveatType, Address> =
  PLACEHOLDER_CONTRACTS.enforcers;
 
/**
 * EIP-712 type definitions for the Delegation Framework.
 */
export const DELEGATION_TYPES: Record<
  string,
  { name: string; type: string }[]
> = harden({
  Delegation: [
    { name: 'delegate', type: 'address' },
    { name: 'delegator', type: 'address' },
    { name: 'authority', type: 'bytes32' },
    { name: 'caveats', type: 'Caveat[]' },
    { name: 'salt', type: 'uint256' },
  ],
  Caveat: [
    { name: 'enforcer', type: 'address' },
    { name: 'terms', type: 'bytes' },
  ],
});