@ghostlytools/cli 1.11.3 → 1.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (218) hide show
  1. package/README.md +52 -30
  2. package/SKILL.md +4 -2
  3. package/dist/assets/{api-DBlaoz8i.js → api-DLzCtePC.js} +533 -195
  4. package/dist/assets/api-DLzCtePC.js.map +1 -0
  5. package/dist/assets/{api-DgU4MZPD.js → api-DYMKyb1k.js} +2 -2
  6. package/dist/assets/{apiKit-DesObIDf.js → apiKit-CVaEzKWQ.js} +104 -21
  7. package/dist/assets/apiKit-CVaEzKWQ.js.map +1 -0
  8. package/dist/assets/appFetch-D56GsYuq.js +602 -0
  9. package/dist/assets/appFetch-D56GsYuq.js.map +1 -0
  10. package/dist/assets/apps-BPFW19Wc.js +1 -0
  11. package/dist/assets/{apps-DkMjDHML.js → apps-XnZ8VEuj.js} +66 -38
  12. package/dist/assets/apps-XnZ8VEuj.js.map +1 -0
  13. package/dist/assets/{argon2id-088uQfnH.js → argon2id-B9wFdSj6.js} +2 -3
  14. package/dist/assets/{argon2id-088uQfnH.js.map → argon2id-B9wFdSj6.js.map} +1 -1
  15. package/dist/assets/{arkade-CarKONmH.js → arkade-DryM-q5n.js} +6 -7
  16. package/dist/assets/arkade-DryM-q5n.js.map +1 -0
  17. package/dist/assets/backup-COX6j1_W.js +1 -0
  18. package/dist/assets/{backup-C0OJCJlM.js → backup-DpyvT9Z6.js} +108 -50
  19. package/dist/assets/backup-DpyvT9Z6.js.map +1 -0
  20. package/dist/assets/bark_ffi_wasm_bg-CcafsJla.js +5 -0
  21. package/dist/assets/{bark_ffi_wasm_bg-EY2uc0Bx.js.map → bark_ffi_wasm_bg-CcafsJla.js.map} +1 -1
  22. package/dist/assets/{bark_ffi_wasm_bg-Bxru_BVW.wasm → bark_ffi_wasm_bg-VNmaJsc9.wasm} +0 -0
  23. package/dist/assets/{bitcoind-CdSmvy2y.js → bitcoind-DTsrd7TF.js} +4 -5
  24. package/dist/assets/{bitcoind-CdSmvy2y.js.map → bitcoind-DTsrd7TF.js.map} +1 -1
  25. package/dist/assets/bitcoindevkit_bg-BWQHx7vd.js +5 -0
  26. package/dist/assets/{bitcoindevkit_bg-B-iLWrmX.js.map → bitcoindevkit_bg-BWQHx7vd.js.map} +1 -1
  27. package/dist/assets/bitcoindevkit_bg-DXil67yS.wasm +0 -0
  28. package/dist/assets/{blobFiles-BmsrvZc3.js → blobFiles-DDs34n0r.js} +4 -5
  29. package/dist/assets/{blobFiles-BmsrvZc3.js.map → blobFiles-DDs34n0r.js.map} +1 -1
  30. package/dist/assets/breez_sdk_spark_wasm_bg.wasm +0 -0
  31. package/dist/assets/buttons-Bq7pTXqF.js +1 -0
  32. package/dist/assets/{buttons-fZTEa_2i.js → buttons-BvWqHJeh.js} +46 -23
  33. package/dist/assets/buttons-BvWqHJeh.js.map +1 -0
  34. package/dist/assets/bytes-C1DNGY18.js +132 -0
  35. package/dist/assets/bytes-C1DNGY18.js.map +1 -0
  36. package/dist/assets/{childEnv-K_IzUqQx.js → childEnv-C-99hdbL.js} +2 -3
  37. package/dist/assets/{childEnv-K_IzUqQx.js.map → childEnv-C-99hdbL.js.map} +1 -1
  38. package/dist/assets/{client-Ibk3-e60.js → client-kGZ3ZdUW.js} +30 -13
  39. package/dist/assets/client-kGZ3ZdUW.js.map +1 -0
  40. package/dist/assets/{codec-DMpQqVSt.js → codec-B2rH1mfx.js} +2 -4
  41. package/dist/assets/{codec-DMpQqVSt.js.map → codec-B2rH1mfx.js.map} +1 -1
  42. package/dist/assets/domainProofs-BDNP1dIy.js +280 -0
  43. package/dist/assets/domainProofs-BDNP1dIy.js.map +1 -0
  44. package/dist/assets/{endpoint-DarqblYE.js → endpoint-B0vE5ojY.js} +421 -420
  45. package/dist/assets/endpoint-B0vE5ojY.js.map +1 -0
  46. package/dist/assets/engineErrors--9hfgKlK.js +150 -0
  47. package/dist/assets/engineErrors--9hfgKlK.js.map +1 -0
  48. package/dist/assets/{envelope-CvQVCGdf.js → envelope-Bi2kMqbp.js} +3 -4
  49. package/dist/assets/{envelope-CvQVCGdf.js.map → envelope-Bi2kMqbp.js.map} +1 -1
  50. package/dist/assets/{errors-Dt-ZnyOX.js → errors-DDLFO4Gf.js} +31 -5
  51. package/dist/assets/errors-DDLFO4Gf.js.map +1 -0
  52. package/dist/assets/{fedimintSdk-zV_5Qdqz.js → fedimintSdk-D-6H9Pe7.js} +4 -5
  53. package/dist/assets/{fedimintSdk-zV_5Qdqz.js.map → fedimintSdk-D-6H9Pe7.js.map} +1 -1
  54. package/dist/assets/fedimint_client_wasm_bg-Cr5xGD-w.js +5 -0
  55. package/dist/assets/{fedimint_client_wasm_bg-BtHHlFpp.js.map → fedimint_client_wasm_bg-Cr5xGD-w.js.map} +1 -1
  56. package/dist/assets/fileBytes-CCKbwqG_.js +1 -0
  57. package/dist/assets/{fileBytes-BLPZq9YU.js → fileBytes-CfvUXdSy.js} +5 -7
  58. package/dist/assets/fileBytes-CfvUXdSy.js.map +1 -0
  59. package/dist/assets/{fileBytes-B0z9A6Le.js → fileBytes-tj6-VZcB.js} +3 -4
  60. package/dist/assets/{fileBytes-B0z9A6Le.js.map → fileBytes-tj6-VZcB.js.map} +1 -1
  61. package/dist/assets/fileBytesIdb-B0zzJ6p7.js +1 -0
  62. package/dist/assets/{fileBytesIdb-Bmlrl9EO.js → fileBytesIdb-BJirUAjI.js} +4 -5
  63. package/dist/assets/{fileBytesIdb-Bmlrl9EO.js.map → fileBytesIdb-BJirUAjI.js.map} +1 -1
  64. package/dist/assets/{fileBytesOpfs-DBS0kbPT.js → fileBytesOpfs-DRUYLER4.js} +4 -5
  65. package/dist/assets/{fileBytesOpfs-DBS0kbPT.js.map → fileBytesOpfs-DRUYLER4.js.map} +1 -1
  66. package/dist/assets/{fixedHosts-6gKn8dp2.js → fixedHosts-CPSsGOI2.js} +4 -5
  67. package/dist/assets/{fixedHosts-6gKn8dp2.js.map → fixedHosts-CPSsGOI2.js.map} +1 -1
  68. package/dist/assets/{ghostly_iroh_web-BzAXJUYH.js → ghostly_iroh_web-DIj5xGmR.js} +2 -3
  69. package/dist/assets/{ghostly_iroh_web-BzAXJUYH.js.map → ghostly_iroh_web-DIj5xGmR.js.map} +1 -1
  70. package/dist/assets/{src-DjVj2ynY.js → groupMeta-DTQAJG50.js} +4879 -23799
  71. package/dist/assets/groupMeta-DTQAJG50.js.map +1 -0
  72. package/dist/assets/{host-CYlIiY3r.js → host-z0FAHP9Z.js} +750 -245
  73. package/dist/assets/host-z0FAHP9Z.js.map +1 -0
  74. package/dist/assets/{hyperdhtRelay-CdsLcAhY.js → hyperdhtRelay-CvsAfQRQ.js} +5 -6
  75. package/dist/assets/hyperdhtRelay-CvsAfQRQ.js.map +1 -0
  76. package/dist/assets/hyperdhtRelay-bjNGacf5.js +32 -0
  77. package/dist/assets/hyperdhtRelay-bjNGacf5.js.map +1 -0
  78. package/dist/assets/{idb-CEuoNsTv.js → idb-ewvbqyDI.js} +2 -3
  79. package/dist/assets/{idb-CEuoNsTv.js.map → idb-ewvbqyDI.js.map} +1 -1
  80. package/dist/assets/identityProofs-Cksc_K7_.js +940 -0
  81. package/dist/assets/identityProofs-Cksc_K7_.js.map +1 -0
  82. package/dist/assets/{irohWeb-D2IuloLL.js → irohWeb-CLBx5MIv.js} +3061 -86
  83. package/dist/assets/irohWeb-CLBx5MIv.js.map +1 -0
  84. package/dist/assets/joinTrace-CBr1jbch.js +1 -0
  85. package/dist/assets/{joinTrace-CjRDFy85.js → joinTrace-xKBe73j9.js} +2 -3
  86. package/dist/assets/{joinTrace-CjRDFy85.js.map → joinTrace-xKBe73j9.js.map} +1 -1
  87. package/dist/assets/{legacy_ciphers-JJOu0JXL.js → legacy_ciphers-BnYATjxo.js} +2 -3
  88. package/dist/assets/{legacy_ciphers-JJOu0JXL.js.map → legacy_ciphers-BnYATjxo.js.map} +1 -1
  89. package/dist/assets/linkPreview-DdMf-YAc.js +252 -0
  90. package/dist/assets/linkPreview-DdMf-YAc.js.map +1 -0
  91. package/dist/assets/{main-C0PfjKTe.js → main-CVqkvuIo.js} +536 -173
  92. package/dist/assets/main-CVqkvuIo.js.map +1 -0
  93. package/dist/assets/{mainline-Cu61qcPS.js → mainline-CVrd0C1n.js} +72 -18
  94. package/dist/assets/mainline-CVrd0C1n.js.map +1 -0
  95. package/dist/assets/mints-BcBODVVW.js +1 -0
  96. package/dist/assets/{mints-j5XAOk4f.js → mints-CMDLbMeH.js} +8 -6
  97. package/dist/assets/mints-CMDLbMeH.js.map +1 -0
  98. package/dist/assets/{nacl-fast-CDhNCYHs.js → nacl-fast-B1ThWvuM.js} +2 -3
  99. package/dist/assets/{nacl-fast-CDhNCYHs.js.map → nacl-fast-B1ThWvuM.js.map} +1 -1
  100. package/dist/assets/{networkReason-Dt7Yx_n4.js → networkReason-Be-hMghr.js} +10 -14
  101. package/dist/assets/{networkReason-Dt7Yx_n4.js.map → networkReason-Be-hMghr.js.map} +1 -1
  102. package/dist/assets/{noble_curves-DDWn5AH0.js → noble_curves-DqpNlhAU.js} +3 -4
  103. package/dist/assets/{noble_curves-DDWn5AH0.js.map → noble_curves-DqpNlhAU.js.map} +1 -1
  104. package/dist/assets/{noble_hashes-B7AZyFyS.js → noble_hashes-LFe1N2xt.js} +3 -4
  105. package/dist/assets/{noble_hashes-B7AZyFyS.js.map → noble_hashes-LFe1N2xt.js.map} +1 -1
  106. package/dist/assets/{nwc-CtZypDcv.js → nwc-DKE2FXr6.js} +5 -6
  107. package/dist/assets/{nwc-CtZypDcv.js.map → nwc-DKE2FXr6.js.map} +1 -1
  108. package/dist/assets/nwcProvider-pL2XqSbW.js +208 -0
  109. package/dist/assets/nwcProvider-pL2XqSbW.js.map +1 -0
  110. package/dist/assets/{openpgp-BBFr-zHQ.js → openpgp-LkR_Zof0.js} +14 -15
  111. package/dist/assets/{openpgp-BBFr-zHQ.js.map → openpgp-LkR_Zof0.js.map} +1 -1
  112. package/dist/assets/ownDid-DY3rYqDI.js +862 -0
  113. package/dist/assets/ownDid-DY3rYqDI.js.map +1 -0
  114. package/dist/assets/{ownStatus-D4HC6tNR.js → ownStatus-DMEhrpkd.js} +81 -19
  115. package/dist/assets/ownStatus-DMEhrpkd.js.map +1 -0
  116. package/dist/assets/pairedTransports-BKmvlKz1.js +581 -0
  117. package/dist/assets/pairedTransports-BKmvlKz1.js.map +1 -0
  118. package/dist/assets/paymentIntent-DySU3Ajm.js +349 -0
  119. package/dist/assets/paymentIntent-DySU3Ajm.js.map +1 -0
  120. package/dist/assets/pkarr-BQo4PcCg.js +367 -0
  121. package/dist/assets/pkarr-BQo4PcCg.js.map +1 -0
  122. package/dist/assets/{profiles-Ccdq6Xf6.js → profiles-BFSGqDFC.js} +30 -16
  123. package/dist/assets/profiles-BFSGqDFC.js.map +1 -0
  124. package/dist/assets/registry-B15Yg_1C.js +1 -0
  125. package/dist/assets/{registry-LvxsLJ5S.js → registry-rXgaZeNn.js} +1273 -58
  126. package/dist/assets/registry-rXgaZeNn.js.map +1 -0
  127. package/dist/assets/relay-Cjfmdec0.js +2219 -0
  128. package/dist/assets/relay-Cjfmdec0.js.map +1 -0
  129. package/dist/assets/resolve-CIc3KZMV.js +970 -0
  130. package/dist/assets/resolve-CIc3KZMV.js.map +1 -0
  131. package/dist/assets/{restoredRows-C3LcxmOw.js → restoredRows-D_WSHE7H.js} +3 -4
  132. package/dist/assets/{restoredRows-C3LcxmOw.js.map → restoredRows-D_WSHE7H.js.map} +1 -1
  133. package/dist/assets/{secretGuard-B7Hj0Kvz.js → secretGuard-B4-LlMjv.js} +75 -16
  134. package/dist/assets/{secretGuard-B7Hj0Kvz.js.map → secretGuard-B4-LlMjv.js.map} +1 -1
  135. package/dist/assets/secretGuard-BmmnEbYf.js +1 -0
  136. package/dist/assets/{server-Q3VcNqGt.js → server-CQR7NLCz.js} +10200 -625
  137. package/dist/assets/server-CQR7NLCz.js.map +1 -0
  138. package/dist/assets/{sha512-BYId-G-T.js → sha512-DST9fbWH.js} +2 -3
  139. package/dist/assets/{sha512-BYId-G-T.js.map → sha512-DST9fbWH.js.map} +1 -1
  140. package/dist/assets/sshForges-BnvwmNj-.js +539 -0
  141. package/dist/assets/sshForges-BnvwmNj-.js.map +1 -0
  142. package/dist/assets/{storage-Bya5b2Xt.js → storage-BpjdxvO2.js} +243 -34
  143. package/dist/assets/storage-BpjdxvO2.js.map +1 -0
  144. package/dist/assets/text-li76pThX.js +106 -0
  145. package/dist/assets/text-li76pThX.js.map +1 -0
  146. package/dist/assets/{translate-iQZq4TnY.js → translate-BcbSWkuq.js} +21 -26
  147. package/dist/assets/{translate-iQZq4TnY.js.map → translate-BcbSWkuq.js.map} +1 -1
  148. package/dist/assets/{tree-store-N362nuzZ.js → tree-store-CAchkO_l.js} +22 -6
  149. package/dist/assets/tree-store-CAchkO_l.js.map +1 -0
  150. package/dist/assets/{types-Clyb52K2.js → types-CdWwgvyt.js} +2 -3
  151. package/dist/assets/{types-Clyb52K2.js.map → types-CdWwgvyt.js.map} +1 -1
  152. package/dist/assets/{unbzip2-stream-B1C-JX9L.js → unbzip2-stream-BtkYGpC5.js} +2 -3
  153. package/dist/assets/{unbzip2-stream-B1C-JX9L.js.map → unbzip2-stream-BtkYGpC5.js.map} +1 -1
  154. package/dist/assets/{usdt-DnyTGdTX.js → usdt-CBJ4Q5Ji.js} +7 -7
  155. package/dist/assets/usdt-CBJ4Q5Ji.js.map +1 -0
  156. package/dist/assets/{waitNotice-Cn0WJ-He.js → waitNotice-BGM0QOS2.js} +27 -4
  157. package/dist/assets/waitNotice-BGM0QOS2.js.map +1 -0
  158. package/dist/assets/{web-DwgdhfcY.js → web-68CYFCl9.js} +314 -91
  159. package/dist/assets/web-68CYFCl9.js.map +1 -0
  160. package/dist/ghostly.mjs +3 -5
  161. package/dist/ghostly.mjs.map +1 -1
  162. package/npm-shrinkwrap.json +225 -200
  163. package/package.json +16 -16
  164. package/dist/assets/api-DBlaoz8i.js.map +0 -1
  165. package/dist/assets/apiKit-DesObIDf.js.map +0 -1
  166. package/dist/assets/appFetch-C8yZIq1e.js +0 -194
  167. package/dist/assets/appFetch-C8yZIq1e.js.map +0 -1
  168. package/dist/assets/apps-DkMjDHML.js.map +0 -1
  169. package/dist/assets/apps-rzxsjfBV.js +0 -2
  170. package/dist/assets/arkade-CarKONmH.js.map +0 -1
  171. package/dist/assets/backup-C0OJCJlM.js.map +0 -1
  172. package/dist/assets/backup-CgWU7x0T.js +0 -2
  173. package/dist/assets/bark_ffi_wasm_bg-EY2uc0Bx.js +0 -6
  174. package/dist/assets/bitcoindProvider-BvoRh0D5.js +0 -97
  175. package/dist/assets/bitcoindProvider-BvoRh0D5.js.map +0 -1
  176. package/dist/assets/bitcoindevkit_bg-B-iLWrmX.js +0 -6
  177. package/dist/assets/bitcoindevkit_bg-CRkZTSbO.wasm +0 -0
  178. package/dist/assets/buttons-Cco0Agfa.js +0 -2
  179. package/dist/assets/buttons-fZTEa_2i.js.map +0 -1
  180. package/dist/assets/client-Ibk3-e60.js.map +0 -1
  181. package/dist/assets/endpoint-DarqblYE.js.map +0 -1
  182. package/dist/assets/errors-Dt-ZnyOX.js.map +0 -1
  183. package/dist/assets/fedimint_client_wasm_bg-BtHHlFpp.js +0 -6
  184. package/dist/assets/fileBytes-BLPZq9YU.js.map +0 -1
  185. package/dist/assets/fileBytes-BtIyoDF2.js +0 -2
  186. package/dist/assets/fileBytesIdb-D8mAK4pF.js +0 -2
  187. package/dist/assets/host-CYlIiY3r.js.map +0 -1
  188. package/dist/assets/hyperdhtRelay-1wzya_0L.js +0 -25
  189. package/dist/assets/hyperdhtRelay-1wzya_0L.js.map +0 -1
  190. package/dist/assets/hyperdhtRelay-CdsLcAhY.js.map +0 -1
  191. package/dist/assets/irohWeb-D2IuloLL.js.map +0 -1
  192. package/dist/assets/joinTrace-D-fnt2p3.js +0 -2
  193. package/dist/assets/jsonOut-POtMBS_V.js +0 -16
  194. package/dist/assets/jsonOut-POtMBS_V.js.map +0 -1
  195. package/dist/assets/main-C0PfjKTe.js.map +0 -1
  196. package/dist/assets/mainline-Cu61qcPS.js.map +0 -1
  197. package/dist/assets/mints-BaOmx517.js +0 -2
  198. package/dist/assets/mints-j5XAOk4f.js.map +0 -1
  199. package/dist/assets/nwcProvider-DYHkd0iu.js +0 -88
  200. package/dist/assets/nwcProvider-DYHkd0iu.js.map +0 -1
  201. package/dist/assets/ownDid-CJ0ta8CL.js +0 -282
  202. package/dist/assets/ownDid-CJ0ta8CL.js.map +0 -1
  203. package/dist/assets/ownStatus-D4HC6tNR.js.map +0 -1
  204. package/dist/assets/profiles-Ccdq6Xf6.js.map +0 -1
  205. package/dist/assets/registry-CE0PpQAE.js +0 -2
  206. package/dist/assets/registry-LvxsLJ5S.js.map +0 -1
  207. package/dist/assets/resolve-6kVu2DE5.js +0 -400
  208. package/dist/assets/resolve-6kVu2DE5.js.map +0 -1
  209. package/dist/assets/secretGuard-DUr6bgKc.js +0 -2
  210. package/dist/assets/server-Q3VcNqGt.js.map +0 -1
  211. package/dist/assets/src-DjVj2ynY.js.map +0 -1
  212. package/dist/assets/sshForges-DeZ9cN9Q.js +0 -261
  213. package/dist/assets/sshForges-DeZ9cN9Q.js.map +0 -1
  214. package/dist/assets/storage-Bya5b2Xt.js.map +0 -1
  215. package/dist/assets/tree-store-N362nuzZ.js.map +0 -1
  216. package/dist/assets/usdt-DnyTGdTX.js.map +0 -1
  217. package/dist/assets/waitNotice-Cn0WJ-He.js.map +0 -1
  218. package/dist/assets/web-DwgdhfcY.js.map +0 -1
