UNPKG

anon-identity

Version:

Decentralized identity framework with DIDs, Verifiable Credentials, and privacy-preserving selective disclosure

123 lines 5.09 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.Ed25519Signature2020Suite = void 0; const ed25519 = __importStar(require("@noble/ed25519")); const sha512_1 = require("@noble/hashes/sha512"); const signature_suite_1 = require("./signature-suite"); // Configure ed25519 to use sha512 ed25519.etc.sha512Sync = (...m) => (0, sha512_1.sha512)(ed25519.etc.concatBytes(...m)); /** * Ed25519Signature2020 implementation * https://w3c-ccg.github.io/lds-ed25519-2020/ */ class Ed25519Signature2020Suite extends signature_suite_1.SignatureSuite { constructor(options = {}) { super(options); this.type = 'Ed25519Signature2020'; this.requiredKeyType = signature_suite_1.KeyType.Ed25519; this.supportsSelectiveDisclosure = false; } /** * Create an Ed25519 signature proof */ async createProof(options) { try { // 1. Create proof metadata const proofMetadata = this.createProofMetadata(options); // 2. Canonicalize the document const canonicalDocument = await this.canonicalizeDocument(options.document); // 3. Create the verification hash const verifyData = await this.createVerifyData(canonicalDocument, proofMetadata); // 4. Sign the data const signature = await ed25519.sign(verifyData, options.privateKey); // 5. Create the final proof const proof = { ...proofMetadata, proofValue: Buffer.from(signature).toString('base64') }; return proof; } catch (error) { throw new Error(`Failed to create Ed25519Signature2020: ${error}`); } } /** * Verify an Ed25519 signature proof */ async verifyProof(options) { try { // 1. Validate proof metadata const metadataValidation = this.validateProofMetadata(options.proof, options); if (!metadataValidation.valid) { console.error('Proof metadata validation failed:', metadataValidation.error); return false; } // 2. Extract signature if (!options.proof.proofValue) { console.error('Missing proofValue'); return false; } const signature = Buffer.from(options.proof.proofValue, 'base64'); // 3. Canonicalize the document const canonicalDocument = await this.canonicalizeDocument(options.document); // 4. Create the verification hash const verifyData = await this.createVerifyData(canonicalDocument, options.proof); // 5. Verify the signature const isValid = await ed25519.verify(signature, verifyData, options.publicKey); return isValid; } catch (error) { console.error('Ed25519Signature2020 verification error:', error); return false; } } /** * Create the data to be signed/verified */ async createVerifyData(canonicalDocument, proof) { // Create proof options without proofValue const proofOptions = { ...proof }; delete proofOptions.proofValue; // Canonicalize proof options const canonicalProofOptions = await this.jsonLdProcessor.canonicalize(proofOptions); // Combine document and proof options const combined = canonicalDocument + canonicalProofOptions; // Hash the combined data return (0, sha512_1.sha512)(new TextEncoder().encode(combined)); } } exports.Ed25519Signature2020Suite = Ed25519Signature2020Suite; //# sourceMappingURL=ed25519-signature-2020.js.map