@nikcli-ai/sdk 0.0.6

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 (81) hide show
  1. package/dist/client.d.ts +7 -0
  2. package/dist/client.js +25 -0
  3. package/dist/cloud/client.d.ts +45 -0
  4. package/dist/cloud/client.js +125 -0
  5. package/dist/cloud/index.d.ts +2 -0
  6. package/dist/cloud/index.js +2 -0
  7. package/dist/cloud/types.d.ts +100 -0
  8. package/dist/cloud/types.js +1 -0
  9. package/dist/crypto/e2e.d.ts +30 -0
  10. package/dist/crypto/e2e.js +172 -0
  11. package/dist/crypto/index.d.ts +1 -0
  12. package/dist/crypto/index.js +1 -0
  13. package/dist/gen/client/client.gen.d.ts +2 -0
  14. package/dist/gen/client/client.gen.js +165 -0
  15. package/dist/gen/client/index.d.ts +7 -0
  16. package/dist/gen/client/index.js +5 -0
  17. package/dist/gen/client/types.gen.d.ts +127 -0
  18. package/dist/gen/client/types.gen.js +2 -0
  19. package/dist/gen/client/utils.gen.d.ts +38 -0
  20. package/dist/gen/client/utils.gen.js +226 -0
  21. package/dist/gen/client.gen.d.ts +12 -0
  22. package/dist/gen/client.gen.js +5 -0
  23. package/dist/gen/core/auth.gen.d.ts +18 -0
  24. package/dist/gen/core/auth.gen.js +14 -0
  25. package/dist/gen/core/bodySerializer.gen.d.ts +17 -0
  26. package/dist/gen/core/bodySerializer.gen.js +57 -0
  27. package/dist/gen/core/params.gen.d.ts +33 -0
  28. package/dist/gen/core/params.gen.js +89 -0
  29. package/dist/gen/core/pathSerializer.gen.d.ts +33 -0
  30. package/dist/gen/core/pathSerializer.gen.js +106 -0
  31. package/dist/gen/core/serverSentEvents.gen.d.ts +59 -0
  32. package/dist/gen/core/serverSentEvents.gen.js +117 -0
  33. package/dist/gen/core/types.gen.d.ts +78 -0
  34. package/dist/gen/core/types.gen.js +2 -0
  35. package/dist/gen/core/utils.gen.d.ts +14 -0
  36. package/dist/gen/core/utils.gen.js +69 -0
  37. package/dist/gen/sdk.gen.d.ts +403 -0
  38. package/dist/gen/sdk.gen.js +881 -0
  39. package/dist/gen/types.gen.d.ts +3380 -0
  40. package/dist/gen/types.gen.js +2 -0
  41. package/dist/index.d.ts +12 -0
  42. package/dist/index.js +18 -0
  43. package/dist/server.d.ts +23 -0
  44. package/dist/server.js +94 -0
  45. package/dist/v2/client.d.ts +8 -0
  46. package/dist/v2/client.js +33 -0
  47. package/dist/v2/gen/client/client.gen.d.ts +2 -0
  48. package/dist/v2/gen/client/client.gen.js +232 -0
  49. package/dist/v2/gen/client/index.d.ts +8 -0
  50. package/dist/v2/gen/client/index.js +6 -0
  51. package/dist/v2/gen/client/types.gen.d.ts +117 -0
  52. package/dist/v2/gen/client/types.gen.js +2 -0
  53. package/dist/v2/gen/client/utils.gen.d.ts +33 -0
  54. package/dist/v2/gen/client/utils.gen.js +226 -0
  55. package/dist/v2/gen/client.gen.d.ts +12 -0
  56. package/dist/v2/gen/client.gen.js +3 -0
  57. package/dist/v2/gen/core/auth.gen.d.ts +18 -0
  58. package/dist/v2/gen/core/auth.gen.js +14 -0
  59. package/dist/v2/gen/core/bodySerializer.gen.d.ts +25 -0
  60. package/dist/v2/gen/core/bodySerializer.gen.js +57 -0
  61. package/dist/v2/gen/core/params.gen.d.ts +43 -0
  62. package/dist/v2/gen/core/params.gen.js +102 -0
  63. package/dist/v2/gen/core/pathSerializer.gen.d.ts +33 -0
  64. package/dist/v2/gen/core/pathSerializer.gen.js +106 -0
  65. package/dist/v2/gen/core/queryKeySerializer.gen.d.ts +18 -0
  66. package/dist/v2/gen/core/queryKeySerializer.gen.js +93 -0
  67. package/dist/v2/gen/core/serverSentEvents.gen.d.ts +71 -0
  68. package/dist/v2/gen/core/serverSentEvents.gen.js +133 -0
  69. package/dist/v2/gen/core/types.gen.d.ts +78 -0
  70. package/dist/v2/gen/core/types.gen.js +2 -0
  71. package/dist/v2/gen/core/utils.gen.d.ts +19 -0
  72. package/dist/v2/gen/core/utils.gen.js +87 -0
  73. package/dist/v2/gen/sdk.gen.d.ts +1781 -0
  74. package/dist/v2/gen/sdk.gen.js +3653 -0
  75. package/dist/v2/gen/types.gen.d.ts +6272 -0
  76. package/dist/v2/gen/types.gen.js +2 -0
  77. package/dist/v2/index.d.ts +10 -0
  78. package/dist/v2/index.js +16 -0
  79. package/dist/v2/server.d.ts +23 -0
  80. package/dist/v2/server.js +94 -0
  81. package/package.json +45 -0
