@did-btcr2/method 0.25.3 → 0.27.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 (406) hide show
  1. package/README.md +86 -233
  2. package/dist/.tsbuildinfo +1 -0
  3. package/dist/browser.js +129207 -122358
  4. package/dist/browser.mjs +129235 -122386
  5. package/dist/cjs/index.js +5332 -43
  6. package/dist/esm/core/aggregation/cohort.js +178 -0
  7. package/dist/esm/core/aggregation/cohort.js.map +1 -0
  8. package/dist/esm/core/aggregation/errors.js +22 -0
  9. package/dist/esm/core/aggregation/errors.js.map +1 -0
  10. package/dist/esm/core/{beacon/aggregation/cohort → aggregation}/messages/base.js +0 -1
  11. package/dist/esm/core/aggregation/messages/base.js.map +1 -0
  12. package/dist/esm/core/aggregation/messages/constants.js +26 -0
  13. package/dist/esm/core/aggregation/messages/constants.js.map +1 -0
  14. package/dist/esm/core/aggregation/messages/factories.js +113 -0
  15. package/dist/esm/core/aggregation/messages/factories.js.map +1 -0
  16. package/dist/esm/core/aggregation/messages/guards.js +37 -0
  17. package/dist/esm/core/aggregation/messages/guards.js.map +1 -0
  18. package/dist/esm/core/aggregation/messages/index.js +5 -0
  19. package/dist/esm/core/aggregation/messages/index.js.map +1 -0
  20. package/dist/esm/core/aggregation/participant.js +376 -0
  21. package/dist/esm/core/aggregation/participant.js.map +1 -0
  22. package/dist/esm/core/aggregation/phases.js +39 -0
  23. package/dist/esm/core/aggregation/phases.js.map +1 -0
  24. package/dist/esm/core/aggregation/runner/events.js +2 -0
  25. package/dist/esm/core/aggregation/runner/events.js.map +1 -0
  26. package/dist/esm/core/aggregation/runner/index.js +5 -0
  27. package/dist/esm/core/aggregation/runner/index.js.map +1 -0
  28. package/dist/esm/core/aggregation/runner/participant-runner.js +282 -0
  29. package/dist/esm/core/aggregation/runner/participant-runner.js.map +1 -0
  30. package/dist/esm/core/aggregation/runner/service-runner.js +290 -0
  31. package/dist/esm/core/aggregation/runner/service-runner.js.map +1 -0
  32. package/dist/esm/core/aggregation/runner/typed-emitter.js +80 -0
  33. package/dist/esm/core/aggregation/runner/typed-emitter.js.map +1 -0
  34. package/dist/esm/core/aggregation/service.js +416 -0
  35. package/dist/esm/core/aggregation/service.js.map +1 -0
  36. package/dist/esm/core/aggregation/signing-session.js +133 -0
  37. package/dist/esm/core/aggregation/signing-session.js.map +1 -0
  38. package/dist/esm/core/aggregation/transport/didcomm.js +32 -0
  39. package/dist/esm/core/aggregation/transport/didcomm.js.map +1 -0
  40. package/dist/esm/core/aggregation/transport/error.js +12 -0
  41. package/dist/esm/core/aggregation/transport/error.js.map +1 -0
  42. package/dist/esm/core/aggregation/transport/factory.js +20 -0
  43. package/dist/esm/core/aggregation/transport/factory.js.map +1 -0
  44. package/dist/esm/core/aggregation/transport/index.js +6 -0
  45. package/dist/esm/core/aggregation/transport/index.js.map +1 -0
  46. package/dist/esm/core/aggregation/transport/nostr.js +262 -0
  47. package/dist/esm/core/aggregation/transport/nostr.js.map +1 -0
  48. package/dist/esm/core/aggregation/transport/transport.js +2 -0
  49. package/dist/esm/core/aggregation/transport/transport.js.map +1 -0
  50. package/dist/esm/core/beacon/beacon.js +80 -0
  51. package/dist/esm/core/beacon/beacon.js.map +1 -1
  52. package/dist/esm/core/beacon/cas-beacon.js +21 -60
  53. package/dist/esm/core/beacon/cas-beacon.js.map +1 -1
  54. package/dist/esm/core/beacon/error.js +0 -10
  55. package/dist/esm/core/beacon/error.js.map +1 -1
  56. package/dist/esm/core/beacon/fee-estimator.js +30 -0
  57. package/dist/esm/core/beacon/fee-estimator.js.map +1 -0
  58. package/dist/esm/core/beacon/signal-discovery.js +1 -1
  59. package/dist/esm/core/beacon/signal-discovery.js.map +1 -1
  60. package/dist/esm/core/beacon/singleton-beacon.js +13 -56
  61. package/dist/esm/core/beacon/singleton-beacon.js.map +1 -1
  62. package/dist/esm/core/beacon/smt-beacon.js +85 -9
  63. package/dist/esm/core/beacon/smt-beacon.js.map +1 -1
  64. package/dist/esm/core/beacon/utils.js.map +1 -1
  65. package/dist/esm/core/identifier.js +13 -0
  66. package/dist/esm/core/identifier.js.map +1 -1
  67. package/dist/esm/core/resolver.js +50 -35
  68. package/dist/esm/core/resolver.js.map +1 -1
  69. package/dist/esm/core/update.js.map +1 -1
  70. package/dist/esm/did-btcr2.js +1 -1
  71. package/dist/esm/did-btcr2.js.map +1 -1
  72. package/dist/esm/index.js +14 -24
  73. package/dist/esm/index.js.map +1 -1
  74. package/dist/esm/utils/appendix.js +7 -6
  75. package/dist/esm/utils/appendix.js.map +1 -1
  76. package/dist/esm/utils/did-document-builder.js.map +1 -1
  77. package/dist/esm/utils/did-document.js +2 -2
  78. package/dist/esm/utils/did-document.js.map +1 -1
  79. package/dist/types/core/aggregation/cohort.d.ts +94 -0
  80. package/dist/types/core/aggregation/cohort.d.ts.map +1 -0
  81. package/dist/types/core/aggregation/errors.d.ts +14 -0
  82. package/dist/types/core/aggregation/errors.d.ts.map +1 -0
  83. package/dist/types/core/{beacon/aggregation/cohort → aggregation}/messages/base.d.ts +7 -1
  84. package/dist/types/core/aggregation/messages/base.d.ts.map +1 -0
  85. package/dist/types/core/aggregation/messages/constants.d.ts +23 -0
  86. package/dist/types/core/aggregation/messages/constants.d.ts.map +1 -0
  87. package/dist/types/core/aggregation/messages/factories.d.ts +177 -0
  88. package/dist/types/core/aggregation/messages/factories.d.ts.map +1 -0
  89. package/dist/types/core/aggregation/messages/guards.d.ts +11 -0
  90. package/dist/types/core/aggregation/messages/guards.d.ts.map +1 -0
  91. package/dist/types/core/aggregation/messages/index.d.ts +5 -0
  92. package/dist/types/core/aggregation/messages/index.d.ts.map +1 -0
  93. package/dist/types/core/aggregation/participant.d.ts +101 -0
  94. package/dist/types/core/aggregation/participant.d.ts.map +1 -0
  95. package/dist/types/core/aggregation/phases.d.ts +49 -0
  96. package/dist/types/core/aggregation/phases.d.ts.map +1 -0
  97. package/dist/types/core/aggregation/runner/events.d.ts +89 -0
  98. package/dist/types/core/aggregation/runner/events.d.ts.map +1 -0
  99. package/dist/types/core/aggregation/runner/index.d.ts +5 -0
  100. package/dist/types/core/aggregation/runner/index.d.ts.map +1 -0
  101. package/dist/types/core/aggregation/runner/participant-runner.d.ts +107 -0
  102. package/dist/types/core/aggregation/runner/participant-runner.d.ts.map +1 -0
  103. package/dist/types/core/aggregation/runner/service-runner.d.ts +102 -0
  104. package/dist/types/core/aggregation/runner/service-runner.d.ts.map +1 -0
  105. package/dist/types/core/aggregation/runner/typed-emitter.d.ts +41 -0
  106. package/dist/types/core/aggregation/runner/typed-emitter.d.ts.map +1 -0
  107. package/dist/types/core/aggregation/service.d.ts +112 -0
  108. package/dist/types/core/aggregation/service.d.ts.map +1 -0
  109. package/dist/types/core/aggregation/signing-session.d.ts +69 -0
  110. package/dist/types/core/aggregation/signing-session.d.ts.map +1 -0
  111. package/dist/types/core/aggregation/transport/didcomm.d.ts +20 -0
  112. package/dist/types/core/aggregation/transport/didcomm.d.ts.map +1 -0
  113. package/dist/types/core/{beacon/aggregation/communication → aggregation/transport}/error.d.ts +2 -2
  114. package/dist/types/core/aggregation/transport/error.d.ts.map +1 -0
  115. package/dist/types/core/aggregation/transport/factory.d.ts +13 -0
  116. package/dist/types/core/aggregation/transport/factory.d.ts.map +1 -0
  117. package/dist/types/core/aggregation/transport/index.d.ts +6 -0
  118. package/dist/types/core/aggregation/transport/index.d.ts.map +1 -0
  119. package/dist/types/core/aggregation/transport/nostr.d.ts +55 -0
  120. package/dist/types/core/aggregation/transport/nostr.d.ts.map +1 -0
  121. package/dist/types/core/aggregation/transport/transport.d.ts +37 -0
  122. package/dist/types/core/aggregation/transport/transport.d.ts.map +1 -0
  123. package/dist/types/core/beacon/beacon.d.ts +41 -6
  124. package/dist/types/core/beacon/beacon.d.ts.map +1 -1
  125. package/dist/types/core/beacon/cas-beacon.d.ts +23 -11
  126. package/dist/types/core/beacon/cas-beacon.d.ts.map +1 -1
  127. package/dist/types/core/beacon/error.d.ts +0 -6
  128. package/dist/types/core/beacon/error.d.ts.map +1 -1
  129. package/dist/types/core/beacon/factory.d.ts +2 -2
  130. package/dist/types/core/beacon/factory.d.ts.map +1 -1
  131. package/dist/types/core/beacon/fee-estimator.d.ts +40 -0
  132. package/dist/types/core/beacon/fee-estimator.d.ts.map +1 -0
  133. package/dist/types/core/beacon/interfaces.d.ts +11 -3
  134. package/dist/types/core/beacon/interfaces.d.ts.map +1 -1
  135. package/dist/types/core/beacon/signal-discovery.d.ts +3 -3
  136. package/dist/types/core/beacon/signal-discovery.d.ts.map +1 -1
  137. package/dist/types/core/beacon/singleton-beacon.d.ts +14 -7
  138. package/dist/types/core/beacon/singleton-beacon.d.ts.map +1 -1
  139. package/dist/types/core/beacon/smt-beacon.d.ts +32 -12
  140. package/dist/types/core/beacon/smt-beacon.d.ts.map +1 -1
  141. package/dist/types/core/beacon/utils.d.ts +4 -4
  142. package/dist/types/core/beacon/utils.d.ts.map +1 -1
  143. package/dist/types/core/identifier.d.ts +10 -2
  144. package/dist/types/core/identifier.d.ts.map +1 -1
  145. package/dist/types/core/interfaces.d.ts +4 -4
  146. package/dist/types/core/interfaces.d.ts.map +1 -1
  147. package/dist/types/core/resolver.d.ts +19 -9
  148. package/dist/types/core/resolver.d.ts.map +1 -1
  149. package/dist/types/core/types.d.ts +6 -5
  150. package/dist/types/core/types.d.ts.map +1 -1
  151. package/dist/types/core/update.d.ts +5 -5
  152. package/dist/types/core/update.d.ts.map +1 -1
  153. package/dist/types/did-btcr2.d.ts +6 -6
  154. package/dist/types/did-btcr2.d.ts.map +1 -1
  155. package/dist/types/index.d.ts +9 -24
  156. package/dist/types/index.d.ts.map +1 -1
  157. package/dist/types/utils/appendix.d.ts +7 -7
  158. package/dist/types/utils/appendix.d.ts.map +1 -1
  159. package/dist/types/utils/did-document-builder.d.ts +3 -2
  160. package/dist/types/utils/did-document-builder.d.ts.map +1 -1
  161. package/dist/types/utils/did-document.d.ts +3 -3
  162. package/dist/types/utils/did-document.d.ts.map +1 -1
  163. package/package.json +35 -33
  164. package/src/core/aggregation/cohort.ts +247 -0
  165. package/src/core/aggregation/errors.ts +25 -0
  166. package/src/core/{beacon/aggregation/cohort → aggregation}/messages/base.ts +8 -3
  167. package/src/core/aggregation/messages/constants.ts +28 -0
  168. package/src/core/aggregation/messages/factories.ts +240 -0
  169. package/src/core/aggregation/messages/guards.ts +55 -0
  170. package/src/core/aggregation/messages/index.ts +4 -0
  171. package/src/core/aggregation/participant.ts +510 -0
  172. package/src/core/aggregation/phases.ts +82 -0
  173. package/src/core/aggregation/runner/events.ts +77 -0
  174. package/src/core/aggregation/runner/index.ts +4 -0
  175. package/src/core/aggregation/runner/participant-runner.ts +360 -0
  176. package/src/core/aggregation/runner/service-runner.ts +365 -0
  177. package/src/core/aggregation/runner/typed-emitter.ts +87 -0
  178. package/src/core/aggregation/service.ts +547 -0
  179. package/src/core/aggregation/signing-session.ts +209 -0
  180. package/src/core/aggregation/transport/didcomm.ts +42 -0
  181. package/src/core/aggregation/transport/error.ts +13 -0
  182. package/src/core/aggregation/transport/factory.ts +29 -0
  183. package/src/core/aggregation/transport/index.ts +5 -0
  184. package/src/core/aggregation/transport/nostr.ts +333 -0
  185. package/src/core/aggregation/transport/transport.ts +46 -0
  186. package/src/core/beacon/beacon.ts +126 -6
  187. package/src/core/beacon/cas-beacon.ts +39 -83
  188. package/src/core/beacon/error.ts +0 -12
  189. package/src/core/beacon/factory.ts +2 -2
  190. package/src/core/beacon/fee-estimator.ts +52 -0
  191. package/src/core/beacon/interfaces.ts +13 -4
  192. package/src/core/beacon/signal-discovery.ts +5 -4
  193. package/src/core/beacon/singleton-beacon.ts +21 -81
  194. package/src/core/beacon/smt-beacon.ts +113 -15
  195. package/src/core/beacon/utils.ts +6 -4
  196. package/src/core/identifier.ts +20 -2
  197. package/src/core/interfaces.ts +4 -4
  198. package/src/core/resolver.ts +84 -53
  199. package/src/core/types.ts +6 -5
  200. package/src/core/update.ts +11 -7
  201. package/src/did-btcr2.ts +12 -10
  202. package/src/index.ts +15 -29
  203. package/src/utils/appendix.ts +14 -16
  204. package/src/utils/did-document-builder.ts +3 -2
  205. package/src/utils/did-document.ts +10 -8
  206. package/dist/cjs/core/beacon/aggregation/cohort/index.js +0 -237
  207. package/dist/cjs/core/beacon/aggregation/cohort/index.js.map +0 -1
  208. package/dist/cjs/core/beacon/aggregation/cohort/messages/base.js +0 -26
  209. package/dist/cjs/core/beacon/aggregation/cohort/messages/base.js.map +0 -1
  210. package/dist/cjs/core/beacon/aggregation/cohort/messages/constants.js +0 -11
  211. package/dist/cjs/core/beacon/aggregation/cohort/messages/constants.js.map +0 -1
  212. package/dist/cjs/core/beacon/aggregation/cohort/messages/index.js +0 -98
  213. package/dist/cjs/core/beacon/aggregation/cohort/messages/index.js.map +0 -1
  214. package/dist/cjs/core/beacon/aggregation/cohort/messages/keygen/cohort-advert.js +0 -31
  215. package/dist/cjs/core/beacon/aggregation/cohort/messages/keygen/cohort-advert.js.map +0 -1
  216. package/dist/cjs/core/beacon/aggregation/cohort/messages/keygen/cohort-ready.js +0 -29
  217. package/dist/cjs/core/beacon/aggregation/cohort/messages/keygen/cohort-ready.js.map +0 -1
  218. package/dist/cjs/core/beacon/aggregation/cohort/messages/keygen/opt-in-accept.js +0 -27
  219. package/dist/cjs/core/beacon/aggregation/cohort/messages/keygen/opt-in-accept.js.map +0 -1
  220. package/dist/cjs/core/beacon/aggregation/cohort/messages/keygen/opt-in.js +0 -23
  221. package/dist/cjs/core/beacon/aggregation/cohort/messages/keygen/opt-in.js.map +0 -1
  222. package/dist/cjs/core/beacon/aggregation/cohort/messages/keygen/subscribe.js +0 -28
  223. package/dist/cjs/core/beacon/aggregation/cohort/messages/keygen/subscribe.js.map +0 -1
  224. package/dist/cjs/core/beacon/aggregation/cohort/messages/sign/aggregated-nonce.js +0 -29
  225. package/dist/cjs/core/beacon/aggregation/cohort/messages/sign/aggregated-nonce.js.map +0 -1
  226. package/dist/cjs/core/beacon/aggregation/cohort/messages/sign/authorization-request.js +0 -30
  227. package/dist/cjs/core/beacon/aggregation/cohort/messages/sign/authorization-request.js.map +0 -1
  228. package/dist/cjs/core/beacon/aggregation/cohort/messages/sign/nonce-contribution.js +0 -30
  229. package/dist/cjs/core/beacon/aggregation/cohort/messages/sign/nonce-contribution.js.map +0 -1
  230. package/dist/cjs/core/beacon/aggregation/cohort/messages/sign/request-signature.js +0 -30
  231. package/dist/cjs/core/beacon/aggregation/cohort/messages/sign/request-signature.js.map +0 -1
  232. package/dist/cjs/core/beacon/aggregation/cohort/messages/sign/signature-authorization.js +0 -31
  233. package/dist/cjs/core/beacon/aggregation/cohort/messages/sign/signature-authorization.js.map +0 -1
  234. package/dist/cjs/core/beacon/aggregation/cohort/status.js +0 -8
  235. package/dist/cjs/core/beacon/aggregation/cohort/status.js.map +0 -1
  236. package/dist/cjs/core/beacon/aggregation/communication/adapter/did-comm.js +0 -121
  237. package/dist/cjs/core/beacon/aggregation/communication/adapter/did-comm.js.map +0 -1
  238. package/dist/cjs/core/beacon/aggregation/communication/adapter/nostr.js +0 -246
  239. package/dist/cjs/core/beacon/aggregation/communication/adapter/nostr.js.map +0 -1
  240. package/dist/cjs/core/beacon/aggregation/communication/error.js +0 -12
  241. package/dist/cjs/core/beacon/aggregation/communication/error.js.map +0 -1
  242. package/dist/cjs/core/beacon/aggregation/communication/factory.js +0 -21
  243. package/dist/cjs/core/beacon/aggregation/communication/factory.js.map +0 -1
  244. package/dist/cjs/core/beacon/aggregation/communication/service.js +0 -2
  245. package/dist/cjs/core/beacon/aggregation/communication/service.js.map +0 -1
  246. package/dist/cjs/core/beacon/aggregation/coordinator.js +0 -343
  247. package/dist/cjs/core/beacon/aggregation/coordinator.js.map +0 -1
  248. package/dist/cjs/core/beacon/aggregation/participant.js +0 -435
  249. package/dist/cjs/core/beacon/aggregation/participant.js.map +0 -1
  250. package/dist/cjs/core/beacon/aggregation/session/index.js +0 -244
  251. package/dist/cjs/core/beacon/aggregation/session/index.js.map +0 -1
  252. package/dist/cjs/core/beacon/aggregation/session/status.js +0 -11
  253. package/dist/cjs/core/beacon/aggregation/session/status.js.map +0 -1
  254. package/dist/cjs/core/beacon/beacon.js +0 -25
  255. package/dist/cjs/core/beacon/beacon.js.map +0 -1
  256. package/dist/cjs/core/beacon/cas-beacon.js +0 -152
  257. package/dist/cjs/core/beacon/cas-beacon.js.map +0 -1
  258. package/dist/cjs/core/beacon/error.js +0 -37
  259. package/dist/cjs/core/beacon/error.js.map +0 -1
  260. package/dist/cjs/core/beacon/factory.js +0 -29
  261. package/dist/cjs/core/beacon/factory.js.map +0 -1
  262. package/dist/cjs/core/beacon/interfaces.js +0 -2
  263. package/dist/cjs/core/beacon/interfaces.js.map +0 -1
  264. package/dist/cjs/core/beacon/signal-discovery.js +0 -183
  265. package/dist/cjs/core/beacon/signal-discovery.js.map +0 -1
  266. package/dist/cjs/core/beacon/singleton-beacon.js +0 -107
  267. package/dist/cjs/core/beacon/singleton-beacon.js.map +0 -1
  268. package/dist/cjs/core/beacon/smt-beacon.js +0 -39
  269. package/dist/cjs/core/beacon/smt-beacon.js.map +0 -1
  270. package/dist/cjs/core/beacon/utils.js +0 -163
  271. package/dist/cjs/core/beacon/utils.js.map +0 -1
  272. package/dist/cjs/core/identifier.js +0 -248
  273. package/dist/cjs/core/identifier.js.map +0 -1
  274. package/dist/cjs/core/interfaces.js +0 -2
  275. package/dist/cjs/core/interfaces.js.map +0 -1
  276. package/dist/cjs/core/resolver.js +0 -476
  277. package/dist/cjs/core/resolver.js.map +0 -1
  278. package/dist/cjs/core/types.js +0 -2
  279. package/dist/cjs/core/types.js.map +0 -1
  280. package/dist/cjs/core/update.js +0 -112
  281. package/dist/cjs/core/update.js.map +0 -1
  282. package/dist/cjs/did-btcr2.js +0 -193
  283. package/dist/cjs/did-btcr2.js.map +0 -1
  284. package/dist/cjs/index.js.map +0 -1
  285. package/dist/cjs/utils/appendix.js +0 -203
  286. package/dist/cjs/utils/appendix.js.map +0 -1
  287. package/dist/cjs/utils/did-document-builder.js +0 -60
  288. package/dist/cjs/utils/did-document-builder.js.map +0 -1
  289. package/dist/cjs/utils/did-document.js +0 -424
  290. package/dist/cjs/utils/did-document.js.map +0 -1
  291. package/dist/esm/core/beacon/aggregation/cohort/index.js +0 -237
  292. package/dist/esm/core/beacon/aggregation/cohort/index.js.map +0 -1
  293. package/dist/esm/core/beacon/aggregation/cohort/messages/base.js.map +0 -1
  294. package/dist/esm/core/beacon/aggregation/cohort/messages/constants.js +0 -11
  295. package/dist/esm/core/beacon/aggregation/cohort/messages/constants.js.map +0 -1
  296. package/dist/esm/core/beacon/aggregation/cohort/messages/index.js +0 -98
  297. package/dist/esm/core/beacon/aggregation/cohort/messages/index.js.map +0 -1
  298. package/dist/esm/core/beacon/aggregation/cohort/messages/keygen/cohort-advert.js +0 -31
  299. package/dist/esm/core/beacon/aggregation/cohort/messages/keygen/cohort-advert.js.map +0 -1
  300. package/dist/esm/core/beacon/aggregation/cohort/messages/keygen/cohort-ready.js +0 -29
  301. package/dist/esm/core/beacon/aggregation/cohort/messages/keygen/cohort-ready.js.map +0 -1
  302. package/dist/esm/core/beacon/aggregation/cohort/messages/keygen/opt-in-accept.js +0 -27
  303. package/dist/esm/core/beacon/aggregation/cohort/messages/keygen/opt-in-accept.js.map +0 -1
  304. package/dist/esm/core/beacon/aggregation/cohort/messages/keygen/opt-in.js +0 -23
  305. package/dist/esm/core/beacon/aggregation/cohort/messages/keygen/opt-in.js.map +0 -1
  306. package/dist/esm/core/beacon/aggregation/cohort/messages/keygen/subscribe.js +0 -28
  307. package/dist/esm/core/beacon/aggregation/cohort/messages/keygen/subscribe.js.map +0 -1
  308. package/dist/esm/core/beacon/aggregation/cohort/messages/sign/aggregated-nonce.js +0 -29
  309. package/dist/esm/core/beacon/aggregation/cohort/messages/sign/aggregated-nonce.js.map +0 -1
  310. package/dist/esm/core/beacon/aggregation/cohort/messages/sign/authorization-request.js +0 -30
  311. package/dist/esm/core/beacon/aggregation/cohort/messages/sign/authorization-request.js.map +0 -1
  312. package/dist/esm/core/beacon/aggregation/cohort/messages/sign/nonce-contribution.js +0 -30
  313. package/dist/esm/core/beacon/aggregation/cohort/messages/sign/nonce-contribution.js.map +0 -1
  314. package/dist/esm/core/beacon/aggregation/cohort/messages/sign/request-signature.js +0 -30
  315. package/dist/esm/core/beacon/aggregation/cohort/messages/sign/request-signature.js.map +0 -1
  316. package/dist/esm/core/beacon/aggregation/cohort/messages/sign/signature-authorization.js +0 -31
  317. package/dist/esm/core/beacon/aggregation/cohort/messages/sign/signature-authorization.js.map +0 -1
  318. package/dist/esm/core/beacon/aggregation/cohort/status.js +0 -8
  319. package/dist/esm/core/beacon/aggregation/cohort/status.js.map +0 -1
  320. package/dist/esm/core/beacon/aggregation/communication/adapter/did-comm.js +0 -121
  321. package/dist/esm/core/beacon/aggregation/communication/adapter/did-comm.js.map +0 -1
  322. package/dist/esm/core/beacon/aggregation/communication/adapter/nostr.js +0 -246
  323. package/dist/esm/core/beacon/aggregation/communication/adapter/nostr.js.map +0 -1
  324. package/dist/esm/core/beacon/aggregation/communication/error.js +0 -12
  325. package/dist/esm/core/beacon/aggregation/communication/error.js.map +0 -1
  326. package/dist/esm/core/beacon/aggregation/communication/factory.js +0 -21
  327. package/dist/esm/core/beacon/aggregation/communication/factory.js.map +0 -1
  328. package/dist/esm/core/beacon/aggregation/communication/service.js +0 -2
  329. package/dist/esm/core/beacon/aggregation/communication/service.js.map +0 -1
  330. package/dist/esm/core/beacon/aggregation/coordinator.js +0 -343
  331. package/dist/esm/core/beacon/aggregation/coordinator.js.map +0 -1
  332. package/dist/esm/core/beacon/aggregation/participant.js +0 -435
  333. package/dist/esm/core/beacon/aggregation/participant.js.map +0 -1
  334. package/dist/esm/core/beacon/aggregation/session/index.js +0 -244
  335. package/dist/esm/core/beacon/aggregation/session/index.js.map +0 -1
  336. package/dist/esm/core/beacon/aggregation/session/status.js +0 -11
  337. package/dist/esm/core/beacon/aggregation/session/status.js.map +0 -1
  338. package/dist/types/core/beacon/aggregation/cohort/index.d.ts +0 -136
  339. package/dist/types/core/beacon/aggregation/cohort/index.d.ts.map +0 -1
  340. package/dist/types/core/beacon/aggregation/cohort/messages/base.d.ts.map +0 -1
  341. package/dist/types/core/beacon/aggregation/cohort/messages/constants.d.ts +0 -11
  342. package/dist/types/core/beacon/aggregation/cohort/messages/constants.d.ts.map +0 -1
  343. package/dist/types/core/beacon/aggregation/cohort/messages/index.d.ts +0 -65
  344. package/dist/types/core/beacon/aggregation/cohort/messages/index.d.ts.map +0 -1
  345. package/dist/types/core/beacon/aggregation/cohort/messages/keygen/cohort-advert.d.ts +0 -29
  346. package/dist/types/core/beacon/aggregation/cohort/messages/keygen/cohort-advert.d.ts.map +0 -1
  347. package/dist/types/core/beacon/aggregation/cohort/messages/keygen/cohort-ready.d.ts +0 -26
  348. package/dist/types/core/beacon/aggregation/cohort/messages/keygen/cohort-ready.d.ts.map +0 -1
  349. package/dist/types/core/beacon/aggregation/cohort/messages/keygen/opt-in-accept.d.ts +0 -24
  350. package/dist/types/core/beacon/aggregation/cohort/messages/keygen/opt-in-accept.d.ts.map +0 -1
  351. package/dist/types/core/beacon/aggregation/cohort/messages/keygen/opt-in.d.ts +0 -20
  352. package/dist/types/core/beacon/aggregation/cohort/messages/keygen/opt-in.d.ts.map +0 -1
  353. package/dist/types/core/beacon/aggregation/cohort/messages/keygen/subscribe.d.ts +0 -25
  354. package/dist/types/core/beacon/aggregation/cohort/messages/keygen/subscribe.d.ts.map +0 -1
  355. package/dist/types/core/beacon/aggregation/cohort/messages/sign/aggregated-nonce.d.ts +0 -25
  356. package/dist/types/core/beacon/aggregation/cohort/messages/sign/aggregated-nonce.d.ts.map +0 -1
  357. package/dist/types/core/beacon/aggregation/cohort/messages/sign/authorization-request.d.ts +0 -26
  358. package/dist/types/core/beacon/aggregation/cohort/messages/sign/authorization-request.d.ts.map +0 -1
  359. package/dist/types/core/beacon/aggregation/cohort/messages/sign/nonce-contribution.d.ts +0 -26
  360. package/dist/types/core/beacon/aggregation/cohort/messages/sign/nonce-contribution.d.ts.map +0 -1
  361. package/dist/types/core/beacon/aggregation/cohort/messages/sign/request-signature.d.ts +0 -26
  362. package/dist/types/core/beacon/aggregation/cohort/messages/sign/request-signature.d.ts.map +0 -1
  363. package/dist/types/core/beacon/aggregation/cohort/messages/sign/signature-authorization.d.ts +0 -27
  364. package/dist/types/core/beacon/aggregation/cohort/messages/sign/signature-authorization.d.ts.map +0 -1
  365. package/dist/types/core/beacon/aggregation/cohort/status.d.ts +0 -8
  366. package/dist/types/core/beacon/aggregation/cohort/status.d.ts.map +0 -1
  367. package/dist/types/core/beacon/aggregation/communication/adapter/did-comm.d.ts +0 -89
  368. package/dist/types/core/beacon/aggregation/communication/adapter/did-comm.d.ts.map +0 -1
  369. package/dist/types/core/beacon/aggregation/communication/adapter/nostr.d.ts +0 -103
  370. package/dist/types/core/beacon/aggregation/communication/adapter/nostr.d.ts.map +0 -1
  371. package/dist/types/core/beacon/aggregation/communication/error.d.ts.map +0 -1
  372. package/dist/types/core/beacon/aggregation/communication/factory.d.ts +0 -10
  373. package/dist/types/core/beacon/aggregation/communication/factory.d.ts.map +0 -1
  374. package/dist/types/core/beacon/aggregation/communication/service.d.ts +0 -36
  375. package/dist/types/core/beacon/aggregation/communication/service.d.ts.map +0 -1
  376. package/dist/types/core/beacon/aggregation/coordinator.d.ts +0 -116
  377. package/dist/types/core/beacon/aggregation/coordinator.d.ts.map +0 -1
  378. package/dist/types/core/beacon/aggregation/participant.d.ts +0 -192
  379. package/dist/types/core/beacon/aggregation/participant.d.ts.map +0 -1
  380. package/dist/types/core/beacon/aggregation/session/index.d.ts +0 -156
  381. package/dist/types/core/beacon/aggregation/session/index.d.ts.map +0 -1
  382. package/dist/types/core/beacon/aggregation/session/status.d.ts +0 -11
  383. package/dist/types/core/beacon/aggregation/session/status.d.ts.map +0 -1
  384. package/src/core/beacon/aggregation/cohort/index.ts +0 -304
  385. package/src/core/beacon/aggregation/cohort/messages/constants.ts +0 -12
  386. package/src/core/beacon/aggregation/cohort/messages/index.ts +0 -143
  387. package/src/core/beacon/aggregation/cohort/messages/keygen/cohort-advert.ts +0 -43
  388. package/src/core/beacon/aggregation/cohort/messages/keygen/cohort-ready.ts +0 -39
  389. package/src/core/beacon/aggregation/cohort/messages/keygen/opt-in-accept.ts +0 -34
  390. package/src/core/beacon/aggregation/cohort/messages/keygen/opt-in.ts +0 -33
  391. package/src/core/beacon/aggregation/cohort/messages/keygen/subscribe.ts +0 -35
  392. package/src/core/beacon/aggregation/cohort/messages/sign/aggregated-nonce.ts +0 -38
  393. package/src/core/beacon/aggregation/cohort/messages/sign/authorization-request.ts +0 -39
  394. package/src/core/beacon/aggregation/cohort/messages/sign/nonce-contribution.ts +0 -39
  395. package/src/core/beacon/aggregation/cohort/messages/sign/request-signature.ts +0 -39
  396. package/src/core/beacon/aggregation/cohort/messages/sign/signature-authorization.ts +0 -40
  397. package/src/core/beacon/aggregation/cohort/status.ts +0 -7
  398. package/src/core/beacon/aggregation/communication/adapter/did-comm.ts +0 -147
  399. package/src/core/beacon/aggregation/communication/adapter/nostr.ts +0 -321
  400. package/src/core/beacon/aggregation/communication/error.ts +0 -13
  401. package/src/core/beacon/aggregation/communication/factory.ts +0 -25
  402. package/src/core/beacon/aggregation/communication/service.ts +0 -42
  403. package/src/core/beacon/aggregation/coordinator.ts +0 -415
  404. package/src/core/beacon/aggregation/participant.ts +0 -512
  405. package/src/core/beacon/aggregation/session/index.ts +0 -300
  406. package/src/core/beacon/aggregation/session/status.ts +0 -18
