Files
ts-scripts/single-scripts/post-note.ts

61 lines
1.9 KiB
TypeScript
Executable File

#!/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 <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.20250123T002653+08:00.MEYCIQCL51hGkl0Vcq+vb16J
// eyZd4vPAJ22t1Jizxrz6G/zA5QIhAJLrrggF4UFu3xbpch3c2tfaKlv5n+1k0Xl2R/OZi4Fp