@@ -0,0 +1,7 @@
1
+ export * from "./gen/types.gen.js";
2
+ import { type Config } from "./gen/client/types.gen.js";
3
+ import { NikcliClient } from "./gen/sdk.gen.js";
4
+ export { type Config as NikcliClientConfig, NikcliClient };
5
+ export declare function createNikcliClient(config?: Config & {
6
+ directory?: string;
7
+ }): NikcliClient;
package/dist/client.js ADDED
@@ -0,0 +1,25 @@
1
+ export * from "./gen/types.gen.js";
2
+ import { createClient } from "./gen/client/client.gen.js";
3
+ import { NikcliClient } from "./gen/sdk.gen.js";
4
+ export { NikcliClient };
5
+ export function createNikcliClient(config) {
6
+ if (!config?.fetch) {
7
+ const customFetch = (req) => {
8
+ // @ts-ignore
9
+ req.timeout = false;
10
+ return fetch(req);
11
+ };
12
+ config = {
13
+ ...config,
14
+ fetch: customFetch,
15
+ };
16
+ }
17
+ if (config?.directory) {
18
+ config.headers = {
19
+ ...config.headers,
20
+ "x-nikcli-directory": config.directory,
21
+ };
22
+ }
23
+ const client = createClient(config);
24
+ return new NikcliClient({ client });
25
+ }
@@ -0,0 +1,45 @@
1
+ import type { AppendMessageInput, CloudClientConfig, CloudDevice, CloudMessage, CloudSession, CloudSyncOperation, CloudUser, PullSyncInput, PushSyncInput, RegisterDeviceInput, UpsertSessionInput } from "./types.js";
2
+ type RelaySocketFactory = (url: string) => WebSocket;
3
+ export declare class CloudClientError extends Error {
4
+ readonly status: number;
5
+ readonly code?: string;
6
+ readonly details?: unknown;
7
+ constructor(message: string, status: number, code?: string, details?: unknown);
8
+ }
9
+ export interface RelaySocketOptions {
10
+ deviceID?: string;
11
+ webSocketFactory?: RelaySocketFactory;
12
+ }
13
+ export declare class NikcliCloudClient {
14
+ private readonly baseUrl;
15
+ private readonly customFetch;
16
+ private readonly getToken;
17
+ private readonly staticHeaders;
18
+ constructor(config: CloudClientConfig);
19
+ private buildURL;
20
+ private request;
21
+ me(): Promise<CloudUser>;
22
+ registerDevice(input: RegisterDeviceInput): Promise<CloudDevice>;
23
+ listDevices(): Promise<CloudDevice[]>;
24
+ listSessions(limit?: number): Promise<CloudSession[]>;
25
+ getSession(sessionID: string): Promise<CloudSession>;
26
+ upsertSession(sessionID: string, input: UpsertSessionInput): Promise<CloudSession>;
27
+ deleteSession(sessionID: string): Promise<void>;
28
+ listMessages(sessionID: string, options?: {
29
+ after?: number;
30
+ limit?: number;
31
+ }): Promise<CloudMessage[]>;
32
+ appendMessage(sessionID: string, input: AppendMessageInput): Promise<CloudMessage>;
33
+ pushSync(input: PushSyncInput): Promise<{
34
+ ok: true;
35
+ applied: number;
36
+ cursor: number;
37
+ }>;
38
+ pullSync(input: PullSyncInput): Promise<{
39
+ operations: CloudSyncOperation[];
40
+ cursor: number;
41
+ }>;
42
+ openRelaySocket(sessionID: string, options?: RelaySocketOptions): Promise<WebSocket>;
43
+ }
44
+ export declare function createNikcliCloudClient(config: CloudClientConfig): NikcliCloudClient;
45
+ export {};
@@ -0,0 +1,125 @@
1
+ export class CloudClientError extends Error {
2
+ status;
3
+ code;
4
+ details;
5
+ constructor(message, status, code, details) {
6
+ super(message);
7
+ this.name = "CloudClientError";
8
+ this.status = status;
9
+ this.code = code;
10
+ this.details = details;
11
+ }
12
+ }
13
+ export class NikcliCloudClient {
14
+ baseUrl;
15
+ customFetch;
16
+ getToken;
17
+ staticHeaders;
18
+ constructor(config) {
19
+ this.baseUrl = new URL(config.baseUrl);
20
+ this.customFetch = config.fetch ?? fetch;
21
+ this.staticHeaders = { ...(config.headers ?? {}) };
22
+ if (config.getToken) {
23
+ this.getToken = async () => config.getToken();
24
+ }
25
+ else if (config.token) {
26
+ const token = config.token;
27
+ this.getToken = async () => token;
28
+ }
29
+ else {
30
+ throw new CloudClientError("Missing authentication token provider", 401, "MISSING_TOKEN");
31
+ }
32
+ }
33
+ buildURL(pathname) {
34
+ return new URL(pathname, this.baseUrl);
35
+ }
36
+ async request(method, pathname, body) {
37
+ const token = await this.getToken();
38
+ if (!token) {
39
+ throw new CloudClientError("Authentication token is empty", 401, "MISSING_TOKEN");
40
+ }
41
+ const headers = {
42
+ "Content-Type": "application/json",
43
+ Authorization: `Bearer ${token}`,
44
+ ...this.staticHeaders,
45
+ };
46
+ const response = await this.customFetch(this.buildURL(pathname), {
47
+ method,
48
+ headers,
49
+ ...(body !== undefined ? { body: JSON.stringify(body) } : {}),
50
+ });
51
+ const text = await response.text();
52
+ const isJson = response.headers.get("content-type")?.includes("application/json") ?? false;
53
+ const parsed = isJson && text ? JSON.parse(text) : text;
54
+ if (!response.ok) {
55
+ const payload = (typeof parsed === "object" && parsed ? parsed : {});
56
+ throw new CloudClientError(payload.error?.message || `Cloud request failed with status ${response.status}`, response.status, payload.error?.code, payload.error?.details);
57
+ }
58
+ return parsed;
59
+ }
60
+ me() {
61
+ return this.request("GET", "/auth/me");
62
+ }
63
+ async registerDevice(input) {
64
+ const result = await this.request("POST", "/auth/device/register", input);
65
+ return result.device;
66
+ }
67
+ async listDevices() {
68
+ const result = await this.request("GET", "/devices");
69
+ return result.devices;
70
+ }
71
+ async listSessions(limit) {
72
+ const query = limit ? `?limit=${encodeURIComponent(String(limit))}` : "";
73
+ const result = await this.request("GET", `/sessions${query}`);
74
+ return result.sessions;
75
+ }
76
+ async getSession(sessionID) {
77
+ const result = await this.request("GET", `/sessions/${encodeURIComponent(sessionID)}`);
78
+ return result.session;
79
+ }
80
+ async upsertSession(sessionID, input) {
81
+ const result = await this.request("PUT", `/sessions/${encodeURIComponent(sessionID)}`, input);
82
+ return result.session;
83
+ }
84
+ async deleteSession(sessionID) {
85
+ await this.request("DELETE", `/sessions/${encodeURIComponent(sessionID)}`);
86
+ }
87
+ async listMessages(sessionID, options) {
88
+ const params = new URLSearchParams();
89
+ if (typeof options?.after === "number")
90
+ params.set("after", String(options.after));
91
+ if (typeof options?.limit === "number")
92
+ params.set("limit", String(options.limit));
93
+ const query = params.toString() ? `?${params.toString()}` : "";
94
+ const result = await this.request("GET", `/sessions/${encodeURIComponent(sessionID)}/messages${query}`);
95
+ return result.messages;
96
+ }
97
+ async appendMessage(sessionID, input) {
98
+ const result = await this.request("POST", `/sessions/${encodeURIComponent(sessionID)}/messages`, input);
99
+ return result.message;
100
+ }
101
+ async pushSync(input) {
102
+ return this.request("POST", "/sync/push", input);
103
+ }
104
+ async pullSync(input) {
105
+ return this.request("POST", "/sync/pull", input);
106
+ }
107
+ async openRelaySocket(sessionID, options) {
108
+ const token = await this.getToken();
109
+ if (!token) {
110
+ throw new CloudClientError("Authentication token is empty", 401, "MISSING_TOKEN");
111
+ }
112
+ const relayPath = `/relay/${encodeURIComponent(sessionID)}`;
113
+ const url = new URL(relayPath, this.baseUrl);
114
+ url.protocol = url.protocol === "https:" ? "wss:" : "ws:";
115
+ url.searchParams.set("token", token);
116
+ if (options?.deviceID) {
117
+ url.searchParams.set("deviceID", options.deviceID);
118
+ }
119
+ const factory = options?.webSocketFactory ?? ((relayUrl) => new WebSocket(relayUrl));
120
+ return factory(url.toString());
121
+ }
122
+ }
123
+ export function createNikcliCloudClient(config) {
124
+ return new NikcliCloudClient(config);
125
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./types.js";
2
+ export { CloudClientError, NikcliCloudClient, createNikcliCloudClient, type RelaySocketOptions } from "./client.js";
@@ -0,0 +1,2 @@
1
+ export * from "./types.js";
2
+ export { CloudClientError, NikcliCloudClient, createNikcliCloudClient } from "./client.js";
@@ -0,0 +1,100 @@
1
+ export type CloudPlatform = "ios" | "android" | "web" | "desktop";
2
+ export interface CloudUser {
3
+ userID: string;
4
+ email?: string;
5
+ claims: Record<string, unknown>;
6
+ }
7
+ export interface CloudDevice {
8
+ id: string;
9
+ userID: string;
10
+ name: string;
11
+ platform: CloudPlatform;
12
+ publicKey: string;
13
+ pushToken?: string;
14
+ createdAt: number;
15
+ lastSeen: number;
16
+ }
17
+ export interface CloudSession {
18
+ id: string;
19
+ userID: string;
20
+ title: string;
21
+ directoryHash?: string;
22
+ encryptedState: string;
23
+ syncVersion: number;
24
+ createdAt: number;
25
+ updatedAt: number;
26
+ }
27
+ export interface CloudMessage {
28
+ id: string;
29
+ sessionID: string;
30
+ userID: string;
31
+ deviceID?: string;
32
+ role: "user" | "assistant" | "system" | "tool";
33
+ encryptedContent: string;
34
+ createdAt: number;
35
+ syncVersion: number;
36
+ }
37
+ export interface CloudSyncOperation {
38
+ id: number;
39
+ userID: string;
40
+ sessionID: string;
41
+ deviceID?: string;
42
+ operation: "upsert" | "delete";
43
+ entityType: "session" | "message";
44
+ entityID: string;
45
+ payload?: string;
46
+ hash: string;
47
+ createdAt: number;
48
+ }
49
+ export interface CloudSyncPushOperation {
50
+ sessionID: string;
51
+ entityType: "session" | "message";
52
+ operation: "upsert" | "delete";
53
+ entityID: string;
54
+ payload?: string;
55
+ hash: string;
56
+ timestamp: number;
57
+ }
58
+ export interface CloudClientConfig {
59
+ baseUrl: string;
60
+ token?: string;
61
+ getToken?: () => Promise<string> | string;
62
+ fetch?: typeof globalThis.fetch;
63
+ headers?: Record<string, string>;
64
+ }
65
+ export interface RegisterDeviceInput {
66
+ deviceID: string;
67
+ name: string;
68
+ platform: CloudPlatform;
69
+ publicKey: string;
70
+ pushToken?: string;
71
+ }
72
+ export interface UpsertSessionInput {
73
+ title: string;
74
+ directoryHash?: string;
75
+ encryptedState: string;
76
+ syncVersion?: number;
77
+ }
78
+ export interface AppendMessageInput {
79
+ messageID: string;
80
+ deviceID?: string;
81
+ role: CloudMessage["role"];
82
+ encryptedContent: string;
83
+ createdAt?: number;
84
+ syncVersion?: number;
85
+ }
86
+ export interface PullSyncInput {
87
+ since: number;
88
+ limit?: number;
89
+ }
90
+ export interface PushSyncInput {
91
+ deviceID: string;
92
+ operations: CloudSyncPushOperation[];
93
+ }
94
+ export interface CloudErrorPayload {
95
+ error?: {
96
+ code: string;
97
+ message: string;
98
+ details?: unknown;
99
+ };
100
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,30 @@
1
+ export interface E2EIdentity {
2
+ publicKey: string;
3
+ privateKey: string;
4
+ }
5
+ export interface E2EPeer {
6
+ id: string;
7
+ publicKey: string;
8
+ }
9
+ export interface E2EEncryptedPayload {
10
+ version: 1;
11
+ iv: string;
12
+ ciphertext: string;
13
+ }
14
+ export declare class E2ECryptoError extends Error {
15
+ constructor(message: string);
16
+ }
17
+ export declare class E2ECrypto {
18
+ private keyPair;
19
+ private readonly peerKeys;
20
+ static create(): Promise<E2ECrypto>;
21
+ generateIdentity(): Promise<E2EIdentity>;
22
+ importIdentity(identity: E2EIdentity): Promise<void>;
23
+ exportIdentity(): Promise<E2EIdentity>;
24
+ setPeer(peer: E2EPeer): Promise<void>;
25
+ removePeer(peerID: string): void;
26
+ fingerprint(publicKey: string): Promise<string>;
27
+ encrypt(peerID: string, plaintext: string | Uint8Array): Promise<E2EEncryptedPayload>;
28
+ decrypt(peerID: string, payload: E2EEncryptedPayload): Promise<Uint8Array>;
29
+ decryptText(peerID: string, payload: E2EEncryptedPayload): Promise<string>;
30
+ }
@@ -0,0 +1,172 @@
1
+ export class E2ECryptoError extends Error {
2
+ constructor(message) {
3
+ super(message);
4
+ this.name = "E2ECryptoError";
5
+ }
6
+ }
7
+ function getCrypto() {
8
+ if (!globalThis.crypto) {
9
+ throw new E2ECryptoError("Web Crypto API is not available in this runtime");
10
+ }
11
+ return globalThis.crypto;
12
+ }
13
+ function getSubtle() {
14
+ const subtle = getCrypto().subtle;
15
+ if (!subtle) {
16
+ throw new E2ECryptoError("SubtleCrypto is not available in this runtime");
17
+ }
18
+ return subtle;
19
+ }
20
+ function toBase64(input) {
21
+ const bytes = input instanceof Uint8Array ? input : new Uint8Array(input);
22
+ if (typeof Buffer !== "undefined") {
23
+ return Buffer.from(bytes).toString("base64");
24
+ }
25
+ let text = "";
26
+ for (const value of bytes) {
27
+ text += String.fromCharCode(value);
28
+ }
29
+ return btoa(text);
30
+ }
31
+ function fromBase64(input) {
32
+ if (typeof Buffer !== "undefined") {
33
+ return new Uint8Array(Buffer.from(input, "base64"));
34
+ }
35
+ const decoded = atob(input);
36
+ const bytes = new Uint8Array(decoded.length);
37
+ for (let i = 0; i < decoded.length; i++) {
38
+ bytes[i] = decoded.charCodeAt(i);
39
+ }
40
+ return bytes;
41
+ }
42
+ function toArrayBuffer(bytes) {
43
+ const start = bytes.byteOffset;
44
+ const end = bytes.byteOffset + bytes.byteLength;
45
+ return bytes.buffer.slice(start, end);
46
+ }
47
+ function ensurePeerKey(peerKey, peerID) {
48
+ if (!peerKey) {
49
+ throw new E2ECryptoError(`Missing public key for peer '${peerID}'`);
50
+ }
51
+ return peerKey;
52
+ }
53
+ export class E2ECrypto {
54
+ keyPair = null;
55
+ peerKeys = new Map();
56
+ static async create() {
57
+ const instance = new E2ECrypto();
58
+ await instance.generateIdentity();
59
+ return instance;
60
+ }
61
+ async generateIdentity() {
62
+ const subtle = getSubtle();
63
+ this.keyPair = await subtle.generateKey({
64
+ name: "ECDH",
65
+ namedCurve: "P-256",
66
+ }, true, ["deriveKey"]);
67
+ const publicKey = await subtle.exportKey("spki", this.keyPair.publicKey);
68
+ const privateKey = await subtle.exportKey("pkcs8", this.keyPair.privateKey);
69
+ return {
70
+ publicKey: toBase64(publicKey),
71
+ privateKey: toBase64(privateKey),
72
+ };
73
+ }
74
+ async importIdentity(identity) {
75
+ const subtle = getSubtle();
76
+ const [publicKey, privateKey] = await Promise.all([
77
+ subtle.importKey("spki", toArrayBuffer(fromBase64(identity.publicKey)), {
78
+ name: "ECDH",
79
+ namedCurve: "P-256",
80
+ }, true, []),
81
+ subtle.importKey("pkcs8", toArrayBuffer(fromBase64(identity.privateKey)), {
82
+ name: "ECDH",
83
+ namedCurve: "P-256",
84
+ }, true, ["deriveKey"]),
85
+ ]);
86
+ this.keyPair = {
87
+ publicKey,
88
+ privateKey,
89
+ };
90
+ }
91
+ async exportIdentity() {
92
+ if (!this.keyPair) {
93
+ throw new E2ECryptoError("Identity has not been initialized");
94
+ }
95
+ const subtle = getSubtle();
96
+ const [publicKey, privateKey] = await Promise.all([
97
+ subtle.exportKey("spki", this.keyPair.publicKey),
98
+ subtle.exportKey("pkcs8", this.keyPair.privateKey),
99
+ ]);
100
+ return {
101
+ publicKey: toBase64(publicKey),
102
+ privateKey: toBase64(privateKey),
103
+ };
104
+ }
105
+ async setPeer(peer) {
106
+ const subtle = getSubtle();
107
+ const key = await subtle.importKey("spki", toArrayBuffer(fromBase64(peer.publicKey)), {
108
+ name: "ECDH",
109
+ namedCurve: "P-256",
110
+ }, true, []);
111
+ this.peerKeys.set(peer.id, key);
112
+ }
113
+ removePeer(peerID) {
114
+ this.peerKeys.delete(peerID);
115
+ }
116
+ async fingerprint(publicKey) {
117
+ const subtle = getSubtle();
118
+ const digest = await subtle.digest("SHA-256", toArrayBuffer(fromBase64(publicKey)));
119
+ return [...new Uint8Array(digest)]
120
+ .map((item) => item.toString(16).padStart(2, "0"))
121
+ .join("")
122
+ .slice(0, 32);
123
+ }
124
+ async encrypt(peerID, plaintext) {
125
+ if (!this.keyPair) {
126
+ throw new E2ECryptoError("Identity has not been initialized");
127
+ }
128
+ const peerKey = ensurePeerKey(this.peerKeys.get(peerID), peerID);
129
+ const subtle = getSubtle();
130
+ const aesKey = await subtle.deriveKey({
131
+ name: "ECDH",
132
+ public: peerKey,
133
+ }, this.keyPair.privateKey, {
134
+ name: "AES-GCM",
135
+ length: 256,
136
+ }, false, ["encrypt", "decrypt"]);
137
+ const iv = getCrypto().getRandomValues(new Uint8Array(12));
138
+ const data = typeof plaintext === "string" ? new TextEncoder().encode(plaintext) : plaintext;
139
+ const encrypted = await subtle.encrypt({
140
+ name: "AES-GCM",
141
+ iv: toArrayBuffer(iv),
142
+ }, aesKey, toArrayBuffer(data));
143
+ return {
144
+ version: 1,
145
+ iv: toBase64(iv),
146
+ ciphertext: toBase64(encrypted),
147
+ };
148
+ }
149
+ async decrypt(peerID, payload) {
150
+ if (!this.keyPair) {
151
+ throw new E2ECryptoError("Identity has not been initialized");
152
+ }
153
+ const peerKey = ensurePeerKey(this.peerKeys.get(peerID), peerID);
154
+ const subtle = getSubtle();
155
+ const aesKey = await subtle.deriveKey({
156
+ name: "ECDH",
157
+ public: peerKey,
158
+ }, this.keyPair.privateKey, {
159
+ name: "AES-GCM",
160
+ length: 256,
161
+ }, false, ["encrypt", "decrypt"]);
162
+ const decrypted = await subtle.decrypt({
163
+ name: "AES-GCM",
164
+ iv: toArrayBuffer(fromBase64(payload.iv)),
165
+ }, aesKey, toArrayBuffer(fromBase64(payload.ciphertext)));
166
+ return new Uint8Array(decrypted);
167
+ }
168
+ async decryptText(peerID, payload) {
169
+ const decrypted = await this.decrypt(peerID, payload);
170
+ return new TextDecoder().decode(decrypted);
171
+ }
172
+ }
@@ -0,0 +1 @@
1
+ export { E2ECrypto, E2ECryptoError, type E2EEncryptedPayload, type E2EIdentity, type E2EPeer } from "./e2e.js";
@@ -0,0 +1 @@
1
+ export { E2ECrypto, E2ECryptoError } from "./e2e.js";
@@ -0,0 +1,2 @@
1
+ import type { Client, Config } from "./types.gen.js";
2
+ export declare const createClient: (config?: Config) => Client;