@massalabs/gossip-sdk 0.0.2-dev.20260128094509 → 0.0.2-dev.20260128111120

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 (142) hide show
  1. package/dist/api/messageProtocol/index.d.ts +19 -0
  2. package/dist/api/messageProtocol/index.js +26 -0
  3. package/dist/api/messageProtocol/mock.d.ts +12 -0
  4. package/{src/api/messageProtocol/mock.ts → dist/api/messageProtocol/mock.js} +2 -3
  5. package/dist/api/messageProtocol/rest.d.ts +22 -0
  6. package/dist/api/messageProtocol/rest.js +161 -0
  7. package/dist/api/messageProtocol/types.d.ts +61 -0
  8. package/dist/api/messageProtocol/types.js +6 -0
  9. package/dist/assets/generated/wasm/README.md +281 -0
  10. package/dist/assets/generated/wasm/gossip_wasm.d.ts +498 -0
  11. package/dist/assets/generated/wasm/gossip_wasm.js +1399 -0
  12. package/dist/assets/generated/wasm/gossip_wasm_bg.wasm +0 -0
  13. package/dist/assets/generated/wasm/gossip_wasm_bg.wasm.d.ts +68 -0
  14. package/dist/assets/generated/wasm/package.json +15 -0
  15. package/dist/config/protocol.d.ts +36 -0
  16. package/dist/config/protocol.js +77 -0
  17. package/dist/config/sdk.d.ts +82 -0
  18. package/dist/config/sdk.js +55 -0
  19. package/{src/contacts.ts → dist/contacts.d.ts} +10 -94
  20. package/dist/contacts.js +166 -0
  21. package/dist/core/SdkEventEmitter.d.ts +36 -0
  22. package/dist/core/SdkEventEmitter.js +59 -0
  23. package/dist/core/SdkPolling.d.ts +35 -0
  24. package/dist/core/SdkPolling.js +100 -0
  25. package/{src/core/index.ts → dist/core/index.d.ts} +0 -2
  26. package/dist/core/index.js +5 -0
  27. package/dist/crypto/bip39.d.ts +34 -0
  28. package/dist/crypto/bip39.js +62 -0
  29. package/dist/crypto/encryption.d.ts +37 -0
  30. package/dist/crypto/encryption.js +46 -0
  31. package/dist/db.d.ts +190 -0
  32. package/dist/db.js +311 -0
  33. package/dist/gossipSdk.d.ts +274 -0
  34. package/dist/gossipSdk.js +690 -0
  35. package/dist/index.d.ts +73 -0
  36. package/dist/index.js +77 -0
  37. package/dist/services/announcement.d.ts +43 -0
  38. package/dist/services/announcement.js +491 -0
  39. package/dist/services/auth.d.ts +37 -0
  40. package/dist/services/auth.js +76 -0
  41. package/dist/services/discussion.d.ts +63 -0
  42. package/dist/services/discussion.js +297 -0
  43. package/dist/services/message.d.ts +74 -0
  44. package/dist/services/message.js +826 -0
  45. package/dist/services/refresh.d.ts +41 -0
  46. package/dist/services/refresh.js +205 -0
  47. package/{src/sw.ts → dist/sw.d.ts} +1 -8
  48. package/dist/sw.js +10 -0
  49. package/dist/types/events.d.ts +80 -0
  50. package/dist/types/events.js +7 -0
  51. package/dist/types.d.ts +32 -0
  52. package/dist/types.js +7 -0
  53. package/dist/utils/base64.d.ts +10 -0
  54. package/dist/utils/base64.js +30 -0
  55. package/dist/utils/contacts.d.ts +42 -0
  56. package/dist/utils/contacts.js +113 -0
  57. package/dist/utils/discussions.d.ts +24 -0
  58. package/dist/utils/discussions.js +38 -0
  59. package/dist/utils/logs.d.ts +19 -0
  60. package/dist/utils/logs.js +89 -0
  61. package/dist/utils/messageSerialization.d.ts +64 -0
  62. package/dist/utils/messageSerialization.js +184 -0
  63. package/dist/utils/queue.d.ts +50 -0
  64. package/dist/utils/queue.js +110 -0
  65. package/dist/utils/type.d.ts +10 -0
  66. package/dist/utils/type.js +4 -0
  67. package/dist/utils/userId.d.ts +40 -0
  68. package/dist/utils/userId.js +90 -0
  69. package/dist/utils/validation.d.ts +50 -0
  70. package/dist/utils/validation.js +112 -0
  71. package/dist/utils.d.ts +30 -0
  72. package/{src/utils.ts → dist/utils.js} +9 -19
  73. package/dist/wasm/encryption.d.ts +56 -0
  74. package/{src/wasm/encryption.ts → dist/wasm/encryption.js} +22 -51
  75. package/dist/wasm/index.d.ts +10 -0
  76. package/{src/wasm/index.ts → dist/wasm/index.js} +1 -8
  77. package/dist/wasm/loader.d.ts +21 -0
  78. package/dist/wasm/loader.js +103 -0
  79. package/dist/wasm/session.d.ts +85 -0
  80. package/dist/wasm/session.js +226 -0
  81. package/dist/wasm/userKeys.d.ts +17 -0
  82. package/{src/wasm/userKeys.ts → dist/wasm/userKeys.js} +6 -13
  83. package/package.json +5 -1
  84. package/src/api/messageProtocol/index.ts +0 -53
  85. package/src/api/messageProtocol/rest.ts +0 -209
  86. package/src/api/messageProtocol/types.ts +0 -70
  87. package/src/config/protocol.ts +0 -97
  88. package/src/config/sdk.ts +0 -131
  89. package/src/core/SdkEventEmitter.ts +0 -91
  90. package/src/core/SdkPolling.ts +0 -134
  91. package/src/crypto/bip39.ts +0 -84
  92. package/src/crypto/encryption.ts +0 -77
  93. package/src/db.ts +0 -465
  94. package/src/gossipSdk.ts +0 -994
  95. package/src/index.ts +0 -211
  96. package/src/services/announcement.ts +0 -653
  97. package/src/services/auth.ts +0 -95
  98. package/src/services/discussion.ts +0 -380
  99. package/src/services/message.ts +0 -1055
  100. package/src/services/refresh.ts +0 -234
  101. package/src/types/events.ts +0 -108
  102. package/src/types.ts +0 -70
  103. package/src/utils/base64.ts +0 -39
  104. package/src/utils/contacts.ts +0 -161
  105. package/src/utils/discussions.ts +0 -55
  106. package/src/utils/logs.ts +0 -86
  107. package/src/utils/messageSerialization.ts +0 -257
  108. package/src/utils/queue.ts +0 -106
  109. package/src/utils/type.ts +0 -7
  110. package/src/utils/userId.ts +0 -114
  111. package/src/utils/validation.ts +0 -144
  112. package/src/wasm/loader.ts +0 -123
  113. package/src/wasm/session.ts +0 -276
  114. package/test/config/protocol.spec.ts +0 -31
  115. package/test/config/sdk.spec.ts +0 -163
  116. package/test/db/helpers.spec.ts +0 -142
  117. package/test/db/operations.spec.ts +0 -128
  118. package/test/db/states.spec.ts +0 -535
  119. package/test/integration/discussion-flow.spec.ts +0 -422
  120. package/test/integration/messaging-flow.spec.ts +0 -708
  121. package/test/integration/sdk-lifecycle.spec.ts +0 -325
  122. package/test/mocks/index.ts +0 -9
  123. package/test/mocks/mockMessageProtocol.ts +0 -100
  124. package/test/services/auth.spec.ts +0 -311
  125. package/test/services/discussion.spec.ts +0 -279
  126. package/test/services/message-deduplication.spec.ts +0 -299
  127. package/test/services/message-startup.spec.ts +0 -331
  128. package/test/services/message.spec.ts +0 -817
  129. package/test/services/refresh.spec.ts +0 -199
  130. package/test/services/session-status.spec.ts +0 -349
  131. package/test/session/wasm.spec.ts +0 -227
  132. package/test/setup.ts +0 -52
  133. package/test/utils/contacts.spec.ts +0 -156
  134. package/test/utils/discussions.spec.ts +0 -66
  135. package/test/utils/queue.spec.ts +0 -52
  136. package/test/utils/serialization.spec.ts +0 -120
  137. package/test/utils/userId.spec.ts +0 -120
  138. package/test/utils/validation.spec.ts +0 -223
  139. package/test/utils.ts +0 -212
  140. package/tsconfig.json +0 -26
  141. package/tsconfig.tsbuildinfo +0 -1
  142. package/vitest.config.ts +0 -28
