Skip to content

Commit

Permalink
chore: make run.ts independent from neuron (#3231)
Browse files Browse the repository at this point in the history
  • Loading branch information
homura authored Aug 11, 2024
1 parent 2169973 commit dc6d50c
Showing 1 changed file with 61 additions and 1 deletion.
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

1 comment on commit dc6d50c

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Packaging for test is done in 10339921060

Please sign in to comment.