@@ -0,0 +1,940 @@
1
+ import { f as utf8Encode, i as fromBase64Url, l as toBase64Url, o as isZ32Key, s as randomBytes } from "./bytes-C1DNGY18.js";
2
+ import { S as verify, _ as identityFromSeed, x as sign, y as publicKeyFromZ32 } from "./pkarr-BQo4PcCg.js";
3
+ import { a as stripHidden, n as hasVisible } from "./text-li76pThX.js";
4
+ import { sha256 } from "@noble/hashes/sha2.js";
5
+ var lastOf = (timeline, side, proofs) => {
6
+ for (let i = timeline.length - 1; i >= 0; i--) if (timeline[i].side === side && proofs.includes(timeline[i].proof)) return i;
7
+ return -1;
8
+ };
9
+ /** The contact's share of this kind still not verified (checking, or failed): the one its next attempt takes over. */
10
+ var unverifiedOf = (timeline, provider) => {
11
+ for (let i = timeline.length - 1; i >= 0; i--) {
12
+ const e = timeline[i];
13
+ if (e.side === "theirs" && e.kind === "shared" && e.state !== "verified" && e.provider === provider) return i;
14
+ }
15
+ return -1;
16
+ };
17
+ /**
18
+ * The timeline after one step. A share already on it is updated in place, never repeated: a request sent again at
19
+ * a reconnect, a second share of a proof the contact already verified. A new attempt after a failure or a stop is
20
+ * a new entry, except the contact's own not verified: those keep one entry per kind, which its next attempt takes
21
+ * over in place (its id and time stay, so no new sound and the chat does not move). The ids and the kind are the
22
+ * contact's to choose, so otherwise its requests alone could push every other entry, this side's too, out of the
23
+ * bound. A stop is written once; a withdrawal later found revoked becomes a revocation in place.
24
+ */
25
+ function applyIdentityEvent(timeline, event, at) {
26
+ const out = [...timeline];
27
+ const i = lastOf(out, event.side, event.type === "result" ? [event.proof, event.requested] : [event.proof]);
28
+ const last = i >= 0 ? out[i] : void 0;
29
+ /** Puts the contact's attempt on the entry of its kind still not verified, if there is one. */
30
+ const takeOver = (entry) => {
31
+ const j = event.side === "theirs" ? unverifiedOf(out, entry.provider) : -1;
32
+ if (j < 0) return false;
33
+ out[j] = {
34
+ id: out[j].id,
35
+ proof: entry.proof,
36
+ side: "theirs",
37
+ kind: "shared",
38
+ provider: entry.provider,
39
+ ...entry.subject ? { subject: entry.subject } : {},
40
+ state: entry.state,
41
+ ...entry.error ? { error: entry.error } : {},
42
+ at: out[j].at
43
+ };
44
+ return true;
45
+ };
46
+ const add = (entry) => {
47
+ const prefix = `${entry.side}:${entry.proof}:`;
48
+ let n = 0;
49
+ for (const e of out) if (e.id.startsWith(prefix)) n = Math.max(n, Number(e.id.slice(prefix.length)) + 1 || 0);
50
+ out.push({
51
+ ...entry,
52
+ id: `${prefix}${n}`,
53
+ at
54
+ });
55
+ };
56
+ switch (event.type) {
57
+ case "shared":
58
+ if (last?.kind === "shared" && last.state !== "failed") {
59
+ out[i] = {
60
+ ...last,
61
+ provider: event.provider,
62
+ subject: event.subject ?? last.subject
63
+ };
64
+ break;
65
+ }
66
+ if (takeOver({
67
+ proof: event.proof,
68
+ provider: event.provider,
69
+ subject: event.subject,
70
+ state: "verifying"
71
+ })) break;
72
+ add({
73
+ proof: event.proof,
74
+ side: event.side,
75
+ kind: "shared",
76
+ provider: event.provider,
77
+ ...event.subject ? { subject: event.subject } : {},
78
+ state: "verifying"
79
+ });
80
+ break;
81
+ case "result": {
82
+ const settled = {
83
+ state: event.ok ? "verified" : "failed",
84
+ ...event.ok || !event.error ? {} : { error: event.error }
85
+ };
86
+ if (last?.kind === "shared") {
87
+ const { error: _, ...rest } = last;
88
+ out[i] = {
89
+ ...rest,
90
+ proof: event.proof,
91
+ provider: event.provider ?? last.provider,
92
+ subject: event.subject ?? last.subject,
93
+ ...settled
94
+ };
95
+ break;
96
+ }
97
+ if (!event.ok && event.provider && takeOver({
98
+ proof: event.proof,
99
+ provider: event.provider,
100
+ subject: event.subject,
101
+ ...settled
102
+ })) break;
103
+ if (event.provider) add({
104
+ proof: event.proof,
105
+ side: event.side,
106
+ kind: "shared",
107
+ provider: event.provider,
108
+ ...event.subject ? { subject: event.subject } : {},
109
+ ...settled
110
+ });
111
+ break;
112
+ }
113
+ case "stopped":
114
+ if (last?.kind === "stopped") {
115
+ if (last.reason === "withdrawn" && event.reason === "revoked") out[i] = {
116
+ ...last,
117
+ reason: "revoked"
118
+ };
119
+ break;
120
+ }
121
+ add({
122
+ proof: event.proof,
123
+ side: event.side,
124
+ kind: "stopped",
125
+ provider: event.provider,
126
+ subject: event.subject ?? last?.subject,
127
+ reason: event.reason
128
+ });
129
+ break;
130
+ case "unsent":
131
+ case "unanswered": if (last?.kind === "shared" && last.state === "verifying") out.splice(i, 1);
132
+ }
133
+ return out.length > 100 ? out.slice(-100) : out;
134
+ }
135
+ /** When the contact last shared an identity here (milliseconds), for the chat list: 0 when never. */
136
+ function lastSharedWithMe(timeline) {
137
+ let at = 0;
138
+ for (const e of timeline ?? []) if (e.side === "theirs" && e.kind === "shared" && e.at > at) at = e.at;
139
+ return at;
140
+ }
141
+ // ../core/src/identityProofs.ts
142
+ /**
143
+ * Identity proofs (WISP 300), provider-agnostic half.
144
+ *
145
+ * A proof is made once per profile: the external identity (a Nostr key, a domain, an SSH key, an
146
+ * OpenID Connect account…) signs or attests the canonical **statement** below, which authorizes a
147
+ * fresh Ed25519 **proof key** that only this profile holds. Sharing it with a contact is then a
148
+ * challenge-response on that contact's authenticated paired channel: the contact sends a fresh nonce,
149
+ * the proof key signs a **presentation** binding the proof to both participation keys, the
150
+ * conversation, the session and that nonce. A copy replayed to anyone else fails: wrong audience,
151
+ * unknown nonce, and nobody else has the proof key.
152
+ *
153
+ * Providers (browser side, `packages/browser/src/proofs/`) only know how to produce and check the
154
+ * evidence over the statement bytes; everything here is shared, so every provider signs the same bytes.
155
+ */
156
+ var IDENTITY_PROOF_CAPABILITY = "identity-proof/1";
157
+ /** Domain separation: the first words of every statement. */
158
+ var IDENTITY_STATEMENT_PREFIX = "Ghostly identity proof v1";
159
+ /** No binding is valid for longer than this, whatever the provider allows. */
160
+ var IDENTITY_MAX_VALIDITY = 3456e4;
161
+ /** Evidence, serialized as JSON, is at most this many characters. */
162
+ var IDENTITY_MAX_EVIDENCE = 16384;
163
+ /** A whole `idp-*` frame, serialized. */
164
+ var IDENTITY_MAX_FRAME = 32768;
165
+ /** Unanswered challenges a verifier keeps per conversation. */
166
+ var MAX_CHALLENGES = 8;
167
+ /** A challenge still open at `now` (seconds): a presentation for it is still taken. */
168
+ var liveAt = (c, now) => c.issuedAt > now - 300;
169
+ var IDENTITY_FRAME_WINDOW_MS = 6e4;
170
+ var IDENTITY_PROVIDER_ID = /^[a-z][a-z0-9-]{0,31}$/;
171
+ /** Printable ASCII without spaces, so the statement can be built but never needs parsing. */
172
+ var IDENTITY_SUBJECT = /^[\x21-\x7e]{1,512}$/;
173
+ var BINDING_NONCE = /^[A-Za-z0-9_-]{22}$/;
174
+ var CHALLENGE_NONCE = /^[A-Za-z0-9_-]{43}$/;
175
+ var HEX64 = /^[a-f0-9]{64}$/;
176
+ var SIG = /^[A-Za-z0-9_-]{86}$/;
177
+ /**
178
+ * Early revocation: the proof key publishes, under its own Pkarr key, a TXT record naming the proof it
179
+ * revokes. Anyone holding a copy can look it up by the key in the binding; nothing else is published,
180
+ * and the key is random, so the record says nothing about the identity or its contacts.
181
+ */
182
+ var IDENTITY_REVOCATION_LABEL = "_ghostly-revoked";
183
+ var identityRevocationValue = (id, at) => `v=1;id=${id};at=${at}`;
184
+ /** True when these records (resolved and signature-checked under the proof key) revoke proof `id`. */
185
+ function revokesIdentity(records, id) {
186
+ return records.some((r) => r.label === "_ghostly-revoked" && new RegExp(`^v=1;id=${id};at=\\d{1,12}$`).test(r.value));
187
+ }
188
+ var nowSeconds = () => Math.floor(Date.now() / 1e3);
189
+ var hex = (bytes) => Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
190
+ var iso = (seconds) => (/* @__PURE__ */ new Date(seconds * 1e3)).toISOString().replace(/\.\d{3}Z$/, "Z");
191
+ var isInt = (v) => Number.isSafeInteger(v) && v > 0;
192
+ function isIdentityBinding(value) {
193
+ const b = value;
194
+ return !!b && typeof b === "object" && !Array.isArray(b) && Object.keys(b).sort().join(",") === "expiresAt,issuedAt,key,nonce,provider,subject,v" && b.v === 1 && typeof b.provider === "string" && IDENTITY_PROVIDER_ID.test(b.provider) && typeof b.subject === "string" && IDENTITY_SUBJECT.test(b.subject) && isZ32Key(b.key) && typeof b.nonce === "string" && BINDING_NONCE.test(b.nonce) && isInt(b.issuedAt) && isInt(b.expiresAt) && b.expiresAt > b.issuedAt && b.expiresAt - b.issuedAt <= 3456e4 && b.expiresAt < 253402300800;
195
+ }
196
+ /**
197
+ * The canonical statement, one line, so it survives every tool and clipboard (no line-ending
198
+ * ambiguity). Every field is space-free and the order is fixed, so the text is injective.
199
+ */
200
+ function identityStatementText(b) {
201
+ if (!isIdentityBinding(b)) throw new Error("Invalid identity binding");
202
+ return `${IDENTITY_STATEMENT_PREFIX}: I control ${b.provider}:${b.subject} and authorize the Ghostly key ${b.key} to present it to contacts I choose, from ${iso(b.issuedAt)} until ${iso(b.expiresAt)}. Nonce: ${b.nonce}`;
203
+ }
204
+ function identityStatement(binding) {
205
+ const text = identityStatementText(binding);
206
+ const bytes = utf8Encode(text);
207
+ return {
208
+ binding: { ...binding },
209
+ text,
210
+ bytes,
211
+ id: hex(sha256(bytes))
212
+ };
213
+ }
214
+ /** A new binding and the seed of its proof key. The seed stays in this profile; never share it. */
215
+ function newIdentityBinding(options) {
216
+ const issuedAt = options.now ?? nowSeconds();
217
+ const seed = randomBytes(32);
218
+ const binding = {
219
+ v: 1,
220
+ provider: options.provider,
221
+ subject: options.subject,
222
+ key: identityFromSeed(seed).pubKeyZ32,
223
+ issuedAt,
224
+ expiresAt: issuedAt + options.validitySeconds,
225
+ nonce: toBase64Url(randomBytes(16))
226
+ };
227
+ if (!isIdentityBinding(binding)) {
228
+ seed.fill(0);
229
+ throw new Error("Invalid identity provider, subject or validity");
230
+ }
231
+ return {
232
+ binding,
233
+ seed
234
+ };
235
+ }
236
+ /** Bytes the proof key signs for one contact. `scope` is the presenter's view (subject = presenter). */
237
+ function identityPresentationBytes(proofId, scope, nonce, issuedAt) {
238
+ return utf8Encode(JSON.stringify([
239
+ "ghostly-identity-presentation",
240
+ 1,
241
+ proofId,
242
+ scope.subject,
243
+ scope.audience,
244
+ scope.context,
245
+ scope.session,
246
+ nonce,
247
+ issuedAt
248
+ ]));
249
+ }
250
+ function signIdentityPresentation(seed, proofId, scope, nonce, issuedAt) {
251
+ return toBase64Url(sign(identityPresentationBytes(proofId, scope, nonce, issuedAt), seed));
252
+ }
253
+ function verifyIdentityPresentation(key, sig, proofId, scope, nonce, issuedAt) {
254
+ if (typeof sig !== "string" || !SIG.test(sig) || !isZ32Key(key)) return false;
255
+ return verify(fromBase64Url(sig), identityPresentationBytes(proofId, scope, nonce, issuedAt), publicKeyFromZ32(key));
256
+ }
257
+ var clean = (value, max) => {
258
+ if (typeof value !== "string") return void 0;
259
+ const text = stripHidden(value).trim();
260
+ return text && hasVisible(text) ? text.slice(0, max) : void 0;
261
+ };
262
+ /**
263
+ * Room for a line that names a domain (up to 253 characters) once or twice: a How line or a reason cut inside the name
264
+ * names a record that does not exist, and the app no longer knows the reason to say it in the person's language.
265
+ */
266
+ var SOURCE_MAX = 640;
267
+ var ERROR_MAX = 512;
268
+ /** Bounds a provider's result so a buggy provider cannot store or show arbitrary data. */
269
+ function boundVerifiedIdentity(v) {
270
+ if (!v || typeof v.subject !== "string" || !IDENTITY_SUBJECT.test(v.subject)) throw new Error("Provider returned an invalid subject");
271
+ const source = clean(v.source, SOURCE_MAX);
272
+ if (!source) throw new Error("Provider did not say how it verified");
273
+ const out = {
274
+ subject: v.subject,
275
+ source
276
+ };
277
+ const attester = clean(v.attester, 253);
278
+ if (attester) out.attester = attester;
279
+ if (v.expiresAt !== void 0) {
280
+ if (!isInt(v.expiresAt)) throw new Error("Provider returned an invalid expiry");
281
+ out.expiresAt = v.expiresAt;
282
+ }
283
+ if (v.display) out.display = boundIdentityDisplay(v.display);
284
+ return out;
285
+ }
286
+ /** Bounds a provider's public name/picture (from a proof or `lookupDisplay`): a name with nothing visible left is dropped. */
287
+ function boundIdentityDisplay(d) {
288
+ const display = {
289
+ source: clean(d.source, 120) ?? "Self-described",
290
+ fetchedAt: isInt(d.fetchedAt) ? d.fetchedAt : nowSeconds()
291
+ };
292
+ const name = clean(d.name, 64);
293
+ if (name) display.name = name;
294
+ if (typeof d.avatar === "string" && d.avatar.length <= 49152 && /^data:image\/(png|jpeg);base64,[A-Za-z0-9+/=]+$/.test(d.avatar)) display.avatar = d.avatar;
295
+ if (typeof d.url === "string" && d.url.length <= 512) try {
296
+ const u = new URL(d.url);
297
+ if (u.protocol === "https:" && !u.username && !u.password) display.url = u.href;
298
+ } catch {}
299
+ return display;
300
+ }
301
+ var emptyIdentityLedger = () => ({
302
+ challenges: [],
303
+ shared: [],
304
+ received: []
305
+ });
306
+ /** What the verifier should show for a received proof right now. */
307
+ function receivedIdentityStatus(r, presenter, audience, now = nowSeconds()) {
308
+ if (r.presenter !== presenter || r.audience !== audience) return "previous-key";
309
+ if (r.status === "withdrawn") return "withdrawn";
310
+ if (r.status === "revoked") return "revoked";
311
+ if (Math.min(r.binding.expiresAt, r.verified.expiresAt ?? Infinity) <= now) return "expired";
312
+ return r.status === "unconfirmed" ? "unconfirmed" : "verified";
313
+ }
314
+ var standing = (r, now) => r.status !== "withdrawn" && r.status !== "revoked" && Math.min(r.binding.expiresAt, r.verified.expiresAt ?? Infinity) > now;
315
+ /**
316
+ * `received` with room for one more proof: `replaced` (the older proof of the same identity) dropped, then the oldest ones
317
+ * no longer standing; undefined when every place is still taken.
318
+ */
319
+ function receivedWithRoom(received, id, replaced, now) {
320
+ const kept = received.filter((r) => r.id !== id && !replaced(r));
321
+ const gone = kept.filter((r) => !standing(r, now)).sort((a, b) => a.verifiedAt - b.verifiedAt);
322
+ const drop = new Set(gone.slice(0, Math.max(0, kept.length - 16 + 1)));
323
+ const out = kept.filter((r) => !drop.has(r));
324
+ return out.length < 16 ? out : void 0;
325
+ }
326
+ var REVOKED = "Its owner revoked this proof";
327
+ /** Revoked is final: a later lookup that fails or finds nothing (the relays forgot it) does not undo it. */
328
+ var revokedHere = (received, id) => received.some((r) => r.id === id && r.status === "revoked");
329
+ var shortError = (e) => clean(e instanceof Error ? e.message : String(e), ERROR_MAX) ?? "Invalid proof";
330
+ /**
331
+ * A check this side could not finish because of what its own network did: a host it refused to contact, a server
332
+ * that did not answer or answered an error. The message is for this side only. The contact is told the proof could
333
+ * not be checked, never which host, address or status was seen, so a proof cannot make a contact's app report on
334
+ * the network it sits in.
335
+ */
336
+ var IdentityCheckUnavailable = class extends Error {
337
+ constructor(message) {
338
+ super(message);
339
+ this.name = "IdentityCheckUnavailable";
340
+ }
341
+ };
342
+ /** What the contact is told when the check failed with an IdentityCheckUnavailable. */
343
+ var IDENTITY_CHECK_UNAVAILABLE = "Your contact's app could not check it. Try again later.";
344
+ var TOO_MANY_RECEIVED = "Your contact keeps too many identities from you already";
345
+ var NEWER_HELD = "Your contact keeps a newer proof of this identity from you";
346
+ var peerError = (e) => e instanceof IdentityCheckUnavailable ? IDENTITY_CHECK_UNAVAILABLE : shortError(e);
347
+ var UNSUPPORTED_KIND = "Your contact's app cannot verify this kind of identity yet";
348
+ var NOT_IN_PROFILE = "That identity is no longer in your profile";
349
+ var CONTACT_REFUSED = "Your contact could not verify it";
350
+ var EXPIRED = "The proof has expired";
351
+ /** The refusals a contact's app writes itself (onRequest, onPresent): the only words of its `idp-result` kept as they came. */
352
+ var CONTACT_REFUSALS = /* @__PURE__ */ new Set([
353
+ UNSUPPORTED_KIND,
354
+ IDENTITY_CHECK_UNAVAILABLE,
355
+ TOO_MANY_RECEIVED,
356
+ REVOKED,
357
+ EXPIRED,
358
+ NEWER_HELD
359
+ ]);
360
+ /**
361
+ * Why a share of mine was not accepted, as this app may say it under my identity: one of the refusals above or my own
362
+ * app's line; else nothing past "not verified". Any other words came from the contact (a ledger kept before this check)
363
+ * and would read as this app's own notice.
364
+ */
365
+ var identityRefusal = (error) => typeof error === "string" && (CONTACT_REFUSALS.has(error) || error === NOT_IN_PROFILE) ? error : void 0;
366
+ /**
367
+ * The `idp-*` exchange of one conversation. Frames (all JSON, additive, over the paired channel,
368
+ * only when both offers carry `identity-proof/1`):
369
+ *
370
+ * idp-hello {providers} each side, when the channel is ready
371
+ * idp-request {id, provider} sharer → verifier: "give me a challenge for this proof"
372
+ * idp-challenge {id, nonce, issuedAt} verifier, after saving the nonce
373
+ * idp-present {nonce, issuedAt, binding, evidence, sig}
374
+ * idp-result {id, ok, error?} verifier, after saving the outcome
375
+ * idp-withdraw {id} / idp-withdrawn {id}
376
+ */
377
+ var IdentityExchange = class {
378
+ options;
379
+ peerProviders;
380
+ requested = /* @__PURE__ */ new Set();
381
+ /** The nonces of the presentations in their provider's check now. */
382
+ checking = /* @__PURE__ */ new Set();
383
+ /** When the contact's presentations were checked with their provider (seconds), for `IDENTITY_MAX_CHECKS_PER_HOUR`. */
384
+ checks = [];
385
+ constructor(options) {
386
+ this.options = options;
387
+ }
388
+ now() {
389
+ return (this.options.now ?? nowSeconds)();
390
+ }
391
+ /** The ledger with one more step on the chat's timeline, in milliseconds (a test's clock, in seconds, when given). */
392
+ log(l, event) {
393
+ return {
394
+ ...l,
395
+ timeline: applyIdentityEvent(l.timeline ?? [], event, this.options.now ? this.now() * 1e3 : Date.now())
396
+ };
397
+ }
398
+ incomingScope() {
399
+ const s = this.options.scope();
400
+ return {
401
+ ...s,
402
+ subject: s.audience,
403
+ audience: s.subject
404
+ };
405
+ }
406
+ send(frame) {
407
+ if (JSON.stringify(frame).length > 32768) throw new Error("Identity proof too large");
408
+ this.options.send(frame);
409
+ }
410
+ shared(id) {
411
+ return this.options.storage.read().shared.find((s) => s.id === id);
412
+ }
413
+ async setShared(entry, event) {
414
+ await this.options.storage.update((l) => {
415
+ const next = {
416
+ ...l,
417
+ shared: [...l.shared.filter((s) => s.id !== entry.id), entry]
418
+ };
419
+ return event ? this.log(next, event) : next;
420
+ });
421
+ }
422
+ /** The providers the contact said it verifies; undefined until its hello. */
423
+ get contactProviders() {
424
+ return this.peerProviders;
425
+ }
426
+ /** The channel is ready: say what we verify, then send what the person queued while offline. */
427
+ ready() {
428
+ this.options.scope();
429
+ this.send({
430
+ t: "idp-hello",
431
+ providers: [...this.options.providers()]
432
+ });
433
+ for (const s of this.options.storage.read().shared) if (s.status === "queued" || s.status === "pending") this.request(s);
434
+ else if (s.status === "withdrawal-pending") this.send({
435
+ t: "idp-withdraw",
436
+ id: s.id
437
+ });
438
+ }
439
+ stop() {
440
+ this.requested.clear();
441
+ this.peerProviders = void 0;
442
+ }
443
+ /**
444
+ * The challenges whose window ended unanswered go, and with them their requests' lines still verifying: a contact
445
+ * whose app closed, or whose proof left its profile, before it presented tells this side nothing, and its line read
446
+ * "verifying" for good. No write when none ended.
447
+ */
448
+ async expire() {
449
+ const now = this.now();
450
+ if (!this.options.storage.read().challenges.some((c) => !liveAt(c, now))) return;
451
+ await this.options.storage.update((l) => this.withoutExpired(l, now));
452
+ }
453
+ withoutExpired(l, now) {
454
+ const live = l.challenges.filter((c) => liveAt(c, now));
455
+ if (live.length === l.challenges.length) return l;
456
+ return [...new Set(l.challenges.filter((c) => c.id !== void 0 && !liveAt(c, now) && !live.some((x) => x.id === c.id)).map((c) => c.id))].reduce((out, proof) => this.log(out, {
457
+ type: "unanswered",
458
+ side: "theirs",
459
+ proof
460
+ }), {
461
+ ...l,
462
+ challenges: live
463
+ });
464
+ }
465
+ /** Share a proof of this profile with this contact: now if connected, else when it next is. */
466
+ async share(id, connected) {
467
+ const local = await this.options.localProof(id);
468
+ if (!local) throw new Error("That identity is no longer in your profile");
469
+ const { provider, subject } = local.statement.binding;
470
+ const entry = {
471
+ id,
472
+ provider,
473
+ subject,
474
+ account: local.account ?? subject,
475
+ status: "queued",
476
+ at: this.now()
477
+ };
478
+ await this.setShared(entry, {
479
+ type: "shared",
480
+ side: "mine",
481
+ proof: id,
482
+ provider,
483
+ subject
484
+ });
485
+ if (connected) this.request(entry);
486
+ }
487
+ request(s) {
488
+ this.requested.add(s.id);
489
+ this.send({
490
+ t: "idp-request",
491
+ id: s.id,
492
+ provider: s.provider
493
+ });
494
+ }
495
+ /** Stop sharing here. The contact is told (now or on reconnect); a copy it kept cannot be erased. */
496
+ async withdraw(id, connected) {
497
+ let s = this.shared(id);
498
+ if (!s || s.status === "withdrawn") return;
499
+ this.requested.delete(id);
500
+ if (s.status === "queued") {
501
+ let answered;
502
+ await this.options.storage.update((l) => {
503
+ answered = l.shared.find((x) => x.id === id && x.status === "pending");
504
+ return answered ? l : this.log({
505
+ ...l,
506
+ shared: l.shared.filter((x) => x.id !== id)
507
+ }, {
508
+ type: "unsent",
509
+ side: "mine",
510
+ proof: id
511
+ });
512
+ });
513
+ if (!answered) return;
514
+ s = answered;
515
+ }
516
+ await this.setShared({
517
+ ...s,
518
+ status: "withdrawal-pending",
519
+ at: this.now(),
520
+ error: void 0
521
+ }, s.status === "rejected" ? void 0 : {
522
+ type: "stopped",
523
+ side: "mine",
524
+ proof: id,
525
+ provider: s.provider,
526
+ subject: s.subject,
527
+ reason: "withdrawn"
528
+ });
529
+ if (connected) try {
530
+ this.send({
531
+ t: "idp-withdraw",
532
+ id
533
+ });
534
+ } catch {}
535
+ }
536
+ /**
537
+ * Run the checks again on a received proof: its revocation record, then (unless `revocationOnly`) the
538
+ * provider's check (a domain record may be gone). `keepUnavailable`: a check that could not run (IdentityCheckUnavailable)
539
+ * is thrown, and the proof is left as it was, for a check this side started on its own.
540
+ */
541
+ async recheck(id, { revocationOnly = false, keepUnavailable = false } = {}) {
542
+ const r = this.options.storage.read().received.find((x) => x.id === id);
543
+ if (!r) throw new Error("Unknown identity");
544
+ if (r.status === "revoked") return r;
545
+ if (await this.options.revoked?.(identityStatement(r.binding)).catch(() => false)) return await this.settleRecheck(id, (x) => x.status === "revoked" ? x : {
546
+ ...x,
547
+ status: "revoked",
548
+ checkedAt: this.now(),
549
+ error: void 0
550
+ }, {
551
+ type: "stopped",
552
+ side: "theirs",
553
+ proof: id,
554
+ provider: r.binding.provider,
555
+ subject: r.verified.subject,
556
+ reason: "revoked"
557
+ }) ?? r;
558
+ if (revocationOnly) return r;
559
+ let outcome;
560
+ try {
561
+ const verified = boundVerifiedIdentity(await this.options.verify(identityStatement(r.binding), r.evidence));
562
+ if (verified.subject !== r.verified.subject) throw new Error("The identity changed");
563
+ outcome = (x) => ({
564
+ ...x,
565
+ verified: {
566
+ ...verified,
567
+ display: verified.display ?? x.verified.display
568
+ },
569
+ checkedAt: this.now(),
570
+ status: "verified",
571
+ error: void 0
572
+ });
573
+ } catch (e) {
574
+ if (keepUnavailable && e instanceof IdentityCheckUnavailable) throw e;
575
+ const error = shortError(e);
576
+ outcome = (x) => ({
577
+ ...x,
578
+ checkedAt: this.now(),
579
+ status: "unconfirmed",
580
+ error
581
+ });
582
+ }
583
+ return await this.settleRecheck(id, (x) => x.status === "revoked" ? x : x.status === "withdrawn" ? {
584
+ ...outcome(x),
585
+ status: "withdrawn"
586
+ } : outcome(x)) ?? r;
587
+ }
588
+ async settleRecheck(id, outcome, event) {
589
+ let next;
590
+ await this.options.storage.update((l) => {
591
+ const x = l.received.find((y) => y.id === id);
592
+ if (!x) return l;
593
+ next = outcome(x);
594
+ const out = {
595
+ ...l,
596
+ received: l.received.map((y) => y.id === id ? next : y)
597
+ };
598
+ return event && x.status !== "revoked" ? this.log(out, event) : out;
599
+ });
600
+ return next;
601
+ }
602
+ async receive(frame) {
603
+ try {
604
+ if (JSON.stringify(frame).length > 32768) throw new Error("Identity proof too large");
605
+ const id = typeof frame.id === "string" && HEX64.test(frame.id) ? frame.id : void 0;
606
+ switch (frame.t) {
607
+ case "idp-hello":
608
+ if (Array.isArray(frame.providers) && frame.providers.length <= 64) this.peerProviders = frame.providers.filter((p) => typeof p === "string" && IDENTITY_PROVIDER_ID.test(p));
609
+ return;
610
+ case "idp-request": return await this.onRequest(id, frame.provider);
611
+ case "idp-challenge": return await this.onChallenge(id, frame.nonce, frame.issuedAt);
612
+ case "idp-present": {
613
+ const nonce = typeof frame.nonce === "string" ? frame.nonce : "";
614
+ if (this.checking.has(nonce)) throw new Error("Unknown or reused identity challenge");
615
+ this.checking.add(nonce);
616
+ try {
617
+ return await this.onPresent(frame);
618
+ } finally {
619
+ this.checking.delete(nonce);
620
+ }
621
+ }
622
+ case "idp-result": {
623
+ const s = id ? this.shared(id) : void 0;
624
+ if (!s || s.status !== "pending") return;
625
+ const ok = frame.ok === true;
626
+ const error = ok ? void 0 : typeof frame.error === "string" && CONTACT_REFUSALS.has(frame.error) ? frame.error : CONTACT_REFUSED;
627
+ const entry = {
628
+ ...s,
629
+ status: ok ? "accepted" : "rejected",
630
+ at: this.now(),
631
+ error
632
+ };
633
+ const replaced = (x) => ok && x.status === "accepted" && x.provider === s.provider && x.account !== void 0 && x.account === s.account;
634
+ await this.options.storage.update((l) => l.shared.filter((x) => x.id !== s.id && replaced(x)).reduce((out, x) => this.log(out, {
635
+ type: "stopped",
636
+ side: "mine",
637
+ proof: x.id,
638
+ provider: x.provider,
639
+ subject: x.subject,
640
+ reason: "withdrawn"
641
+ }), this.log({
642
+ ...l,
643
+ shared: [...l.shared.filter((x) => x.id !== s.id).map((x) => replaced(x) ? {
644
+ ...x,
645
+ status: "withdrawn",
646
+ at: entry.at
647
+ } : x), entry]
648
+ }, {
649
+ type: "result",
650
+ side: "mine",
651
+ proof: s.id,
652
+ ok,
653
+ error
654
+ })));
655
+ return;
656
+ }
657
+ case "idp-withdraw": {
658
+ if (!id) return;
659
+ const r = this.options.storage.read().received.find((x) => x.id === id);
660
+ if (r && r.status !== "withdrawn" && r.status !== "revoked") await this.options.storage.update((l) => this.log({
661
+ ...l,
662
+ received: l.received.map((x) => x.id === id ? {
663
+ ...x,
664
+ status: "withdrawn"
665
+ } : x)
666
+ }, {
667
+ type: "stopped",
668
+ side: "theirs",
669
+ proof: id,
670
+ provider: r.binding.provider,
671
+ subject: r.verified.subject,
672
+ reason: "withdrawn"
673
+ }));
674
+ if (this.options.storage.read().challenges.some((c) => c.id === id && !c.withdrawn)) await this.options.storage.update((l) => ({
675
+ ...l,
676
+ challenges: l.challenges.map((c) => c.id === id ? {
677
+ ...c,
678
+ withdrawn: true
679
+ } : c)
680
+ }));
681
+ this.send({
682
+ t: "idp-withdrawn",
683
+ id
684
+ });
685
+ return;
686
+ }
687
+ case "idp-withdrawn": {
688
+ const s = id ? this.shared(id) : void 0;
689
+ if (s?.status === "withdrawal-pending") await this.setShared({
690
+ ...s,
691
+ status: "withdrawn",
692
+ at: this.now()
693
+ });
694
+ return;
695
+ }
696
+ }
697
+ } catch (e) {
698
+ this.options.onError?.(shortError(e));
699
+ }
700
+ }
701
+ async onRequest(id, provider) {
702
+ if (!id) return;
703
+ if (typeof provider !== "string" || !this.options.providers().includes(provider)) {
704
+ const error = UNSUPPORTED_KIND;
705
+ this.send({
706
+ t: "idp-result",
707
+ id,
708
+ ok: false,
709
+ error
710
+ });
711
+ return;
712
+ }
713
+ this.options.scope();
714
+ const now = this.now();
715
+ const challenge = {
716
+ nonce: toBase64Url(randomBytes(32)),
717
+ issuedAt: now,
718
+ provider,
719
+ id
720
+ };
721
+ await this.options.storage.update((l) => {
722
+ if (l.challenges.filter((c) => liveAt(c, now)).length >= MAX_CHALLENGES) throw new Error("Too many identity challenges");
723
+ return this.withoutExpired(this.log({
724
+ ...l,
725
+ challenges: [...l.challenges, challenge]
726
+ }, {
727
+ type: "shared",
728
+ side: "theirs",
729
+ proof: id,
730
+ provider
731
+ }), now);
732
+ });
733
+ this.send({
734
+ t: "idp-challenge",
735
+ id,
736
+ nonce: challenge.nonce,
737
+ issuedAt: challenge.issuedAt
738
+ });
739
+ }
740
+ async onChallenge(id, nonce, issuedAt) {
741
+ if (!id || !this.requested.has(id) || typeof nonce !== "string" || !CHALLENGE_NONCE.test(nonce) || !isInt(issuedAt)) return;
742
+ const s = this.shared(id);
743
+ if (!s || s.status !== "queued" && s.status !== "pending") return;
744
+ this.requested.delete(id);
745
+ const local = await this.options.localProof(id);
746
+ if (!local) {
747
+ const error = NOT_IN_PROFILE;
748
+ await this.setShared({
749
+ ...s,
750
+ status: "rejected",
751
+ at: this.now(),
752
+ error
753
+ }, {
754
+ type: "result",
755
+ side: "mine",
756
+ proof: id,
757
+ ok: false,
758
+ error
759
+ });
760
+ return;
761
+ }
762
+ const scope = this.options.scope();
763
+ const sig = signIdentityPresentation(local.seed, id, scope, nonce, issuedAt);
764
+ let stopped = false;
765
+ await this.options.storage.update((l) => {
766
+ const x = l.shared.find((y) => y.id === id);
767
+ if (!x || x.status !== "queued" && x.status !== "pending") {
768
+ stopped = true;
769
+ return l;
770
+ }
771
+ return {
772
+ ...l,
773
+ shared: [...l.shared.filter((y) => y.id !== id), {
774
+ ...x,
775
+ status: "pending",
776
+ at: this.now(),
777
+ error: void 0
778
+ }]
779
+ };
780
+ });
781
+ if (stopped) return;
782
+ this.send({
783
+ t: "idp-present",
784
+ nonce,
785
+ issuedAt,
786
+ binding: local.statement.binding,
787
+ evidence: local.evidence,
788
+ sig
789
+ });
790
+ }
791
+ async onPresent(frame) {
792
+ const { nonce, issuedAt, binding, evidence, sig } = frame;
793
+ if (typeof nonce !== "string" || !CHALLENGE_NONCE.test(nonce) || !isInt(issuedAt) || !isIdentityBinding(binding)) return;
794
+ const statement = identityStatement(binding);
795
+ const id = statement.id;
796
+ const scope = this.incomingScope();
797
+ const now = this.now();
798
+ const challengeOf = (l) => l.challenges.find((c) => c.nonce === nonce && c.issuedAt === issuedAt && c.provider === binding.provider && liveAt(c, now));
799
+ const known = (l) => !!challengeOf(l);
800
+ if (!known(this.options.storage.read())) throw new Error("Unknown or reused identity challenge");
801
+ let verified;
802
+ let error;
803
+ /** The same, as this side's own timeline says it. */
804
+ let reason;
805
+ /** Its owner withdrew the proof while this check ran: no result goes out (see below). */
806
+ let withdrawn = false;
807
+ /** The check could not run on this side (IdentityCheckUnavailable). */
808
+ let unavailable = false;
809
+ /** The same proof this side holds verified, checked recently: its answer is reused (`IDENTITY_REUSE_CHECK_S`). */
810
+ let reused;
811
+ try {
812
+ const mayReplace = (r) => r.binding.provider === binding.provider && (r.binding.subject === binding.subject || r.verified.subject === binding.subject);
813
+ if (revokedHere(this.options.storage.read().received, id)) throw new Error(REVOKED);
814
+ if (!receivedWithRoom(this.options.storage.read().received, id, mayReplace, now)) throw new Error(TOO_MANY_RECEIVED);
815
+ if (JSON.stringify(evidence ?? null).length > 16384) throw new Error("Evidence too large");
816
+ if (!verifyIdentityPresentation(binding.key, sig, id, scope, nonce, issuedAt)) throw new Error("The proof key did not sign this conversation's challenge");
817
+ if (binding.issuedAt > now + 300) throw new Error("The proof is dated in the future");
818
+ const evidenceJson = JSON.stringify(evidence ?? null);
819
+ reused = this.options.storage.read().received.find((r) => r.id === id && r.status === "verified" && r.presenter === scope.subject && r.audience === scope.audience && r.context === scope.context && r.checkedAt <= now && now - r.checkedAt < 3600 && JSON.stringify(r.evidence ?? null) === evidenceJson);
820
+ if (!reused) {
821
+ this.checks = this.checks.filter((t) => t > now - 3600 && t <= now);
822
+ if (this.checks.length >= 20) throw new IdentityCheckUnavailable("Too many of this contact's proofs to check this hour");
823
+ this.checks.push(now);
824
+ }
825
+ const result = reused ? reused.verified : boundVerifiedIdentity(await this.options.verify(statement, evidence));
826
+ if (Math.min(binding.expiresAt, result.expiresAt ?? Infinity) <= now) throw new Error(EXPIRED);
827
+ if (!reused && await this.options.revoked?.(statement).catch(() => false)) throw new Error(REVOKED);
828
+ verified = result;
829
+ } catch (e) {
830
+ error = peerError(e);
831
+ reason = shortError(e);
832
+ unavailable = e instanceof IdentityCheckUnavailable;
833
+ }
834
+ await this.options.storage.update((l) => {
835
+ const challenge = challengeOf(l);
836
+ if (!challenge) throw new Error("Unknown or reused identity challenge");
837
+ const challenges = l.challenges.filter((c) => c.nonce !== nonce);
838
+ withdrawn = !!challenge.withdrawn;
839
+ if (withdrawn && (challenges.some((c) => c.id === challenge.id && !c.withdrawn) || l.received.some((r) => r.id === challenge.id && r.status !== "withdrawn" && r.status !== "revoked"))) return {
840
+ ...l,
841
+ challenges
842
+ };
843
+ const subject = verified?.subject;
844
+ if (verified && revokedHere(l.received, id)) {
845
+ verified = void 0;
846
+ error = reason = REVOKED;
847
+ }
848
+ if (verified && l.received.some((r) => r.id !== id && r.binding.provider === binding.provider && r.verified.subject === subject && r.binding.issuedAt > binding.issuedAt && standing(r, now))) {
849
+ verified = void 0;
850
+ error = reason = NEWER_HELD;
851
+ }
852
+ const prior = reused && l.received.find((r) => r.id === id);
853
+ if (verified && reused && prior?.status !== (withdrawn ? "withdrawn" : "verified")) {
854
+ verified = void 0;
855
+ error = IDENTITY_CHECK_UNAVAILABLE;
856
+ reason = "It was checked again while it was presented";
857
+ }
858
+ const received = verified && receivedWithRoom(l.received, id, (r) => r.binding.provider === binding.provider && r.verified.subject === subject, now);
859
+ if (verified && !received) {
860
+ verified = void 0;
861
+ error = reason = TOO_MANY_RECEIVED;
862
+ }
863
+ const held = verified ? void 0 : l.received.find((r) => r.id === id && r.presenter === scope.subject && r.audience === scope.audience && (r.status === "verified" || r.status === "unconfirmed"));
864
+ const stands = held?.status === "verified" && unavailable;
865
+ if (stands) {
866
+ verified = held.verified;
867
+ error = reason = void 0;
868
+ }
869
+ const logged = this.log(l, {
870
+ type: "result",
871
+ side: "theirs",
872
+ proof: id,
873
+ requested: challenge.id,
874
+ provider: binding.provider,
875
+ subject: verified?.subject ?? binding.subject,
876
+ ok: !!verified,
877
+ ...reason ? { error: reason } : {}
878
+ });
879
+ if (stands) return {
880
+ ...logged,
881
+ challenges
882
+ };
883
+ if (!verified || !received) return {
884
+ ...logged,
885
+ challenges,
886
+ received: held && !unavailable ? l.received.map((r) => r === held ? {
887
+ ...r,
888
+ checkedAt: now,
889
+ status: "unconfirmed",
890
+ error: reason
891
+ } : r) : l.received
892
+ };
893
+ const record = {
894
+ id,
895
+ binding,
896
+ evidence,
897
+ verified,
898
+ presenter: scope.subject,
899
+ audience: scope.audience,
900
+ context: scope.context,
901
+ verifiedAt: prior?.verifiedAt ?? now,
902
+ checkedAt: prior?.checkedAt ?? now,
903
+ status: withdrawn ? "withdrawn" : "verified"
904
+ };
905
+ const kept = l.received.filter((r) => r.id !== id && !received.includes(r) && r.binding.provider === binding.provider && r.verified.subject === subject && r.status !== "withdrawn" && r.status !== "revoked").reduce((out, r) => this.log(out, {
906
+ type: "stopped",
907
+ side: "theirs",
908
+ proof: r.id,
909
+ provider: r.binding.provider,
910
+ subject: r.verified.subject,
911
+ reason: "withdrawn"
912
+ }), {
913
+ ...logged,
914
+ challenges,
915
+ received: [...received, record]
916
+ });
917
+ return withdrawn ? this.log(kept, {
918
+ type: "stopped",
919
+ side: "theirs",
920
+ proof: id,
921
+ provider: binding.provider,
922
+ subject: verified.subject,
923
+ reason: "withdrawn"
924
+ }) : kept;
925
+ });
926
+ if (!withdrawn) this.send(verified ? {
927
+ t: "idp-result",
928
+ id,
929
+ ok: true
930
+ } : {
931
+ t: "idp-result",
932
+ id,
933
+ ok: false,
934
+ error
935
+ });
936
+ }
937
+ };
938
+ export { receivedIdentityStatus as _, IDENTITY_PROOF_CAPABILITY as a, IdentityExchange as c, emptyIdentityLedger as d, identityRefusal as f, newIdentityBinding as g, isIdentityBinding as h, IDENTITY_MAX_VALIDITY as i, boundIdentityDisplay as l, identityStatement as m, IDENTITY_MAX_EVIDENCE as n, IDENTITY_REVOCATION_LABEL as o, identityRevocationValue as p, IDENTITY_MAX_FRAME as r, IdentityCheckUnavailable as s, IDENTITY_FRAME_WINDOW_MS as t, boundVerifiedIdentity as u, revokesIdentity as v, lastSharedWithMe as y };
939
+
940
+ //# sourceMappingURL=identityProofs-Cksc_K7_.js.map