@@ -0,0 +1,68 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+ export const memory: WebAssembly.Memory;
4
+ export const __wbg_sessionconfig_free: (a: number, b: number) => void;
5
+ export const sessionconfig_new: (a: number, b: number, c: number, d: number, e: number, f: number, g: bigint) => number;
6
+ export const sessionconfig_new_default: () => number;
7
+ export const __wbg_userpublickeys_free: (a: number, b: number) => void;
8
+ export const userpublickeys_derive_id: (a: number) => [number, number];
9
+ export const userpublickeys_dsa_verification_key: (a: number) => [number, number];
10
+ export const userpublickeys_kem_public_key: (a: number) => [number, number];
11
+ export const userpublickeys_massa_public_key: (a: number) => [number, number];
12
+ export const userpublickeys_to_bytes: (a: number) => [number, number, number, number];
13
+ export const userpublickeys_from_bytes: (a: number, b: number) => [number, number, number];
14
+ export const __wbg_usersecretkeys_free: (a: number, b: number) => void;
15
+ export const usersecretkeys_to_bytes: (a: number) => [number, number, number, number];
16
+ export const usersecretkeys_from_bytes: (a: number, b: number) => [number, number, number];
17
+ export const usersecretkeys_dsa_signing_key: (a: number) => [number, number];
18
+ export const usersecretkeys_kem_secret_key: (a: number) => [number, number];
19
+ export const usersecretkeys_massa_secret_key: (a: number) => [number, number];
20
+ export const __wbg_userkeys_free: (a: number, b: number) => void;
21
+ export const userkeys_public_keys: (a: number) => [number, number, number];
22
+ export const userkeys_secret_keys: (a: number) => [number, number, number];
23
+ export const generate_user_keys: (a: number, b: number) => [number, number, number];
24
+ export const __wbg_encryptionkey_free: (a: number, b: number) => void;
25
+ export const encryptionkey_generate: () => number;
26
+ export const encryptionkey_from_seed: (a: number, b: number, c: number, d: number) => [number, number, number];
27
+ export const encryptionkey_from_bytes: (a: number, b: number) => [number, number, number];
28
+ export const encryptionkey_to_bytes: (a: number) => [number, number];
29
+ export const __wbg_nonce_free: (a: number, b: number) => void;
30
+ export const nonce_generate: () => number;
31
+ export const nonce_from_bytes: (a: number, b: number) => [number, number, number];
32
+ export const nonce_to_bytes: (a: number) => [number, number];
33
+ export const aead_encrypt: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number];
34
+ export const aead_decrypt: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number];
35
+ export const __wbg_sendmessageoutput_free: (a: number, b: number) => void;
36
+ export const sendmessageoutput_seeker: (a: number) => [number, number];
37
+ export const sendmessageoutput_data: (a: number) => [number, number];
38
+ export const __wbg_receivemessageoutput_free: (a: number, b: number) => void;
39
+ export const __wbg_announcementresult_free: (a: number, b: number) => void;
40
+ export const announcementresult_announcer_public_keys: (a: number) => number;
41
+ export const announcementresult_timestamp: (a: number) => number;
42
+ export const announcementresult_user_data: (a: number) => [number, number];
43
+ export const receivemessageoutput_message: (a: number) => [number, number];
44
+ export const receivemessageoutput_timestamp: (a: number) => number;
45
+ export const receivemessageoutput_acknowledged_seekers: (a: number) => any;
46
+ export const receivemessageoutput_user_id: (a: number) => [number, number];
47
+ export const __wbg_sessionmanagerwrapper_free: (a: number, b: number) => void;
48
+ export const sessionmanagerwrapper_new: (a: number) => number;
49
+ export const sessionmanagerwrapper_from_encrypted_blob: (a: number, b: number, c: number) => [number, number, number];
50
+ export const sessionmanagerwrapper_to_encrypted_blob: (a: number, b: number) => [number, number, number, number];
51
+ export const sessionmanagerwrapper_establish_outgoing_session: (a: number, b: number, c: number, d: number, e: number, f: number) => [number, number];
52
+ export const sessionmanagerwrapper_feed_incoming_announcement: (a: number, b: number, c: number, d: number, e: number) => number;
53
+ export const sessionmanagerwrapper_get_message_board_read_keys: (a: number) => any;
54
+ export const sessionmanagerwrapper_send_message: (a: number, b: number, c: number, d: number, e: number) => [number, number, number];
55
+ export const sessionmanagerwrapper_feed_incoming_message_board_read: (a: number, b: number, c: number, d: number, e: number, f: number) => number;
56
+ export const sessionmanagerwrapper_peer_list: (a: number) => any;
57
+ export const sessionmanagerwrapper_peer_session_status: (a: number, b: number, c: number) => [number, number, number];
58
+ export const sessionmanagerwrapper_peer_discard: (a: number, b: number, c: number) => [number, number];
59
+ export const sessionmanagerwrapper_refresh: (a: number) => any;
60
+ export const start: () => void;
61
+ export const __wbindgen_exn_store: (a: number) => void;
62
+ export const __externref_table_alloc: () => number;
63
+ export const __wbindgen_export_2: WebAssembly.Table;
64
+ export const __wbindgen_free: (a: number, b: number, c: number) => void;
65
+ export const __wbindgen_malloc: (a: number, b: number) => number;
66
+ export const __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
67
+ export const __externref_table_dealloc: (a: number) => void;
68
+ export const __wbindgen_start: () => void;
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "gossip-wasm",
3
+ "type": "module",
4
+ "version": "0.1.0",
5
+ "files": [
6
+ "gossip_wasm_bg.wasm",
7
+ "gossip_wasm.js",
8
+ "gossip_wasm.d.ts"
9
+ ],
10
+ "main": "gossip_wasm.js",
11
+ "types": "gossip_wasm.d.ts",
12
+ "sideEffects": [
13
+ "./snippets/*"
14
+ ]
15
+ }
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Protocol API Configuration
3
+ *
4
+ * Centralized configuration for the message protocol API endpoints.
5
+ * This allows easy switching between different protocol implementations.
6
+ */
7
+ export interface ProtocolConfig {
8
+ baseUrl: string;
9
+ timeout: number;
10
+ retryAttempts: number;
11
+ }
12
+ export declare const protocolConfig: ProtocolConfig;
13
+ export declare enum MessageProtocolType {
14
+ REST = "rest",
15
+ MOCK = "mock"
16
+ }
17
+ export declare const defaultMessageProtocol: MessageProtocolType;
18
+ /**
19
+ * Set the base URL for the protocol API at runtime.
20
+ * This overrides environment variables and defaults.
21
+ *
22
+ * @param baseUrl - The base URL to use (e.g., 'https://api.example.com/api')
23
+ *
24
+ * @example
25
+ * ```typescript
26
+ * import { setProtocolBaseUrl } from 'gossip-sdk';
27
+ *
28
+ * // Set custom API endpoint
29
+ * setProtocolBaseUrl('https://my-server.com/api');
30
+ * ```
31
+ */
32
+ export declare function setProtocolBaseUrl(baseUrl: string): void;
33
+ /**
34
+ * Reset the base URL to use environment variables or defaults.
35
+ */
36
+ export declare function resetProtocolBaseUrl(): void;
@@ -0,0 +1,77 @@
1
+ /**
2
+ * Protocol API Configuration
3
+ *
4
+ * Centralized configuration for the message protocol API endpoints.
5
+ * This allows easy switching between different protocol implementations.
6
+ */
7
+ // Default API URL for the hosted REST protocol.
8
+ // Override via setProtocolBaseUrl() or environment variables.
9
+ const DEFAULT_API_URL = 'https://api.usegossip.com';
10
+ // Mutable config that can be updated at runtime
11
+ let currentBaseUrl = null;
12
+ function buildProtocolApiBaseUrl() {
13
+ // If runtime override is set, use it
14
+ if (currentBaseUrl !== null) {
15
+ return currentBaseUrl;
16
+ }
17
+ // Try to get from environment variable (Vite)
18
+ let apiUrl;
19
+ try {
20
+ // Check if import.meta.env is available (Vite environment)
21
+ if (typeof import.meta !== 'undefined' &&
22
+ import.meta.env?.VITE_GOSSIP_API_URL) {
23
+ apiUrl = import.meta.env.VITE_GOSSIP_API_URL;
24
+ }
25
+ }
26
+ catch {
27
+ // import.meta.env not available (Node.js without Vite)
28
+ }
29
+ // Check process.env for Node.js environment
30
+ if (!apiUrl &&
31
+ typeof process !== 'undefined' &&
32
+ process.env?.GOSSIP_API_URL) {
33
+ apiUrl = process.env.GOSSIP_API_URL;
34
+ }
35
+ // Fall back to default
36
+ if (!apiUrl)
37
+ apiUrl = DEFAULT_API_URL;
38
+ // Normalize trailing slashes to avoid `//api`
39
+ const trimmed = apiUrl.replace(/\/+$/, '');
40
+ return `${trimmed}/api`;
41
+ }
42
+ export const protocolConfig = {
43
+ get baseUrl() {
44
+ return buildProtocolApiBaseUrl();
45
+ },
46
+ timeout: 10000,
47
+ retryAttempts: 3,
48
+ };
49
+ export var MessageProtocolType;
50
+ (function (MessageProtocolType) {
51
+ MessageProtocolType["REST"] = "rest";
52
+ MessageProtocolType["MOCK"] = "mock";
53
+ })(MessageProtocolType || (MessageProtocolType = {}));
54
+ export const defaultMessageProtocol = MessageProtocolType.REST;
55
+ /**
56
+ * Set the base URL for the protocol API at runtime.
57
+ * This overrides environment variables and defaults.
58
+ *
59
+ * @param baseUrl - The base URL to use (e.g., 'https://api.example.com/api')
60
+ *
61
+ * @example
62
+ * ```typescript
63
+ * import { setProtocolBaseUrl } from 'gossip-sdk';
64
+ *
65
+ * // Set custom API endpoint
66
+ * setProtocolBaseUrl('https://my-server.com/api');
67
+ * ```
68
+ */
69
+ export function setProtocolBaseUrl(baseUrl) {
70
+ currentBaseUrl = baseUrl;
71
+ }
72
+ /**
73
+ * Reset the base URL to use environment variables or defaults.
74
+ */
75
+ export function resetProtocolBaseUrl() {
76
+ currentBaseUrl = null;
77
+ }
@@ -0,0 +1,82 @@
1
+ /**
2
+ * SDK Configuration
3
+ *
4
+ * Centralized configuration for the Gossip SDK.
5
+ * All values have sensible defaults that can be overridden.
6
+ */
7
+ /**
8
+ * Protocol configuration for network requests
9
+ */
10
+ export interface ProtocolConfig {
11
+ /** API base URL (default: from environment or https://api.usegossip.com) */
12
+ baseUrl?: string;
13
+ /** Request timeout in milliseconds (default: 10000) */
14
+ timeout: number;
15
+ /** Number of retry attempts for failed requests (default: 3) */
16
+ retryAttempts: number;
17
+ }
18
+ /**
19
+ * Polling configuration for automatic message/announcement fetching
20
+ */
21
+ export interface PollingConfig {
22
+ /** Enable automatic polling (default: false) */
23
+ enabled: boolean;
24
+ /** Interval for fetching messages in milliseconds (default: 5000) */
25
+ messagesIntervalMs: number;
26
+ /** Interval for fetching announcements in milliseconds (default: 10000) */
27
+ announcementsIntervalMs: number;
28
+ /** Interval for session refresh/keep-alive in milliseconds (default: 30000) */
29
+ sessionRefreshIntervalMs: number;
30
+ }
31
+ /**
32
+ * Message fetching configuration
33
+ */
34
+ export interface MessagesConfig {
35
+ /** Delay between fetch iterations in milliseconds (default: 100) */
36
+ fetchDelayMs: number;
37
+ /** Maximum number of fetch iterations per call (default: 30) */
38
+ maxFetchIterations: number;
39
+ /**
40
+ * Time window in milliseconds for duplicate detection (default: 30000 = 30 seconds).
41
+ * Messages with same content from same sender within this window are considered duplicates.
42
+ * This handles edge case where app crashes after network send but before DB update,
43
+ * resulting in message being re-sent on restart.
44
+ */
45
+ deduplicationWindowMs: number;
46
+ }
47
+ /**
48
+ * Announcement configuration
49
+ */
50
+ export interface AnnouncementsConfig {
51
+ /** Maximum announcements to fetch per request (default: 500) */
52
+ fetchLimit: number;
53
+ /** Time before marking failed announcements as broken in ms (default: 3600000 = 1 hour) */
54
+ brokenThresholdMs: number;
55
+ }
56
+ /**
57
+ * Complete SDK configuration
58
+ */
59
+ export interface SdkConfig {
60
+ /** Network/protocol settings */
61
+ protocol: ProtocolConfig;
62
+ /** Automatic polling settings */
63
+ polling: PollingConfig;
64
+ /** Message fetching settings */
65
+ messages: MessagesConfig;
66
+ /** Announcement settings */
67
+ announcements: AnnouncementsConfig;
68
+ }
69
+ /**
70
+ * Default SDK configuration values
71
+ */
72
+ export declare const defaultSdkConfig: SdkConfig;
73
+ /**
74
+ * Deep merge partial config with defaults
75
+ */
76
+ export declare function mergeConfig(partial?: DeepPartial<SdkConfig>): SdkConfig;
77
+ /**
78
+ * Helper type for deep partial objects
79
+ */
80
+ export type DeepPartial<T> = {
81
+ [P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P];
82
+ };
@@ -0,0 +1,55 @@
1
+ /**
2
+ * SDK Configuration
3
+ *
4
+ * Centralized configuration for the Gossip SDK.
5
+ * All values have sensible defaults that can be overridden.
6
+ */
7
+ /**
8
+ * Default SDK configuration values
9
+ */
10
+ export const defaultSdkConfig = {
11
+ protocol: {
12
+ timeout: 10000,
13
+ retryAttempts: 3,
14
+ },
15
+ polling: {
16
+ enabled: false,
17
+ messagesIntervalMs: 5000,
18
+ announcementsIntervalMs: 10000,
19
+ sessionRefreshIntervalMs: 30000,
20
+ },
21
+ messages: {
22
+ fetchDelayMs: 100,
23
+ maxFetchIterations: 30,
24
+ deduplicationWindowMs: 30000, // 30 seconds
25
+ },
26
+ announcements: {
27
+ fetchLimit: 500,
28
+ brokenThresholdMs: 60 * 60 * 1000, // 1 hour
29
+ },
30
+ };
31
+ /**
32
+ * Deep merge partial config with defaults
33
+ */
34
+ export function mergeConfig(partial) {
35
+ if (!partial)
36
+ return { ...defaultSdkConfig };
37
+ return {
38
+ protocol: {
39
+ ...defaultSdkConfig.protocol,
40
+ ...partial.protocol,
41
+ },
42
+ polling: {
43
+ ...defaultSdkConfig.polling,
44
+ ...partial.polling,
45
+ },
46
+ messages: {
47
+ ...defaultSdkConfig.messages,
48
+ ...partial.messages,
49
+ },
50
+ announcements: {
51
+ ...defaultSdkConfig.announcements,
52
+ ...partial.announcements,
53
+ },
54
+ };
55
+ }
@@ -17,22 +17,11 @@
17
17
  * await deleteContact(userId, contactUserId);
