# Handoff — Ark / bridge / NWC work Working note, not project docs. Delete when it's stale. Persistent memory for all of this lives in `~/.claude/projects/-home-adam-coinosv3/memory/` — start by reading `MEMORY.md`, then `hal-nwc`, `coinos-ark-mainnet`, `ark-swap-bridge`, `coinos-ark-asp`, `ark-third-party-htlc`. --- ## 1. ~~THE ONE UNRESOLVED BUG~~ — **RESOLVED 2026-08-01** **Root cause:** nostr-tools ≥2.23 changed `SimplePool.subscribeMany` to take a SINGLE filter object instead of an array. Our `[filter]` wrapping serialized as `["REQ",id,[{...}]]` — invalid NIP-01 — and every relay answered `["CLOSED","sub:N","ERROR: bad req: provided filter is not an object"]`, then silently dropped the sub. Neither guard nor decrypt was ever reached; there was simply no live subscription. The CDP probe's own harness had the same bug, which is why it "received frames" but could never observe a 23195. Fixed in `src/nostr.js` (`subscribeOn` + `NostrSync.subscribeEvents` — device sync's live subscription was broken too) and `nwcpush/server.js` (deployed to cs, restarted, resubscribed OK). Verified end-to-end: a real nip04 `get_balance` published from the stored connection's client key got back `{"result_type":"get_balance","result":{"balance":20000000}}` over public relays. Instrumentation (guard logging + sub lifecycle gens) is still in `nwc.js` — strip once Amethyst is confirmed. **Remaining human step:** paste the existing connection string into Amethyst again — it should now show a balance. **LAYER 3 (2026-08-01 evening — the zap):** NWC now round-trips; the zap `pay_invoice` failed for stacked reasons, all found: (a) the swap bridge defaults OFF in hal (`BRIDGE_DEFAULT.mainnet='off'`) so pays went to the ASP, whose CLN routing budget = 90% of the ASP's 1-sat min fee = 0 sats → unroutable; (b) once enabled, the bridge needed its bearer token (cs `~/arkswap/config-main.json` `tokens[0]`, now set in the laptop wallet) and (c) its state dir `cs:~/arkswap/data-main` had been deleted → dangling mount, 500s (recreated + container restarted); (d) with all that fixed the 21-sat zap STILL fails: the recipient is a private node behind Megalithic.me and is offline — even manual `xpay` with 10-sat maxfee dies on temporary_channel_failure everywhere. Not our infra. Refund machinery worked. ALSO fixed: pool ping+reconnect (silent socket death used to kill sync + NWC subs until reload — that's why a phone-created connection stayed invisible to the open laptop tab). RESOLVED SAME EVENING (Adam approved): ASP min_fee_sat 1→3 (captaind-main restarted, healthy); bridge default ON for mainnet (commit 0344733) and bridge opened — tokens cleared in `cs:~/arkswap/config-main.json`. Still open: phone PWA needs two loads to get new builds. **LAYER 2 (found after the filter fix — Amethyst still spun):** relay.coinos.io's `write-policy.pl` (strfry `sf` on desk, `~/coinos-server/data/strfry/`, gitignored) restricted kinds 13194/23195 to coinos server pubkeys, so every reply hal published there was rejected — and Amethyst listens for replies ONLY on the first URI relay, which is relay.coinos.io. The rejections also strike-counted the laptop IP into a temp-ban. Fixed 2026-08-01: NWC kinds opened to all pubkeys on the relay (rate limits intact), `publishOn` now logs refused publishes, 13194 goes out once per session per connection. Amethyst-shaped probe (coinos-only, #e+#p filter) round-trips get_balance green on the deployed build.
Original investigation notes (superseded) **Symptom:** pasting a hal NWC connection string into Amethyst shows a spinner forever. No balance, no error. **This is narrowed, not mysterious. Do not re-derive the following — all of it was verified against the live laptop tab over Chrome DevTools Protocol:** - hal DOES subscribe correctly, on all three NWC relays. Captured from its own socket: `["REQ","sub:1",[{"kinds":[23194],"#p":[""],"since":}]]` - The stored connection's keypairs are self-consistent: `getPublicKey(secret) === clientPk` and `getPublicKey(serviceSk) === servicePk`. So the string Amethyst holds is valid. - hal's sockets **did receive 23194 frames** (6 — one per relay) in one run. - No 23195 reply is ever produced. - The decrypt path is now instrumented (logs + Settings card + replies with an INTERNAL error in both schemes). A fresh probe produced **no log, no UI error, no reply** — so execution isn't reaching decrypt. **Therefore the drop is in one of the silent guards before decrypt**, in `onRequest` in `src/features/nwc.js`: ```js if (handled.has(ev.id)) return; // replay set if (nowSec() - ev.created_at > MAX_AGE_SEC) return; // 120s age check if (ev.pubkey !== c.clientPk) return; // wrong client ``` …or `onRequest` isn't being invoked at all. **Second suspect, untested:** `subscribeOn` reuses the shared `pool` in `src/nostr.js`, and `listen()` is called from three places — feature `init()`, `createConn()`, and the sync-snapshot `load()`. Each call does `stop()` then re-subscribes. If a later `listen()` closes the subscription whose callback would have fired, frames keep arriving on the socket with nothing handling them. That fits every observation. **Next step (one cycle should name it):** log at the TOP of `onRequest` — every event id seen and which guard returns — plus subscription lifecycle (each `listen()`/`stop()` and how many subs are live). Then re-run the probe below. ### The probe (copy-paste, needs Chrome with `--remote-debugging-port=9222` and a hal tab open) ```js // bun run this from ~/coinosv3 import puppeteer from 'puppeteer-core'; import { SimplePool } from 'nostr-tools/pool'; import { nip04, finalizeEvent } from './src/nostr.js'; import { hex } from '@scure/base'; const b = await puppeteer.connect({ browserURL: 'http://127.0.0.1:9222', defaultViewport: null }); const p = (await b.pages()).find(x => x.url().includes('halwallet.app')); p.on('console', (m) => { const t = m.text(); if (/nwc/i.test(t)) console.log('PAGE:', t.slice(0, 240)); }); // watch hal's own relay traffic const cdp = await p.createCDPSession(); await cdp.send('Network.enable'); let got23194 = 0; cdp.on('Network.webSocketFrameSent', (e) => { if (e.response.payloadData.startsWith('["REQ"')) console.log('SUB:', e.response.payloadData.slice(0, 200)); }); cdp.on('Network.webSocketFrameReceived', (e) => { if (e.response.payloadData.includes('23194')) got23194++; }); await p.reload({ waitUntil: 'networkidle2' }); await new Promise(r => setTimeout(r, 8000)); const c = await p.evaluate(() => { const k = Object.keys(localStorage).find(x => x.endsWith(':nwc')); return (JSON.parse(localStorage.getItem(k)).conns || []).filter(y => !y.revoked)[0]; }); const RELAYS = ['wss://relay.coinos.io', 'wss://relay.damus.io', 'wss://nos.lol']; const pool = new SimplePool(); const cSk = hex.decode(c.secret); const now = Math.floor(Date.now() / 1000); let reply = null; const sub = pool.subscribeMany(RELAYS, [{ kinds: [23195], '#p': [c.clientPk], since: now - 10 }], { onevent: async (ev) => { if (!reply) reply = await nip04.decrypt(cSk, c.servicePk, ev.content).catch(e => '(undecryptable: ' + e.message + ')'); }, }); await new Promise(r => setTimeout(r, 2500)); const req = finalizeEvent({ kind: 23194, created_at: now, tags: [['p', c.servicePk]], content: await nip04.encrypt(cSk, c.servicePk, JSON.stringify({ method: 'get_balance', params: {} })) }, cSk); await Promise.allSettled(pool.publish(RELAYS, req)); for (let i = 0; i < 40 && !reply; i++) await new Promise(r => setTimeout(r, 500)); console.log('hal received 23194 frames:', got23194); console.log('reply:', reply || 'NONE'); sub.close(); pool.close(RELAYS); await b.disconnect(); ``` Also worth trying, as a cheap independent signal: is it Amethyst-specific? Test the same connection string with Alby Go or a NIP-47 CLI. If those work, the bug is in how Amethyst reads the URI, not in hal's handler.
--- ## 2. WHAT IS LIVE | Thing | Where | Notes | |---|---|---| | Mainnet ASP | `https://ark.coinos.io` | patched captaind, 5M sat funded, 500k vtxo cap, 1h rounds, CLN attached | | Mutinynet ASP | `https://ark-staging.coinos.io` | 30s rounds, free boards | | Mainnet bridge | `https://arkswap.coinos.io` | max(2 sat, 2000ppm), shares boltz's CLN | | Mutinynet bridge | `https://arkswap-staging.coinos.io` | max(1 sat, 1000ppm) | | NWC push notifier | `https://nwcpush.coinos.io` | `/health`, `/vapid`, `POST|DELETE /register` | Containers on **cs**: `captaind-main`, `ark-pg-main`, `captaind-mut`, `ark-pg-mut`, `arkswap-main`, `arkswap-mut`, `nwcpush`. Configs in `~/ark/` and `~/arkswap/`. Backups: `~/backup-ark.sh`, hourly at :17, mirrored to `desk:/home/adam/backups/ark-cs/`, restore-tested. **Machines:** `lap` = this laptop (its `cl`/`clb`/`clc`/`bc` containers are a **regtest** dev stack, despite the "coinos" alias). `desk` = prod box (mainnet bitcoind :8332, mainnet CLN `cl`, `~/scripts/solvency-monitor.ts`). `cs` = coinos server (everything above + cloudflared tunnels). --- ## 3. LANDMINES THAT COST TIME 1. **build.js has its own feature registry.** It swaps `src/features/index.js` for a generated module from `ALL_FEATURES`. Adding a feature to index.js alone ships its i18n strings but NOT its code, so it looks deployed and silently does nothing. Guard: `bun tools/feature-registry-check.js`. **Grep the built bundle for feature code, not just a green build.** 2. **Cloudflare challenges CORS preflights.** Any browser POST with `content-type: application/json` preflights, and the managed WAF 403s the OPTIONS → the browser reports a bare "Failed to fetch". Bit us three times. One skip rule covers it: **`d3ee4d654c1b4e3eb646e14bfe8d76d1`** in ruleset `48c017dca49b40ea897cd2ed5185fa11`, zone `cc9b01de29250639d6b0b733e19cefda`. **Any new coinos hostname a browser POSTs to must be added.** curl will not reproduce it — curl doesn't preflight. Creds: `~/.cf_global_key` (line 1 email, line 2 global key; use `X-Auth-Email` + `X-Auth-Key`). 3. **Android Chrome freezes backgrounded PWAs** and closes their sockets. A phone with hal "open" in the background answers nothing. Desktop keeps background sockets alive; mobile does not. 4. **captaind + CLN TLS:** address it as `https://cln:9736` / `https://hold:9737`, never `bcln` — the certs' SANs are `cln`/`hold`/`localhost`, and a mismatch shows only as an endless "Trying to connect to offline node". 5. **Board outputs must be vout 0.** `bitcoin-cli sendtoaddress` randomizes output order; use `~/ark/fund-board.sh` (pins change to position 1). 6. **Service-worker staleness:** the PWA often needs two loads to pick up a deploy. When verifying in a live tab, check `caches.keys()` matches the deployed `cold-*` hash before concluding anything. --- ## 4. OTHER OPEN WORK - **NWC step 3, remaining half:** the SW currently only *wakes* the user (notification → tap → hal answers). Auto-answering needs the Ark spend stack (musig2, arkoor build, gRPC-web) bundled into the service worker — a second build entry point — plus the pouch funding UI. `src/nwc-pouch.js` (chain-6 keys in IndexedDB, seed stays in localStorage) is built and tested; nothing funds it yet. - **Upstream:** issue filed at `https://gitlab.com/ark-bitcoin/bark/-/work_items/1388`, no reply yet. Draft MR sits in the fork at `asoltys/bark!1`, branch `third-party-htlc`, rebased and green. Once the live demo works, adding it to the issue would strengthen the case. Personal GitLab token: `~/.gitlab_pat`. - **Monitoring:** nothing watches the mainnet ASP's rounds wallet or bcln's outbound. Was going to the agent on desk (`~/scripts/solvency-monitor.ts`). Suggested alerts: rounds wallet under ~1M of 5M; bcln outbound low. - **bcln is shared** by boltz, the ark bridge, and now the ASP. A dedicated CLN is the clean fix if volume grows. - **Liquidity model** (verified in bark's source): ASP working capital ≈ total user balance under management, locked ~30 days per cycle. Boards and arkoor sends cost the ASP nothing; rounds and offboards front the full value. 5M sat comfortably supports ~3–4M of user balances. --- ## 5. TEST COMMANDS ``` bun tools/feature-registry-check.js # features ship (membership + order) bun tools/nwc-test.js # NWC protocol, budgets, sync merge bun tools/nwc-pouch-test.js # pouch key isolation bun tools/ark/manager-test.js # ark regression (needs regtest stack) bun tools/ark/ln-e2e-test.js # ark<->LN over the ASP bun tools/ark/bridge-e2e-test.js # trustless bridge, regtest bun tools/ark/htlc-bridge-test.js # third-party HTLC + attack rejections bun tools/ark/live-bridge-demo.js # live mutinynet demo (public infra) ``` Regtest stack: `docker start ark-postgres ark-electrs captaind cln-ark` (plain `docker start`, not compose — those services aren't in the compose file). 'n203' href='#n203'>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
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182











































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                                    






                                                                                                  


















































































































































































































































































































































































































































































































































































































                                                                                                             
// MLS (RFC 9420) — the group key agreement under Marmot / White Noise
// https://www.rfc-editor.org/rfc/rfc9420.html
//
// One ciphersuite (0x0001: X25519 / AES-128-GCM / SHA-256 / Ed25519), basic
// credentials, and the draft-ietf-mls-extensions-10 pieces Marmot needs:
// app_data_dictionary, app_data_update, app_ephemeral, self_remove. Protocol-pure
// and synchronous: no DOM, no app state, no storage.
//
// States are plain data (objects, arrays, numbers, strings, Uint8Array) and are
// never mutated: every call returns a new state, so the caller can keep old
// epochs and candidate branches around. Where ts-mls and OpenMLS disagree the
// code follows OpenMLS, because that is what the other clients run.
//
// Decoded but refused: reinit, external_init / external commits, external and
// new_member senders, reinit and branch PSKs. We never send Update proposals
// (a commit rotates our leaf) but apply other members'.

