@stvor/sdk 2.4.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (82) hide show
  1. package/dist/facade/app.cjs +29 -0
  2. package/dist/facade/app.d.ts +83 -76
  3. package/dist/facade/app.js +330 -195
  4. package/dist/facade/crypto-session.cjs +29 -0
  5. package/dist/facade/crypto-session.d.ts +49 -54
  6. package/dist/facade/crypto-session.js +117 -140
  7. package/dist/facade/errors.cjs +29 -0
  8. package/dist/facade/errors.d.ts +29 -12
  9. package/dist/facade/errors.js +49 -8
  10. package/dist/facade/index.cjs +29 -0
  11. package/dist/facade/index.d.ts +27 -8
  12. package/dist/facade/index.js +23 -3
  13. package/dist/facade/local-storage-identity-store.cjs +29 -0
  14. package/dist/facade/local-storage-identity-store.d.ts +50 -0
  15. package/dist/facade/local-storage-identity-store.js +100 -0
  16. package/dist/facade/metrics-attestation.cjs +29 -0
  17. package/dist/facade/metrics-attestation.d.ts +209 -0
  18. package/dist/facade/metrics-attestation.js +333 -0
  19. package/dist/facade/metrics-engine.cjs +29 -0
  20. package/dist/facade/metrics-engine.d.ts +91 -0
  21. package/dist/facade/metrics-engine.js +170 -0
  22. package/dist/facade/redis-replay-cache.cjs +29 -0
  23. package/dist/facade/redis-replay-cache.d.ts +88 -0
  24. package/dist/facade/redis-replay-cache.js +60 -0
  25. package/dist/facade/relay-client.cjs +29 -0
  26. package/dist/facade/relay-client.d.ts +22 -23
  27. package/dist/facade/relay-client.js +107 -128
  28. package/dist/facade/replay-manager.cjs +29 -0
  29. package/dist/facade/replay-manager.d.ts +28 -35
  30. package/dist/facade/replay-manager.js +102 -69
  31. package/dist/facade/sodium-singleton.cjs +29 -0
  32. package/dist/facade/tofu-manager.cjs +29 -0
  33. package/dist/facade/tofu-manager.d.ts +38 -36
  34. package/dist/facade/tofu-manager.js +109 -77
  35. package/dist/facade/types.cjs +29 -0
  36. package/dist/facade/types.d.ts +2 -0
  37. package/dist/index.cjs +29 -0
  38. package/dist/index.d.cts +6 -0
  39. package/dist/index.d.ts +4 -0
  40. package/dist/index.js +7 -0
  41. package/dist/legacy.cjs +29 -0
  42. package/dist/legacy.d.ts +31 -1
  43. package/dist/legacy.js +90 -2
  44. package/dist/ratchet/core-production.cjs +29 -0
  45. package/dist/ratchet/core-production.d.ts +95 -0
  46. package/dist/ratchet/core-production.js +286 -0
  47. package/dist/ratchet/index.cjs +29 -0
  48. package/dist/ratchet/index.d.ts +49 -78
  49. package/dist/ratchet/index.js +313 -288
  50. package/dist/ratchet/key-recovery.cjs +29 -0
  51. package/dist/ratchet/replay-protection.cjs +29 -0
  52. package/dist/ratchet/tofu.cjs +29 -0
  53. package/dist/src/facade/app.cjs +29 -0
  54. package/dist/src/facade/app.d.ts +105 -0
  55. package/dist/src/facade/app.js +245 -0
  56. package/dist/src/facade/crypto.cjs +29 -0
  57. package/dist/src/facade/errors.cjs +29 -0
  58. package/dist/src/facade/errors.d.ts +19 -0
  59. package/dist/src/facade/errors.js +21 -0
  60. package/dist/src/facade/index.cjs +29 -0
  61. package/dist/src/facade/index.d.ts +8 -0
  62. package/dist/src/facade/index.js +5 -0
  63. package/dist/src/facade/relay-client.cjs +29 -0
  64. package/dist/src/facade/relay-client.d.ts +36 -0
  65. package/dist/src/facade/relay-client.js +154 -0
  66. package/dist/src/facade/types.cjs +29 -0
  67. package/dist/src/facade/types.d.ts +50 -0
  68. package/dist/src/facade/types.js +4 -0
  69. package/dist/src/index.cjs +29 -0
  70. package/dist/src/index.d.ts +2 -0
  71. package/dist/src/index.js +2 -0
  72. package/dist/src/legacy.cjs +29 -0
  73. package/dist/src/legacy.d.ts +0 -0
  74. package/dist/src/legacy.js +1 -0
  75. package/dist/src/mock-relay-server.cjs +29 -0
  76. package/dist/src/mock-relay-server.d.ts +30 -0
  77. package/dist/src/mock-relay-server.js +236 -0
  78. package/package.json +37 -11
  79. package/dist/ratchet/tests/ratchet.test.d.ts +0 -1
  80. package/dist/ratchet/tests/ratchet.test.js +0 -160
  81. /package/dist/{facade → src/facade}/crypto.d.ts +0 -0
  82. /package/dist/{facade → src/facade}/crypto.js +0 -0
