@private.me/xbind 1.3.5 → 3.0.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 (306) hide show
  1. package/LICENSES.md +212 -0
  2. package/README.md +388 -6
  3. package/dist-standalone/_deps/mldsa-wasm/dist/mldsa.js +1 -1920
  4. package/dist-standalone/_deps/shared/cjs/errors.js +1 -639
  5. package/dist-standalone/_deps/shared/cjs/index.js +1 -496
  6. package/dist-standalone/_deps/shared/cjs/types.js +1 -317
  7. package/dist-standalone/_deps/shared/errors.js +1 -255
  8. package/dist-standalone/_deps/shared/index.js +1 -74
  9. package/dist-standalone/_deps/shared/types.js +1 -90
  10. package/dist-standalone/_deps/ux-helpers/cjs/errors.js +1 -1
  11. package/dist-standalone/_deps/ux-helpers/cjs/index.js +1 -1
  12. package/dist-standalone/_deps/ux-helpers/cjs/pagination.js +1 -1
  13. package/dist-standalone/_deps/ux-helpers/cjs/progress.js +1 -1
  14. package/dist-standalone/_deps/ux-helpers/cjs/search.js +1 -1
  15. package/dist-standalone/_deps/ux-helpers/cjs/types.js +1 -1
  16. package/dist-standalone/_deps/ux-helpers/errors.js +1 -1
  17. package/dist-standalone/_deps/ux-helpers/index.js +1 -1
  18. package/dist-standalone/_deps/ux-helpers/pagination.js +1 -1
  19. package/dist-standalone/_deps/ux-helpers/progress.js +1 -1
  20. package/dist-standalone/_deps/ux-helpers/search.js +1 -1
  21. package/dist-standalone/_deps/xchange/auto-accept.js +1 -1
  22. package/dist-standalone/_deps/xchange/cjs/auto-accept.js +1 -1
  23. package/dist-standalone/_deps/xchange/cjs/errors.js +1 -1
  24. package/dist-standalone/_deps/xchange/cjs/index.js +1 -1
  25. package/dist-standalone/_deps/xchange/cjs/invite-client.js +1 -1
  26. package/dist-standalone/_deps/xchange/cjs/lazy-init.js +1 -1
  27. package/dist-standalone/_deps/xchange/cjs/trust-integration.js +1 -1
  28. package/dist-standalone/_deps/xchange/cjs/xchange.js +1 -1
  29. package/dist-standalone/_deps/xchange/errors.js +1 -1
  30. package/dist-standalone/_deps/xchange/index.js +1 -1
  31. package/dist-standalone/_deps/xchange/invite-client.js +1 -1
  32. package/dist-standalone/_deps/xchange/lazy-init.js +1 -1
  33. package/dist-standalone/_deps/xchange/trust-integration.js +1 -1
  34. package/dist-standalone/_deps/xchange/xchange.js +1 -1
  35. package/dist-standalone/_deps/xregistry/cjs/discovery.js +1 -1
  36. package/dist-standalone/_deps/xregistry/cjs/errors.js +1 -1
  37. package/dist-standalone/_deps/xregistry/cjs/index.js +1 -1
  38. package/dist-standalone/_deps/xregistry/cjs/registry.js +1 -1
  39. package/dist-standalone/_deps/xregistry/cjs/schema.js +1 -1
  40. package/dist-standalone/_deps/xregistry/cjs/types.js +1 -1
  41. package/dist-standalone/_deps/xregistry/discovery.js +1 -1
  42. package/dist-standalone/_deps/xregistry/errors.js +1 -1
  43. package/dist-standalone/_deps/xregistry/index.js +1 -1
  44. package/dist-standalone/_deps/xregistry/registry.js +1 -1
  45. package/dist-standalone/_deps/xregistry/schema.js +1 -1
  46. package/dist-standalone/_deps/xregistry/types.js +1 -1
  47. package/dist-standalone/agent-call.js +1 -642
  48. package/dist-standalone/agent-sdk.js +1 -328
  49. package/dist-standalone/agent.d.ts +95 -5
  50. package/dist-standalone/agent.js +1 -1545
  51. package/dist-standalone/approval.js +1 -193
  52. package/dist-standalone/async-iterators.d.ts +275 -0
  53. package/dist-standalone/async-iterators.js +1 -0
  54. package/dist-standalone/auth.js +1 -219
  55. package/dist-standalone/auto-accept.js +1 -229
  56. package/dist-standalone/backup-config.js +1 -201
  57. package/dist-standalone/backup.d.ts +114 -0
  58. package/dist-standalone/backup.js +1 -0
  59. package/dist-standalone/batch-operations.d.ts +297 -0
  60. package/dist-standalone/batch-operations.js +1 -0
  61. package/dist-standalone/cancellation.d.ts +301 -0
  62. package/dist-standalone/cancellation.js +1 -0
  63. package/dist-standalone/checkpoint.js +1 -186
  64. package/dist-standalone/circuit-breaker.d.ts +351 -0
  65. package/dist-standalone/circuit-breaker.js +1 -0
  66. package/dist-standalone/cjs/agent-call.js +1 -651
  67. package/dist-standalone/cjs/agent-sdk.js +1 -332
  68. package/dist-standalone/cjs/agent.js +1 -1582
  69. package/dist-standalone/cjs/approval.js +1 -199
  70. package/dist-standalone/cjs/async-iterators.js +1 -0
  71. package/dist-standalone/cjs/auth.js +1 -225
  72. package/dist-standalone/cjs/auto-accept.js +1 -233
  73. package/dist-standalone/cjs/backup-config.js +1 -207
  74. package/dist-standalone/cjs/backup.js +1 -0
  75. package/dist-standalone/cjs/batch-operations.js +1 -0
  76. package/dist-standalone/cjs/cancellation.js +1 -0
  77. package/dist-standalone/cjs/checkpoint.js +1 -193
  78. package/dist-standalone/cjs/circuit-breaker.js +1 -0
  79. package/dist-standalone/cjs/cli/init.js +1 -486
  80. package/dist-standalone/cjs/config-validation.js +1 -0
  81. package/dist-standalone/cjs/connect.js +1 -312
  82. package/dist-standalone/cjs/connection-pool.js +1 -0
  83. package/dist-standalone/cjs/correlation-id.js +1 -339
  84. package/dist-standalone/cjs/crypto-utils.js +1 -0
  85. package/dist-standalone/cjs/debug-mode.js +1 -0
  86. package/dist-standalone/cjs/did-document.js +1 -101
  87. package/dist-standalone/cjs/did-privateme.js +1 -130
  88. package/dist-standalone/cjs/did-web.js +1 -201
  89. package/dist-standalone/cjs/discovery.js +1 -462
  90. package/dist-standalone/cjs/dual-mode.js +1 -251
  91. package/dist-standalone/cjs/email-templates.js +1 -313
  92. package/dist-standalone/cjs/email-transport.js +1 -239
  93. package/dist-standalone/cjs/envelope.js +1 -510
  94. package/dist-standalone/cjs/errors.js +1 -826
  95. package/dist-standalone/cjs/event-emitter.js +1 -0
  96. package/dist-standalone/cjs/gateway-state.js +1 -55
  97. package/dist-standalone/cjs/gateway-transport.js +1 -120
  98. package/dist-standalone/cjs/graceful-degradation.js +1 -0
  99. package/dist-standalone/cjs/guardrails.js +1 -223
  100. package/dist-standalone/cjs/health-check.js +1 -0
  101. package/dist-standalone/cjs/http-compat.js +1 -272
  102. package/dist-standalone/cjs/http-status-map.js +1 -571
  103. package/dist-standalone/cjs/identity.js +1 -540
  104. package/dist-standalone/cjs/index.js +1 -237
  105. package/dist-standalone/cjs/invitation.js +1 -421
  106. package/dist-standalone/cjs/invite.js +1 -328
  107. package/dist-standalone/cjs/key-agreement.js +1 -246
  108. package/dist-standalone/cjs/lazy-init.js +1 -300
  109. package/dist-standalone/cjs/logger.js +1 -0
  110. package/dist-standalone/cjs/mdns-discovery.js +1 -202
  111. package/dist-standalone/cjs/nonce-store.js +1 -66
  112. package/dist-standalone/cjs/pairing-manager.js +1 -223
  113. package/dist-standalone/cjs/plugin-system.js +1 -0
  114. package/dist-standalone/cjs/plugins/logging.js +1 -0
  115. package/dist-standalone/cjs/plugins/metrics.js +1 -0
  116. package/dist-standalone/cjs/plugins/validation.js +1 -0
  117. package/dist-standalone/cjs/policy.js +1 -320
  118. package/dist-standalone/cjs/progress-callbacks.js +1 -0
  119. package/dist-standalone/cjs/redis-nonce-store.js +1 -76
  120. package/dist-standalone/cjs/registry-middleware.js +1 -50
  121. package/dist-standalone/cjs/retry-strategies.js +1 -0
  122. package/dist-standalone/cjs/retry-transport.js +1 -102
  123. package/dist-standalone/cjs/runtime/browser.js +1 -0
  124. package/dist-standalone/cjs/runtime/edge.js +1 -0
  125. package/dist-standalone/cjs/runtime/react-native.js +1 -0
  126. package/dist-standalone/cjs/security-policy.js +1 -245
  127. package/dist-standalone/cjs/serialization.js +1 -0
  128. package/dist-standalone/cjs/split-channel.js +1 -177
  129. package/dist-standalone/cjs/subscription-proof.js +1 -230
  130. package/dist-standalone/cjs/succession.js +1 -148
  131. package/dist-standalone/cjs/timeouts.js +1 -0
  132. package/dist-standalone/cjs/trace-context.js +1 -0
  133. package/dist-standalone/cjs/trace-spans.js +1 -0
  134. package/dist-standalone/cjs/transport.js +1 -63
  135. package/dist-standalone/cjs/trust-registry.js +1 -742
  136. package/dist-standalone/cjs/types/error-response.js +1 -56
  137. package/dist-standalone/cjs/vault-auth.js +1 -0
  138. package/dist-standalone/cjs/vault-store-loader.js +1 -0
  139. package/dist-standalone/cjs/verify.js +1 -25
  140. package/dist-standalone/cjs/version-info.js +1 -0
  141. package/dist-standalone/cjs/xfetch.js +1 -252
  142. package/dist-standalone/cli/init.js +1 -449
  143. package/dist-standalone/cli/setup.js +1 -514
  144. package/dist-standalone/cli/types.js +1 -27
  145. package/dist-standalone/cli/xbind.js +1 -148
  146. package/dist-standalone/config-validation.d.ts +185 -0
  147. package/dist-standalone/config-validation.js +1 -0
  148. package/dist-standalone/connect.js +1 -274
  149. package/dist-standalone/connection-pool.d.ts +251 -0
  150. package/dist-standalone/connection-pool.js +1 -0
  151. package/dist-standalone/correlation-id.js +1 -326
  152. package/dist-standalone/crypto-utils.d.ts +60 -0
  153. package/dist-standalone/crypto-utils.js +1 -0
  154. package/dist-standalone/debug-mode.d.ts +286 -0
  155. package/dist-standalone/debug-mode.js +1 -0
  156. package/dist-standalone/did-document.js +1 -96
  157. package/dist-standalone/did-privateme.js +1 -121
  158. package/dist-standalone/did-web.js +1 -196
  159. package/dist-standalone/discovery.js +1 -458
  160. package/dist-standalone/dual-mode.js +1 -247
  161. package/dist-standalone/email-templates.js +1 -309
  162. package/dist-standalone/email-transport.js +1 -232
  163. package/dist-standalone/envelope.d.ts +29 -1
  164. package/dist-standalone/envelope.js +1 -497
  165. package/dist-standalone/errors.d.ts +10 -0
  166. package/dist-standalone/errors.js +1 -811
  167. package/dist-standalone/event-emitter.d.ts +395 -0
  168. package/dist-standalone/event-emitter.js +1 -0
  169. package/dist-standalone/gateway-state.js +1 -51
  170. package/dist-standalone/gateway-transport.js +1 -116
  171. package/dist-standalone/graceful-degradation.d.ts +246 -0
  172. package/dist-standalone/graceful-degradation.js +1 -0
  173. package/dist-standalone/guardrails.js +1 -216
  174. package/dist-standalone/health-check.d.ts +150 -0
  175. package/dist-standalone/health-check.js +1 -0
  176. package/dist-standalone/http-compat.js +1 -267
  177. package/dist-standalone/http-status-map.js +1 -561
  178. package/dist-standalone/identity.d.ts +64 -1
  179. package/dist-standalone/identity.js +1 -515
  180. package/dist-standalone/index.d.ts +45 -3
  181. package/dist-standalone/index.js +1 -52
  182. package/dist-standalone/invitation.js +1 -415
  183. package/dist-standalone/invite.js +1 -324
  184. package/dist-standalone/key-agreement.d.ts +61 -13
  185. package/dist-standalone/key-agreement.js +1 -236
  186. package/dist-standalone/lazy-init.js +1 -295
  187. package/dist-standalone/logger.d.ts +77 -0
  188. package/dist-standalone/logger.js +1 -0
  189. package/dist-standalone/mdns-discovery.js +1 -195
  190. package/dist-standalone/nonce-store.d.ts +16 -3
  191. package/dist-standalone/nonce-store.js +1 -62
  192. package/dist-standalone/package.json +0 -1
  193. package/dist-standalone/pairing-manager.js +1 -219
  194. package/dist-standalone/plugin-system.d.ts +145 -0
  195. package/dist-standalone/plugin-system.js +1 -0
  196. package/dist-standalone/policy.js +1 -315
  197. package/dist-standalone/progress-callbacks.d.ts +394 -0
  198. package/dist-standalone/progress-callbacks.js +1 -0
  199. package/dist-standalone/redis-nonce-store.js +1 -72
  200. package/dist-standalone/registry-middleware.js +1 -47
  201. package/dist-standalone/retry-strategies.d.ts +382 -0
  202. package/dist-standalone/retry-strategies.js +1 -0
  203. package/dist-standalone/retry-transport.js +1 -98
  204. package/dist-standalone/security-policy.js +1 -239
  205. package/dist-standalone/serialization.d.ts +244 -0
  206. package/dist-standalone/serialization.js +1 -0
  207. package/dist-standalone/split-channel.d.ts +49 -1
  208. package/dist-standalone/split-channel.js +1 -171
  209. package/dist-standalone/subscription-proof.js +1 -224
  210. package/dist-standalone/succession.js +1 -142
  211. package/dist-standalone/timeouts.d.ts +275 -0
  212. package/dist-standalone/timeouts.js +1 -0
  213. package/dist-standalone/trace-context.d.ts +252 -0
  214. package/dist-standalone/trace-context.js +1 -0
  215. package/dist-standalone/trace-spans.d.ts +360 -0
  216. package/dist-standalone/trace-spans.js +1 -0
  217. package/dist-standalone/transport.js +1 -59
  218. package/dist-standalone/trust-registry.d.ts +106 -5
  219. package/dist-standalone/trust-registry.js +1 -702
  220. package/dist-standalone/vault-auth.d.ts +91 -0
  221. package/dist-standalone/vault-auth.js +1 -0
  222. package/dist-standalone/vault-store-loader.d.ts +110 -0
  223. package/dist-standalone/vault-store-loader.js +1 -0
  224. package/dist-standalone/verify.js +1 -16
  225. package/dist-standalone/version-info.d.ts +259 -0
  226. package/dist-standalone/version-info.js +1 -0
  227. package/dist-standalone/xfetch.js +1 -247
  228. package/llms.txt +1 -0
  229. package/package.json +65 -5
  230. package/share1.dat +0 -0
  231. package/dist-standalone/_deps/crypto/base64.d.ts +0 -29
  232. package/dist-standalone/_deps/crypto/base64.js +0 -222
  233. package/dist-standalone/_deps/crypto/cjs/base64.js +0 -665
  234. package/dist-standalone/_deps/crypto/cjs/errors.js +0 -675
  235. package/dist-standalone/_deps/crypto/cjs/hmac.js +0 -473
  236. package/dist-standalone/_deps/crypto/cjs/index.js +0 -852
  237. package/dist-standalone/_deps/crypto/cjs/package.json +0 -1
  238. package/dist-standalone/_deps/crypto/cjs/padding.js +0 -511
  239. package/dist-standalone/_deps/crypto/cjs/share-header.js +0 -372
  240. package/dist-standalone/_deps/crypto/cjs/shares.js +0 -874
  241. package/dist-standalone/_deps/crypto/cjs/tlv.js +0 -1021
  242. package/dist-standalone/_deps/crypto/cjs/uuid.js +0 -443
  243. package/dist-standalone/_deps/crypto/cjs/verify.js +0 -414
  244. package/dist-standalone/_deps/crypto/cjs/xorida.js +0 -923
  245. package/dist-standalone/_deps/crypto/errors.d.ts +0 -51
  246. package/dist-standalone/_deps/crypto/errors.js +0 -199
  247. package/dist-standalone/_deps/crypto/hmac.d.ts +0 -39
  248. package/dist-standalone/_deps/crypto/hmac.js +0 -134
  249. package/dist-standalone/_deps/crypto/index.d.ts +0 -20
  250. package/dist-standalone/_deps/crypto/index.js +0 -145
  251. package/dist-standalone/_deps/crypto/padding.d.ts +0 -19
  252. package/dist-standalone/_deps/crypto/padding.js +0 -159
  253. package/dist-standalone/_deps/crypto/share-header.d.ts +0 -44
  254. package/dist-standalone/_deps/crypto/share-header.js +0 -92
  255. package/dist-standalone/_deps/crypto/shares.d.ts +0 -27
  256. package/dist-standalone/_deps/crypto/shares.js +0 -295
  257. package/dist-standalone/_deps/crypto/tlv.d.ts +0 -26
  258. package/dist-standalone/_deps/crypto/tlv.js +0 -364
  259. package/dist-standalone/_deps/crypto/uuid.d.ts +0 -22
  260. package/dist-standalone/_deps/crypto/uuid.js +0 -136
  261. package/dist-standalone/_deps/crypto/verify.d.ts +0 -15
  262. package/dist-standalone/_deps/crypto/verify.js +0 -71
  263. package/dist-standalone/_deps/crypto/xorida.d.ts +0 -44
  264. package/dist-standalone/_deps/crypto/xorida.js +0 -366
  265. package/dist-standalone/_deps/shared/errors.d.ts.map +0 -1
  266. package/dist-standalone/_deps/shared/errors.js.map +0 -1
  267. package/dist-standalone/_deps/shared/index.d.ts.map +0 -1
  268. package/dist-standalone/_deps/shared/index.js.map +0 -1
  269. package/dist-standalone/_deps/shared/types.d.ts.map +0 -1
  270. package/dist-standalone/_deps/shared/types.js.map +0 -1
  271. package/dist-standalone/_deps/ux-helpers/cjs/errors.d.ts.map +0 -1
  272. package/dist-standalone/_deps/ux-helpers/cjs/errors.js.map +0 -1
  273. package/dist-standalone/_deps/ux-helpers/cjs/index.d.ts.map +0 -1
  274. package/dist-standalone/_deps/ux-helpers/cjs/index.js.map +0 -1
  275. package/dist-standalone/_deps/ux-helpers/cjs/pagination.d.ts.map +0 -1
  276. package/dist-standalone/_deps/ux-helpers/cjs/pagination.js.map +0 -1
  277. package/dist-standalone/_deps/ux-helpers/cjs/progress.d.ts.map +0 -1
  278. package/dist-standalone/_deps/ux-helpers/cjs/progress.js.map +0 -1
  279. package/dist-standalone/_deps/ux-helpers/cjs/search.d.ts.map +0 -1
  280. package/dist-standalone/_deps/ux-helpers/cjs/search.js.map +0 -1
  281. package/dist-standalone/_deps/ux-helpers/cjs/types.d.ts.map +0 -1
  282. package/dist-standalone/_deps/ux-helpers/cjs/types.js.map +0 -1
  283. package/dist-standalone/_deps/ux-helpers/errors.d.ts.map +0 -1
  284. package/dist-standalone/_deps/ux-helpers/errors.js.map +0 -1
  285. package/dist-standalone/_deps/ux-helpers/index.d.ts.map +0 -1
  286. package/dist-standalone/_deps/ux-helpers/index.js.map +0 -1
  287. package/dist-standalone/_deps/ux-helpers/pagination.d.ts.map +0 -1
  288. package/dist-standalone/_deps/ux-helpers/pagination.js.map +0 -1
  289. package/dist-standalone/_deps/ux-helpers/progress.d.ts.map +0 -1
  290. package/dist-standalone/_deps/ux-helpers/progress.js.map +0 -1
  291. package/dist-standalone/_deps/ux-helpers/search.d.ts.map +0 -1
  292. package/dist-standalone/_deps/ux-helpers/search.js.map +0 -1
  293. package/dist-standalone/_deps/ux-helpers/types.d.ts.map +0 -1
  294. package/dist-standalone/_deps/ux-helpers/types.js.map +0 -1
  295. package/dist-standalone/_deps/xregistry/discovery.d.ts.map +0 -1
  296. package/dist-standalone/_deps/xregistry/discovery.js.map +0 -1
  297. package/dist-standalone/_deps/xregistry/errors.d.ts.map +0 -1
  298. package/dist-standalone/_deps/xregistry/errors.js.map +0 -1
  299. package/dist-standalone/_deps/xregistry/index.d.ts.map +0 -1
  300. package/dist-standalone/_deps/xregistry/index.js.map +0 -1
  301. package/dist-standalone/_deps/xregistry/registry.d.ts.map +0 -1
  302. package/dist-standalone/_deps/xregistry/registry.js.map +0 -1
  303. package/dist-standalone/_deps/xregistry/schema.d.ts.map +0 -1
  304. package/dist-standalone/_deps/xregistry/schema.js.map +0 -1
  305. package/dist-standalone/_deps/xregistry/types.d.ts.map +0 -1
  306. package/dist-standalone/_deps/xregistry/types.js.map +0 -1
