@did-btcr2/method 0.26.0 → 0.28.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 (297) hide show
  1. package/README.md +118 -236
  2. package/dist/.tsbuildinfo +1 -1
  3. package/dist/browser.js +27665 -23764
  4. package/dist/browser.mjs +27665 -23764
  5. package/dist/cjs/index.js +2661 -2239
  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 +286 -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 +15 -56
  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/singleton-beacon.js +10 -53
  59. package/dist/esm/core/beacon/singleton-beacon.js.map +1 -1
  60. package/dist/esm/core/beacon/smt-beacon.js +85 -9
  61. package/dist/esm/core/beacon/smt-beacon.js.map +1 -1
  62. package/dist/esm/core/identifier.js +13 -0
  63. package/dist/esm/core/identifier.js.map +1 -1
  64. package/dist/esm/core/resolver.js +9 -0
  65. package/dist/esm/core/resolver.js.map +1 -1
  66. package/dist/esm/core/updater.js +269 -0
  67. package/dist/esm/core/updater.js.map +1 -0
  68. package/dist/esm/did-btcr2.js +30 -42
  69. package/dist/esm/did-btcr2.js.map +1 -1
  70. package/dist/esm/index.js +16 -25
  71. package/dist/esm/index.js.map +1 -1
  72. package/dist/types/core/aggregation/cohort.d.ts +94 -0
  73. package/dist/types/core/aggregation/cohort.d.ts.map +1 -0
  74. package/dist/types/core/aggregation/errors.d.ts +14 -0
  75. package/dist/types/core/aggregation/errors.d.ts.map +1 -0
  76. package/dist/types/core/{beacon/aggregation/cohort → aggregation}/messages/base.d.ts +7 -1
  77. package/dist/types/core/aggregation/messages/base.d.ts.map +1 -0
  78. package/dist/types/core/aggregation/messages/constants.d.ts +23 -0
  79. package/dist/types/core/aggregation/messages/constants.d.ts.map +1 -0
  80. package/dist/types/core/aggregation/messages/factories.d.ts +177 -0
  81. package/dist/types/core/aggregation/messages/factories.d.ts.map +1 -0
  82. package/dist/types/core/aggregation/messages/guards.d.ts +11 -0
  83. package/dist/types/core/aggregation/messages/guards.d.ts.map +1 -0
  84. package/dist/types/core/aggregation/messages/index.d.ts +5 -0
  85. package/dist/types/core/aggregation/messages/index.d.ts.map +1 -0
  86. package/dist/types/core/aggregation/participant.d.ts +101 -0
  87. package/dist/types/core/aggregation/participant.d.ts.map +1 -0
  88. package/dist/types/core/aggregation/phases.d.ts +49 -0
  89. package/dist/types/core/aggregation/phases.d.ts.map +1 -0
  90. package/dist/types/core/aggregation/runner/events.d.ts +89 -0
  91. package/dist/types/core/aggregation/runner/events.d.ts.map +1 -0
  92. package/dist/types/core/aggregation/runner/index.d.ts +5 -0
  93. package/dist/types/core/aggregation/runner/index.d.ts.map +1 -0
  94. package/dist/types/core/aggregation/runner/participant-runner.d.ts +111 -0
  95. package/dist/types/core/aggregation/runner/participant-runner.d.ts.map +1 -0
  96. package/dist/types/core/aggregation/runner/service-runner.d.ts +102 -0
  97. package/dist/types/core/aggregation/runner/service-runner.d.ts.map +1 -0
  98. package/dist/types/core/aggregation/runner/typed-emitter.d.ts +41 -0
  99. package/dist/types/core/aggregation/runner/typed-emitter.d.ts.map +1 -0
  100. package/dist/types/core/aggregation/service.d.ts +112 -0
  101. package/dist/types/core/aggregation/service.d.ts.map +1 -0
  102. package/dist/types/core/aggregation/signing-session.d.ts +69 -0
  103. package/dist/types/core/aggregation/signing-session.d.ts.map +1 -0
  104. package/dist/types/core/aggregation/transport/didcomm.d.ts +20 -0
  105. package/dist/types/core/aggregation/transport/didcomm.d.ts.map +1 -0
  106. package/dist/types/core/{beacon/aggregation/communication → aggregation/transport}/error.d.ts +2 -2
  107. package/dist/types/core/aggregation/transport/error.d.ts.map +1 -0
  108. package/dist/types/core/aggregation/transport/factory.d.ts +13 -0
  109. package/dist/types/core/aggregation/transport/factory.d.ts.map +1 -0
  110. package/dist/types/core/aggregation/transport/index.d.ts +6 -0
  111. package/dist/types/core/aggregation/transport/index.d.ts.map +1 -0
  112. package/dist/types/core/aggregation/transport/nostr.d.ts +55 -0
  113. package/dist/types/core/aggregation/transport/nostr.d.ts.map +1 -0
  114. package/dist/types/core/aggregation/transport/transport.d.ts +37 -0
  115. package/dist/types/core/aggregation/transport/transport.d.ts.map +1 -0
  116. package/dist/types/core/beacon/beacon.d.ts +37 -2
  117. package/dist/types/core/beacon/beacon.d.ts.map +1 -1
  118. package/dist/types/core/beacon/cas-beacon.d.ts +19 -7
  119. package/dist/types/core/beacon/cas-beacon.d.ts.map +1 -1
  120. package/dist/types/core/beacon/error.d.ts +0 -6
  121. package/dist/types/core/beacon/error.d.ts.map +1 -1
  122. package/dist/types/core/beacon/fee-estimator.d.ts +40 -0
  123. package/dist/types/core/beacon/fee-estimator.d.ts.map +1 -0
  124. package/dist/types/core/beacon/interfaces.d.ts +8 -0
  125. package/dist/types/core/beacon/interfaces.d.ts.map +1 -1
  126. package/dist/types/core/beacon/singleton-beacon.d.ts +9 -2
  127. package/dist/types/core/beacon/singleton-beacon.d.ts.map +1 -1
  128. package/dist/types/core/beacon/smt-beacon.d.ts +27 -7
  129. package/dist/types/core/beacon/smt-beacon.d.ts.map +1 -1
  130. package/dist/types/core/identifier.d.ts +8 -0
  131. package/dist/types/core/identifier.d.ts.map +1 -1
  132. package/dist/types/core/interfaces.d.ts +2 -2
  133. package/dist/types/core/resolver.d.ts +11 -1
  134. package/dist/types/core/resolver.d.ts.map +1 -1
  135. package/dist/types/core/updater.d.ts +178 -0
  136. package/dist/types/core/updater.d.ts.map +1 -0
  137. package/dist/types/did-btcr2.d.ts +23 -23
  138. package/dist/types/did-btcr2.d.ts.map +1 -1
  139. package/dist/types/index.d.ts +11 -25
  140. package/dist/types/index.d.ts.map +1 -1
  141. package/package.json +31 -30
  142. package/src/core/aggregation/cohort.ts +247 -0
  143. package/src/core/aggregation/errors.ts +25 -0
  144. package/src/core/{beacon/aggregation/cohort → aggregation}/messages/base.ts +8 -3
  145. package/src/core/aggregation/messages/constants.ts +28 -0
  146. package/src/core/aggregation/messages/factories.ts +240 -0
  147. package/src/core/aggregation/messages/guards.ts +55 -0
  148. package/src/core/aggregation/messages/index.ts +4 -0
  149. package/src/core/aggregation/participant.ts +510 -0
  150. package/src/core/aggregation/phases.ts +82 -0
  151. package/src/core/aggregation/runner/events.ts +77 -0
  152. package/src/core/aggregation/runner/index.ts +4 -0
  153. package/src/core/aggregation/runner/participant-runner.ts +364 -0
  154. package/src/core/aggregation/runner/service-runner.ts +365 -0
  155. package/src/core/aggregation/runner/typed-emitter.ts +87 -0
  156. package/src/core/aggregation/service.ts +547 -0
  157. package/src/core/aggregation/signing-session.ts +209 -0
  158. package/src/core/aggregation/transport/didcomm.ts +42 -0
  159. package/src/core/aggregation/transport/error.ts +13 -0
  160. package/src/core/aggregation/transport/factory.ts +29 -0
  161. package/src/core/aggregation/transport/index.ts +5 -0
  162. package/src/core/aggregation/transport/nostr.ts +333 -0
  163. package/src/core/aggregation/transport/transport.ts +46 -0
  164. package/src/core/beacon/beacon.ts +122 -2
  165. package/src/core/beacon/cas-beacon.ts +28 -76
  166. package/src/core/beacon/error.ts +0 -12
  167. package/src/core/beacon/fee-estimator.ts +52 -0
  168. package/src/core/beacon/interfaces.ts +10 -1
  169. package/src/core/beacon/singleton-beacon.ts +14 -75
  170. package/src/core/beacon/smt-beacon.ts +109 -11
  171. package/src/core/identifier.ts +17 -0
  172. package/src/core/interfaces.ts +2 -2
  173. package/src/core/resolver.ts +25 -2
  174. package/src/core/updater.ts +415 -0
  175. package/src/did-btcr2.ts +36 -66
  176. package/src/index.ts +17 -30
  177. package/dist/esm/core/beacon/aggregation/cohort/index.js +0 -237
  178. package/dist/esm/core/beacon/aggregation/cohort/index.js.map +0 -1
  179. package/dist/esm/core/beacon/aggregation/cohort/messages/base.js.map +0 -1
  180. package/dist/esm/core/beacon/aggregation/cohort/messages/constants.js +0 -11
  181. package/dist/esm/core/beacon/aggregation/cohort/messages/constants.js.map +0 -1
  182. package/dist/esm/core/beacon/aggregation/cohort/messages/index.js +0 -98
  183. package/dist/esm/core/beacon/aggregation/cohort/messages/index.js.map +0 -1
  184. package/dist/esm/core/beacon/aggregation/cohort/messages/keygen/cohort-advert.js +0 -31
  185. package/dist/esm/core/beacon/aggregation/cohort/messages/keygen/cohort-advert.js.map +0 -1
  186. package/dist/esm/core/beacon/aggregation/cohort/messages/keygen/cohort-ready.js +0 -29
  187. package/dist/esm/core/beacon/aggregation/cohort/messages/keygen/cohort-ready.js.map +0 -1
  188. package/dist/esm/core/beacon/aggregation/cohort/messages/keygen/opt-in-accept.js +0 -27
  189. package/dist/esm/core/beacon/aggregation/cohort/messages/keygen/opt-in-accept.js.map +0 -1
  190. package/dist/esm/core/beacon/aggregation/cohort/messages/keygen/opt-in.js +0 -23
  191. package/dist/esm/core/beacon/aggregation/cohort/messages/keygen/opt-in.js.map +0 -1
  192. package/dist/esm/core/beacon/aggregation/cohort/messages/keygen/subscribe.js +0 -28
  193. package/dist/esm/core/beacon/aggregation/cohort/messages/keygen/subscribe.js.map +0 -1
  194. package/dist/esm/core/beacon/aggregation/cohort/messages/sign/aggregated-nonce.js +0 -29
  195. package/dist/esm/core/beacon/aggregation/cohort/messages/sign/aggregated-nonce.js.map +0 -1
  196. package/dist/esm/core/beacon/aggregation/cohort/messages/sign/authorization-request.js +0 -30
  197. package/dist/esm/core/beacon/aggregation/cohort/messages/sign/authorization-request.js.map +0 -1
  198. package/dist/esm/core/beacon/aggregation/cohort/messages/sign/nonce-contribution.js +0 -30
  199. package/dist/esm/core/beacon/aggregation/cohort/messages/sign/nonce-contribution.js.map +0 -1
  200. package/dist/esm/core/beacon/aggregation/cohort/messages/sign/request-signature.js +0 -30
  201. package/dist/esm/core/beacon/aggregation/cohort/messages/sign/request-signature.js.map +0 -1
  202. package/dist/esm/core/beacon/aggregation/cohort/messages/sign/signature-authorization.js +0 -31
  203. package/dist/esm/core/beacon/aggregation/cohort/messages/sign/signature-authorization.js.map +0 -1
  204. package/dist/esm/core/beacon/aggregation/cohort/status.js +0 -8
  205. package/dist/esm/core/beacon/aggregation/cohort/status.js.map +0 -1
  206. package/dist/esm/core/beacon/aggregation/communication/adapter/did-comm.js +0 -121
  207. package/dist/esm/core/beacon/aggregation/communication/adapter/did-comm.js.map +0 -1
  208. package/dist/esm/core/beacon/aggregation/communication/adapter/nostr.js +0 -245
  209. package/dist/esm/core/beacon/aggregation/communication/adapter/nostr.js.map +0 -1
  210. package/dist/esm/core/beacon/aggregation/communication/error.js +0 -12
  211. package/dist/esm/core/beacon/aggregation/communication/error.js.map +0 -1
  212. package/dist/esm/core/beacon/aggregation/communication/factory.js +0 -21
  213. package/dist/esm/core/beacon/aggregation/communication/factory.js.map +0 -1
  214. package/dist/esm/core/beacon/aggregation/communication/service.js +0 -2
  215. package/dist/esm/core/beacon/aggregation/communication/service.js.map +0 -1
  216. package/dist/esm/core/beacon/aggregation/coordinator.js +0 -343
  217. package/dist/esm/core/beacon/aggregation/coordinator.js.map +0 -1
  218. package/dist/esm/core/beacon/aggregation/participant.js +0 -435
  219. package/dist/esm/core/beacon/aggregation/participant.js.map +0 -1
  220. package/dist/esm/core/beacon/aggregation/session/index.js +0 -244
  221. package/dist/esm/core/beacon/aggregation/session/index.js.map +0 -1
  222. package/dist/esm/core/beacon/aggregation/session/status.js +0 -11
  223. package/dist/esm/core/beacon/aggregation/session/status.js.map +0 -1
  224. package/dist/esm/core/update.js +0 -112
  225. package/dist/esm/core/update.js.map +0 -1
  226. package/dist/types/core/beacon/aggregation/cohort/index.d.ts +0 -136
  227. package/dist/types/core/beacon/aggregation/cohort/index.d.ts.map +0 -1
  228. package/dist/types/core/beacon/aggregation/cohort/messages/base.d.ts.map +0 -1
  229. package/dist/types/core/beacon/aggregation/cohort/messages/constants.d.ts +0 -11
  230. package/dist/types/core/beacon/aggregation/cohort/messages/constants.d.ts.map +0 -1
  231. package/dist/types/core/beacon/aggregation/cohort/messages/index.d.ts +0 -65
  232. package/dist/types/core/beacon/aggregation/cohort/messages/index.d.ts.map +0 -1
  233. package/dist/types/core/beacon/aggregation/cohort/messages/keygen/cohort-advert.d.ts +0 -29
  234. package/dist/types/core/beacon/aggregation/cohort/messages/keygen/cohort-advert.d.ts.map +0 -1
  235. package/dist/types/core/beacon/aggregation/cohort/messages/keygen/cohort-ready.d.ts +0 -26
  236. package/dist/types/core/beacon/aggregation/cohort/messages/keygen/cohort-ready.d.ts.map +0 -1
  237. package/dist/types/core/beacon/aggregation/cohort/messages/keygen/opt-in-accept.d.ts +0 -24
  238. package/dist/types/core/beacon/aggregation/cohort/messages/keygen/opt-in-accept.d.ts.map +0 -1
  239. package/dist/types/core/beacon/aggregation/cohort/messages/keygen/opt-in.d.ts +0 -20
  240. package/dist/types/core/beacon/aggregation/cohort/messages/keygen/opt-in.d.ts.map +0 -1
  241. package/dist/types/core/beacon/aggregation/cohort/messages/keygen/subscribe.d.ts +0 -25
  242. package/dist/types/core/beacon/aggregation/cohort/messages/keygen/subscribe.d.ts.map +0 -1
  243. package/dist/types/core/beacon/aggregation/cohort/messages/sign/aggregated-nonce.d.ts +0 -25
  244. package/dist/types/core/beacon/aggregation/cohort/messages/sign/aggregated-nonce.d.ts.map +0 -1
  245. package/dist/types/core/beacon/aggregation/cohort/messages/sign/authorization-request.d.ts +0 -26
  246. package/dist/types/core/beacon/aggregation/cohort/messages/sign/authorization-request.d.ts.map +0 -1
  247. package/dist/types/core/beacon/aggregation/cohort/messages/sign/nonce-contribution.d.ts +0 -26
  248. package/dist/types/core/beacon/aggregation/cohort/messages/sign/nonce-contribution.d.ts.map +0 -1
  249. package/dist/types/core/beacon/aggregation/cohort/messages/sign/request-signature.d.ts +0 -26
  250. package/dist/types/core/beacon/aggregation/cohort/messages/sign/request-signature.d.ts.map +0 -1
  251. package/dist/types/core/beacon/aggregation/cohort/messages/sign/signature-authorization.d.ts +0 -27
  252. package/dist/types/core/beacon/aggregation/cohort/messages/sign/signature-authorization.d.ts.map +0 -1
  253. package/dist/types/core/beacon/aggregation/cohort/status.d.ts +0 -8
  254. package/dist/types/core/beacon/aggregation/cohort/status.d.ts.map +0 -1
  255. package/dist/types/core/beacon/aggregation/communication/adapter/did-comm.d.ts +0 -89
  256. package/dist/types/core/beacon/aggregation/communication/adapter/did-comm.d.ts.map +0 -1
  257. package/dist/types/core/beacon/aggregation/communication/adapter/nostr.d.ts +0 -103
  258. package/dist/types/core/beacon/aggregation/communication/adapter/nostr.d.ts.map +0 -1
  259. package/dist/types/core/beacon/aggregation/communication/error.d.ts.map +0 -1
  260. package/dist/types/core/beacon/aggregation/communication/factory.d.ts +0 -10
  261. package/dist/types/core/beacon/aggregation/communication/factory.d.ts.map +0 -1
  262. package/dist/types/core/beacon/aggregation/communication/service.d.ts +0 -36
  263. package/dist/types/core/beacon/aggregation/communication/service.d.ts.map +0 -1
  264. package/dist/types/core/beacon/aggregation/coordinator.d.ts +0 -116
  265. package/dist/types/core/beacon/aggregation/coordinator.d.ts.map +0 -1
  266. package/dist/types/core/beacon/aggregation/participant.d.ts +0 -192
  267. package/dist/types/core/beacon/aggregation/participant.d.ts.map +0 -1
  268. package/dist/types/core/beacon/aggregation/session/index.d.ts +0 -156
  269. package/dist/types/core/beacon/aggregation/session/index.d.ts.map +0 -1
  270. package/dist/types/core/beacon/aggregation/session/status.d.ts +0 -11
  271. package/dist/types/core/beacon/aggregation/session/status.d.ts.map +0 -1
  272. package/dist/types/core/update.d.ts +0 -52
  273. package/dist/types/core/update.d.ts.map +0 -1
  274. package/src/core/beacon/aggregation/cohort/index.ts +0 -305
  275. package/src/core/beacon/aggregation/cohort/messages/constants.ts +0 -12
  276. package/src/core/beacon/aggregation/cohort/messages/index.ts +0 -143
  277. package/src/core/beacon/aggregation/cohort/messages/keygen/cohort-advert.ts +0 -44
  278. package/src/core/beacon/aggregation/cohort/messages/keygen/cohort-ready.ts +0 -40
  279. package/src/core/beacon/aggregation/cohort/messages/keygen/opt-in-accept.ts +0 -35
  280. package/src/core/beacon/aggregation/cohort/messages/keygen/opt-in.ts +0 -34
  281. package/src/core/beacon/aggregation/cohort/messages/keygen/subscribe.ts +0 -36
  282. package/src/core/beacon/aggregation/cohort/messages/sign/aggregated-nonce.ts +0 -39
  283. package/src/core/beacon/aggregation/cohort/messages/sign/authorization-request.ts +0 -40
  284. package/src/core/beacon/aggregation/cohort/messages/sign/nonce-contribution.ts +0 -40
  285. package/src/core/beacon/aggregation/cohort/messages/sign/request-signature.ts +0 -40
  286. package/src/core/beacon/aggregation/cohort/messages/sign/signature-authorization.ts +0 -41
  287. package/src/core/beacon/aggregation/cohort/status.ts +0 -7
  288. package/src/core/beacon/aggregation/communication/adapter/did-comm.ts +0 -148
  289. package/src/core/beacon/aggregation/communication/adapter/nostr.ts +0 -323
  290. package/src/core/beacon/aggregation/communication/error.ts +0 -13
  291. package/src/core/beacon/aggregation/communication/factory.ts +0 -25
  292. package/src/core/beacon/aggregation/communication/service.ts +0 -42
  293. package/src/core/beacon/aggregation/coordinator.ts +0 -419
  294. package/src/core/beacon/aggregation/participant.ts +0 -517
  295. package/src/core/beacon/aggregation/session/index.ts +0 -301
  296. package/src/core/beacon/aggregation/session/status.ts +0 -18
  297. package/src/core/update.ts +0 -158
