identity-repository.ts raw

   1  import { IdentityId } from '../value-objects';
   2  
   3  /**
   4   * Snapshot of an identity for persistence.
   5   * This is the data structure that gets persisted, separate from the domain entity.
   6   */
   7  export interface IdentitySnapshot {
   8    id: string;
   9    nick: string;
  10    privkey: string;
  11    createdAt: string;
  12  }
  13  
  14  /**
  15   * Repository interface for Identity aggregate.
  16   * Implementations handle encryption and storage specifics.
  17   */
  18  export interface IdentityRepository {
  19    /**
  20     * Find an identity by its ID.
  21     * Returns undefined if not found.
  22     */
  23    findById(id: IdentityId): Promise<IdentitySnapshot | undefined>;
  24  
  25    /**
  26     * Find an identity by its public key.
  27     * Returns undefined if not found.
  28     */
  29    findByPublicKey(publicKey: string): Promise<IdentitySnapshot | undefined>;
  30  
  31    /**
  32     * Find an identity by its private key.
  33     * Used for duplicate detection.
  34     * Returns undefined if not found.
  35     */
  36    findByPrivateKey(privateKey: string): Promise<IdentitySnapshot | undefined>;
  37  
  38    /**
  39     * Get all identities.
  40     */
  41    findAll(): Promise<IdentitySnapshot[]>;
  42  
  43    /**
  44     * Save a new or updated identity.
  45     * If an identity with the same ID exists, it will be updated.
  46     */
  47    save(identity: IdentitySnapshot): Promise<void>;
  48  
  49    /**
  50     * Delete an identity by its ID.
  51     * Returns true if the identity was deleted, false if it didn't exist.
  52     */
  53    delete(id: IdentityId): Promise<boolean>;
  54  
  55    /**
  56     * Get the currently selected identity ID.
  57     */
  58    getSelectedId(): Promise<IdentityId | null>;
  59  
  60    /**
  61     * Set the currently selected identity ID.
  62     */
  63    setSelectedId(id: IdentityId | null): Promise<void>;
  64  
  65    /**
  66     * Count the total number of identities.
  67     */
  68    count(): Promise<number>;
  69  }
  70  
  71  /**
  72   * Error thrown when an identity operation fails.
  73   */
  74  export class IdentityRepositoryError extends Error {
  75    constructor(
  76      message: string,
  77      public readonly code: IdentityErrorCode
  78    ) {
  79      super(message);
  80      this.name = 'IdentityRepositoryError';
  81    }
  82  }
  83  
  84  export enum IdentityErrorCode {
  85    DUPLICATE_PRIVATE_KEY = 'DUPLICATE_PRIVATE_KEY',
  86    NOT_FOUND = 'NOT_FOUND',
  87    ENCRYPTION_FAILED = 'ENCRYPTION_FAILED',
  88    STORAGE_FAILED = 'STORAGE_FAILED',
  89  }
  90