@@ -1,510 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createEnvelope = createEnvelope;
4
- exports.createEnvelopeV2 = createEnvelopeV2;
5
- exports.createEnvelopeV3 = createEnvelopeV3;
6
- exports.createEnvelopeV4 = createEnvelopeV4;
7
- exports.decryptPayload = decryptPayload;
8
- exports.serializeEnvelope = serializeEnvelope;
9
- exports.deserializeEnvelope = deserializeEnvelope;
10
- exports.validateEnvelope = validateEnvelope;
11
- exports.generateSharedKey = generateSharedKey;
12
- exports.createSignedEnvelope = createSignedEnvelope;
13
- exports.openSignedEnvelope = openSignedEnvelope;
14
- const shared_1 = require("../_deps/shared/index.js");
15
- const crypto_1 = require("../_deps/crypto/index.js");
16
- const identity_js_1 = require("./identity.js");
17
- /* ── Constants ── */
18
- const NONCE_BYTES = 16;
19
- const AES_IV_BYTES = 12;
20
- const AES_ALGO = 'AES-GCM';
21
- /** ML-KEM-768 ciphertext length in bytes. */
22
- const KEM_CIPHERTEXT_BYTES = 1088;
23
- /** ML-DSA-65 signature length in bytes. */
24
- const ML_DSA65_SIG_BYTES = 3309;
25
- /* ── Helpers ── */
26
- /** Copy Uint8Array to fresh ArrayBuffer. */
27
- function toArrayBuffer(data) {
28
- const buf = new ArrayBuffer(data.byteLength);
29
- new Uint8Array(buf).set(data);
30
- return buf;
31
- }
32
- /**
33
- * Create canonical representation of envelope fields for signing.
34
- *
35
- * SECURITY FIX (v1.1.3): Sign ALL envelope metadata (nonce, timestamp, sender,
36
- * recipient, scope, payload) to prevent replay attacks via nonce substitution.
37
- *
38
- * Previous versions only signed the payload, allowing attackers to:
39
- * 1. Capture an envelope
40
- * 2. Change the nonce to a fresh value
41
- * 3. Replay the message (signature still verified)
42
- *
43
- * Canonical format: UTF-8 JSON with sorted keys, no whitespace.
44
- * Example: {"alg":"Ed25519","nonce":"...","payload":"...","recipient":"...","scope":"...","sender":"...","timestamp":1234567890,"v":1}
45
- */
46
- function createSignedData(envelope) {
47
- // Create canonical JSON representation (sorted keys)
48
- const canonical = JSON.stringify({
49
- v: envelope.v,
50
- alg: envelope.alg,
51
- sender: envelope.sender,
52
- recipient: envelope.recipient,
53
- timestamp: envelope.timestamp,
54
- nonce: envelope.nonce,
55
- scope: envelope.scope,
56
- payload: envelope.payload,
57
- });
58
- return new TextEncoder().encode(canonical);
59
- }
60
- /* ── Envelope Creation ── */
61
- /**
62
- * Create a signed, encrypted TransportEnvelope.
63
- *
64
- * Encrypt-then-sign: AES-256-GCM encrypts payload, Ed25519 signs ciphertext.
65
- */
66
- async function createEnvelope(opts) {
67
- const nonce = new Uint8Array(NONCE_BYTES);
68
- crypto.getRandomValues(nonce);
69
- const iv = new Uint8Array(AES_IV_BYTES);
70
- crypto.getRandomValues(iv);
71
- let ciphertext;
72
- try {
73
- const encrypted = await crypto.subtle.encrypt({ name: AES_ALGO, iv: toArrayBuffer(iv) }, opts.sharedKey, toArrayBuffer(opts.plaintext));
74
- const ivAndCipher = new Uint8Array(iv.length + encrypted.byteLength);
75
- ivAndCipher.set(iv);
76
- ivAndCipher.set(new Uint8Array(encrypted), iv.length);
77
- ciphertext = ivAndCipher;
78
- }
79
- catch {
80
- return (0, shared_1.err)('ENCRYPT_FAILED');
81
- }
82
- // SECURITY FIX (v1.1.3): Sign canonical representation of ENTIRE envelope
83
- // (not just payload) to prevent replay attacks via nonce substitution
84
- const timestamp = Date.now();
85
- const envelopeFields = {
86
- v: 1,
87
- alg: 'Ed25519',
88
- sender: opts.senderDid,
89
- recipient: opts.recipientDid,
90
- timestamp,
91
- nonce: (0, crypto_1.toBase64)(nonce),
92
- scope: opts.scope,
93
- payload: (0, crypto_1.toBase64)(ciphertext),
94
- };
95
- const signedData = createSignedData(envelopeFields);
96
- const sigResult = await (0, identity_js_1.sign)(opts.privateKey, signedData);
97
- if (!sigResult.ok)
98
- return (0, shared_1.err)('SIGN_FAILED');
99
- const envelope = {
100
- ...envelopeFields,
101
- signature: (0, crypto_1.toBase64)(sigResult.value),
102
- ...(opts.ephemeralPublicKey
103
- ? { ephemeralPub: (0, crypto_1.toBase64)(opts.ephemeralPublicKey) }
104
- : {}),
105
- ...(opts.shareIndex !== undefined
106
- ? {
107
- shareIndex: opts.shareIndex,
108
- shareTotal: opts.shareTotal,
109
- shareThreshold: opts.shareThreshold,
110
- shareGroupId: opts.shareGroupId,
111
- shareHmacKey: opts.shareHmacKey,
112
- shareHmacSig: opts.shareHmacSig,
113
- }
114
- : {}),
115
- protocol: 'PRIVATE.ME/xBind/v3',
116
- documentationUrl: 'https://private.me/docs/xbind.html',
117
- };
118
- return (0, shared_1.ok)(envelope);
119
- }
120
- /* ── V2 Envelope Creation ── */
121
- /**
122
- * Create a signed, encrypted TransportEnvelopeV2 with hybrid KEM.
123
- *
124
- * Same encrypt-then-sign as v1, but includes ML-KEM-768 ciphertext.
125
- */
126
- async function createEnvelopeV2(opts) {
127
- const nonce = new Uint8Array(NONCE_BYTES);
128
- crypto.getRandomValues(nonce);
129
- const iv = new Uint8Array(AES_IV_BYTES);
130
- crypto.getRandomValues(iv);
131
- let ciphertext;
132
- try {
133
- const encrypted = await crypto.subtle.encrypt({ name: AES_ALGO, iv: toArrayBuffer(iv) }, opts.sharedKey, toArrayBuffer(opts.plaintext));
134
- const ivAndCipher = new Uint8Array(iv.length + encrypted.byteLength);
135
- ivAndCipher.set(iv);
136
- ivAndCipher.set(new Uint8Array(encrypted), iv.length);
137
- ciphertext = ivAndCipher;
138
- }
139
- catch {
140
- return (0, shared_1.err)('ENCRYPT_FAILED');
141
- }
142
- // SECURITY FIX (v1.1.3): Sign canonical representation of ENTIRE envelope
143
- const timestamp = Date.now();
144
- const envelopeFields = {
145
- v: 2,
146
- alg: 'Ed25519',
147
- sender: opts.senderDid,
148
- recipient: opts.recipientDid,
149
- timestamp,
150
- nonce: (0, crypto_1.toBase64)(nonce),
151
- scope: opts.scope,
152
- payload: (0, crypto_1.toBase64)(ciphertext),
153
- };
154
- const signedData = createSignedData(envelopeFields);
155
- const sigResult = await (0, identity_js_1.sign)(opts.privateKey, signedData);
156
- if (!sigResult.ok)
157
- return (0, shared_1.err)('SIGN_FAILED');
158
- const envelope = {
159
- ...envelopeFields,
160
- kem: 'X25519-MLKEM768',
161
- signature: (0, crypto_1.toBase64)(sigResult.value),
162
- ephemeralPub: (0, crypto_1.toBase64)(opts.ephemeralPublicKey),
163
- kemCiphertext: (0, crypto_1.toBase64)(opts.kemCiphertext),
164
- ...(opts.shareIndex !== undefined
165
- ? {
166
- shareIndex: opts.shareIndex,
167
- shareTotal: opts.shareTotal,
168
- shareThreshold: opts.shareThreshold,
169
- shareGroupId: opts.shareGroupId,
170
- shareHmacKey: opts.shareHmacKey,
171
- shareHmacSig: opts.shareHmacSig,
172
- }
173
- : {}),
174
- protocol: 'PRIVATE.ME/xBind/v3',
175
- documentationUrl: 'https://private.me/docs/xbind.html',
176
- };
177
- return (0, shared_1.ok)(envelope);
178
- }
179
- /* ── V3 Envelope Creation ── */
180
- /**
181
- * Create a signed, encrypted TransportEnvelopeV3 with hybrid KEM + dual signatures.
182
- *
183
- * Encrypt-then-sign: AES-256-GCM encrypts payload, Ed25519 AND ML-DSA-65 sign ciphertext.
184
- * Both signatures must verify for the envelope to be accepted.
185
- */
186
- async function createEnvelopeV3(opts) {
187
- const nonce = new Uint8Array(NONCE_BYTES);
188
- crypto.getRandomValues(nonce);
189
- const iv = new Uint8Array(AES_IV_BYTES);
190
- crypto.getRandomValues(iv);
191
- let ciphertext;
192
- try {
193
- const encrypted = await crypto.subtle.encrypt({ name: AES_ALGO, iv: toArrayBuffer(iv) }, opts.sharedKey, toArrayBuffer(opts.plaintext));
194
- const ivAndCipher = new Uint8Array(iv.length + encrypted.byteLength);
195
- ivAndCipher.set(iv);
196
- ivAndCipher.set(new Uint8Array(encrypted), iv.length);
197
- ciphertext = ivAndCipher;
198
- }
199
- catch {
200
- return (0, shared_1.err)('ENCRYPT_FAILED');
201
- }
202
- // SECURITY FIX (v1.1.3): Sign canonical representation of ENTIRE envelope
203
- const timestamp = Date.now();
204
- const envelopeFields = {
205
- v: 3,
206
- alg: 'Ed25519',
207
- sender: opts.senderDid,
208
- recipient: opts.recipientDid,
209
- timestamp,
210
- nonce: (0, crypto_1.toBase64)(nonce),
211
- scope: opts.scope,
212
- payload: (0, crypto_1.toBase64)(ciphertext),
213
- };
214
- const signedData = createSignedData(envelopeFields);
215
- const sigResult = await (0, identity_js_1.sign)(opts.privateKey, signedData);
216
- if (!sigResult.ok)
217
- return (0, shared_1.err)('SIGN_FAILED');
218
- const pqSigResult = await (0, identity_js_1.signMlDsa65)(opts.mlDsaSecretKey, signedData);
219
- if (!pqSigResult.ok)
220
- return (0, shared_1.err)('PQ_SIGN_FAILED');
221
- const envelope = {
222
- ...envelopeFields,
223
- sig: 'Ed25519+MLDSA65',
224
- kem: 'X25519-MLKEM768',
225
- signature: (0, crypto_1.toBase64)(sigResult.value),
226
- pqSignature: (0, crypto_1.toBase64)(pqSigResult.value),
227
- ephemeralPub: (0, crypto_1.toBase64)(opts.ephemeralPublicKey),
228
- kemCiphertext: (0, crypto_1.toBase64)(opts.kemCiphertext),
229
- ...(opts.shareIndex !== undefined
230
- ? {
231
- shareIndex: opts.shareIndex,
232
- shareTotal: opts.shareTotal,
233
- shareThreshold: opts.shareThreshold,
234
- shareGroupId: opts.shareGroupId,
235
- shareHmacKey: opts.shareHmacKey,
236
- shareHmacSig: opts.shareHmacSig,
237
- }
238
- : {}),
239
- protocol: 'PRIVATE.ME/xBind/v3',
240
- documentationUrl: 'https://private.me/docs/xbind.html',
241
- };
242
- return (0, shared_1.ok)(envelope);
243
- }
244
- /* ── Xchange Mode Envelope Creation ── */
245
- /**
246
- * Create a signed Xchange mode envelope (v4 wire format).
247
- *
248
- * Opt-in performance mode. No encryption — the share IS the payload.
249
- * Ed25519 signs the raw share data for sender authentication.
250
- * The XorIDA split provides confidentiality (information-theoretic).
251
- */
252
- async function createEnvelopeV4(opts) {
253
- const nonce = new Uint8Array(NONCE_BYTES);
254
- crypto.getRandomValues(nonce);
255
- // SECURITY FIX (v1.1.3): Sign canonical representation of ENTIRE envelope
256
- const timestamp = Date.now();
257
- const envelopeFields = {
258
- v: 4,
259
- alg: 'Ed25519',
260
- sender: opts.senderDid,
261
- recipient: opts.recipientDid,
262
- timestamp,
263
- nonce: (0, crypto_1.toBase64)(nonce),
264
- scope: opts.scope,
265
- payload: (0, crypto_1.toBase64)(opts.shareData),
266
- };
267
- const signedData = createSignedData(envelopeFields);
268
- const sigResult = await (0, identity_js_1.sign)(opts.privateKey, signedData);
269
- if (!sigResult.ok)
270
- return (0, shared_1.err)('SIGN_FAILED');
271
- const envelope = {
272
- ...envelopeFields,
273
- kem: 'Xchange',
274
- signature: (0, crypto_1.toBase64)(sigResult.value),
275
- shareIndex: opts.shareIndex,
276
- shareTotal: opts.shareTotal,
277
- shareThreshold: opts.shareThreshold,
278
- shareGroupId: opts.shareGroupId,
279
- shareHmacKey: opts.shareHmacKey,
280
- shareHmacSig: opts.shareHmacSig,
281
- protocol: 'PRIVATE.ME/xBind/v3',
282
- documentationUrl: 'https://private.me/docs/xbind.html',
283
- };
284
- return (0, shared_1.ok)(envelope);
285
- }
286
- /* ── Decryption ── */
287
- /**
288
- * Decrypt the payload from a v1, v2, or v3 TransportEnvelope.
289
- * Caller must verify signature before calling this.
290
- */
291
- async function decryptPayload(envelope, sharedKey) {
292
- try {
293
- const ciphertext = (0, crypto_1.fromBase64)(envelope.payload);
294
- const iv = ciphertext.slice(0, AES_IV_BYTES);
295
- const data = ciphertext.slice(AES_IV_BYTES);
296
- const decrypted = await crypto.subtle.decrypt({ name: AES_ALGO, iv: toArrayBuffer(iv) }, sharedKey, toArrayBuffer(data));
297
- return (0, shared_1.ok)(new Uint8Array(decrypted));
298
- }
299
- catch {
300
- return (0, shared_1.err)('DECRYPT_FAILED');
301
- }
302
- }
303
- /* ── Serialization ── */
304
- /** Serialize envelope to UTF-8 bytes. */
305
- function serializeEnvelope(envelope) {
306
- return new TextEncoder().encode(JSON.stringify(envelope));
307
- }
308
- /** Deserialize bytes to a validated v1 or v2 TransportEnvelope. */
309
- function deserializeEnvelope(bytes) {
310
- let parsed;
311
- try {
312
- parsed = JSON.parse(new TextDecoder().decode(bytes));
313
- }
314
- catch {
315
- return (0, shared_1.err)('PARSE_FAILED');
316
- }
317
- return validateEnvelope(parsed);
318
- }
319
- /* ── Validation ── */
320
- /** Validate a parsed object is a valid v1 or v2 TransportEnvelope. */
321
- function validateEnvelope(obj) {
322
- if (typeof obj !== 'object' || obj === null)
323
- return (0, shared_1.err)('PARSE_FAILED');
324
- const e = obj;
325
- if (e['v'] !== 1 && e['v'] !== 2 && e['v'] !== 3 && e['v'] !== 4)
326
- return (0, shared_1.err)('INVALID_VERSION');
327
- if (e['alg'] !== 'Ed25519')
328
- return (0, shared_1.err)('INVALID_ALG');
329
- if (typeof e['sender'] !== 'string' || !e['sender'].startsWith('did:'))
330
- return (0, shared_1.err)('INVALID_DID');
331
- if (typeof e['recipient'] !== 'string' || !e['recipient'].startsWith('did:'))
332
- return (0, shared_1.err)('INVALID_DID');
333
- if (typeof e['timestamp'] !== 'number')
334
- return (0, shared_1.err)('INVALID_FIELDS');
335
- if (typeof e['scope'] !== 'string')
336
- return (0, shared_1.err)('INVALID_FIELDS');
337
- if (typeof e['payload'] !== 'string')
338
- return (0, shared_1.err)('INVALID_FIELDS');
339
- if (typeof e['signature'] !== 'string')
340
- return (0, shared_1.err)('INVALID_FIELDS');
341
- if (typeof e['nonce'] !== 'string')
342
- return (0, shared_1.err)('INVALID_NONCE');
343
- try {
344
- const nonceBytes = (0, crypto_1.fromBase64)(e['nonce']);
345
- if (nonceBytes.length !== NONCE_BYTES)
346
- return (0, shared_1.err)('INVALID_NONCE');
347
- }
348
- catch {
349
- return (0, shared_1.err)('INVALID_NONCE');
350
- }
351
- if (e['ephemeralPub'] !== undefined && typeof e['ephemeralPub'] !== 'string') {
352
- return (0, shared_1.err)('INVALID_FIELDS');
353
- }
354
- // V2-specific validation
355
- if (e['v'] === 2) {
356
- if (e['kem'] !== 'X25519-MLKEM768')
357
- return (0, shared_1.err)('INVALID_KEM');
358
- if (typeof e['ephemeralPub'] !== 'string')
359
- return (0, shared_1.err)('INVALID_FIELDS');
360
- if (typeof e['kemCiphertext'] !== 'string')
361
- return (0, shared_1.err)('INVALID_FIELDS');
362
- try {
363
- const kemBytes = (0, crypto_1.fromBase64)(e['kemCiphertext']);
364
- if (kemBytes.length !== KEM_CIPHERTEXT_BYTES)
365
- return (0, shared_1.err)('INVALID_FIELDS');
366
- }
367
- catch {
368
- return (0, shared_1.err)('INVALID_FIELDS');
369
- }
370
- }
371
- // V3-specific validation (hybrid KEM + dual signatures)
372
- if (e['v'] === 3) {
373
- if (e['sig'] !== 'Ed25519+MLDSA65')
374
- return (0, shared_1.err)('INVALID_ALG');
375
- if (e['kem'] !== 'X25519-MLKEM768')
376
- return (0, shared_1.err)('INVALID_KEM');
377
- if (typeof e['ephemeralPub'] !== 'string')
378
- return (0, shared_1.err)('INVALID_FIELDS');
379
- if (typeof e['kemCiphertext'] !== 'string')
380
- return (0, shared_1.err)('INVALID_FIELDS');
381
- if (typeof e['pqSignature'] !== 'string')
382
- return (0, shared_1.err)('INVALID_FIELDS');
383
- try {
384
- const kemBytes = (0, crypto_1.fromBase64)(e['kemCiphertext']);
385
- if (kemBytes.length !== KEM_CIPHERTEXT_BYTES)
386
- return (0, shared_1.err)('INVALID_FIELDS');
387
- }
388
- catch {
389
- return (0, shared_1.err)('INVALID_FIELDS');
390
- }
391
- try {
392
- const pqSigBytes = (0, crypto_1.fromBase64)(e['pqSignature']);
393
- if (pqSigBytes.length !== ML_DSA65_SIG_BYTES)
394
- return (0, shared_1.err)('INVALID_FIELDS');
395
- }
396
- catch {
397
- return (0, shared_1.err)('INVALID_FIELDS');
398
- }
399
- }
400
- // V4-specific validation (Xchange — no encryption, share metadata required)
401
- if (e['v'] === 4) {
402
- if (e['kem'] !== 'Xchange')
403
- return (0, shared_1.err)('INVALID_KEM');
404
- if (typeof e['shareIndex'] !== 'number')
405
- return (0, shared_1.err)('INVALID_FIELDS');
406
- if (typeof e['shareTotal'] !== 'number')
407
- return (0, shared_1.err)('INVALID_FIELDS');
408
- if (typeof e['shareThreshold'] !== 'number')
409
- return (0, shared_1.err)('INVALID_FIELDS');
410
- if (typeof e['shareGroupId'] !== 'string')
411
- return (0, shared_1.err)('INVALID_FIELDS');
412
- if (typeof e['shareHmacKey'] !== 'string')
413
- return (0, shared_1.err)('INVALID_FIELDS');
414
- if (typeof e['shareHmacSig'] !== 'string')
415
- return (0, shared_1.err)('INVALID_FIELDS');
416
- return (0, shared_1.ok)(e);
417
- }
418
- if (e['shareIndex'] !== undefined && typeof e['shareIndex'] !== 'number') {
419
- return (0, shared_1.err)('INVALID_FIELDS');
420
- }
421
- if (e['shareTotal'] !== undefined && typeof e['shareTotal'] !== 'number') {
422
- return (0, shared_1.err)('INVALID_FIELDS');
423
- }
424
- if (e['shareThreshold'] !== undefined && typeof e['shareThreshold'] !== 'number') {
425
- return (0, shared_1.err)('INVALID_FIELDS');
426
- }
427
- if (e['shareGroupId'] !== undefined && typeof e['shareGroupId'] !== 'string') {
428
- return (0, shared_1.err)('INVALID_FIELDS');
429
- }
430
- if (e['shareHmacKey'] !== undefined && typeof e['shareHmacKey'] !== 'string') {
431
- return (0, shared_1.err)('INVALID_FIELDS');
432
- }
433
- if (e['shareHmacSig'] !== undefined && typeof e['shareHmacSig'] !== 'string') {
434
- return (0, shared_1.err)('INVALID_FIELDS');
435
- }
436
- return (0, shared_1.ok)(e);
437
- }
438
- /** Generate an AES-256-GCM key for envelope encryption. */
439
- async function generateSharedKey() {
440
- return crypto.subtle.generateKey({ name: AES_ALGO, length: 256 }, true, ['encrypt', 'decrypt']);
441
- }
442
- /* ── Signed (Unencrypted) Envelopes ── */
443
- /**
444
- * Create a signed but unencrypted TransportEnvelope.
445
- *
446
- * Payload is base64-encoded plaintext (NOT encrypted).
447
- * Signature covers the raw plaintext bytes.
448
- * Use for public announcements, audit logs, or telemetry where
449
- * confidentiality is not required but integrity is.
450
- *
451
- * @param opts - Sender, recipient, scope, plaintext, and signing key.
452
- * @returns Signed envelope or error.
453
- */
454
- async function createSignedEnvelope(opts) {
455
- const nonce = new Uint8Array(NONCE_BYTES);
456
- crypto.getRandomValues(nonce);
457
- const timestamp = Date.now();
458
- const payloadBase64 = (0, crypto_1.toBase64)(opts.plaintext);
459
- const nonceBase64 = (0, crypto_1.toBase64)(nonce);
460
- // SECURITY FIX (v1.1.3): Sign canonical representation of ENTIRE envelope
461
- // (not just payload) to prevent replay attacks via nonce/timestamp mutation
462
- const envelopeFields = {
463
- v: 1,
464
- alg: 'Ed25519',
465
- sender: opts.senderDid,
466
- recipient: opts.recipientDid,
467
- timestamp,
468
- nonce: nonceBase64,
469
- scope: opts.scope,
470
- payload: payloadBase64,
471
- };
472
- const signedData = createSignedData(envelopeFields);
473
- const sigResult = await (0, identity_js_1.sign)(opts.privateKey, signedData);
474
- if (!sigResult.ok)
475
- return (0, shared_1.err)('SIGN_FAILED');
476
- const envelope = {
477
- ...envelopeFields,
478
- signature: (0, crypto_1.toBase64)(sigResult.value),
479
- };
480
- return (0, shared_1.ok)(envelope);
481
- }
482
- /**
483
- * Verify signature on a signed (unencrypted) envelope and return plaintext.
484
- *
485
- * @param envelope - The signed envelope to verify.
486
- * @param senderPublicKey - The sender's Ed25519 public key.
487
- * @returns Plaintext bytes if signature is valid, error otherwise.
488
- */
489
- async function openSignedEnvelope(envelope, senderPublicKey) {
490
- const payloadBytes = (0, crypto_1.fromBase64)(envelope.payload);
491
- const sigBytes = (0, crypto_1.fromBase64)(envelope.signature);
492
- // SECURITY FIX (v1.1.3): Verify canonical representation (not just payload)
493
- const canonicalData = JSON.stringify({
494
- v: envelope.v,
495
- alg: envelope.alg,
496
- sender: envelope.sender,
497
- recipient: envelope.recipient,
498
- timestamp: envelope.timestamp,
499
- nonce: envelope.nonce,
500
- scope: envelope.scope,
501
- payload: envelope.payload,
502
- });
503
- const canonicalBytes = new TextEncoder().encode(canonicalData);
504
- const valid = await (0, identity_js_1.verify)(senderPublicKey, sigBytes, canonicalBytes);
505
- if (!valid.ok)
506
- return (0, shared_1.err)('VERIFY_FAILED');
507
- if (!valid.value)
508
- return (0, shared_1.err)('VERIFY_FAILED');
509
- return (0, shared_1.ok)(payloadBytes);
510
- }
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.createEnvelope=createEnvelope,exports.createEnvelopeV2=createEnvelopeV2,exports.createEnvelopeV3=createEnvelopeV3,exports.createEnvelopeV4=createEnvelopeV4,exports.decryptPayload=decryptPayload,exports.serializeEnvelope=serializeEnvelope,exports.deserializeEnvelope=deserializeEnvelope,exports.validateEnvelope=validateEnvelope,exports.generateSharedKey=generateSharedKey,exports.createSignedEnvelope=createSignedEnvelope,exports.openSignedEnvelope=openSignedEnvelope;const shared_1=require("../_deps/shared/index.js"),crypto_utils_js_1=require("./crypto-utils.js"),identity_js_1=require("./identity.js"),NONCE_BYTES=16,AES_IV_BYTES=12,AES_ALGO="AES-GCM",KEM_CIPHERTEXT_BYTES=1088,ML_DSA65_SIG_BYTES=3309;function toArrayBuffer(e){const r=new ArrayBuffer(e.byteLength);return new Uint8Array(r).set(e),r}function createSignedData(e){const r=JSON.stringify({v:e.v,alg:e.alg,sender:e.sender,recipient:e.recipient,timestamp:e.timestamp,nonce:e.nonce,scope:e.scope,payload:e.payload});return(new TextEncoder).encode(r)}async function createEnvelope(e){const r=new Uint8Array(NONCE_BYTES);crypto.getRandomValues(r);const t=new Uint8Array(AES_IV_BYTES);let a;crypto.getRandomValues(t);try{const r=await crypto.subtle.encrypt({name:AES_ALGO,iv:toArrayBuffer(t)},e.sharedKey,toArrayBuffer(e.plaintext)),s=new Uint8Array(t.length+r.byteLength);s.set(t),s.set(new Uint8Array(r),t.length),a=s}catch{return(0,shared_1.err)("ENCRYPT_FAILED")}const s=Date.now(),n={v:1,alg:"Ed25519",sender:e.senderDid,recipient:e.recipientDid,timestamp:s,nonce:(0,crypto_utils_js_1.toBase64)(r),scope:e.scope,payload:(0,crypto_utils_js_1.toBase64)(a)},o=createSignedData(n),i=await(0,identity_js_1.sign)(e.privateKey,o);if(!i.ok)return(0,shared_1.err)("SIGN_FAILED");const _={...n,signature:(0,crypto_utils_js_1.toBase64)(i.value),...e.ephemeralPublicKey?{ephemeralPub:(0,crypto_utils_js_1.toBase64)(e.ephemeralPublicKey)}:{},...void 0!==e.shareIndex?{shareIndex:e.shareIndex,shareTotal:e.shareTotal,shareThreshold:e.shareThreshold,shareGroupId:e.shareGroupId,shareHmacKey:e.shareHmacKey,shareHmacSig:e.shareHmacSig}:{},protocol:"PRIVATE.ME/xBind/v3",documentationUrl:"https://private.me/docs/xbind.html"};return(0,shared_1.ok)(_)}async function createEnvelopeV2(e){const r=new Uint8Array(NONCE_BYTES);crypto.getRandomValues(r);const t=new Uint8Array(AES_IV_BYTES);let a;crypto.getRandomValues(t);try{const r=await crypto.subtle.encrypt({name:AES_ALGO,iv:toArrayBuffer(t)},e.sharedKey,toArrayBuffer(e.plaintext)),s=new Uint8Array(t.length+r.byteLength);s.set(t),s.set(new Uint8Array(r),t.length),a=s}catch{return(0,shared_1.err)("ENCRYPT_FAILED")}const s=Date.now(),n={v:2,alg:"Ed25519",sender:e.senderDid,recipient:e.recipientDid,timestamp:s,nonce:(0,crypto_utils_js_1.toBase64)(r),scope:e.scope,payload:(0,crypto_utils_js_1.toBase64)(a)},o=createSignedData(n),i=await(0,identity_js_1.sign)(e.privateKey,o);if(!i.ok)return(0,shared_1.err)("SIGN_FAILED");const _={...n,kem:"X25519-MLKEM768",signature:(0,crypto_utils_js_1.toBase64)(i.value),ephemeralPub:(0,crypto_utils_js_1.toBase64)(e.ephemeralPublicKey),kemCiphertext:(0,crypto_utils_js_1.toBase64)(e.kemCiphertext),...void 0!==e.shareIndex?{shareIndex:e.shareIndex,shareTotal:e.shareTotal,shareThreshold:e.shareThreshold,shareGroupId:e.shareGroupId,shareHmacKey:e.shareHmacKey,shareHmacSig:e.shareHmacSig}:{},protocol:"PRIVATE.ME/xBind/v3",documentationUrl:"https://private.me/docs/xbind.html"};return(0,shared_1.ok)(_)}async function createEnvelopeV3(e){const r=new Uint8Array(NONCE_BYTES);crypto.getRandomValues(r);const t=new Uint8Array(AES_IV_BYTES);let a;crypto.getRandomValues(t);try{const r=await crypto.subtle.encrypt({name:AES_ALGO,iv:toArrayBuffer(t)},e.sharedKey,toArrayBuffer(e.plaintext)),s=new Uint8Array(t.length+r.byteLength);s.set(t),s.set(new Uint8Array(r),t.length),a=s}catch{return(0,shared_1.err)("ENCRYPT_FAILED")}const s=Date.now(),n={v:3,alg:"Ed25519",sender:e.senderDid,recipient:e.recipientDid,timestamp:s,nonce:(0,crypto_utils_js_1.toBase64)(r),scope:e.scope,payload:(0,crypto_utils_js_1.toBase64)(a)},o=createSignedData(n),i=await(0,identity_js_1.sign)(e.privateKey,o);if(!i.ok)return(0,shared_1.err)("SIGN_FAILED");const _=await(0,identity_js_1.signMlDsa65)(e.mlDsaSecretKey,o);if(!_.ok)return(0,shared_1.err)("PQ_SIGN_FAILED");const c={...n,sig:"Ed25519+MLDSA65",kem:"X25519-MLKEM768",signature:(0,crypto_utils_js_1.toBase64)(i.value),pqSignature:(0,crypto_utils_js_1.toBase64)(_.value),ephemeralPub:(0,crypto_utils_js_1.toBase64)(e.ephemeralPublicKey),kemCiphertext:(0,crypto_utils_js_1.toBase64)(e.kemCiphertext),...void 0!==e.shareIndex?{shareIndex:e.shareIndex,shareTotal:e.shareTotal,shareThreshold:e.shareThreshold,shareGroupId:e.shareGroupId,shareHmacKey:e.shareHmacKey,shareHmacSig:e.shareHmacSig}:{},protocol:"PRIVATE.ME/xBind/v3",documentationUrl:"https://private.me/docs/xbind.html"};return(0,shared_1.ok)(c)}async function createEnvelopeV4(e){const r=new Uint8Array(NONCE_BYTES);crypto.getRandomValues(r);const t=Date.now(),a={v:4,alg:"Ed25519",sender:e.senderDid,recipient:e.recipientDid,timestamp:t,nonce:(0,crypto_utils_js_1.toBase64)(r),scope:e.scope,payload:(0,crypto_utils_js_1.toBase64)(e.shareData)},s=createSignedData(a),n=await(0,identity_js_1.sign)(e.privateKey,s);if(!n.ok)return(0,shared_1.err)("SIGN_FAILED");const o={...a,kem:"Xchange",signature:(0,crypto_utils_js_1.toBase64)(n.value),shareIndex:e.shareIndex,shareTotal:e.shareTotal,shareThreshold:e.shareThreshold,shareGroupId:e.shareGroupId,shareHmacKey:e.shareHmacKey,shareHmacSig:e.shareHmacSig,protocol:"PRIVATE.ME/xBind/v3",documentationUrl:"https://private.me/docs/xbind.html"};return(0,shared_1.ok)(o)}async function decryptPayload(e,r){try{const t=(0,crypto_utils_js_1.fromBase64)(e.payload),a=t.slice(0,AES_IV_BYTES),s=t.slice(AES_IV_BYTES),n=await crypto.subtle.decrypt({name:AES_ALGO,iv:toArrayBuffer(a)},r,toArrayBuffer(s));return(0,shared_1.ok)(new Uint8Array(n))}catch{return(0,shared_1.err)("DECRYPT_FAILED")}}function serializeEnvelope(e){return(new TextEncoder).encode(JSON.stringify(e))}function deserializeEnvelope(e){let r;try{r=JSON.parse((new TextDecoder).decode(e))}catch{return(0,shared_1.err)("PARSE_FAILED")}return validateEnvelope(r)}function validateEnvelope(e){if("object"!=typeof e||null===e)return(0,shared_1.err)("PARSE_FAILED");const r=e;if(1!==r.v&&2!==r.v&&3!==r.v&&4!==r.v)return(0,shared_1.err)("INVALID_VERSION");if("Ed25519"!==r.alg)return(0,shared_1.err)("INVALID_ALG");if("string"!=typeof r.sender||!r.sender.startsWith("did:"))return(0,shared_1.err)("INVALID_DID");if("string"!=typeof r.recipient||!r.recipient.startsWith("did:"))return(0,shared_1.err)("INVALID_DID");if("number"!=typeof r.timestamp)return(0,shared_1.err)("INVALID_FIELDS");if("string"!=typeof r.scope)return(0,shared_1.err)("INVALID_FIELDS");if("string"!=typeof r.payload)return(0,shared_1.err)("INVALID_FIELDS");if("string"!=typeof r.signature)return(0,shared_1.err)("INVALID_FIELDS");if("string"!=typeof r.nonce)return(0,shared_1.err)("INVALID_NONCE");try{if((0,crypto_utils_js_1.fromBase64)(r.nonce).length!==NONCE_BYTES)return(0,shared_1.err)("INVALID_NONCE")}catch{return(0,shared_1.err)("INVALID_NONCE")}if(void 0!==r.ephemeralPub&&"string"!=typeof r.ephemeralPub)return(0,shared_1.err)("INVALID_FIELDS");if(2===r.v){if("X25519-MLKEM768"!==r.kem)return(0,shared_1.err)("INVALID_KEM");if("string"!=typeof r.ephemeralPub)return(0,shared_1.err)("INVALID_FIELDS");if("string"!=typeof r.kemCiphertext)return(0,shared_1.err)("INVALID_FIELDS");try{if((0,crypto_utils_js_1.fromBase64)(r.kemCiphertext).length!==KEM_CIPHERTEXT_BYTES)return(0,shared_1.err)("INVALID_FIELDS")}catch{return(0,shared_1.err)("INVALID_FIELDS")}}if(3===r.v){if("Ed25519+MLDSA65"!==r.sig)return(0,shared_1.err)("INVALID_ALG");if("X25519-MLKEM768"!==r.kem)return(0,shared_1.err)("INVALID_KEM");if("string"!=typeof r.ephemeralPub)return(0,shared_1.err)("INVALID_FIELDS");if("string"!=typeof r.kemCiphertext)return(0,shared_1.err)("INVALID_FIELDS");if("string"!=typeof r.pqSignature)return(0,shared_1.err)("INVALID_FIELDS");try{if((0,crypto_utils_js_1.fromBase64)(r.kemCiphertext).length!==KEM_CIPHERTEXT_BYTES)return(0,shared_1.err)("INVALID_FIELDS")}catch{return(0,shared_1.err)("INVALID_FIELDS")}try{if((0,crypto_utils_js_1.fromBase64)(r.pqSignature).length!==ML_DSA65_SIG_BYTES)return(0,shared_1.err)("INVALID_FIELDS")}catch{return(0,shared_1.err)("INVALID_FIELDS")}}return 4===r.v?"Xchange"!==r.kem?(0,shared_1.err)("INVALID_KEM"):"number"!=typeof r.shareIndex||"number"!=typeof r.shareTotal||"number"!=typeof r.shareThreshold||"string"!=typeof r.shareGroupId||"string"!=typeof r.shareHmacKey||"string"!=typeof r.shareHmacSig?(0,shared_1.err)("INVALID_FIELDS"):(0,shared_1.ok)(r):void 0!==r.shareIndex&&"number"!=typeof r.shareIndex||void 0!==r.shareTotal&&"number"!=typeof r.shareTotal||void 0!==r.shareThreshold&&"number"!=typeof r.shareThreshold||void 0!==r.shareGroupId&&"string"!=typeof r.shareGroupId||void 0!==r.shareHmacKey&&"string"!=typeof r.shareHmacKey||void 0!==r.shareHmacSig&&"string"!=typeof r.shareHmacSig?(0,shared_1.err)("INVALID_FIELDS"):(0,shared_1.ok)(r)}async function generateSharedKey(){return crypto.subtle.generateKey({name:AES_ALGO,length:256},!0,["encrypt","decrypt"])}async function createSignedEnvelope(e){const r=new Uint8Array(NONCE_BYTES);crypto.getRandomValues(r);const t=Date.now(),a=(0,crypto_utils_js_1.toBase64)(e.plaintext),s=(0,crypto_utils_js_1.toBase64)(r),n={v:1,alg:"Ed25519",sender:e.senderDid,recipient:e.recipientDid,timestamp:t,nonce:s,scope:e.scope,payload:a},o=createSignedData(n),i=await(0,identity_js_1.sign)(e.privateKey,o);if(!i.ok)return(0,shared_1.err)("SIGN_FAILED");const _={...n,signature:(0,crypto_utils_js_1.toBase64)(i.value)};return(0,shared_1.ok)(_)}async function openSignedEnvelope(e,r){const t=(0,crypto_utils_js_1.fromBase64)(e.payload),a=(0,crypto_utils_js_1.fromBase64)(e.signature),s=JSON.stringify({v:e.v,alg:e.alg,sender:e.sender,recipient:e.recipient,timestamp:e.timestamp,nonce:e.nonce,scope:e.scope,payload:e.payload}),n=(new TextEncoder).encode(s),o=await(0,identity_js_1.verify)(r,a,n);return o.ok&&o.value?(0,shared_1.ok)(t):(0,shared_1.err)("VERIFY_FAILED")}