import { ed25519, x25519 } from "@noble/curves/ed25519";
import { sha256 } from "@noble/hashes/sha256";
import { hmac } from "@noble/hashes/hmac";
import { extract, expand } from "@noble/hashes/hkdf";
import { bytesToHex, randomBytes } from "@noble/hashes/utils";
import { gcm } from "@noble/ciphers/aes.js";

export const EXT = {
  APPLICATION_ID: 1,
  RATCHET_TREE: 2,
  REQUIRED_CAPABILITIES: 3,
  EXTERNAL_PUB: 4,
  EXTERNAL_SENDERS: 5,
  APP_DATA_DICTIONARY: 6,
};

export const PROPOSAL = {
  ADD: 1,
  UPDATE: 2,
  REMOVE: 3,
  PSK: 4,
  REINIT: 5,
  EXTERNAL_INIT: 6,
  GROUP_CONTEXT_EXTENSIONS: 7,
  APP_DATA_UPDATE: 8,
  APP_EPHEMERAL: 9,
  SELF_REMOVE: 10,
};

export const LEAF_SOURCE = { KEY_PACKAGE: 1, UPDATE: 2, COMMIT: 3 };

const WIRE = { PUBLIC: 1, PRIVATE: 2, WELCOME: 3, GROUP_INFO: 4, KEY_PACKAGE: 5 };
const WIRE_NAME = [undefined, "public", "private", "welcome", "group_info", "key_package"];
const CONTENT = { APPLICATION: 1, PROPOSAL: 2, COMMIT: 3 };
const CONTENT_NAME = [undefined, "application", "proposal", "commit"];
const SENDER = { MEMBER: 1, EXTERNAL: 2, NEW_MEMBER_PROPOSAL: 3, NEW_MEMBER_COMMIT: 4 };

const EMPTY = new Uint8Array(0);
const ZERO32 = new Uint8Array(32);
// how far a sender's ratchet may jump ahead, and how many skipped keys we keep per ratchet
const MAX_SKIP = 1000;

const utf8 = (s) => new TextEncoder().encode(s);
const hex = bytesToHex;

const fail = (msg, code) => {
  let e = new Error("mls: " + msg);
  if (code) e.code = code;
  throw e;
};

const concat = (...arrs) => {
  let out = new Uint8Array(arrs.reduce((n, a) => n + a.length, 0));
  let o = 0;
  for (let a of arrs) (out.set(a, o), (o += a.length));
  return out;
};

const eq = (a, b) => {
  if (a.length !== b.length) return false;
  let d = 0;
  for (let i = 0; i < a.length; i++) d |= a[i] ^ b[i];
  return d === 0;
};

// ---- TLS presentation language, with RFC 9420 §2.1.2 variable-length vectors ----

export class Writer {
  constructor() {
    this.parts = [];
  }
  bytes(b) {
    this.parts.push(b);
    return this;
  }
  uint(n, size) {
    if (!Number.isInteger(n) || n < 0 || n >= 2 ** (8 * size))
      fail(`uint${8 * size} out of range: ${n}`);
    let b = new Uint8Array(size);
    for (let i = size - 1; i >= 0; i--) ((b[i] = n % 256), (n = Math.floor(n / 256)));
    return this.bytes(b);
  }
  u8(n) {
    return this.uint(n, 1);
  }
  u16(n) {
    return this.uint(n, 2);
  }
  u32(n) {
    return this.uint(n, 4);
  }
  // numbers past 2^53 travel as decimal strings (see Reader.u64)
  u64(n) {
    let v = BigInt(n);
    if (v < 0n || v >> 64n) fail("uint64 out of range: " + n);
    return this.u32(Number(v >> 32n)).u32(Number(v & 0xffffffffn));
  }
  varint(n) {
    if (n < 64) return this.u8(n);
    if (n < 16384) return this.u16(n | 0x4000);
    if (n < 0x40000000) return this.u32(n + 0x80000000);
    return fail("vector too long");
  }
  vec(b) {
    return this.varint(b.length).bytes(b);
  }
  list(items, fn) {
    let w = new Writer();
    items.forEach((item, i) => fn(w, item, i));
    return this.vec(w.finish());
  }
  opt(v, fn) {
    if (v == null) return this.u8(0);
    fn(this.u8(1), v);
    return this;
  }
  finish() {
    return concat(...this.parts);
  }
}

export class Reader {
  constructor(buf, pos = 0, end = buf.length) {
    this.buf = buf;
    this.pos = pos;
    this.end = end;
  }
  need(n) {
    if (n < 0 || this.pos + n > this.end) fail("truncated input");
    let p = this.pos;
    this.pos += n;
    return p;
  }
  u8() {
    return this.buf[this.need(1)];
  }
  u16() {
    let p = this.need(2);
    return (this.buf[p] << 8) | this.buf[p + 1];
  }
  u32() {
    let p = this.need(4);
    return (
      this.buf[p] * 0x1000000 + ((this.buf[p + 1] << 16) | (this.buf[p + 2] << 8) | this.buf[p + 3])
    );
  }
  // a JS number while it is exact, a decimal string beyond that — never a BigInt,
  // so decoded structures stay structured-clone friendly and still re-encode exactly
  u64() {
    let hi = this.u32();
    let lo = this.u32();
    return hi < 0x200000 ? hi * 0x100000000 + lo : ((BigInt(hi) << 32n) | BigInt(lo)).toString();
  }
  // copies: a view would pin (and structured-clone) the whole message buffer
  bytes(n) {
    let p = this.need(n);
    return this.buf.slice(p, p + n);
  }
  varint() {
    let b = this.u8();
    let prefix = b >> 6;
    if (prefix === 3) fail("invalid vector length");
    let n = b & 0x3f;
    for (let i = (1 << prefix) - 1; i > 0; i--) n = n * 256 + this.u8();
    if ((prefix === 1 && n < 64) || (prefix === 2 && n < 16384))
      fail("vector length not in shortest form");
    return n;
  }
  vec() {
    return this.bytes(this.varint());
  }
  list(fn) {
    let n = this.varint();
    let sub = new Reader(this.buf, this.need(n), this.pos);
    let out = [];
    while (sub.pos < sub.end) out.push(fn(sub, out.length));
    return out;
  }
  opt(fn) {
    let b = this.u8();
    if (b > 1) fail("invalid optional");
    return b ? fn(this) : null;
  }
  done() {
    if (this.pos !== this.end) fail("trailing bytes");
  }
}

const enc = (fn, v, ...rest) => fn(new Writer(), v, ...rest).finish();
const dec = (fn, bytes) => {
  let r = new Reader(bytes);
  let v = fn(r);
  r.done();
  return v;
};

// ---- ciphersuite 0x0001 ----

const mac = (key, data) => hmac(sha256, key, data);

const expandWithLabel = (secret, label, context, length) =>
  expand(
    sha256,
    secret,
    new Writer()
      .u16(length)
      .vec(utf8("MLS 1.0 " + label))
      .vec(context)
      .finish(),
    length,
  );
const deriveSecret = (secret, label) => expandWithLabel(secret, label, EMPTY, 32);
const deriveTreeSecret = (secret, label, generation, length) =>
  expandWithLabel(secret, label, new Writer().u32(generation).finish(), length);
const refHash = (label, value) => sha256(new Writer().vec(utf8(label)).vec(value).finish());

const labeled = (label, content) =>
  new Writer()
    .vec(utf8("MLS 1.0 " + label))
    .vec(content)
    .finish();
// Ed25519 private keys are the 32-byte seed; some libraries hand out seed || public
const signWithLabel = (priv, label, content) =>
  ed25519.sign(labeled(label, content), priv.subarray(0, 32));
const verifyWithLabel = (pub, label, content, signature) => {
  try {
    return ed25519.verify(signature, labeled(label, content), pub);
  } catch {
    return false;
  }
};

// HPKE (RFC 9180), base mode, DHKEM(X25519, HKDF-SHA256) / HKDF-SHA256 / AES-128-GCM
const KEM_SUITE = concat(utf8("KEM"), Uint8Array.of(0, 0x20));
const HPKE_SUITE = concat(utf8("HPKE"), Uint8Array.of(0, 0x20, 0, 1, 0, 1));
const HPKE_V1 = utf8("HPKE-v1");

const labeledExtract = (suite, salt, label, ikm) =>
  extract(sha256, concat(HPKE_V1, suite, utf8(label), ikm), salt);
const labeledExpand = (suite, prk, label, info, length) =>
  expand(
    sha256,
    prk,
    concat(Uint8Array.of(length >> 8, length & 255), HPKE_V1, suite, utf8(label), info),
    length,
  );

const deriveKeyPair = (ikm) => {
  let priv = labeledExpand(
    KEM_SUITE,
    labeledExtract(KEM_SUITE, EMPTY, "dkp_prk", ikm),
    "sk",
    EMPTY,
    32,
  );
  return { priv, pub: x25519.getPublicKey(priv) };
};

const hpkeKey = (dh, kemOutput, pub, info) => {
  let eae = labeledExtract(KEM_SUITE, EMPTY, "eae_prk", dh);
  let shared = labeledExpand(KEM_SUITE, eae, "shared_secret", concat(kemOutput, pub), 32);
  let context = concat(
    Uint8Array.of(0),
    labeledExtract(HPKE_SUITE, EMPTY, "psk_id_hash", EMPTY),
    labeledExtract(HPKE_SUITE, EMPTY, "info_hash", info),
  );
  let secret = labeledExtract(HPKE_SUITE, shared, "secret", EMPTY);
  return {
    key: labeledExpand(HPKE_SUITE, secret, "key", context, 16),
    nonce: labeledExpand(HPKE_SUITE, secret, "base_nonce", context, 12),
  };
};

const hpkeSeal = (pub, info, aad, plaintext) => {
  let eph = randomBytes(32);
  let kemOutput = x25519.getPublicKey(eph);
  let { key, nonce } = hpkeKey(x25519.getSharedSecret(eph, pub), kemOutput, pub, info);
  return { kemOutput, ciphertext: gcm(key, nonce, aad).encrypt(plaintext) };
};

const hpkeOpen = (priv, kemOutput, info, aad, ciphertext) => {
  let { key, nonce } = hpkeKey(
    x25519.getSharedSecret(priv, kemOutput),
    kemOutput,
    x25519.getPublicKey(priv),
    info,
  );
  return gcm(key, nonce, aad).decrypt(ciphertext);
};

const encryptWithLabel = (pub, label, context, plaintext) =>
  hpkeSeal(pub, labeled(label, context), EMPTY, plaintext);