@@ -0,0 +1,282 @@
1
+ import { AGGREGATED_NONCE, AUTHORIZATION_REQUEST, COHORT_ADVERT, COHORT_OPT_IN_ACCEPT, COHORT_READY, DISTRIBUTE_AGGREGATED_DATA, } from '../messages/constants.js';
2
+ import { AggregationParticipant } from '../participant.js';
3
+ import { ParticipantCohortPhase } from '../phases.js';
4
+ import { TypedEventEmitter } from './typed-emitter.js';
5
+ /**
6
+ * High-level facade for an Aggregation Participant.
7
+ *
8
+ * Long-running listener: waits for cohort adverts, applies the `shouldJoin`
9
+ * filter, and drives each accepted cohort through the full protocol to
10
+ * completion in parallel.
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * const transport = new NostrTransport({ relays: [RELAY] });
15
+ * transport.registerActor(myDid, myKeys);
16
+ *
17
+ * const runner = new AggregationParticipantRunner({
18
+ * transport,
19
+ * did: myDid,
20
+ * keys: myKeys,
21
+ * shouldJoin: async (advert) => advert.beaconType === 'CASBeacon',
22
+ * onProvideUpdate: async ({ beaconAddress }) => {
23
+ * return Update.sign(myDid, unsigned, vm, secretKey);
24
+ * },
25
+ * });
26
+ *
27
+ * runner.on('cohort-complete', ({ beaconAddress }) => {
28
+ * console.log(`Add to DID document: bitcoin:${beaconAddress}`);
29
+ * });
30
+ *
31
+ * await runner.start();
32
+ * ```
33
+ *
34
+ * For full manual control, drop down to the underlying state machine via
35
+ * `runner.session`. The state machine has no transport coupling and exposes
36
+ * every protocol decision as an explicit method.
37
+ *
38
+ * @class AggregationParticipantRunner
39
+ * @extends TypedEventEmitter<AggregationParticipantEvents>
40
+ */
41
+ export class AggregationParticipantRunner extends TypedEventEmitter {
42
+ /** Direct access to the underlying state machine for advanced use. */
43
+ session;
44
+ #transport;
45
+ #did;
46
+ #shouldJoin;
47
+ #onProvideUpdate;
48
+ #onValidateData;
49
+ #onApproveSigning;
50
+ #handlersRegistered = false;
51
+ #stopped = false;
52
+ constructor(options) {
53
+ super();
54
+ this.#transport = options.transport;
55
+ this.#did = options.did;
56
+ this.#shouldJoin = options.shouldJoin ?? (async () => false);
57
+ this.#onProvideUpdate = options.onProvideUpdate;
58
+ this.#onValidateData = options.onValidateData ?? (async (info) => ({ approved: info.matches }));
59
+ this.#onApproveSigning = options.onApproveSigning ?? (async () => ({ approved: true }));
60
+ this.session = new AggregationParticipant({ did: options.did, keys: options.keys });
61
+ }
62
+ /**
63
+ * Start listening for cohorts. The runner stays active until {@link stop}
64
+ * is called or the underlying transport disconnects.
65
+ */
66
+ async start() {
67
+ this.#registerHandlers();
68
+ }
69
+ /** Stop the runner. Does not unregister transport handlers. */
70
+ stop() {
71
+ this.#stopped = true;
72
+ }
73
+ /**
74
+ * Single-shot helper: start, join the first cohort that passes `shouldJoin`,
75
+ * drive it to completion, and resolve. Convenient for tests and demos.
76
+ */
77
+ static async joinFirst(options) {
78
+ return new Promise((resolve, reject) => {
79
+ const runner = new AggregationParticipantRunner(options);
80
+ runner.once('cohort-complete', (info) => {
81
+ runner.stop();
82
+ resolve(info);
83
+ });
84
+ runner.on('error', reject);
85
+ runner.start().catch(reject);
86
+ });
87
+ }
88
+ /**
89
+ * Internal: handler registration with the transport. Idempotent and safe to call multiple times,
90
+ * but only registers handlers once.
91
+ */
92
+ #registerHandlers() {
93
+ if (this.#handlersRegistered)
94
+ return;
95
+ this.#handlersRegistered = true;
96
+ this.#transport.registerMessageHandler(this.#did, COHORT_ADVERT, this.#handleAdvert.bind(this));
97
+ this.#transport.registerMessageHandler(this.#did, COHORT_OPT_IN_ACCEPT, this.#handleOptInAccept.bind(this));
98
+ this.#transport.registerMessageHandler(this.#did, COHORT_READY, this.#handleCohortReady.bind(this));
99
+ this.#transport.registerMessageHandler(this.#did, DISTRIBUTE_AGGREGATED_DATA, this.#handleDistributeData.bind(this));
100
+ this.#transport.registerMessageHandler(this.#did, AUTHORIZATION_REQUEST, this.#handleAuthorizationRequest.bind(this));
101
+ this.#transport.registerMessageHandler(this.#did, AGGREGATED_NONCE, this.#handleAggregatedNonce.bind(this));
102
+ }
103
+ /**
104
+ * Internal: handler for cohort adverts. Applies the `shouldJoin` filter and joins if approved.
105
+ * @param {BaseMessage} msg - The received cohort advert message.
106
+ * @returns {Promise<void>} Resolves when processing is complete.
107
+ */
108
+ async #handleAdvert(msg) {
109
+ if (this.#stopped)
110
+ return;
111
+ try {
112
+ this.session.receive(msg);
113
+ const advert = this.session.discoveredCohorts.get(msg.body?.cohortId ?? '');
114
+ if (!advert)
115
+ return;
116
+ this.emit('cohort-discovered', advert);
117
+ // Register the service's communication key for encrypted message routing
118
+ if (advert.serviceCommunicationPk) {
119
+ this.#transport.registerPeer(advert.serviceDid, advert.serviceCommunicationPk);
120
+ }
121
+ const join = await this.#shouldJoin(advert);
122
+ if (!join)
123
+ return;
124
+ await this.#sendAll(this.session.joinCohort(advert.cohortId));
125
+ this.emit('cohort-joined', { cohortId: advert.cohortId });
126
+ }
127
+ catch (err) {
128
+ this.emit('error', err);
129
+ }
130
+ }
131
+ /**
132
+ * Internal: handler for opt-in accept messages. Updates the session state accordingly.
133
+ * @param {BaseMessage} msg - The received opt-in accept message.
134
+ */
135
+ #handleOptInAccept(msg) {
136
+ if (this.#stopped)
137
+ return;
138
+ try {
139
+ this.session.receive(msg);
140
+ }
141
+ catch (err) {
142
+ this.emit('error', err);
143
+ }
144
+ }
145
+ /**
146
+ * Internal: handler for cohort ready messages. Updates the session state, emits a 'cohort-ready'
147
+ * event, and triggers the update submission flow via the `onProvideUpdate` callback.
148
+ * @param {BaseMessage} msg - The received cohort ready message.
149
+ * @returns {Promise<void>} Resolves when processing is complete.
150
+ */
151
+ async #handleCohortReady(msg) {
152
+ if (this.#stopped)
153
+ return;
154
+ try {
155
+ this.session.receive(msg);
156
+ const cohortId = msg.body?.cohortId;
157
+ if (!cohortId)
158
+ return;
159
+ const info = this.session.joinedCohorts.get(cohortId);
160
+ if (!info)
161
+ return;
162
+ this.emit('cohort-ready', { cohortId, beaconAddress: info.beaconAddress });
163
+ // Construct the signed update via caller callback and submit
164
+ const signedUpdate = await this.#onProvideUpdate({
165
+ cohortId,
166
+ beaconAddress: info.beaconAddress,
167
+ });
168
+ await this.#sendAll(this.session.submitUpdate(cohortId, signedUpdate));
169
+ this.emit('update-submitted', { cohortId });
170
+ }
171
+ catch (err) {
172
+ this.emit('error', err);
173
+ }
174
+ }
175
+ /**
176
+ * Internal: handler for distribute aggregated data messages. Updates the session state, emits a
177
+ * 'validation-requested' event, and triggers the validation decision flow via the `onValidateData`
178
+ * callback. Depending on the decision, sends an approval or rejection message.
179
+ * @param {BaseMessage} msg - The received distribute aggregated data message.
180
+ * @returns {Promise<void>} Resolves when processing is complete.
181
+ * @throws {Error} If an error occurs during message processing or callback execution.
182
+ */
183
+ async #handleDistributeData(msg) {
184
+ if (this.#stopped)
185
+ return;
186
+ try {
187
+ this.session.receive(msg);
188
+ const cohortId = msg.body?.cohortId;
189
+ if (!cohortId)
190
+ return;
191
+ const validation = this.session.pendingValidations.get(cohortId);
192
+ if (!validation)
193
+ return;
194
+ this.emit('validation-requested', validation);
195
+ const decision = await this.#onValidateData(validation);
196
+ if (decision.approved) {
197
+ await this.#sendAll(this.session.approveValidation(cohortId));
198
+ }
199
+ else {
200
+ await this.#sendAll(this.session.rejectValidation(cohortId));
201
+ this.emit('cohort-failed', { cohortId, reason: 'Validation rejected by participant' });
202
+ }
203
+ }
204
+ catch (err) {
205
+ this.emit('error', err);
206
+ }
207
+ }
208
+ /**
209
+ * Internal: handler for authorization request messages. Updates the session state, emits a
210
+ * 'signing-requested' event, and triggers the signing approval flow via the `onApproveSigning`
211
+ * callback. Depending on the decision, sends a nonce approval message or emits a 'cohort-failed'
212
+ * event.
213
+ * @param {BaseMessage} msg - The received authorization request message.
214
+ * @returns {Promise<void>} Resolves when processing is complete.
215
+ * @throws {Error} If an error occurs during message processing or callback execution.
216
+ */
217
+ async #handleAuthorizationRequest(msg) {
218
+ if (this.#stopped)
219
+ return;
220
+ try {
221
+ this.session.receive(msg);
222
+ const cohortId = msg.body?.cohortId;
223
+ if (!cohortId)
224
+ return;
225
+ const req = this.session.pendingSigningRequests.get(cohortId);
226
+ if (!req)
227
+ return;
228
+ this.emit('signing-requested', req);
229
+ const decision = await this.#onApproveSigning(req);
230
+ if (!decision.approved) {
231
+ this.emit('cohort-failed', { cohortId, reason: 'Signing rejected by participant' });
232
+ return;
233
+ }
234
+ await this.#sendAll(this.session.approveNonce(cohortId));
235
+ }
236
+ catch (err) {
237
+ this.emit('error', err);
238
+ }
239
+ }
240
+ /**
241
+ * Internal: handler for aggregated nonce messages. Updates the session state, triggers partial
242
+ * signature generation, and sends the partial signature to the aggregator. If the cohort reaches
243
+ * completion after processing the nonce, emits a 'cohort-complete' event.
244
+ * @param {BaseMessage} msg - The received aggregated nonce message.
245
+ * @returns {Promise<void>} Resolves when processing is complete.
246
+ * @throws {Error} If an error occurs during message processing or partial signature generation.
247
+ */
248
+ async #handleAggregatedNonce(msg) {
249
+ if (this.#stopped)
250
+ return;
251
+ try {
252
+ this.session.receive(msg);
253
+ const cohortId = msg.body?.cohortId;
254
+ if (!cohortId)
255
+ return;
256
+ await this.#sendAll(this.session.generatePartialSignature(cohortId));
257
+ // Check if we've reached completion
258
+ if (this.session.getCohortPhase(cohortId) === ParticipantCohortPhase.Complete) {
259
+ const info = this.session.joinedCohorts.get(cohortId);
260
+ if (info) {
261
+ this.emit('cohort-complete', { cohortId, beaconAddress: info.beaconAddress });
262
+ }
263
+ }
264
+ }
265
+ catch (err) {
266
+ this.emit('error', err);
267
+ }
268
+ }
269
+ /**
270
+ * Internal: send helper to ensure messages are sent sequentially. This is important for protocol
271
+ * correctness, as some transports may not guarantee message order if sent in parallel.
272
+ * @param {BaseMessage[]} msgs - The messages to send.
273
+ * @returns {Promise<void>} Resolves when all messages have been sent.
274
+ * @throws {Error} If an error occurs during message sending.
275
+ */
276
+ async #sendAll(msgs) {
277
+ for (const m of msgs) {
278
+ await this.#transport.sendMessage(m, this.#did, m.to);
279
+ }
280
+ }
281
+ }
282
+ //# sourceMappingURL=participant-runner.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"participant-runner.js","sourceRoot":"","sources":["../../../../../src/core/aggregation/runner/participant-runner.ts"],"names":[],"mappings":"AAGA,OAAO,EACL,gBAAgB,EAChB,qBAAqB,EACrB,aAAa,EACb,oBAAoB,EACpB,YAAY,EACZ,0BAA0B,GAC3B,MAAM,0BAA0B,CAAC;AAKlC,OAAO,EACL,sBAAsB,EACvB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,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AC;YACpC,IAAI,CAAC,QAAQ;gBAAE,OAAO;YAEtB,MAAM,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,wBAAwB,CAAC,QAAQ,CAAC,CAAC,CAAC;YAErE,oCAAoC;YACpC,IAAI,IAAI,CAAC,OAAO,CAAC,cAAc,CAAC,QAAQ,CAAC,KAAK,sBAAsB,CAAC,QAAQ,EAAE,CAAC;gBAC9E,MAAM,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,aAAa,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;gBACtD,IAAI,IAAI,EAAE,CAAC;oBACT,IAAI,CAAC,IAAI,CAAC,iBAAiB,EAAE,EAAE,QAAQ,EAAE,aAAa,EAAE,IAAI,CAAC,aAAa,EAAE,CAAC,CAAC;gBAChF,CAAC;YACH,CAAC;QACH,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,GAAY,CAAC,CAAC;QACnC,CAAC;IACH,CAAC;IAED;;;;;;OAMG;IACH,KAAK,CAAC,QAAQ,CAAC,IAAmB;QAChC,KAAK,MAAM,CAAC,IAAI,IAAI,EAAE,CAAC;YACrB,MAAM,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC,CAAC,EAAE,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC;QACxD,CAAC;IACH,CAAC;CACF"}
@@ -0,0 +1,290 @@
1
+ import { COHORT_OPT_IN, NONCE_CONTRIBUTION, SIGNATURE_AUTHORIZATION, SUBMIT_UPDATE, VALIDATION_ACK, } from '../messages/constants.js';
2
+ import { ServiceCohortPhase } from '../phases.js';
3
+ import { AggregationService } from '../service.js';
4
+ import { TypedEventEmitter } from './typed-emitter.js';
5
+ /**
6
+ * High-level facade for running an Aggregation Service over a Transport.
7
+ *
8
+ * Wires the {@link AggregationService} state machine to a {@link Transport},
9
+ * encapsulating message handler registration, outgoing message dispatch,
10
+ * and decision callback orchestration.
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * const transport = new NostrTransport({ relays: [RELAY] });
15
+ * transport.registerActor(serviceDid, serviceKeys);
16
+ *
17
+ * const runner = new AggregationServiceRunner({
18
+ * transport,
19
+ * did: serviceDid,
20
+ * keys: serviceKeys,
21
+ * config: { minParticipants: 2, network: 'mutinynet', beaconType: 'CASBeacon' },
22
+ * onProvideTxData: async ({ beaconAddress, signalBytes }) => {
23
+ * return await buildBeaconTransaction(beaconAddress, signalBytes, bitcoin);
24
+ * },
25
+ * });
26
+ *
27
+ * runner.on('keygen-complete', ({ beaconAddress }) => console.log(beaconAddress));
28
+ * runner.on('signing-complete', ({ signature }) => console.log('done'));
29
+ *
30
+ * const result = await runner.run();
31
+ * ```
32
+ *
33
+ * For full manual control, drop down to the underlying state machine via
34
+ * `runner.session`. The state machine has no transport coupling and exposes
35
+ * every protocol decision as an explicit method.
36
+ *
37
+ * @class AggregationServiceRunner
38
+ */
39
+ export class AggregationServiceRunner extends TypedEventEmitter {
40
+ /** Direct access to the underlying state machine for advanced use. */
41
+ session;
42
+ #transport;
43
+ #did;
44
+ #config;
45
+ #onOptInReceived;
46
+ #onReadyToFinalize;
47
+ #onProvideTxData;
48
+ #cohortId;
49
+ #handlersRegistered = false;
50
+ #stopped = false;
51
+ #resolveRun;
52
+ #rejectRun;
53
+ constructor(options) {
54
+ super();
55
+ this.#transport = options.transport;
56
+ this.#did = options.did;
57
+ this.#config = options.config;
58
+ this.#onOptInReceived = options.onOptInReceived ?? (async () => ({ accepted: true }));
59
+ this.#onReadyToFinalize = options.onReadyToFinalize ?? (async ({ acceptedCount, minRequired }) => ({
60
+ finalize: acceptedCount >= minRequired,
61
+ }));
62
+ this.#onProvideTxData = options.onProvideTxData;
63
+ this.session = new AggregationService({ did: options.did, keys: options.keys });
64
+ }
65
+ /**
66
+ * Run the protocol to completion. Resolves with the final aggregation result
67
+ * (signature + signed transaction) once signing is complete.
68
+ *
69
+ * @returns {Promise<AggregationResult>} The final result with signature and signed tx.
70
+ */
71
+ run() {
72
+ return new Promise((resolve, reject) => {
73
+ this.#resolveRun = resolve;
74
+ this.#rejectRun = reject;
75
+ try {
76
+ this.#registerHandlers();
77
+ this.#cohortId = this.session.createCohort(this.#config);
78
+ // Emit cohort-advertised BEFORE the send so the event fires before any downstream cascade
79
+ const advertMsgs = this.session.advertise(this.#cohortId);
80
+ this.emit('cohort-advertised', { cohortId: this.#cohortId });
81
+ this.#sendAll(advertMsgs).catch(err => this.#fail(err));
82
+ }
83
+ catch (err) {
84
+ this.#fail(err);
85
+ }
86
+ });
87
+ }
88
+ /**
89
+ * Stop the runner early. Cleans up internal state.
90
+ * Note: does not unregister transport handlers (the transport interface
91
+ * does not currently expose unregister).
92
+ */
93
+ stop() {
94
+ this.#stopped = true;
95
+ }
96
+ /**
97
+ * Internal: handler registration with the transport. Idempotent.
98
+ */
99
+ #registerHandlers() {
100
+ if (this.#handlersRegistered)
101
+ return;
102
+ this.#handlersRegistered = true;
103
+ this.#transport.registerMessageHandler(this.#did, COHORT_OPT_IN, this.#handleOptIn.bind(this));
104
+ this.#transport.registerMessageHandler(this.#did, SUBMIT_UPDATE, this.#handleSubmitUpdate.bind(this));
105
+ this.#transport.registerMessageHandler(this.#did, VALIDATION_ACK, this.#handleValidationAck.bind(this));
106
+ this.#transport.registerMessageHandler(this.#did, NONCE_CONTRIBUTION, this.#handleNonceContribution.bind(this));
107
+ this.#transport.registerMessageHandler(this.#did, SIGNATURE_AUTHORIZATION, this.#handleSignatureAuthorization.bind(this));
108
+ }
109
+ /**
110
+ * Internal: message handlers for each protocol step. Each handler:
111
+ * 1) feeds the message into the state machine via session.receive()
112
+ * 2) emits a high-level event for external observers
113
+ * 3) checks if the new state triggers any automatic next steps, and if so:
114
+ * a) calls the appropriate decision callback(s)
115
+ * b) sends any resulting messages from the state machine
116
+ * @param {BaseMessage} msg - The incoming message to handle.
117
+ * @returns {Promise<void>} Resolves when handling is complete.
118
+ * @throws {Error} If any step of handling fails, the error is emitted and the run promise is rejected.
119
+ * Note: if the runner has been stopped, handlers will ignore incoming messages.
120
+ */
121
+ async #handleOptIn(msg) {
122
+ if (this.#stopped)
123
+ return;
124
+ try {
125
+ this.session.receive(msg);
126
+ const optIn = this.session.pendingOptIns(this.#cohortId).get(msg.from);
127
+ if (!optIn)
128
+ return;
129
+ this.emit('opt-in-received', optIn);
130
+ // Register peer key for encrypted messaging
131
+ if (optIn.communicationPk) {
132
+ this.#transport.registerPeer(msg.from, optIn.communicationPk);
133
+ }
134
+ const decision = await this.#onOptInReceived(optIn);
135
+ if (!decision.accepted)
136
+ return;
137
+ await this.#sendAll(this.session.acceptParticipant(this.#cohortId, msg.from));
138
+ this.emit('participant-accepted', { participantDid: msg.from });
139
+ // Check if it's time to finalize
140
+ const cohort = this.session.getCohort(this.#cohortId);
141
+ if (cohort.participants.length >= this.#config.minParticipants) {
142
+ const finalizeDecision = await this.#onReadyToFinalize({
143
+ acceptedCount: cohort.participants.length,
144
+ minRequired: this.#config.minParticipants,
145
+ });
146
+ if (finalizeDecision.finalize) {
147
+ // finalizeKeygen() computes the beacon address synchronously
148
+ // emit BEFORE awaiting sendAll. Otherwise the downstream cascade
149
+ // (which can run all the way to signing-complete) would resolve the
150
+ // run() promise before this event fires.
151
+ const readyMsgs = this.session.finalizeKeygen(this.#cohortId);
152
+ this.emit('keygen-complete', {
153
+ cohortId: this.#cohortId,
154
+ beaconAddress: cohort.beaconAddress,
155
+ });
156
+ await this.#sendAll(readyMsgs);
157
+ }
158
+ }
159
+ }
160
+ catch (err) {
161
+ this.#fail(err);
162
+ }
163
+ }
164
+ /**
165
+ * Handler for receiving participant updates. When all updates are received, automatically builds
166
+ * and distributes the data for validation.
167
+ * @param {BaseMessage} msg - The incoming message to handle.
168
+ * @returns {Promise<void>} Resolves when handling is complete.
169
+ * @throws {Error} If any step of handling fails, the error is emitted and the run promise is rejected.
170
+ * Note: if the runner has been stopped, handlers will ignore incoming messages.
171
+ */
172
+ async #handleSubmitUpdate(msg) {
173
+ if (this.#stopped)
174
+ return;
175
+ try {
176
+ this.session.receive(msg);
177
+ this.emit('update-received', { participantDid: msg.from });
178
+ // When all updates collected, build and distribute
179
+ if (this.session.getCohortPhase(this.#cohortId) === ServiceCohortPhase.UpdatesCollected) {
180
+ const distributeMsgs = this.session.buildAndDistribute(this.#cohortId);
181
+ this.emit('data-distributed', { cohortId: this.#cohortId });
182
+ await this.#sendAll(distributeMsgs);
183
+ }
184
+ }
185
+ catch (err) {
186
+ this.#fail(err);
187
+ }
188
+ }
189
+ /**
190
+ * Handler for receiving validation acknowledgments. When all validations are received,
191
+ * automatically requests tx data and starts signing.
192
+ * @param {BaseMessage} msg - The incoming message to handle.
193
+ * @returns {Promise<void>} Resolves when handling is complete.
194
+ * @throws {Error} If any step of handling fails, the error is emitted and the run promise is rejected.
195
+ * Note: if the runner has been stopped, handlers will ignore incoming messages.
196
+ */
197
+ async #handleValidationAck(msg) {
198
+ if (this.#stopped)
199
+ return;
200
+ try {
201
+ this.session.receive(msg);
202
+ const approved = !!msg.body?.approved;
203
+ this.emit('validation-received', { participantDid: msg.from, approved });
204
+ // When all validations received, request tx data and start signing
205
+ if (this.session.getCohortPhase(this.#cohortId) === ServiceCohortPhase.Validated) {
206
+ const cohort = this.session.getCohort(this.#cohortId);
207
+ const txData = await this.#onProvideTxData({
208
+ cohortId: this.#cohortId,
209
+ beaconAddress: cohort.beaconAddress,
210
+ signalBytes: cohort.signalBytes,
211
+ });
212
+ const authMsgs = this.session.startSigning(this.#cohortId, txData);
213
+ const sessionId = this.session.getSigningSessionId(this.#cohortId) ?? '';
214
+ this.emit('signing-started', { sessionId });
215
+ await this.#sendAll(authMsgs);
216
+ }
217
+ }
218
+ catch (err) {
219
+ this.#fail(err);
220
+ }
221
+ }
222
+ /**
223
+ * Handler for receiving nonce contributions and signature authorizations. When all nonces or
224
+ * signatures are received,
225
+ * @param {BaseMessage} msg - The incoming message to handle.
226
+ * @returns {Promise<void>} Resolves when handling is complete.
227
+ * @throws {Error} If any step of handling fails, the error is emitted and the run promise is rejected.
228
+ * Note: if the runner has been stopped, handlers will ignore incoming messages.
229
+ */
230
+ async #handleNonceContribution(msg) {
231
+ if (this.#stopped)
232
+ return;
233
+ try {
234
+ this.session.receive(msg);
235
+ this.emit('nonce-received', { participantDid: msg.from });
236
+ // When all nonces collected, send aggregated nonce
237
+ if (this.session.getCohortPhase(this.#cohortId) === ServiceCohortPhase.NoncesCollected) {
238
+ await this.#sendAll(this.session.sendAggregatedNonce(this.#cohortId));
239
+ }
240
+ }
241
+ catch (err) {
242
+ this.#fail(err);
243
+ }
244
+ }
245
+ /**
246
+ * Handler for receiving signature authorizations. When all partial signatures are received, the
247
+ * session automatically completes and the final result is emitted and the run() promise is resolved.
248
+ * @param {BaseMessage} msg - The incoming message to handle.
249
+ * @returns {Promise<void>} Resolves when handling is complete.
250
+ * @throws {Error} If any step of handling fails, the error is emitted and the run promise is rejected.
251
+ * Note: if the runner has been stopped, handlers will ignore incoming messages.
252
+ */
253
+ async #handleSignatureAuthorization(msg) {
254
+ if (this.#stopped)
255
+ return;
256
+ try {
257
+ this.session.receive(msg);
258
+ // The state machine auto-completes when all partial sigs received
259
+ const result = this.session.getResult(this.#cohortId);
260
+ if (result) {
261
+ this.emit('signing-complete', result);
262
+ this.#resolveRun?.(result);
263
+ }
264
+ }
265
+ catch (err) {
266
+ this.#fail(err);
267
+ }
268
+ }
269
+ /**
270
+ * Internal: helper to send all messages sequentially. Catches and propagates errors.
271
+ * @param {BaseMessage[]} msgs - The messages to send.
272
+ * @returns {Promise<void>} Resolves when all messages have been sent.
273
+ * @throws {Error} If sending any message fails, the error is emitted and the run promise is
274
+ * rejected.
275
+ */
276
+ async #sendAll(msgs) {
277
+ for (const m of msgs) {
278
+ await this.#transport.sendMessage(m, this.#did, m.to);
279
+ }
280
+ }
281
+ /**
282
+ * Internal: helper to handle errors. Emits an 'error' event and rejects the run promise.
283
+ * @param {Error} err - The error to handle.
284
+ */
285
+ #fail(err) {
286
+ this.emit('error', err);
287
+ this.#rejectRun?.(err);
288
+ }
289
+ }
290
+ //# sourceMappingURL=service-runner.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"service-runner.js","sourceRoot":"","sources":["../../../../../src/core/aggregation/runner/service-runner.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,aAAa,EACb,kBAAkB,EAClB,uBAAuB,EACvB,aAAa,EACb,cAAc,GACf,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAMlD,OAAO,EACL,kBAAkB,EACnB,MAAM,eAAe,CAAC;AAGvB,OAAO,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAiDvD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,MAAM,OAAO,wBAAyB,SAAQ,iBAA2C;IACvF,sEAAsE;IAC7D,OAAO,CAAqB;IAE5B,UAAU,CAAY;IACtB,IAAI,CAAS;IACb,OAAO,CAAe;IACtB,gBAAgB,CAAkB;IAClC,kBAAkB,CAAoB;IACtC,gBAAgB,CAAkB;IAE3C,SAAS,CAAU;IACnB,mBAAmB,GAAG,KAAK,CAAC;IAC5B,QAAQ,GAAG,KAAK,CAAC;IACjB,WAAW,CAAuC;IAClD,UAAU,CAAwB;IAElC,YAAY,OAAwC;QAClD,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,UAAU,GAAG,OAAO,CAAC,SAAS,CAAC;QACpC,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,GAAG,CAAC;QACxB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC,MAAM,CAAC;QAC9B,IAAI,CAAC,gBAAgB,GAAG,OAAO,CAAC,eAAe,IAAI,CAAC,KAAK,IAAI,EAAE,CAAC,CAAC,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC;QACtF,IAAI,CAAC,kBAAkB,GAAG,OAAO,CAAC,iBAAiB,IAAI,CAA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