4544a06239477fcc05d1739331bd9086f4bdfc717063fcf8d0af06a80605cf3e.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 { IdentityRepositoryError, IdentityErrorCode } from '../../domain/repositories/identity-repository';\nimport { IdentityId } from '../../domain/value-objects';\nimport { NostrHelper } from '../../helpers/nostr-helper';\n/**\n * Implementation of IdentityRepository using browser storage.\n * Handles encryption/decryption transparently.\n */\nexport class BrowserIdentityRepository {\n storage;\n encryption;\n constructor(storage, encryption) {\n this.storage = storage;\n this.encryption = encryption;\n }\n findById(id) {\n var _this = this;\n return _asyncToGenerator(function* () {\n const identities = _this.storage.getSessionIdentities();\n return identities.find(i => i.id === id.value);\n })();\n }\n findByPublicKey(publicKey) {\n var _this2 = this;\n return _asyncToGenerator(function* () {\n const identities = _this2.storage.getSessionIdentities();\n return identities.find(i => {\n try {\n const derivedPubkey = NostrHelper.pubkeyFromPrivkey(i.privkey);\n return derivedPubkey === publicKey;\n } catch {\n return false;\n }\n });\n })();\n }\n findByPrivateKey(privateKey) {\n var _this3 = this;\n return _asyncToGenerator(function* () {\n // Normalize the private key to hex format\n let privkeyHex;\n try {\n privkeyHex = NostrHelper.getNostrPrivkeyObject(privateKey.toLowerCase()).hex;\n } catch {\n return undefined;\n }\n const identities = _this3.storage.getSessionIdentities();\n return identities.find(i => i.privkey === privkeyHex);\n })();\n }\n findAll() {\n var _this4 = this;\n return _asyncToGenerator(function* () {\n return _this4.storage.getSessionIdentities();\n })();\n }\n save(identity) {\n var _this5 = this;\n return _asyncToGenerator(function* () {\n // Check for duplicate private key (excluding self)\n const existing = yield _this5.findByPrivateKey(identity.privkey);\n if (existing && existing.id !== identity.id) {\n throw new IdentityRepositoryError(`An identity with the same private key already exists: ${existing.nick}`, IdentityErrorCode.DUPLICATE_PRIVATE_KEY);\n }\n // Update session storage\n const sessionIdentities = _this5.storage.getSessionIdentities();\n const existingIndex = sessionIdentities.findIndex(i => i.id === identity.id);\n if (existingIndex >= 0) {\n // Update existing\n sessionIdentities[existingIndex] = identity;\n } else {\n // Add new\n sessionIdentities.push(identity);\n // Auto-select if first identity\n if (sessionIdentities.length === 1) {\n _this5.storage.setSessionSelectedId(identity.id);\n }\n }\n _this5.storage.setSessionIdentities(sessionIdentities);\n yield _this5.storage.saveSessionData();\n // Encrypt and save to sync storage\n const encryptedIdentity = yield _this5.encryptIdentity(identity);\n const syncIdentities = _this5.storage.getSyncIdentities();\n const syncIndex = syncIdentities.findIndex(/*#__PURE__*/function () {\n var _ref = _asyncToGenerator(function* (i) {\n return (yield _this5.encryption.decryptString(i.id)) === identity.id;\n });\n return function (_x) {\n return _ref.apply(this, arguments);\n };\n }());\n if (syncIndex >= 0) {\n syncIdentities[syncIndex] = encryptedIdentity;\n } else {\n syncIdentities.push(encryptedIdentity);\n }\n yield _this5.storage.saveSyncIdentities(syncIdentities);\n // Update selected ID in sync if this was the first identity\n if (sessionIdentities.length === 1) {\n const encryptedId = yield _this5.encryption.encryptString(identity.id);\n yield _this5.storage.saveSyncSelectedId(encryptedId);\n }\n })();\n }\n delete(id) {\n var _this6 = this;\n return _asyncToGenerator(function* () {\n const sessionIdentities = _this6.storage.getSessionIdentities();\n const initialLength = sessionIdentities.length;\n const filtered = sessionIdentities.filter(i => i.id !== id.value);\n if (filtered.length === initialLength) {\n return false; // Nothing was deleted\n }\n // Update selected identity if needed\n const currentSelectedId = _this6.storage.getSessionSelectedId();\n if (currentSelectedId === id.value) {\n const newSelectedId = filtered.length > 0 ? filtered[0].id : null;\n _this6.storage.setSessionSelectedId(newSelectedId);\n }\n _this6.storage.setSessionIdentities(filtered);\n yield _this6.storage.saveSessionData();\n // Remove from sync storage\n const encryptedId = yield _this6.encryption.encryptString(id.value);\n const syncIdentities = _this6.storage.getSyncIdentities();\n const filteredSync = syncIdentities.filter(i => i.id !== encryptedId);\n yield _this6.storage.saveSyncIdentities(filteredSync);\n // Update selected ID in sync\n const newSelectedId = _this6.storage.getSessionSelectedId();\n const encryptedSelectedId = newSelectedId ? yield _this6.encryption.encryptString(newSelectedId) : null;\n yield _this6.storage.saveSyncSelectedId(encryptedSelectedId);\n return true;\n })();\n }\n getSelectedId() {\n var _this7 = this;\n return _asyncToGenerator(function* () {\n const selectedId = _this7.storage.getSessionSelectedId();\n return selectedId ? IdentityId.from(selectedId) : null;\n })();\n }\n setSelectedId(id) {\n var _this8 = this;\n return _asyncToGenerator(function* () {\n if (id) {\n // Verify the identity exists\n const exists = yield _this8.findById(id);\n if (!exists) {\n throw new IdentityRepositoryError(`Identity not found: ${id.value}`, IdentityErrorCode.NOT_FOUND);\n }\n }\n _this8.storage.setSessionSelectedId(id?.value ?? null);\n yield _this8.storage.saveSessionData();\n // Update sync storage\n const encryptedId = id ? yield _this8.encryption.encryptString(id.value) : null;\n yield _this8.storage.saveSyncSelectedId(encryptedId);\n })();\n }\n count() {\n var _this9 = this;\n return _asyncToGenerator(function* () {\n return _this9.storage.getSessionIdentities().length;\n })();\n }\n // ─────────────────────────────────────────────────────────────────────────\n // Private helpers\n // ─────────────────────────────────────────────────────────────────────────\n encryptIdentity(identity) {\n var _this0 = this;\n return _asyncToGenerator(function* () {\n return {\n id: yield _this0.encryption.encryptString(identity.id),\n nick: yield _this0.encryption.encryptString(identity.nick),\n privkey: yield _this0.encryption.encryptString(identity.privkey),\n createdAt: yield _this0.encryption.encryptString(identity.createdAt)\n };\n })();\n }\n}\n/**\n * Factory function to create a BrowserIdentityRepository.\n */\nexport function createIdentityRepository(storage, encryption) {\n return new BrowserIdentityRepository(storage, encryption);\n}","map":null,"metadata":{},"sourceType":"module","externalDependencies":[]}