@naylence/advanced-security 0.3.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 (304) hide show
  1. package/LICENSE +33 -0
  2. package/NOTICE +5 -0
  3. package/README.md +101 -0
  4. package/dist/browser/index.js +157043 -0
  5. package/dist/browser/index.js.map +1 -0
  6. package/dist/cjs/browser.js +6 -0
  7. package/dist/cjs/browser.js.map +1 -0
  8. package/dist/cjs/index.js +7 -0
  9. package/dist/cjs/index.js.map +1 -0
  10. package/dist/cjs/naylence/fame/factory-manifest.js +24 -0
  11. package/dist/cjs/naylence/fame/factory-manifest.js.map +1 -0
  12. package/dist/cjs/naylence/fame/security/cert/ca-service-client.js +284 -0
  13. package/dist/cjs/naylence/fame/security/cert/ca-service-client.js.map +1 -0
  14. package/dist/cjs/naylence/fame/security/cert/ca-service-factory.js +65 -0
  15. package/dist/cjs/naylence/fame/security/cert/ca-service-factory.js.map +1 -0
  16. package/dist/cjs/naylence/fame/security/cert/ca-types.js +36 -0
  17. package/dist/cjs/naylence/fame/security/cert/ca-types.js.map +1 -0
  18. package/dist/cjs/naylence/fame/security/cert/default-ca-service-factory.js +70 -0
  19. package/dist/cjs/naylence/fame/security/cert/default-ca-service-factory.js.map +1 -0
  20. package/dist/cjs/naylence/fame/security/cert/default-ca-service.js +270 -0
  21. package/dist/cjs/naylence/fame/security/cert/default-ca-service.js.map +1 -0
  22. package/dist/cjs/naylence/fame/security/cert/default-certificate-manager-factory.js +77 -0
  23. package/dist/cjs/naylence/fame/security/cert/default-certificate-manager-factory.js.map +1 -0
  24. package/dist/cjs/naylence/fame/security/cert/default-certificate-manager.js +675 -0
  25. package/dist/cjs/naylence/fame/security/cert/default-certificate-manager.js.map +1 -0
  26. package/dist/cjs/naylence/fame/security/cert/grants.js +5 -0
  27. package/dist/cjs/naylence/fame/security/cert/grants.js.map +1 -0
  28. package/dist/cjs/naylence/fame/security/cert/index.js +52 -0
  29. package/dist/cjs/naylence/fame/security/cert/index.js.map +1 -0
  30. package/dist/cjs/naylence/fame/security/cert/internal-ca-service.js +793 -0
  31. package/dist/cjs/naylence/fame/security/cert/internal-ca-service.js.map +1 -0
  32. package/dist/cjs/naylence/fame/security/cert/util.js +120 -0
  33. package/dist/cjs/naylence/fame/security/cert/util.js.map +1 -0
  34. package/dist/cjs/naylence/fame/security/encryption/channel/channel-encryption-manager-factory.js +89 -0
  35. package/dist/cjs/naylence/fame/security/encryption/channel/channel-encryption-manager-factory.js.map +1 -0
  36. package/dist/cjs/naylence/fame/security/encryption/channel/channel-encryption-manager.js +732 -0
  37. package/dist/cjs/naylence/fame/security/encryption/channel/channel-encryption-manager.js.map +1 -0
  38. package/dist/cjs/naylence/fame/security/encryption/channel/index.js +8 -0
  39. package/dist/cjs/naylence/fame/security/encryption/channel/index.js.map +1 -0
  40. package/dist/cjs/naylence/fame/security/encryption/composite-encryption-manager-factory.js +117 -0
  41. package/dist/cjs/naylence/fame/security/encryption/composite-encryption-manager-factory.js.map +1 -0
  42. package/dist/cjs/naylence/fame/security/encryption/composite-encryption-manager.js +325 -0
  43. package/dist/cjs/naylence/fame/security/encryption/composite-encryption-manager.js.map +1 -0
  44. package/dist/cjs/naylence/fame/security/encryption/default-secure-channel-manager-factory.js +57 -0
  45. package/dist/cjs/naylence/fame/security/encryption/default-secure-channel-manager-factory.js.map +1 -0
  46. package/dist/cjs/naylence/fame/security/encryption/default-secure-channel-manager.js +282 -0
  47. package/dist/cjs/naylence/fame/security/encryption/default-secure-channel-manager.js.map +1 -0
  48. package/dist/cjs/naylence/fame/security/encryption/encryption-manager-registry.js +173 -0
  49. package/dist/cjs/naylence/fame/security/encryption/encryption-manager-registry.js.map +1 -0
  50. package/dist/cjs/naylence/fame/security/encryption/index.js +16 -0
  51. package/dist/cjs/naylence/fame/security/encryption/index.js.map +1 -0
  52. package/dist/cjs/naylence/fame/security/encryption/sealed/index.js +8 -0
  53. package/dist/cjs/naylence/fame/security/encryption/sealed/index.js.map +1 -0
  54. package/dist/cjs/naylence/fame/security/encryption/sealed/x25519-encryption-manager-factory.js +86 -0
  55. package/dist/cjs/naylence/fame/security/encryption/sealed/x25519-encryption-manager-factory.js.map +1 -0
  56. package/dist/cjs/naylence/fame/security/encryption/sealed/x25519-encryption-manager.js +546 -0
  57. package/dist/cjs/naylence/fame/security/encryption/sealed/x25519-encryption-manager.js.map +1 -0
  58. package/dist/cjs/naylence/fame/security/index.js +11 -0
  59. package/dist/cjs/naylence/fame/security/index.js.map +1 -0
  60. package/dist/cjs/naylence/fame/security/keys/index.js +9 -0
  61. package/dist/cjs/naylence/fame/security/keys/index.js.map +1 -0
  62. package/dist/cjs/naylence/fame/security/keys/x5c-key-manager-factory.js +40 -0
  63. package/dist/cjs/naylence/fame/security/keys/x5c-key-manager-factory.js.map +1 -0
  64. package/dist/cjs/naylence/fame/security/keys/x5c-key-manager.js +441 -0
  65. package/dist/cjs/naylence/fame/security/keys/x5c-key-manager.js.map +1 -0
  66. package/dist/cjs/naylence/fame/security/register-advanced-security-factories.js +131 -0
  67. package/dist/cjs/naylence/fame/security/register-advanced-security-factories.js.map +1 -0
  68. package/dist/cjs/naylence/fame/security/signing/eddsa-envelope-signer-factory.js +71 -0
  69. package/dist/cjs/naylence/fame/security/signing/eddsa-envelope-signer-factory.js.map +1 -0
  70. package/dist/cjs/naylence/fame/security/signing/eddsa-envelope-verifier-factory.js +31 -0
  71. package/dist/cjs/naylence/fame/security/signing/eddsa-envelope-verifier-factory.js.map +1 -0
  72. package/dist/cjs/naylence/fame/security/signing/eddsa-envelope-verifier.js +176 -0
  73. package/dist/cjs/naylence/fame/security/signing/eddsa-envelope-verifier.js.map +1 -0
  74. package/dist/cjs/naylence/fame/stickiness/aft-helper.js +77 -0
  75. package/dist/cjs/naylence/fame/stickiness/aft-helper.js.map +1 -0
  76. package/dist/cjs/naylence/fame/stickiness/aft-load-balancer-stickiness-manager-factory.js +69 -0
  77. package/dist/cjs/naylence/fame/stickiness/aft-load-balancer-stickiness-manager-factory.js.map +1 -0
  78. package/dist/cjs/naylence/fame/stickiness/aft-load-balancer-stickiness-manager.js +451 -0
  79. package/dist/cjs/naylence/fame/stickiness/aft-load-balancer-stickiness-manager.js.map +1 -0
  80. package/dist/cjs/naylence/fame/stickiness/aft-model.js +62 -0
  81. package/dist/cjs/naylence/fame/stickiness/aft-model.js.map +1 -0
  82. package/dist/cjs/naylence/fame/stickiness/aft-replica-stickiness-manager-factory.js +54 -0
  83. package/dist/cjs/naylence/fame/stickiness/aft-replica-stickiness-manager-factory.js.map +1 -0
  84. package/dist/cjs/naylence/fame/stickiness/aft-replica-stickiness-manager.js +208 -0
  85. package/dist/cjs/naylence/fame/stickiness/aft-replica-stickiness-manager.js.map +1 -0
  86. package/dist/cjs/naylence/fame/stickiness/aft-signer.js +154 -0
  87. package/dist/cjs/naylence/fame/stickiness/aft-signer.js.map +1 -0
  88. package/dist/cjs/naylence/fame/stickiness/aft-utils.js +95 -0
  89. package/dist/cjs/naylence/fame/stickiness/aft-utils.js.map +1 -0
  90. package/dist/cjs/naylence/fame/stickiness/aft-verifier.js +297 -0
  91. package/dist/cjs/naylence/fame/stickiness/aft-verifier.js.map +1 -0
  92. package/dist/cjs/naylence/fame/stickiness/index.js +40 -0
  93. package/dist/cjs/naylence/fame/stickiness/index.js.map +1 -0
  94. package/dist/cjs/naylence/fame/stickiness/stickiness-mode.js +28 -0
  95. package/dist/cjs/naylence/fame/stickiness/stickiness-mode.js.map +1 -0
  96. package/dist/cjs/naylence/fame/welcome/advanced-welcome-service-factory.js +97 -0
  97. package/dist/cjs/naylence/fame/welcome/advanced-welcome-service-factory.js.map +1 -0
  98. package/dist/cjs/naylence/fame/welcome/advanced-welcome-service.js +216 -0
  99. package/dist/cjs/naylence/fame/welcome/advanced-welcome-service.js.map +1 -0
  100. package/dist/cjs/naylence/fame/welcome/index.js +9 -0
  101. package/dist/cjs/naylence/fame/welcome/index.js.map +1 -0
  102. package/dist/cjs/plugin.js +41 -0
  103. package/dist/cjs/plugin.js.map +1 -0
  104. package/dist/esm/browser.js +3 -0
  105. package/dist/esm/browser.js.map +1 -0
  106. package/dist/esm/index.js +4 -0
  107. package/dist/esm/index.js.map +1 -0
  108. package/dist/esm/naylence/fame/factory-manifest.js +21 -0
  109. package/dist/esm/naylence/fame/factory-manifest.js.map +1 -0
  110. package/dist/esm/naylence/fame/security/cert/ca-server.js +153 -0
  111. package/dist/esm/naylence/fame/security/cert/ca-server.js.map +1 -0
  112. package/dist/esm/naylence/fame/security/cert/ca-service-client.js +278 -0
  113. package/dist/esm/naylence/fame/security/cert/ca-service-client.js.map +1 -0
  114. package/dist/esm/naylence/fame/security/cert/ca-service-factory.js +61 -0
  115. package/dist/esm/naylence/fame/security/cert/ca-service-factory.js.map +1 -0
  116. package/dist/esm/naylence/fame/security/cert/ca-types.js +31 -0
  117. package/dist/esm/naylence/fame/security/cert/ca-types.js.map +1 -0
  118. package/dist/esm/naylence/fame/security/cert/default-ca-service-factory.js +66 -0
  119. package/dist/esm/naylence/fame/security/cert/default-ca-service-factory.js.map +1 -0
  120. package/dist/esm/naylence/fame/security/cert/default-ca-service.js +233 -0
  121. package/dist/esm/naylence/fame/security/cert/default-ca-service.js.map +1 -0
  122. package/dist/esm/naylence/fame/security/cert/default-certificate-manager-factory.js +73 -0
  123. package/dist/esm/naylence/fame/security/cert/default-certificate-manager-factory.js.map +1 -0
  124. package/dist/esm/naylence/fame/security/cert/default-certificate-manager.js +638 -0
  125. package/dist/esm/naylence/fame/security/cert/default-certificate-manager.js.map +1 -0
  126. package/dist/esm/naylence/fame/security/cert/grants.js +2 -0
  127. package/dist/esm/naylence/fame/security/cert/grants.js.map +1 -0
  128. package/dist/esm/naylence/fame/security/cert/index.js +12 -0
  129. package/dist/esm/naylence/fame/security/cert/index.js.map +1 -0
  130. package/dist/esm/naylence/fame/security/cert/internal-ca-service.js +750 -0
  131. package/dist/esm/naylence/fame/security/cert/internal-ca-service.js.map +1 -0
  132. package/dist/esm/naylence/fame/security/cert/util.js +116 -0
  133. package/dist/esm/naylence/fame/security/cert/util.js.map +1 -0
  134. package/dist/esm/naylence/fame/security/encryption/channel/channel-encryption-manager-factory.js +85 -0
  135. package/dist/esm/naylence/fame/security/encryption/channel/channel-encryption-manager-factory.js.map +1 -0
  136. package/dist/esm/naylence/fame/security/encryption/channel/channel-encryption-manager.js +728 -0
  137. package/dist/esm/naylence/fame/security/encryption/channel/channel-encryption-manager.js.map +1 -0
  138. package/dist/esm/naylence/fame/security/encryption/channel/index.js +3 -0
  139. package/dist/esm/naylence/fame/security/encryption/channel/index.js.map +1 -0
  140. package/dist/esm/naylence/fame/security/encryption/composite-encryption-manager-factory.js +113 -0
  141. package/dist/esm/naylence/fame/security/encryption/composite-encryption-manager-factory.js.map +1 -0
  142. package/dist/esm/naylence/fame/security/encryption/composite-encryption-manager.js +321 -0
  143. package/dist/esm/naylence/fame/security/encryption/composite-encryption-manager.js.map +1 -0
  144. package/dist/esm/naylence/fame/security/encryption/default-secure-channel-manager-factory.js +53 -0
  145. package/dist/esm/naylence/fame/security/encryption/default-secure-channel-manager-factory.js.map +1 -0
  146. package/dist/esm/naylence/fame/security/encryption/default-secure-channel-manager.js +278 -0
  147. package/dist/esm/naylence/fame/security/encryption/default-secure-channel-manager.js.map +1 -0
  148. package/dist/esm/naylence/fame/security/encryption/encryption-manager-registry.js +167 -0
  149. package/dist/esm/naylence/fame/security/encryption/encryption-manager-registry.js.map +1 -0
  150. package/dist/esm/naylence/fame/security/encryption/index.js +7 -0
  151. package/dist/esm/naylence/fame/security/encryption/index.js.map +1 -0
  152. package/dist/esm/naylence/fame/security/encryption/sealed/index.js +3 -0
  153. package/dist/esm/naylence/fame/security/encryption/sealed/index.js.map +1 -0
  154. package/dist/esm/naylence/fame/security/encryption/sealed/x25519-encryption-manager-factory.js +82 -0
  155. package/dist/esm/naylence/fame/security/encryption/sealed/x25519-encryption-manager-factory.js.map +1 -0
  156. package/dist/esm/naylence/fame/security/encryption/sealed/x25519-encryption-manager.js +542 -0
  157. package/dist/esm/naylence/fame/security/encryption/sealed/x25519-encryption-manager.js.map +1 -0
  158. package/dist/esm/naylence/fame/security/index.js +6 -0
  159. package/dist/esm/naylence/fame/security/index.js.map +1 -0
  160. package/dist/esm/naylence/fame/security/keys/index.js +3 -0
  161. package/dist/esm/naylence/fame/security/keys/index.js.map +1 -0
  162. package/dist/esm/naylence/fame/security/keys/x5c-key-manager-factory.js +36 -0
  163. package/dist/esm/naylence/fame/security/keys/x5c-key-manager-factory.js.map +1 -0
  164. package/dist/esm/naylence/fame/security/keys/x5c-key-manager.js +405 -0
  165. package/dist/esm/naylence/fame/security/keys/x5c-key-manager.js.map +1 -0
  166. package/dist/esm/naylence/fame/security/register-advanced-security-factories.js +95 -0
  167. package/dist/esm/naylence/fame/security/register-advanced-security-factories.js.map +1 -0
  168. package/dist/esm/naylence/fame/security/signing/eddsa-envelope-signer-factory.js +34 -0
  169. package/dist/esm/naylence/fame/security/signing/eddsa-envelope-signer-factory.js.map +1 -0
  170. package/dist/esm/naylence/fame/security/signing/eddsa-envelope-verifier-factory.js +27 -0
  171. package/dist/esm/naylence/fame/security/signing/eddsa-envelope-verifier-factory.js.map +1 -0
  172. package/dist/esm/naylence/fame/security/signing/eddsa-envelope-verifier.js +172 -0
  173. package/dist/esm/naylence/fame/security/signing/eddsa-envelope-verifier.js.map +1 -0
  174. package/dist/esm/naylence/fame/stickiness/aft-helper.js +72 -0
  175. package/dist/esm/naylence/fame/stickiness/aft-helper.js.map +1 -0
  176. package/dist/esm/naylence/fame/stickiness/aft-load-balancer-stickiness-manager-factory.js +65 -0
  177. package/dist/esm/naylence/fame/stickiness/aft-load-balancer-stickiness-manager-factory.js.map +1 -0
  178. package/dist/esm/naylence/fame/stickiness/aft-load-balancer-stickiness-manager.js +447 -0
  179. package/dist/esm/naylence/fame/stickiness/aft-load-balancer-stickiness-manager.js.map +1 -0
  180. package/dist/esm/naylence/fame/stickiness/aft-model.js +54 -0
  181. package/dist/esm/naylence/fame/stickiness/aft-model.js.map +1 -0
  182. package/dist/esm/naylence/fame/stickiness/aft-replica-stickiness-manager-factory.js +50 -0
  183. package/dist/esm/naylence/fame/stickiness/aft-replica-stickiness-manager-factory.js.map +1 -0
  184. package/dist/esm/naylence/fame/stickiness/aft-replica-stickiness-manager.js +203 -0
  185. package/dist/esm/naylence/fame/stickiness/aft-replica-stickiness-manager.js.map +1 -0
  186. package/dist/esm/naylence/fame/stickiness/aft-signer.js +147 -0
  187. package/dist/esm/naylence/fame/stickiness/aft-signer.js.map +1 -0
  188. package/dist/esm/naylence/fame/stickiness/aft-utils.js +90 -0
  189. package/dist/esm/naylence/fame/stickiness/aft-utils.js.map +1 -0
  190. package/dist/esm/naylence/fame/stickiness/aft-verifier.js +290 -0
  191. package/dist/esm/naylence/fame/stickiness/aft-verifier.js.map +1 -0
  192. package/dist/esm/naylence/fame/stickiness/index.js +11 -0
  193. package/dist/esm/naylence/fame/stickiness/index.js.map +1 -0
  194. package/dist/esm/naylence/fame/stickiness/stickiness-mode.js +24 -0
  195. package/dist/esm/naylence/fame/stickiness/stickiness-mode.js.map +1 -0
  196. package/dist/esm/naylence/fame/welcome/advanced-welcome-service-factory.js +93 -0
  197. package/dist/esm/naylence/fame/welcome/advanced-welcome-service-factory.js.map +1 -0
  198. package/dist/esm/naylence/fame/welcome/advanced-welcome-service.js +212 -0
  199. package/dist/esm/naylence/fame/welcome/advanced-welcome-service.js.map +1 -0
  200. package/dist/esm/naylence/fame/welcome/index.js +3 -0
  201. package/dist/esm/naylence/fame/welcome/index.js.map +1 -0
  202. package/dist/esm/plugin.js +37 -0
  203. package/dist/esm/plugin.js.map +1 -0
  204. package/dist/types/browser.d.ts +2 -0
  205. package/dist/types/browser.d.ts.map +1 -0
  206. package/dist/types/index.d.ts +4 -0
  207. package/dist/types/index.d.ts.map +1 -0
  208. package/dist/types/naylence/fame/factory-manifest.d.ts +9 -0
  209. package/dist/types/naylence/fame/factory-manifest.d.ts.map +1 -0
  210. package/dist/types/naylence/fame/security/cert/ca-server.d.ts +19 -0
  211. package/dist/types/naylence/fame/security/cert/ca-server.d.ts.map +1 -0
  212. package/dist/types/naylence/fame/security/cert/ca-service-client.d.ts +75 -0
  213. package/dist/types/naylence/fame/security/cert/ca-service-client.d.ts.map +1 -0
  214. package/dist/types/naylence/fame/security/cert/ca-service-factory.d.ts +43 -0
  215. package/dist/types/naylence/fame/security/cert/ca-service-factory.d.ts.map +1 -0
  216. package/dist/types/naylence/fame/security/cert/ca-types.d.ts +97 -0
  217. package/dist/types/naylence/fame/security/cert/ca-types.d.ts.map +1 -0
  218. package/dist/types/naylence/fame/security/cert/default-ca-service-factory.d.ts +55 -0
  219. package/dist/types/naylence/fame/security/cert/default-ca-service-factory.d.ts.map +1 -0
  220. package/dist/types/naylence/fame/security/cert/default-ca-service.d.ts +84 -0
  221. package/dist/types/naylence/fame/security/cert/default-ca-service.d.ts.map +1 -0
  222. package/dist/types/naylence/fame/security/cert/default-certificate-manager-factory.d.ts +25 -0
  223. package/dist/types/naylence/fame/security/cert/default-certificate-manager-factory.d.ts.map +1 -0
  224. package/dist/types/naylence/fame/security/cert/default-certificate-manager.d.ts +38 -0
  225. package/dist/types/naylence/fame/security/cert/default-certificate-manager.d.ts.map +1 -0
  226. package/dist/types/naylence/fame/security/cert/grants.d.ts +2 -0
  227. package/dist/types/naylence/fame/security/cert/grants.d.ts.map +1 -0
  228. package/dist/types/naylence/fame/security/cert/index.d.ts +11 -0
  229. package/dist/types/naylence/fame/security/cert/index.d.ts.map +1 -0
  230. package/dist/types/naylence/fame/security/cert/internal-ca-service.d.ts +132 -0
  231. package/dist/types/naylence/fame/security/cert/internal-ca-service.d.ts.map +1 -0
  232. package/dist/types/naylence/fame/security/cert/util.d.ts +35 -0
  233. package/dist/types/naylence/fame/security/cert/util.d.ts.map +1 -0
  234. package/dist/types/naylence/fame/security/encryption/channel/channel-encryption-manager-factory.d.ts +29 -0
  235. package/dist/types/naylence/fame/security/encryption/channel/channel-encryption-manager-factory.d.ts.map +1 -0
  236. package/dist/types/naylence/fame/security/encryption/channel/channel-encryption-manager.d.ts +51 -0
  237. package/dist/types/naylence/fame/security/encryption/channel/channel-encryption-manager.d.ts.map +1 -0
  238. package/dist/types/naylence/fame/security/encryption/channel/index.d.ts +3 -0
  239. package/dist/types/naylence/fame/security/encryption/channel/index.d.ts.map +1 -0
  240. package/dist/types/naylence/fame/security/encryption/composite-encryption-manager-factory.d.ts +32 -0
  241. package/dist/types/naylence/fame/security/encryption/composite-encryption-manager-factory.d.ts.map +1 -0
  242. package/dist/types/naylence/fame/security/encryption/composite-encryption-manager.d.ts +52 -0
  243. package/dist/types/naylence/fame/security/encryption/composite-encryption-manager.d.ts.map +1 -0
  244. package/dist/types/naylence/fame/security/encryption/default-secure-channel-manager-factory.d.ts +23 -0
  245. package/dist/types/naylence/fame/security/encryption/default-secure-channel-manager-factory.d.ts.map +1 -0
  246. package/dist/types/naylence/fame/security/encryption/default-secure-channel-manager.d.ts +37 -0
  247. package/dist/types/naylence/fame/security/encryption/default-secure-channel-manager.d.ts.map +1 -0
  248. package/dist/types/naylence/fame/security/encryption/encryption-manager-registry.d.ts +34 -0
  249. package/dist/types/naylence/fame/security/encryption/encryption-manager-registry.d.ts.map +1 -0
  250. package/dist/types/naylence/fame/security/encryption/index.d.ts +7 -0
  251. package/dist/types/naylence/fame/security/encryption/index.d.ts.map +1 -0
  252. package/dist/types/naylence/fame/security/encryption/sealed/index.d.ts +3 -0
  253. package/dist/types/naylence/fame/security/encryption/sealed/index.d.ts.map +1 -0
  254. package/dist/types/naylence/fame/security/encryption/sealed/x25519-encryption-manager-factory.d.ts +28 -0
  255. package/dist/types/naylence/fame/security/encryption/sealed/x25519-encryption-manager-factory.d.ts.map +1 -0
  256. package/dist/types/naylence/fame/security/encryption/sealed/x25519-encryption-manager.d.ts +43 -0
  257. package/dist/types/naylence/fame/security/encryption/sealed/x25519-encryption-manager.d.ts.map +1 -0
  258. package/dist/types/naylence/fame/security/index.d.ts +6 -0
  259. package/dist/types/naylence/fame/security/index.d.ts.map +1 -0
  260. package/dist/types/naylence/fame/security/keys/index.d.ts +3 -0
  261. package/dist/types/naylence/fame/security/keys/index.d.ts.map +1 -0
  262. package/dist/types/naylence/fame/security/keys/x5c-key-manager-factory.d.ts +19 -0
  263. package/dist/types/naylence/fame/security/keys/x5c-key-manager-factory.d.ts.map +1 -0
  264. package/dist/types/naylence/fame/security/keys/x5c-key-manager.d.ts +39 -0
  265. package/dist/types/naylence/fame/security/keys/x5c-key-manager.d.ts.map +1 -0
  266. package/dist/types/naylence/fame/security/register-advanced-security-factories.d.ts +8 -0
  267. package/dist/types/naylence/fame/security/register-advanced-security-factories.d.ts.map +1 -0
  268. package/dist/types/naylence/fame/security/signing/eddsa-envelope-signer-factory.d.ts +20 -0
  269. package/dist/types/naylence/fame/security/signing/eddsa-envelope-signer-factory.d.ts.map +1 -0
  270. package/dist/types/naylence/fame/security/signing/eddsa-envelope-verifier-factory.d.ts +21 -0
  271. package/dist/types/naylence/fame/security/signing/eddsa-envelope-verifier-factory.d.ts.map +1 -0
  272. package/dist/types/naylence/fame/security/signing/eddsa-envelope-verifier.d.ts +17 -0
  273. package/dist/types/naylence/fame/security/signing/eddsa-envelope-verifier.d.ts.map +1 -0
  274. package/dist/types/naylence/fame/stickiness/aft-helper.d.ts +29 -0
  275. package/dist/types/naylence/fame/stickiness/aft-helper.d.ts.map +1 -0
  276. package/dist/types/naylence/fame/stickiness/aft-load-balancer-stickiness-manager-factory.d.ts +25 -0
  277. package/dist/types/naylence/fame/stickiness/aft-load-balancer-stickiness-manager-factory.d.ts.map +1 -0
  278. package/dist/types/naylence/fame/stickiness/aft-load-balancer-stickiness-manager.d.ts +33 -0
  279. package/dist/types/naylence/fame/stickiness/aft-load-balancer-stickiness-manager.d.ts.map +1 -0
  280. package/dist/types/naylence/fame/stickiness/aft-model.d.ts +33 -0
  281. package/dist/types/naylence/fame/stickiness/aft-model.d.ts.map +1 -0
  282. package/dist/types/naylence/fame/stickiness/aft-replica-stickiness-manager-factory.d.ts +23 -0
  283. package/dist/types/naylence/fame/stickiness/aft-replica-stickiness-manager-factory.d.ts.map +1 -0
  284. package/dist/types/naylence/fame/stickiness/aft-replica-stickiness-manager.d.ts +31 -0
  285. package/dist/types/naylence/fame/stickiness/aft-replica-stickiness-manager.d.ts.map +1 -0
  286. package/dist/types/naylence/fame/stickiness/aft-signer.d.ts +55 -0
  287. package/dist/types/naylence/fame/stickiness/aft-signer.d.ts.map +1 -0
  288. package/dist/types/naylence/fame/stickiness/aft-utils.d.ts +4 -0
  289. package/dist/types/naylence/fame/stickiness/aft-utils.d.ts.map +1 -0
  290. package/dist/types/naylence/fame/stickiness/aft-verifier.d.ts +50 -0
  291. package/dist/types/naylence/fame/stickiness/aft-verifier.d.ts.map +1 -0
  292. package/dist/types/naylence/fame/stickiness/index.d.ts +15 -0
  293. package/dist/types/naylence/fame/stickiness/index.d.ts.map +1 -0
  294. package/dist/types/naylence/fame/stickiness/stickiness-mode.d.ts +7 -0
  295. package/dist/types/naylence/fame/stickiness/stickiness-mode.d.ts.map +1 -0
  296. package/dist/types/naylence/fame/welcome/advanced-welcome-service-factory.d.ts +21 -0
  297. package/dist/types/naylence/fame/welcome/advanced-welcome-service-factory.d.ts.map +1 -0
  298. package/dist/types/naylence/fame/welcome/advanced-welcome-service.d.ts +21 -0
  299. package/dist/types/naylence/fame/welcome/advanced-welcome-service.d.ts.map +1 -0
  300. package/dist/types/naylence/fame/welcome/index.d.ts +3 -0
  301. package/dist/types/naylence/fame/welcome/index.d.ts.map +1 -0
  302. package/dist/types/plugin.d.ts +11 -0
  303. package/dist/types/plugin.d.ts.map +1 -0
  304. package/package.json +130 -0