const decryptWithLabel = (priv, label, context, kemOutput, ciphertext) =>
  hpkeOpen(priv, kemOutput, labeled(label, context), EMPTY, ciphertext);

// ---- structures ----

const wU16s = (w, list) => w.list(list, (w, n) => w.u16(n));
const rU16s = (r) => r.list((r) => r.u16());

// extensions stay { type, data }: unknown and GREASE types round-trip untouched
const wExts = (w, exts) => w.list(exts, (w, e) => w.u16(e.type).vec(e.data));
const rExts = (r) => r.list((r) => ({ type: r.u16(), data: r.vec() }));

const CAPS = ["versions", "ciphersuites", "extensions", "proposals", "credentials"];
const wCaps = (w, c) => CAPS.reduce((w, k) => wU16s(w, c[k]), w);
const rCaps = (r) => Object.fromEntries(CAPS.map((k) => [k, rU16s(r)]));

const wLeafBody = (w, l) => {
  w.vec(l.encryptionKey).vec(l.signatureKey).u16(l.credential.type).vec(l.credential.identity);
  wCaps(w, l.capabilities).u8(l.source);
  if (l.source === LEAF_SOURCE.KEY_PACKAGE) w.u64(l.lifetime.notBefore).u64(l.lifetime.notAfter);
  if (l.source === LEAF_SOURCE.COMMIT) w.vec(l.parentHash);
  return wExts(w, l.extensions);
};
const wLeaf = (w, l) => wLeafBody(w, l).vec(l.signature);
const rLeaf = (r) => {
  // every credential is type + one vector on the wire (basic: identity, x509: the chain),
  // so unknown kinds decode as opaque; only basic is accepted as a member
  let l = {
    encryptionKey: r.vec(),
    signatureKey: r.vec(),
    credential: { type: r.u16(), identity: r.vec() },
  };
  l.capabilities = rCaps(r);
  l.source = r.u8();
  if (l.source === LEAF_SOURCE.KEY_PACKAGE) l.lifetime = { notBefore: r.u64(), notAfter: r.u64() };
  else if (l.source === LEAF_SOURCE.COMMIT) l.parentHash = r.vec();
  else if (l.source !== LEAF_SOURCE.UPDATE) fail("unknown leaf node source " + l.source);
  l.extensions = rExts(r);
  l.signature = r.vec();
  return l;
};
const leafTBS = (l, groupId, leafIndex) => {
  let w = wLeafBody(new Writer(), l);
  if (l.source !== LEAF_SOURCE.KEY_PACKAGE) w.vec(groupId).u32(leafIndex);
  return w.finish();
};
const signLeaf = (l, priv, groupId, leafIndex) => ({
  ...l,
  signature: signWithLabel(priv, "LeafNodeTBS", leafTBS(l, groupId, leafIndex)),
});

const wKeyPackageBody = (w, k) =>
  wExts(wLeaf(w.u16(k.version).u16(k.cipherSuite).vec(k.initKey), k.leaf), k.extensions);
const wKeyPackage = (w, k) => wKeyPackageBody(w, k).vec(k.signature);
const rKeyPackage = (r) => ({
  version: r.u16(),
  cipherSuite: r.u16(),
  initKey: r.vec(),
  leaf: rLeaf(r),
  extensions: rExts(r),
  signature: r.vec(),
});
const keyPackageRef = (k) => refHash("MLS 1.0 KeyPackage Reference", enc(wKeyPackage, k));

const wParent = (w, p) =>
  w
    .vec(p.encryptionKey)
    .vec(p.parentHash)
    .list(p.unmergedLeaves, (w, n) => w.u32(n));
const rParent = (r) => ({
  encryptionKey: r.vec(),
  parentHash: r.vec(),
  unmergedLeaves: r.list((r) => r.u32()),
});

// a ratchet tree is an array over node indices: leaves at even slots, parents at
// odd ones, null for blank; always 2^k - 1 wide
const wTree = (w, tree) => {
  let n = tree.length;
  while (n && !tree[n - 1]) n--;
  return w.list(tree.slice(0, n), (w, node, i) =>
    w.opt(node, (w, node) => (i % 2 ? wParent(w.u8(2), node) : wLeaf(w.u8(1), node))),
  );
};
const rTree = (r) => {
  let tree = r.list((r, i) =>
    r.opt((r) => {
      if (r.u8() !== (i % 2 ? 2 : 1)) fail("ratchet tree: wrong node type at index " + i);
      return i % 2 ? rParent(r) : rLeaf(r);
    }),
  );
  if (!tree.length || !tree[tree.length - 1]) fail("ratchet tree must end in a non-blank node");
  let width = 1;
  while (width < tree.length) width = 2 * width + 1;
  while (tree.length < width) tree.push(null);
  return tree;
};

const wContext = (w, c) =>
  wExts(
    w
      .u16(c.version)
      .u16(c.cipherSuite)
      .vec(c.groupId)
      .u64(c.epoch)
      .vec(c.treeHash)
      .vec(c.confirmedTranscriptHash),
    c.extensions,
  );
const rContext = (r) => ({
  version: r.u16(),
  cipherSuite: r.u16(),
  groupId: r.vec(),
  epoch: r.u64(),
  treeHash: r.vec(),
  confirmedTranscriptHash: r.vec(),
  extensions: rExts(r),
});

const wPskId = (w, p) => {
  w.u8(p.psktype);
  if (p.psktype === 1) w.vec(p.pskId);
  else w.u8(p.usage).vec(p.pskGroupId).u64(p.pskEpoch);
  return w.vec(p.nonce);
};
const rPskId = (r) => {
  let p = { psktype: r.u8() };
  if (p.psktype === 1) p.pskId = r.vec();
  else if (p.psktype === 2)
    Object.assign(p, { usage: r.u8(), pskGroupId: r.vec(), pskEpoch: r.u64() });
  else fail("unknown PSK type " + p.psktype);
  p.nonce = r.vec();
  return p;
};

const wProposal = (w, p) => {
  w.u16(p.type);
  switch (p.type) {
    case PROPOSAL.ADD:
      return wKeyPackage(w, p.keyPackage);
    case PROPOSAL.UPDATE:
      return wLeaf(w, p.leaf);
    case PROPOSAL.REMOVE:
      return w.u32(p.removed);
    case PROPOSAL.PSK:
      return wPskId(w, p.psk);
    case PROPOSAL.REINIT:
      return wExts(w.vec(p.groupId).u16(p.version).u16(p.cipherSuite), p.extensions);
    case PROPOSAL.EXTERNAL_INIT:
      return w.vec(p.kemOutput);
    case PROPOSAL.GROUP_CONTEXT_EXTENSIONS:
      return wExts(w, p.extensions);
    case PROPOSAL.APP_DATA_UPDATE:
      w.u16(p.componentId).u8(p.op);
      return p.op === 1 ? w.vec(p.data) : w;
    case PROPOSAL.APP_EPHEMERAL:
      return w.u16(p.componentId).vec(p.data);
    case PROPOSAL.SELF_REMOVE:
      return w;
    default:
      return w.vec(p.data);
  }
};
const rProposal = (r) => {
  let type = r.u16();
  switch (type) {
    case PROPOSAL.ADD:
      return { type, keyPackage: rKeyPackage(r) };
    case PROPOSAL.UPDATE:
      return { type, leaf: rLeaf(r) };
    case PROPOSAL.REMOVE:
      return { type, removed: r.u32() };
    case PROPOSAL.PSK:
      return { type, psk: rPskId(r) };
    case PROPOSAL.REINIT:
      return {
        type,
        groupId: r.vec(),
        version: r.u16(),
        cipherSuite: r.u16(),
        extensions: rExts(r),
      };
    case PROPOSAL.EXTERNAL_INIT:
      return { type, kemOutput: r.vec() };
    case PROPOSAL.GROUP_CONTEXT_EXTENSIONS:
      return { type, extensions: rExts(r) };
    case PROPOSAL.APP_DATA_UPDATE: {
      let p = { type, componentId: r.u16(), op: r.u8() };
      if (p.op === 1) p.data = r.vec();
      else if (p.op !== 2) fail("unknown app_data_update operation " + p.op);
      return p;
    }
    case PROPOSAL.APP_EPHEMERAL:
      return { type, componentId: r.u16(), data: r.vec() };
    case PROPOSAL.SELF_REMOVE:
      return { type };
    default:
      // unknown and GREASE proposal types carry one opaque vector (as OpenMLS frames them)
      return { type, data: r.vec() };
  }
};

const wHpke = (w, c) => w.vec(c.kemOutput).vec(c.ciphertext);
const rHpke = (r) => ({ kemOutput: r.vec(), ciphertext: r.vec() });

const wPath = (w, p) =>
  wLeaf(w, p.leaf).list(p.nodes, (w, n) => w.vec(n.encryptionKey).list(n.secrets, wHpke));
const rPath = (r) => ({
  leaf: rLeaf(r),
  nodes: r.list((r) => ({ encryptionKey: r.vec(), secrets: r.list(rHpke) })),
});

const wCommit = (w, c) =>
  w
    .list(c.proposals, (w, p) => (p.ref ? w.u8(2).vec(p.ref) : wProposal(w.u8(1), p.proposal)))
    .opt(c.path, wPath);
const rCommit = (r) => ({
  proposals: r.list((r) => {
    let t = r.u8();
    if (t === 1) return { proposal: rProposal(r) };
    if (t === 2) return { ref: r.vec() };
    return fail("unknown ProposalOrRef type " + t);
  }),
  path: r.opt(rPath),
});

const wSender = (w, s) => {
  w.u8(s.type);
  if (s.type === SENDER.MEMBER) w.u32(s.leafIndex);
  if (s.type === SENDER.EXTERNAL) w.u32(s.senderIndex);
  return w;
};
const rSender = (r) => {
  let type = r.u8();
  if (type === SENDER.MEMBER) return { type, leafIndex: r.u32() };
  if (type === SENDER.EXTERNAL) return { type, senderIndex: r.u32() };
  if (type === SENDER.NEW_MEMBER_PROPOSAL || type === SENDER.NEW_MEMBER_COMMIT) return { type };
  return fail("unknown sender type " + type);
};

const wBody = (w, c) =>
  c.contentType === CONTENT.APPLICATION
    ? w.vec(c.data)
    : c.contentType === CONTENT.PROPOSAL
      ? wProposal(w, c.proposal)
      : wCommit(w, c.commit);
const rBody = (r, contentType) => {
  if (contentType === CONTENT.APPLICATION) return { data: r.vec() };
  if (contentType === CONTENT.PROPOSAL) return { proposal: rProposal(r) };
  if (contentType === CONTENT.COMMIT) return { commit: rCommit(r) };
  return fail("unknown content type " + contentType);
};

// FramedContent
const wContent = (w, c) =>
  wBody(wSender(w.vec(c.groupId).u64(c.epoch), c.sender).vec(c.aad).u8(c.contentType), c);
const rContent = (r) => {
  let c = {
    groupId: r.vec(),
    epoch: r.u64(),
    sender: rSender(r),
    aad: r.vec(),
    contentType: r.u8(),
  };
  return Object.assign(c, rBody(r, c.contentType));
};

const wAuth = (w, a, contentType) =>
  contentType === CONTENT.COMMIT ? w.vec(a.signature).vec(a.confirmationTag) : w.vec(a.signature);
const rAuth = (r, contentType) =>
  contentType === CONTENT.COMMIT
    ? { signature: r.vec(), confirmationTag: r.vec() }
    : { signature: r.vec() };

const wPublic = (w, m) => {
  wAuth(wContent(w, m.content), m.auth, m.content.contentType);
  return m.content.sender.type === SENDER.MEMBER ? w.vec(m.membershipTag) : w;
};
const rPublic = (r) => {
  let content = rContent(r);
  let m = { content, auth: rAuth(r, content.contentType) };
  if (content.sender.type === SENDER.MEMBER) m.membershipTag = r.vec();
  return m;
};

const wPrivate = (w, m) =>
  w
    .vec(m.groupId)
    .u64(m.epoch)
    .u8(m.contentType)
    .vec(m.aad)
    .vec(m.encryptedSenderData)
    .vec(m.ciphertext);
const rPrivate = (r) => ({
  groupId: r.vec(),
  epoch: r.u64(),
  contentType: r.u8(),
  aad: r.vec(),
  encryptedSenderData: r.vec(),
  ciphertext: r.vec(),
});

