UNPKG

everything-dev

Version:

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

1 lines • 6.31 kB
{"version":3,"file":"manifest-signature.cjs","names":["parseKey","SignatureInvalid","verify","parsePublicKey","createPublicKey"],"sources":["../../../src/services/deploy/manifest-signature.ts"],"sourcesContent":["import { createPublicKey, verify } from \"node:crypto\";\nimport type { ManifestSignature } from \"every-plugin/version-manifest\";\nimport { parseKey, parsePublicKey } from \"near-kit\";\nimport { SignatureInvalid } from \"../verify/engine\";\n\n/**\n * Manifest attribution (ADR 0046 ticket 05): the deploy train signs each\n * slot's version manifest with the runtime's existing publish identity, and\n * the chain walker verifies the signature against the key record published\n * in the on-chain config (`publish.verification.publicKey`). The payload is\n * `signaturePayloadOf(manifest)` — the canonical key-sorted serialization\n * the version-id derivation also uses — so signer and verifier agree on the\n * bytes without depending on the stored document's whitespace.\n */\n\nexport type ManifestSigner = (payload: string) => Promise<ManifestSignature>;\n\n/** The payload's signature, made by the explicit publish identity (the\n * NEAR-format secret key the registry write also signs with — env-provided\n * or the keychain credentials file). */\nexport function signManifestPayload(payload: string, privateKey: string): ManifestSignature {\n const key = parseKey(privateKey);\n const signature = key.sign(new TextEncoder().encode(payload));\n return {\n algorithm: \"ed25519\",\n publicKey: key.publicKey.toString(),\n value: Buffer.from(signature.data).toString(\"base64\"),\n };\n}\n\n/** Verify a manifest's attribution against the config's key record. Throws\n * `SignatureInvalid` naming the failure mode — a missing signature, a\n * foreign key (present in the document but never registered in the config),\n * or a signature that does not verify. */\nexport function verifyManifestSignature(\n payload: string,\n signature: ManifestSignature | undefined,\n expectedPublicKey: string,\n): void {\n const url = \"manifest signature\";\n if (!signature) {\n throw new SignatureInvalid({\n url,\n reason: \"no signature — publish.verification demands attribution\",\n });\n }\n if (signature.algorithm !== \"ed25519\") {\n throw new SignatureInvalid({\n url,\n reason: `unsupported signature algorithm ${signature.algorithm}`,\n });\n }\n const expected = nearKeyString(expectedPublicKey);\n if (nearKeyString(signature.publicKey) !== expected) {\n throw new SignatureInvalid({\n url,\n reason: `signed by unregistered key ${signature.publicKey} — the config's key record names ${expected}`,\n });\n }\n const signatureBytes = Buffer.from(signature.value, \"base64\");\n if (signatureBytes.length !== 64) {\n throw new SignatureInvalid({\n url,\n reason: `signature value is ${signatureBytes.length} bytes — ed25519 signatures are 64`,\n });\n }\n const verified = verify(\n null,\n new TextEncoder().encode(payload),\n ed25519PublicKeyObject(expected),\n signatureBytes,\n );\n if (!verified) {\n throw new SignatureInvalid({\n url,\n reason: \"signature does not verify against the key record\",\n });\n }\n}\n\n/** Wrap a NEAR-format ed25519 public key (`ed25519:<base58>`) into the DER\n * SubjectPublicKeyInfo form node:crypto's `verify` consumes. */\nfunction ed25519PublicKeyObject(nearKey: string) {\n const parsed = parsePublicKey(nearKey);\n if (parsed.keyType !== 0) {\n throw new SignatureInvalid({\n url: \"manifest signature\",\n reason: `key record is not ed25519 (keyType ${parsed.keyType})`,\n });\n }\n const spki = Buffer.concat([\n Buffer.from(\"302a300506032b6570032100\", \"hex\"),\n Buffer.from(parsed.data),\n ]);\n return createPublicKey({ key: spki, format: \"der\", type: \"spki\" });\n}\n\n/** Accept the key record in either NEAR format (`ed25519:<base58>`) or bare\n * base58 — the config is hand-authored. */\nexport function nearKeyString(key: string): string {\n return key.includes(\":\") ? key : `ed25519:${key}`;\n}\n\n/** Train-time guard: the identity about to sign must be the identity the\n * config's key record names — a mismatch fails the deploy before anything\n * uploads (the record and the signer drifting apart is exactly the\n * attribution gap). */\nexport function assertSigningIdentityMatches(\n signingPublicKey: string,\n expectedPublicKey: string,\n): void {\n const expected = nearKeyString(expectedPublicKey);\n if (nearKeyString(signingPublicKey) !== expected) {\n throw new Error(\n `Manifest attribution mismatch: the publish identity signs with ${signingPublicKey} ` +\n `but publish.verification.publicKey names ${expected}. Update the config's key record ` +\n \"(rotation is a config publish) or sign with the recorded identity.\",\n );\n }\n}\n"],"mappings":";;;;;;;;AAoBA,SAAgB,oBAAoB,SAAiB,YAAuC;CAC1F,MAAM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