import { encryptAes128gcm, b64d, b64u } from "./lib_ece.ts";
const sub = JSON.parse(await Deno.readTextFile(Deno.args[0]));
const payload = Deno.args[1];
// fresh ephemeral sender keypair + salt (as a real server must do per message)
const kp = await crypto.subtle.generateKey({ name: "ECDH", namedCurve: "P-256" }, true, ["deriveBits"]);
const asPublic = new Uint8Array(await crypto.subtle.exportKey("raw", kp.publicKey));
const jwk = await crypto.subtle.exportKey("jwk", kp.privateKey);
const asPrivate = b64d(jwk.d!);
const salt = crypto.getRandomValues(new Uint8Array(16));
const r = await encryptAes128gcm({
  plaintext: new TextEncoder().encode(payload),
  uaPublic: b64d(sub.keys.p256dh), authSecret: b64d(sub.keys.auth),
  asPrivate, asPublic, salt, rs: 4096,
});
console.log(b64u(r.body));
