|
| 1 | +import crypto from "node:crypto"; |
| 2 | + |
| 3 | +const ED25519_SPKI_PREFIX = Buffer.from("302a300506032b6570032100", "hex"); |
| 4 | + |
| 5 | +export function stableStringify(value) { |
| 6 | + const seen = new WeakSet(); |
| 7 | + const helper = (current) => { |
| 8 | + if (current === null || typeof current !== "object") return current; |
| 9 | + if (seen.has(current)) return "[Circular]"; |
| 10 | + seen.add(current); |
| 11 | + if (Array.isArray(current)) return current.map(helper); |
| 12 | + const output = {}; |
| 13 | + for (const key of Object.keys(current).sort()) output[key] = helper(current[key]); |
| 14 | + return output; |
| 15 | + }; |
| 16 | + return JSON.stringify(helper(value)); |
| 17 | +} |
| 18 | + |
| 19 | +export function parseEd25519PublicKey(text) { |
| 20 | + if (typeof text !== "string") throw new Error("Invalid ed25519 format"); |
| 21 | + const [alg, payload] = text.trim().split(":", 2); |
| 22 | + if (alg?.toLowerCase() !== "ed25519" || !payload) throw new Error("Invalid ed25519 format"); |
| 23 | + |
| 24 | + const bytes = Buffer.from(payload, "base64"); |
| 25 | + if (!bytes.length || bytes.toString("base64") !== payload || bytes.length !== 32) { |
| 26 | + throw new Error("Invalid ed25519 format"); |
| 27 | + } |
| 28 | + return bytes; |
| 29 | +} |
| 30 | + |
| 31 | +export function computeReceiptHash(receipt) { |
| 32 | + const canonicalReceipt = { |
| 33 | + issuer: receipt.issuer, |
| 34 | + verb: receipt.verb, |
| 35 | + version: receipt.version, |
| 36 | + timestamp: receipt.timestamp, |
| 37 | + payload_hash: receipt.payload_hash, |
| 38 | + alg: receipt.alg, |
| 39 | + kid: receipt.kid, |
| 40 | + }; |
| 41 | + return crypto.createHash("sha256").update(stableStringify(canonicalReceipt)).digest("hex"); |
| 42 | +} |
| 43 | + |
| 44 | +export async function resolveSigner(agentEnsName, resolver) { |
| 45 | + const signer = await resolver.getText(agentEnsName, "cl.receipt.signer"); |
| 46 | + const normalized = String(signer || "").trim(); |
| 47 | + if (!normalized) throw new Error("Missing cl.receipt.signer"); |
| 48 | + return normalized; |
| 49 | +} |
| 50 | + |
| 51 | +export async function resolveSignatureKey(signerEnsName, resolver) { |
| 52 | + const pub = await resolver.getText(signerEnsName, "cl.sig.pub"); |
| 53 | + const kid = String(await resolver.getText(signerEnsName, "cl.sig.kid") || "").trim(); |
| 54 | + if (!pub) throw new Error("Missing cl.sig.pub"); |
| 55 | + if (!kid) throw new Error("Missing cl.sig.kid"); |
| 56 | + |
| 57 | + const pubkeyBytes = parseEd25519PublicKey(pub); |
| 58 | + return { algorithm: "ed25519", kid, pubkeyBytes, rawPublicKeyBytes: pubkeyBytes }; |
| 59 | +} |
| 60 | + |
| 61 | +function verifySignature(receiptHash, signatureB64, pubkeyBytes) { |
| 62 | + const spki = Buffer.concat([ED25519_SPKI_PREFIX, pubkeyBytes]); |
| 63 | + const key = crypto.createPublicKey({ key: spki, format: "der", type: "spki" }); |
| 64 | + return crypto.verify(null, Buffer.from(receiptHash, "utf8"), key, Buffer.from(signatureB64, "base64")); |
| 65 | +} |
| 66 | + |
| 67 | +export async function verifyReceipt(receipt, { resolver, expectedIssuer } = {}) { |
| 68 | + if (!resolver) throw new Error("Resolver required"); |
| 69 | + if (expectedIssuer && receipt.issuer !== expectedIssuer) throw new Error("Issuer mismatch"); |
| 70 | + |
| 71 | + const signerEnsName = await resolveSigner(receipt.issuer, resolver); |
| 72 | + const key = await resolveSignatureKey(signerEnsName, resolver); |
| 73 | + if (receipt.kid !== key.kid) { |
| 74 | + return { valid: false, error: "Unknown key id" }; |
| 75 | + } |
| 76 | + |
| 77 | + const computedHash = computeReceiptHash(receipt); |
| 78 | + if (computedHash !== receipt.receipt_hash) { |
| 79 | + return { valid: false, error: "Receipt hash mismatch" }; |
| 80 | + } |
| 81 | + |
| 82 | + const signatureOk = verifySignature(receipt.receipt_hash, receipt.sig, key.pubkeyBytes); |
| 83 | + if (!signatureOk) { |
| 84 | + return { valid: false, error: "Signature verification failed" }; |
| 85 | + } |
| 86 | + |
| 87 | + return { valid: true, signer: signerEnsName, kid: key.kid }; |
| 88 | +} |
| 89 | + |
| 90 | +export async function resolveSignerKey(agentEnsName, resolver) { |
| 91 | + const signer = await resolveSigner(agentEnsName, resolver); |
| 92 | + return resolveSignatureKey(signer, resolver); |
| 93 | +} |
0 commit comments