everything-dev
Version:
A consolidated product package for building Module Federation apps with oRPC APIs.
78 lines (76 loc) • 3.65 kB
JavaScript
import { SignatureInvalid } from "../verify/engine.mjs";
import { createPublicKey, verify } from "node:crypto";
import { parseKey, parsePublicKey } from "near-kit";
//#region src/services/deploy/manifest-signature.ts
/** The payload's signature, made by the explicit publish identity (the
* NEAR-format secret key the registry write also signs with — env-provided
* or the keychain credentials file). */
function signManifestPayload(payload, privateKey) {
const key = parseKey(privateKey);
const signature = key.sign(new TextEncoder().encode(payload));
return {
algorithm: "ed25519",
publicKey: key.publicKey.toString(),
value: Buffer.from(signature.data).toString("base64")
};
}
/** Verify a manifest's attribution against the config's key record. Throws
* `SignatureInvalid` naming the failure mode — a missing signature, a
* foreign key (present in the document but never registered in the config),
* or a signature that does not verify. */
function verifyManifestSignature(payload, signature, expectedPublicKey) {
const url = "manifest signature";
if (!signature) throw new SignatureInvalid({
url,
reason: "no signature — publish.verification demands attribution"
});
if (signature.algorithm !== "ed25519") throw new SignatureInvalid({
url,
reason: `unsupported signature algorithm ${signature.algorithm}`
});
const expected = nearKeyString(expectedPublicKey);
if (nearKeyString(signature.publicKey) !== expected) throw new SignatureInvalid({
url,
reason: `signed by unregistered key ${signature.publicKey} — the config's key record names ${expected}`
});
const signatureBytes = Buffer.from(signature.value, "base64");
if (signatureBytes.length !== 64) throw new SignatureInvalid({
url,
reason: `signature value is ${signatureBytes.length} bytes — ed25519 signatures are 64`
});
if (!verify(null, new TextEncoder().encode(payload), ed25519PublicKeyObject(expected), signatureBytes)) throw new SignatureInvalid({
url,
reason: "signature does not verify against the key record"
});
}
/** Wrap a NEAR-format ed25519 public key (`ed25519:<base58>`) into the DER
* SubjectPublicKeyInfo form node:crypto's `verify` consumes. */
function ed25519PublicKeyObject(nearKey) {
const parsed = parsePublicKey(nearKey);
if (parsed.keyType !== 0) throw new SignatureInvalid({
url: "manifest signature",
reason: `key record is not ed25519 (keyType ${parsed.keyType})`
});
const spki = Buffer.concat([Buffer.from("302a300506032b6570032100", "hex"), Buffer.from(parsed.data)]);
return createPublicKey({
key: spki,
format: "der",
type: "spki"
});
}
/** Accept the key record in either NEAR format (`ed25519:<base58>`) or bare
* base58 — the config is hand-authored. */
function nearKeyString(key) {
return key.includes(":") ? key : `ed25519:${key}`;
}
/** Train-time guard: the identity about to sign must be the identity the
* config's key record names — a mismatch fails the deploy before anything
* uploads (the record and the signer drifting apart is exactly the
* attribution gap). */
function assertSigningIdentityMatches(signingPublicKey, expectedPublicKey) {
const expected = nearKeyString(expectedPublicKey);
if (nearKeyString(signingPublicKey) !== expected) throw new Error(`Manifest attribution mismatch: the publish identity signs with ${signingPublicKey} but publish.verification.publicKey names ${expected}. Update the config's key record (rotation is a config publish) or sign with the recorded identity.`);
}
//#endregion
export { assertSigningIdentityMatches, nearKeyString, signManifestPayload, verifyManifestSignature };
//# sourceMappingURL=manifest-signature.mjs.map