@koralabs/handle-chat-sdk 0.0.13 → 1.1.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 (82) 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 +207 -0
  7. package/lib/libsignal-wasm/pkg/handle_libsignal_wasm.js +945 -0
  8. package/lib/src/chat/ChatService.d.ts +117 -0
  9. package/lib/src/chat/ChatService.js +922 -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 +55 -0
  13. package/lib/src/chat/blob.js +247 -0
  14. package/lib/src/chat/chainDirectory.d.ts +24 -0
  15. package/lib/src/chat/chainDirectory.js +93 -0
  16. package/lib/src/chat/demeter.d.ts +3 -0
  17. package/lib/src/chat/demeter.js +8 -0
  18. package/lib/src/chat/directory.d.ts +24 -0
  19. package/lib/src/chat/directory.js +37 -0
  20. package/lib/src/chat/guardian.d.ts +75 -0
  21. package/lib/src/chat/guardian.js +67 -0
  22. package/lib/src/chat/guardianHistory.d.ts +3 -0
  23. package/lib/src/chat/guardianHistory.js +36 -0
  24. package/lib/src/chat/httpRateLimit.d.ts +5 -0
  25. package/lib/src/chat/httpRateLimit.js +54 -0
  26. package/lib/src/chat/identity.d.ts +28 -0
  27. package/lib/src/chat/identity.js +61 -0
  28. package/lib/src/chat/invite.d.ts +19 -0
  29. package/lib/src/chat/invite.js +73 -0
  30. package/lib/src/chat/mesh/b2b.d.ts +17 -0
  31. package/lib/src/chat/mesh/b2b.js +50 -0
  32. package/lib/src/chat/mesh/client.d.ts +28 -0
  33. package/lib/src/chat/mesh/client.js +209 -0
  34. package/lib/src/chat/mesh/node.d.ts +12 -0
  35. package/lib/src/chat/mesh/node.js +46 -0
  36. package/lib/src/chat/mesh/presenceAuth.d.ts +19 -0
  37. package/lib/src/chat/mesh/presenceAuth.js +46 -0
  38. package/lib/src/chat/mesh/protocol.d.ts +92 -0
  39. package/lib/src/chat/mesh/protocol.js +19 -0
  40. package/lib/src/chat/mesh/webrtcAddr.d.ts +3 -0
  41. package/lib/src/chat/mesh/webrtcAddr.js +14 -0
  42. package/lib/src/chat/mesh/wire.d.ts +5 -0
  43. package/lib/src/chat/mesh/wire.js +36 -0
  44. package/lib/src/chat/message.d.ts +28 -0
  45. package/lib/src/chat/message.js +92 -0
  46. package/lib/src/chat/publish.d.ts +6 -0
  47. package/lib/src/chat/publish.js +33 -0
  48. package/lib/src/chat/qr.d.ts +9 -0
  49. package/lib/src/chat/qr.js +25 -0
  50. package/lib/src/chat/receipt.d.ts +17 -0
  51. package/lib/src/chat/receipt.js +22 -0
  52. package/lib/src/chat/sessionStore.d.ts +74 -0
  53. package/lib/src/chat/sessionStore.js +183 -0
  54. package/lib/src/chat/transport.d.ts +51 -0
  55. package/lib/src/chat/transport.js +139 -0
  56. package/lib/src/index.d.ts +19 -0
  57. package/lib/src/index.js +18 -0
  58. package/lib/src/memory.d.ts +18 -0
  59. package/lib/src/memory.js +44 -0
  60. package/lib/src/signal/SignalEngine.d.ts +73 -0
  61. package/lib/src/signal/SignalEngine.js +131 -0
  62. package/package.json +44 -19
  63. package/index.d.ts +0 -8
  64. package/index.js +0 -8
  65. package/interfaces/Chat.d.ts +0 -33
  66. package/interfaces/Chat.js +0 -1
  67. package/interfaces/Directory.d.ts +0 -27
  68. package/interfaces/Directory.js +0 -1
  69. package/interfaces/SessionMessage.d.ts +0 -15
  70. package/interfaces/SessionMessage.js +0 -1
  71. package/interfaces/Store.d.ts +0 -29
  72. package/interfaces/Store.js +0 -1
  73. package/lib/SignalDirectory.d.ts +0 -23
  74. package/lib/SignalDirectory.js +0 -157
  75. package/lib/SignalProtocolStore.d.ts +0 -46
  76. package/lib/SignalProtocolStore.js +0 -195
  77. package/lib/helper.d.ts +0 -2
  78. package/lib/helper.js +0 -17
  79. package/lib/helper.test.d.ts +0 -1
  80. package/lib/helper.test.js +0 -10
  81. package/lib/message-helper.d.ts +0 -20
  82. package/lib/message-helper.js +0 -53
