From 21b574f60c4f20a359559ba6a202c89d178b4e45 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Wed, 22 Jan 2025 00:08:20 +0800 Subject: [PATCH] feat: add post-note.ts --- script-meta-v2.json | 9 ++++++ single-scripts/post-note.ts | 59 +++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100755 single-scripts/post-note.ts diff --git a/script-meta-v2.json b/script-meta-v2.json index 635eb70..5d1161a 100644 --- a/script-meta-v2.json +++ b/script-meta-v2.json @@ -42,6 +42,15 @@ "publish_time": 1737272626141, "update_time": 1737285822488 }, + "post-note.ts": { + "script_name": "post-note.ts", + "script_length": 1903, + "script_sha256": "c66d114a5028ff57652c26d042cf070639e6fed81c4c2c2108ce4c591bc49384", + "script_full_url": "https://git.hatter.ink/hatter/ts-scripts/raw/branch/main/single-scripts/post-note.ts", + "single_script_file": true, + "publish_time": 1737475603673, + "update_time": 1737475655526 + }, "print-env.ts": { "script_name": "print-env.ts", "script_length": 156, diff --git a/single-scripts/post-note.ts b/single-scripts/post-note.ts new file mode 100755 index 0000000..53304d9 --- /dev/null +++ b/single-scripts/post-note.ts @@ -0,0 +1,59 @@ +#!/usr/bin/env -S deno run --allow-env --allow-import --allow-net + +import { parseArgs } from "jsr:@std/cli/parse-args"; +import { encryptEcdhP256 } from "https://hatter.ink/script/get/@0/deno-wrapkey-mod.ts"; +import { log } from "https://hatter.ink/script/get/@1/deno-commons-mod.ts"; +import { fetchWithTimoutAndAutoProxy } from "https://hatter.ink/script/get/@0/deno-fetch-auto-proxy-mod.ts"; + +const endpoint = Deno.env.get("POST_ENCRYPTED_NOTE_ENDPOINT"); +const publicKeyPointHex = Deno.env.get("ENCRYPTION_PUBLIC_KEY_POINT_HEX"); + +if (!endpoint || !publicKeyPointHex) { + log.error("Endpoint and public key point hex must both assigned."); + if (!endpoint) { + log.info("POST_ENCRYPTED_NOTE_ENDPOINT must be assigned."); + } + if (!publicKeyPointHex) { + log.info("ENCRYPTION_PUBLIC_KEY_POINT_HEX must be assigned."); + } + Deno.exit(1); +} + +const flags = parseArgs(Deno.args, { + boolean: ["help"], + string: ["title"], +}); + +if (flags.help) { + console.log("post-note.ts [--title ] <message>"); + Deno.exit(0); +} + +if (flags._.length === 0) { + log.error("Requires message."); + Deno.exit(1); +} + +const args = []; +if (flags.title) { + const encryptedTitle = await encryptEcdhP256( + "k", + publicKeyPointHex, + new TextEncoder().encode(flags.title), + ); + args.push(`title=${encodeURIComponent(encryptedTitle.toString())}`); +} +const encryptedMessage = await encryptEcdhP256( + "k", + publicKeyPointHex, + new TextEncoder().encode(flags._.join("\n")), +); +args.push(`memo=${encodeURIComponent(encryptedMessage.toString())}`); + +const response = await fetchWithTimoutAndAutoProxy( + `${endpoint}?${args.join("&")}`, +); + +console.info(response.status, await response.json()); + +// @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20250122T000717+08:00.MEYCIQCJfSI5Ya9590XLCYhFks7+QiF5xKaf/YQlasdcXXfPlAIhAJ2leR/iyeAGqOMbNjHyfh4W92oDF1EuywQ3vfzPzmc5