@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,73 @@
1
+ import initWasm from "../../libsignal-wasm/pkg/handle_libsignal_wasm.js";
2
+ /**
3
+ * Initialize the libsignal WASM once. Browsers need nothing — the default fetches {@link WASM_URL}
4
+ * from the serving origin. Non-browser hosts (Node tools, the SDK's examples/tests) call this first
5
+ * with the module bytes, since there is no origin to fetch from.
6
+ */
7
+ export declare const initSignalWasm: (input?: Parameters<typeof initWasm>[0]) => Promise<void>;
8
+ export declare class SignalEngine {
9
+ private readonly account;
10
+ readonly handle: string;
11
+ private constructor();
12
+ static create(handle: string, identityPriv: Uint8Array, registrationId: number): Promise<SignalEngine>;
13
+ /** The publishable PQXDH bundle — goes in the on-chain anchor. */
14
+ createBundle(): Uint8Array;
15
+ /** Open an outbound session to a peer from their published bundle. */
16
+ processBundle(remote: string, bundle: Uint8Array): void;
17
+ encrypt(remote: string, plaintext: string): Uint8Array;
18
+ decrypt(remote: string, message: Uint8Array): string;
19
+ /**
20
+ * Sealed-sender encrypt: the sender handle is sealed INSIDE the ciphertext (libsignal native sealed
21
+ * sender), so the envelope this returns carries no sender label for a relay to read.
22
+ */
23
+ sealedSend(remote: string, plaintext: Uint8Array): Uint8Array;
24
+ /** Read an untrusted sender hint without changing the session; authenticate it after decryption. */
25
+ sealedSenderHint(ciphertext: Uint8Array): {
26
+ sender: string;
27
+ identityKey: Uint8Array;
28
+ };
29
+ /**
30
+ * Sealed-sender decrypt: recovers the sender handle + its certificate identity key (for the caller's
31
+ * on-chain anchor cross-check) and the plaintext bytes, opening the seal with our own identity key
32
+ * alone. The crypto layer is bytes-native; the typed-message codec lives above it (see `message.ts`).
33
+ */
34
+ sealedReceive(ciphertext: Uint8Array): {
35
+ sender: string;
36
+ identityKey: Uint8Array;
37
+ body: Uint8Array;
38
+ };
39
+ /**
40
+ * Serialize this device's session state (Double-Ratchet records + learned peer identities + our own
41
+ * pre-keys) for at-rest persistence. The identity key pair is NOT included — it's re-derived from the
42
+ * wallet on the next sign-in. Pass the handles of known contacts (sessions/identities are keyed by them).
43
+ */
44
+ exportState(remotes: string[]): Uint8Array;
45
+ /** Restore state from `exportState` into this (freshly created, same-wallet) engine. */
46
+ importState(blob: Uint8Array): void;
47
+ /** My rotating routing labels (previous + current + next epoch) — what I subscribe under, so the relay
48
+ * never sees my handle and can't track a stable pseudonym. Listening on the neighbours covers a rollover
49
+ * boundary + clock skew. */
50
+ myTopics(now?: number): string[];
51
+ /** A peer's CURRENT-epoch routing label — where I deposit a message (needs an open session to them). */
52
+ peerTopic(remote: string, now?: number): string;
53
+ /** Tier 2: the per-CONVERSATION label I deposit a message to `remote` at (current epoch). Derived from the
54
+ * static-static identity-key agreement, so it's unguessable without one of our private keys — the
55
+ * chain-watcher can't recompute it, and it's bound to the two keys (survives a handle transfer). */
56
+ convSendTopic(remote: string, now?: number): string;
57
+ /** Tier 2: the per-conversation labels `remote` deposits at FOR me (previous + current + next epoch) — I
58
+ * subscribe to the union of these over all known contacts, plus my public first-contact slot (myTopics). */
59
+ convRecvTopics(remote: string, now?: number): string[];
60
+ /** The peer identity key the established session authenticates (the verify-once TRUSTED key), or null if
61
+ * there's no session for `remote`. Survives a reopen via the persisted session, independent of the anchor. */
62
+ peerIdentityKey(remote: string): Uint8Array | null;
63
+ /** Tier 2 Stage 3: my per-identity Ed25519 presence-auth PUBLIC key (goes in my bundle; the helper verifies
64
+ * my public-slot registrations against it). */
65
+ presenceAuthPub(): Uint8Array;
66
+ /** Tier 2 Stage 3: sign a public-slot presence registration (Ed25519 over topic‖addr‖epoch). */
67
+ signPresence(topic: Uint8Array, addr: string, epoch: number): Uint8Array;
68
+ /** Tier 2 Stage 3: my PUBLIC first-contact slot labels (prev/cur/next) = HKDF(presenceAuthPub, epoch). */
69
+ publicSlotTopics(now?: number): string[];
70
+ /** Tier 2 Stage 3: a PEER's current-epoch public first-contact slot from their presence-auth pub key — where
71
+ * I deposit a first-contact message before our pairwise topic exists. */
72
+ publicSlotTopicFor(peerPresenceAuthPub: Uint8Array, now?: number): string;
73
+ }
@@ -0,0 +1,131 @@
1
+ import initWasm, { Account } from "../../libsignal-wasm/pkg/handle_libsignal_wasm.js";
2
+ /**
3
+ * The app's end-to-end crypto seam — the **latest libsignal via WASM** (`../../libsignal-wasm`), and
4
+ * nothing else. No legacy `@privacyresearch` code. The 32-byte identity private key is derived in the
5
+ * app's identity layer from the wallet signature (HKDF→X25519); this just wraps the WASM `Account`.
6
+ */
7
+ const WASM_URL = "/public/libsignal_bg.wasm";
8
+ let ready = null;
9
+ /**
10
+ * Initialize the libsignal WASM once. Browsers need nothing — the default fetches {@link WASM_URL}
11
+ * from the serving origin. Non-browser hosts (Node tools, the SDK's examples/tests) call this first
12
+ * with the module bytes, since there is no origin to fetch from.
13
+ */
14
+ export const initSignalWasm = (input) => (ready ??= initWasm(input ?? WASM_URL).then(() => undefined));
15
+ export class SignalEngine {
16
+ account;
17
+ handle;
18
+ constructor(account, handle) {
19
+ this.account = account;
20
+ this.handle = handle;
21
+ }
22
+ static async create(handle, identityPriv, registrationId) {
23
+ await initSignalWasm();
24
+ return new SignalEngine(Account.fromIdentity(handle, identityPriv, registrationId), handle);
25
+ }
26
+ /** The publishable PQXDH bundle — goes in the on-chain anchor. */
27
+ createBundle() {
28
+ return this.account.createBundle();
29
+ }
30
+ /** Open an outbound session to a peer from their published bundle. */
31
+ processBundle(remote, bundle) {
32
+ this.account.processBundle(remote, bundle, Date.now());
33
+ }
34
+ encrypt(remote, plaintext) {
35
+ return this.account.encrypt(remote, new TextEncoder().encode(plaintext), Date.now());
36
+ }
37
+ decrypt(remote, message) {
38
+ return new TextDecoder().decode(this.account.decrypt(remote, message));
39
+ }
40
+ /**
41
+ * Sealed-sender encrypt: the sender handle is sealed INSIDE the ciphertext (libsignal native sealed
42
+ * sender), so the envelope this returns carries no sender label for a relay to read.
43
+ */
44
+ sealedSend(remote, plaintext) {
45
+ return this.account.sealedSend(remote, plaintext, Date.now());
46
+ }
47
+ /** Read an untrusted sender hint without changing the session; authenticate it after decryption. */
48
+ sealedSenderHint(ciphertext) {
49
+ const hint = this.account.sealedSenderHint(ciphertext, Date.now());
50
+ try {
51
+ return { sender: hint.sender, identityKey: hint.identityKey };
52
+ }
53
+ finally {
54
+ hint.free();
55
+ }
56
+ }
57
+ /**
58
+ * Sealed-sender decrypt: recovers the sender handle + its certificate identity key (for the caller's
59
+ * on-chain anchor cross-check) and the plaintext bytes, opening the seal with our own identity key
60
+ * alone. The crypto layer is bytes-native; the typed-message codec lives above it (see `message.ts`).
61
+ */
62
+ sealedReceive(ciphertext) {
63
+ const r = this.account.sealedReceive(ciphertext, Date.now());
64
+ try {
65
+ return { sender: r.sender, identityKey: r.identityKey, body: r.plaintext };
66
+ }
67
+ finally {
68
+ r.free(); // wasm-bindgen owned struct — release it
69
+ }
70
+ }
71
+ /**
72
+ * Serialize this device's session state (Double-Ratchet records + learned peer identities + our own
73
+ * pre-keys) for at-rest persistence. The identity key pair is NOT included — it's re-derived from the
74
+ * wallet on the next sign-in. Pass the handles of known contacts (sessions/identities are keyed by them).
75
+ */
76
+ exportState(remotes) {
77
+ return this.account.exportState(remotes);
78
+ }
79
+ /** Restore state from `exportState` into this (freshly created, same-wallet) engine. */
80
+ importState(blob) {
81
+ this.account.importState(blob);
82
+ }
83
+ /** My rotating routing labels (previous + current + next epoch) — what I subscribe under, so the relay
84
+ * never sees my handle and can't track a stable pseudonym. Listening on the neighbours covers a rollover
85
+ * boundary + clock skew. */
86
+ myTopics(now = Date.now()) {
87
+ const flat = this.account.myTopics(now);
88
+ return [0, 32, 64].map((o) => toHex(flat.subarray(o, o + 32)));
89
+ }
90
+ /** A peer's CURRENT-epoch routing label — where I deposit a message (needs an open session to them). */
91
+ peerTopic(remote, now = Date.now()) {
92
+ return toHex(this.account.peerTopic(remote, now));
93
+ }
94
+ /** Tier 2: the per-CONVERSATION label I deposit a message to `remote` at (current epoch). Derived from the
95
+ * static-static identity-key agreement, so it's unguessable without one of our private keys — the
96
+ * chain-watcher can't recompute it, and it's bound to the two keys (survives a handle transfer). */
97
+ convSendTopic(remote, now = Date.now()) {
98
+ return toHex(this.account.convSendTopic(remote, now));
99
+ }
100
+ /** Tier 2: the per-conversation labels `remote` deposits at FOR me (previous + current + next epoch) — I
101
+ * subscribe to the union of these over all known contacts, plus my public first-contact slot (myTopics). */
102
+ convRecvTopics(remote, now = Date.now()) {
103
+ const flat = this.account.convRecvTopics(remote, now);
104
+ return [0, 32, 64].map((o) => toHex(flat.subarray(o, o + 32)));
105
+ }
106
+ /** The peer identity key the established session authenticates (the verify-once TRUSTED key), or null if
107
+ * there's no session for `remote`. Survives a reopen via the persisted session, independent of the anchor. */
108
+ peerIdentityKey(remote) {
109
+ return this.account.peerIdentityKey(remote) ?? null;
110
+ }
111
+ /** Tier 2 Stage 3: my per-identity Ed25519 presence-auth PUBLIC key (goes in my bundle; the helper verifies
112
+ * my public-slot registrations against it). */
113
+ presenceAuthPub() {
114
+ return this.account.presenceAuthPub();
115
+ }
116
+ /** Tier 2 Stage 3: sign a public-slot presence registration (Ed25519 over topic‖addr‖epoch). */
117
+ signPresence(topic, addr, epoch) {
118
+ return this.account.signPresence(topic, addr, epoch);
119
+ }
120
+ /** Tier 2 Stage 3: my PUBLIC first-contact slot labels (prev/cur/next) = HKDF(presenceAuthPub, epoch). */
121
+ publicSlotTopics(now = Date.now()) {
122
+ const flat = this.account.publicSlotTopics(now);
123
+ return [0, 32, 64].map((o) => toHex(flat.subarray(o, o + 32)));
124
+ }
125
+ /** Tier 2 Stage 3: a PEER's current-epoch public first-contact slot from their presence-auth pub key — where
126
+ * I deposit a first-contact message before our pairwise topic exists. */
127
+ publicSlotTopicFor(peerPresenceAuthPub, now = Date.now()) {
128
+ return toHex(this.account.publicSlotTopicFor(peerPresenceAuthPub, now));
129
+ }
130
+ }
131
+ const toHex = (b) => Array.from(b, (x) => x.toString(16).padStart(2, "0")).join("");
package/package.json CHANGED
@@ -1,28 +1,46 @@
1
1
  {
2
2
  "name": "@koralabs/handle-chat-sdk",
3
- "version": "0.0.13",
4
- "description": "SDK for handling chat functionalities",
5
- "main": "index.js",
3
+ "version": "1.1.0",
4
+ "description": "Programmatic client for handle-to-handle E2E chat (chat.handle.me): wallet-rooted identities (sign-to-derive), libsignal WASM sessions, transports, invite links, and the SSR guardian payloads.",
6
5
  "type": "module",
6
+ "main": "lib/src/index.js",
7
+ "types": "lib/src/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./lib/src/index.d.ts",
11
+ "default": "./lib/src/index.js"
12
+ },
13
+ "./package.json": "./package.json"
14
+ },
15
+ "files": [
16
+ "lib",
17
+ "assets",
18
+ "examples",
19
+ "README.md"
20
+ ],
7
21
  "scripts": {
8
- "build": "rm -rf lib/ && tsc",
9
- "lint": "npx eslint './src/**/*.{ts,mjs}' --max-warnings=0",
10
- "test": "node --import tsx --test src/**/*.test.ts"
22
+ "build": "node scripts/build.mjs",
23
+ "test": "npm run build && node --test --test-concurrency=2 \"test/**/*.test.mjs\"",
24
+ "examples": "node examples/quickstart.mjs && node examples/guardian-flow.mjs",
25
+ "prepack": "npm run build"
11
26
  },