18
18
  * ```
19
19
  */
20
-
21
- import {
22
- updateContactName as updateContactNameUtil,
23
- deleteContact as deleteContactUtil,
24
- } from './utils/contacts';
25
20
  import { type Contact, type GossipDatabase } from './db';
26
- import type {
27
- UpdateContactNameResult,
28
- DeleteContactResult,
29
- } from './utils/contacts';
21
+ import type { UpdateContactNameResult, DeleteContactResult } from './utils/contacts';
30
22
  import type { UserPublicKeys } from './assets/generated/wasm/gossip_wasm';
31
23
  import type { SessionModule } from './wasm/session';
32
-
33
- // Re-export result types
34
24
  export type { UpdateContactNameResult, DeleteContactResult };
35
-
36
25
  /**
37
26
  * Get all contacts for an owner.
38
27
  *
@@ -46,18 +35,7 @@ export type { UpdateContactNameResult, DeleteContactResult };
46
35
  * contacts.forEach(c => console.log(c.name, c.userId));
47
36
  * ```
48
37
  */
49
- export async function getContacts(
50
- ownerUserId: string,
51
- db: GossipDatabase
52
- ): Promise<Contact[]> {
53
- try {
54
- return await db.getContactsByOwner(ownerUserId);
55
- } catch (error) {
56
- console.error('Error getting contacts:', error);
57
- return [];
58
- }
59
- }
60
-
38
+ export declare function getContacts(ownerUserId: string, db: GossipDatabase): Promise<Contact[]>;
61
39
  /**
62
40
  * Get a specific contact by owner and contact user IDs.
63
41
  *
@@ -74,23 +52,7 @@ export async function getContacts(
74
52
  * }
75
53
  * ```
76
54
  */