@@ -0,0 +1,365 @@
1
+ import type { SchnorrKeyPair } from '@did-btcr2/keypair';
2
+ import type { BaseMessage } from '../messages/base.js';
3
+ import {
4
+ COHORT_OPT_IN,
5
+ NONCE_CONTRIBUTION,
6
+ SIGNATURE_AUTHORIZATION,
7
+ SUBMIT_UPDATE,
8
+ VALIDATION_ACK,
9
+ } from '../messages/constants.js';
10
+ import { ServiceCohortPhase } from '../phases.js';
11
+ import type {
12
+ AggregationResult,
13
+ CohortConfig,
14
+ PendingOptIn,
15
+ SigningTxData} from '../service.js';
16
+ import {
17
+ AggregationService
18
+ } from '../service.js';
19
+ import type { Transport } from '../transport/transport.js';
20
+ import type { AggregationServiceEvents } from './events.js';
21
+ import { TypedEventEmitter } from './typed-emitter.js';
22
+
23
+ /** Decision callback: accept or reject a participant's opt-in. */
24
+ export type OnOptInReceived = (optIn: PendingOptIn) => Promise<{ accepted: boolean }>;
25
+
26
+ /** Decision callback: finalize keygen now, or wait for more participants. */
27
+ export type OnReadyToFinalize = (info: {
28
+ acceptedCount: number;
29
+ minRequired: number;
30
+ }) => Promise<{ finalize: boolean }>;
31
+
32
+ /** Data callback: provide the Bitcoin transaction data to sign. */
33
+ export type OnProvideTxData = (info: {
34
+ cohortId: string;
35
+ beaconAddress: string;
36
+ signalBytes: Uint8Array;
37
+ }) => Promise<SigningTxData>;
38
+
39
+ export interface AggregationServiceRunnerOptions {
40
+ /** Underlying transport (NostrTransport, MockTransport, etc.). */
41
+ transport: Transport;
42
+
43
+ /** This service's identity. */
44
+ did: string;
45
+ keys: SchnorrKeyPair;
46
+
47
+ /** Cohort configuration. */
48
+ config: CohortConfig;
49
+
50
+ /**
51
+ * Decide whether to accept a participant's opt-in.
52
+ * Default: auto-accept all opt-ins.
53
+ */
54
+ onOptInReceived?: OnOptInReceived;
55
+
56
+ /**
57
+ * Decide whether to finalize keygen now or wait for more participants.
58
+ * Called after each accepted opt-in once minParticipants is reached.
59
+ * Default: finalize as soon as minParticipants is reached.
60
+ */
61
+ onReadyToFinalize?: OnReadyToFinalize;
62
+
63
+ /**
64
+ * Provide the Bitcoin transaction data to sign.
65
+ * REQUIRED — no sensible default.
66
+ */
67
+ onProvideTxData: OnProvideTxData;
68
+ }
69
+
70
+ /**
71
+ * High-level facade for running an Aggregation Service over a Transport.
72
+ *
73
+ * Wires the {@link AggregationService} state machine to a {@link Transport},
74
+ * encapsulating message handler registration, outgoing message dispatch,
75
+ * and decision callback orchestration.
76
+ *
77
+ * @example
78
+ * ```typescript
79
+ * const transport = new NostrTransport({ relays: [RELAY] });
80
+ * transport.registerActor(serviceDid, serviceKeys);
81
+ *
82
+ * const runner = new AggregationServiceRunner({
83
+ * transport,
84
+ * did: serviceDid,
85
+ * keys: serviceKeys,
86
+ * config: { minParticipants: 2, network: 'mutinynet', beaconType: 'CASBeacon' },
87
+ * onProvideTxData: async ({ beaconAddress, signalBytes }) => {
88
+ * return await buildBeaconTransaction(beaconAddress, signalBytes, bitcoin);
89
+ * },
90
+ * });
91
+ *
92
+ * runner.on('keygen-complete', ({ beaconAddress }) => console.log(beaconAddress));
93
+ * runner.on('signing-complete', ({ signature }) => console.log('done'));
94
+ *
95
+ * const result = await runner.run();
96
+ * ```
97
+ *
98
+ * For full manual control, drop down to the underlying state machine via
99
+ * `runner.session`. The state machine has no transport coupling and exposes
100
+ * every protocol decision as an explicit method.
101
+ *
102
+ * @class AggregationServiceRunner
103
+ */
104
+ export class AggregationServiceRunner extends TypedEventEmitter<AggregationServiceEvents> {
105
+ /** Direct access to the underlying state machine for advanced use. */
106
+ readonly session: AggregationService;
107
+
108
+ readonly #transport: Transport;
109
+ readonly #did: string;
110
+ readonly #config: CohortConfig;
111
+ readonly #onOptInReceived: OnOptInReceived;
112
+ readonly #onReadyToFinalize: OnReadyToFinalize;
113
+ readonly #onProvideTxData: OnProvideTxData;
114
+
115
+ #cohortId?: string;
116
+ #handlersRegistered = false;
117
+ #stopped = false;
118
+ #resolveRun?: (result: AggregationResult) => void;
119
+ #rejectRun?: (err: Error) => void;
120
+
121
+ constructor(options: AggregationServiceRunnerOptions) {
122
+ super();
123
+ this.#transport = options.transport;
124
+ this.#did = options.did;
125
+ this.#config = options.config;
126
+ this.#onOptInReceived = options.onOptInReceived ?? (async () => ({ accepted: true }));
127
+ this.#onReadyToFinalize = options.onReadyToFinalize ?? (async ({ acceptedCount, minRequired }) => ({
128
+ finalize : acceptedCount >= minRequired,
129
+ }));
130
+ this.#onProvideTxData = options.onProvideTxData;
131
+
132
+ this.session = new AggregationService({ did: options.did, keys: options.keys });
133
+ }
134
+
135
+ /**
136
+ * Run the protocol to completion. Resolves with the final aggregation result
137
+ * (signature + signed transaction) once signing is complete.
138
+ *
139
+ * @returns {Promise<AggregationResult>} The final result with signature and signed tx.
140
+ */
141
+ run(): Promise<AggregationResult> {
142
+ return new Promise((resolve, reject) => {
143
+ this.#resolveRun = resolve;
144
+ this.#rejectRun = reject;
145
+
146
+ try {
147
+ this.#registerHandlers();
148
+ this.#cohortId = this.session.createCohort(this.#config);
149
+ // Emit cohort-advertised BEFORE the send so the event fires before any downstream cascade
150
+ const advertMsgs = this.session.advertise(this.#cohortId);
151
+ this.emit('cohort-advertised', { cohortId: this.#cohortId });
152
+ this.#sendAll(advertMsgs).catch(err => this.#fail(err));
153
+ } catch(err) {
154
+ this.#fail(err as Error);
155
+ }
156
+ });
157
+ }
158
+
159
+ /**
160
+ * Stop the runner early. Cleans up internal state.
161
+ * Note: does not unregister transport handlers (the transport interface
162
+ * does not currently expose unregister).
163
+ */
164
+ stop(): void {
165
+ this.#stopped = true;
166
+ }
167
+
168
+ /**
169
+ * Internal: handler registration with the transport. Idempotent.
170
+ */
171
+ #registerHandlers(): void {
172
+ if(this.#handlersRegistered) return;
173
+ this.#handlersRegistered = true;
174
+
175
+ this.#transport.registerMessageHandler(this.#did, COHORT_OPT_IN, this.#handleOptIn.bind(this));
176
+ this.#transport.registerMessageHandler(this.#did, SUBMIT_UPDATE, this.#handleSubmitUpdate.bind(this));
177
+ this.#transport.registerMessageHandler(this.#did, VALIDATION_ACK, this.#handleValidationAck.bind(this));
178
+ this.#transport.registerMessageHandler(this.#did, NONCE_CONTRIBUTION, this.#handleNonceContribution.bind(this));
179
+ this.#transport.registerMessageHandler(this.#did, SIGNATURE_AUTHORIZATION, this.#handleSignatureAuthorization.bind(this));
180
+ }
181
+
182
+ /**
183
+ * Internal: message handlers for each protocol step. Each handler:
184
+ * 1) feeds the message into the state machine via session.receive()
185
+ * 2) emits a high-level event for external observers
186
+ * 3) checks if the new state triggers any automatic next steps, and if so:
187
+ * a) calls the appropriate decision callback(s)
188
+ * b) sends any resulting messages from the state machine
189
+ * @param {BaseMessage} msg - The incoming message to handle.
190
+ * @returns {Promise<void>} Resolves when handling is complete.
191
+ * @throws {Error} If any step of handling fails, the error is emitted and the run promise is rejected.
192
+ * Note: if the runner has been stopped, handlers will ignore incoming messages.
193
+ */
194
+ async #handleOptIn(msg: BaseMessage): Promise<void> {
195
+ if(this.#stopped) return;
196
+ try {
197
+ this.session.receive(msg);
198
+
199
+ const optIn = this.session.pendingOptIns(this.#cohortId!).get(msg.from);
200
+ if(!optIn) return;
201
+ this.emit('opt-in-received', optIn);
202
+
203
+ // Register peer key for encrypted messaging
204
+ if(optIn.communicationPk) {
205
+ this.#transport.registerPeer(msg.from, optIn.communicationPk);
206
+ }
207
+
208
+ const decision = await this.#onOptInReceived(optIn);
209
+ if(!decision.accepted) return;
210
+
211
+ await this.#sendAll(this.session.acceptParticipant(this.#cohortId!, msg.from));
212
+ this.emit('participant-accepted', { participantDid: msg.from });
213
+
214
+ // Check if it's time to finalize
215
+ const cohort = this.session.getCohort(this.#cohortId!)!;
216
+ if(cohort.participants.length >= this.#config.minParticipants) {
217
+ const finalizeDecision = await this.#onReadyToFinalize({
218
+ acceptedCount : cohort.participants.length,
219
+ minRequired : this.#config.minParticipants,
220
+ });
221
+ if(finalizeDecision.finalize) {
222
+ // finalizeKeygen() computes the beacon address synchronously
223
+ // emit BEFORE awaiting sendAll. Otherwise the downstream cascade
224
+ // (which can run all the way to signing-complete) would resolve the
225
+ // run() promise before this event fires.
226
+ const readyMsgs = this.session.finalizeKeygen(this.#cohortId!);
227
+ this.emit('keygen-complete', {
228
+ cohortId : this.#cohortId!,
229
+ beaconAddress : cohort.beaconAddress,
230
+ });
231
+ await this.#sendAll(readyMsgs);
232
+ }
233
+ }
234
+ } catch(err) {
235
+ this.#fail(err as Error);
236
+ }
237
+ }
238
+
239
+ /**
240
+ * Handler for receiving participant updates. When all updates are received, automatically builds
241
+ * and distributes the data for validation.
242
+ * @param {BaseMessage} msg - The incoming message to handle.
243
+ * @returns {Promise<void>} Resolves when handling is complete.
244
+ * @throws {Error} If any step of handling fails, the error is emitted and the run promise is rejected.
245
+ * Note: if the runner has been stopped, handlers will ignore incoming messages.
246
+ */
247
+ async #handleSubmitUpdate(msg: BaseMessage): Promise<void> {
248
+ if(this.#stopped) return;
249
+ try {
250
+ this.session.receive(msg);
251
+ this.emit('update-received', { participantDid: msg.from });
252
+
253
+ // When all updates collected, build and distribute
254
+ if(this.session.getCohortPhase(this.#cohortId!) === ServiceCohortPhase.UpdatesCollected) {
255
+ const distributeMsgs = this.session.buildAndDistribute(this.#cohortId!);
256
+ this.emit('data-distributed', { cohortId: this.#cohortId! });
257
+ await this.#sendAll(distributeMsgs);
258
+ }
259
+ } catch(err) {
260
+ this.#fail(err as Error);
261
+ }
262
+ }
263
+
264
+ /**
265
+ * Handler for receiving validation acknowledgments. When all validations are received,
266
+ * automatically requests tx data and starts signing.
267
+ * @param {BaseMessage} msg - The incoming message to handle.
268
+ * @returns {Promise<void>} Resolves when handling is complete.
269
+ * @throws {Error} If any step of handling fails, the error is emitted and the run promise is rejected.
270
+ * Note: if the runner has been stopped, handlers will ignore incoming messages.
271
+ */
272
+ async #handleValidationAck(msg: BaseMessage): Promise<void> {
273
+ if(this.#stopped) return;
274
+ try {
275
+ this.session.receive(msg);
276
+ const approved = !!msg.body?.approved;
277
+ this.emit('validation-received', { participantDid: msg.from, approved });
278
+
279
+ // When all validations received, request tx data and start signing
280
+ if(this.session.getCohortPhase(this.#cohortId!) === ServiceCohortPhase.Validated) {
281
+ const cohort = this.session.getCohort(this.#cohortId!)!;
282
+ const txData = await this.#onProvideTxData({
283
+ cohortId : this.#cohortId!,
284
+ beaconAddress : cohort.beaconAddress,
285
+ signalBytes : cohort.signalBytes!,
286
+ });
287
+ const authMsgs = this.session.startSigning(this.#cohortId!, txData);
288
+ const sessionId = this.session.getSigningSessionId(this.#cohortId!) ?? '';
289
+ this.emit('signing-started', { sessionId });
290
+ await this.#sendAll(authMsgs);
291
+ }
292
+ } catch(err) {
293
+ this.#fail(err as Error);
294
+ }
295
+ }
296
+
297
+ /**
298
+ * Handler for receiving nonce contributions and signature authorizations. When all nonces or
299
+ * signatures are received,
300
+ * @param {BaseMessage} msg - The incoming message to handle.
301
+ * @returns {Promise<void>} Resolves when handling is complete.
302
+ * @throws {Error} If any step of handling fails, the error is emitted and the run promise is rejected.
303
+ * Note: if the runner has been stopped, handlers will ignore incoming messages.
304
+ */
305
+ async #handleNonceContribution(msg: BaseMessage): Promise<void> {
306
+ if(this.#stopped) return;
307
+ try {
308
+ this.session.receive(msg);
309
+ this.emit('nonce-received', { participantDid: msg.from });
310
+
311
+ // When all nonces collected, send aggregated nonce
312
+ if(this.session.getCohortPhase(this.#cohortId!) === ServiceCohortPhase.NoncesCollected) {
313
+ await this.#sendAll(this.session.sendAggregatedNonce(this.#cohortId!));
314
+ }
315
+ } catch(err) {
316
+ this.#fail(err as Error);
317
+ }
318
+ }
319
+
320
+ /**
321
+ * Handler for receiving signature authorizations. When all partial signatures are received, the
322
+ * session automatically completes and the final result is emitted and the run() promise is resolved.
323
+ * @param {BaseMessage} msg - The incoming message to handle.
324
+ * @returns {Promise<void>} Resolves when handling is complete.
325
+ * @throws {Error} If any step of handling fails, the error is emitted and the run promise is rejected.
326
+ * Note: if the runner has been stopped, handlers will ignore incoming messages.
327
+ */
328
+ async #handleSignatureAuthorization(msg: BaseMessage): Promise<void> {
329
+ if(this.#stopped) return;
330
+ try {
331
+ this.session.receive(msg);
332
+
333
+ // The state machine auto-completes when all partial sigs received
334
+ const result = this.session.getResult(this.#cohortId!);
335
+ if(result) {
336
+ this.emit('signing-complete', result);
337
+ this.#resolveRun?.(result);
338
+ }
339
+ } catch(err) {
340
+ this.#fail(err as Error);
341
+ }
342
+ }
343
+
344
+ /**
345
+ * Internal: helper to send all messages sequentially. Catches and propagates errors.
346
+ * @param {BaseMessage[]} msgs - The messages to send.
347
+ * @returns {Promise<void>} Resolves when all messages have been sent.
348
+ * @throws {Error} If sending any message fails, the error is emitted and the run promise is
349
+ * rejected.
350
+ */
351
+ async #sendAll(msgs: BaseMessage[]): Promise<void> {
352
+ for(const m of msgs) {
353
+ await this.#transport.sendMessage(m, this.#did, m.to);
354
+ }
355
+ }
356
+
357
+ /**
358
+ * Internal: helper to handle errors. Emits an 'error' event and rejects the run promise.
359
+ * @param {Error} err - The error to handle.
360
+ */
361
+ #fail(err: Error): void {
362
+ this.emit('error', err);
363
+ this.#rejectRun?.(err);
364
+ }
365
+ }
@@ -0,0 +1,87 @@
1
+ /**
2
+ * Tiny strongly-typed event emitter.
3
+ *
4
+ * Browser-compatible (no Node.js `events` module). Uses a generic event map
5
+ * to give compile-time safety on event names and listener signatures.
6
+ *
7
+ * @example
8
+ * type MyEvents = {
9
+ * 'connected': [{ host: string }];
10
+ * 'data': [Uint8Array];
11
+ * 'error': [Error];
12
+ * };
13
+ *
14
+ * class MyClient extends TypedEventEmitter<MyEvents> {
15
+ * doStuff() {
16
+ * this.emit('connected', { host: 'example.com' }); // ✓ typed
17
+ * this.emit('data', new Uint8Array([1, 2])); // ✓ typed
18
+ * }
19
+ * }
20
+ *
21
+ * const client = new MyClient();
22
+ * client.on('connected', ({ host }) => console.log(host)); // ✓ typed
23
+ */
24
+
25
+ export type EventMap = Record<string, ReadonlyArray<unknown>>;
26
+
27
+ export type Listener<Args extends ReadonlyArray<unknown>> = (...args: Args) => void;
28
+
29
+ export class TypedEventEmitter<Events extends EventMap> {
30
+ #listeners: Map<keyof Events, Set<Listener<any>>> = new Map();
31
+
32
+ /** Subscribe to an event. */
33
+ on<K extends keyof Events>(event: K, listener: Listener<Events[K]>): this {
34
+ let set = this.#listeners.get(event);
35
+ if(!set) {
36
+ set = new Set();
37
+ this.#listeners.set(event, set);
38
+ }
39
+ set.add(listener as Listener<any>);
40
+ return this;
41
+ }
42
+
43
+ /** Subscribe to an event, automatically unsubscribing after the first call. */
44
+ once<K extends keyof Events>(event: K, listener: Listener<Events[K]>): this {
45
+ const wrapped = ((...args: Events[K]) => {
46
+ this.off(event, wrapped as Listener<Events[K]>);
47
+ listener(...args);
48
+ });
49
+ return this.on(event, wrapped as Listener<Events[K]>);
50
+ }
51
+
52
+ /** Unsubscribe a specific listener. */
53
+ off<K extends keyof Events>(event: K, listener: Listener<Events[K]>): this {
54
+ this.#listeners.get(event)?.delete(listener as Listener<any>);
55
+ return this;
56
+ }
57
+
58
+ /** Emit an event to all subscribed listeners. */
59
+ emit<K extends keyof Events>(event: K, ...args: Events[K]): boolean {
60
+ const set = this.#listeners.get(event);
61
+ if(!set || set.size === 0) return false;
62
+ for(const listener of set) {
63
+ try {
64
+ listener(...args);
65
+ } catch(err) {
66
+ // Listener errors must not break other listeners
67
+ console.error(`Listener for event "${String(event)}" threw:`, err);
68
+ }
69
+ }
70
+ return true;
71
+ }
72
+
73
+ /** Remove all listeners (optionally for a specific event). */
74
+ removeAllListeners<K extends keyof Events>(event?: K): this {
75
+ if(event === undefined) {
76
+ this.#listeners.clear();
77
+ } else {
78
+ this.#listeners.delete(event);
79
+ }
80
+ return this;
81
+ }
82
+
83
+ /** Number of listeners for an event. */
84
+ listenerCount<K extends keyof Events>(event: K): number {
85
+ return this.#listeners.get(event)?.size ?? 0;
86
+ }
87
+ }