anon-identity
Version:
Decentralized identity framework with DIDs, Verifiable Credentials, and privacy-preserving selective disclosure
311 lines • 11.4 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MemoryStorageProvider = void 0;
const uuid_1 = require("uuid");
class MemoryStorageProvider {
constructor() {
this.dids = new Map();
this.credentials = new Map();
this.credentialsByHolder = new Map();
this.keyPairs = new Map();
this.revocationLists = new Map();
this.schemas = new Map();
this.schemasByIssuer = new Map();
this.phoneNumbers = new Map();
this.phoneNumbersByUser = new Map();
this.emailAddresses = new Map();
this.emailAddressesByUser = new Map();
this.addresses = new Map();
this.addressesByUser = new Map();
}
// DID Operations
async storeDID(did, document) {
this.dids.set(did, document);
}
async resolveDID(did) {
return this.dids.get(did) || null;
}
async listDIDs(owner) {
if (!owner) {
return Array.from(this.dids.keys());
}
// Filter by owner - check if any verification method has this owner as controller
return Array.from(this.dids.keys()).filter(did => {
const doc = this.dids.get(did);
if (!doc || !doc.verificationMethod)
return false;
return doc.verificationMethod.some(vm => vm.controller === owner);
});
}
// Credential Operations
async storeCredential(credential) {
this.credentials.set(credential.id, credential);
// Update holder index
const holder = credential.credentialSubject.id;
if (!this.credentialsByHolder.has(holder)) {
this.credentialsByHolder.set(holder, new Set());
}
this.credentialsByHolder.get(holder).add(credential.id);
}
async getCredential(id) {
return this.credentials.get(id) || null;
}
async listCredentials(holder) {
const credentialIds = this.credentialsByHolder.get(holder);
if (!credentialIds)
return [];
const credentials = [];
for (const id of credentialIds) {
const credential = this.credentials.get(id);
if (credential) {
credentials.push(credential);
}
}
return credentials;
}
async deleteCredential(id) {
const credential = this.credentials.get(id);
if (credential) {
const holder = credential.credentialSubject.id;
const holderCreds = this.credentialsByHolder.get(holder);
if (holderCreds) {
holderCreds.delete(id);
if (holderCreds.size === 0) {
this.credentialsByHolder.delete(holder);
}
}
this.credentials.delete(id);
}
}
// Revocation Operations
async publishRevocation(issuerDID, revocationList) {
this.revocationLists.set(issuerDID, revocationList);
}
async checkRevocation(issuerDID, credentialId) {
const revocationList = this.revocationLists.get(issuerDID);
if (!revocationList)
return false;
return revocationList.revokedCredentialIds.includes(credentialId);
}
async getRevocationList(issuerDID) {
return this.revocationLists.get(issuerDID) || null;
}
// Key Management
async storeKeyPair(identifier, encryptedKeyPair) {
this.keyPairs.set(identifier, encryptedKeyPair);
}
async retrieveKeyPair(identifier) {
return this.keyPairs.get(identifier) || null;
}
async deleteKeyPair(identifier) {
this.keyPairs.delete(identifier);
}
// Schema Operations
async registerSchema(schema) {
const schemaId = schema.id || `schema:${(0, uuid_1.v4)()}`;
const schemaWithId = { ...schema, id: schemaId };
this.schemas.set(schemaId, schemaWithId);
// Update issuer index
if (!this.schemasByIssuer.has(schema.issuerDID)) {
this.schemasByIssuer.set(schema.issuerDID, new Set());
}
this.schemasByIssuer.get(schema.issuerDID).add(schemaId);
return schemaId;
}
async getSchema(schemaId) {
return this.schemas.get(schemaId) || null;
}
async listSchemas(issuerDID) {
if (!issuerDID) {
return Array.from(this.schemas.values());
}
const schemaIds = this.schemasByIssuer.get(issuerDID);
if (!schemaIds)
return [];
const schemas = [];
for (const id of schemaIds) {
const schema = this.schemas.get(id);
if (schema) {
schemas.push(schema);
}
}
return schemas;
}
// Phone Number Operations
async storePhoneNumber(userDID, phoneNumber) {
const phoneId = phoneNumber.id || `phone:${(0, uuid_1.v4)()}`;
const phoneWithId = { ...phoneNumber, id: phoneId };
this.phoneNumbers.set(phoneId, phoneWithId);
// Update user index
if (!this.phoneNumbersByUser.has(userDID)) {
this.phoneNumbersByUser.set(userDID, new Set());
}
this.phoneNumbersByUser.get(userDID).add(phoneId);
return phoneId;
}
async getPhoneNumber(userDID, phoneId) {
const userPhones = this.phoneNumbersByUser.get(userDID);
if (!userPhones || !userPhones.has(phoneId)) {
return null;
}
return this.phoneNumbers.get(phoneId) || null;
}
async listPhoneNumbers(userDID) {
const phoneIds = this.phoneNumbersByUser.get(userDID);
if (!phoneIds)
return [];
const phoneNumbers = [];
for (const id of phoneIds) {
const phone = this.phoneNumbers.get(id);
if (phone) {
phoneNumbers.push(phone);
}
}
return phoneNumbers;
}
async updatePhoneNumber(userDID, phoneId, phoneNumber) {
const userPhones = this.phoneNumbersByUser.get(userDID);
if (!userPhones || !userPhones.has(phoneId)) {
throw new Error('Phone number not found');
}
const existingPhone = this.phoneNumbers.get(phoneId);
if (existingPhone) {
const updatedPhone = { ...existingPhone, ...phoneNumber, id: phoneId };
this.phoneNumbers.set(phoneId, updatedPhone);
}
}
async deletePhoneNumber(userDID, phoneId) {
const userPhones = this.phoneNumbersByUser.get(userDID);
if (userPhones) {
userPhones.delete(phoneId);
if (userPhones.size === 0) {
this.phoneNumbersByUser.delete(userDID);
}
}
this.phoneNumbers.delete(phoneId);
}
// Email Address Operations
async storeEmailAddress(userDID, emailAddress) {
const emailId = emailAddress.id || `email:${(0, uuid_1.v4)()}`;
const emailWithId = { ...emailAddress, id: emailId };
this.emailAddresses.set(emailId, emailWithId);
// Update user index
if (!this.emailAddressesByUser.has(userDID)) {
this.emailAddressesByUser.set(userDID, new Set());
}
this.emailAddressesByUser.get(userDID).add(emailId);
return emailId;
}
async getEmailAddress(userDID, emailId) {
const userEmails = this.emailAddressesByUser.get(userDID);
if (!userEmails || !userEmails.has(emailId)) {
return null;
}
return this.emailAddresses.get(emailId) || null;
}
async listEmailAddresses(userDID) {
const emailIds = this.emailAddressesByUser.get(userDID);
if (!emailIds)
return [];
const emailAddresses = [];
for (const id of emailIds) {
const email = this.emailAddresses.get(id);
if (email) {
emailAddresses.push(email);
}
}
return emailAddresses;
}
async updateEmailAddress(userDID, emailId, emailAddress) {
const userEmails = this.emailAddressesByUser.get(userDID);
if (!userEmails || !userEmails.has(emailId)) {
throw new Error('Email address not found');
}
const existingEmail = this.emailAddresses.get(emailId);
if (existingEmail) {
const updatedEmail = { ...existingEmail, ...emailAddress, id: emailId };
this.emailAddresses.set(emailId, updatedEmail);
}
}
async deleteEmailAddress(userDID, emailId) {
const userEmails = this.emailAddressesByUser.get(userDID);
if (userEmails) {
userEmails.delete(emailId);
if (userEmails.size === 0) {
this.emailAddressesByUser.delete(userDID);
}
}
this.emailAddresses.delete(emailId);
}
// Address Operations
async storeAddress(userDID, address) {
const addressId = address.id || `address:${(0, uuid_1.v4)()}`;
const addressWithId = { ...address, id: addressId };
this.addresses.set(addressId, addressWithId);
// Update user index
if (!this.addressesByUser.has(userDID)) {
this.addressesByUser.set(userDID, new Set());
}
this.addressesByUser.get(userDID).add(addressId);
return addressId;
}
async getAddress(userDID, addressId) {
const userAddresses = this.addressesByUser.get(userDID);
if (!userAddresses || !userAddresses.has(addressId)) {
return null;
}
return this.addresses.get(addressId) || null;
}
async listAddresses(userDID) {
const addressIds = this.addressesByUser.get(userDID);
if (!addressIds)
return [];
const addresses = [];
for (const id of addressIds) {
const address = this.addresses.get(id);
if (address) {
addresses.push(address);
}
}
return addresses;
}
async updateAddress(userDID, addressId, address) {
const userAddresses = this.addressesByUser.get(userDID);
if (!userAddresses || !userAddresses.has(addressId)) {
throw new Error('Address not found');
}
const existingAddress = this.addresses.get(addressId);
if (existingAddress) {
const updatedAddress = { ...existingAddress, ...address, id: addressId };
this.addresses.set(addressId, updatedAddress);
}
}
async deleteAddress(userDID, addressId) {
const userAddresses = this.addressesByUser.get(userDID);
if (userAddresses) {
userAddresses.delete(addressId);
if (userAddresses.size === 0) {
this.addressesByUser.delete(userDID);
}
}
this.addresses.delete(addressId);
}
// General operations
async clear() {
this.dids.clear();
this.credentials.clear();
this.credentialsByHolder.clear();
this.keyPairs.clear();
this.revocationLists.clear();
this.schemas.clear();
this.schemasByIssuer.clear();
this.phoneNumbers.clear();
this.phoneNumbersByUser.clear();
this.emailAddresses.clear();
this.emailAddressesByUser.clear();
this.addresses.clear();
this.addressesByUser.clear();
}
}
exports.MemoryStorageProvider = MemoryStorageProvider;
//# sourceMappingURL=memory-storage-provider.js.map