7bebedb7d6250bc8b504365c598a23765d66c9555bc8365a56b1dabfcc2176f8.json raw

   1  {"ast":null,"code":"import _asyncToGenerator from \"/home/mleku/src/orly.dev/next/signer/node_modules/@babel/runtime/helpers/esm/asyncToGenerator.js\";\nimport { AggregateRoot } from '../events/domain-event';\nimport { IdentityCreated, IdentityRenamed, IdentitySigned } from '../events/identity-events';\nimport { IdentityId, Nickname, NostrKeyPair } from '../value-objects';\n/**\n * Identity entity - represents a Nostr identity with its keypair.\n *\n * This is an aggregate root that encapsulates all operations\n * related to a single Nostr identity.\n */\nexport class Identity extends AggregateRoot {\n  _id;\n  _nickname;\n  _keyPair;\n  _createdAt;\n  constructor(id, nickname, keyPair, createdAt) {\n    super();\n    this._id = id;\n    this._nickname = nickname;\n    this._keyPair = keyPair;\n    this._createdAt = createdAt;\n  }\n  // ─────────────────────────────────────────────────────────────────────────\n  // Factory Methods\n  // ─────────────────────────────────────────────────────────────────────────\n  /**\n   * Create a new identity with an optional private key.\n   * If no private key is provided, a new one will be generated.\n   *\n   * @param nickname - User-friendly name for this identity\n   * @param privateKey - Optional private key (hex or nsec format)\n   * @throws InvalidNicknameError if nickname is invalid\n   * @throws InvalidNostrKeyError if private key is invalid\n   */\n  static create(nickname, privateKey) {\n    const keyPair = privateKey ? NostrKeyPair.fromPrivateKey(privateKey) : NostrKeyPair.generate();\n    const identity = new Identity(IdentityId.generate(), Nickname.create(nickname), keyPair, new Date());\n    identity.addDomainEvent(new IdentityCreated(identity._id.value, identity.publicKey, identity.nickname));\n    return identity;\n  }\n  /**\n   * Reconstitute an identity from storage.\n   * This bypasses validation since data comes from trusted storage.\n   */\n  static fromSnapshot(snapshot) {\n    return new Identity(IdentityId.from(snapshot.id), Nickname.fromStorage(snapshot.nick), NostrKeyPair.fromStorage(snapshot.privkey), new Date(snapshot.createdAt));\n  }\n  // ─────────────────────────────────────────────────────────────────────────\n  // Getters (Read-only access to state)\n  // ─────────────────────────────────────────────────────────────────────────\n  get id() {\n    return this._id;\n  }\n  get nickname() {\n    return this._nickname.value;\n  }\n  get publicKey() {\n    return this._keyPair.publicKeyHex;\n  }\n  get npub() {\n    return this._keyPair.npub;\n  }\n  get nsec() {\n    return this._keyPair.nsec;\n  }\n  get createdAt() {\n    return this._createdAt;\n  }\n  // ─────────────────────────────────────────────────────────────────────────\n  // Behavior Methods\n  // ─────────────────────────────────────────────────────────────────────────\n  /**\n   * Rename this identity.\n   *\n   * @param newNickname - The new nickname\n   * @throws InvalidNicknameError if nickname is invalid\n   */\n  rename(newNickname) {\n    const oldNickname = this._nickname.value;\n    this._nickname = Nickname.create(newNickname);\n    this.addDomainEvent(new IdentityRenamed(this._id.value, oldNickname, newNickname));\n  }\n  /**\n   * Sign a Nostr event with this identity's private key.\n   *\n   * @param event - The unsigned event template\n   * @param signFn - The signing function (injected to avoid coupling)\n   * @returns The signed event with id, pubkey, and sig\n   */\n  sign(event, signFn) {\n    const signedEvent = signFn(event, this._keyPair.getPrivateKeyBytes());\n    this.addDomainEvent(new IdentitySigned(this._id.value, event.kind, signedEvent.id));\n    return signedEvent;\n  }\n  /**\n   * Encrypt a message using NIP-04 encryption.\n   *\n   * @param plaintext - The message to encrypt\n   * @param recipientPubkey - The recipient's public key (hex)\n   * @param encryptFn - The NIP-04 encryption function\n   */\n  encryptNip04(plaintext, recipientPubkey, encryptFn) {\n    var _this = this;\n    return _asyncToGenerator(function* () {\n      return encryptFn(_this._keyPair.getPrivateKeyBytes(), recipientPubkey, plaintext);\n    })();\n  }\n  /**\n   * Decrypt a message using NIP-04 decryption.\n   *\n   * @param ciphertext - The encrypted message\n   * @param senderPubkey - The sender's public key (hex)\n   * @param decryptFn - The NIP-04 decryption function\n   */\n  decryptNip04(ciphertext, senderPubkey, decryptFn) {\n    var _this2 = this;\n    return _asyncToGenerator(function* () {\n      return decryptFn(_this2._keyPair.getPrivateKeyBytes(), senderPubkey, ciphertext);\n    })();\n  }\n  /**\n   * Encrypt a message using NIP-44 encryption.\n   *\n   * @param plaintext - The message to encrypt\n   * @param recipientPubkey - The recipient's public key (hex)\n   * @param encryptFn - The NIP-44 encryption function\n   */\n  encryptNip44(plaintext, recipientPubkey, encryptFn) {\n    var _this3 = this;\n    return _asyncToGenerator(function* () {\n      return encryptFn(_this3._keyPair.getPrivateKeyBytes(), recipientPubkey, plaintext);\n    })();\n  }\n  /**\n   * Decrypt a message using NIP-44 decryption.\n   *\n   * @param ciphertext - The encrypted message\n   * @param senderPubkey - The sender's public key (hex)\n   * @param decryptFn - The NIP-44 decryption function\n   */\n  decryptNip44(ciphertext, senderPubkey, decryptFn) {\n    var _this4 = this;\n    return _asyncToGenerator(function* () {\n      return decryptFn(_this4._keyPair.getPrivateKeyBytes(), senderPubkey, ciphertext);\n    })();\n  }\n  /**\n   * Check if this identity has the same private key as another.\n   * Used for duplicate detection.\n   */\n  hasSameKeyAs(other) {\n    return this._keyPair.hasSamePublicKey(other._keyPair);\n  }\n  /**\n   * Check if this identity matches a given public key.\n   */\n  matchesPublicKey(publicKey) {\n    return this._keyPair.matchesPublicKey(publicKey);\n  }\n  // ─────────────────────────────────────────────────────────────────────────\n  // Persistence\n  // ─────────────────────────────────────────────────────────────────────────\n  /**\n   * Convert to a snapshot for persistence.\n   */\n  toSnapshot() {\n    return {\n      id: this._id.value,\n      nick: this._nickname.value,\n      privkey: this._keyPair.toStorageHex(),\n      createdAt: this._createdAt.toISOString()\n    };\n  }\n  // ─────────────────────────────────────────────────────────────────────────\n  // Equality\n  // ─────────────────────────────────────────────────────────────────────────\n  /**\n   * Check equality based on identity ID.\n   */\n  equals(other) {\n    return this._id.equals(other._id);\n  }\n}","map":null,"metadata":{},"sourceType":"module","externalDependencies":[]}