12
27
  "dependencies": {
13
- "@privacyresearch/libsignal-protocol-typescript": "^0.0.15",
14
- "@signalapp/libsignal-client": "^0.55.0",
15
- "tsx": "^4.20.6",
16
- "uuid": "^9.0.0"
28
+ "@chainsafe/libp2p-noise": "^17.0.0",
29
+ "@chainsafe/libp2p-yamux": "^8.0.1",
30
+ "@libp2p/circuit-relay-v2": "^4.2.7",
31
+ "@libp2p/identify": "^4.1.8",
32
+ "@libp2p/interface": "^3.2.4",
33
+ "@libp2p/webrtc": "^6.0.29",
34
+ "@libp2p/websockets": "^10.1.15",
35
+ "@multiformats/multiaddr": "^13.0.3",
36
+ "libp2p": "^3.3.4",
37
+ "qrcode-generator": "^1.5.2"
17
38
  },
18
39
  "devDependencies": {
19
- "@eslint/js": "^9.39.1",
20
- "@types/node": "^24.7.2",
21
- "@types/uuid": "^10.0.0",
22
- "eslint": "^9.39.1",
23
- "eslint-plugin-simple-import-sort": "^12.1.1",
24
- "globals": "^16.5.0",
25
- "typescript-eslint": "^8.46.3"
40
+ "@types/node": "^22.10.0",
41
+ "@types/qrcode-generator": "^0.0.16",
42
+ "node-datachannel": "^0.32.3",
43
+ "typescript": "^5.7.2"
26
44
  },
