anon-identity
Version:
Decentralized identity framework with DIDs, Verifiable Credentials, and privacy-preserving selective disclosure
136 lines • 4.41 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SignatureSuiteRegistry = exports.SignatureSuite = exports.KeyType = void 0;
const jsonld_processor_1 = require("../jsonld-processor");
/**
* Key type supported by a signature suite
*/
var KeyType;
(function (KeyType) {
KeyType["Ed25519"] = "Ed25519";
KeyType["BLS12381G2"] = "Bls12381G2";
})(KeyType || (exports.KeyType = KeyType = {}));
/**
* Abstract base class for signature suites
*/
class SignatureSuite {
constructor(options = {}) {
// Canonicalization algorithm
this.canonicalizationAlgorithm = 'URDNA2015';
this.jsonLdProcessor = options.jsonLdProcessor || new jsonld_processor_1.JsonLdProcessor();
}
/**
* Create a derived proof with selective disclosure (if supported)
*/
async createDerivedProof(options) {
if (!this.supportsSelectiveDisclosure) {
throw new Error(`${this.type} does not support selective disclosure`);
}
throw new Error('Method not implemented');
}
/**
* Verify a derived proof (if supported)
*/
async verifyDerivedProof(options) {
if (!this.supportsSelectiveDisclosure) {
throw new Error(`${this.type} does not support selective disclosure`);
}
throw new Error('Method not implemented');
}
/**
* Canonicalize document for signing/verification
*/
async canonicalizeDocument(document) {
// Remove proof before canonicalization
const docCopy = { ...document };
delete docCopy.proof;
return await this.jsonLdProcessor.canonicalize(docCopy);
}
/**
* Create proof value metadata
*/
createProofMetadata(options) {
const proof = {
type: this.type,
created: options.created || new Date().toISOString(),
verificationMethod: options.verificationMethod,
proofPurpose: options.purpose
};
if (options.challenge)
proof.challenge = options.challenge;
if (options.domain)
proof.domain = options.domain;
if (options.expires)
proof.expires = options.expires;
return proof;
}
/**
* Validate proof metadata
*/
validateProofMetadata(proof, options) {
// Check proof type
if (proof.type !== this.type) {
return { valid: false, error: `Invalid proof type: ${proof.type}` };
}
// Check proof purpose
if (proof.proofPurpose !== options.expectedPurpose) {
return {
valid: false,
error: `Invalid proof purpose: ${proof.proofPurpose}, expected: ${options.expectedPurpose}`
};
}
// Check expiration
if (proof.expires) {
const expirationDate = new Date(proof.expires);
if (expirationDate < new Date()) {
return { valid: false, error: 'Proof has expired' };
}
}
// Check challenge if expected
if (options.expectedChallenge && proof.challenge !== options.expectedChallenge) {
return { valid: false, error: 'Challenge mismatch' };
}
// Check domain if expected
if (options.expectedDomain && proof.domain !== options.expectedDomain) {
return { valid: false, error: 'Domain mismatch' };
}
return { valid: true };
}
}
exports.SignatureSuite = SignatureSuite;
/**
* Registry for signature suites
*/
class SignatureSuiteRegistry {
/**
* Register a signature suite
*/
static register(type, suiteClass) {
this.suites.set(type, suiteClass);
}
/**
* Get a signature suite by type
*/
static getSuite(type, options) {
const SuiteClass = this.suites.get(type);
if (!SuiteClass) {
throw new Error(`Unknown signature suite: ${type}`);
}
return new SuiteClass(options);
}
/**
* Check if a suite is registered
*/
static hasSuite(type) {
return this.suites.has(type);
}
/**
* Get all registered suite types
*/
static getRegisteredTypes() {
return Array.from(this.suites.keys());
}
}
exports.SignatureSuiteRegistry = SignatureSuiteRegistry;
SignatureSuiteRegistry.suites = new Map();
//# sourceMappingURL=signature-suite.js.map