@skyware/labeler
Version:
A lightweight alternative to Ozone for operating an atproto labeler.
28 lines (27 loc) • 1.04 kB
JavaScript
import { encode as cborEncode, toBytes } from "@atcute/cbor";
import { k256Sign } from "./crypto.js";
import { excludeNullish } from "./util.js";
const LABEL_VERSION = 1;
function formatLabelCbor(label) {
return excludeNullish({ ...label, ver: LABEL_VERSION, neg: !!label.neg });
}
export function formatLabel(label) {
const sig = label.sig instanceof ArrayBuffer
? toBytes(new Uint8Array(label.sig))
: label.sig instanceof Uint8Array
? toBytes(label.sig)
: label.sig;
if (!sig || !("$bytes" in sig)) {
throw new Error("Expected sig to be an object with base64 $bytes, got " + sig);
}
return excludeNullish({ ...label, ver: LABEL_VERSION, neg: !!label.neg, sig });
}
export function signLabel(label, signingKey) {
const toSign = formatLabelCbor(label);
const bytes = cborEncode(toSign);
const sig = k256Sign(signingKey, bytes);
return { ...toSign, sig };
}
export function labelIsSigned(label) {
return "sig" in label && label.sig !== undefined;
}