@@ -0,0 +1,117 @@
1
+ import { type SessionVault } from "./sessionStore";
2
+ import { type ContentMessage } from "./message";
3
+ import { type ReceiptState } from "./receipt";
4
+ import type { AsyncDirectory, Directory } from "./directory";
5
+ import type { Transport } from "./transport";
6
+ /** A delivery/read receipt arriving for one of my sent messages. */
7
+ export interface ReceiptEvent {
8
+ from: string;
9
+ ref: string;
10
+ state: ReceiptState;
11
+ }
12
+ /** A message delivered to the app — typed `content` (any MIME) plus a `text` convenience for the
13
+ * text/plain case (empty for media; use `content` to render images/audio/video/structured payloads). */
14
+ export interface IncomingMessage {
15
+ from: string;
16
+ content: ContentMessage;
17
+ text: string;
18
+ ts: number;
19
+ }
20
+ /** A message as held in memory / handed to the UI for a conversation thread. */
21
+ export interface ThreadMessage {
22
+ from: string;
23
+ content: ContentMessage;
24
+ ts: number;
25
+ }
26
+ /** `anchorDiverged: true` means opening a conversation supplied a bundle different from the pinned key.
27
+ * The open is refused; the existing session stays usable. `false` means an inbound envelope claimed the
28
+ * pinned handle with a different key and was dropped. Neither event replaces the trusted key. */
29
+ export interface KeyChangeEvent {
30
+ handle: string;
31
+ knownKey: Uint8Array;
32
+ newKey: Uint8Array;
33
+ anchorDiverged: boolean;
34
+ }
35
+ export declare const UNVERIFIED_FIRST_CONTACT_LIMIT = 8;
36
+ /** The identity key (chain-as-CA cross-check of a sealed sender's claimed key vs. what the handle publishes). */
37
+ export declare const bundleIdentityKey: (bundle: Uint8Array) => Uint8Array;
38
+ /**
39
+ * Orchestrates one handle's chat: derive the wallet-rooted identity, provision the WASM Signal
40
+ * engine, publish the bundle to the directory, and send/receive E2E messages over the transport.
41
+ * Crypto = libsignal (WASM). Directory/transport are seams (demo: localStorage + BroadcastChannel).
42
+ */
43
+ export declare class ChatService {
44
+ #private;
45
+ readonly handle: string;
46
+ private readonly directory;
47
+ private readonly transport;
48
+ /** Clock for topic-epoch derivation — injectable so tests can cross a rotation boundary. */
49
+ private readonly now;
50
+ private engine;
51
+ private onMessageCb;
52
+ private onReceiptCb;
53
+ private onKeyChangeCb;
54
+ private onSendUpdateCb;
55
+ private started;
56
+ static readonly MAX_HISTORY = 500;
57
+ constructor(handle: string, directory: Directory | AsyncDirectory, transport: Transport,
58
+ /** Where per-device session state persists. Omit for the default (wallet-encrypted IndexedDB);
59
+ * pass `null` to disable persistence; inject a custom vault in tests. */
60
+ vault?: SessionVault | null,
61
+ /** Clock for topic-epoch derivation — injectable so tests can cross a rotation boundary. */
62
+ now?: () => number);
63
+ start(seed: Uint8Array): Promise<void>;
64
+ /** Serialize + encrypt the current session state to the vault. Serialized via a chain so concurrent
65
+ * mutations don't interleave writes. A mailbox consumer must await the transaction. */
66
+ private persist;
67
+ /** Append a message to a conversation's history and persist that thread (capped, chained writes). */
68
+ private record;
69
+ /** Restored conversation history (per remote handle) — for the UI to seed its threads on reopen. */
70
+ loadedHistory(): ReadonlyMap<string, readonly ThreadMessage[]>;
71
+ /** Await any in-flight persistence (session state + history) — so a reopen reads the latest. */
72
+ flush(): Promise<void>;
73
+ /** Tear down timers + the global `online` listener, and stop the transport. Idempotent. Call before
74
+ * discarding the service (e.g. a wallet switch / re-provision) so nothing leaks or fires on a stale engine. */
75
+ stop(): void;
76
+ /** This identity's publishable key-bundle — what goes in an invite (or the chain anchor). */
77
+ myBundle(): Uint8Array;
78
+ /** My wallet-rooted identity public key (libsignal serialization: type byte + 32-byte Curve25519).
79
+ * SSR (secrets.handle.me) hands this out in a guardian-consent — "the key is handed over the medium". */
80
+ myIdentityKey(): Uint8Array;
81
+ /** The identity pinned to this conversation, not a fresh directory lookup or an identity claimed in a message body. */
82
+ pinnedIdentityKey(remote: string): Uint8Array | null;
83
+ /** Open an outbound session to a peer by resolving their published bundle (cold lookup). */
84
+ openChat(remote: string): boolean;
85
+ openChatAsync(remote: string): Promise<boolean>;
86
+ /** Open an outbound session from a bundle handed over directly (invite link / QR — no directory). */
87
+ openChatWithBundle(remote: string, bundle: Uint8Array): void;
88
+ /** Send a plain-text message (convenience over {@link sendContent}). */
89
+ send(remote: string, text: string): void;
90
+ /** Sealed-sender encrypt and send typed content (any MIME) to a peer (must have an open session). The
91
+ * wire envelope carries no sender label — the recipient recovers it from the seal. Returns the message
92
+ * id (stamped into `meta.id`), which delivery/read receipts reference. */
93
+ sendContent(remote: string, content: ContentMessage): string;
94
+ /** Retry queued envelopes in order, stopping at the first the network still won't take (preserves order). */
95
+ flushQueue(): Promise<void>;
96
+ /** Mark a received message read — sends a "read" receipt to its sender. */
97
+ markRead(remote: string, messageId: string): void;
98
+ /** Current delivery state of one of my sent messages ("queued"/"sent" until the peer acks). */
99
+ sentState(messageId: string): "queued" | "sent" | ReceiptState | undefined;
100
+ onReceipt(cb: (r: ReceiptEvent) => void): void;
101
+ /** Register a handler for a conversation's identity key changing after first contact (verify-once-then-warn
102
+ * — the handle was re-anchored / may have changed hands, or a different key tried to claim it). */
103
+ onKeyChange(cb: (e: KeyChangeEvent) => void): void;
104
+ /** Fires when any sent-message state changes (queued/sent/delivered/read) — for the UI to refresh ticks. */
105
+ onSendUpdate(cb: () => void): void;
106
+ /** Send a large blob (image/audio/video/…) by STREAMING it: encrypt + chunk + content-address, then
107
+ * send a small `blob-ref` (tracked, carries the message id) + raw `blob-chunk` messages (reliable, but
108
+ * untracked). The recipient reassembles + verifies + decrypts. Returns the message id. The caller still
109
+ * echoes the full media locally (the sender already has the bytes). */
110
+ sendBlob(remote: string, mime: string, bytes: Uint8Array, name?: string): Promise<string>;
111
+ onMessage(cb: (m: IncomingMessage) => void): void;
112
+ /** Serialize inbound processing: each envelope fully completes (incl. async blob reassembly) before the
113
+ * next, so app-level ordering of history/UI is preserved even when a blob await interleaves. */
114
+ private receive;
115
+ /** Retry in-memory first contacts after a directory outage; provider-imposed waits are never bypassed. */
116
+ retryPendingContacts(): Promise<void>;
117
+ }