@@ -0,0 +1,29 @@
1
+ 'use strict';
2
+
3
+ // Auto-generated CommonJS wrapper for facade/app.js
4
+ // This allows `require('@stvor/sdk')` to work alongside ESM `import`.
5
+
6
+ const mod = require('module');
7
+ const url = require('url');
8
+
9
+ // Use dynamic import to load the ESM module
10
+ let _cached;
11
+ async function _load() {
12
+ if (!_cached) {
13
+ _cached = await import(url.pathToFileURL(__filename.replace(/\.cjs$/, '.js')).href);
14
+ }
15
+ return _cached;
16
+ }
17
+
18
+ // For simple CJS usage, expose a promise-based loader
19
+ module.exports = new Proxy({ load: _load }, {
20
+ get(target, prop) {
21
+ if (prop === '__esModule') return true;
22
+ if (prop === 'then') return undefined; // prevent treating as thenable
23
+ if (prop === 'load') return _load;
24
+ if (prop === 'default') {
25
+ return _load().then(m => m.default);
26
+ }
27
+ return _load().then(m => m[prop]);
28
+ }
29
+ });
@@ -1,100 +1,108 @@
1
- import type { StvorAppConfig, UserId, MessageContent } from './types.js';
2
- import { RelayClient } from './relay-client.js';
3
- type MessageHandler = (from: UserId, msg: string | Uint8Array) => void;
4
- type UserAvailableHandler = (userId: UserId) => void;
5
- export declare class StvorFacadeClient {
6
- readonly userId: UserId;
7
- private readonly relay;
8
- private readonly defaultTimeout;
9
- private crypto;
10
- private handlers;
11
- private userAvailableHandlers;
12
- private knownPubKeys;
13
- private pendingKeyResolvers;
14
- constructor(userId: UserId, relay: RelayClient, defaultTimeout?: number);
15
- private handleRelayMessage;
16
- internalInitialize(): Promise<void>;
1
+ /**
2
+ * STVOR DX Facade - Main Application Classes
3
+ *
4
+ * Security Guarantees:
5
+ * - X3DH + Double Ratchet (Signal Protocol)
6
+ * - Forward Secrecy via automatic DH ratchet rotation
7
+ * - Post-Compromise Security via forced ratchet steps
8
+ * - TOFU (Trust On First Use) for identity verification
9
+ * - Replay protection via nonce validation
10
+ * - Cryptographically verified metrics (HMAC-SHA256)
11
+ * - Node.js crypto for all cryptographic operations
12
+ */
13
+ import { StvorAppConfig, UserId, MessageContent } from './types';
14
+ import { DecryptedMessage } from './types';
15
+ import { Errors, StvorError, ErrorCode } from './errors';
16
+ import { SealedPayload } from './types';
17
+ export type { DecryptedMessage, SealedPayload, ErrorCode };
18
+ export { StvorError, Errors };
19
+ import { RelayClient } from './relay-client';
20
+ import { Counter, Gauge, register } from 'prom-client';
21
+ import { MetricsAttestationEngine } from './metrics-attestation';
22
+ declare const messagesDeliveredTotal: Counter<string>;
23
+ declare const quotaExceededTotal: Counter<string>;
24
+ declare const rateLimitedTotal: Counter<string>;
25
+ declare const activeTokens: Gauge<string>;
26
+ export { messagesDeliveredTotal, quotaExceededTotal, rateLimitedTotal, activeTokens, register };
27
+ export declare class StvorApp {
28
+ private relay;
29
+ private config;
30
+ private connectedClients;
31
+ private metricsAttestation;
32
+ private backendUrl;
33
+ private appToken;
34
+ constructor(config: Required<StvorAppConfig>);
35
+ isReady(): boolean;
17
36
  /**
18
- * Check if a user's public key is available locally
37
+ * Get attestation engine for recording metrics
19
38
  */
20
- isUserAvailable(userId: UserId): boolean;
39
+ getMetricsAttestationEngine(): MetricsAttestationEngine;
21
40
  /**
22
- * Get list of all known users (whose public keys we have)
41
+ * Periodically send metrics attestations to backend
42
+ * Backend verifies and stores only valid attestations
23
43
  */
24
- getAvailableUsers(): UserId[];
44
+ sendMetricsAttestation(): Promise<void>;
25
45
  /**
26
- * Wait until a specific user's public key becomes available.
27
- * This is the recommended way to ensure you can send messages.
28
- *
29
- * @param userId - The user to wait for
30
- * @param timeoutMs - Maximum time to wait (default: 10000ms)
31
- * @throws StvorError with RECIPIENT_TIMEOUT if timeout expires
32
- *
33
- * @example
34
- * ```typescript
35
- * await alice.waitForUser('bob@example.com');
36
- * await alice.send('bob@example.com', 'Hello!');
37
- * ```
46
+ * Flush metrics to backend
47
+ * Sends current metrics attestation (if there is any activity)
48
+ * Called explicitly by user or on disconnect
38
49
  */
39
- waitForUser(userId: UserId, timeoutMs?: number): Promise<void>;
50
+ flushMetrics(): Promise<void>;
51
+ connect(userId: UserId): Promise<StvorFacadeClient>;
52
+ disconnect(userId?: UserId): Promise<void>;
53
+ private initClient;
54
+ }
55
+ export declare class StvorFacadeClient {
56
+ private userId;
57
+ private relay;
58
+ private metricsAttestation;
59
+ private initialized;
60
+ private cryptoSession;
61
+ private messageHandlers;
62
+ private messageQueue;
63
+ private isReceiving;
64
+ constructor(userId: UserId, relay: RelayClient, metricsAttestation: MetricsAttestationEngine);
65
+ internalInitialize(): Promise<void>;
66
+ private initialize;
40
67
  /**
41
68
  * Send an encrypted message to a recipient.
42
69
  *
43
- * If the recipient's public key is not yet available, this method will
44
- * automatically wait up to `timeoutMs` for the key to arrive via the relay.
70
+ * By default, if the recipient is not yet registered, the method will
71
+ * poll up to `options.timeout` ms for their keys to appear on the relay.
72
+ * Set `options.waitForRecipient: false` to throw immediately instead.
45
73
  *
46
- * @param recipientId - The recipient's user ID
47
- * @param content - Message content (string or Uint8Array)
48
- * @param options - Optional: { timeout: number, waitForRecipient: boolean }
49
- * @throws StvorError with RECIPIENT_TIMEOUT if recipient key doesn't arrive in time
50
- *
51
- * @example
52
- * ```typescript
53
- * // Auto-waits for recipient (recommended)
54
- * await alice.send('bob@example.com', 'Hello!');
55
- *
56
- * // Skip waiting (throws immediately if not available)
57
- * await alice.send('bob@example.com', 'Hello!', { waitForRecipient: false });
58
- * ```
74
+ * @param recipientId - The recipient's user ID
75
+ * @param content - Message content (string or Uint8Array)
76
+ * @param options - Optional settings:
77
+ * - `timeout` — Max wait time in ms (default: 10 000)
78
+ * - `waitForRecipient` — Auto-wait for recipient keys (default: true)
59
79
  */
60
80
  send(recipientId: UserId, content: MessageContent, options?: {
61
81
  timeout?: number;
62
82
  waitForRecipient?: boolean;
63
83
  }): Promise<void>;
64
84
  /**
65
- * Register a handler for incoming messages
85
+ * Check current quota usage from the relay server
66
86
  */
67
- onMessage(handler: MessageHandler): () => void;
87
+ private checkQuota;
68
88
  /**
69
- * Register a handler that fires when a new user becomes available.
70
- * This is triggered when we receive a user's public key announcement.
71
- *
72
- * **Edge-triggered**: Fires only ONCE per user, on first key discovery.
73
- * Will NOT fire again if user reconnects with same identity.
89
+ * Wait for a specific recipient's public keys to become available on the relay.
90
+ * Polls the relay at 500ms intervals until the keys appear or timeout expires.
74
91
  *
75
- * @example
76
- * ```typescript
77
- * client.onUserAvailable((userId) => {
78
- * console.log(`${userId} is now available for messaging`);
79
- * });
80
- * ```
92
+ * @param recipientId - The user ID of the recipient
93
+ * @param timeoutMs - Max time to wait in milliseconds (default: 10000)
94
+ * @returns The recipient's serialized public keys, or null if timeout
81
95
  */
82
- onUserAvailable(handler: UserAvailableHandler): () => void;
83
- }
84
- export declare class StvorApp {
85
- private readonly config;
86
- private clients;
87
- constructor(config: StvorAppConfig);
88
- connect(userId: UserId): Promise<StvorFacadeClient>;
89
- /**
90
- * Get a connected client by user ID
91
- */
92
- getClient(userId: UserId): StvorFacadeClient | undefined;
96
+ waitForUser(recipientId: UserId, timeoutMs?: number): Promise<boolean>;
97
+ private waitForRecipientKeys;
98
+ onMessage(handler: (msg: DecryptedMessage) => void): () => void;
99
+ getUserId(): UserId;
100
+ private decryptMessage;
101
+ private startMessagePolling;
93
102
  /**
94
- * Check if a user is connected locally
103
+ * Disconnect the client from the relay server.
95
104
  */
96
- isConnected(userId: UserId): boolean;
97
- disconnect(userId?: UserId): Promise<void>;
105
+ disconnect(): Promise<void>;
98
106
  }
99
107
  export declare function init(config: StvorAppConfig): Promise<StvorApp>;
100
108
  export declare const createApp: typeof init;
@@ -102,4 +110,3 @@ export declare const Stvor: {
102
110
  init: typeof init;
103
111
  createApp: typeof init;
104
112
  };
105
- export {};