const wGroupInfoTBS = (w, g) =>
  wExts(wContext(w, g.context), g.extensions).vec(g.confirmationTag).u32(g.signer);
const wGroupInfo = (w, g) => wGroupInfoTBS(w, g).vec(g.signature);
const rGroupInfo = (r) => ({
  context: rContext(r),
  extensions: rExts(r),
  confirmationTag: r.vec(),
  signer: r.u32(),
  signature: r.vec(),
});

const wWelcome = (w, m) =>
  w
    .u16(m.cipherSuite)
    .list(m.secrets, (w, s) => wHpke(w.vec(s.ref), s))
    .vec(m.encryptedGroupInfo);
const rWelcome = (r) => ({
  cipherSuite: r.u16(),
  secrets: r.list((r) => ({ ref: r.vec(), ...rHpke(r) })),
  encryptedGroupInfo: r.vec(),
});

const wGroupSecrets = (w, g) =>
  w
    .vec(g.joinerSecret)
    .opt(g.pathSecret, (w, s) => w.vec(s))
    .list(g.psks, wPskId);
const rGroupSecrets = (r) => ({
  joinerSecret: r.vec(),
  pathSecret: r.opt((r) => r.vec()),
  psks: r.list(rPskId),
});

const BODY = [undefined, "public", "private", "welcome", "groupInfo", "keyPackage"];
const W_BODY = [undefined, wPublic, wPrivate, wWelcome, wGroupInfo, wKeyPackage];
const R_BODY = [undefined, rPublic, rPrivate, rWelcome, rGroupInfo, rKeyPackage];

const wMessage = (w, m) => W_BODY[m.wireFormat](w.u16(1).u16(m.wireFormat), m[BODY[m.wireFormat]]);
const rMessage = (r) => {
  let version = r.u16();
  let wireFormat = r.u16();
  if (version !== 1) fail("unsupported protocol version " + version, "unsupported");
  if (!R_BODY[wireFormat]) fail("unknown wire format " + wireFormat);
  return { wireFormat, [BODY[wireFormat]]: R_BODY[wireFormat](r) };
};

// ---- extensions we read ----

const extOf = (exts, type) => exts.find((e) => e.type === type)?.data;

const uniqueTypes = (exts, what) => {
  if (new Set(exts.map((e) => e.type)).size !== exts.length)
    fail("duplicate extension type in " + what);
};

const requiredCapabilities = (exts) => {
  let data = extOf(exts, EXT.REQUIRED_CAPABILITIES);
  return data
    ? dec((r) => ({ extensions: rU16s(r), proposals: rU16s(r), credentials: rU16s(r) }), data)
    : null;
};

// the RFC 9420 types every client supports without listing them in its capabilities
const supportsExtension = (caps, t) => (t >= 1 && t <= 5) || caps.extensions.includes(t);
const supportsProposal = (caps, t) => (t >= 1 && t <= 7) || caps.proposals.includes(t);
const meetsRequired = (caps, rc) =>
  !rc ||
  (rc.extensions.every((t) => supportsExtension(caps, t)) &&
    rc.proposals.every((t) => supportsProposal(caps, t)) &&
    rc.credentials.every((t) => caps.credentials.includes(t)));

// app_data_dictionary: entries are { id, data }, sorted by id, one per component
export const encodeDict = (entries) => {
  let sorted = [...entries].sort((a, b) => a.id - b.id);
  if (sorted.some((e, i) => i && sorted[i - 1].id === e.id))
    fail("app_data_dictionary: duplicate component id");
  return new Writer().list(sorted, (w, e) => w.u16(e.id).vec(e.data)).finish();
};

export const decodeDict = (bytes) => {
  let entries = dec((r) => r.list((r) => ({ id: r.u16(), data: r.vec() })), bytes);
  if (entries.some((e, i) => i && entries[i - 1].id >= e.id))
    fail("app_data_dictionary: entries not sorted and unique");
  return entries;
};

export const dictOf = (extensions) => {
  let data = extOf(extensions, EXT.APP_DATA_DICTIONARY);
  return data ? decodeDict(data) : [];
};

// ---- tree math (RFC 9420 appendix C, complete trees only) ----

const level = (x) => {
  let k = 0;
  while ((x >> k) & 1) k++;
  return k;
};
const left = (x) => x ^ (1 << (level(x) - 1));
const right = (x) => x ^ (3 << (level(x) - 1));
const parent = (x) => {
  let k = level(x);
  return (x | (1 << k)) & ~(1 << (k + 1));
};
const rootOf = (width) => width >> 1;
const sibling = (x) => {
  let p = parent(x);
  return x < p ? right(p) : left(p);
};
const directPath = (x, width) => {
  let path = [];
  while (x !== rootOf(width)) path.push((x = parent(x)));
  return path;
};
const inSubtree = (top, x) => Math.abs(x - top) < 1 << level(top);

const resolution = (tree, x) => {
  let node = tree[x];
  if (node) return x % 2 ? [x, ...node.unmergedLeaves.map((l) => 2 * l)] : [x];
  return x % 2 ? [...resolution(tree, left(x)), ...resolution(tree, right(x))] : [];
};

// the direct path of leaf node x minus the parents whose copath child resolves to nobody
const filteredPath = (tree, x) => {
  let out = [];
  while (x !== rootOf(tree.length)) {
    let copath = sibling(x);
    let res = resolution(tree, copath);
    x = parent(x);
    if (res.length) out.push({ node: x, copath, res });
  }
  return out;
};

// `without` (a Set of leaf indices) hashes the subtree as it was before those leaves joined
const treeHash = (tree, x, without) => {
  if (x % 2 === 0) {
    let leaf = without?.has(x / 2) ? null : tree[x];
    return sha256(
      new Writer()
        .u8(1)
        .u32(x / 2)
        .opt(leaf, wLeaf)
        .finish(),
    );
  }
  let node = tree[x];
  if (node && without)
    node = { ...node, unmergedLeaves: node.unmergedLeaves.filter((l) => !without.has(l)) };
  return sha256(
    new Writer()
      .u8(2)
      .opt(node, wParent)
      .vec(treeHash(tree, left(x), without))
      .vec(treeHash(tree, right(x), without))
      .finish(),
  );
};
const rootHash = (tree) => treeHash(tree, rootOf(tree.length));

// the hash a child stores to pin parent node p, given the copath child s under p
const parentHashOf = (tree, p, s) => {
  let node = tree[p];
  let without = node.unmergedLeaves.length ? new Set(node.unmergedLeaves) : undefined;
  return sha256(
    new Writer()
      .vec(node.encryptionKey)
      .vec(node.parentHash)
      .vec(treeHash(tree, s, without))
      .finish(),
  );
};

// RFC 9420 §7.9.2: every non-blank parent must be vouched for by a node in the
// resolution of one of its children (one that was there when the parent was set)
const verifyParentHashes = (tree) =>
  tree.every((node, x) => {
    if (!node || x % 2 === 0) return true;
    let late = new Set(node.unmergedLeaves);
    return [
      [left(x), right(x)],
      [right(x), left(x)],
    ].some(([child, other]) => {
      let ph = parentHashOf(tree, x, other);
      return resolution(tree, child).some((d) => {
        if (d % 2 === 0 && late.has(d / 2)) return false;
        let stored = d % 2 || tree[d].source === LEAF_SOURCE.COMMIT ? tree[d].parentHash : null;
        return stored && eq(stored, ph);
      });
    });
  });

// tree edits work on a fresh copy of the array and replace nodes, never touch them

const blankPath = (tree, x) => {
  for (let p of directPath(x, tree.length)) tree[p] = null;
};

const truncate = (tree) => {
  let n = tree.length;
  while (n > 1 && !tree[n - 1]) n--;
  let width = 1;
  while (width < n) width = 2 * width + 1;
  tree.length = width;
};

const addLeaf = (tree, leaf) => {
  let x = 0;
  while (x < tree.length && tree[x]) x += 2;
  if (x >= tree.length) for (let n = tree.length + 1; n > 0; n--) tree.push(null);
  tree[x] = leaf;
  // sorted, as OpenMLS keeps (and requires) them; ts-mls appends
  for (let p of directPath(x, tree.length))
    if (tree[p])
      tree[p] = {
        ...tree[p],
        unmergedLeaves: [...tree[p].unmergedLeaves, x / 2].sort((a, b) => a - b),
      };
  return x / 2;
};

// ---- validation ----

// RFC 9420 §7.3, the parts that need only the leaf and the group's extensions
const checkLeaf = (leaf, groupId, leafIndex, extensions, now) => {
  if (leaf.encryptionKey.length !== 32 || leaf.signatureKey.length !== 32)
    fail("leaf node key of the wrong size");
  if (
    !verifyWithLabel(
      leaf.signatureKey,
      "LeafNodeTBS",
      leafTBS(leaf, groupId, leafIndex),
      leaf.signature,
    )
  )
    fail("invalid leaf node signature");
  if (leaf.credential.type !== 1)
    fail("unsupported credential type " + leaf.credential.type, "unsupported");
  let caps = leaf.capabilities;
  if (!caps.versions.includes(1) || !caps.ciphersuites.includes(1))
    fail("leaf node does not support this protocol version and ciphersuite");
  if (!caps.credentials.includes(leaf.credential.type))
    fail("leaf node credential type missing from its capabilities");
  uniqueTypes(leaf.extensions, "leaf node");
  if (!leaf.extensions.every((e) => supportsExtension(caps, e.type)))
    fail("leaf node extension missing from its capabilities");
  if (!meetsRequired(caps, requiredCapabilities(extensions)))
    fail("leaf node lacks the group's required capabilities");
  if (leaf.source === LEAF_SOURCE.KEY_PACKAGE) {
    let from = BigInt(leaf.lifetime.notBefore);
    let to = BigInt(leaf.lifetime.notAfter);
    if (from > to) fail("leaf node lifetime ends before it starts");
    if (now != null && (from > BigInt(now) || to <= BigInt(now)))
      fail("key package outside its lifetime", "lifetime");
  }
};

const checkKeyPackage = (kp) => {
  if (kp.version !== 1 || kp.cipherSuite !== 1)
    fail("key package for another version or ciphersuite", "unsupported");
  if (kp.leaf.source !== LEAF_SOURCE.KEY_PACKAGE)
    fail("key package leaf node has the wrong source");
  if (kp.initKey.length !== 32 || eq(kp.initKey, kp.leaf.encryptionKey))
    fail("key package init key invalid");
  uniqueTypes(kp.extensions, "key package");
  if (
    !verifyWithLabel(kp.leaf.signatureKey, "KeyPackageTBS", enc(wKeyPackageBody, kp), kp.signature)
  )
    fail("invalid key package signature");
};

const checkTreeKeys = (tree) => {
  let seen = new Set();
  tree.forEach((node, x) => {
    if (!node) return;
    let keys = x % 2 ? [node.encryptionKey] : [node.encryptionKey, node.signatureKey];
    for (let k of keys.map(hex)) {
      if (seen.has(k)) fail("key appears twice in the ratchet tree");
      seen.add(k);
    }
  });
};

const checkAllSupport = (tree, extensions) => {
  let rc = requiredCapabilities(extensions);
  tree.forEach((leaf, x) => {
    if (!leaf || x % 2) return;
    if (
      !meetsRequired(leaf.capabilities, rc) ||
      !extensions.every((e) => supportsExtension(leaf.capabilities, e.type))
    )
      fail("a member does not support the group context extensions");
  });
};

// a tree somebody handed us (Welcome): RFC 9420 §12.4.3.1
const checkTree = (tree, groupId, extensions, now) => {
  tree.forEach((node, x) => {
    if (!node) return;
    if (x % 2 === 0) return checkLeaf(node, groupId, x / 2, extensions, now);
    let last = -1;
    for (let l of node.unmergedLeaves) {
      if (l <= last) fail("unmerged leaves not sorted");
      last = l;
      if (2 * l >= tree.length || !tree[2 * l] || !inSubtree(x, 2 * l))
        fail("unmerged leaf is not a member below its node");
      for (let p of directPath(2 * l, tree.length)) {
        if (p === x) break;
        if (tree[p] && !tree[p].unmergedLeaves.includes(l))
          fail("unmerged leaf missing from an intermediate node");
      }
    }
  });
  checkTreeKeys(tree);
  if (!verifyParentHashes(tree)) fail("invalid parent hash in ratchet tree");
};

// ---- key schedule ----

