@koralabs/handle-chat-sdk 0.0.12 → 1.0.1

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 (76) hide show
  1. package/LICENSE +6 -0
  2. package/README.md +97 -0
  3. package/assets/libsignal_bg.wasm +0 -0
  4. package/examples/guardian-flow.mjs +88 -0
  5. package/examples/quickstart.mjs +74 -0
  6. package/lib/libsignal-wasm/pkg/handle_libsignal_wasm.d.ts +187 -0
  7. package/lib/libsignal-wasm/pkg/handle_libsignal_wasm.js +860 -0
  8. package/lib/src/chat/ChatService.d.ts +113 -0
  9. package/lib/src/chat/ChatService.js +643 -0
  10. package/lib/src/chat/anchor.d.ts +17 -0
  11. package/lib/src/chat/anchor.js +43 -0
  12. package/lib/src/chat/blob.d.ts +41 -0
  13. package/lib/src/chat/blob.js +146 -0
  14. package/lib/src/chat/chainDirectory.d.ts +29 -0
  15. package/lib/src/chat/chainDirectory.js +51 -0
  16. package/lib/src/chat/directory.d.ts +14 -0
  17. package/lib/src/chat/directory.js +14 -0
  18. package/lib/src/chat/guardian.d.ts +73 -0
  19. package/lib/src/chat/guardian.js +55 -0
  20. package/lib/src/chat/identity.d.ts +28 -0
  21. package/lib/src/chat/identity.js +55 -0
  22. package/lib/src/chat/invite.d.ts +12 -0
  23. package/lib/src/chat/invite.js +55 -0
  24. package/lib/src/chat/mesh/b2b.d.ts +17 -0
  25. package/lib/src/chat/mesh/b2b.js +50 -0
  26. package/lib/src/chat/mesh/client.d.ts +25 -0
  27. package/lib/src/chat/mesh/client.js +133 -0
  28. package/lib/src/chat/mesh/node.d.ts +12 -0
  29. package/lib/src/chat/mesh/node.js +46 -0
  30. package/lib/src/chat/mesh/presenceAuth.d.ts +19 -0
  31. package/lib/src/chat/mesh/presenceAuth.js +46 -0
  32. package/lib/src/chat/mesh/protocol.d.ts +57 -0
  33. package/lib/src/chat/mesh/protocol.js +8 -0
  34. package/lib/src/chat/mesh/webrtcAddr.d.ts +3 -0
  35. package/lib/src/chat/mesh/webrtcAddr.js +14 -0
  36. package/lib/src/chat/mesh/wire.d.ts +5 -0
  37. package/lib/src/chat/mesh/wire.js +36 -0
  38. package/lib/src/chat/message.d.ts +28 -0
  39. package/lib/src/chat/message.js +92 -0
  40. package/lib/src/chat/publish.d.ts +6 -0
  41. package/lib/src/chat/publish.js +22 -0
  42. package/lib/src/chat/qr.d.ts +9 -0
  43. package/lib/src/chat/qr.js +25 -0
  44. package/lib/src/chat/receipt.d.ts +17 -0
  45. package/lib/src/chat/receipt.js +22 -0
  46. package/lib/src/chat/sessionStore.d.ts +65 -0
  47. package/lib/src/chat/sessionStore.js +138 -0
  48. package/lib/src/chat/transport.d.ts +51 -0
  49. package/lib/src/chat/transport.js +79 -0
  50. package/lib/src/index.d.ts +19 -0
  51. package/lib/src/index.js +18 -0
  52. package/lib/src/memory.d.ts +18 -0
  53. package/lib/src/memory.js +44 -0
  54. package/lib/src/signal/SignalEngine.d.ts +68 -0
  55. package/lib/src/signal/SignalEngine.js +121 -0
  56. package/package.json +43 -19
  57. package/index.d.ts +0 -8
  58. package/index.js +0 -8
  59. package/interfaces/Chat.d.ts +0 -33
  60. package/interfaces/Chat.js +0 -1
  61. package/interfaces/Directory.d.ts +0 -27
  62. package/interfaces/Directory.js +0 -1
  63. package/interfaces/SessionMessage.d.ts +0 -15
  64. package/interfaces/SessionMessage.js +0 -1
  65. package/interfaces/Store.d.ts +0 -29
  66. package/interfaces/Store.js +0 -1
  67. package/lib/SignalDirectory.d.ts +0 -23
  68. package/lib/SignalDirectory.js +0 -157
  69. package/lib/SignalProtocolStore.d.ts +0 -46
  70. package/lib/SignalProtocolStore.js +0 -195
  71. package/lib/helper.d.ts +0 -2
  72. package/lib/helper.js +0 -17
  73. package/lib/helper.test.d.ts +0 -1
  74. package/lib/helper.test.js +0 -10
  75. package/lib/message-helper.d.ts +0 -20
  76. package/lib/message-helper.js +0 -53