77
- export async function getContact(
78
- ownerUserId: string,
79
- contactUserId: string,
80
- db: GossipDatabase
81
- ): Promise<Contact | null> {
82
- try {
83
- const contact = await db.getContactByOwnerAndUserId(
84
- ownerUserId,
85
- contactUserId
86
- );
87
- return contact ?? null;
88
- } catch (error) {
89
- console.error('Error getting contact:', error);
90
- return null;
91
- }
92
- }
93
-
55
+ export declare function getContact(ownerUserId: string, contactUserId: string, db: GossipDatabase): Promise<Contact | null>;
94
56
  /**
95
57
  * Add a new contact.
96
58
  *
@@ -117,42 +79,11 @@ export async function getContact(
117
79
  * }
118
80
  * ```
119
81
  */
120
- export async function addContact(
121
- ownerUserId: string,
122
- userId: string,
123
- name: string,
124
- publicKeys: UserPublicKeys,
125
- db: GossipDatabase
126
- ): Promise<{ success: boolean; error?: string; contact?: Contact }> {
127
- try {
128
- // Check if contact already exists
129
- const existing = await db.getContactByOwnerAndUserId(ownerUserId, userId);
130
- if (existing) {
131
- return { success: false, error: 'Contact already exists' };
132
- }
133
-
134
- const contact: Contact = {
135
- ownerUserId,
136
- userId,
137
- name,
138
- publicKeys: publicKeys.to_bytes(),
139
- isOnline: false,
140
- lastSeen: new Date(),
141
- createdAt: new Date(),
142
- };
143
-
144
- const id = await db.contacts.add(contact);
145
- const newContact = await db.contacts.get(id);
146
- return { success: true, contact: newContact };
147
- } catch (error) {
148
- console.error('Error adding contact:', error);
149
- return {
150
- success: false,
151
- error: error instanceof Error ? error.message : 'Unknown error',
152
- };
153
- }
154
- }
155
-
82
+ export declare function addContact(ownerUserId: string, userId: string, name: string, publicKeys: UserPublicKeys, db: GossipDatabase): Promise<{
83
+ success: boolean;
84
+ error?: string;
85
+ contact?: Contact;
86
+ }>;
156
87
  /**
157
88
  * Update contact name.
158
89
  *
@@ -172,15 +103,7 @@ export async function addContact(
172
103
  * }
173
104
  * ```
174
105
  */
