UNPKG

everything-dev

Version:

A consolidated product package for building Module Federation apps with oRPC APIs.

81 lines (79 loc) • 3.89 kB
const require_engine = require("../verify/engine.cjs"); let node_crypto = require("node:crypto"); let near_kit = require("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 = (0, near_kit.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 require_engine.SignatureInvalid({ url, reason: "no signature — publish.verification demands attribution" }); if (signature.algorithm !== "ed25519") throw new require_engine.SignatureInvalid({ url, reason: `unsupported signature algorithm ${signature.algorithm}` }); const expected = nearKeyString(expectedPublicKey); if (nearKeyString(signature.publicKey) !== expected) throw new require_engine.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 require_engine.SignatureInvalid({ url, reason: `signature value is ${signatureBytes.length} bytes — ed25519 signatures are 64` }); if (!(0, node_crypto.verify)(null, new TextEncoder().encode(payload), ed25519PublicKeyObject(expected), signatureBytes)) throw new require_engine.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 = (0, near_kit.parsePublicKey)(nearKey); if (parsed.keyType !== 0) throw new require_engine.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 (0, node_crypto.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 exports.assertSigningIdentityMatches = assertSigningIdentityMatches; exports.nearKeyString = nearKeyString; exports.signManifestPayload = signManifestPayload; exports.verifyManifestSignature = verifyManifestSignature; //# sourceMappingURL=manifest-signature.cjs.map