const epochKeys = (epochSecret) => {
  let d = (label) => deriveSecret(epochSecret, label);
  return {
    encryption: d("encryption"),
    keys: {
      senderData: d("sender data"),
      exporter: d("exporter"),
      external: d("external"),
      confirmation: d("confirm"),
      membership: d("membership"),
      resumption: d("resumption"),
      authenticator: d("authentication"),
      init: d("init"),
    },
  };
};

const joinerSecret = (init, commitSecret, context) =>
  expandWithLabel(extract(sha256, commitSecret, init), "joiner", context, 32);

const fromJoiner = (joiner, pskSecret, context) => {
  let member = extract(sha256, pskSecret, joiner);
  return {
    welcome: deriveSecret(member, "welcome"),
    ...epochKeys(expandWithLabel(member, "epoch", context, 32)),
  };
};

const welcomeCipher = (welcome) =>
  gcm(expandWithLabel(welcome, "key", EMPTY, 16), expandWithLabel(welcome, "nonce", EMPTY, 12));

// RFC 9420 §8.4; `psks` is [{ id, psk }]
const pskSecret = (psks) =>
  psks.reduce((secret, { id, psk }, i) => {
    let label = wPskId(new Writer(), id).u16(i).u16(psks.length).finish();
    let input = expandWithLabel(extract(sha256, psk, ZERO32), "derived psk", label, 32);
    return extract(sha256, secret, input);
  }, ZERO32);

// PSKs come from the caller: { [hex(psk_id)]: bytes } for external ones, and
// { ["resumption:" + epoch]: state.keys.resumption } for earlier epochs of this
// group (we only hold the current epoch's ourselves)
const lookupPsks = (ids, known = {}, s) => {
  if (new Set(ids.map((id) => hex(enc(wPskId, id)))).size !== ids.length) fail("duplicate PSK id");
  return pskSecret(
    ids.map((id) => {
      let psk = id.psktype === 1 ? known[hex(id.pskId)] : null;
      if (id.psktype === 2) {
        if (id.usage !== 1 || !s || !eq(id.pskGroupId, s.groupId))
          fail("reinit and branch PSKs are not supported", "unsupported");
        psk = id.pskEpoch === s.epoch ? s.keys.resumption : known["resumption:" + id.pskEpoch];
      }
      if (!psk)
        fail(
          id.psktype === 1 ? "unknown external PSK" : "no resumption PSK for epoch " + id.pskEpoch,
          "unsupported",
        );
      return { id, psk };
    }),
  );
};

const interimHash = (confirmed, tag) => sha256(concat(confirmed, new Writer().vec(tag).finish()));
const confirmedHash = (interim, wire, content, signature) =>
  sha256(concat(interim, wContent(new Writer().u16(wire), content).vec(signature).finish()));

// ---- secret tree (RFC 9420 §9) ----
//
// { nodes: { nodeIndex: secret }, ratchets: { leafNodeIndex: { h, a } } }: node
// secrets are expanded only on the way down to a leaf that speaks, and deleted
// once used. A ratchet is { gen, secret, skip: { generation: { key, nonce } } }.

const ratchetKey = (r) => ({
  key: deriveTreeSecret(r.secret, "key", r.gen, 16),
  nonce: deriveTreeSecret(r.secret, "nonce", r.gen, 12),
});
const ratchetNext = (r) => ({
  ...r,
  gen: r.gen + 1,
  secret: deriveTreeSecret(r.secret, "secret", r.gen, 32),
});

const leafRatchets = (st, width, x) => {
  if (st.ratchets[x]) return st;
  let nodes = { ...st.nodes };
  let path = [x, ...directPath(x, width)].reverse();
  let i = path.findIndex((n) => nodes[n]);
  if (i < 0) fail("secret tree has no secret for leaf " + x / 2);
  for (; path[i] !== x; i++) {
    let n = path[i];
    nodes[left(n)] = expandWithLabel(nodes[n], "tree", utf8("left"), 32);
    nodes[right(n)] = expandWithLabel(nodes[n], "tree", utf8("right"), 32);
    delete nodes[n];
  }
  let ratchet = (label) => ({
    gen: 0,
    secret: expandWithLabel(nodes[x], label, EMPTY, 32),
    skip: {},
  });
  let ratchets = { ...st.ratchets, [x]: { h: ratchet("handshake"), a: ratchet("application") } };
  delete nodes[x];
  return { nodes, ratchets };
};

const setRatchet = (st, x, kind, r) => ({
  ...st,
  ratchets: { ...st.ratchets, [x]: { ...st.ratchets[x], [kind]: r } },
});

// the key for `generation`, and the ratchet after it has been taken
const seek = (r, generation) => {
  if (generation < r.gen) {
    let kn = r.skip[generation];
    if (!kn) fail("message key already used", "replay");
    let skip = { ...r.skip };
    delete skip[generation];
    return { kn, r: { ...r, skip } };
  }
  if (generation - r.gen > MAX_SKIP) fail("message generation too far ahead");
  let skip = { ...r.skip };
  for (; r.gen < generation; r = ratchetNext(r)) skip[r.gen] = ratchetKey(r);
  let gens = Object.keys(skip);
  for (let g of gens.slice(0, Math.max(0, gens.length - MAX_SKIP))) delete skip[g];
  return { kn: ratchetKey(r), r: { ...ratchetNext(r), skip } };
};

// ---- message framing ----

const contentTBS = (s, wire, content) => {
  let w = wContent(new Writer().u16(1).u16(wire), content);
  let t = content.sender.type;
  if (t === SENDER.MEMBER || t === SENDER.NEW_MEMBER_COMMIT) wContext(w, s);
  return w.finish();
};

const signContent = (s, wire, content) =>
  signWithLabel(s.sigPriv, "FramedContentTBS", contentTBS(s, wire, content));

const memberLeaf = (s, leafIndex) => {
  let leaf = 2 * leafIndex < s.tree.length ? s.tree[2 * leafIndex] : null;
  return leaf || fail("sender is not a member of the group");
};

const verifyContent = (s, wire, content, auth) => {
  let leaf = memberLeaf(s, content.sender.leafIndex);
  if (
    !verifyWithLabel(
      leaf.signatureKey,
      "FramedContentTBS",
      contentTBS(s, wire, content),
      auth.signature,
    )
  )
    fail("invalid message signature");
};

const membershipTag = (s, content, auth) =>
  mac(
    s.keys.membership,
    wAuth(
      new Writer().bytes(contentTBS(s, WIRE.PUBLIC, content)),
      auth,
      content.contentType,
    ).finish(),
  );

const sealPublic = (s, content, auth) =>
  enc(wMessage, {
    wireFormat: WIRE.PUBLIC,
    public: { content, auth, membershipTag: membershipTag(s, content, auth) },
  });

const refuseSender = (sender) => {
  if (sender.type === SENDER.EXTERNAL) fail("external senders are not supported", "unsupported");
  if (sender.type === SENDER.NEW_MEMBER_PROPOSAL)
    fail("new_member proposals are not supported", "unsupported");
  if (sender.type === SENDER.NEW_MEMBER_COMMIT)
    fail("external commits are not supported", "unsupported");
};

const openPublic = (s, m) => {
  refuseSender(m.content.sender);
  if (m.content.contentType === CONTENT.APPLICATION) fail("application data must be encrypted");
  if (!eq(m.membershipTag, membershipTag(s, m.content, m.auth))) fail("invalid membership tag");
  verifyContent(s, WIRE.PUBLIC, m.content, m.auth);
  return m;
};

const privateAAD = (m) =>
  new Writer().vec(m.groupId).u64(m.epoch).u8(m.contentType).vec(m.aad).finish();
const senderDataCipher = (s, m) => {
  let sample = m.ciphertext.subarray(0, 32);
  let aad = new Writer().vec(m.groupId).u64(m.epoch).u8(m.contentType).finish();
  let secret = s.keys.senderData;
  return gcm(
    expandWithLabel(secret, "key", sample, 16),
    expandWithLabel(secret, "nonce", sample, 12),
    aad,
  );
};
const guarded = (nonce, guard) => nonce.map((b, i) => (i < 4 ? b ^ guard[i] : b));
const ratchetKind = (contentType) => (contentType === CONTENT.APPLICATION ? "a" : "h");

const sealPrivate = (s, content, auth, padding = 0) => {
  let x = 2 * s.leafIndex;
  let kind = ratchetKind(content.contentType);
  let st = leafRatchets(s.secretTree, s.tree.length, x);
  let r = st.ratchets[x][kind];
  let kn = ratchetKey(r);
  let guard = randomBytes(4);
  let m = {
    groupId: content.groupId,
    epoch: content.epoch,
    contentType: content.contentType,
    aad: content.aad,
  };
  let body = wAuth(wBody(new Writer(), content), auth, content.contentType)
    .bytes(new Uint8Array(padding))
    .finish();
  m.ciphertext = gcm(kn.key, guarded(kn.nonce, guard), privateAAD(m)).encrypt(body);
  let senderData = new Writer().u32(s.leafIndex).u32(r.gen).bytes(guard).finish();
  m.encryptedSenderData = senderDataCipher(s, m).encrypt(senderData);
  return {
    message: enc(wMessage, { wireFormat: WIRE.PRIVATE, private: m }),
    secretTree: setRatchet(st, x, kind, ratchetNext(r)),
  };
};

// the ratchet only moves in the returned secretTree, and only once the message has authenticated
const openPrivate = (s, m) => {
  let sd;
  try {
    sd = new Reader(senderDataCipher(s, m).decrypt(m.encryptedSenderData));
  } catch {
    fail("cannot decrypt sender data");
  }
  let leafIndex = sd.u32();
  let generation = sd.u32();
  let guard = sd.bytes(4);
  sd.done();
  memberLeaf(s, leafIndex);
  if (leafIndex === s.leafIndex) fail("message was sent by this client", "own");
  let x = 2 * leafIndex;
  let kind = ratchetKind(m.contentType);
  let st = leafRatchets(s.secretTree, s.tree.length, x);
  let { kn, r } = seek(st.ratchets[x][kind], generation);
  let body;
  try {
    body = new Reader(gcm(kn.key, guarded(kn.nonce, guard), privateAAD(m)).decrypt(m.ciphertext));
  } catch {
    fail("cannot decrypt message");
  }
  let content = {
    groupId: m.groupId,
    epoch: m.epoch,
    sender: { type: SENDER.MEMBER, leafIndex },
    aad: m.aad,
    contentType: m.contentType,
    ...rBody(body, m.contentType),
  };
  let auth = rAuth(body, m.contentType);
  if (body.buf.subarray(body.pos).some((b) => b)) fail("non-zero message padding");
  verifyContent(s, WIRE.PRIVATE, content, auth);
  return { content, auth, secretTree: setRatchet(st, x, kind, r) };
};

// ---- key packages ----

const DEFAULT_CAPS = {
  versions: [1],
  ciphersuites: [1],
  extensions: [],
  proposals: [],
  credentials: [1],
};

const bytesOf = (v) => (typeof v === "string" ? utf8(v) : v);

// an hour of slack behind us and twelve weeks ahead: the widest range OpenMLS accepts
const lifetimeOf = (l = {}) => {
  let now = Math.floor(Date.now() / 1000);
  return { notBefore: l.notBefore ?? now - 3600, notAfter: l.notAfter ?? now + 84 * 86400 };
};

const newLeaf = ({ sig, identity, capabilities, leafExtensions = [], lifetime }, encryptionKey) =>
  signLeaf(
    {
      encryptionKey,
      signatureKey: sig.pub,
      credential: { type: 1, identity: bytesOf(identity) },
      capabilities: { ...DEFAULT_CAPS, ...capabilities },
      source: LEAF_SOURCE.KEY_PACKAGE,
      lifetime: lifetimeOf(lifetime),
      extensions: leafExtensions,
    },
    sig.priv,
  );

// { priv, pub }: an Ed25519 seed and its public key
export const generateSignatureKeyPair = () => {
  let priv = randomBytes(32);
  return { priv, pub: ed25519.getPublicKey(priv) };
};

// ({ sig, identity, capabilities, leafExtensions, extensions, lifetime }) →
// { keyPackage, message, ref, secrets: { initPriv, encPriv } }. `keyPackage` is the bare
// struct, `message` the same thing framed as an MLSMessage; keep `secrets` until the Welcome.
export const generateKeyPackage = (opts) => {
  let initPriv = randomBytes(32);
  let encPriv = randomBytes(32);
  let kp = {
    version: 1,
    cipherSuite: 1,
    initKey: x25519.getPublicKey(initPriv),
    leaf: newLeaf(opts, x25519.getPublicKey(encPriv)),
    extensions: opts.extensions || [],
  };
  kp.signature = signWithLabel(opts.sig.priv, "KeyPackageTBS", enc(wKeyPackageBody, kp));
  checkLeaf(kp.leaf, EMPTY, 0, []);
  checkKeyPackage(kp);
  return {
    keyPackage: enc(wKeyPackage, kp),
    message: enc(wMessage, { wireFormat: WIRE.KEY_PACKAGE, keyPackage: kp }),
    ref: keyPackageRef(kp),
    secrets: { initPriv, encPriv },
  };
};