175
- export async function updateContactName(
176
- ownerUserId: string,
177
- contactUserId: string,
178
- newName: string,
179
- db: GossipDatabase
180
- ): Promise<UpdateContactNameResult> {
181
- return await updateContactNameUtil(ownerUserId, contactUserId, newName, db);
182
- }
183
-
106
+ export declare function updateContactName(ownerUserId: string, contactUserId: string, newName: string, db: GossipDatabase): Promise<UpdateContactNameResult>;
184
107
  /**
185
108
  * Delete a contact and all associated discussions and messages.
186
109
  *
@@ -200,11 +123,4 @@ export async function updateContactName(
200
123
  * }
201
124
  * ```
202
125
  */
203
- export async function deleteContact(
204
- ownerUserId: string,
205
- contactUserId: string,
206
- db: GossipDatabase,
207
- session: SessionModule
208
- ): Promise<DeleteContactResult> {
209
- return await deleteContactUtil(ownerUserId, contactUserId, db, session);
210
- }
126
+ export declare function deleteContact(ownerUserId: string, contactUserId: string, db: GossipDatabase, session: SessionModule): Promise<DeleteContactResult>;
@@ -0,0 +1,166 @@
1
+ /**
2
+ * Contact Management SDK
3
+ *
4
+ * Functions for managing contacts including CRUD operations.
5
+ *
6
+ * @example
7
+ * ```typescript
8
+ * import { getContacts, addContact, deleteContact } from 'gossip-sdk';
9
+ *
10
+ * // Get all contacts
11
+ * const contacts = await getContacts(userId);
12
+ *
13
+ * // Add a new contact
14
+ * const result = await addContact(userId, contactUserId, 'Alice', publicKeys);
15
+ *
16
+ * // Delete a contact
17
+ * await deleteContact(userId, contactUserId);
18
+ * ```
19
+ */
20
+ import { updateContactName as updateContactNameUtil, deleteContact as deleteContactUtil, } from './utils/contacts';
21
+ /**
22
+ * Get all contacts for an owner.
23
+ *
24
+ * @param ownerUserId - The user ID of the contact owner
25
+ * @param db - Database instance
26
+ * @returns Array of contacts
27
+ *
28
+ * @example
29
+ * ```typescript
30
+ * const contacts = await getContacts(myUserId, db);
31
+ * contacts.forEach(c => console.log(c.name, c.userId));
32
+ * ```
33
+ */
34
+ export async function getContacts(ownerUserId, db) {
35
+ try {
36
+ return await db.getContactsByOwner(ownerUserId);
37
+ }
38
+ catch (error) {
39
+ console.error('Error getting contacts:', error);
40
+ return [];
41
+ }
42
+ }
43
+ /**
44
+ * Get a specific contact by owner and contact user IDs.
45
+ *
46
+ * @param ownerUserId - The user ID of the contact owner
47
+ * @param contactUserId - The user ID of the contact
48
+ * @param db - Database instance
49
+ * @returns Contact or null if not found
50
+ *
51
+ * @example
52
+ * ```typescript
53
+ * const contact = await getContact(myUserId, theirUserId, db);
54
+ * if (contact) {
55
+ * console.log('Found contact:', contact.name);
56
+ * }
57
+ * ```
58
+ */
59
+ export async function getContact(ownerUserId, contactUserId, db) {
60
+ try {
61
+ const contact = await db.getContactByOwnerAndUserId(ownerUserId, contactUserId);
62
+ return contact ?? null;
63
+ }
64
+ catch (error) {
65
+ console.error('Error getting contact:', error);
66
+ return null;
67
+ }
68
+ }
69
+ /**
70
+ * Add a new contact.
71
+ *
72
+ * @param ownerUserId - The user ID of the contact owner
73
+ * @param userId - The user ID of the contact (Bech32-encoded)
74
+ * @param name - Display name for the contact
75
+ * @param publicKeys - The contact's public keys
76
+ * @param db - Database instance
77
+ * @returns Result with success status and optional contact
78
+ *
79
+ * @example
80
+ * ```typescript
81
+ * const result = await addContact(
82
+ * myUserId,
83
+ * 'gossip1abc...',
84
+ * 'Alice',
85
+ * alicePublicKeys,
86
+ * db
87
+ * );
88
+ * if (result.success) {
89
+ * console.log('Contact added:', result.contact?.name);
90
+ * } else if (result.error === 'Contact already exists') {
91
+ * console.log('Contact already exists');
92
+ * }
93
+ * ```
94
+ */
95
+ export async function addContact(ownerUserId, userId, name, publicKeys, db) {
96
+ try {
97
+ // Check if contact already exists
98
+ const existing = await db.getContactByOwnerAndUserId(ownerUserId, userId);
99
+ if (existing) {
100
+ return { success: false, error: 'Contact already exists' };
101
+ }
102
+ const contact = {
103
+ ownerUserId,
104
+ userId,
105
+ name,
106
+ publicKeys: publicKeys.to_bytes(),
107
+ isOnline: false,
108
+ lastSeen: new Date(),
109
+ createdAt: new Date(),
110
+ };
111
+ const id = await db.contacts.add(contact);
112
+ const newContact = await db.contacts.get(id);
113
+ return { success: true, contact: newContact };
114
+ }
115
+ catch (error) {
116
+ console.error('Error adding contact:', error);
117
+ return {
118
+ success: false,
119
+ error: error instanceof Error ? error.message : 'Unknown error',
120
+ };
121
+ }
122
+ }
123
+ /**
124
+ * Update contact name.
125
+ *
126
+ * @param ownerUserId - The user ID of the contact owner
127
+ * @param contactUserId - The user ID of the contact
128
+ * @param newName - New name for the contact
129
+ * @param db - Database instance
130
+ * @returns Result with success status and trimmed name
131
+ *
132
+ * @example
133
+ * ```typescript
134
+ * const result = await updateContactName(myUserId, theirUserId, 'Alice Smith', db);
135
+ * if (result.ok) {
136
+ * console.log('Updated to:', result.trimmedName);
137
+ * } else {
138
+ * console.error('Failed:', result.message);
139
+ * }
140
+ * ```
141
+ */
142
+ export async function updateContactName(ownerUserId, contactUserId, newName, db) {
143
+ return await updateContactNameUtil(ownerUserId, contactUserId, newName, db);
144
+ }
145
+ /**
146
+ * Delete a contact and all associated discussions and messages.
147
+ *
148
+ * @param ownerUserId - The user ID of the contact owner
149
+ * @param contactUserId - The user ID of the contact to delete
150
+ * @param db - Database instance
151
+ * @param session - Session module for peer management
152
+ * @returns Result with success status
153
+ *
154
+ * @example
155
+ * ```typescript
156
+ * const result = await deleteContact(myUserId, theirUserId, db, session);
157
+ * if (result.ok) {
158
+ * console.log('Contact deleted');
159
+ * } else {
160
+ * console.error('Failed:', result.message);
161
+ * }
162
+ * ```
163
+ */
164
+ export async function deleteContact(ownerUserId, contactUserId, db, session) {
165
+ return await deleteContactUtil(ownerUserId, contactUserId, db, session);
166
+ }
@@ -0,0 +1,36 @@
1
+ /**
2
+ * SDK Event Emitter
3
+ *
4
+ * Type-safe event emitter for SDK events.
5
+ */
6
+ import type { Message, Discussion, Contact } from '../db';
7
+ export type SdkEventType = 'message' | 'messageSent' | 'messageFailed' | 'discussionRequest' | 'discussionStatusChanged' | 'sessionBroken' | 'sessionRenewed' | 'error';
8
+ export interface SdkEventHandlers {
9
+ message: (message: Message) => void;
10
+ messageSent: (message: Message) => void;
11
+ messageFailed: (message: Message, error: Error) => void;
12
+ discussionRequest: (discussion: Discussion, contact: Contact) => void;
13
+ discussionStatusChanged: (discussion: Discussion) => void;
14
+ sessionBroken: (discussion: Discussion) => void;
15
+ sessionRenewed: (discussion: Discussion) => void;
16
+ error: (error: Error, context: string) => void;
17
+ }
18
+ export declare class SdkEventEmitter {
19
+ private handlers;
20
+ /**
21
+ * Register an event handler
22
+ */
23
+ on<K extends SdkEventType>(event: K, handler: SdkEventHandlers[K]): void;
24
+ /**
25
+ * Remove an event handler
26
+ */
27
+ off<K extends SdkEventType>(event: K, handler: SdkEventHandlers[K]): void;
28
+ /**
29
+ * Emit an event to all registered handlers
30
+ */
31
+ emit<K extends SdkEventType>(event: K, ...args: Parameters<SdkEventHandlers[K]>): void;
32
+ /**
33
+ * Remove all handlers for all events
34
+ */
35
+ clear(): void;
36
+ }