feat(protocol): implement OIDB 0xcde_2 (RequestDbKey) (#69)
This commit is contained in:
1 parent
f2b7ef0056
commit
00ca582f1a
6 files changed
+146
-2
No files matched your search
@@ -1,3 +1,4 @@
|
||||
import { readFile } from 'node:fs/promises';
|
||||
import type { ApiActionContext, ApiHandler } from '../api-handler';
|
||||
import { asBoolean, asMessage, asNumber, asString } from '../api-handler';
|
||||
import type { ForwardPreviewMeta } from '../modules/message-actions';
|
||||
@@ -1175,6 +1176,35 @@ export function register(h: ApiHandler, ctx: ApiActionContext): void {
|
||||
return failedResponse(RETCODE.ACTION_FAILED, err instanceof Error ? err.message : String(err));
|
||||
}
|
||||
});
|
||||
|
||||
h.registerAction('request_decrypt_key', async (params) => {
|
||||
const dbPath = asString(params.db_path);
|
||||
if (!dbPath) {
|
||||
return failedResponse(RETCODE.BAD_REQUEST, 'db_path is required');
|
||||
}
|
||||
|
||||
try {
|
||||
const buffer = Buffer.alloc(128);
|
||||
const fileHandle = await readFile(dbPath);
|
||||
|
||||
if (fileHandle.length < 0xaf) {
|
||||
return failedResponse(RETCODE.ACTION_FAILED, 'Database file too short');
|
||||
}
|
||||
|
||||
fileHandle.copy(buffer, 0, 0x2f, 0xaf);
|
||||
const dbSalt = buffer.toString('utf8');
|
||||
|
||||
if (!/^[0-9a-fA-F]{128}$/.test(dbSalt)) {
|
||||
return failedResponse(RETCODE.ACTION_FAILED, 'Invalid db_salt: not a valid 128-character hex string');
|
||||
}
|
||||
|
||||
const dbKey = await ctx.bridge.apis.misc.getDecryptKey(dbSalt.toLowerCase());
|
||||
|
||||
return okResponse({ db_key: dbKey });
|
||||
} catch (err) {
|
||||
return failedResponse(RETCODE.ACTION_FAILED, err instanceof Error ? err.message : String(err));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function hexToBytes(hex: string): Uint8Array {
|
||||
|
||||
Reference in new issue
Block a user