@@ -0,0 +1,732 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ChannelEncryptionManager = void 0;
4
+ const chacha_js_1 = require("@noble/ciphers/chacha.js");
5
+ const core_1 = require("@naylence/core");
6
+ const runtime_1 = require("@naylence/runtime");
7
+ const runtime_2 = require("@naylence/runtime");
8
+ const runtime_3 = require("@naylence/runtime");
9
+ const core_2 = require("@naylence/core");
10
+ const runtime_4 = require("@naylence/runtime");
11
+ const logger = (0, runtime_3.getLogger)("naylence.fame.security.encryption.channel.channel_encryption_manager");
12
+ const SUPPORTED_CHANNEL_ALGORITHMS = ["chacha20-poly1305-channel"];
13
+ const CHANNEL_ENCRYPTION_ALGORITHM = "chacha20-poly1305-channel";
14
+ const HANDSHAKE_ALGORITHM = "CHACHA20P1305";
15
+ const SYSTEM_INBOX = "__sys__";
16
+ const NONCE_LENGTH = 12;
17
+ function isTaskSpawnerLike(value) {
18
+ return Boolean(value && typeof value.spawn === "function");
19
+ }
20
+ function toUint8Array(value) {
21
+ if (value instanceof Uint8Array) {
22
+ return value;
23
+ }
24
+ if (typeof ArrayBuffer !== "undefined") {
25
+ if (value instanceof ArrayBuffer) {
26
+ return new Uint8Array(value);
27
+ }
28
+ if (ArrayBuffer.isView(value)) {
29
+ const view = value;
30
+ return new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
31
+ }
32
+ }
33
+ if (Array.isArray(value)) {
34
+ return Uint8Array.from(value);
35
+ }
36
+ return null;
37
+ }
38
+ function encodeBase64(data) {
39
+ if (typeof Buffer !== "undefined") {
40
+ return Buffer.from(data).toString("base64");
41
+ }
42
+ let binary = "";
43
+ for (const byte of data) {
44
+ binary += String.fromCharCode(byte);
45
+ }
46
+ if (typeof btoa === "function") {
47
+ return btoa(binary);
48
+ }
49
+ throw new Error("Base64 encoding not supported in this environment");
50
+ }
51
+ function decodeBase64(encoded) {
52
+ if (typeof Buffer !== "undefined") {
53
+ return Uint8Array.from(Buffer.from(encoded, "base64"));
54
+ }
55
+ if (typeof atob === "function") {
56
+ const binary = atob(encoded);
57
+ const bytes = new Uint8Array(binary.length);
58
+ for (let i = 0; i < binary.length; i += 1) {
59
+ bytes[i] = binary.charCodeAt(i);
60
+ }
61
+ return bytes;
62
+ }
63
+ throw new Error("Base64 decoding not supported in this environment");
64
+ }
65
+ function randomBytes(length) {
66
+ if (typeof crypto !== "undefined" &&
67
+ typeof crypto.getRandomValues === "function") {
68
+ const buffer = new Uint8Array(length);
69
+ crypto.getRandomValues(buffer);
70
+ return buffer;
71
+ }
72
+ try {
73
+ const { randomBytes: nodeRandomBytes } = require("node:crypto");
74
+ return nodeRandomBytes(length);
75
+ }
76
+ catch {
77
+ throw new Error("Crypto random bytes are not available in this environment");
78
+ }
79
+ }
80
+ function toFameAddress(value) {
81
+ if (!value) {
82
+ return null;
83
+ }
84
+ if (value instanceof core_1.FameAddress) {
85
+ return value;
86
+ }
87
+ return new core_1.FameAddress(String(value));
88
+ }
89
+ function toDestinationString(value) {
90
+ if (!value) {
91
+ return null;
92
+ }
93
+ if (value instanceof core_1.FameAddress) {
94
+ return value.toString();
95
+ }
96
+ if (typeof value === "string") {
97
+ return value;
98
+ }
99
+ return null;
100
+ }
101
+ function makeJsonSerializable(value) {
102
+ if (value === null || value === undefined) {
103
+ return null;
104
+ }
105
+ if (Array.isArray(value)) {
106
+ return value.map((item) => makeJsonSerializable(item));
107
+ }
108
+ if (value instanceof Uint8Array) {
109
+ return Array.from(value);
110
+ }
111
+ if (typeof value === "object") {
112
+ const candidate = value;
113
+ if (typeof candidate.toJSON === "function") {
114
+ return candidate.toJSON();
115
+ }
116
+ if (typeof candidate.model_dump === "function") {
117
+ return candidate.model_dump();
118
+ }
119
+ if (typeof candidate.dict === "function") {
120
+ return candidate.dict();
121
+ }
122
+ return { ...candidate };
123
+ }
124
+ return value;
125
+ }
126
+ class ChannelEncryptionManager {
127
+ constructor({ secureChannelManager = null, nodeLike = null, taskSpawner = null, } = {}) {
128
+ this.pendingEnvelopes = new Map();
129
+ this.handshakeInProgress = new Set();
130
+ this.addrChannelMap = new Map();
131
+ this.secureChannelManager = secureChannelManager ?? null;
132
+ this.nodeLike = nodeLike ?? null;
133
+ this.taskSpawner =
134
+ taskSpawner ?? (isTaskSpawnerLike(nodeLike) ? nodeLike : taskSpawner);
135
+ }
136
+ async encryptEnvelope(envelope, opts = null) {
137
+ const frame = envelope.frame;
138
+ if (!this.isDataFrame(frame)) {
139
+ return runtime_1.EncryptionResult.skipped(envelope);
140
+ }
141
+ if (frame.payload === null || typeof frame.payload === "undefined") {
142
+ return runtime_1.EncryptionResult.skipped(envelope);
143
+ }
144
+ const destination = opts?.destination ?? envelope.to ?? null;
145
+ const destinationStr = toDestinationString(destination);
146
+ if (!destinationStr) {
147
+ logger.warning("no_destination_for_channel_encryption", {
148
+ envelope_id: envelope.id,
149
+ });
150
+ return runtime_1.EncryptionResult.skipped(envelope);
151
+ }
152
+ if (!this.secureChannelManager) {
153
+ logger.warning("no_secure_channel_manager_available", {
154
+ envelope_id: envelope.id,
155
+ });
156
+ return runtime_1.EncryptionResult.skipped(envelope);
157
+ }
158
+ const existingChannelId = this.findExistingChannel(destinationStr);
159
+ if (existingChannelId) {
160
+ try {
161
+ return this.encryptWithChannel(envelope, existingChannelId);
162
+ }
163
+ catch (error) {
164
+ logger.error("channel_encryption_failed", {
165
+ error: error instanceof Error ? error.message : String(error),
166
+ channel_id: existingChannelId,
167
+ });
168
+ return runtime_1.EncryptionResult.skipped(envelope);
169
+ }
170
+ }
171
+ await this.queueAndInitiateHandshake(envelope, destination, destinationStr, opts ?? null);
172
+ return runtime_1.EncryptionResult.queued();
173
+ }
174
+ async decryptEnvelope(envelope, opts = null) {
175
+ void opts;
176
+ (0, runtime_2.requireCryptoSupport)();
177
+ const frame = envelope.frame;
178
+ if (!this.isDataFrame(frame) ||
179
+ frame.payload === null ||
180
+ typeof frame.payload === "undefined") {
181
+ return envelope;
182
+ }
183
+ if (!envelope.sec || !envelope.sec.enc) {
184
+ return envelope;
185
+ }
186
+ const encHeader = envelope.sec.enc;
187
+ if (!encHeader.alg || !this.isChannelAlgorithm(encHeader.alg)) {
188
+ return envelope;
189
+ }
190
+ const channelId = encHeader.kid;
191
+ if (!channelId) {
192
+ logger.error("missing_channel_id_in_encryption_header", {
193
+ envelope_id: envelope.id,
194
+ });
195
+ return envelope;
196
+ }
197
+ const nonce = this.decodeNonceValue(encHeader.val ?? "");
198
+ if (!nonce) {
199
+ logger.error("invalid_nonce_in_encryption_header", {
200
+ envelope_id: envelope.id,
201
+ value_present: Boolean(encHeader.val),
202
+ });
203
+ return envelope;
204
+ }
205
+ if (!this.secureChannelManager) {
206
+ logger.warning("no_secure_channel_manager_for_decryption", {
207
+ envelope_id: envelope.id,
208
+ });
209
+ return envelope;
210
+ }
211
+ const channelState = this.getChannelState(channelId);
212
+ if (!channelState) {
213
+ logger.error("channel_not_available_for_decryption", {
214
+ channel_id: channelId,
215
+ });
216
+ return envelope;
217
+ }
218
+ const ciphertext = this.extractCiphertext(frame.payload);
219
+ if (!ciphertext) {
220
+ logger.error("invalid_ciphertext_payload", { envelope_id: envelope.id });
221
+ return envelope;
222
+ }
223
+ try {
224
+ const aad = new TextEncoder().encode(channelId);
225
+ const aead = (0, chacha_js_1.chacha20poly1305)(channelState.key, nonce, aad);
226
+ const plaintextBytes = aead.decrypt(ciphertext);
227
+ const decodedPayload = this.deserializePayload(plaintextBytes);
228
+ frame.payload = decodedPayload;
229
+ frame.codec = "json";
230
+ if (envelope.sec) {
231
+ delete envelope.sec.enc;
232
+ if (!envelope.sec.sig) {
233
+ envelope.sec = undefined;
234
+ }
235
+ }
236
+ if (envelope.replyTo) {
237
+ this.addrChannelMap.set(String(envelope.replyTo), channelId);
238
+ }
239
+ if (envelope.sid) {
240
+ this.addrChannelMap.set(envelope.sid, channelId);
241
+ }
242
+ return envelope;
243
+ }
244
+ catch (error) {
245
+ logger.error("channel_decryption_failed", {
246
+ channel_id: channelId,
247
+ error: error instanceof Error ? error.message : String(error),
248
+ });
249
+ return envelope;
250
+ }
251
+ }
252
+ async notifyChannelEstablished(channelId) {
253
+ logger.debug("channel_encryption_manager_notified", {
254
+ channel_id: channelId,
255
+ manager_type: "channel",
256
+ });
257
+ if (!channelId.startsWith("auto-")) {
258
+ logger.warning("unexpected_channel_id_format", { channel_id: channelId });
259
+ return;
260
+ }
261
+ const destinationStr = this.extractDestinationFromChannelId(channelId);
262
+ if (!destinationStr) {
263
+ logger.warning("cannot_parse_destination_from_channel_id", {
264
+ channel_id: channelId,
265
+ });
266
+ return;
267
+ }
268
+ this.handshakeInProgress.delete(destinationStr);
269
+ if (!this.pendingEnvelopes.has(destinationStr)) {
270
+ logger.debug("no_pending_queue_for_destination", {
271
+ destination: destinationStr,
272
+ });
273
+ return;
274
+ }
275
+ const queuedEnvelopes = this.pendingEnvelopes.get(destinationStr) ?? [];
276
+ this.pendingEnvelopes.delete(destinationStr);
277
+ if (!this.secureChannelManager) {
278
+ logger.error("no_secure_channel_manager_for_queue_drain", {
279
+ channel_id: channelId,
280
+ });
281
+ return;
282
+ }
283
+ for (const envelope of queuedEnvelopes) {
284
+ try {
285
+ const result = this.encryptWithChannel(envelope, channelId);
286
+ if (!result.envelope) {
287
+ logger.warning("failed_to_encrypt_queued_envelope", {
288
+ envelope_id: envelope.id,
289
+ channel_id: channelId,
290
+ });
291
+ continue;
292
+ }
293
+ const encryptedEnvelope = result.envelope;
294
+ this.runAsyncTask(() => this.deliverEnvelope(encryptedEnvelope), `deliver-queued-${envelope.id}`);
295
+ }
296
+ catch (error) {
297
+ logger.error("failed_to_encrypt_queued_envelope", {
298
+ envelope_id: envelope.id,
299
+ error: error instanceof Error ? error.message : String(error),
300
+ });
301
+ }
302
+ }
303
+ }
304
+ async notifyChannelFailed(channelId, reason = "handshake_failed") {
305
+ logger.debug("channel_encryption_manager_notified_failure", {
306
+ channel_id: channelId,
307
+ reason,
308
+ });
309
+ if (!channelId.startsWith("auto-")) {
310
+ logger.warning("unexpected_channel_id_format_on_failure", {
311
+ channel_id: channelId,
312
+ });
313
+ return;
314
+ }
315
+ const destinationStr = this.extractDestinationFromChannelId(channelId);
316
+ if (!destinationStr) {
317
+ logger.warning("cannot_parse_destination_from_channel_id_on_failure", {
318
+ channel_id: channelId,
319
+ });
320
+ return;
321
+ }
322
+ this.handshakeInProgress.delete(destinationStr);
323
+ // Clear any cached channel mapping for this destination since the channel failed
324
+ const cachedChannelId = this.addrChannelMap.get(destinationStr);
325
+ if (cachedChannelId === channelId) {
326
+ this.addrChannelMap.delete(destinationStr);
327
+ logger.debug("cleared_channel_cache_for_failed_channel", {
328
+ destination: destinationStr,
329
+ channel_id: channelId,
330
+ });
331
+ }
332
+ const queuedEnvelopes = this.pendingEnvelopes.get(destinationStr);
333
+ if (!queuedEnvelopes || queuedEnvelopes.length === 0) {
334
+ logger.debug("no_pending_queue_for_failed_destination", {
335
+ destination: destinationStr,
336
+ });
337
+ return;
338
+ }
339
+ this.pendingEnvelopes.delete(destinationStr);
340
+ for (const envelope of queuedEnvelopes) {
341
+ await this.handleFailedEnvelope(envelope, destinationStr, channelId, reason);
342
+ }
343
+ }
344
+ /**
345
+ * Clear cached channel mappings for a destination.
346
+ * This should be called when routes are removed or channels are closed
347
+ * to prevent using stale channel references.
348
+ */
349
+ clearChannelCacheForDestination(destination) {
350
+ const cached = this.addrChannelMap.get(destination);
351
+ if (cached) {
352
+ this.addrChannelMap.delete(destination);
353
+ logger.debug("cleared_channel_cache_for_destination", {
354
+ destination,
355
+ cached_channel_id: cached,
356
+ });
357
+ }
358
+ }
359
+ isChannelAlgorithm(algorithm) {
360
+ return SUPPORTED_CHANNEL_ALGORITHMS.includes(algorithm);
361
+ }
362
+ isDataFrame(frame) {
363
+ return Boolean(frame && frame.type === "Data");
364
+ }
365
+ findExistingChannel(destination) {
366
+ if (!this.secureChannelManager) {
367
+ return null;
368
+ }
369
+ const cached = this.addrChannelMap.get(destination);
370
+ if (cached && this.getChannelState(cached)) {
371
+ logger.debug("using_cached_channel", { destination, channel_id: cached });
372
+ return cached;
373
+ }
374
+ const channels = this.secureChannelManager.channels;
375
+ for (const channelId of Object.keys(channels)) {
376
+ if (channelId.startsWith(`auto-${destination}-`)) {
377
+ this.addrChannelMap.set(destination, channelId);
378
+ logger.debug("using_existing_channel", {
379
+ destination,
380
+ channel_id: channelId,
381
+ });
382
+ return channelId;
383
+ }
384
+ }
385
+ return null;
386
+ }
387
+ async queueAndInitiateHandshake(envelope, destination, destinationStr, opts) {
388
+ const queue = this.pendingEnvelopes.get(destinationStr) ?? [];
389
+ queue.push(envelope);
390
+ this.pendingEnvelopes.set(destinationStr, queue);
391
+ logger.debug("queued_envelope_for_channel_handshake", {
392
+ envelope_id: envelope.id,
393
+ destination: destinationStr,
394
+ });
395
+ if (this.handshakeInProgress.has(destinationStr)) {
396
+ logger.debug("handshake_already_in_progress", {
397
+ destination: destinationStr,
398
+ });
399
+ return;
400
+ }
401
+ this.handshakeInProgress.add(destinationStr);
402
+ const taskName = `handshake-${destinationStr}`;
403
+ this.runAsyncTask(async () => {
404
+ try {
405
+ await this.initiateChannelHandshakeAsync(destination ?? destinationStr, destinationStr, opts);
406
+ }
407
+ finally {
408
+ this.handshakeInProgress.delete(destinationStr);
409
+ }
410
+ }, taskName);
411
+ }
412
+ async initiateChannelHandshakeAsync(destination, destinationStr, opts) {
413
+ void opts;
414
+ if (!this.secureChannelManager) {
415
+ logger.error("no_secure_channel_manager_for_async_handshake_initiation");
416
+ return;
417
+ }
418
+ const channelId = this.generateChannelId(destinationStr);
419
+ try {
420
+ const openFrame = this.secureChannelManager.generateOpenFrame(channelId, HANDSHAKE_ALGORITHM);
421
+ const success = await this.sendSecureOpenFrameAsync(openFrame, destination);
422
+ if (success) {
423
+ logger.debug("sent_secure_open_frame_async", {
424
+ channel_id: channelId,
425
+ destination: destinationStr,
426
+ });
427
+ }
428
+ else {
429
+ logger.warning("failed_to_send_secure_open_frame_async", {
430
+ channel_id: channelId,
431
+ });
432
+ }
433
+ }
434
+ catch (error) {
435
+ logger.error("async_channel_handshake_initiation_failed", {
436
+ destination: destinationStr,
437
+ error: error instanceof Error ? error.message : String(error),
438
+ });
439
+ }
440
+ }
441
+ async sendSecureOpenFrameAsync(openFrame, destination) {
442
+ const node = this.nodeLike;
443
+ if (!node) {
444
+ logger.error("no_node_available_for_sending_secure_open_async");
445
+ return false;
446
+ }
447
+ const envelopeFactory = node.envelopeFactory;
448
+ if (!envelopeFactory) {
449
+ logger.error("no_envelope_factory_available_for_secure_open_async");
450
+ return false;
451
+ }
452
+ const replyTo = this.buildSystemReplyTo();
453
+ if (!replyTo) {
454
+ logger.error("no_physical_path_available_for_reply_to_async");
455
+ return false;
456
+ }
457
+ const toAddress = toFameAddress(destination);
458
+ if (!toAddress) {
459
+ logger.error("invalid_destination_for_secure_open", {
460
+ destination: String(destination),
461
+ });
462
+ return false;
463
+ }
464
+ const envelope = envelopeFactory.createEnvelope({
465
+ to: toAddress,
466
+ frame: openFrame,
467
+ replyTo,
468
+ corrId: (0, core_2.generateId)(),
469
+ });
470
+ await this.deliverEnvelope(envelope);
471
+ logger.debug("delivered_secure_open_frame_async", {
472
+ channel_id: openFrame.cid,
473
+ });
474
+ return true;
475
+ }
476
+ async deliverEnvelope(envelope) {
477
+ const node = this.nodeLike;
478
+ if (!node) {
479
+ logger.error("no_node_available_for_delivery", {
480
+ envelope_id: envelope.id,
481
+ });
482
+ return;
483
+ }
484
+ const context = (0, core_1.localDeliveryContext)(node.sid ?? undefined);
485
+ await node.deliver(envelope, context);
486
+ }
487
+ encryptWithChannel(envelope, channelId) {
488
+ if (!this.secureChannelManager) {
489
+ logger.error("no_secure_channel_manager_for_encryption");
490
+ return runtime_1.EncryptionResult.skipped(envelope);
491
+ }
492
+ const frame = envelope.frame;
493
+ if (!this.isDataFrame(frame)) {
494
+ logger.error("attempted_to_encrypt_non_dataframe", {
495
+ frame_type: frame.type ?? typeof frame,
496
+ });
497
+ return runtime_1.EncryptionResult.skipped(envelope);
498
+ }
499
+ const channelState = this.getChannelState(channelId);
500
+ if (!channelState) {
501
+ logger.error("channel_not_in_channels", { channel_id: channelId });
502
+ return runtime_1.EncryptionResult.skipped(envelope);
503
+ }
504
+ const payloadBytes = this.serializePayload(frame.payload);
505
+ if (!payloadBytes) {
506
+ return runtime_1.EncryptionResult.skipped(envelope);
507
+ }
508
+ const nonce = randomBytes(NONCE_LENGTH);
509
+ const aad = new TextEncoder().encode(channelId);
510
+ const aead = (0, chacha_js_1.chacha20poly1305)(channelState.key, nonce, aad);
511
+ const ciphertext = aead.encrypt(payloadBytes);
512
+ const encryptionHeader = {
513
+ alg: CHANNEL_ENCRYPTION_ALGORITHM,
514
+ val: Array.from(nonce)
515
+ .map((byte) => byte.toString(16).padStart(2, "0"))
516
+ .join(""), // Hex encoding (Python reference)
517
+ kid: channelId,
518
+ };
519
+ const encodedCiphertext = encodeBase64(ciphertext);
520
+ frame.payload = encodedCiphertext;
521
+ frame.codec = "b64";
522
+ if (envelope.sec) {
523
+ envelope.sec.enc = encryptionHeader;
524
+ }
525
+ else {
526
+ envelope.sec = { enc: encryptionHeader };
527
+ }
528
+ return runtime_1.EncryptionResult.ok(envelope);
529
+ }
530
+ serializePayload(payload) {
531
+ if (payload === null || typeof payload === "undefined") {
532
+ return null;
533
+ }
534
+ if (payload instanceof Uint8Array) {
535
+ return payload;
536
+ }
537
+ if (typeof ArrayBuffer !== "undefined") {
538
+ if (payload instanceof ArrayBuffer || ArrayBuffer.isView(payload)) {
539
+ return toUint8Array(payload);
540
+ }
541
+ }
542
+ if (typeof payload === "string") {
543
+ return new TextEncoder().encode(payload);
544
+ }
545
+ if (typeof payload === "number" || typeof payload === "boolean") {
546
+ return new TextEncoder().encode(JSON.stringify(payload));
547
+ }
548
+ const serializable = makeJsonSerializable(payload);
549
+ return new TextEncoder().encode(JSON.stringify(serializable));
550
+ }
551
+ extractCiphertext(payload) {
552
+ if (payload instanceof Uint8Array) {
553
+ return payload;
554
+ }
555
+ if (typeof payload === "string") {
556
+ try {
557
+ return decodeBase64(payload);
558
+ }
559
+ catch (error) {
560
+ logger.error("failed_to_decode_base64_ciphertext", {
561
+ error: error instanceof Error ? error.message : String(error),
562
+ });
563
+ return null;
564
+ }
565
+ }
566
+ if (payload instanceof ArrayBuffer ||
567
+ ArrayBuffer.isView(payload)) {
568
+ return toUint8Array(payload);
569
+ }
570
+ return null;
571
+ }
572
+ deserializePayload(bytes) {
573
+ const decoder = new TextDecoder();
574
+ const decoded = decoder.decode(bytes);
575
+ try {
576
+ return JSON.parse(decoded);
577
+ }
578
+ catch {
579
+ return decoded;
580
+ }
581
+ }
582
+ extractDestinationFromChannelId(channelId) {
583
+ const parts = channelId.split("-");
584
+ if (parts.length < 3) {
585
+ return null;
586
+ }
587
+ return parts.slice(1, -1).join("-");
588
+ }
589
+ async handleFailedEnvelope(envelope, destinationStr, channelId, reason) {
590
+ logger.warning("envelope_failed_due_to_channel_handshake_failure", {
591
+ envelope_id: envelope.id,
592
+ destination: destinationStr,
593
+ channel_id: channelId,
594
+ reason,
595
+ });
596
+ const frame = envelope.frame;
597
+ if (!this.isDataFrame(frame)) {
598
+ logger.debug("skipping_nack_for_non_dataframe", {
599
+ envelope_id: envelope.id,
600
+ frame_type: frame.type ?? typeof frame,
601
+ });
602
+ return;
603
+ }
604
+ if (!envelope.replyTo) {
605
+ logger.debug("skipping_nack_no_reply_to", { envelope_id: envelope.id });
606
+ return;
607
+ }
608
+ await this.sendDeliveryNack(envelope, `channel_handshake_failed: ${reason}`);
609
+ }
610
+ async sendDeliveryNack(envelope, failureReason) {
611
+ const node = this.nodeLike;
612
+ if (!node) {
613
+ logger.error("no_node_available_for_sending_delivery_nack");
614
+ return;
615
+ }
616
+ const envelopeFactory = node.envelopeFactory;
617
+ if (!envelopeFactory) {
618
+ logger.error("no_envelope_factory_available_for_delivery_nack");
619
+ return;
620
+ }
621
+ const replyTo = toFameAddress(envelope.replyTo ?? null);
622
+ if (!replyTo) {
623
+ logger.error("invalid_reply_to_for_delivery_nack", {
624
+ reply_to: envelope.replyTo,
625
+ });
626
+ return;
627
+ }
628
+ const nackFrame = {
629
+ type: "DeliveryAck",
630
+ ok: false,
631
+ code: "channel_handshake_failed",
632
+ reason: failureReason,
633
+ };
634
+ const nackEnvelope = envelopeFactory.createEnvelope({
635
+ to: replyTo,
636
+ frame: nackFrame,
637
+ corrId: envelope.corrId ?? (0, core_2.generateId)(),
638
+ });
639
+ await this.deliverEnvelope(nackEnvelope);
640
+ logger.debug("delivered_delivery_nack", {
641
+ original_envelope_id: envelope.id,
642
+ nack_envelope_id: nackEnvelope.id,
643
+ });
644
+ }
645
+ getChannelState(channelId) {
646
+ if (!this.secureChannelManager) {
647
+ return null;
648
+ }
649
+ const channelState = this.secureChannelManager.channels[channelId];
650
+ return channelState ?? null;
651
+ }
652
+ buildSystemReplyTo() {
653
+ const node = this.nodeLike;
654
+ if (!node) {
655
+ return null;
656
+ }
657
+ const physicalPath = node.physicalPath ?? "";
658
+ if (!physicalPath) {
659
+ return null;
660
+ }
661
+ return (0, core_1.formatAddress)(SYSTEM_INBOX, physicalPath);
662
+ }
663
+ generateChannelId(destinationStr) {
664
+ return `auto-${destinationStr}-${(0, core_2.generateId)()}`;
665
+ }
666
+ runAsyncTask(task, name) {
667
+ if (this.taskSpawner) {
668
+ this.taskSpawner.spawn(async () => {
669
+ await task();
670
+ }, { name });
671
+ return;
672
+ }
673
+ (async () => {
674
+ try {
675
+ await task();
676
+ }
677
+ catch (error) {
678
+ logger.error("async_task_failed", {
679
+ task_name: name,
680
+ error: error instanceof Error ? error.message : String(error),
681
+ });
682
+ }
683
+ })().catch(() => {
684
+ // Swallow to avoid unhandled rejection; error already logged above.
685
+ });
686
+ }
687
+ decodeNonceValue(value) {
688
+ if (!value) {
689
+ return null;
690
+ }
691
+ const hexCandidate = value.trim();
692
+ if (hexCandidate.length % 2 === 0 && /^[0-9a-fA-F]+$/.test(hexCandidate)) {
693
+ const bytes = new Uint8Array(hexCandidate.length / 2);
694
+ for (let i = 0; i < hexCandidate.length; i += 2) {
695
+ bytes[i / 2] = parseInt(hexCandidate.slice(i, i + 2), 16);
696
+ }
697
+ if (bytes.length > 0) {
698
+ return bytes;
699
+ }
700
+ }
701
+ const base64Candidates = [
702
+ value,
703
+ value.replace(/-/g, "+").replace(/_/g, "/"),
704
+ ];
705
+ for (const candidate of base64Candidates) {
706
+ try {
707
+ // Standard base64 decode first (Buffer), then urlsafe fallback
708
+ if (typeof Buffer !== "undefined") {
709
+ const decoded = Uint8Array.from(Buffer.from(candidate, "base64"));
710
+ if (decoded.length > 0) {
711
+ return decoded;
712
+ }
713
+ }
714
+ }
715
+ catch {
716
+ // ignore and try next
717
+ }
718
+ try {
719
+ const decoded = (0, runtime_4.urlsafeBase64Decode)(candidate);
720
+ if (decoded.length > 0) {
721
+ return decoded;
722
+ }
723
+ }
724
+ catch {
725
+ // ignore and continue
726
+ }
727
+ }
728
+ return null;
729
+ }
730
+ }
731
+ exports.ChannelEncryptionManager = ChannelEncryptionManager;
732
+ //# sourceMappingURL=channel-encryption-manager.js.map