// bare KeyPackage or MLSMessage(mls_key_package): a bare one starts version 1, suite 1
// where the framed one says version 1, wire format 5
const readKeyPackage = (bytes) =>
  bytes[2] === 0 && bytes[3] === WIRE.KEY_PACKAGE
    ? dec(rMessage, bytes).keyPackage || fail("not a key package")
    : dec(rKeyPackage, bytes);

// either framing; throws unless both signatures verify
export const parseKeyPackage = (bytes) => {
  let kp = readKeyPackage(bytes);
  checkKeyPackage(kp);
  if (!verifyWithLabel(kp.leaf.signatureKey, "LeafNodeTBS", leafTBS(kp.leaf), kp.leaf.signature))
    fail("invalid leaf node signature");
  let { initKey, leaf, extensions, cipherSuite, version } = kp;
  return {
    keyPackage: enc(wKeyPackage, kp),
    ref: keyPackageRef(kp),
    initKey,
    leaf,
    extensions,
    cipherSuite,
    version,
  };
};

// a look at an MLSMessage without any group state: enough to route it
export const decodeMessage = (bytes) => {
  let m = dec(rMessage, bytes);
  let out = { wireFormat: WIRE_NAME[m.wireFormat] };
  if (m.public) {
    let { groupId, epoch, sender, aad, contentType } = m.public.content;
    Object.assign(out, { groupId, epoch, sender, aad, contentType: CONTENT_NAME[contentType] });
  } else if (m.private) {
    let { groupId, epoch, aad, contentType } = m.private;
    Object.assign(out, {
      groupId,
      epoch,
      aad,
      contentType: CONTENT_NAME[contentType] || fail("unknown content type"),
    });
  } else if (m.welcome) {
    Object.assign(out, {
      cipherSuite: m.welcome.cipherSuite,
      refs: m.welcome.secrets.map((s) => s.ref),
    });
  } else if (m.groupInfo) {
    let { groupId, epoch } = m.groupInfo.context;
    Object.assign(out, {
      groupId,
      epoch,
      extensions: m.groupInfo.extensions,
      signer: m.groupInfo.signer,
    });
  } else {
    Object.assign(out, {
      keyPackage: enc(wKeyPackage, m.keyPackage),
      ref: keyPackageRef(m.keyPackage),
    });
  }
  return out;
};

// ---- groups ----

const secretTreeFor = (tree, encryption) => ({
  nodes: { [rootOf(tree.length)]: encryption },
  ratchets: {},
});

// ({ groupId, sig, identity, capabilities, leafExtensions, extensions, lifetime }) → state
// at epoch 0 with one member; `extensions` are the GroupContext's
export const createGroup = (opts) => {
  let { groupId, sig, extensions = [] } = opts;
  let encPriv = randomBytes(32);
  let leaf = newLeaf(opts, x25519.getPublicKey(encPriv));
  uniqueTypes(extensions, "group context");
  checkLeaf(leaf, groupId, 0, extensions);
  checkAllSupport([leaf], extensions);
  let tree = [leaf];
  let { keys, encryption } = epochKeys(randomBytes(32));
  let confirmationTag = mac(keys.confirmation, EMPTY);
  return {
    version: 1,
    cipherSuite: 1,
    groupId,
    epoch: 0,
    treeHash: rootHash(tree),
    confirmedTranscriptHash: EMPTY,
    extensions,
    interimTranscriptHash: interimHash(EMPTY, confirmationTag),
    confirmationTag,
    tree,
    leafIndex: 0,
    sigPriv: sig.priv,
    priv: { 0: encPriv },
    keys,
    secretTree: secretTreeFor(tree, encryption),
    pending: [],
  };
};

// node keys from a path secret upward: fp[from] is the node the secret belongs to
const derivePath = (tree, fp, from, secret) => {
  let priv = {};
  for (let i = from; i < fp.length; i++) {
    let kp = deriveKeyPair(deriveSecret(secret, "node"));
    if (!eq(kp.pub, tree[fp[i].node].encryptionKey))
      fail("path secret does not match the node's public key");
    priv[fp[i].node] = kp.priv;
    secret = deriveSecret(secret, "path");
  }
  return { priv, commitSecret: secret };
};

// The state a Welcome invites us into. `keyPackage` and `secrets` are what
// generateKeyPackage returned; `state.joinedVia` is the leaf that signed the GroupInfo.
// `ratchetTree` supplies the tree out of band when the GroupInfo has none, `now`
// (unix seconds) turns on the key package lifetime check for every leaf.
export const joinWelcome = (bytes, { keyPackage, secrets, sig, ratchetTree, psks, now } = {}) => {
  let { welcome } = dec(rMessage, bytes);
  if (!welcome) fail("not a Welcome message");
  if (welcome.cipherSuite !== 1)
    fail("unsupported ciphersuite " + welcome.cipherSuite, "unsupported");
  let kp = readKeyPackage(keyPackage);
  let ref = keyPackageRef(kp);
  let mine = welcome.secrets.find((e) => eq(e.ref, ref));
  if (!mine) fail("Welcome is not addressed to this key package", "not-for-us");
  if (
    !eq(x25519.getPublicKey(secrets.encPriv), kp.leaf.encryptionKey) ||
    !eq(sig.pub, kp.leaf.signatureKey)
  )
    fail("private keys do not match the key package");

  let groupSecrets;
  try {
    let plain = decryptWithLabel(
      secrets.initPriv,
      "Welcome",
      welcome.encryptedGroupInfo,
      mine.kemOutput,
      mine.ciphertext,
    );
    groupSecrets = dec(rGroupSecrets, plain);
  } catch (e) {
    if (e.code) throw e;
    fail("cannot decrypt group secrets");
  }
  let member = extract(sha256, lookupPsks(groupSecrets.psks, psks), groupSecrets.joinerSecret);
  let info;
  try {
    info = dec(
      rGroupInfo,
      welcomeCipher(deriveSecret(member, "welcome")).decrypt(welcome.encryptedGroupInfo),
    );
  } catch {
    fail("cannot decrypt group info");
  }
  let ctx = info.context;
  if (ctx.version !== 1 || ctx.cipherSuite !== 1)
    fail("group uses another version or ciphersuite", "unsupported");
  if (typeof ctx.epoch !== "number") fail("epoch out of range");
  uniqueTypes(ctx.extensions, "group context");

  // without the tree nothing in the GroupInfo can be checked, so it has to travel with it
  let treeBytes = extOf(info.extensions, EXT.RATCHET_TREE) || ratchetTree;
  if (!treeBytes) fail("Welcome carries no ratchet_tree extension");
  let tree = dec(rTree, treeBytes);
  let signer = 2 * info.signer < tree.length ? tree[2 * info.signer] : null;
  if (!signer) fail("GroupInfo signer is not in the tree");
  if (
    !verifyWithLabel(signer.signatureKey, "GroupInfoTBS", enc(wGroupInfoTBS, info), info.signature)
  )
    fail("invalid GroupInfo signature");
  if (!eq(rootHash(tree), ctx.treeHash)) fail("ratchet tree does not match the group's tree hash");
  checkTree(tree, ctx.groupId, ctx.extensions, now);

  let own = enc(wLeaf, kp.leaf);
  let x = tree.findIndex((node, i) => node && i % 2 === 0 && eq(enc(wLeaf, node), own));
  if (x < 0) fail("our leaf is not in the ratchet tree");
  let priv = { [x]: secrets.encPriv };
  if (groupSecrets.pathSecret) {
    let fp = filteredPath(tree, 2 * info.signer);
    let from = fp.findIndex((f) => inSubtree(f.node, x));
    if (from < 0) fail("no common ancestor with the GroupInfo signer");
    Object.assign(priv, derivePath(tree, fp, from, groupSecrets.pathSecret).priv);
  }

  let { keys, encryption } = epochKeys(expandWithLabel(member, "epoch", enc(wContext, ctx), 32));
  if (!eq(info.confirmationTag, mac(keys.confirmation, ctx.confirmedTranscriptHash)))
    fail("invalid confirmation tag");
  return {
    ...ctx,
    interimTranscriptHash: interimHash(ctx.confirmedTranscriptHash, info.confirmationTag),
    confirmationTag: info.confirmationTag,
    tree,
    leafIndex: x / 2,
    sigPriv: sig.priv,
    priv,
    keys,
    secretTree: secretTreeFor(tree, encryption),
    pending: [],
    joinedVia: info.signer,
  };
};

// ---- proposals ----

export const proposeAdd = (keyPackageBytes) => ({
  type: PROPOSAL.ADD,
  keyPackage: readKeyPackage(keyPackageBytes),
});
export const proposeRemove = (leafIndex) => ({ type: PROPOSAL.REMOVE, removed: leafIndex });
export const proposeGroupContextExtensions = (extensions) => ({
  type: PROPOSAL.GROUP_CONTEXT_EXTENSIONS,
  extensions,
});
export const proposeAppDataUpdate = (componentId, data) =>
  data == null
    ? { type: PROPOSAL.APP_DATA_UPDATE, componentId, op: 2 }
    : { type: PROPOSAL.APP_DATA_UPDATE, componentId, op: 1, data };
export const proposeAppEphemeral = (componentId, data) => ({
  type: PROPOSAL.APP_EPHEMERAL,
  componentId,
  data,
});
export const proposeSelfRemove = () => ({ type: PROPOSAL.SELF_REMOVE });

// the default meaning of app_data_update: the payload replaces the component's data.
// A caller's hook (id, current | undefined, updates[]) → bytes may say otherwise,
// or return undefined to refuse the commit.
const lastWins = (id, current, updates) => updates[updates.length - 1];

// draft-ietf-mls-extensions §4.7. As OpenMLS does it: start from the dictionary of
// the CURRENT context and re-add the extension at the END of the list — the
// position is part of the GroupContext every member hashes.
const applyAppData = (current, target, updates, hook = lastWins, strict) => {
  let dict = dictOf(current);
  for (let id of new Set(updates.map((u) => u.componentId))) {
    let ups = updates.filter((u) => u.componentId === id);
    let at = dict.findIndex((e) => e.id === id);
    if (ups.some((u) => u.op === 2)) {
      if (ups.length > 1)
        fail("app_data_update: a remove cannot share a commit with other changes to the component");
      // The draft calls removing absent state invalid and we never send it, but OpenMLS
      // only notices when the commit also carries the dictionary in a
      // GroupContextExtensions; refusing more than it does would fork us off the group.
      if (at < 0 && strict) fail("app_data_update: component " + id + " has no state to remove");
      if (at >= 0) dict.splice(at, 1);
      continue;
    }
    let data = hook(
      id,
      at < 0 ? undefined : dict[at].data,
      ups.map((u) => u.data),
    );
    if (!(data instanceof Uint8Array)) fail("app_data_update rejected for component " + id);
    if (at < 0) dict.push({ id, data });
    else dict[at] = { id, data };
  }
  return [
    ...target.filter((e) => e.type !== EXT.APP_DATA_DICTIONARY),
    { type: EXT.APP_DATA_DICTIONARY, data: encodeDict(dict) },
  ];
};

const requiresAppDataUpdate = (extensions) =>
  !!requiredCapabilities(extensions)?.proposals.includes(PROPOSAL.APP_DATA_UPDATE);

const PATH_REQUIRED = [
  PROPOSAL.UPDATE,
  PROPOSAL.REMOVE,
  PROPOSAL.EXTERNAL_INIT,
  PROPOSAL.GROUP_CONTEXT_EXTENSIONS,
  PROPOSAL.SELF_REMOVE,
];

