All files / evm-wallet-experiment/src/vats away-coordinator.ts

0% Statements 0/462
0% Branches 0/341
0% Functions 0/60
0% Lines 0/454

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 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
import { E } from '@endo/eventual-send';
import { makeDefaultExo } from '@metamask/kernel-utils/exo';
import { Logger } from '@metamask/logger';
import type { Baggage } from '@metamask/ocap-kernel';
 
import type { DelegationSection } from '../lib/delegation-twin.ts';
import { makeDelegationTwin } from '../lib/delegation-twin.ts';
import {
  decodeBalanceOfResult,
  decodeDecimalsResult,
  decodeNameResult,
  decodeSymbolResult,
  encodeBalanceOf,
  encodeDecimals,
  encodeName,
  encodeSymbol,
} from '../lib/erc20.ts';
import {
  registerEnvironment,
  resolveEnvironment,
  buildSdkRedeemCallData,
  isEip7702Delegated,
  prepareUserOpTypedData,
  setSdkLogger,
  computeSmartAccountAddress,
} from '../lib/sdk.ts';
import {
  applyGasBuffer,
  validateGasEstimate,
  validateTokenCallResult,
} from '../lib/tx-utils.ts';
import { ENTRY_POINT_V07 } from '../lib/userop.ts';
import type {
  Address,
  ChainConfig,
  Delegation,
  DelegationGrant,
  Eip712TypedData,
  Execution,
  Hex,
  SmartAccountConfig,
  TransactionRequest,
  UserOperation,
  WalletCapabilities,
} from '../types.ts';
 
const harden = globalThis.harden ?? (<T>(value: T): T => value);
 
/**
 * Convert a wei amount in hex to a human-readable ETH string.
 *
 * @param weiHex - The wei amount as a hex string.
 * @returns A formatted string like "1.5 ETH".
 */
function weiToEth(weiHex: string): string {
  const wei = BigInt(weiHex);
  const whole = wei / 10n ** 18n;
  const frac = wei % 10n ** 18n;
  if (frac === 0n) {
    return `${String(whole)} ETH`;
  }
  const fracStr = frac.toString().padStart(18, '0').replace(/0+$/u, '');
  return `${String(whole)}.${fracStr} ETH`;
}
 
// ---------------------------------------------------------------------------
// Vat powers and wiring types
// ---------------------------------------------------------------------------
 
/**
 * Vat powers for the away coordinator vat.
 */
type VatPowers = {
  logger?: Logger;
};
 
/**
 * Vat references available in the away wallet subcluster.
 */
type WalletVats = {
  keyring?: unknown;
  provider?: unknown;
  redeemer?: unknown;
};
 
/**
 * Services available to the away wallet subcluster.
 */
type WalletServices = {
  ocapURLRedemptionService?: unknown;
};
 
// ---------------------------------------------------------------------------
// Typed facets for E() calls (no `any` — explicit method signatures)
// ---------------------------------------------------------------------------
 
type KeyringFacet = {
  initialize: (
    options: { type: string; mnemonic?: string },
    password?: string,
    salt?: string,
  ) => Promise<void>;
  unlock: (password: string) => Promise<void>;
  isLocked: () => Promise<boolean>;
  hasKeys: () => Promise<boolean>;
  getAccounts: () => Promise<Address[]>;
  deriveAccount: (index: number) => Promise<Address>;
  signTransaction: (tx: TransactionRequest) => Promise<Hex>;
  signTypedData: (data: Eip712TypedData, from?: Address) => Promise<Hex>;
  signMessage: (message: string, from?: Address) => Promise<Hex>;
  signHash: (hash: Hex, from?: Address) => Promise<Hex>;
  signAuthorization: (options: {
    contractAddress: Address;
    chainId: number;
    nonce?: number;
    from?: Address;
  }) => Promise<unknown>;
};
 
type ProviderFacet = {
  configure: (config: ChainConfig) => Promise<void>;
  request: (method: string, params?: unknown[]) => Promise<unknown>;
  broadcastTransaction: (signedTx: Hex) => Promise<Hex>;
  getChainId: () => Promise<number>;
  getNonce: (address: Address) => Promise<number>;
  getEntryPointNonce: (options: {
    entryPoint: Address;
    sender: Address;
    key?: Hex;
  }) => Promise<Hex>;
  submitUserOp: (options: {
    bundlerUrl: string;
    entryPoint: Hex;
    userOp: UserOperation;
  }) => Promise<Hex>;
  estimateUserOpGas: (options: {
    bundlerUrl: string;
    entryPoint: Hex;
    userOp: UserOperation;
  }) => Promise<{
    callGasLimit: Hex;
    verificationGasLimit: Hex;
    preVerificationGas: Hex;
  }>;
  getUserOpReceipt: (options: {
    bundlerUrl: string;
    userOpHash: Hex;
  }) => Promise<unknown>;
  getGasFees: () => Promise<{
    maxFeePerGas: Hex;
    maxPriorityFeePerGas: Hex;
  }>;
  configureBundler: (config: {
    bundlerUrl: string;
    chainId: number;
  }) => Promise<void>;
  httpGetJson: (url: string) => Promise<unknown>;
  getUserOperationGasPrice: () => Promise<{
    fast: { maxFeePerGas: Hex; maxPriorityFeePerGas: Hex };
  }>;
  sponsorUserOp: (options: {
    bundlerUrl: string;
    entryPoint: Hex;
    userOp: UserOperation;
    context?: Record<string, unknown>;
  }) => Promise<{
    paymaster: Address;
    paymasterData: Hex;
    paymasterVerificationGasLimit: Hex;
    paymasterPostOpGasLimit: Hex;
    callGasLimit: Hex;
    verificationGasLimit: Hex;
    preVerificationGas: Hex;
  }>;
};
 
type ExternalSignerFacet = {
  getAccounts: () => Promise<Address[]>;
  signTypedData: (data: Eip712TypedData, from: Address) => Promise<Hex>;
  signMessage: (message: string, from: Address) => Promise<Hex>;
  signTransaction: (tx: TransactionRequest) => Promise<Hex>;
};
 
type RedeemerFacet = {
  receiveGrant: (grant: DelegationGrant) => Promise<void>;
  removeGrant: (id: string) => Promise<void>;
  listGrants: () => Promise<DelegationGrant[]>;
};
 
type OcapURLRedemptionFacet = {
  redeem: (url: string) => Promise<unknown>;
};
 
// ---------------------------------------------------------------------------
// buildRootObject
// ---------------------------------------------------------------------------
 
/**
 * Build the root object for the away coordinator vat.
 *
 * The away coordinator manages routing for the semantic wallet API on the away
 * (agent) side. It keeps execution infrastructure (provider, bundler, smart
 * account, tx submission, ERC-20 queries) and routes semantic calls
 * (`transferNative`, `transferFungible`) through delegation twins first, then
 * falls back to calling home.
 *
 * @param vatPowers - Special powers granted to this vat.
 * @param _parameters - Initialization parameters (role: 'away').
 * @param baggage - Root of vat's persistent state.
 * @returns The root object for the away coordinator vat.
 */
