Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: make run.ts independent from neuron #3231

Merged
merged 1 commit into from
Aug 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 61 additions & 1 deletion scripts/admin/decrypt-log/run.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,64 @@
import { LogDecryption } from "../../../packages/neuron-wallet/src/services/log-encryption";
import { createDecipheriv, privateDecrypt } from "node:crypto";

export const DEFAULT_ALGORITHM = "aes-256-cbc";

export class LogDecryption {
private readonly adminPrivateKey: string;

constructor(adminPrivateKey: string) {
this.adminPrivateKey = adminPrivateKey;
}

decrypt(encryptedMessage: string): string {
const { iv, key, content } = parseMessage(encryptedMessage);

const decipher = createDecipheriv(
DEFAULT_ALGORITHM,
privateDecrypt(this.adminPrivateKey, Buffer.from(key, "base64")),
Buffer.from(iv, "base64")
);

return Buffer.concat([decipher.update(content, "base64"), decipher.final()]).toString("utf-8");
}
}

/**
* Parse a message into a JSON
*
* Input:
* ```
* [key1:value2] [key2:value2] remain content
* ```
* Output:
* ```json
* {
* "key1": "value1",
* "key2": "value2",
* "content": "remain content"
* }
* ```
* @param message
*/
function parseMessage(message: string) {
const result: Record<string, string> = {};
const regex = /\[([^\]:]+):([^\]]+)]/g;
let match;
let lastIndex = 0;

while ((match = regex.exec(message)) !== null) {
const [, key, value] = match;
result[key.trim()] = value.trim();
lastIndex = regex.lastIndex;
}

// Extract remaining content after the last bracket
const remainingContent = message.slice(lastIndex).trim();
if (remainingContent) {
result.content = remainingContent;
}

return result;
}

const ADMIN_PRIVATE_KEY = process.env
.ADMIN_PRIVATE_KEY!.split(/\r?\n/)
Expand Down