// RFC 9420 §12.2 for a member's commit: validates `list` ([{ proposal, sender, byRef }])
// and applies it to a copy of the tree
const applyProposals = (s, list, committer, { appData, psks, now, own } = {}) => {
  let of = (type) => list.filter((e) => e.proposal.type === type);
  let types = list.map((e) => e.proposal.type);
  for (let t of types) {
    if (t === PROPOSAL.REINIT) fail("reinit is not supported", "unsupported");
    if (t === PROPOSAL.EXTERNAL_INIT) fail("external_init is not valid in a member's commit");
    if (t < 1 || t > PROPOSAL.SELF_REMOVE) fail("unsupported proposal type " + t, "unsupported");
  }

  let removals = new Set(of(PROPOSAL.REMOVE).map((e) => e.proposal.removed));
  for (let t of new Set(types.filter((t) => t > PROPOSAL.GROUP_CONTEXT_EXTENSIONS)))
    s.tree.forEach((leaf, x) => {
      if (leaf && x % 2 === 0 && !removals.has(x / 2) && !supportsProposal(leaf.capabilities, t))
        fail("proposal type " + t + " is not supported by every member");
    });

  let touched = new Set();
  let touch = (leafIndex) => {
    if (2 * leafIndex >= s.tree.length || !s.tree[2 * leafIndex])
      fail("proposal targets a blank leaf");
    if (touched.has(leafIndex)) fail("several update/remove proposals for the same leaf");
    touched.add(leafIndex);
  };
  for (let e of of(PROPOSAL.UPDATE)) {
    if (!e.byRef || e.sender === committer)
      fail("a committer cannot commit its own update proposal");
    touch(e.sender);
  }
  for (let e of of(PROPOSAL.REMOVE)) {
    if (e.proposal.removed === committer) fail("a committer cannot remove itself");
    touch(e.proposal.removed);
  }
  for (let e of of(PROPOSAL.SELF_REMOVE)) {
    // the leaver is the proposal's sender, so inline (sender = committer) can never be right
    if (!e.byRef || e.sender === committer)
      fail("self_remove must be committed by reference by another member");
    touch(e.sender);
  }

  let gce = of(PROPOSAL.GROUP_CONTEXT_EXTENSIONS);
  if (gce.length > 1) fail("several group_context_extensions proposals");
  let extensions = gce.length ? gce[0].proposal.extensions : s.extensions;
  if (gce.length) {
    uniqueTypes(extensions, "group context");
    // An OpenMLS rule, not an RFC one: whatever the context carries must be
    // required of every member. Applied where the group plays by it (a
    // required_capabilities extension in the context, as White Noise groups
    // have); a ts-mls group (Cordn) carries none and changes its metadata
    // extension freely, as the RFC allows.
    let required = requiredCapabilities(extensions)?.extensions;
    if (required && !extensions.every((e) => supportsExtension({ extensions: required }, e.type)))
      fail("group context extension type missing from required_capabilities");
    // with app_data_update required, the dictionary only changes through app_data_update
    if (requiresAppDataUpdate(s.extensions) || requiresAppDataUpdate(extensions)) {
      let a = extOf(s.extensions, EXT.APP_DATA_DICTIONARY);
      let b = extOf(extensions, EXT.APP_DATA_DICTIONARY);
      if (!!a !== !!b || (a && !eq(a, b)))
        fail(
          "group_context_extensions cannot change app_data_dictionary while app_data_update is required",
        );
    }
  }
  let firstAppData = types.indexOf(PROPOSAL.APP_DATA_UPDATE);
  if (firstAppData >= 0 && types.lastIndexOf(PROPOSAL.GROUP_CONTEXT_EXTENSIONS) > firstAppData)
    fail("app_data_update must come after group_context_extensions");

  let tree = s.tree.slice();
  for (let e of of(PROPOSAL.UPDATE)) {
    let leaf = e.proposal.leaf;
    if (leaf.source !== LEAF_SOURCE.UPDATE) fail("update proposal leaf node has the wrong source");
    checkLeaf(leaf, s.groupId, e.sender, extensions);
    if (eq(leaf.encryptionKey, tree[2 * e.sender].encryptionKey))
      fail("update proposal keeps the old encryption key");
    blankPath(tree, 2 * e.sender);
    tree[2 * e.sender] = leaf;
  }
  let leaving = [
    ...of(PROPOSAL.REMOVE).map((e) => e.proposal.removed),
    ...of(PROPOSAL.SELF_REMOVE).map((e) => e.sender),
  ];
  for (let leafIndex of leaving) {
    blankPath(tree, 2 * leafIndex);
    tree[2 * leafIndex] = null;
  }
  truncate(tree);
  let added = of(PROPOSAL.ADD).map((e) => {
    let keyPackage = e.proposal.keyPackage;
    checkKeyPackage(keyPackage);
    checkLeaf(keyPackage.leaf, EMPTY, 0, extensions, now);
    if (!s.extensions.every((ext) => supportsExtension(keyPackage.leaf.capabilities, ext.type)))
      fail("added member does not support the group context extensions");
    return { leafIndex: addLeaf(tree, keyPackage.leaf), keyPackage };
  });
  if (gce.length) checkAllSupport(tree, extensions);

  let updates = of(PROPOSAL.APP_DATA_UPDATE).map((e) => e.proposal);
  let carried = gce.length > 0 && !!extOf(extensions, EXT.APP_DATA_DICTIONARY);
  if (updates.length)
    extensions = applyAppData(s.extensions, extensions, updates, appData, own || carried);

  return {
    tree,
    extensions,
    added,
    removedSelf: leaving.includes(s.leafIndex),
    pathRequired: !list.length || types.some((t) => PATH_REQUIRED.includes(t)),
    pskSecret: lookupPsks(
      of(PROPOSAL.PSK).map((e) => e.proposal.psk),
      psks,
      s,
    ),
    pskIds: of(PROPOSAL.PSK).map((e) => e.proposal.psk),
    ephemeral: of(PROPOSAL.APP_EPHEMERAL).map((e) => ({
      componentId: e.proposal.componentId,
      data: e.proposal.data,
      sender: e.sender,
    })),
  };
};

// ---- commits ----

// writes the new parent nodes of a commit into the tree; returns the parent hash the leaf must carry
const setPath = (tree, x, fp, keys) => {
  blankPath(tree, x);
  let parentHash = EMPTY;
  for (let i = fp.length - 1; i >= 0; i--) {
    tree[fp[i].node] = { encryptionKey: keys[i], parentHash, unmergedLeaves: [] };
    parentHash = parentHashOf(tree, fp[i].node, fp[i].copath);
  }
  return parentHash;
};

// who each path secret is encrypted to: the copath resolutions, minus the leaves
// this commit adds (they learn theirs from the Welcome)
const pathTargets = (fp, added, path) => {
  let fresh = new Set(added.map((a) => 2 * a.leafIndex));
  let targets = fp.map((f) => f.res.filter((n) => !fresh.has(n)));
  if (
    path.nodes.length !== fp.length ||
    path.nodes.some((n, i) => n.secrets.length !== targets[i].length)
  )
    fail("update path does not fit the tree");
  return targets;
};

const openPathSecret = (priv, leafIndex, fp, targets, path, context) => {
  let at = fp.findIndex((f) => inSubtree(f.node, 2 * leafIndex));
  let to = at < 0 ? -1 : targets[at].findIndex((n) => priv[n]);
  if (to < 0) fail("no private key for the update path");
  try {
    let { kemOutput, ciphertext } = path.nodes[at].secrets[to];
    return {
      at,
      secret: decryptWithLabel(
        priv[targets[at][to]],
        "UpdatePathNode",
        context,
        kemOutput,
        ciphertext,
      ),
    };
  } catch {
    return fail("cannot decrypt path secret");
  }
};

// private keys survive an epoch change only for nodes whose public key did
const carryKeys = (priv, before, after) =>
  Object.fromEntries(
    Object.entries(priv).filter(
      ([x]) => after[x] && before[x] && eq(after[x].encryptionKey, before[x].encryptionKey),
    ),
  );

// the state every member lands in once a commit is applied
const enterEpoch = (
  s,
  wire,
  content,
  signature,
  { tree, treeHash, extensions, priv, commitSecret, pskSecret },
) => {
  let confirmedTranscriptHash = confirmedHash(s.interimTranscriptHash, wire, content, signature);
  let next = {
    ...s,
    epoch: s.epoch + 1,
    treeHash,
    confirmedTranscriptHash,
    extensions,
    tree,
    priv,
    pending: [],
  };
  let context = enc(wContext, next);
  let joiner = joinerSecret(s.keys.init, commitSecret, context);
  let { welcome, keys, encryption } = fromJoiner(joiner, pskSecret, context);
  next.keys = keys;
  next.confirmationTag = mac(keys.confirmation, confirmedTranscriptHash);
  next.interimTranscriptHash = interimHash(confirmedTranscriptHash, next.confirmationTag);
  next.secretTree = secretTreeFor(tree, encryption);
  return { state: next, joiner, welcome };
};

const active = (s) => {
  if (s.removed) fail("this client was removed from the group", "removed");
};

const findPending = (s, ref) => s.pending.find((p) => eq(p.ref, ref));

// which cached proposals a commit of ours can carry: never our own update or
// self_remove, one proposal per leaf (leaving beats updating), one context change.
// Among several self_removes of one leaf Marmot picks the lowest message digest.
const committable = (s, inline) => {
  let me = s.leafIndex;
  let leafOf = (p) =>
    p.proposal.type === PROPOSAL.REMOVE
      ? p.proposal.removed
      : p.proposal.type === PROPOSAL.UPDATE || p.proposal.type === PROPOSAL.SELF_REMOVE
        ? p.sender
        : null;
  let rank = (p) =>
    [PROPOSAL.UPDATE, PROPOSAL.REMOVE, PROPOSAL.SELF_REMOVE].indexOf(p.proposal.type);
  let best = new Map();
  for (let p of inline) if (p.type === PROPOSAL.REMOVE) best.set(p.removed, null);
  let hasContext = inline.some((p) => p.type === PROPOSAL.GROUP_CONTEXT_EXTENSIONS);
  let signers = new Set(
    inline.filter((p) => p.type === PROPOSAL.ADD).map((p) => hex(p.keyPackage.leaf.signatureKey)),
  );
  let keep = [];
  for (let p of [...s.pending].reverse()) {
    let type = p.proposal.type;
    let leaf = leafOf(p);
    if (leaf != null) {
      if (leaf === me || 2 * leaf >= s.tree.length || !s.tree[2 * leaf]) continue;
      let cur = best.get(leaf);
      if (cur === null) continue;
      let better =
        !cur ||
        rank(p) > rank(cur) ||
        (type === PROPOSAL.SELF_REMOVE &&
          cur.proposal.type === type &&
          hex(p.digest) < hex(cur.digest));
      if (better) best.set(leaf, p);
    } else if (type === PROPOSAL.GROUP_CONTEXT_EXTENSIONS) {
      if (!hasContext) keep.push(p);
      hasContext = true;
    } else if (type === PROPOSAL.ADD) {
      let k = hex(p.proposal.keyPackage.leaf.signatureKey);
      if (!signers.has(k)) keep.push(p);
      signers.add(k);
    } else keep.push(p);
  }
  for (let p of best.values()) if (p) keep.push(p);
  return s.pending.filter((p) => keep.includes(p));
};

