UNPKG

anon-identity

Version:

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

233 lines 10.4 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.ContractClient = void 0; // Lazy-loaded dependencies let ethersModule; let DIDRegistryABI; let RevocationRegistryABI; let SchemaRegistryABI; class ContractClient { constructor(rpcUrlOrConfig, privateKey, contractAddresses) { this.rpcUrlOrConfig = rpcUrlOrConfig; this.privateKey = privateKey; this.contractAddresses = contractAddresses; this.provider = null; this.contracts = { didRegistry: null, revocationRegistry: null, schemaRegistry: null, }; this.initialized = false; this.initPromise = null; } async initialize() { if (this.initialized) return; if (this.initPromise) return this.initPromise; this.initPromise = this.doInitialize(); await this.initPromise; this.initialized = true; } async doInitialize() { try { // Dynamic import of ethers ethersModule = await Promise.resolve().then(() => __importStar(require('ethers'))); const { ethers } = ethersModule; // Dynamic import of contract ABIs [DIDRegistryABI, RevocationRegistryABI, SchemaRegistryABI] = await Promise.all([ Promise.resolve().then(() => __importStar(require('../../artifacts/contracts/DIDRegistry.sol/DIDRegistry.json'))), Promise.resolve().then(() => __importStar(require('../../artifacts/contracts/RevocationRegistry.sol/RevocationRegistry.json'))), Promise.resolve().then(() => __importStar(require('../../artifacts/contracts/SchemaRegistry.sol/SchemaRegistry.json'))), ]); // Handle both constructor signatures if (typeof this.rpcUrlOrConfig === 'string') { // New signature: (rpcUrl, privateKey, contractAddresses) this.provider = new ethers.JsonRpcProvider(this.rpcUrlOrConfig); if (this.privateKey) { this.signer = new ethers.Wallet(this.privateKey, this.provider); } if (!this.contractAddresses) { throw new Error('Contract addresses required when using RPC URL constructor'); } const signerOrProvider = this.signer || this.provider; this.contracts = { didRegistry: new ethers.Contract(this.contractAddresses.didRegistry, DIDRegistryABI.abi, signerOrProvider), revocationRegistry: new ethers.Contract(this.contractAddresses.revocationRegistry, RevocationRegistryABI.abi, signerOrProvider), schemaRegistry: new ethers.Contract(this.contractAddresses.schemaRegistry, SchemaRegistryABI.abi, signerOrProvider), }; } else { // Legacy signature: (config) const config = this.rpcUrlOrConfig; this.provider = new ethers.JsonRpcProvider(config.rpcUrl); if (config.privateKey) { this.signer = new ethers.Wallet(config.privateKey, this.provider); } const signerOrProvider = this.signer || this.provider; this.contracts = { didRegistry: new ethers.Contract(config.contracts.didRegistry, DIDRegistryABI.abi, signerOrProvider), revocationRegistry: new ethers.Contract(config.contracts.revocationRegistry, RevocationRegistryABI.abi, signerOrProvider), schemaRegistry: new ethers.Contract(config.contracts.schemaRegistry, SchemaRegistryABI.abi, signerOrProvider), }; } } catch (error) { throw new Error(`Failed to initialize blockchain client: ${error instanceof Error ? error.message : 'Unknown error'}`); } } // DID Registry Methods async registerDID(did, didDocument) { await this.initialize(); if (!this.contracts.didRegistry) throw new Error('DID Registry not initialized'); if (!this.signer) throw new Error('Signer required for write operations'); return await this.contracts.didRegistry.registerDID(did, didDocument); } async resolveDID(did) { await this.initialize(); if (!this.contracts.didRegistry) throw new Error('DID Registry not initialized'); return await this.contracts.didRegistry.resolveDID(did); } async updateDID(did, didDocument) { await this.initialize(); if (!this.contracts.didRegistry) throw new Error('DID Registry not initialized'); if (!this.signer) throw new Error('Signer required for write operations'); return await this.contracts.didRegistry.updateDID(did, didDocument); } async deactivateDID(did) { await this.initialize(); if (!this.contracts.didRegistry) throw new Error('DID Registry not initialized'); if (!this.signer) throw new Error('Signer required for write operations'); return await this.contracts.didRegistry.deactivateDID(did); } async isDIDActive(did) { await this.initialize(); if (!this.contracts.didRegistry) throw new Error('DID Registry not initialized'); return await this.contracts.didRegistry.isDIDActive(did); } // Revocation Registry Methods async publishRevocationList(issuerDID, listId, revokedCredentials) { await this.initialize(); if (!this.contracts.revocationRegistry) throw new Error('Revocation Registry not initialized'); if (!this.signer) throw new Error('Signer required for write operations'); return await this.contracts.revocationRegistry.publishRevocationList(issuerDID, listId, revokedCredentials); } async isCredentialRevoked(issuerDID, credentialId) { await this.initialize(); if (!this.contracts.revocationRegistry) throw new Error('Revocation Registry not initialized'); return await this.contracts.revocationRegistry.isCredentialRevoked(issuerDID, credentialId); } async getRevocationList(issuerDID, listId) { await this.initialize(); if (!this.contracts.revocationRegistry) throw new Error('Revocation Registry not initialized'); return await this.contracts.revocationRegistry.getRevocationList(issuerDID, listId); } // Schema Registry Methods async registerSchema(schemaId, schema) { await this.initialize(); if (!this.contracts.schemaRegistry) throw new Error('Schema Registry not initialized'); if (!this.signer) throw new Error('Signer required for write operations'); return await this.contracts.schemaRegistry.registerSchema(schemaId, schema); } async getSchema(schemaId) { await this.initialize(); if (!this.contracts.schemaRegistry) throw new Error('Schema Registry not initialized'); return await this.contracts.schemaRegistry.getSchema(schemaId); } async updateSchema(schemaId, schema) { await this.initialize(); if (!this.contracts.schemaRegistry) throw new Error('Schema Registry not initialized'); if (!this.signer) throw new Error('Signer required for write operations'); return await this.contracts.schemaRegistry.updateSchema(schemaId, schema); } async deactivateSchema(schemaId) { await this.initialize(); if (!this.contracts.schemaRegistry) throw new Error('Schema Registry not initialized'); if (!this.signer) throw new Error('Signer required for write operations'); return await this.contracts.schemaRegistry.deactivateSchema(schemaId); } async isSchemaActive(schemaId) { await this.initialize(); if (!this.contracts.schemaRegistry) throw new Error('Schema Registry not initialized'); return await this.contracts.schemaRegistry.isSchemaActive(schemaId); } // Utility methods async getBlockNumber() { await this.initialize(); if (!this.provider) throw new Error('Provider not initialized'); return await this.provider.getBlockNumber(); } async waitForTransaction(txHash, confirmations = 1) { await this.initialize(); if (!this.provider) throw new Error('Provider not initialized'); return await this.provider.waitForTransaction(txHash, confirmations); } getProvider() { if (!this.provider) throw new Error('Provider not initialized. Call initialize() first.'); return this.provider; } getSigner() { return this.signer; } getContracts() { return this.contracts; } } exports.ContractClient = ContractClient; //# sourceMappingURL=contract-client-lazy.js.map