aicq-chat-plugin 3.9.0 → 3.9.1
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/README.md +80 -80
- package/SKILL.md +78 -78
- package/cli.cjs +356 -356
- package/index.js +417 -385
- package/lib/chat.js +854 -971
- package/lib/crypto.js +168 -168
- package/lib/database.js +455 -455
- package/lib/file-transfer.js +266 -266
- package/lib/handshake.js +147 -147
- package/lib/identity.js +165 -165
- package/lib/package.json +3 -3
- package/lib/server-client.js +380 -337
- package/openclaw.plugin.json +170 -168
- package/package.json +87 -87
- package/postinstall.cjs +27 -27
- package/public/favicon.ico +0 -0
- package/public/icon-16.png +0 -0
- package/public/icon-32.png +0 -0
- package/public/index.html +1468 -1468
- package/public/logo-512.png +0 -0
- package/setup-entry.js +14 -14
- package/src/channel.js +616 -637
- package/src/ui-routes.js +647 -594
package/lib/crypto.js
CHANGED
|
@@ -1,168 +1,168 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* AICQ Crypto Utilities
|
|
3
|
-
* NaCl-based E2EE: Ed25519 signing, X25519 key exchange, symmetric encryption
|
|
4
|
-
*/
|
|
5
|
-
const nacl = require('tweetnacl');
|
|
6
|
-
const naclUtil = require('tweetnacl-util');
|
|
7
|
-
|
|
8
|
-
// ─── Key Generation ────────────────────────────────────────────────────
|
|
9
|
-
|
|
10
|
-
function generateSigningKeypair() {
|
|
11
|
-
const keyPair = nacl.sign.keyPair();
|
|
12
|
-
return {
|
|
13
|
-
publicKey: Buffer.from(keyPair.publicKey).toString('hex'),
|
|
14
|
-
secretKey: Buffer.from(keyPair.secretKey).toString('hex'),
|
|
15
|
-
publicKeyB64: Buffer.from(keyPair.publicKey).toString('base64'),
|
|
16
|
-
secretKeyB64: Buffer.from(keyPair.secretKey).toString('base64'),
|
|
17
|
-
};
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
function generateExchangeKeypair() {
|
|
21
|
-
const keyPair = nacl.box.keyPair();
|
|
22
|
-
return {
|
|
23
|
-
publicKey: Buffer.from(keyPair.publicKey).toString('hex'),
|
|
24
|
-
secretKey: Buffer.from(keyPair.secretKey).toString('hex'),
|
|
25
|
-
publicKeyB64: Buffer.from(keyPair.publicKey).toString('base64'),
|
|
26
|
-
secretKeyB64: Buffer.from(keyPair.secretKey).toString('base64'),
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
// ─── Signing ───────────────────────────────────────────────────────────
|
|
31
|
-
|
|
32
|
-
function signMessage(message, secretKeyHex) {
|
|
33
|
-
const secretKey = Buffer.from(secretKeyHex, 'hex');
|
|
34
|
-
// If message looks like hex (64 chars), treat as raw bytes to match server's bytes.fromhex()
|
|
35
|
-
let messageBytes;
|
|
36
|
-
if (/^[0-9a-fA-F]{64}$/.test(message)) {
|
|
37
|
-
messageBytes = Buffer.from(message, 'hex');
|
|
38
|
-
} else {
|
|
39
|
-
messageBytes = naclUtil.decodeUTF8(message);
|
|
40
|
-
}
|
|
41
|
-
const signature = nacl.sign.detached(messageBytes, secretKey);
|
|
42
|
-
return Buffer.from(signature).toString('hex');
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
function verifySignature(message, signatureHex, publicKeyHex) {
|
|
46
|
-
try {
|
|
47
|
-
const publicKey = Buffer.from(publicKeyHex, 'hex');
|
|
48
|
-
// If message looks like hex (64 chars), treat as raw bytes to match server
|
|
49
|
-
let messageBytes;
|
|
50
|
-
if (/^[0-9a-fA-F]{64}$/.test(message)) {
|
|
51
|
-
messageBytes = Buffer.from(message, 'hex');
|
|
52
|
-
} else {
|
|
53
|
-
messageBytes = naclUtil.decodeUTF8(message);
|
|
54
|
-
}
|
|
55
|
-
const signature = Buffer.from(signatureHex, 'hex');
|
|
56
|
-
return nacl.sign.detached.verify(messageBytes, signature, publicKey);
|
|
57
|
-
} catch (e) {
|
|
58
|
-
return false;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
// ─── Key Exchange & Session Key Derivation ─────────────────────────────
|
|
63
|
-
|
|
64
|
-
function deriveSessionKey(ourSecretKeyHex, theirPublicKeyHex) {
|
|
65
|
-
const ourSecret = Buffer.from(ourSecretKeyHex, 'hex');
|
|
66
|
-
const theirPublic = Buffer.from(theirPublicKeyHex, 'hex');
|
|
67
|
-
const shared = nacl.box.before(theirPublic, ourSecret);
|
|
68
|
-
const hash = nacl.hash(shared);
|
|
69
|
-
return Buffer.from(hash).toString('hex');
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// ─── Symmetric Encryption (NaCl SecretBox) ─────────────────────────────
|
|
73
|
-
|
|
74
|
-
function encryptMessage(plaintext, sessionKeyB64) {
|
|
75
|
-
const key = Buffer.from(sessionKeyB64, 'base64');
|
|
76
|
-
const nonce = nacl.randomBytes(nacl.secretbox.nonceLength);
|
|
77
|
-
const messageBytes = naclUtil.decodeUTF8(plaintext);
|
|
78
|
-
const encrypted = nacl.secretbox(messageBytes, nonce, key);
|
|
79
|
-
if (!encrypted) throw new Error('Encryption failed');
|
|
80
|
-
// Combine nonce + ciphertext
|
|
81
|
-
const combined = new Uint8Array(nonce.length + encrypted.length);
|
|
82
|
-
combined.set(nonce);
|
|
83
|
-
combined.set(encrypted, nonce.length);
|
|
84
|
-
return Buffer.from(combined).toString('base64');
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
function decryptMessage(ciphertextB64, sessionKeyB64) {
|
|
88
|
-
const key = Buffer.from(sessionKeyB64, 'base64');
|
|
89
|
-
const combined = Buffer.from(ciphertextB64, 'base64');
|
|
90
|
-
const nonce = combined.slice(0, nacl.secretbox.nonceLength);
|
|
91
|
-
const ciphertext = combined.slice(nacl.secretbox.nonceLength);
|
|
92
|
-
const decrypted = nacl.secretbox.open(ciphertext, nonce, key);
|
|
93
|
-
if (!decrypted) throw new Error('Decryption failed');
|
|
94
|
-
return naclUtil.encodeUTF8(decrypted);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
// ─── Fingerprint ───────────────────────────────────────────────────────
|
|
98
|
-
|
|
99
|
-
function computeFingerprint(publicKeyHex) {
|
|
100
|
-
const publicKey = Buffer.from(publicKeyHex, 'hex');
|
|
101
|
-
const hash = nacl.hash(publicKey);
|
|
102
|
-
const hex = Buffer.from(hash).toString('hex');
|
|
103
|
-
return hex.match(/.{2}/g).join(':');
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
// ─── Password-based Encryption ─────────────────────────────────────────
|
|
107
|
-
|
|
108
|
-
function encryptWithPassword(plaintext, password) {
|
|
109
|
-
const salt = nacl.randomBytes(nacl.pwhash.saltbytes);
|
|
110
|
-
const key = nacl.pwhash(plaintext.length + nacl.secretbox.overheadLength,
|
|
111
|
-
naclUtil.decodeUTF8(password), salt,
|
|
112
|
-
nacl.pwhash.opslimit.interactive,
|
|
113
|
-
nacl.pwhash.memlimit.interactive);
|
|
114
|
-
// Simplified: use secretbox with derived key
|
|
115
|
-
const nonce = nacl.randomBytes(nacl.secretbox.nonceLength);
|
|
116
|
-
const messageBytes = naclUtil.decodeUTF8(plaintext);
|
|
117
|
-
// Fallback: just use a hash-based key
|
|
118
|
-
const keyHash = nacl.hash(naclUtil.decodeUTF8(password + Buffer.from(salt).toString('base64')));
|
|
119
|
-
const encrypted = nacl.secretbox(messageBytes, nonce, keyHash.slice(0, 32));
|
|
120
|
-
if (!encrypted) throw new Error('Password encryption failed');
|
|
121
|
-
const result = new Uint8Array(salt.length + nonce.length + encrypted.length);
|
|
122
|
-
result.set(salt);
|
|
123
|
-
result.set(nonce, salt.length);
|
|
124
|
-
result.set(encrypted, salt.length + nonce.length);
|
|
125
|
-
return Buffer.from(result).toString('base64');
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
function decryptWithPassword(ciphertextB64, password) {
|
|
129
|
-
const combined = Buffer.from(ciphertextB64, 'base64');
|
|
130
|
-
const salt = combined.slice(0, nacl.pwhash.saltbytes);
|
|
131
|
-
const nonce = combined.slice(nacl.pwhash.saltbytes, nacl.pwhash.saltbytes + nacl.secretbox.nonceLength);
|
|
132
|
-
const ciphertext = combined.slice(nacl.pwhash.saltbytes + nacl.secretbox.nonceLength);
|
|
133
|
-
const keyHash = nacl.hash(naclUtil.decodeUTF8(password + Buffer.from(salt).toString('base64')));
|
|
134
|
-
const decrypted = nacl.secretbox.open(ciphertext, nonce, keyHash.slice(0, 32));
|
|
135
|
-
if (!decrypted) throw new Error('Password decryption failed');
|
|
136
|
-
return naclUtil.encodeUTF8(decrypted);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
// ─── Convert Ed25519 to X25519 ─────────────────────────────────────────
|
|
140
|
-
|
|
141
|
-
function convertEd25519ToX25519(ed25519PublicKeyB64) {
|
|
142
|
-
const edPk = Buffer.from(ed25519PublicKeyB64, 'base64');
|
|
143
|
-
// NaCl provides convert for this
|
|
144
|
-
try {
|
|
145
|
-
const xPk = nacl.sign.keyPair.fromSeed(edPk.slice(0, 32)).publicKey;
|
|
146
|
-
// This is approximate - in production use proper conversion
|
|
147
|
-
return Buffer.from(xPk).toString('base64');
|
|
148
|
-
} catch(e) {
|
|
149
|
-
// Fallback: derive from hash
|
|
150
|
-
const hash = nacl.hash(edPk);
|
|
151
|
-
return Buffer.from(hash.slice(0, 32)).toString('base64');
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
module.exports = {
|
|
156
|
-
generateSigningKeypair,
|
|
157
|
-
generateExchangeKeypair,
|
|
158
|
-
signMessage,
|
|
159
|
-
verifySignature,
|
|
160
|
-
deriveSessionKey,
|
|
161
|
-
encryptMessage,
|
|
162
|
-
decryptMessage,
|
|
163
|
-
computeFingerprint,
|
|
164
|
-
encryptWithPassword,
|
|
165
|
-
decryptWithPassword,
|
|
166
|
-
convertEd25519ToX25519,
|
|
167
|
-
randomBytes: (n) => Buffer.from(nacl.randomBytes(n)).toString('base64'),
|
|
168
|
-
};
|
|
1
|
+
/**
|
|
2
|
+
* AICQ Crypto Utilities
|
|
3
|
+
* NaCl-based E2EE: Ed25519 signing, X25519 key exchange, symmetric encryption
|
|
4
|
+
*/
|
|
5
|
+
const nacl = require('tweetnacl');
|
|
6
|
+
const naclUtil = require('tweetnacl-util');
|
|
7
|
+
|
|
8
|
+
// ─── Key Generation ────────────────────────────────────────────────────
|
|
9
|
+
|
|
10
|
+
function generateSigningKeypair() {
|
|
11
|
+
const keyPair = nacl.sign.keyPair();
|
|
12
|
+
return {
|
|
13
|
+
publicKey: Buffer.from(keyPair.publicKey).toString('hex'),
|
|
14
|
+
secretKey: Buffer.from(keyPair.secretKey).toString('hex'),
|
|
15
|
+
publicKeyB64: Buffer.from(keyPair.publicKey).toString('base64'),
|
|
16
|
+
secretKeyB64: Buffer.from(keyPair.secretKey).toString('base64'),
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function generateExchangeKeypair() {
|
|
21
|
+
const keyPair = nacl.box.keyPair();
|
|
22
|
+
return {
|
|
23
|
+
publicKey: Buffer.from(keyPair.publicKey).toString('hex'),
|
|
24
|
+
secretKey: Buffer.from(keyPair.secretKey).toString('hex'),
|
|
25
|
+
publicKeyB64: Buffer.from(keyPair.publicKey).toString('base64'),
|
|
26
|
+
secretKeyB64: Buffer.from(keyPair.secretKey).toString('base64'),
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// ─── Signing ───────────────────────────────────────────────────────────
|
|
31
|
+
|
|
32
|
+
function signMessage(message, secretKeyHex) {
|
|
33
|
+
const secretKey = Buffer.from(secretKeyHex, 'hex');
|
|
34
|
+
// If message looks like hex (64 chars), treat as raw bytes to match server's bytes.fromhex()
|
|
35
|
+
let messageBytes;
|
|
36
|
+
if (/^[0-9a-fA-F]{64}$/.test(message)) {
|
|
37
|
+
messageBytes = Buffer.from(message, 'hex');
|
|
38
|
+
} else {
|
|
39
|
+
messageBytes = naclUtil.decodeUTF8(message);
|
|
40
|
+
}
|
|
41
|
+
const signature = nacl.sign.detached(messageBytes, secretKey);
|
|
42
|
+
return Buffer.from(signature).toString('hex');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function verifySignature(message, signatureHex, publicKeyHex) {
|
|
46
|
+
try {
|
|
47
|
+
const publicKey = Buffer.from(publicKeyHex, 'hex');
|
|
48
|
+
// If message looks like hex (64 chars), treat as raw bytes to match server
|
|
49
|
+
let messageBytes;
|
|
50
|
+
if (/^[0-9a-fA-F]{64}$/.test(message)) {
|
|
51
|
+
messageBytes = Buffer.from(message, 'hex');
|
|
52
|
+
} else {
|
|
53
|
+
messageBytes = naclUtil.decodeUTF8(message);
|
|
54
|
+
}
|
|
55
|
+
const signature = Buffer.from(signatureHex, 'hex');
|
|
56
|
+
return nacl.sign.detached.verify(messageBytes, signature, publicKey);
|
|
57
|
+
} catch (e) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// ─── Key Exchange & Session Key Derivation ─────────────────────────────
|
|
63
|
+
|
|
64
|
+
function deriveSessionKey(ourSecretKeyHex, theirPublicKeyHex) {
|
|
65
|
+
const ourSecret = Buffer.from(ourSecretKeyHex, 'hex');
|
|
66
|
+
const theirPublic = Buffer.from(theirPublicKeyHex, 'hex');
|
|
67
|
+
const shared = nacl.box.before(theirPublic, ourSecret);
|
|
68
|
+
const hash = nacl.hash(shared);
|
|
69
|
+
return Buffer.from(hash).toString('hex');
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// ─── Symmetric Encryption (NaCl SecretBox) ─────────────────────────────
|
|
73
|
+
|
|
74
|
+
function encryptMessage(plaintext, sessionKeyB64) {
|
|
75
|
+
const key = Buffer.from(sessionKeyB64, 'base64');
|
|
76
|
+
const nonce = nacl.randomBytes(nacl.secretbox.nonceLength);
|
|
77
|
+
const messageBytes = naclUtil.decodeUTF8(plaintext);
|
|
78
|
+
const encrypted = nacl.secretbox(messageBytes, nonce, key);
|
|
79
|
+
if (!encrypted) throw new Error('Encryption failed');
|
|
80
|
+
// Combine nonce + ciphertext
|
|
81
|
+
const combined = new Uint8Array(nonce.length + encrypted.length);
|
|
82
|
+
combined.set(nonce);
|
|
83
|
+
combined.set(encrypted, nonce.length);
|
|
84
|
+
return Buffer.from(combined).toString('base64');
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function decryptMessage(ciphertextB64, sessionKeyB64) {
|
|
88
|
+
const key = Buffer.from(sessionKeyB64, 'base64');
|
|
89
|
+
const combined = Buffer.from(ciphertextB64, 'base64');
|
|
90
|
+
const nonce = combined.slice(0, nacl.secretbox.nonceLength);
|
|
91
|
+
const ciphertext = combined.slice(nacl.secretbox.nonceLength);
|
|
92
|
+
const decrypted = nacl.secretbox.open(ciphertext, nonce, key);
|
|
93
|
+
if (!decrypted) throw new Error('Decryption failed');
|
|
94
|
+
return naclUtil.encodeUTF8(decrypted);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// ─── Fingerprint ───────────────────────────────────────────────────────
|
|
98
|
+
|
|
99
|
+
function computeFingerprint(publicKeyHex) {
|
|
100
|
+
const publicKey = Buffer.from(publicKeyHex, 'hex');
|
|
101
|
+
const hash = nacl.hash(publicKey);
|
|
102
|
+
const hex = Buffer.from(hash).toString('hex');
|
|
103
|
+
return hex.match(/.{2}/g).join(':');
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// ─── Password-based Encryption ─────────────────────────────────────────
|
|
107
|
+
|
|
108
|
+
function encryptWithPassword(plaintext, password) {
|
|
109
|
+
const salt = nacl.randomBytes(nacl.pwhash.saltbytes);
|
|
110
|
+
const key = nacl.pwhash(plaintext.length + nacl.secretbox.overheadLength,
|
|
111
|
+
naclUtil.decodeUTF8(password), salt,
|
|
112
|
+
nacl.pwhash.opslimit.interactive,
|
|
113
|
+
nacl.pwhash.memlimit.interactive);
|
|
114
|
+
// Simplified: use secretbox with derived key
|
|
115
|
+
const nonce = nacl.randomBytes(nacl.secretbox.nonceLength);
|
|
116
|
+
const messageBytes = naclUtil.decodeUTF8(plaintext);
|
|
117
|
+
// Fallback: just use a hash-based key
|
|
118
|
+
const keyHash = nacl.hash(naclUtil.decodeUTF8(password + Buffer.from(salt).toString('base64')));
|
|
119
|
+
const encrypted = nacl.secretbox(messageBytes, nonce, keyHash.slice(0, 32));
|
|
120
|
+
if (!encrypted) throw new Error('Password encryption failed');
|
|
121
|
+
const result = new Uint8Array(salt.length + nonce.length + encrypted.length);
|
|
122
|
+
result.set(salt);
|
|
123
|
+
result.set(nonce, salt.length);
|
|
124
|
+
result.set(encrypted, salt.length + nonce.length);
|
|
125
|
+
return Buffer.from(result).toString('base64');
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function decryptWithPassword(ciphertextB64, password) {
|
|
129
|
+
const combined = Buffer.from(ciphertextB64, 'base64');
|
|
130
|
+
const salt = combined.slice(0, nacl.pwhash.saltbytes);
|
|
131
|
+
const nonce = combined.slice(nacl.pwhash.saltbytes, nacl.pwhash.saltbytes + nacl.secretbox.nonceLength);
|
|
132
|
+
const ciphertext = combined.slice(nacl.pwhash.saltbytes + nacl.secretbox.nonceLength);
|
|
133
|
+
const keyHash = nacl.hash(naclUtil.decodeUTF8(password + Buffer.from(salt).toString('base64')));
|
|
134
|
+
const decrypted = nacl.secretbox.open(ciphertext, nonce, keyHash.slice(0, 32));
|
|
135
|
+
if (!decrypted) throw new Error('Password decryption failed');
|
|
136
|
+
return naclUtil.encodeUTF8(decrypted);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// ─── Convert Ed25519 to X25519 ─────────────────────────────────────────
|
|
140
|
+
|
|
141
|
+
function convertEd25519ToX25519(ed25519PublicKeyB64) {
|
|
142
|
+
const edPk = Buffer.from(ed25519PublicKeyB64, 'base64');
|
|
143
|
+
// NaCl provides convert for this
|
|
144
|
+
try {
|
|
145
|
+
const xPk = nacl.sign.keyPair.fromSeed(edPk.slice(0, 32)).publicKey;
|
|
146
|
+
// This is approximate - in production use proper conversion
|
|
147
|
+
return Buffer.from(xPk).toString('base64');
|
|
148
|
+
} catch(e) {
|
|
149
|
+
// Fallback: derive from hash
|
|
150
|
+
const hash = nacl.hash(edPk);
|
|
151
|
+
return Buffer.from(hash.slice(0, 32)).toString('base64');
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
module.exports = {
|
|
156
|
+
generateSigningKeypair,
|
|
157
|
+
generateExchangeKeypair,
|
|
158
|
+
signMessage,
|
|
159
|
+
verifySignature,
|
|
160
|
+
deriveSessionKey,
|
|
161
|
+
encryptMessage,
|
|
162
|
+
decryptMessage,
|
|
163
|
+
computeFingerprint,
|
|
164
|
+
encryptWithPassword,
|
|
165
|
+
decryptWithPassword,
|
|
166
|
+
convertEd25519ToX25519,
|
|
167
|
+
randomBytes: (n) => Buffer.from(nacl.randomBytes(n)).toString('base64'),
|
|
168
|
+
};
|