// (state, { proposals, includePending, refs, leaf, appData, aad, path, psks, now }) →
// { message, welcome, state, addedRefs }. `proposals` go inline, cached ones by
// reference (all that can be committed, or exactly `refs`). `leaf` replaces our
// leaf's { capabilities, extensions }; `path: false` drops the update path when no
// proposal needs one. The input state is untouched: hold on to it until the commit
// is published, then switch to the returned one. One Welcome covers every add.
export const createCommit = (state, opts = {}) => {
  active(state);
  let {
    proposals = [],
    includePending = true,
    refs,
    leaf: patch,
    aad = EMPTY,
    path: wantPath = true,
  } = opts;
  let me = state.leafIndex;
  let cached = refs
    ? refs.map((ref) => findPending(state, ref) || fail("unknown proposal reference"))
    : includePending
      ? committable(state, proposals)
      : [];
  let list = [
    ...cached.map((p) => ({ proposal: p.proposal, sender: p.sender, byRef: true, ref: p.ref })),
    ...proposals.map((proposal) => ({ proposal, sender: me, byRef: false })),
  ];
  // app_data_update is applied last and must follow a GroupContextExtensions on the wire
  let late = (e) => e.proposal.type === PROPOSAL.APP_DATA_UPDATE;
  list = [...list.filter((e) => !late(e)), ...list.filter(late)];

  let ap = applyProposals(state, list, me, { ...opts, own: true });
  let { tree, extensions, pskSecret } = ap;
  let x = 2 * me;
  let commitSecret = ZERO32;
  let path = null;
  let steps = [];
  let priv;
  if (wantPath || ap.pathRequired) {
    let fp = filteredPath(tree, x);
    let secret = randomBytes(32);
    for (let f of fp) {
      steps.push({ ...f, secret, ...deriveKeyPair(deriveSecret(secret, "node")) });
      secret = deriveSecret(secret, "path");
    }
    commitSecret = secret;
    let old = tree[x];
    let leafPriv = randomBytes(32);
    let parentHash = setPath(
      tree,
      x,
      fp,
      steps.map((st) => st.pub),
    );
    let { lifetime, signature, ...rest } = old;
    let leaf = signLeaf(
      {
        ...rest,
        encryptionKey: x25519.getPublicKey(leafPriv),
        capabilities: patch?.capabilities
          ? { ...DEFAULT_CAPS, ...patch.capabilities }
          : old.capabilities,
        extensions: patch?.extensions || old.extensions,
        source: LEAF_SOURCE.COMMIT,
        parentHash,
      },
      state.sigPriv,
      state.groupId,
      me,
    );
    checkLeaf(leaf, state.groupId, me, extensions);
    tree[x] = leaf;
    priv = { ...carryKeys(state.priv, state.tree, tree), [x]: leafPriv };
    for (let st of steps) priv[st.node] = st.priv;
    // new members learn their path secret from the Welcome instead
    let fresh = new Set(ap.added.map((a) => 2 * a.leafIndex));
    let context = enc(wContext, {
      ...state,
      epoch: state.epoch + 1,
      treeHash: rootHash(tree),
      extensions,
    });
    path = {
      leaf,
      nodes: steps.map((st) => ({
        encryptionKey: st.pub,
        secrets: st.res
          .filter((n) => !fresh.has(n))
          .map((n) =>
            encryptWithLabel(tree[n].encryptionKey, "UpdatePathNode", context, st.secret),
          ),
      })),
    };
  } else priv = carryKeys(state.priv, state.tree, tree);
  checkTreeKeys(tree);

  let content = {
    groupId: state.groupId,
    epoch: state.epoch,
    sender: { type: SENDER.MEMBER, leafIndex: me },
    aad,
    contentType: CONTENT.COMMIT,
    commit: {
      proposals: list.map((e) => (e.byRef ? { ref: e.ref } : { proposal: e.proposal })),
      path,
    },
  };
  let signature = signContent(state, WIRE.PUBLIC, content);
  let next = enterEpoch(state, WIRE.PUBLIC, content, signature, {
    tree,
    treeHash: rootHash(tree),
    extensions,
    priv,
    commitSecret,
    pskSecret,
  });
  let message = sealPublic(state, content, {
    signature,
    confirmationTag: next.state.confirmationTag,
  });

  let welcome = null;
  let addedRefs = ap.added.map((a) => keyPackageRef(a.keyPackage));
  if (ap.added.length) {
    let info = {
      context: next.state,
      extensions: [{ type: EXT.RATCHET_TREE, data: enc(wTree, tree) }],
      confirmationTag: next.state.confirmationTag,
      signer: me,
    };
    info.signature = signWithLabel(state.sigPriv, "GroupInfoTBS", enc(wGroupInfoTBS, info));
    let encryptedGroupInfo = welcomeCipher(next.welcome).encrypt(enc(wGroupInfo, info));
    let secrets = ap.added.map(({ leafIndex, keyPackage }, i) => {
      let groupSecrets = enc(wGroupSecrets, {
        joinerSecret: next.joiner,
        pathSecret: steps.find((st) => inSubtree(st.node, 2 * leafIndex))?.secret,
        psks: ap.pskIds,
      });
      return {
        ref: addedRefs[i],
        ...encryptWithLabel(keyPackage.initKey, "Welcome", encryptedGroupInfo, groupSecrets),
      };
    });
    welcome = enc(wMessage, {
      wireFormat: WIRE.WELCOME,
      welcome: { cipherSuite: 1, secrets, encryptedGroupInfo },
    });
  }
  return { message, welcome, state: next.state, addedRefs };
};

const proposalRef = (wire, content, auth) =>
  refHash(
    "MLS 1.0 Proposal Reference",
    wAuth(wContent(new Writer().u16(wire), content), auth, content.contentType).finish(),
  );

const cacheProposal = (s, wire, content, auth, message) => {
  let ref = proposalRef(wire, content, auth);
  let entry = {
    ref,
    proposal: content.proposal,
    sender: content.sender.leafIndex,
    digest: sha256(message),
  };
  return { ref, state: findPending(s, ref) ? s : { ...s, pending: [...s.pending, entry] } };
};

// a standalone proposal as a PublicMessage, cached in the returned state like anyone else's
export const createProposal = (state, proposal, { aad = EMPTY } = {}) => {
  active(state);
  if (proposal.type === PROPOSAL.UPDATE)
    fail("update proposals are not supported; commit instead", "unsupported");
  let content = {
    groupId: state.groupId,
    epoch: state.epoch,
    sender: { type: SENDER.MEMBER, leafIndex: state.leafIndex },
    aad,
    contentType: CONTENT.PROPOSAL,
    proposal,
  };
  let auth = { signature: signContent(state, WIRE.PUBLIC, content) };
  let message = sealPublic(state, content, auth);
  return { message, ...cacheProposal(state, WIRE.PUBLIC, content, auth, message) };
};

const processCommit = (s, wire, content, auth, opts) => {
  let committer = content.sender.leafIndex;
  // our own commit comes back from the relay; its state is the one createCommit returned
  if (committer === s.leafIndex) fail("commit was created by this client", "own");
  let { commit } = content;
  let list = commit.proposals.map((p) => {
    if (!p.ref) return { proposal: p.proposal, sender: committer, byRef: false };
    let cached =
      findPending(s, p.ref) ||
      fail("commit references a proposal we have not seen", "missing-proposal");
    return { proposal: cached.proposal, sender: cached.sender, byRef: true };
  });
  let ap = applyProposals(s, list, committer, opts);
  let result = {
    type: "commit",
    sender: committer,
    aad: content.aad,
    proposals: list,
    ephemeral: ap.ephemeral,
    removedSelf: ap.removedSelf,
  };
  // removed members get no path secret: the old state stays readable, the group moves on without us
  if (ap.removedSelf) return { ...result, state: { ...s, removed: true, pending: [] } };

  let { tree, extensions, pskSecret } = ap;
  let { path } = commit;
  if (!path && ap.pathRequired) fail("commit is missing its update path");
  let commitSecret = ZERO32;
  let priv;
  if (path) {
    let x = 2 * committer;
    if (path.leaf.source !== LEAF_SOURCE.COMMIT) fail("commit leaf node has the wrong source");
    checkLeaf(path.leaf, s.groupId, committer, extensions);
    if (eq(path.leaf.encryptionKey, tree[x].encryptionKey))
      fail("commit keeps the committer's old encryption key");
    let fp = filteredPath(tree, x);
    let targets = pathTargets(fp, ap.added, path);
    let parentHash = setPath(
      tree,
      x,
      fp,
      path.nodes.map((n) => n.encryptionKey),
    );
    if (!eq(parentHash, path.leaf.parentHash))
      fail("commit leaf node carries the wrong parent hash");
    tree[x] = path.leaf;
    let context = enc(wContext, { ...s, epoch: s.epoch + 1, treeHash: rootHash(tree), extensions });
    let { at, secret } = openPathSecret(s.priv, s.leafIndex, fp, targets, path, context);
    let derived = derivePath(tree, fp, at, secret);
    commitSecret = derived.commitSecret;
    priv = { ...carryKeys(s.priv, s.tree, tree), ...derived.priv };
  } else priv = carryKeys(s.priv, s.tree, tree);
  checkTreeKeys(tree);

  let next = enterEpoch(s, wire, content, auth.signature, {
    tree,
    treeHash: rootHash(tree),
    extensions,
    priv,
    commitSecret,
    pskSecret,
  });
  if (!eq(next.state.confirmationTag, auth.confirmationTag)) fail("invalid confirmation tag");
  return { ...result, state: next.state };
};

// (state, bytes, { appData, psks, now }) →
//   { type: "application", data, aad, sender, state }
//   { type: "proposal", proposal, sender, ref, aad, state }
//   { type: "commit", proposals: [{ proposal, sender, byRef }], ephemeral, removedSelf, sender, aad, state }
// Handshake may arrive as PublicMessage or PrivateMessage. Throws on anything
// invalid; e.code says why when the caller can act on it: "epoch" (try another
// retained state), "group", "own" (we sent it), "replay", "missing-proposal",
// "removed", "unsupported". When a commit removes us the returned state is the old
// one marked `removed`: it still reads its epoch but can no longer send or advance.
export const processMessage = (state, bytes, opts = {}) => {
  let m = dec(rMessage, bytes);
  let framed = m.public?.content || m.private;
  if (!framed) fail("not a group message");
  if (!eq(framed.groupId, state.groupId)) fail("message is for another group", "group");
  if (framed.epoch !== state.epoch)
    fail(`message is for epoch ${framed.epoch}, state is at ${state.epoch}`, "epoch");

  let wire = m.wireFormat;
  let s = state;
  let content, auth;
  if (m.public) ({ content, auth } = openPublic(state, m.public));
  else {
    let opened = openPrivate(state, m.private);
    ({ content, auth } = opened);
    s = { ...state, secretTree: opened.secretTree };
  }
  let sender = content.sender.leafIndex;
  if (content.contentType === CONTENT.APPLICATION)
    return { type: "application", data: content.data, aad: content.aad, sender, state: s };
  active(state);
  if (content.contentType === CONTENT.PROPOSAL)
    return {
      type: "proposal",
      proposal: content.proposal,
      sender,
      aad: content.aad,
      ...cacheProposal(s, wire, content, auth, bytes),
    };
  return processCommit(s, wire, content, auth, opts);
};

// application data as a PrivateMessage; the returned state has the ratchet moved on
export const encryptApplication = (state, data, aad = EMPTY) => {
  active(state);
  let content = {
    groupId: state.groupId,
    epoch: state.epoch,
    sender: { type: SENDER.MEMBER, leafIndex: state.leafIndex },
    aad,
    contentType: CONTENT.APPLICATION,
    data,
  };
  let { message, secretTree } = sealPrivate(state, content, {
    signature: signContent(state, WIRE.PRIVATE, content),
  });
  return { message, state: { ...state, secretTree } };
};

// MLS-Exporter (RFC 9420 §8.5)
export const exportSecret = (state, label, context, length) =>
  expandWithLabel(deriveSecret(state.keys.exporter, label), "exported", sha256(context), length);

export const epochAuthenticator = (state) => state.keys.authenticator;

export const members = (state) =>
  state.tree.flatMap((leaf, x) =>
    leaf && x % 2 === 0
      ? [
          {
            index: x / 2,
            identity: leaf.credential.identity,
            signatureKey: leaf.signatureKey,
            encryptionKey: leaf.encryptionKey,
            leaf,
          },
        ]
      : [],
  );

// not API: what tools/mls-vectors-test.js needs to run the RFC 9420 interop vectors
export const internals = {
  enc,
  dec,
  codec: {
    message: [wMessage, rMessage],
    tree: [wTree, rTree],
    proposal: [wProposal, rProposal],
    commit: [wCommit, rCommit],
    path: [wPath, rPath],
    groupSecrets: [wGroupSecrets, rGroupSecrets],
    groupInfo: [wGroupInfo, rGroupInfo],
    groupInfoTBS: [wGroupInfoTBS],
    context: [wContext, rContext],
    content: [wContent, rContent],
    auth: [wAuth, rAuth],
    leaf: [wLeaf, rLeaf],
    pskId: [wPskId, rPskId],
  },
  tree: {
    level,
    left,
    right,
    parent,
    sibling,
    rootOf,
    directPath,
    resolution,
    filteredPath,
    treeHash,
    rootHash,
  },
  ratchetTree: { verifyParentHashes, blankPath, truncate, addLeaf, checkTree, leafTBS },
  treekem: { setPath, derivePath, pathTargets, openPathSecret },
  crypto: {
    expandWithLabel,
    deriveSecret,
    deriveTreeSecret,
    refHash,
    signWithLabel,
    verifyWithLabel,
    encryptWithLabel,
    decryptWithLabel,
    deriveKeyPair,
    mac,
  },
  schedule: {
    epochKeys,
    joinerSecret,
    fromJoiner,
    welcomeCipher,
    pskSecret,
    interimHash,
    confirmedHash,
  },
  secretTree: { leafRatchets, seek, secretTreeFor },
  framing: { sealPublic, openPublic, sealPrivate, openPrivate, signContent },
  keyPackageRef,
};