27
45
  "keywords": [
28
46
  "messaging",
@@ -30,8 +48,15 @@
30
48
  "handles",
31
49
  "chat",
32
50
  "sdk",
33
- "crypto"
51
+ "libsignal",
52
+ "cardano"
34
53
  ],
35
54
  "author": "",
36
- "license": "MIT"
55
+ "license": "AGPL-3.0-only",
56
+ "engines": {
57
+ "node": ">=26"
58
+ },
59
+ "overrides": {
60
+ "image-size": "2.0.2"
61
+ }
37
62
  }
package/index.d.ts DELETED
@@ -1,8 +0,0 @@
1
- export * from './interfaces/Chat.js';
2
- export * from './interfaces/Directory.js';
3
- export * from './interfaces/SessionMessage.js';
4
- export * from './interfaces/Store.js';
5
- export * from './lib/helper.js';
6
- export * from './lib/message-helper.js';
7
- export * from './lib/SignalDirectory.js';
8
- export * from './lib/SignalProtocolStore.js';
package/index.js DELETED
@@ -1,8 +0,0 @@
1
- export * from './interfaces/Chat.js';
2
- export * from './interfaces/Directory.js';
3
- export * from './interfaces/SessionMessage.js';
4
- export * from './interfaces/Store.js';
5
- export * from './lib/helper.js';
6
- export * from './lib/message-helper.js';
7
- export * from './lib/SignalDirectory.js';
8
- export * from './lib/SignalProtocolStore.js';
@@ -1,33 +0,0 @@
1
- import { MessageType } from "@privacyresearch/libsignal-protocol-typescript";
2
- import { SerializableFullDirectoryEntry } from "./Directory.js";
3
- import { SerializedStoreEntry } from "./Store.js";
4
- export interface ChatUser {
5
- address: string;
6
- name: string;
7
- accepted: boolean;
8
- active: boolean;
9
- chats: ProcessedChatMessage[];
10
- waitingForSession?: boolean;
11
- hasPendingChats?: boolean;
12
- deviceId?: number;
13
- image?: string;
14
- bundle?: SerializableFullDirectoryEntry;
15
- store?: SerializedStoreEntry;
16
- }
17
- export interface ProcessedChatMessage {
18
- id: number;
19
- to: string;
20
- from: string;
21
- messageText: string;
22
- timestamp: number;
23
- failed?: boolean;
24
- }
25
- export interface ChatMessage {
26
- id: number;
27
- to: string;
28
- from: string;
29
- message: MessageType;
30
- delivered: boolean;
31
- timestamp: number;
32
- session_init?: boolean;
33
- }
@@ -1 +0,0 @@
1
- export {};
@@ -1,27 +0,0 @@
1
- import { PreKeyType, SignedPublicPreKeyType } from "@privacyresearch/libsignal-protocol-typescript";
2
- export interface PublicDirectoryEntry {
3
- identityPubKey: ArrayBuffer;
4
- signedPreKey: SignedPublicPreKeyType;
5
- oneTimePreKey?: ArrayBuffer;
6
- }
7
- export interface FullDirectoryEntry {
8
- registrationId: number;
9
- identityPubKey: ArrayBuffer;
10
- signedPreKey: SignedPublicPreKeyType;
11
- oneTimePreKeys: PreKeyType[];
12
- }
13
- export interface SerializablePreKey {
14
- keyId: number;
15
- publicKey: string;
16
- }
17
- export interface SerializableSignedPublicPreKey {
18
- keyId: number;
19
- publicKey: string;
20
- signature: string;
21
- }
22
- export interface SerializableFullDirectoryEntry {
23
- registrationId: number;
24
- identityPubKey: string;
25
- signedPreKey: SerializableSignedPublicPreKey;
26
- oneTimePreKeys: SerializablePreKey[];
27
- }
@@ -1 +0,0 @@
1
- export {};
@@ -1,15 +0,0 @@
1
- import { SerializableFullDirectoryEntry } from "./Directory.js";
2
- import { SerializedStoreEntry } from "./Store.js";
3
- export type SessionMessageType = 'session_create' | 'session_accept' | 'session_deny' | 'session_failed' | 'session_ended';
4
- export interface SessionMessage {
5
- recipientAddress: string;
6
- senderAddress: string;
7
- senderDeviceId: number;
8
- senderBundle: SerializableFullDirectoryEntry;
9
- senderStore: SerializedStoreEntry;
10
- senderName: string;
11
- senderImage: string;
12
- type: SessionMessageType;
13
- timestamp: number;
14
- failedMessageId?: number;
15
- }
@@ -1 +0,0 @@
1
- export {};
@@ -1,29 +0,0 @@
1
- import { KeyPairType, PreKeyPairType, SignedPreKeyPairType } from "@privacyresearch/libsignal-protocol-typescript";
2
- export interface StoreEntry {
3
- registrationId: number;
4
- identityKey: KeyPairType;
5
- preKey: PreKeyPairType;
6
- signedPreKey: SignedPreKeyPairType;
7
- }
8
- export interface SerializedStoreEntry {
9
- registrationId: number;
10
- identityKey: {
11
- pubKey: string;
12
- privKey: string;
13
- };
14
- preKey: {
15
- keyId: number;
16
- keyPair: {
17
- pubKey: string;
18
- privKey: string;
19
- };
20
- };
21
- signedPreKey: {
22
- keyId: number;
23
- keyPair: {
24
- pubKey: string;
25
- privKey: string;
26
- };
27
- signature: string;
28
- };
29
- }
@@ -1 +0,0 @@
1
- export {};
@@ -1,23 +0,0 @@
1
- import { DeviceType, PreKeyType } from "@privacyresearch/libsignal-protocol-typescript";
2
- import { FullDirectoryEntry, SerializableFullDirectoryEntry } from "../interfaces/Directory.js";
3
- import { SerializedStoreEntry, StoreEntry } from "../interfaces/Store.js";
4
- import { SignalProtocolStore } from "./SignalProtocolStore.js";
5
- export declare class SignalDirectory {
6
- private _data;
7
- storeKeyBundle(address: string, bundle: FullDirectoryEntry): void;
8
- addOneTimePreKeys(address: string, keys: PreKeyType[]): void;
9
- getPreKeyBundle(address: string): DeviceType | undefined;
10
- }
11
- export declare const serializeBundle: (bundle: FullDirectoryEntry) => SerializableFullDirectoryEntry;
12
- export declare const deserializeBundle: (serializable: SerializableFullDirectoryEntry) => DeviceType;
13
- export declare const serializeStore: (entry: StoreEntry) => SerializedStoreEntry;
14
- export declare const deserializeStore: (serializedEntry: SerializedStoreEntry) => StoreEntry;
15
- export declare const populateStore: (store: SignalProtocolStore, storeEntry: StoreEntry) => void;
16
- export declare const createStoreBundle: () => Promise<{
17
- bundle: FullDirectoryEntry;
18
- storeEntry: StoreEntry;
19
- }>;
20
- export declare const createID: (directory: SignalDirectory, name: string, store: SignalProtocolStore) => Promise<{
21
- bundle: FullDirectoryEntry;
22
- storeEntry: StoreEntry;
23
- }>;
@@ -1,157 +0,0 @@
1
- import { KeyHelper } from "@privacyresearch/libsignal-protocol-typescript";
2
- import { arrayBufferToBase64, base64ToArrayBuffer } from "./helper.js";
3
- export class SignalDirectory {
4
- _data = {};
5
- storeKeyBundle(address, bundle) {
6
- this._data[address] = bundle;
7
- }
8
- addOneTimePreKeys(address, keys) {
9
- this._data[address].oneTimePreKeys.unshift(...keys);
10
- }
11
- getPreKeyBundle(address) {
12
- const bundle = this._data[address];
13
- if (!bundle) {
14
- return undefined;
15
- }
16
- const oneTimePreKey = bundle.oneTimePreKeys.pop();
17
- const { identityPubKey, signedPreKey, registrationId } = bundle;
18
- return {
19
- identityKey: identityPubKey,
20
- signedPreKey,
21
- preKey: oneTimePreKey,
22
- registrationId,
23
- };
24
- }
25
- }
26
- // Serialize ArrayBuffer to base64 for storage
27
- export const serializeBundle = (bundle) => {
28
- return {
29
- registrationId: bundle.registrationId,
30
- identityPubKey: arrayBufferToBase64(bundle.identityPubKey),
31
- signedPreKey: {
32
- keyId: bundle.signedPreKey.keyId,
33
- publicKey: arrayBufferToBase64(bundle.signedPreKey.publicKey),
34
- signature: arrayBufferToBase64(bundle.signedPreKey.signature),
35
- },
36
- oneTimePreKeys: bundle.oneTimePreKeys.map((key) => ({
37
- keyId: key.keyId,
38
- publicKey: arrayBufferToBase64(key.publicKey),
39
- })),
40
- };
41
- };
42
- // Deserialize base64 back to ArrayBuffer
43
- export const deserializeBundle = (serializable) => {
44
- const deserializedBundle = {
45
- registrationId: serializable.registrationId,
46
- identityPubKey: base64ToArrayBuffer(serializable.identityPubKey),
47
- signedPreKey: {
48
- keyId: serializable.signedPreKey.keyId,
49
- publicKey: base64ToArrayBuffer(serializable.signedPreKey.publicKey),
50
- signature: base64ToArrayBuffer(serializable.signedPreKey.signature),
51
- },
52
- oneTimePreKeys: serializable.oneTimePreKeys.map((key) => ({
53
- keyId: key.keyId,
54
- publicKey: base64ToArrayBuffer(key.publicKey),
55
- })),
56
- };
57
- const oneTimePreKey = deserializedBundle.oneTimePreKeys.pop();
58
- const { identityPubKey, signedPreKey, registrationId } = deserializedBundle;
59
- return {
60
- identityKey: identityPubKey,
61
- signedPreKey,
62
- preKey: oneTimePreKey,
63
- registrationId,
64
- };
65
- };
66
- export const serializeStore = (entry) => {
67
- return {
68
- registrationId: entry.registrationId,
69
- identityKey: {
70
- pubKey: arrayBufferToBase64(entry.identityKey.pubKey),
71
- privKey: arrayBufferToBase64(entry.identityKey.privKey)
72
- },
73
- preKey: {
74
- keyId: entry.preKey.keyId,
75
- keyPair: {
76
- pubKey: arrayBufferToBase64(entry.preKey.keyPair.pubKey),
77
- privKey: arrayBufferToBase64(entry.preKey.keyPair.privKey),
78
- }
79
- },
80
- signedPreKey: {
81
- keyId: entry.signedPreKey.keyId,
82
- keyPair: {
83
- pubKey: arrayBufferToBase64(entry.signedPreKey.keyPair.pubKey),
84
- privKey: arrayBufferToBase64(entry.signedPreKey.keyPair.privKey),
85
- },
86
- signature: arrayBufferToBase64(entry.signedPreKey.signature)
87
- }
88
- };
89
- };
90
- export const deserializeStore = (serializedEntry) => {
91
- return {
92
- registrationId: serializedEntry.registrationId,
93
- identityKey: {
94
- pubKey: base64ToArrayBuffer(serializedEntry.identityKey.pubKey),
95
- privKey: base64ToArrayBuffer(serializedEntry.identityKey.privKey)
96
- },
97
- preKey: {
98
- keyId: serializedEntry.preKey.keyId,
99
- keyPair: {
100
- pubKey: base64ToArrayBuffer(serializedEntry.preKey.keyPair.pubKey),
101
- privKey: base64ToArrayBuffer(serializedEntry.preKey.keyPair.privKey),
102
- }
103
- },
104
- signedPreKey: {
105
- keyId: serializedEntry.signedPreKey.keyId,
106
- keyPair: {
107
- pubKey: base64ToArrayBuffer(serializedEntry.signedPreKey.keyPair.pubKey),
108
- privKey: base64ToArrayBuffer(serializedEntry.signedPreKey.keyPair.privKey),
109
- },
110
- signature: base64ToArrayBuffer(serializedEntry.signedPreKey.signature)
111
- }
112
- };
113
- };
114
- export const populateStore = (store, storeEntry) => {
115
- const { registrationId, identityKey: identityKeyPair, preKey, signedPreKey } = storeEntry;
116
- store.storeRegistrationId(registrationId);
117
- store.storeIdentityKey(identityKeyPair);
118
- store.storePreKey(`${preKey.keyId}`, preKey.keyPair);
119
- store.storeSignedPreKey(signedPreKey.keyId, signedPreKey.keyPair);
120
- };
121
- export const createStoreBundle = async () => {
122
- const registrationId = KeyHelper.generateRegistrationId();
123
- const identityKeyPair = await KeyHelper.generateIdentityKeyPair();
124
- const baseKeyId = Math.floor(10000 * Math.random());
125
- const preKey = await KeyHelper.generatePreKey(baseKeyId);
126
- const signedPreKeyId = Math.floor(10000 * Math.random());
127
- const signedPreKey = await KeyHelper.generateSignedPreKey(identityKeyPair, signedPreKeyId);
128
- const storeEntry = {
129
- registrationId,
130
- identityKey: identityKeyPair,
131
- preKey,
132
- signedPreKey,
133
- };
134
- const publicSignedPreKey = {
135
- keyId: signedPreKeyId,
136
- publicKey: signedPreKey.keyPair.pubKey,
137
- signature: signedPreKey.signature,
138
- };
139
- // Now we register this with the server so all users can see them
140
- const publicPreKey = {
141
- keyId: preKey.keyId,
142
- publicKey: preKey.keyPair.pubKey,
143
- };
144
- const bundle = {
145
- registrationId,
146
- identityPubKey: identityKeyPair.pubKey,
147
- signedPreKey: publicSignedPreKey,
148
- oneTimePreKeys: [publicPreKey],
149
- };
150
- return { bundle, storeEntry };
151
- };
152
- export const createID = async (directory, name, store) => {
153
- const { bundle, storeEntry } = await createStoreBundle();
154
- populateStore(store, storeEntry);
155
- directory.storeKeyBundle(name, bundle);
156
- return { bundle, storeEntry };
157
- };
@@ -1,46 +0,0 @@
1
- import { Direction, PreKeyPairType, SessionRecordType, SignedPreKeyPairType, StorageType } from '@privacyresearch/libsignal-protocol-typescript';
2
- export declare function isKeyPairType(kp: any): kp is KeyPairType;
3
- export declare function isPreKeyType(pk: any): pk is PreKeyPairType;
4
- export declare function isSignedPreKeyType(spk: any): spk is SignedPreKeyPairType;
5
- interface KeyPairType {
6
- pubKey: ArrayBuffer;
7
- privKey: ArrayBuffer;
8
- }
9
- interface PreKeyType {
10
- keyId: number;
11
- keyPair: KeyPairType;
12
- }
13
- interface SignedPreKeyType extends PreKeyType {
14
- signature: ArrayBuffer;
15
- }
16
- export type StoreValue = KeyPairType | string | number | KeyPairType | PreKeyType | SignedPreKeyType | ArrayBuffer | undefined;
17
- export declare class SignalProtocolStore implements StorageType {
18
- private _store;
19
- constructor();
20
- get(key: string, defaultValue: StoreValue): StoreValue;
21
- remove(key: string): void;
22
- put(key: string, value: StoreValue): void;
23
- getIdentityKeyPair(): Promise<KeyPairType | undefined>;
24
- getLocalRegistrationId(): Promise<number | undefined>;
25
- isTrustedIdentity(identifier: string, identityKey: ArrayBuffer, _direction: Direction): Promise<boolean>;
26
- loadPreKey(keyId: string | number): Promise<KeyPairType | undefined>;
27
- loadSession(identifier: string): Promise<SessionRecordType | undefined>;
28
- loadSignedPreKey(keyId: number | string): Promise<KeyPairType | undefined>;
29
- removePreKey(keyId: number | string): Promise<void>;
30
- saveIdentity(identifier: string, identityKey: ArrayBuffer): Promise<boolean>;
31
- storeSession(identifier: string, record: SessionRecordType): Promise<void>;
32
- loadIdentityKey(identifier: string): Promise<ArrayBuffer | undefined>;
33
- storePreKey(keyId: number | string, keyPair: KeyPairType): Promise<void>;
34
- storeRegistrationId(registrationId: number): Promise<void>;
35
- deleteRegistrationId(registrationId: number): Promise<void>;
36
- storeIdentityKey(keyPair: KeyPairType): Promise<void>;
37
- storeSignedPreKey(keyId: number | string, keyPair: KeyPairType): Promise<void>;
38
- removeSignedPreKey(keyId: number | string): Promise<void>;
39
- removeSession(identifier: string): Promise<void>;
40
- removeSessionIdentity(address: string): Promise<void>;
41
- removeAllSessions(identifier: string): Promise<void>;
42
- removeKeysWithIdentifier(identifier: string): Promise<void>;
43
- }
44
- export declare function arrayBufferToString(b: ArrayBuffer): string;
45
- export declare function uint8ArrayToString(arr: Uint8Array): string;
46
- export {};