@@ -0,0 +1,860 @@
1
+ /* @ts-self-types="./handle_libsignal_wasm.d.ts" */
2
+
3
+ /**
4
+ * One end of a Signal session: a wallet-rooted identity + the in-memory Signal stores.
5
+ */
6
+ export class Account {
7
+ static __wrap(ptr) {
8
+ const obj = Object.create(Account.prototype);
9
+ obj.__wbg_ptr = ptr;
10
+ AccountFinalization.register(obj, obj.__wbg_ptr, obj);
11
+ return obj;
12
+ }
13
+ __destroy_into_raw() {
14
+ const ptr = this.__wbg_ptr;
15
+ this.__wbg_ptr = 0;
16
+ AccountFinalization.unregister(this);
17
+ return ptr;
18
+ }
19
+ free() {
20
+ const ptr = this.__destroy_into_raw();
21
+ wasm.__wbg_account_free(ptr, 0);
22
+ }
23
+ /**
24
+ * Tier 2: the per-conversation labels `remote` deposits at FOR me — `prev ++ current ++ next` (3 × 32),
25
+ * covering a rotation boundary + clock skew. Directional: `from = peer, to = me`. I subscribe to the
26
+ * union of these (over all known contacts) plus my public first-contact slot (`myTopics`).
27
+ * @param {string} remote
28
+ * @param {number} now_ms
29
+ * @returns {Uint8Array}
30
+ */
31
+ convRecvTopics(remote, now_ms) {
32
+ try {
33
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
34
+ const ptr0 = passStringToWasm0(remote, wasm.__wbindgen_export, wasm.__wbindgen_export2);
35
+ const len0 = WASM_VECTOR_LEN;
36
+ wasm.account_convRecvTopics(retptr, this.__wbg_ptr, ptr0, len0, now_ms);
37
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
38
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
39
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
40
+ var r3 = getDataViewMemory0().getInt32(retptr + 4 * 3, true);
41
+ if (r3) {
42
+ throw takeObject(r2);
43
+ }
44
+ var v2 = getArrayU8FromWasm0(r0, r1).slice();
45
+ wasm.__wbindgen_export4(r0, r1 * 1, 1);
46
+ return v2;
47
+ } finally {
48
+ wasm.__wbindgen_add_to_stack_pointer(16);
49
+ }
50
+ }
51
+ /**
52
+ * Tier 2: the per-conversation label I deposit a message to `remote` at (current epoch only — the peer
53
+ * subscribes prev/cur/next). Directional: `from = me, to = peer`. Unguessable to anyone without one of
54
+ * the two identity private keys.
55
+ * @param {string} remote
56
+ * @param {number} now_ms
57
+ * @returns {Uint8Array}
58
+ */
59
+ convSendTopic(remote, now_ms) {
60
+ try {
61
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
62
+ const ptr0 = passStringToWasm0(remote, wasm.__wbindgen_export, wasm.__wbindgen_export2);
63
+ const len0 = WASM_VECTOR_LEN;
64
+ wasm.account_convSendTopic(retptr, this.__wbg_ptr, ptr0, len0, now_ms);
65
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
66
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
67
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
68
+ var r3 = getDataViewMemory0().getInt32(retptr + 4 * 3, true);
69
+ if (r3) {
70
+ throw takeObject(r2);
71
+ }
72
+ var v2 = getArrayU8FromWasm0(r0, r1).slice();
73
+ wasm.__wbindgen_export4(r0, r1 * 1, 1);
74
+ return v2;
75
+ } finally {
76
+ wasm.__wbindgen_add_to_stack_pointer(16);
77
+ }
78
+ }
79
+ /**
80
+ * Generate + store a signed pre-key and a Kyber (PQXDH) pre-key, and return the serialized
81
+ * publishable bundle (this is what goes in the on-chain anchor). Signed-pre-key-only: no
82
+ * one-time pre-key, so the same anchor serves every sender.
83
+ * @returns {Uint8Array}
84
+ */
85
+ createBundle() {
86
+ try {
87
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
88
+ wasm.account_createBundle(retptr, this.__wbg_ptr);
89
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
90
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
91
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
92
+ var r3 = getDataViewMemory0().getInt32(retptr + 4 * 3, true);
93
+ if (r3) {
94
+ throw takeObject(r2);
95
+ }
96
+ var v1 = getArrayU8FromWasm0(r0, r1).slice();
97
+ wasm.__wbindgen_export4(r0, r1 * 1, 1);
98
+ return v1;
99
+ } finally {
100
+ wasm.__wbindgen_add_to_stack_pointer(16);
101
+ }
102
+ }
103
+ /**
104
+ * Decrypt a message from `remote` (the `[type_code] ++ body` produced by `encrypt`).
105
+ * @param {string} remote
106
+ * @param {Uint8Array} msg
107
+ * @returns {Uint8Array}
108
+ */
109
+ decrypt(remote, msg) {
110
+ try {
111
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
112
+ const ptr0 = passStringToWasm0(remote, wasm.__wbindgen_export, wasm.__wbindgen_export2);
113
+ const len0 = WASM_VECTOR_LEN;
114
+ const ptr1 = passArray8ToWasm0(msg, wasm.__wbindgen_export);
115
+ const len1 = WASM_VECTOR_LEN;
116
+ wasm.account_decrypt(retptr, this.__wbg_ptr, ptr0, len0, ptr1, len1);
117
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
118
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
119
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
120
+ var r3 = getDataViewMemory0().getInt32(retptr + 4 * 3, true);
121
+ if (r3) {
122
+ throw takeObject(r2);
123
+ }
124
+ var v3 = getArrayU8FromWasm0(r0, r1).slice();
125
+ wasm.__wbindgen_export4(r0, r1 * 1, 1);
126
+ return v3;
127
+ } finally {
128
+ wasm.__wbindgen_add_to_stack_pointer(16);
129
+ }
130
+ }
131
+ /**
132
+ * Encrypt to `remote`. Returns `[type_code]` (1 = prekey, 2 = whisper) followed by the
133
+ * serialized Signal ciphertext.
134
+ * @param {string} remote
135
+ * @param {Uint8Array} plaintext
136
+ * @param {number} now_ms
137
+ * @returns {Uint8Array}
138
+ */
139
+ encrypt(remote, plaintext, now_ms) {
140
+ try {
141
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
142
+ const ptr0 = passStringToWasm0(remote, wasm.__wbindgen_export, wasm.__wbindgen_export2);
143
+ const len0 = WASM_VECTOR_LEN;
144
+ const ptr1 = passArray8ToWasm0(plaintext, wasm.__wbindgen_export);
145
+ const len1 = WASM_VECTOR_LEN;
146
+ wasm.account_encrypt(retptr, this.__wbg_ptr, ptr0, len0, ptr1, len1, now_ms);
147
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
148
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
149
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
150
+ var r3 = getDataViewMemory0().getInt32(retptr + 4 * 3, true);
151
+ if (r3) {
152
+ throw takeObject(r2);
153
+ }
154
+ var v3 = getArrayU8FromWasm0(r0, r1).slice();
155
+ wasm.__wbindgen_export4(r0, r1 * 1, 1);
156
+ return v3;
157
+ } finally {
158
+ wasm.__wbindgen_add_to_stack_pointer(16);
159
+ }
160
+ }
161
+ /**
162
+ * Serialize the per-device session state for persistence: the Double-Ratchet `SessionRecord` and
163
+ * learned identity key for each known `remote`, plus our own signed + Kyber pre-keys (so a peer who
164
+ * processed our published anchor can still first-contact us after a reopen). The identity key pair
165
+ * itself is NOT included — it is re-derived from the wallet on the next sign-in, never stored. The
166
+ * caller encrypts this blob at rest under a wallet-derived key.
167
+ * @param {string[]} remotes
168
+ * @returns {Uint8Array}
169
+ */
170
+ exportState(remotes) {
171
+ try {
172
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
173
+ const ptr0 = passArrayJsValueToWasm0(remotes, wasm.__wbindgen_export);
174
+ const len0 = WASM_VECTOR_LEN;
175
+ wasm.account_exportState(retptr, this.__wbg_ptr, ptr0, len0);
176
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
177
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
178
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
179
+ var r3 = getDataViewMemory0().getInt32(retptr + 4 * 3, true);
180
+ if (r3) {
181
+ throw takeObject(r2);
182
+ }
183
+ var v2 = getArrayU8FromWasm0(r0, r1).slice();
184
+ wasm.__wbindgen_export4(r0, r1 * 1, 1);
185
+ return v2;
186
+ } finally {
187
+ wasm.__wbindgen_add_to_stack_pointer(16);
188
+ }
189
+ }
190
+ /**
191
+ * Build an account from a 32-byte identity private key (derived in JS from the wallet/seed)
192
+ * plus a deterministic registration id. The key doubles as the Signal identity key.
193
+ * @param {string} handle
194
+ * @param {Uint8Array} identity_priv
195
+ * @param {number} registration_id
196
+ * @returns {Account}
197
+ */
198
+ static fromIdentity(handle, identity_priv, registration_id) {
199
+ try {
200
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
201
+ const ptr0 = passStringToWasm0(handle, wasm.__wbindgen_export, wasm.__wbindgen_export2);
202
+ const len0 = WASM_VECTOR_LEN;
203
+ const ptr1 = passArray8ToWasm0(identity_priv, wasm.__wbindgen_export);
204
+ const len1 = WASM_VECTOR_LEN;
205
+ wasm.account_fromIdentity(retptr, ptr0, len0, ptr1, len1, registration_id);
206
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
207
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
208
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
209
+ if (r2) {
210
+ throw takeObject(r1);
211
+ }
212
+ return Account.__wrap(r0);
213
+ } finally {
214
+ wasm.__wbindgen_add_to_stack_pointer(16);
215
+ }
216
+ }
217
+ /**
218
+ * Restore state produced by `exportState` into a freshly-created account (same wallet identity). Idempotent
219
+ * per record; re-running just overwrites with the same bytes.
220
+ * @param {Uint8Array} blob
221
+ */
222
+ importState(blob) {
223
+ try {
224
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
225
+ const ptr0 = passArray8ToWasm0(blob, wasm.__wbindgen_export);
226
+ const len0 = WASM_VECTOR_LEN;
227
+ wasm.account_importState(retptr, this.__wbg_ptr, ptr0, len0);
228
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
229
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
230
+ if (r1) {
231
+ throw takeObject(r0);
232
+ }
233
+ } finally {
234
+ wasm.__wbindgen_add_to_stack_pointer(16);
235
+ }
236
+ }
237
+ /**
238
+ * My routing topics for the current epoch and its neighbours (previous + next), so I keep receiving
239
+ * across a rotation boundary + minor clock skew. The relay never sees my handle — only these opaque,
240
+ * rotating labels. Returns `prev ++ current ++ next` (3 × 32 bytes). A peer deposits at my CURRENT
241
+ * label via `peerTopic`.
242
+ * @param {number} now_ms
243
+ * @returns {Uint8Array}
244
+ */
245
+ myTopics(now_ms) {
246
+ try {
247
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
248
+ wasm.account_myTopics(retptr, this.__wbg_ptr, now_ms);
249
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
250
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
251
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
252
+ var r3 = getDataViewMemory0().getInt32(retptr + 4 * 3, true);
253
+ if (r3) {
254
+ throw takeObject(r2);
255
+ }
256
+ var v1 = getArrayU8FromWasm0(r0, r1).slice();
257
+ wasm.__wbindgen_export4(r0, r1 * 1, 1);
258
+ return v1;
259
+ } finally {
260
+ wasm.__wbindgen_add_to_stack_pointer(16);
261
+ }
262
+ }
263
+ /**
264
+ * The peer's identity public key as authenticated by their established session (33 bytes), or `None` if
265
+ * no session/identity for `remote`. This is the key the ratchet + libsignal's trusted-identity gate
266
+ * vouch for — the caller uses it as the per-conversation TRUSTED key (verify-once-then-warn): it survives
267
+ * a reopen via the persisted session state, independent of the live (mutable) on-chain anchor.
268
+ * @param {string} remote
269
+ * @returns {Uint8Array | undefined}
270
+ */
271
+ peerIdentityKey(remote) {
272
+ try {
273
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
274
+ const ptr0 = passStringToWasm0(remote, wasm.__wbindgen_export, wasm.__wbindgen_export2);
275
+ const len0 = WASM_VECTOR_LEN;
276
+ wasm.account_peerIdentityKey(retptr, this.__wbg_ptr, ptr0, len0);
277
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
278
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
279
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
280
+ var r3 = getDataViewMemory0().getInt32(retptr + 4 * 3, true);
281
+ if (r3) {
282
+ throw takeObject(r2);
283
+ }
284
+ let v2;
285
+ if (r0 !== 0) {
286
+ v2 = getArrayU8FromWasm0(r0, r1).slice();
287
+ wasm.__wbindgen_export4(r0, r1 * 1, 1);
288
+ }
289
+ return v2;
290
+ } finally {
291
+ wasm.__wbindgen_add_to_stack_pointer(16);
292
+ }
293
+ }
294
+ /**
295
+ * A peer's CURRENT-epoch routing topic — the label I deposit a message at. Derived from the peer's
296
+ * identity key (held from their bundle or an inbound message) + the current epoch. Requires an
297
+ * established session/identity for `remote`.
298
+ * @param {string} remote
299
+ * @param {number} now_ms
300
+ * @returns {Uint8Array}
301
+ */
302
+ peerTopic(remote, now_ms) {
303
+ try {
304
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
305
+ const ptr0 = passStringToWasm0(remote, wasm.__wbindgen_export, wasm.__wbindgen_export2);
306
+ const len0 = WASM_VECTOR_LEN;
307
+ wasm.account_peerTopic(retptr, this.__wbg_ptr, ptr0, len0, now_ms);
308
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
309
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
310
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
311
+ var r3 = getDataViewMemory0().getInt32(retptr + 4 * 3, true);
312
+ if (r3) {
313
+ throw takeObject(r2);
314
+ }
315
+ var v2 = getArrayU8FromWasm0(r0, r1).slice();
316
+ wasm.__wbindgen_export4(r0, r1 * 1, 1);
317
+ return v2;
318
+ } finally {
319
+ wasm.__wbindgen_add_to_stack_pointer(16);
320
+ }
321
+ }
322
+ /**
323
+ * My presence-auth Ed25519 PUBLIC key (32 bytes). Goes in the published bundle so first-contact senders
324
+ * derive my public slot `HKDF(presence_auth_pub, epoch)`; the helper verifies my registrations against it.
325
+ * @returns {Uint8Array}
326
+ */
327
+ presenceAuthPub() {
328
+ try {
329
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
330
+ wasm.account_presenceAuthPub(retptr, this.__wbg_ptr);
331
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
332
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
333
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
334
+ var r3 = getDataViewMemory0().getInt32(retptr + 4 * 3, true);
335
+ if (r3) {
336
+ throw takeObject(r2);
337
+ }
338
+ var v1 = getArrayU8FromWasm0(r0, r1).slice();
339
+ wasm.__wbindgen_export4(r0, r1 * 1, 1);
340
+ return v1;
341
+ } finally {
342
+ wasm.__wbindgen_add_to_stack_pointer(16);
343
+ }
344
+ }
345
+ /**
346
+ * Process a peer's serialized bundle, establishing an outbound session to `remote`.
347
+ * @param {string} remote
348
+ * @param {Uint8Array} bundle
349
+ * @param {number} now_ms
350
+ */
351
+ processBundle(remote, bundle, now_ms) {
352
+ try {
353
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
354
+ const ptr0 = passStringToWasm0(remote, wasm.__wbindgen_export, wasm.__wbindgen_export2);
355
+ const len0 = WASM_VECTOR_LEN;
356
+ const ptr1 = passArray8ToWasm0(bundle, wasm.__wbindgen_export);
357
+ const len1 = WASM_VECTOR_LEN;
358
+ wasm.account_processBundle(retptr, this.__wbg_ptr, ptr0, len0, ptr1, len1, now_ms);
359
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
360
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
361
+ if (r1) {
362
+ throw takeObject(r0);
363
+ }
364
+ } finally {
365
+ wasm.__wbindgen_add_to_stack_pointer(16);
366
+ }
367
+ }
368
+ /**
369
+ * A PEER's current-epoch public first-contact slot from their presence-auth pub key (in their bundle):
370
+ * `HKDF(peer_presence_auth_pub, epoch)`. Lets a first-contact sender address the peer's public slot.
371
+ * @param {Uint8Array} pa_pub
372
+ * @param {number} now_ms
373
+ * @returns {Uint8Array}
374
+ */
375
+ publicSlotTopicFor(pa_pub, now_ms) {
376
+ try {
377
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
378
+ const ptr0 = passArray8ToWasm0(pa_pub, wasm.__wbindgen_export);
379
+ const len0 = WASM_VECTOR_LEN;
380
+ wasm.account_publicSlotTopicFor(retptr, this.__wbg_ptr, ptr0, len0, now_ms);
381
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
382
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
383
+ var v2 = getArrayU8FromWasm0(r0, r1).slice();
384
+ wasm.__wbindgen_export4(r0, r1 * 1, 1);
385
+ return v2;
386
+ } finally {
387
+ wasm.__wbindgen_add_to_stack_pointer(16);
388
+ }
389
+ }
390
+ /**
391
+ * My PUBLIC first-contact slot labels for prev/cur/next epoch (3 × 32 bytes) — `HKDF(presence_auth_pub,
392
+ * epoch)`. Keyed by the presence-auth PUBLIC key (in my bundle) so the helper can bind a registration to
393
+ * it; a first-contact sender derives the same from my bundle. Replaces the identity-key-derived public
394
+ * slot once wired through.
395
+ * @param {number} now_ms
396
+ * @returns {Uint8Array}
397
+ */
398
+ publicSlotTopics(now_ms) {
399
+ try {
400
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
401
+ wasm.account_publicSlotTopics(retptr, this.__wbg_ptr, now_ms);
402
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
403
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
404
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
405
+ var r3 = getDataViewMemory0().getInt32(retptr + 4 * 3, true);
406
+ if (r3) {
407
+ throw takeObject(r2);
408
+ }
409
+ var v1 = getArrayU8FromWasm0(r0, r1).slice();
410
+ wasm.__wbindgen_export4(r0, r1 * 1, 1);
411
+ return v1;
412
+ } finally {
413
+ wasm.__wbindgen_add_to_stack_pointer(16);
414
+ }
415
+ }
416
+ /**
417
+ * Sealed-sender decrypt. Opens the outer seal with our identity key ALONE (no sender hint on the
418
+ * wire), recovers the sender's handle + claimed identity key from the sealed certificate, decrypts
419
+ * the inner Signal message (which authenticates the sender via the ratchet), and enforces that the
420
+ * certificate's key equals the session-authenticated identity. The caller still cross-checks
421
+ * handle↔key against the on-chain anchor — that is the chain-as-CA trust decision.
422
+ * @param {Uint8Array} ciphertext
423
+ * @param {number} now_ms
424
+ * @returns {SealedIncoming}
425
+ */
426
+ sealedReceive(ciphertext, now_ms) {
427
+ try {
428
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
429
+ const ptr0 = passArray8ToWasm0(ciphertext, wasm.__wbindgen_export);
430
+ const len0 = WASM_VECTOR_LEN;
431
+ wasm.account_sealedReceive(retptr, this.__wbg_ptr, ptr0, len0, now_ms);
432
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
433
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
434
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
435
+ if (r2) {
436
+ throw takeObject(r1);
437
+ }
438
+ return SealedIncoming.__wrap(r0);
439
+ } finally {
440
+ wasm.__wbindgen_add_to_stack_pointer(16);
441
+ }
442
+ }
443
+ /**
444
+ * Sealed-sender encrypt to `remote`. The sender's handle rides inside an authenticated, sealed
445
+ * `UnidentifiedSenderMessageContent` — never on the wire — so a relay learns only the ciphertext.
446
+ * The certificate is self-signed by our identity key (chain-rooted, no Signal server). Returns the
447
+ * envelope body.
448
+ * @param {string} remote
449
+ * @param {Uint8Array} plaintext
450
+ * @param {number} now_ms
451
+ * @returns {Uint8Array}
452
+ */
453
+ sealedSend(remote, plaintext, now_ms) {
454
+ try {
455
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
456
+ const ptr0 = passStringToWasm0(remote, wasm.__wbindgen_export, wasm.__wbindgen_export2);
457
+ const len0 = WASM_VECTOR_LEN;
458
+ const ptr1 = passArray8ToWasm0(plaintext, wasm.__wbindgen_export);
459
+ const len1 = WASM_VECTOR_LEN;
460
+ wasm.account_sealedSend(retptr, this.__wbg_ptr, ptr0, len0, ptr1, len1, now_ms);
461
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
462
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
463
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
464
+ var r3 = getDataViewMemory0().getInt32(retptr + 4 * 3, true);
465
+ if (r3) {
466
+ throw takeObject(r2);
467
+ }
468
+ var v3 = getArrayU8FromWasm0(r0, r1).slice();
469
+ wasm.__wbindgen_export4(r0, r1 * 1, 1);
470
+ return v3;
471
+ } finally {
472
+ wasm.__wbindgen_add_to_stack_pointer(16);
473
+ }
474
+ }
475
+ /**
476
+ * Sign a presence registration: a standard Ed25519 signature (64 bytes) over `topic ‖ addr ‖ epoch_le`.
477
+ * The helper checks `topic == HKDF(presence_auth_pub, epoch)` and verifies this signature — so only the
478
+ * holder of the identity (hence the presence-auth key) can register the public slot (audit J).
479
+ * @param {Uint8Array} topic
480
+ * @param {string} addr
481
+ * @param {number} epoch
482
+ * @returns {Uint8Array}
483
+ */
484
+ signPresence(topic, addr, epoch) {
485
+ try {
486
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
487
+ const ptr0 = passArray8ToWasm0(topic, wasm.__wbindgen_export);
488
+ const len0 = WASM_VECTOR_LEN;
489
+ const ptr1 = passStringToWasm0(addr, wasm.__wbindgen_export, wasm.__wbindgen_export2);
490
+ const len1 = WASM_VECTOR_LEN;
491
+ wasm.account_signPresence(retptr, this.__wbg_ptr, ptr0, len0, ptr1, len1, epoch);
492
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
493
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
494
+ var r2 = getDataViewMemory0().getInt32(retptr + 4 * 2, true);
495
+ var r3 = getDataViewMemory0().getInt32(retptr + 4 * 3, true);
496
+ if (r3) {
497
+ throw takeObject(r2);
498
+ }
499
+ var v3 = getArrayU8FromWasm0(r0, r1).slice();
500
+ wasm.__wbindgen_export4(r0, r1 * 1, 1);
501
+ return v3;
502
+ } finally {
503
+ wasm.__wbindgen_add_to_stack_pointer(16);
504
+ }
505
+ }
506
+ }
507
+ if (Symbol.dispose) Account.prototype[Symbol.dispose] = Account.prototype.free;
508
+
509
+ /**
510
+ * What `sealedReceive` returns: the recovered sender handle, the certificate identity key (33-byte
511
+ * libsignal serialization, for the caller's anchor cross-check), and the decrypted plaintext.
512
+ */
513
+ export class SealedIncoming {
514
+ static __wrap(ptr) {
515
+ const obj = Object.create(SealedIncoming.prototype);
516
+ obj.__wbg_ptr = ptr;
517
+ SealedIncomingFinalization.register(obj, obj.__wbg_ptr, obj);
518
+ return obj;
519
+ }
520
+ __destroy_into_raw() {
521
+ const ptr = this.__wbg_ptr;
522
+ this.__wbg_ptr = 0;
523
+ SealedIncomingFinalization.unregister(this);
524
+ return ptr;
525
+ }
526
+ free() {
527
+ const ptr = this.__destroy_into_raw();
528
+ wasm.__wbg_sealedincoming_free(ptr, 0);
529
+ }
530
+ /**
531
+ * @returns {Uint8Array}
532
+ */
533
+ get identityKey() {
534
+ try {
535
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
536
+ wasm.sealedincoming_identityKey(retptr, this.__wbg_ptr);
537
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
538
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
539
+ var v1 = getArrayU8FromWasm0(r0, r1).slice();
540
+ wasm.__wbindgen_export4(r0, r1 * 1, 1);
541
+ return v1;
542
+ } finally {
543
+ wasm.__wbindgen_add_to_stack_pointer(16);
544
+ }
545
+ }
546
+ /**
547
+ * @returns {Uint8Array}
548
+ */
549
+ get plaintext() {
550
+ try {
551
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
552
+ wasm.sealedincoming_plaintext(retptr, this.__wbg_ptr);
553
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
554
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
555
+ var v1 = getArrayU8FromWasm0(r0, r1).slice();
556
+ wasm.__wbindgen_export4(r0, r1 * 1, 1);
557
+ return v1;
558
+ } finally {
559
+ wasm.__wbindgen_add_to_stack_pointer(16);
560
+ }
561
+ }
562
+ /**
563
+ * @returns {string}
564
+ */
565
+ get sender() {
566
+ let deferred1_0;
567
+ let deferred1_1;
568
+ try {
569
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
570
+ wasm.sealedincoming_sender(retptr, this.__wbg_ptr);
571
+ var r0 = getDataViewMemory0().getInt32(retptr + 4 * 0, true);
572
+ var r1 = getDataViewMemory0().getInt32(retptr + 4 * 1, true);
573
+ deferred1_0 = r0;
574
+ deferred1_1 = r1;
575
+ return getStringFromWasm0(r0, r1);
576
+ } finally {
577
+ wasm.__wbindgen_add_to_stack_pointer(16);
578
+ wasm.__wbindgen_export4(deferred1_0, deferred1_1, 1);
579
+ }
580
+ }
581
+ }
582
+ if (Symbol.dispose) SealedIncoming.prototype[Symbol.dispose] = SealedIncoming.prototype.free;
583
+ function __wbg_get_imports() {
584
+ const import0 = {
585
+ __proto__: null,
586
+ __wbg_Error_fdd633d4bb5dd76a: function(arg0, arg1) {
587
+ const ret = Error(getStringFromWasm0(arg0, arg1));
588
+ return addHeapObject(ret);
589
+ },
590
+ __wbg___wbindgen_string_get_71bb4348194e31f0: function(arg0, arg1) {
591
+ const obj = getObject(arg1);
592
+ const ret = typeof(obj) === 'string' ? obj : undefined;
593
+ var ptr1 = isLikeNone(ret) ? 0 : passStringToWasm0(ret, wasm.__wbindgen_export, wasm.__wbindgen_export2);
594
+ var len1 = WASM_VECTOR_LEN;
595
+ getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
596
+ getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
597
+ },
598
+ __wbg___wbindgen_throw_ea4887a5f8f9a9db: function(arg0, arg1) {
599
+ throw new Error(getStringFromWasm0(arg0, arg1));
600
+ },
601
+ __wbg_getRandomValues_3f44b700395062e5: function() { return handleError(function (arg0, arg1) {
602
+ globalThis.crypto.getRandomValues(getArrayU8FromWasm0(arg0, arg1));
603
+ }, arguments); },
604
+ __wbindgen_object_drop_ref: function(arg0) {
605
+ takeObject(arg0);
606
+ },
607
+ };
608
+ return {
609
+ __proto__: null,
610
+ "./handle_libsignal_wasm_bg.js": import0,
611
+ };
612
+ }
613
+
614
+ const AccountFinalization = (typeof FinalizationRegistry === 'undefined')
615
+ ? { register: () => {}, unregister: () => {} }
616
+ : new FinalizationRegistry(ptr => wasm.__wbg_account_free(ptr, 1));
617
+ const SealedIncomingFinalization = (typeof FinalizationRegistry === 'undefined')
618
+ ? { register: () => {}, unregister: () => {} }
619
+ : new FinalizationRegistry(ptr => wasm.__wbg_sealedincoming_free(ptr, 1));
620
+
621
+ function addHeapObject(obj) {
622
+ if (heap_next === heap.length) heap.push(heap.length + 1);
623
+ const idx = heap_next;
624
+ heap_next = heap[idx];
625
+
626
+ heap[idx] = obj;
627
+ return idx;
628
+ }
629
+
630
+ function dropObject(idx) {
631
+ if (idx < 1028) return;
632
+ heap[idx] = heap_next;
633
+ heap_next = idx;
634
+ }
635
+
636
+ function getArrayU8FromWasm0(ptr, len) {
637
+ ptr = ptr >>> 0;
638
+ return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len);
639
+ }
640
+
641
+ let cachedDataViewMemory0 = null;
642
+ function getDataViewMemory0() {
643
+ if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) {
644
+ cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
645
+ }
646
+ return cachedDataViewMemory0;
647
+ }
648
+
649
+ function getStringFromWasm0(ptr, len) {
650
+ return decodeText(ptr >>> 0, len);
651
+ }
652
+
653
+ let cachedUint8ArrayMemory0 = null;
654
+ function getUint8ArrayMemory0() {
655
+ if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
656
+ cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
657
+ }
658
+ return cachedUint8ArrayMemory0;
659
+ }
660
+
661
+ function getObject(idx) { return heap[idx]; }
662
+
663
+ function handleError(f, args) {
664
+ try {
665
+ return f.apply(this, args);
666
+ } catch (e) {
667
+ wasm.__wbindgen_export3(addHeapObject(e));
668
+ }
669
+ }
670
+
671
+ let heap = new Array(1024).fill(undefined);
672
+ heap.push(undefined, null, true, false);
673
+
674
+ let heap_next = heap.length;
675
+
676
+ function isLikeNone(x) {
677
+ return x === undefined || x === null;
678
+ }
679
+
680
+ function passArray8ToWasm0(arg, malloc) {
681
+ const ptr = malloc(arg.length * 1, 1) >>> 0;
682
+ getUint8ArrayMemory0().set(arg, ptr / 1);
683
+ WASM_VECTOR_LEN = arg.length;
684
+ return ptr;
685
+ }
686
+
687
+ function passArrayJsValueToWasm0(array, malloc) {
688
+ const ptr = malloc(array.length * 4, 4) >>> 0;
689
+ const mem = getDataViewMemory0();
690
+ for (let i = 0; i < array.length; i++) {
691
+ mem.setUint32(ptr + 4 * i, addHeapObject(array[i]), true);
692
+ }
693
+ WASM_VECTOR_LEN = array.length;
694
+ return ptr;
695
+ }
696
+
697
+ function passStringToWasm0(arg, malloc, realloc) {
698
+ if (realloc === undefined) {
699
+ const buf = cachedTextEncoder.encode(arg);
700
+ const ptr = malloc(buf.length, 1) >>> 0;
701
+ getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
702
+ WASM_VECTOR_LEN = buf.length;
703
+ return ptr;
704
+ }
705
+
706
+ let len = arg.length;
707
+ let ptr = malloc(len, 1) >>> 0;
708
+
709
+ const mem = getUint8ArrayMemory0();
710
+
711
+ let offset = 0;
712
+
713
+ for (; offset < len; offset++) {
714
+ const code = arg.charCodeAt(offset);
715
+ if (code > 0x7F) break;
716
+ mem[ptr + offset] = code;
717
+ }
718
+ if (offset !== len) {
719
+ if (offset !== 0) {
720
+ arg = arg.slice(offset);
721
+ }
722
+ ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
723
+ const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
724
+ const ret = cachedTextEncoder.encodeInto(arg, view);
725
+
726
+ offset += ret.written;
727
+ ptr = realloc(ptr, len, offset, 1) >>> 0;
728
+ }
729
+
730
+ WASM_VECTOR_LEN = offset;
731
+ return ptr;
732
+ }
733
+
734
+ function takeObject(idx) {
735
+ const ret = getObject(idx);
736
+ dropObject(idx);
737
+ return ret;
738
+ }
739
+
740
+ let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
741
+ cachedTextDecoder.decode();
742
+ const MAX_SAFARI_DECODE_BYTES = 2146435072;
743
+ let numBytesDecoded = 0;
744
+ function decodeText(ptr, len) {
745
+ numBytesDecoded += len;
746
+ if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
747
+ cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
748
+ cachedTextDecoder.decode();
749
+ numBytesDecoded = len;
750
+ }
751
+ return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
752
+ }
753
+
754
+ const cachedTextEncoder = new TextEncoder();
755
+
756
+ if (!('encodeInto' in cachedTextEncoder)) {
757
+ cachedTextEncoder.encodeInto = function (arg, view) {
758
+ const buf = cachedTextEncoder.encode(arg);
759
+ view.set(buf);
760
+ return {
761
+ read: arg.length,
762
+ written: buf.length
763
+ };
764
+ };
765
+ }
766
+
767
+ let WASM_VECTOR_LEN = 0;
768
+
769
+ let wasmModule, wasmInstance, wasm;
770
+ function __wbg_finalize_init(instance, module) {
771
+ wasmInstance = instance;
772
+ wasm = instance.exports;
773
+ wasmModule = module;
774
+ cachedDataViewMemory0 = null;
775
+ cachedUint8ArrayMemory0 = null;
776
+ return wasm;
777
+ }
778
+
779
+ async function __wbg_load(module, imports) {
780
+ if (typeof Response === 'function' && module instanceof Response) {
781
+ if (typeof WebAssembly.instantiateStreaming === 'function') {
782
+ try {
783
+ return await WebAssembly.instantiateStreaming(module, imports);
784
+ } catch (e) {
785
+ const validResponse = module.ok && expectedResponseType(module.type);
786
+
787
+ if (validResponse && module.headers.get('Content-Type') !== 'application/wasm') {
788
+ console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
789
+
790
+ } else { throw e; }
791
+ }
792
+ }
793
+
794
+ const bytes = await module.arrayBuffer();
795
+ return await WebAssembly.instantiate(bytes, imports);
796
+ } else {
797
+ const instance = await WebAssembly.instantiate(module, imports);
798
+
799
+ if (instance instanceof WebAssembly.Instance) {
800
+ return { instance, module };
801
+ } else {
802
+ return instance;
803
+ }
804
+ }
805
+
806
+ function expectedResponseType(type) {
807
+ switch (type) {
808
+ case 'basic': case 'cors': case 'default': return true;
809
+ }
810
+ return false;
811
+ }
812
+ }
813
+
814
+ function initSync(module) {
815
+ if (wasm !== undefined) return wasm;
816
+
817
+
818
+ if (module !== undefined) {
819
+ if (Object.getPrototypeOf(module) === Object.prototype) {
820
+ ({module} = module)
821
+ } else {
822
+ console.warn('using deprecated parameters for `initSync()`; pass a single object instead')
823
+ }
824
+ }
825
+
826
+ const imports = __wbg_get_imports();
827
+ if (!(module instanceof WebAssembly.Module)) {
828
+ module = new WebAssembly.Module(module);
829
+ }
830
+ const instance = new WebAssembly.Instance(module, imports);
831
+ return __wbg_finalize_init(instance, module);
832
+ }
833
+
834
+ async function __wbg_init(module_or_path) {
835
+ if (wasm !== undefined) return wasm;
836
+
837
+
838
+ if (module_or_path !== undefined) {
839
+ if (Object.getPrototypeOf(module_or_path) === Object.prototype) {
840
+ ({module_or_path} = module_or_path)
841
+ } else {
842
+ console.warn('using deprecated parameters for the initialization function; pass a single object instead')
843
+ }
844
+ }
845
+
846
+ if (module_or_path === undefined) {
847
+ module_or_path = new URL('handle_libsignal_wasm_bg.wasm', import.meta.url);
848
+ }
849
+ const imports = __wbg_get_imports();
850
+
851
+ if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) {
852
+ module_or_path = fetch(module_or_path);
853
+ }
854
+
855
+ const { instance, module } = await __wbg_load(await module_or_path, imports);
856
+
857
+ return __wbg_finalize_init(instance, module);
858
+ }
859
+
860
+ export { initSync, __wbg_init as default };