export function buildRootObject(
  vatPowers: VatPowers,
  _parameters: unknown,
  baggage: Baggage,
): object {
  const logger = (vatPowers.logger ?? new Logger()).subLogger({
    tags: ['away-coordinator'],
  });
 
  // Wire SDK logger so resolveEnvironment/registerEnvironment are visible
  setSdkLogger((level, message, data) => {
    if (level === 'info') {
      logger.info(message, data);
    } else {
      logger.debug(message, data);
    }
  });
 
  // -------------------------------------------------------------------------
  // State variables
  // -------------------------------------------------------------------------
 
  // References to other vats (set during bootstrap)
  let keyringVat: KeyringFacet | undefined;
  let providerVat: ProviderFacet | undefined;
  let redeemerVat: RedeemerFacet | undefined;
 
  // External signer reference (e.g. MetaMask).
  // Note: external signers are transient — they must be reconnected after
  // kernel restart via connectExternalSigner(). The baggage entry tracks
  // the reference but it may be stale after resuscitation.
  let externalSigner: ExternalSignerFacet | undefined;
 
  // Bundler configuration for ERC-4337 UserOps
  let bundlerConfig:
    | {
        bundlerUrl: string;
        entryPoint: Hex;
        chainId: number;
        usePaymaster?: boolean;
        sponsorshipPolicyId?: string;
        environment?: {
          EntryPoint: Hex;
          DelegationManager: Hex;
          SimpleFactory: Hex;
          implementations: Record<string, Hex>;
          caveatEnforcers: Record<string, Hex>;
        };
      }
    | undefined;
 
  // Smart account configuration (persisted in baggage)
  let smartAccountConfig: SmartAccountConfig | undefined;
 
  // OCAP URL redemption service (wired from services in bootstrap)
  let redemptionService: OcapURLRedemptionFacet | undefined;
 
  // Routing state
  let homeSection: object | undefined; // remote ref to home's homeSection exo
  let homeCoordRef: object | undefined; // remote ref to home coordinator (for delegate registration)
  let delegationSections: DelegationSection[] = [];
  // Keyed by delegation.id so rebuildRouting preserves in-memory spend counters.
  const delegationTwinMap = new Map<string, DelegationSection>();
 
  // -------------------------------------------------------------------------
  // Baggage helpers
  // -------------------------------------------------------------------------
 
  /**
   * Typed helper for restoring values from baggage (resuscitation).
   *
   * @param key - The baggage key to look up.
   * @returns The stored value cast to T, or undefined if not present.
   */
  function restoreFromBaggage<T>(key: string): T | undefined {
    return baggage.has(key) ? (baggage.get(key) as T) : undefined;
  }
 
  /**
   * Persist a baggage key-value pair, handling both init and update.
   *
   * @param key - The baggage key.
   * @param value - The value to persist.
   */
  function persistBaggage(key: string, value: unknown): void {
    if (baggage.has(key)) {
      baggage.set(key, value);
    } else {
      baggage.init(key, value);
    }
  }
 
  // -------------------------------------------------------------------------
  // Restore state from baggage (resuscitation)
  // -------------------------------------------------------------------------
 
  keyringVat = restoreFromBaggage<KeyringFacet>('keyringVat');
  providerVat = restoreFromBaggage<ProviderFacet>('providerVat');
  redeemerVat = restoreFromBaggage<RedeemerFacet>('redeemerVat');
  externalSigner = restoreFromBaggage<ExternalSignerFacet>('externalSigner');
  bundlerConfig = restoreFromBaggage<typeof bundlerConfig>('bundlerConfig');
  if (bundlerConfig?.environment) {
    registerEnvironment(bundlerConfig.chainId, bundlerConfig.environment);
  }
  smartAccountConfig =
    restoreFromBaggage<SmartAccountConfig>('smartAccountConfig');
  homeCoordRef = restoreFromBaggage<object>('homeCoordRef');
  homeSection = restoreFromBaggage<object>('homeSection');
 
  /** Chain ID from the last `configureProvider` call (avoids RPC on every send). */
  let cachedProviderChainId: number | undefined = restoreFromBaggage<number>(
    'cachedProviderChainId',
  );
 
  // -------------------------------------------------------------------------
  // Internal async helpers
  // -------------------------------------------------------------------------
 
  /**
   * Resolve the wallet chain ID for SDK addresses and txs.
   *
   * Order: bundler config → cached provider config → `eth_chainId` RPC.
   *
   * @returns The resolved chain ID.
   */
  async function resolveChainId(): Promise<number> {
    if (bundlerConfig?.chainId !== undefined) {
      return bundlerConfig.chainId;
    }
    if (cachedProviderChainId !== undefined) {
      return cachedProviderChainId;
    }
    if (!providerVat) {
      throw new Error(
        'Provider not configured — call configureProvider() first',
      );
    }
    return E(providerVat).getChainId();
  }
 
  /**
   * Whether smart-account operations for this sender should use Infura-style
   * raw transactions (stateless 7702) instead of ERC-4337 UserOps.
   *
   * @param sender - Smart account address (same as EOA for stateless 7702).
   * @returns True when direct EIP-1559 submission should be used.
   */
  async function useDirect7702Tx(sender: Address): Promise<boolean> {
    if (smartAccountConfig?.implementation === 'stateless7702') {
      if (
        smartAccountConfig.address !== undefined &&
        smartAccountConfig.address.toLowerCase() !== sender.toLowerCase()
      ) {
        // Config points at a different account — fall through to lazy check.
      } else {
        return true;
      }
    }
    if (smartAccountConfig?.implementation === 'hybrid') {
      return false;
    }
    if (!providerVat) {
      throw new Error(
        'Cannot determine account type: provider not configured and ' +
          'smartAccountConfig is absent. Call configureProvider() first.',
      );
    }
    const code = (await E(providerVat).request('eth_getCode', [
      sender,
      'latest',
    ])) as string;
    const chainId = await resolveChainId();
    return isEip7702Delegated(code, chainId);
  }
 
  /**
   * Resolve the signing strategy for typed data.
   * Priority: keyring → external signer → error.
   * Away has no peer wallet fallback for typed data signing.
   *
   * @param data - The EIP-712 typed data to sign.
   * @returns The signature as a hex string.
   */
  async function resolveTypedDataSigning(data: Eip712TypedData): Promise<Hex> {
    if (keyringVat) {
      const hasKeys = await E(keyringVat).hasKeys();
      if (hasKeys) {
        return E(keyringVat).signTypedData(data);
      }
    }
 
    if (externalSigner) {
      const accounts = await E(externalSigner).getAccounts();
      if (accounts.length > 0) {
        return E(externalSigner).signTypedData(data, accounts[0] as Address);
      }
    }
 
    if (homeCoordRef) {
      return E(homeCoordRef).signTypedData(data);
    }
 
    throw new Error('No authority to sign typed data');
  }
 
  /**
   * Resolve the signing strategy for a transaction.
   * LOCAL KEY ONLY on away side — no peer fallback.
   * Priority: keyring → external signer → error.
   *
   * @param tx - The transaction request to sign.
   * @returns The signed transaction as a hex string.
   */
  async function resolveTransactionSigning(
    tx: TransactionRequest,
  ): Promise<Hex> {
    // Strategy 1: Check if local keyring owns this account
    if (keyringVat) {
      const accounts = await E(keyringVat).getAccounts();
      if (accounts.includes(tx.from.toLowerCase() as Address)) {
        return E(keyringVat).signTransaction(tx);
      }
    }
 
    // Strategy 2: Check if external signer can handle it
    if (externalSigner) {
      return E(externalSigner).signTransaction({
        ...tx,
        from: tx.from.toLowerCase() as Address,
      });
    }
 
    throw new Error('No authority to sign this transaction');
  }
 
  /**
   * Sign and broadcast a self-call tx with SDK-encoded DeleGator calldata
   * (7702 EOA). Returns the transaction hash immediately after broadcast.
   *
   * @param options - Direct submission options.
   * @param options.sender - Upgraded EOA / smart account address.
   * @param options.callData - SDK-wrapped `execute` calldata.
   * @param options.maxFeePerGas - Optional max fee per gas override.
   * @param options.maxPriorityFeePerGas - Optional priority fee override.
   * @returns The transaction hash from `eth_sendRawTransaction`.
   */
  async function buildAndSubmitDirect7702Tx(options: {
    sender: Address;
    callData: Hex;
    maxFeePerGas?: Hex;
    maxPriorityFeePerGas?: Hex;
  }): Promise<Hex> {
    if (!providerVat) {
      throw new Error('Provider vat not available');
    }
    const chainId = await resolveChainId();
    let { maxFeePerGas, maxPriorityFeePerGas } = options;
    if (!maxFeePerGas || !maxPriorityFeePerGas) {
      const fees = await E(providerVat).getGasFees();
      maxFeePerGas = maxFeePerGas ?? fees.maxFeePerGas;
      maxPriorityFeePerGas = maxPriorityFeePerGas ?? fees.maxPriorityFeePerGas;
    }
    const nonce = await E(providerVat).getNonce(options.sender);
    const estimatedGas = validateGasEstimate(
      await E(providerVat).request('eth_estimateGas', [
        {
          from: options.sender,
          to: options.sender,
          data: options.callData,
        },
      ]),
    );
    const gasLimit = applyGasBuffer(estimatedGas, 10);
    const filledTx: TransactionRequest = {
      from: options.sender,
      to: options.sender,
      chainId,
      nonce,
      maxFeePerGas,
      maxPriorityFeePerGas,
      gasLimit,
      data: options.callData,
      value: '0x0' as Hex,
    };
    const signedTx = await resolveTransactionSigning(filledTx);
    return E(providerVat).broadcastTransaction(signedTx);
  }
 
  /**
   * Build, sign, and submit a UserOp. Shared pipeline for delegation
   * redemption.
   *
   * @param options - Pipeline options.
   * @param options.sender - The smart account address that sends the UserOp.
   * @param options.callData - The encoded callData for the UserOp.
   * @param options.maxFeePerGas - Optional max fee per gas override.
   * @param options.maxPriorityFeePerGas - Optional max priority fee per gas override.
   * @returns The UserOp hash from the bundler.
   */
  async function buildAndSubmitUserOp(options: {
    sender: Address;
    callData: Hex;
    maxFeePerGas?: Hex;
    maxPriorityFeePerGas?: Hex;
  }): Promise<Hex> {
    if (!providerVat) {
      throw new Error('Provider vat not available');
    }
    if (!bundlerConfig) {
      throw new Error('Bundler not configured');
    }
 
    const { sender, callData } = options;
 
    // Get gas prices from the bundler (pimlico_getUserOperationGasPrice)
    // which returns prices the bundler will accept, avoiding rejection
    // due to stale node-reported fees.
    let { maxFeePerGas, maxPriorityFeePerGas } = options;
    if (!maxFeePerGas || !maxPriorityFeePerGas) {
      const gasPrice = await E(providerVat).getUserOperationGasPrice();
      maxFeePerGas = maxFeePerGas ?? gasPrice.fast.maxFeePerGas;
      maxPriorityFeePerGas =
        maxPriorityFeePerGas ?? gasPrice.fast.maxPriorityFeePerGas;
    }
 
    // Get nonce from EntryPoint contract (ERC-4337 nonce)
    const nonceHex = await E(providerVat).getEntryPointNonce({
      entryPoint: bundlerConfig.entryPoint,
      sender,
    });
 
    // Detect signing mode: check smartAccountConfig first, then fall back
    // to on-chain code inspection. This ensures the correct signing mode
    // even if smartAccountConfig is lost from baggage.
    let isStateless7702 =
      smartAccountConfig?.implementation === 'stateless7702';
 
    // Always fetch on-chain code — needed for both factory detection and
    // signing mode fallback.
    const onChainCode = (await E(providerVat).request('eth_getCode', [
      sender,
      'latest',
    ])) as string | undefined;
 
    if (typeof onChainCode !== 'string') {
      throw new Error(
        `eth_getCode for ${sender} returned ${String(onChainCode)}; check provider configuration`,
      );
    }
 
    // Fall back to on-chain code detection for 7702 accounts that weren't
    // configured via smartAccountConfig (e.g., restored from stale baggage).
    // Any EIP-7702 designator prefix (0xef0100) indicates a Stateless7702
    // DeleGator, which uses a different EIP-712 domain name for signing.
    if (!isStateless7702 && onChainCode.toLowerCase().startsWith('0xef0100')) {
      isStateless7702 = true;
    }
 
    // Check on-chain whether the smart account is deployed (eth_getCode).
    // This avoids relying on a cached flag that could be stale if the
    // deployment UserOp failed on-chain.
    let includeFactory = false;
    if (
      !isStateless7702 &&
      smartAccountConfig?.factory &&
      smartAccountConfig.factoryData
    ) {
      includeFactory = onChainCode === '0x' || onChainCode === '0x0';
 
      if (!includeFactory && smartAccountConfig.deployed === false) {
        smartAccountConfig = harden({
          ...smartAccountConfig,
          deployed: true,
        });
        persistBaggage('smartAccountConfig', smartAccountConfig);
      }
    }
 
    // Build unsigned UserOp with a dummy 65-byte signature so that the
    // smart account's validateUserOp can parse the ECDSA signature during
    // bundler/paymaster simulation. An empty signature (0x) causes revert.
    const unsignedUserOp: UserOperation = {
      sender,
      nonce: nonceHex,
      callData,
      callGasLimit: '0x50000' as Hex,
      verificationGasLimit: '0x60000' as Hex,
      preVerificationGas: '0x10000' as Hex,
      maxFeePerGas,
      maxPriorityFeePerGas,
      signature:
        '0xfffffffffffffffffffffffffffffff0000000000000000000000000000000007aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa1c' as Hex,
      ...(includeFactory && smartAccountConfig
        ? {
            factory: smartAccountConfig.factory as Hex,
            factoryData: smartAccountConfig.factoryData as Hex,
          }
        : {}),
    };
 
    let userOpWithGas: UserOperation;
 
    if (bundlerConfig.usePaymaster) {
      // Use paymaster sponsorship instead of gas estimation
      const sponsorContext: Record<string, unknown> = {};
      if (bundlerConfig.sponsorshipPolicyId) {
        sponsorContext.sponsorshipPolicyId = bundlerConfig.sponsorshipPolicyId;
      }
 
      const sponsorResult = await E(providerVat).sponsorUserOp({
        bundlerUrl: bundlerConfig.bundlerUrl,
        entryPoint: bundlerConfig.entryPoint,
        userOp: unsignedUserOp,
        context: sponsorContext,
      });
 
      userOpWithGas = {
        ...unsignedUserOp,
        paymaster: sponsorResult.paymaster,
        paymasterData: sponsorResult.paymasterData,
        paymasterVerificationGasLimit:
          sponsorResult.paymasterVerificationGasLimit,
        paymasterPostOpGasLimit: sponsorResult.paymasterPostOpGasLimit,
        callGasLimit: sponsorResult.callGasLimit,
        verificationGasLimit: sponsorResult.verificationGasLimit,
        preVerificationGas: sponsorResult.preVerificationGas,
      };
    } else {
      // Estimate gas via bundler
      const gasEstimate = await E(providerVat).estimateUserOpGas({
        bundlerUrl: bundlerConfig.bundlerUrl,
        entryPoint: bundlerConfig.entryPoint,
        userOp: unsignedUserOp,
      });
 
      userOpWithGas = {
        ...unsignedUserOp,
        callGasLimit: applyGasBuffer(gasEstimate.callGasLimit, 10),
        verificationGasLimit: applyGasBuffer(
          gasEstimate.verificationGasLimit,
          10,
        ),
        preVerificationGas: gasEstimate.preVerificationGas,
      };
    }
 
    // Sign the UserOp via EIP-712 typed data. Both Hybrid and Stateless7702
    // DeleGators validate signatures using EIP-712 — the only difference is
    // the domain name.
    const userOpTypedData = prepareUserOpTypedData({
      userOp: userOpWithGas,
      entryPoint: bundlerConfig.entryPoint,
      chainId: bundlerConfig.chainId,
      smartAccountAddress: sender,
      ...(isStateless7702
        ? { smartAccountName: 'EIP7702StatelessDeleGator' }
        : {}),
    });
    const signature: Hex = await resolveTypedDataSigning(userOpTypedData);
 
    // Attach signature and submit
    const signedUserOp: UserOperation = {
      ...userOpWithGas,
      signature,
    };
 
    return E(providerVat).submitUserOp({
      bundlerUrl: bundlerConfig.bundlerUrl,
      entryPoint: bundlerConfig.entryPoint,
      userOp: signedUserOp,
    });
  }
 
  /**
   * Build, sign, and submit a UserOp that redeems one or more delegations.
   *
   * @param options - UserOp pipeline options.
   * @param options.delegations - The delegation chain (leaf to root).
   * @param options.execution - The execution to perform.
   * @param options.maxFeePerGas - Max fee per gas.
   * @param options.maxPriorityFeePerGas - Max priority fee per gas.
   * @returns The UserOp hash from the bundler.
   */
  async function submitDelegationUserOp(options: {
    delegations: Delegation[];
    execution: Execution;
    maxFeePerGas?: Hex | undefined;
    maxPriorityFeePerGas?: Hex | undefined;
  }): Promise<Hex> {
    if (!bundlerConfig && !smartAccountConfig) {
      throw new Error(
        'Bundler not configured — cannot redeem delegation without bundler or smart account config',
      );
    }
 
    const sender =
      smartAccountConfig?.address ?? options.delegations[0].delegate;
 
    const chainId = await resolveChainId();
    const sdkCallData = buildSdkRedeemCallData({
      delegations: options.delegations,
      execution: options.execution,
      chainId,
    });
 
    if (await useDirect7702Tx(sender)) {
      return buildAndSubmitDirect7702Tx({
        sender,
        callData: sdkCallData,
        maxFeePerGas: options.maxFeePerGas,
        maxPriorityFeePerGas: options.maxPriorityFeePerGas,
      });
    }
 
    if (!bundlerConfig) {
      throw new Error(
        'Bundler not configured (required for hybrid smart account redemption)',
      );
    }
 
    const userOpOptions: {
      sender: Address;
      callData: Hex;
      maxFeePerGas?: Hex;
      maxPriorityFeePerGas?: Hex;
    } = { sender, callData: sdkCallData };
    if (options.maxFeePerGas) {
      userOpOptions.maxFeePerGas = options.maxFeePerGas;
    }
    if (options.maxPriorityFeePerGas) {
      userOpOptions.maxPriorityFeePerGas = options.maxPriorityFeePerGas;
    }
 
    return buildAndSubmitUserOp(userOpOptions);
  }
 
  /**
   * Poll until an EIP-1559 transaction is mined or timeout.
   *
   * @param options - Polling options.
   * @param options.txHash - Transaction hash to wait for.
   * @param options.pollIntervalMs - Delay between RPC polls in milliseconds.
   * @param options.timeoutMs - Maximum time to wait in milliseconds.
   * @returns Whether the mined transaction succeeded (`status` 0x1).
   */
  async function pollTransactionReceipt(options: {
    txHash: Hex;
    pollIntervalMs?: number;
    timeoutMs?: number;
  }): Promise<{ success: boolean }> {
    if (!providerVat) {
      throw new Error('Provider not configured');
    }
    if (
      typeof globalThis.Date?.now !== 'function' ||
      typeof globalThis.setTimeout !== 'function'
    ) {
      throw new Error(
        'Transaction receipt polling requires Date.now and setTimeout',
      );
    }
    const interval = options.pollIntervalMs ?? 2000;
    const timeout = options.timeoutMs ?? 120_000;
    const start = Date.now();
    while (Date.now() - start < timeout) {
      let receipt: { status?: string | number } | null = null;
      try {
        receipt = (await E(providerVat).request('eth_getTransactionReceipt', [
          options.txHash,
        ])) as { status?: string | number } | null;
      } catch (error) {
        // Transient RPC errors (network hiccups, rate limits) should not
        // abort polling — the tx was already broadcast and may still mine.
        logger.warn(
          `RPC error polling receipt for ${options.txHash}, will retry`,
          error,
        );
        await new Promise((resolve) => setTimeout(resolve, interval));
        continue;
      }
      if (receipt) {
        // Normalize: some providers return status as a number (1) rather
        // than the standard hex string ('0x1'). EIP-1559 receipts must have
        // a status field; a missing one likely indicates a malformed response.
        const { status } = receipt;
        if (status === undefined || status === null) {
          logger.warn(
            `Receipt for ${options.txHash} has no status field — assuming success`,
          );
          return harden({ success: true });
        }
        const normalizedStatus =
          typeof status === 'number' ? `0x${status.toString(16)}` : status;
        return harden({ success: normalizedStatus === '0x1' });
      }
      await new Promise((resolve) => setTimeout(resolve, interval));
    }
    throw new Error(
      `Transaction ${options.txHash} not mined after ${String(timeout)}ms`,
    );
  }
 
  /**
   * Resolve the EOA owner address from the keyring or external signer.
   *
   * @returns The first available EOA address.
   * @throws If no accounts are available.
   */
  async function resolveOwnerAddress(): Promise<Address> {
    if (keyringVat) {
      const accounts = await E(keyringVat).getAccounts();
      if (accounts.length > 0) {
        return accounts[0] as Address;
      }
    }
    if (externalSigner) {
      const accounts = await E(externalSigner).getAccounts();
      if (accounts.length > 0) {
        return accounts[0] as Address;
      }
    }
    throw new Error('No accounts available');
  }
 
  /**
   * Create a Stateless7702 smart account by signing and broadcasting
   * an EIP-7702 authorization transaction. The user's EOA address
   * becomes the smart account — no factory deployment or funding needed.
   *
   * @param chainId - The chain ID.
   * @returns The smart account configuration.
   */
  async function createStateless7702SmartAccount(
    chainId: number,
  ): Promise<SmartAccountConfig> {
    if (!providerVat) {
      throw new Error('Provider vat required for EIP-7702 authorization');
    }
 
    // Resolve EOA address: keyring first, then external signer.
    let eoaAddress: Address;
    try {
      eoaAddress = await resolveOwnerAddress();
    } catch {
      throw new Error('No accounts available for EIP-7702 smart account');
    }
 
    // Check if already set up (persisted from a prior call)
    if (
      smartAccountConfig?.implementation === 'stateless7702' &&
      smartAccountConfig.address === eoaAddress
    ) {
      return smartAccountConfig;
    }
 
    // Best-effort on-chain check — works on providers that support
    // EIP-7702 designator codes via eth_getCode (not all do, e.g. Infura).
    const code = (await E(providerVat).request('eth_getCode', [
      eoaAddress,
      'latest',
    ])) as string;
 
    if (isEip7702Delegated(code, chainId)) {
      // eslint-disable-next-line require-atomic-updates
      smartAccountConfig = harden({
        implementation: 'stateless7702' as const,
        address: eoaAddress,
        deployed: true,
      });
      persistBaggage('smartAccountConfig', smartAccountConfig);
      return smartAccountConfig;
    }
 
    // EIP-7702 promotion requires signAuthorization which is only
    // available on the local keyring (not supported by external signers).
    if (!keyringVat || !(await E(keyringVat).hasKeys())) {
      throw new Error(
        'EIP-7702 promotion requires a local keyring with initialized keys. ' +
          'Use implementation: "hybrid", or promote the account through MetaMask first.',
      );
    }
 
    // Sign EIP-7702 authorization
    const env = resolveEnvironment(chainId);
    const implAddress = (
      env.implementations as Record<string, string | undefined>
    ).EIP7702StatelessDeleGatorImpl;
    if (!implAddress) {
      throw new Error(
        `EIP7702StatelessDeleGatorImpl not found in environment for chain ${String(chainId)}`,
      );
    }
 
    // Fetch the EOA nonce, gas fees, and sign the authorization in parallel.
    // EIP-7702 self-execution: the tx sender is the same EOA as the
    // authorization authority. The sender's nonce is incremented by the tx
    // validity check BEFORE authorizations are processed, so the
    // authorization nonce must be txNonce + 1.
    const EIP7702_FALLBACK_GAS = '0x19000' as Hex; // 102400
    // Minimum plausible gas for an EIP-7702 auth tx (~40k). Estimates
    // below this likely indicate the RPC ignored the authorizationList
    // and returned a plain-transfer estimate (21000).
    const EIP7702_MIN_GAS = 0xa000n; // 40960
    const [nonce, fees, estimatedAuthGas] = await Promise.all([
      E(providerVat).getNonce(eoaAddress),
      E(providerVat).getGasFees(),
      (
        E(providerVat).request('eth_estimateGas', [
          {
            from: eoaAddress,
            to: eoaAddress,
            authorizationList: [{ address: implAddress, chainId }],
          },
        ]) as Promise<Hex>
      ).then(
        (result) => {
          if (typeof result !== 'string' || !result.startsWith('0x')) {
            logger.warn(
              `eth_estimateGas returned non-hex for EIP-7702 auth: ${String(result)}, using fallback`,
            );
            return EIP7702_FALLBACK_GAS;
          }
          if (BigInt(result) < EIP7702_MIN_GAS) {
            logger.warn(
              `eth_estimateGas returned suspiciously low value ${result} for EIP-7702 auth, using fallback`,
            );
            return EIP7702_FALLBACK_GAS;
          }
          return result;
        },
        (error: unknown) => {
          const message =
            error instanceof Error ? error.message : String(error);
          // Only fall back when the RPC doesn't support authorizationList param
          if (
            message.includes('-32602') ||
            message.includes('-32601') ||
            message.includes('not supported') ||
            message.includes('unknown field')
          ) {
            logger.warn(
              'eth_estimateGas does not support authorizationList, using fallback gas',
            );
            return EIP7702_FALLBACK_GAS;
          }
          throw new Error(
            `eth_estimateGas failed for EIP-7702 authorization: ${message}`,
          );
        },
      ),
    ]);
    const authGasLimit = applyGasBuffer(estimatedAuthGas, 20);
    const signedAuth = await E(keyringVat).signAuthorization({
      contractAddress: implAddress as Address,
      chainId,
      nonce: nonce + 1,
    });
 
    const signedTx = await E(keyringVat).signTransaction({
      from: eoaAddress,
      to: eoaAddress,
      chainId,
      nonce,
      maxFeePerGas: fees.maxFeePerGas,
      maxPriorityFeePerGas: fees.maxPriorityFeePerGas,
      gasLimit: authGasLimit,
      authorizationList: [signedAuth],
    });
 
    const txHash = await E(providerVat).broadcastTransaction(signedTx);
 
    // Wait for the authorization tx to be mined. Some RPC providers (e.g.
    // Infura) don't expose EIP-7702 designator code via eth_getCode, so we
    // poll eth_getTransactionReceipt instead (status 0x1 = success).
    if (typeof globalThis.setTimeout !== 'function') {
      throw new Error(
        'EIP-7702 confirmation polling requires setTimeout ' +
          '(not available in SES compartments without timer endowments)',
      );
    }
    const maxAttempts = 45;
    for (let i = 0; i < maxAttempts; i++) {
      const receipt = (await E(providerVat).request(
        'eth_getTransactionReceipt',
        [txHash],
      )) as { status?: string } | null;
      if (receipt?.status === '0x1') {
        break;
      }
      if (receipt?.status === '0x0') {
        throw new Error(
          `EIP-7702 authorization tx ${txHash as string} reverted on-chain`,
        );
      }
      if (i === maxAttempts - 1) {
        throw new Error(
          `EIP-7702 authorization tx ${txHash} not confirmed after 90s`,
        );
      }
      await new Promise((resolve) => setTimeout(resolve, 2000));
    }
 
    // eslint-disable-next-line require-atomic-updates
    smartAccountConfig = harden({
      implementation: 'stateless7702' as const,
      address: eoaAddress,
      deployed: true,
    });
    persistBaggage('smartAccountConfig', smartAccountConfig);
    return smartAccountConfig;
  }
 
  // -------------------------------------------------------------------------
  // Routing helpers
  // -------------------------------------------------------------------------
 
  /**
   * Build a `redeemFn` closure for a given delegation.
   * Used by `makeDelegationTwin` to submit delegation redemptions.
   *
   * @param delegation - The delegation to redeem against.
   * @returns An async function that submits an execution as a delegation UserOp.
   */
  function makeRedeemFn(
    delegation: Delegation,
  ): (execution: Execution) => Promise<Hex> {
    return async (execution: Execution): Promise<Hex> => {
      if (bundlerConfig || smartAccountConfig) {
        return submitDelegationUserOp({ delegations: [delegation], execution });
      }
      if (homeCoordRef) {
        return E(homeCoordRef).redeemDelegation({ delegation, execution });
      }
      throw new Error(
        'Bundler not configured — cannot redeem delegation without bundler or smart account config',
      );
    };
  }
 
  /**
   * Rebuild the delegation sections from current redeemer grants.
   * Called after `receiveDelegation` or `connectToPeer`.
   */
  async function rebuildRouting(): Promise<void> {
    const grants = redeemerVat ? await E(redeemerVat).listGrants() : [];
    const currentIds = new Set(grants.map((grant) => grant.delegation.id));
    for (const id of delegationTwinMap.keys()) {
      if (!currentIds.has(id)) {
        delegationTwinMap.delete(id);
      }
    }
    for (const grant of grants) {
      if (!delegationTwinMap.has(grant.delegation.id)) {
        delegationTwinMap.set(
          grant.delegation.id,
          makeDelegationTwin({
            grant,
            redeemFn: makeRedeemFn(grant.delegation),
          }),
        );
      }
    }
    delegationSections = grants.map(
      (grant) =>
        delegationTwinMap.get(grant.delegation.id) as DelegationSection,
    );
  }
 
  // -------------------------------------------------------------------------
  // Exo (public API)
  // -------------------------------------------------------------------------
 
  const awayCoordinator = makeDefaultExo('awayCoordinator', {
    // ------------------------------------------------------------------
    // Lifecycle
    // ------------------------------------------------------------------
 
    /**
     * Wire vat references and services. Called by the kernel during subcluster
     * bootstrap.
     *
     * @param vats - References to co-located vats.
     * @param services - External services available to this subcluster.
     */
    async bootstrap(vats: WalletVats, services: WalletServices): Promise<void> {
      keyringVat = vats.keyring as KeyringFacet | undefined;
      providerVat = vats.provider as ProviderFacet | undefined;
      redeemerVat = vats.redeemer as RedeemerFacet | undefined;
      redemptionService = services.ocapURLRedemptionService as
        | OcapURLRedemptionFacet
        | undefined;
 
      if (keyringVat) {
        persistBaggage('keyringVat', keyringVat);
      }
      if (providerVat) {
        persistBaggage('providerVat', providerVat);
      }
      if (redeemerVat) {
        persistBaggage('redeemerVat', redeemerVat);
      }
 
      logger.info('away bootstrap complete', {
        hasKeyring: Boolean(keyringVat),
        hasProvider: Boolean(providerVat),
        hasRedeemer: Boolean(redeemerVat),
      });
 
      // Rebuild routing from persisted state (e.g. after kernel restart).
      // homeSection is already restored from baggage; grants come from redeemerVat.
      if (redeemerVat || homeSection) {
        await rebuildRouting();
      }
    },
 
    // ------------------------------------------------------------------
    // Keyring
    // ------------------------------------------------------------------
 
    /**
     * Initialize the keyring vat with a seed phrase or throwaway entropy.
     *
     * @param options - Keyring initialization options.
     * @param options.type - 'srp' for a seed phrase, 'throwaway' for ephemeral key.
     * @param options.mnemonic - BIP-39 mnemonic (srp only).
     * @param options.entropy - Random entropy hex (throwaway only).
     * @param options.password - Encryption password (srp only).
     * @param options.salt - Encryption salt (srp only).
     * @param options.addressIndex - HD derivation index (srp only).
     */
    async initializeKeyring(options: {
      type: 'srp' | 'throwaway';
      mnemonic?: string;
      entropy?: Hex;
      password?: string;
      salt?: string;
      addressIndex?: number;
    }): Promise<void> {
      if (!keyringVat) {
        throw new Error('Keyring vat not available');
      }
      let initOptions:
        | { type: 'srp'; mnemonic: string; addressIndex?: number }
        | { type: 'throwaway'; entropy?: Hex };
      if (options.type === 'throwaway') {
        initOptions = { type: 'throwaway', entropy: options.entropy };
      } else {
        initOptions =
          options.addressIndex === undefined
            ? { type: 'srp', mnemonic: options.mnemonic ?? '' }
            : {
                type: 'srp',
                mnemonic: options.mnemonic ?? '',
                addressIndex: options.addressIndex,
              };
      }
 
      const password = options.type === 'srp' ? options.password : undefined;
      await E(keyringVat).initialize(initOptions, password, options.salt);
    },
 
    /**
     * Unlock the keyring with the stored password.
     *
     * @param password - The decryption password.
     */
    async unlockKeyring(password: string): Promise<void> {
      if (!keyringVat) {
        throw new Error('Keyring vat not available');
      }
      await E(keyringVat).unlock(password);
    },
 
    /**
     * Check whether the keyring is currently locked.
     *
     * @returns True if the keyring is locked, false otherwise.
     */
    async isKeyringLocked(): Promise<boolean> {
      if (!keyringVat) {
        throw new Error('Keyring vat not available');
      }
      return E(keyringVat).isLocked();
    },
 
    // ------------------------------------------------------------------
    // Provider & bundler configuration
    // ------------------------------------------------------------------
 
    /**
     * Configure the JSON-RPC provider (sets RPC URL and chain ID).
     *
     * @param chainConfig - Chain configuration with RPC URL and chain ID.
     */
    async configureProvider(chainConfig: ChainConfig): Promise<void> {
      if (!providerVat) {
        throw new Error('Provider vat not available');
      }
 
      // Validate RPC URL (regex — URL constructor unavailable under SES)
      if (!/^https?:\/\/.+/u.test(chainConfig.rpcUrl)) {
        throw new Error(
          `Invalid RPC URL: "${chainConfig.rpcUrl}". Must be a valid HTTP(S) URL.`,
        );
      }
 
      if (!Number.isInteger(chainConfig.chainId) || chainConfig.chainId <= 0) {
        throw new Error(
          `Invalid chain ID: ${String(chainConfig.chainId)}. Must be a positive integer.`,
        );
      }
 
      await E(providerVat).configure(chainConfig);
 
      cachedProviderChainId = chainConfig.chainId;
      persistBaggage('cachedProviderChainId', cachedProviderChainId);
    },
 
    /**
     * Configure the ERC-4337 bundler for UserOp submission.
     *
     * @param config - Bundler configuration.
     * @param config.bundlerUrl - The bundler RPC URL.
     * @param config.entryPoint - EntryPoint contract address (defaults to v0.7).
     * @param config.chainId - The chain ID the bundler operates on.
     * @param config.usePaymaster - Whether to use paymaster sponsorship.
     * @param config.sponsorshipPolicyId - Paymaster policy ID for sponsored ops.
     * @param config.environment - Custom SDK environment for non-standard chains.
     * @param config.environment.EntryPoint - EntryPoint contract address.
     * @param config.environment.DelegationManager - DelegationManager contract address.
     * @param config.environment.SimpleFactory - SimpleFactory contract address.
     * @param config.environment.implementations - Map of implementation names to addresses.
     * @param config.environment.caveatEnforcers - Map of enforcer names to addresses.
     */
    async configureBundler(config: {
      bundlerUrl: string;
      entryPoint?: Hex;
      chainId: number;
      usePaymaster?: boolean;
      sponsorshipPolicyId?: string;
      environment?: {
        EntryPoint: Hex;
        DelegationManager: Hex;
        SimpleFactory: Hex;
        implementations: Record<string, Hex>;
        caveatEnforcers: Record<string, Hex>;
      };
    }): Promise<void> {
      // Validate bundler URL (regex — URL constructor unavailable under SES)
      if (!/^https?:\/\/.+/u.test(config.bundlerUrl)) {
        throw new Error(
          `Invalid bundler URL: "${config.bundlerUrl}". Must be a valid HTTP(S) URL.`,
        );
      }
 
      if (!Number.isInteger(config.chainId) || config.chainId <= 0) {
        throw new Error(
          `Invalid chain ID: ${String(config.chainId)}. Must be a positive integer.`,
        );
      }
 
      // Register a custom SDK environment for chains not in the SDK's built-in
      // registry (e.g. local Anvil at chain 31337).
      if (config.environment) {
        registerEnvironment(config.chainId, config.environment);
      }
 
      bundlerConfig = harden({
        bundlerUrl: config.bundlerUrl,
        entryPoint: config.entryPoint ?? ENTRY_POINT_V07,
        chainId: config.chainId,
        usePaymaster: config.usePaymaster,
        sponsorshipPolicyId: config.sponsorshipPolicyId,
        environment: config.environment,
      });
      persistBaggage('bundlerConfig', bundlerConfig);
      logger.info('bundler configured', {
        bundlerUrl: config.bundlerUrl,
        chainId: config.chainId,
        entryPoint: bundlerConfig.entryPoint,
        hasEnvironment: Boolean(config.environment),
      });
 
      if (!providerVat) {
        throw new Error(
          'Provider vat not available. Call configureProvider() before configureBundler().',
        );
      }
      await E(providerVat).configureBundler({
        bundlerUrl: config.bundlerUrl,
        chainId: config.chainId,
      });
    },
 
    /**
     * Connect an external signer (e.g. MetaMask) for transaction signing.
     *
     * @param signer - The external signer facet.
     */
    async connectExternalSigner(signer: ExternalSignerFacet): Promise<void> {
      if (!signer || typeof signer !== 'object') {
        throw new Error('Invalid external signer: must be a non-null object');
      }
      externalSigner = signer;
      persistBaggage('externalSigner', externalSigner);
    },
 
    // ------------------------------------------------------------------
    // Smart account configuration
    // ------------------------------------------------------------------
 
    /**
     * Create or restore a smart account configuration.
     *
     * @param config - Smart account creation options.
     * @param config.deploySalt - Optional deploy salt for counterfactual address derivation.
     * @param config.chainId - The chain ID the smart account is deployed on.
     * @param config.address - Optional explicit smart account address (skips derivation).
     * @param config.implementation - 'hybrid' (ERC-4337) or 'stateless7702' (EIP-7702).
     * @returns The smart account configuration including the derived address.
     */
    async createSmartAccount(config: {
      deploySalt?: Hex;
      chainId: number;
      address?: Address;
      implementation?: 'hybrid' | 'stateless7702';
    }): Promise<SmartAccountConfig> {
      const implementation = config.implementation ?? 'hybrid';
 
      if (implementation === 'stateless7702') {
        return createStateless7702SmartAccount(config.chainId);
      }
 
      // Hybrid path (existing logic)
      let { address } = config;
      let factory: Address | undefined;
      let factoryData: Hex | undefined;
      const deploySalt =
        config.deploySalt ??
        ('0x0000000000000000000000000000000000000000000000000000000000000001' as Hex);
 
      // Derive counterfactual address if not explicitly provided
      if (!address) {
        // Find the owner EOA from keyring or external signer
        let owner: Address;
        try {
          owner = await resolveOwnerAddress();
        } catch {
          throw new Error(
            'No owner account available to derive smart account address',
          );
        }
 
        const env = resolveEnvironment(config.chainId);
        factory = env.SimpleFactory;
 
        const derived = await computeSmartAccountAddress({
          owner,
          deploySalt,
          chainId: config.chainId,
        });
        address = derived.address;
        factoryData = derived.factoryData;
      }
 
      smartAccountConfig = harden({
        implementation: 'hybrid' as const,
        deploySalt,
        address,
        factory,
        factoryData,
        deployed: false,
      });
      persistBaggage('smartAccountConfig', smartAccountConfig);
      return smartAccountConfig;
    },
 
    /**
     * Return the smart account address, if one is configured.
     *
     * @returns The smart account address, or undefined.
     */
    async getSmartAccountAddress(): Promise<Address | undefined> {
      return smartAccountConfig?.address;
    },
 
    // ------------------------------------------------------------------
    // Public wallet API
    // ------------------------------------------------------------------
 
    /**
     * Return all locally available accounts (keyring + external signer).
     *
     * @returns Array of Ethereum addresses.
     */
    async getAccounts(): Promise<Address[]> {
      // Peer (home) accounts take priority — they are the identity visible to
      // the outside world. Local throwaway keys are exposed only via
      // getCapabilities().localAccounts, not here.
      if (homeCoordRef) {
        return E(homeCoordRef).getAccounts();
      }
 
      const localAccounts: Address[] = keyringVat
        ? await E(keyringVat).getAccounts()
        : [];
 
      const extAccounts: Address[] = externalSigner
        ? await E(externalSigner).getAccounts()
        : [];
 
      // Deduplicate by lowercasing
      const seen = new Set(localAccounts.map((a) => a.toLowerCase()));
      const merged = [...localAccounts];
      for (const account of extAccounts) {
        if (!seen.has(account.toLowerCase())) {
          seen.add(account.toLowerCase());
          merged.push(account);
        }
      }
      return merged;
    },
 
    /**
     * Sign a raw transaction using the local key (keyring or external signer).
     * Away coordinator has no peer wallet fallback for signing.
     *
     * @param tx - The transaction request to sign.
     * @returns The signed transaction as a hex string.
     */
    async signTransaction(tx: TransactionRequest): Promise<Hex> {
      return resolveTransactionSigning(tx);
    },
 
    /**
     * Sign an arbitrary message using the local key (keyring or external signer).
     *
     * @param message - The message to sign.
     * @returns The signature as a hex string.
     */
    async signMessage(message: string): Promise<Hex> {
      if (keyringVat) {
        const hasKeys = await E(keyringVat).hasKeys();
        if (hasKeys) {
          return E(keyringVat).signMessage(message);
        }
      }
      if (externalSigner) {
        const accounts = await E(externalSigner).getAccounts();
        if (accounts.length > 0) {
          return E(externalSigner).signMessage(message, accounts[0] as Address);
        }
      }
      if (homeCoordRef) {
        return E(homeCoordRef).signMessage(message);
      }
      throw new Error('No authority to sign message');
    },
 
    /**
     * Sign EIP-712 typed data using the local key (keyring or external signer).
     *
     * @param data - The EIP-712 typed data to sign.
     * @returns The signature as a hex string.
     */
    async signTypedData(data: Eip712TypedData): Promise<Hex> {
      return resolveTypedDataSigning(data);
    },
 
    /**
     * Send a transaction using the direct path only (no delegation matching).
     * Away's delegation routing goes through transferNative/transferFungible, not sendTransaction.
     *
     * @param tx - The transaction request.
     * @returns The transaction hash.
     */
    async sendTransaction(tx: TransactionRequest): Promise<Hex> {
      if (!providerVat) {
        throw new Error('Provider not configured');
      }
      logger.debug('sendTransaction (direct path only)', {
        from: tx.from,
        to: tx.to,
        value: tx.value,
        hasBundlerConfig: Boolean(bundlerConfig),
      });
 
      // Direct (non-delegation) send: estimate missing gas fields
      const filledTx = { ...tx };
 
      filledTx.nonce ??= await E(providerVat).getNonce(filledTx.from);
      filledTx.chainId ??= await E(providerVat).getChainId();
      if (!filledTx.maxFeePerGas || !filledTx.maxPriorityFeePerGas) {
        const fees = await E(providerVat).getGasFees();
        filledTx.maxFeePerGas ??= fees.maxFeePerGas;
        filledTx.maxPriorityFeePerGas ??= fees.maxPriorityFeePerGas;
      }
      filledTx.gasLimit ??= applyGasBuffer(
        validateGasEstimate(
          await E(providerVat).request('eth_estimateGas', [
            {
              from: filledTx.from,
              to: filledTx.to,
              value: filledTx.value,
              data: filledTx.data,
            },
          ]),
        ),
        10,
      );
 
      const signedTx = await resolveTransactionSigning(filledTx);
      return E(providerVat).broadcastTransaction(signedTx);
    },
 
    /**
     * Pass a JSON-RPC method call through to the provider vat.
     *
     * @param method - The JSON-RPC method name.
     * @param params - Optional method parameters.
     * @returns The raw RPC response.
     */
    async request(method: string, params?: unknown[]): Promise<unknown> {
      if (!providerVat) {
        throw new Error('Provider not configured');
      }
      return E(providerVat).request(method, params);
    },
 
    /**
     * Look up a transaction by hash. Tries the bundler first (in case the
     * hash is a UserOp hash from delegation redemption), then falls back
     * to a regular `eth_getTransactionReceipt` RPC call.
     *
     * @param hash - A UserOp hash or regular tx hash.
     * @returns An object with `txHash` and `receipt`, or null if not found.
     */
    async getTransactionReceipt(hash: Hex): Promise<{
      txHash: Hex;
      userOpHash?: Hex;
      success: boolean;
    } | null> {
      if (!providerVat) {
        throw new Error('Provider not configured');
      }
 
      // Try bundler first (UserOp hash)
      if (bundlerConfig) {
        try {
          const userOpReceipt = (await E(providerVat).getUserOpReceipt({
            bundlerUrl: bundlerConfig.bundlerUrl,
            userOpHash: hash,
          })) as {
            success: boolean;
            receipt?: { transactionHash?: string };
          } | null;
 
          if (userOpReceipt?.receipt?.transactionHash) {
            return harden({
              txHash: userOpReceipt.receipt.transactionHash as Hex,
              userOpHash: hash,
              success: userOpReceipt.success,
            });
          }
        } catch (error) {
          // Not a UserOp hash — fall through to regular RPC
          logger.debug(
            'UserOp receipt lookup failed, trying regular RPC',
            error,
          );
        }
      }
 
      // Try regular tx receipt
      const receipt = (await E(providerVat).request(
        'eth_getTransactionReceipt',
        [hash],
      )) as { status?: string; transactionHash?: string } | null;
 
      if (receipt?.transactionHash) {
        return harden({
          txHash: receipt.transactionHash as Hex,
          success: receipt.status === '0x1',
        });
      }
 
      return null;
    },
 
    /**
     * Poll for a UserOp receipt until it appears or the timeout elapses.
     *
     * @param options - Polling options.
     * @param options.userOpHash - The UserOp hash returned by the bundler.
     * @param options.pollIntervalMs - Delay between polls in milliseconds.
     * @param options.timeoutMs - Maximum time to wait in milliseconds.
     * @returns The raw receipt object from the bundler.
     */
    async waitForUserOpReceipt(options: {
      userOpHash: Hex;
      pollIntervalMs?: number;
      timeoutMs?: number;
    }): Promise<unknown> {
      if (!providerVat || !bundlerConfig) {
        throw new Error('Provider and bundler must be configured');
      }
 
      if (
        typeof globalThis.Date?.now !== 'function' ||
        typeof globalThis.setTimeout !== 'function'
      ) {
        throw new Error(
          'waitForUserOpReceipt requires Date.now and setTimeout ' +
            '(not available in SES compartments without timer endowments)',
        );
      }
 
      const interval = options.pollIntervalMs ?? 2000;
      const timeout = options.timeoutMs ?? 60000;
      const start = Date.now();
 
      while (Date.now() - start < timeout) {
        const receipt = await E(providerVat).getUserOpReceipt({
          bundlerUrl: bundlerConfig.bundlerUrl,
          userOpHash: options.userOpHash,
        });
        if (receipt !== null) {
          return receipt;
        }
        await new Promise((resolve) => setTimeout(resolve, interval));
      }
      throw new Error(
        `UserOp ${options.userOpHash} not found after ${timeout}ms`,
      );
    },
 
    /**
     * Poll until a regular EIP-1559 transaction is mined.
     * Prefer `waitForUserOpReceipt` for ERC-4337 UserOp hashes.
     *
     * @param options - Polling options.
     * @param options.txHash - Transaction hash to wait for.
     * @param options.pollIntervalMs - Delay between RPC polls in milliseconds.
     * @param options.timeoutMs - Maximum time to wait in milliseconds.
     * @returns Whether the mined transaction succeeded (`status` 0x1).
     */
    async waitForTransactionReceipt(options: {
      txHash: Hex;
      pollIntervalMs?: number;
      timeoutMs?: number;
    }): Promise<{ success: boolean }> {
      return pollTransactionReceipt(options);
    },
 
    // ------------------------------------------------------------------
    // ERC-20 token utilities
    // ------------------------------------------------------------------
 
    /**
     * Query the ERC-20 token balance for an owner address.
     *
     * @param options - Query options.
     * @param options.token - Token contract address.
     * @param options.owner - Account address to query.
     * @returns The balance as a decimal string.
     */
    async getTokenBalance(options: {
      token: Address;
      owner: Address;
    }): Promise<string> {
      if (!providerVat) {
        throw new Error('Provider not configured');
      }
      const callData = encodeBalanceOf(options.owner);
      const result = await E(providerVat).request('eth_call', [
        { to: options.token, data: callData },
        'latest',
      ]);
      const validated = validateTokenCallResult(
        result,
        'balanceOf',
        options.token,
      );
      return decodeBalanceOfResult(validated).toString();
    },
 
    /**
     * Query the name, symbol, and decimals of an ERC-20 token.
     *
     * @param options - Query options.
     * @param options.token - Token contract address.
     * @returns An object with name, symbol, and decimals.
     */
    async getTokenMetadata(options: {
      token: Address;
    }): Promise<{ name: string; symbol: string; decimals: number }> {
      if (!providerVat) {
        throw new Error('Provider not configured');
      }
      const [nameSettled, symbolSettled, decimalsSettled] =
        await Promise.allSettled([
          E(providerVat).request('eth_call', [
            { to: options.token, data: encodeName() },
            'latest',
          ]),
          E(providerVat).request('eth_call', [
            { to: options.token, data: encodeSymbol() },
            'latest',
          ]),
          E(providerVat).request('eth_call', [
            { to: options.token, data: encodeDecimals() },
            'latest',
          ]),
        ]);
 
      // decimals is mandatory — wrong decimals causes financial errors
      if (decimalsSettled.status === 'rejected') {
        throw new Error(
          `decimals() call failed for token ${options.token}: ${
            decimalsSettled.reason instanceof Error
              ? decimalsSettled.reason.message
              : String(decimalsSettled.reason)
          }`,
        );
      }
 
      // name and symbol are optional in ERC-20; fall back gracefully
      let name = 'Unknown';
      if (nameSettled.status === 'fulfilled') {
        try {
          name = decodeNameResult(
            validateTokenCallResult(nameSettled.value, 'name', options.token),
          );
        } catch {
          // name() not implemented or returned invalid data
        }
      }
 
      let symbol = 'Unknown';
      if (symbolSettled.status === 'fulfilled') {
        try {
          symbol = decodeSymbolResult(
            validateTokenCallResult(
              symbolSettled.value,
              'symbol',
              options.token,
            ),
          );
        } catch {
          // symbol() not implemented or returned invalid data
        }
      }
 
      return harden({
        name,
        symbol,
        decimals: decodeDecimalsResult(
          validateTokenCallResult(
            decimalsSettled.value,
            'decimals',
            options.token,
          ),
        ),
      });
    },
 
    // ------------------------------------------------------------------
    // Semantic wallet API
    // ------------------------------------------------------------------
 
    /**
     * Transfer native ETH.
     * Tries each matching delegation twin in order; the first success is
     * returned. If all matched twins fail, throws with all collected errors
     * as the cause array; does not fall through to the home section.
     *
     * @param to - Recipient address.
     * @param amount - Amount in wei.
     * @returns The transaction hash.
     */
    async transferNative(
      to: Address,
      amount: string | number | bigint,
    ): Promise<Hex> {
      // Coerce at the JSON boundary — CLI callers pass numeric strings.
      const amt = BigInt(amount);
      const matching = delegationSections.filter(
        (sec) => sec.method === 'transferNative',
      );
      if (matching.length > 0) {
        const errors: unknown[] = [];
        for (const section of matching) {
          try {
            return await E(section.exo).transferNative(to, amt);
          } catch (error) {
            errors.push(error);
          }
        }
        throw new Error('All delegation twins failed', { cause: errors });
      }
      if (homeSection) {
        return E(homeSection).transferNative(to, amt);
      }
      throw new Error(
        'No routing available — call connectToPeer first or receive a delegation',
      );
    },
 
    /**
     * Transfer ERC-20 tokens.
     * Tries each matching delegation twin in order; the first success is
     * returned. If all matched twins fail, throws with all collected errors
     * as the cause array; does not fall through to the home section.
     *
     * @param token - ERC-20 token contract address.
     * @param to - Recipient address.
     * @param amount - Amount in token units.
     * @returns The transaction hash.
     */
    async transferFungible(
      token: Address,
      to: Address,
      amount: string | number | bigint,
    ): Promise<Hex> {
      // Coerce at the JSON boundary — CLI callers pass numeric strings.
      const amt = BigInt(amount);
      const tokenLower = token.toLowerCase() as Address;
      const matching = delegationSections.filter(
        (sec) => sec.method === 'transferFungible' && sec.token === tokenLower,
      );
      if (matching.length > 0) {
        const errors: unknown[] = [];
        for (const section of matching) {
          try {
            return await E(section.exo).transferFungible(tokenLower, to, amt);
          } catch (error) {
            errors.push(error);
          }
        }
        throw new Error('All delegation twins failed', { cause: errors });
      }
      if (homeSection) {
        return E(homeSection).transferFungible(token, to, amt);
      }
      throw new Error(
        'No routing available — call connectToPeer first or receive a delegation',
      );
    },
 
    /**
     * Receive a delegation grant from home and persist it to the redeemer vat.
     * Rebuilds the delegation sections to incorporate the new grant.
     *
     * @param grant - The semantic delegation grant to store.
     */
    async receiveDelegation(grant: DelegationGrant): Promise<void> {
      if (!redeemerVat) {
        throw new Error('Redeemer vat not available');
      }
      await E(redeemerVat).receiveGrant(grant);
      await rebuildRouting();
    },
 
    /**
     * List all delegation grants stored in the redeemer vat.
     *
     * @returns An array of all DelegationGrant objects received on this device.
     */
    async listGrants(): Promise<DelegationGrant[]> {
      if (!redeemerVat) {
        return [];
      }
      return E(redeemerVat).listGrants();
    },
 
    /**
     * Connect to the home coordinator via an OCAP URL.
     * Redeems the URL to obtain a remote reference to the home coordinator,
     * then fetches the home section exo for the call-home fallback path.
     * Persists homeCoordRef and homeSection and rebuilds routing.
     *
     * @param ocapUrl - The OCAP URL issued by the home coordinator.
     */
    async connectToPeer(ocapUrl: string): Promise<void> {
      if (!redemptionService) {
        throw new Error('OCAP URL redemption service not available');
      }
      homeCoordRef = await E(redemptionService).redeem(ocapUrl);
      homeSection = await E(homeCoordRef).getHomeSection();
      persistBaggage('homeCoordRef', homeCoordRef);
      persistBaggage('homeSection', homeSection);
      await rebuildRouting();
    },
 
    /**
     * Refresh cached peer accounts from the home coordinator.
     * No-op if no home coordinator connection has been established.
     */
    async refreshPeerAccounts(): Promise<void> {
      if (!homeCoordRef) {
        return;
      }
      await E(homeCoordRef).getAccounts();
    },
 
    /**
     * Register this device's delegate address on the home coordinator.
     * Called after connecting to the peer to allow the home coordinator to
     * record the away wallet's on-chain delegate address.
     *
     * @param address - The away wallet's delegate address (0x-prefixed).
     */
    async sendDelegateAddressToPeer(address: string): Promise<void> {
      if (!homeCoordRef) {
        throw new Error('Not connected to a peer — call connectToPeer first');
      }
      await E(homeCoordRef).registerDelegateAddress(address);
    },
 
    // ------------------------------------------------------------------
    // Introspection
    // ------------------------------------------------------------------
 
    /**
     * Return a summary of the away coordinator's current capabilities.
     * Reflects away-side state: local key, bundler, smart account, grants
     * count (from redeemerVat), and whether homeSection is wired.
     *
     * @returns A WalletCapabilities object describing current state.
     */
    async getCapabilities(): Promise<WalletCapabilities> {
      const hasLocalKeys = keyringVat ? await E(keyringVat).hasKeys() : false;
      const localAccounts: Address[] = keyringVat
        ? await E(keyringVat).getAccounts()
        : [];
      const grants = redeemerVat ? await E(redeemerVat).listGrants() : [];
 
      let signingMode = 'none';
      if (externalSigner) {
        signingMode = 'external:metamask';
      } else if (hasLocalKeys) {
        signingMode = 'local';
      }
 
      let autonomy = 'no signing authority';
      if (grants.length > 0) {
        // Describe any native ETH amount limits in human-readable form.
        const limitParts: string[] = grants
          .filter(
            (grant) =>
              grant.method === 'transferNative' &&
              grant.maxAmount !== undefined,
          )
          .map(
            (grant) =>
              `max ${weiToEth(`0x${BigInt(grant.maxAmount ?? 0n).toString(16)}`)} per tx`,
          );
        const limitSuffix =
          limitParts.length > 0 ? ` (${limitParts.join('; ')})` : '';
        autonomy = `autonomous via ${grants.length} delegation(s)${limitSuffix}`;
      } else if (homeSection) {
        autonomy = 'call-home (no delegations)';
      }
 
      let capabilityChainId: number | undefined;
      try {
        capabilityChainId = await resolveChainId();
      } catch {
        // ignore — chain ID may not be configured yet
      }
 
      return harden({
        hasLocalKeys,
        localAccounts,
        delegationCount: grants.length,
        hasPeerWallet: homeSection !== undefined,
        hasExternalSigner: externalSigner !== undefined,
        hasBundlerConfig: bundlerConfig !== undefined,
        smartAccountAddress: smartAccountConfig?.address,
        chainId: capabilityChainId,
        signingMode,
        autonomy,
      });
    },
  });
 
  return awayCoordinator;
}