@agentvault/secure-channel 0.6.12 → 0.6.13
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.
- package/dist/channel.d.ts +17 -0
- package/dist/channel.d.ts.map +1 -1
- package/dist/cli.js +166 -3
- package/dist/cli.js.map +4 -4
- package/dist/index.js +166 -3
- package/dist/index.js.map +4 -4
- package/package.json +1 -1
package/dist/channel.d.ts
CHANGED
|
@@ -74,6 +74,23 @@ export declare class SecureChannel extends EventEmitter {
|
|
|
74
74
|
* and relays as sync messages to sibling sessions.
|
|
75
75
|
*/
|
|
76
76
|
private _handleIncomingMessage;
|
|
77
|
+
/**
|
|
78
|
+
* Download an encrypted attachment blob, decrypt it, verify integrity,
|
|
79
|
+
* and save the plaintext file to disk.
|
|
80
|
+
*/
|
|
81
|
+
private _downloadAndDecryptAttachment;
|
|
82
|
+
/**
|
|
83
|
+
* Upload an attachment file: encrypt, upload to server, return metadata
|
|
84
|
+
* for inclusion in the message envelope.
|
|
85
|
+
*/
|
|
86
|
+
private _uploadAttachment;
|
|
87
|
+
/**
|
|
88
|
+
* Send a message with an attached file. Encrypts the file, uploads it,
|
|
89
|
+
* then sends the envelope with attachment metadata via Double Ratchet.
|
|
90
|
+
*/
|
|
91
|
+
sendWithAttachment(plaintext: string, filePath: string, options?: {
|
|
92
|
+
topicId?: string;
|
|
93
|
+
}): Promise<void>;
|
|
77
94
|
/**
|
|
78
95
|
* Relay an owner's message to all sibling sessions as encrypted sync messages.
|
|
79
96
|
* This allows all owner devices to see messages from any single device.
|
package/dist/channel.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"channel.d.ts","sourceRoot":"","sources":["../src/channel.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"channel.d.ts","sourceRoot":"","sources":["../src/channel.ts"],"names":[],"mappings":"AAAA,OAAO,EAA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S;IAOjB,OAAO,CAAC,YAAY;IAKpB;;;OAGG;YACW,aAAa;CAc5B"}
|
package/dist/cli.js
CHANGED
|
@@ -4,6 +4,9 @@
|
|
|
4
4
|
import { EventEmitter } from "node:events";
|
|
5
5
|
import { createServer } from "node:http";
|
|
6
6
|
import { randomUUID } from "node:crypto";
|
|
7
|
+
import { writeFile as writeFile2, mkdir as mkdir2 } from "node:fs/promises";
|
|
8
|
+
import { join as join2 } from "node:path";
|
|
9
|
+
import { readFile as readFile2 } from "node:fs/promises";
|
|
7
10
|
|
|
8
11
|
// ../../node_modules/libsodium-sumo/dist/modules-sumo-esm/libsodium-sumo.mjs
|
|
9
12
|
var __filename;
|
|
@@ -44920,6 +44923,39 @@ var DoubleRatchet = class _DoubleRatchet {
|
|
|
44920
44923
|
}
|
|
44921
44924
|
};
|
|
44922
44925
|
|
|
44926
|
+
// ../crypto/dist/file-crypto.js
|
|
44927
|
+
function encryptFile(plainData) {
|
|
44928
|
+
const fileKey = libsodium_wrappers_default.randombytes_buf(libsodium_wrappers_default.crypto_aead_xchacha20poly1305_ietf_KEYBYTES);
|
|
44929
|
+
const fileNonce = libsodium_wrappers_default.randombytes_buf(libsodium_wrappers_default.crypto_aead_xchacha20poly1305_ietf_NPUBBYTES);
|
|
44930
|
+
const encryptedData = libsodium_wrappers_default.crypto_aead_xchacha20poly1305_ietf_encrypt(
|
|
44931
|
+
plainData,
|
|
44932
|
+
null,
|
|
44933
|
+
// no additional data
|
|
44934
|
+
null,
|
|
44935
|
+
// secret nonce (unused)
|
|
44936
|
+
fileNonce,
|
|
44937
|
+
fileKey
|
|
44938
|
+
);
|
|
44939
|
+
const digestBytes = libsodium_wrappers_default.crypto_generichash(32, encryptedData);
|
|
44940
|
+
const digest = libsodium_wrappers_default.to_hex(digestBytes);
|
|
44941
|
+
return { encryptedData, fileKey, fileNonce, digest };
|
|
44942
|
+
}
|
|
44943
|
+
function decryptFile(encryptedData, fileKey, fileNonce) {
|
|
44944
|
+
return libsodium_wrappers_default.crypto_aead_xchacha20poly1305_ietf_decrypt(
|
|
44945
|
+
null,
|
|
44946
|
+
// secret nonce (unused)
|
|
44947
|
+
encryptedData,
|
|
44948
|
+
null,
|
|
44949
|
+
// no additional data
|
|
44950
|
+
fileNonce,
|
|
44951
|
+
fileKey
|
|
44952
|
+
);
|
|
44953
|
+
}
|
|
44954
|
+
function computeFileDigest(data) {
|
|
44955
|
+
const digestBytes = libsodium_wrappers_default.crypto_generichash(32, data);
|
|
44956
|
+
return libsodium_wrappers_default.to_hex(digestBytes);
|
|
44957
|
+
}
|
|
44958
|
+
|
|
44923
44959
|
// src/crypto-helpers.ts
|
|
44924
44960
|
function hexToBytes(hex) {
|
|
44925
44961
|
return libsodium_wrappers_default.from_hex(hex);
|
|
@@ -45241,7 +45277,11 @@ var SecureChannel = class _SecureChannel extends EventEmitter {
|
|
|
45241
45277
|
res.end(JSON.stringify({ ok: false, error: "Missing 'text' field" }));
|
|
45242
45278
|
return;
|
|
45243
45279
|
}
|
|
45244
|
-
|
|
45280
|
+
if (parsed.file_path && typeof parsed.file_path === "string") {
|
|
45281
|
+
await this.sendWithAttachment(text, parsed.file_path, { topicId: parsed.topicId });
|
|
45282
|
+
} else {
|
|
45283
|
+
await this.send(text, { topicId: parsed.topicId });
|
|
45284
|
+
}
|
|
45245
45285
|
res.writeHead(200, { "Content-Type": "application/json" });
|
|
45246
45286
|
res.end(JSON.stringify({ ok: true }));
|
|
45247
45287
|
} catch (err) {
|
|
@@ -45594,10 +45634,14 @@ var SecureChannel = class _SecureChannel extends EventEmitter {
|
|
|
45594
45634
|
}
|
|
45595
45635
|
let messageText;
|
|
45596
45636
|
let messageType;
|
|
45637
|
+
let attachmentInfo = null;
|
|
45597
45638
|
try {
|
|
45598
45639
|
const parsed = JSON.parse(plaintext);
|
|
45599
45640
|
messageType = parsed.type || "message";
|
|
45600
45641
|
messageText = parsed.text || plaintext;
|
|
45642
|
+
if (parsed.attachment) {
|
|
45643
|
+
attachmentInfo = parsed.attachment;
|
|
45644
|
+
}
|
|
45601
45645
|
} catch {
|
|
45602
45646
|
messageType = "message";
|
|
45603
45647
|
messageText = plaintext;
|
|
@@ -45610,15 +45654,29 @@ var SecureChannel = class _SecureChannel extends EventEmitter {
|
|
|
45610
45654
|
}
|
|
45611
45655
|
if (messageType === "message") {
|
|
45612
45656
|
const topicId = msgData.topic_id;
|
|
45657
|
+
let attachmentPath;
|
|
45658
|
+
if (attachmentInfo) {
|
|
45659
|
+
try {
|
|
45660
|
+
attachmentPath = await this._downloadAndDecryptAttachment(attachmentInfo);
|
|
45661
|
+
} catch (err) {
|
|
45662
|
+
console.error(`[SecureChannel] Failed to download attachment:`, err);
|
|
45663
|
+
}
|
|
45664
|
+
}
|
|
45613
45665
|
this._appendHistory("owner", messageText, topicId);
|
|
45666
|
+
let emitText = messageText;
|
|
45667
|
+
if (attachmentPath) {
|
|
45668
|
+
emitText = `[Attachment: ${attachmentInfo.filename} saved to ${attachmentPath}]
|
|
45669
|
+
|
|
45670
|
+
${messageText}`;
|
|
45671
|
+
}
|
|
45614
45672
|
const metadata = {
|
|
45615
45673
|
messageId: msgData.message_id,
|
|
45616
45674
|
conversationId: convId,
|
|
45617
45675
|
timestamp: msgData.created_at,
|
|
45618
45676
|
topicId
|
|
45619
45677
|
};
|
|
45620
|
-
this.emit("message",
|
|
45621
|
-
this.config.onMessage?.(
|
|
45678
|
+
this.emit("message", emitText, metadata);
|
|
45679
|
+
this.config.onMessage?.(emitText, metadata);
|
|
45622
45680
|
await this._relaySyncToSiblings(convId, session.ownerDeviceId, messageText, topicId);
|
|
45623
45681
|
}
|
|
45624
45682
|
if (this._persisted) {
|
|
@@ -45626,6 +45684,111 @@ var SecureChannel = class _SecureChannel extends EventEmitter {
|
|
|
45626
45684
|
}
|
|
45627
45685
|
await this._persistState();
|
|
45628
45686
|
}
|
|
45687
|
+
/**
|
|
45688
|
+
* Download an encrypted attachment blob, decrypt it, verify integrity,
|
|
45689
|
+
* and save the plaintext file to disk.
|
|
45690
|
+
*/
|
|
45691
|
+
async _downloadAndDecryptAttachment(info) {
|
|
45692
|
+
const attachDir = join2(this.config.dataDir, "attachments");
|
|
45693
|
+
await mkdir2(attachDir, { recursive: true });
|
|
45694
|
+
const url = `${this.config.apiUrl}${info.blobUrl}`;
|
|
45695
|
+
const res = await fetch(url, {
|
|
45696
|
+
headers: { Authorization: `Bearer ${this._deviceJwt}` }
|
|
45697
|
+
});
|
|
45698
|
+
if (!res.ok) {
|
|
45699
|
+
throw new Error(`Attachment download failed: ${res.status}`);
|
|
45700
|
+
}
|
|
45701
|
+
const buffer = await res.arrayBuffer();
|
|
45702
|
+
const encryptedData = new Uint8Array(buffer);
|
|
45703
|
+
const digest = computeFileDigest(encryptedData);
|
|
45704
|
+
if (digest !== info.digest) {
|
|
45705
|
+
throw new Error("Attachment digest mismatch \u2014 possible tampering");
|
|
45706
|
+
}
|
|
45707
|
+
const fileKey = base64ToBytes(info.fileKey);
|
|
45708
|
+
const fileNonce = base64ToBytes(info.fileNonce);
|
|
45709
|
+
const decrypted = decryptFile(encryptedData, fileKey, fileNonce);
|
|
45710
|
+
const filePath = join2(attachDir, info.filename);
|
|
45711
|
+
await writeFile2(filePath, decrypted);
|
|
45712
|
+
console.log(`[SecureChannel] Attachment saved: ${filePath} (${decrypted.length} bytes)`);
|
|
45713
|
+
return filePath;
|
|
45714
|
+
}
|
|
45715
|
+
/**
|
|
45716
|
+
* Upload an attachment file: encrypt, upload to server, return metadata
|
|
45717
|
+
* for inclusion in the message envelope.
|
|
45718
|
+
*/
|
|
45719
|
+
async _uploadAttachment(filePath, conversationId) {
|
|
45720
|
+
const data = await readFile2(filePath);
|
|
45721
|
+
const plainData = new Uint8Array(data);
|
|
45722
|
+
const result = encryptFile(plainData);
|
|
45723
|
+
const { Blob: NodeBlob, FormData: NodeFormData } = await import("node:buffer").then(
|
|
45724
|
+
() => globalThis
|
|
45725
|
+
);
|
|
45726
|
+
const formData = new FormData();
|
|
45727
|
+
formData.append("conversation_id", conversationId);
|
|
45728
|
+
formData.append(
|
|
45729
|
+
"file",
|
|
45730
|
+
new Blob([result.encryptedData.buffer], { type: "application/octet-stream" }),
|
|
45731
|
+
"attachment.bin"
|
|
45732
|
+
);
|
|
45733
|
+
const res = await fetch(`${this.config.apiUrl}/api/v1/attachments/upload`, {
|
|
45734
|
+
method: "POST",
|
|
45735
|
+
headers: { Authorization: `Bearer ${this._deviceJwt}` },
|
|
45736
|
+
body: formData
|
|
45737
|
+
});
|
|
45738
|
+
if (!res.ok) {
|
|
45739
|
+
const detail = await res.text();
|
|
45740
|
+
throw new Error(`Attachment upload failed (${res.status}): ${detail}`);
|
|
45741
|
+
}
|
|
45742
|
+
const resp = await res.json();
|
|
45743
|
+
const filename = filePath.split("/").pop() || "file";
|
|
45744
|
+
return {
|
|
45745
|
+
blobId: resp.blob_id,
|
|
45746
|
+
blobUrl: resp.blob_url,
|
|
45747
|
+
fileKey: bytesToBase64(result.fileKey),
|
|
45748
|
+
fileNonce: bytesToBase64(result.fileNonce),
|
|
45749
|
+
digest: result.digest,
|
|
45750
|
+
filename,
|
|
45751
|
+
mime: "application/octet-stream",
|
|
45752
|
+
size: plainData.length
|
|
45753
|
+
};
|
|
45754
|
+
}
|
|
45755
|
+
/**
|
|
45756
|
+
* Send a message with an attached file. Encrypts the file, uploads it,
|
|
45757
|
+
* then sends the envelope with attachment metadata via Double Ratchet.
|
|
45758
|
+
*/
|
|
45759
|
+
async sendWithAttachment(plaintext, filePath, options) {
|
|
45760
|
+
if (this._state !== "ready" || this._sessions.size === 0 || !this._ws) {
|
|
45761
|
+
throw new Error("Channel is not ready");
|
|
45762
|
+
}
|
|
45763
|
+
const topicId = options?.topicId ?? this._persisted?.defaultTopicId;
|
|
45764
|
+
const attachMeta = await this._uploadAttachment(filePath, this._primaryConversationId);
|
|
45765
|
+
const envelope = JSON.stringify({
|
|
45766
|
+
type: "message",
|
|
45767
|
+
text: plaintext,
|
|
45768
|
+
topicId,
|
|
45769
|
+
attachment: attachMeta
|
|
45770
|
+
});
|
|
45771
|
+
this._appendHistory("agent", plaintext, topicId);
|
|
45772
|
+
const messageGroupId = randomUUID();
|
|
45773
|
+
for (const [convId, session] of this._sessions) {
|
|
45774
|
+
if (!session.activated) continue;
|
|
45775
|
+
const encrypted = session.ratchet.encrypt(envelope);
|
|
45776
|
+
const transport = encryptedMessageToTransport(encrypted);
|
|
45777
|
+
this._ws.send(
|
|
45778
|
+
JSON.stringify({
|
|
45779
|
+
event: "message",
|
|
45780
|
+
data: {
|
|
45781
|
+
conversation_id: convId,
|
|
45782
|
+
header_blob: transport.header_blob,
|
|
45783
|
+
ciphertext: transport.ciphertext,
|
|
45784
|
+
message_group_id: messageGroupId,
|
|
45785
|
+
topic_id: topicId
|
|
45786
|
+
}
|
|
45787
|
+
})
|
|
45788
|
+
);
|
|
45789
|
+
}
|
|
45790
|
+
await this._persistState();
|
|
45791
|
+
}
|
|
45629
45792
|
/**
|
|
45630
45793
|
* Relay an owner's message to all sibling sessions as encrypted sync messages.
|
|
45631
45794
|
* This allows all owner devices to see messages from any single device.
|