270b21715d7f5b781234c06f09bb0a0d86a8a60f2cea499523a0bd44d1491afa.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 { CryptoHelper } from '@common';\n/**\n * Validate a Cashu mint URL\n */\nexport function isValidMintUrl(url) {\n  try {\n    const parsed = new URL(url);\n    return parsed.protocol === 'https:' || parsed.protocol === 'http:';\n  } catch {\n    return false;\n  }\n}\nexport const addCashuMint = /*#__PURE__*/function () {\n  var _ref = _asyncToGenerator(function* (data) {\n    this.assureIsInitialized();\n    // Validate the mint URL\n    if (!isValidMintUrl(data.mintUrl)) {\n      throw new Error('Invalid mint URL format');\n    }\n    // Normalize URL (remove trailing slash)\n    const normalizedUrl = data.mintUrl.replace(/\\/$/, '');\n    // Check if a mint with the same URL already exists\n    const existingMint = (this.getBrowserSessionHandler().browserSessionData?.cashuMints ?? []).find(x => x.mintUrl === normalizedUrl);\n    if (existingMint) {\n      throw new Error(`A connection to this mint already exists: ${existingMint.name}`);\n    }\n    const browserSessionData = this.getBrowserSessionHandler().browserSessionData;\n    if (!browserSessionData) {\n      throw new Error('Browser session data is undefined.');\n    }\n    const decryptedMint = {\n      id: CryptoHelper.v4(),\n      name: data.name,\n      mintUrl: normalizedUrl,\n      unit: data.unit ?? 'sat',\n      createdAt: new Date().toISOString(),\n      proofs: [],\n      // Start with no proofs\n      cachedBalance: 0,\n      cachedBalanceAt: new Date().toISOString()\n    };\n    // Initialize array if needed\n    if (!browserSessionData.cashuMints) {\n      browserSessionData.cashuMints = [];\n    }\n    // Add the new mint to the session data\n    browserSessionData.cashuMints.push(decryptedMint);\n    this.getBrowserSessionHandler().saveFullData(browserSessionData);\n    // Encrypt the new mint and add it to the sync data\n    const encryptedMint = yield encryptCashuMint.call(this, decryptedMint);\n    const encryptedMints = [...(this.getBrowserSyncHandler().browserSyncData?.cashuMints ?? []), encryptedMint];\n    yield this.getBrowserSyncHandler().saveAndSetPartialData_CashuMints({\n      cashuMints: encryptedMints\n    });\n    return decryptedMint;\n  });\n  return function addCashuMint(_x) {\n    return _ref.apply(this, arguments);\n  };\n}();\nexport const deleteCashuMint = /*#__PURE__*/function () {\n  var _ref2 = _asyncToGenerator(function* (mintId) {\n    this.assureIsInitialized();\n    if (!mintId) {\n      return;\n    }\n    const browserSessionData = this.getBrowserSessionHandler().browserSessionData;\n    const browserSyncData = this.getBrowserSyncHandler().browserSyncData;\n    if (!browserSessionData || !browserSyncData) {\n      throw new Error('Browser session or sync data is undefined.');\n    }\n    // Remove from session data\n    browserSessionData.cashuMints = (browserSessionData.cashuMints ?? []).filter(x => x.id !== mintId);\n    yield this.getBrowserSessionHandler().saveFullData(browserSessionData);\n    // Handle Sync data\n    const encryptedMintId = yield this.encrypt(mintId);\n    yield this.getBrowserSyncHandler().saveAndSetPartialData_CashuMints({\n      cashuMints: (browserSyncData.cashuMints ?? []).filter(x => x.id !== encryptedMintId)\n    });\n  });\n  return function deleteCashuMint(_x2) {\n    return _ref2.apply(this, arguments);\n  };\n}();\n/**\n * Update the proofs for a Cashu mint\n * This is called after send/receive operations\n */\nexport const updateCashuMintProofs = /*#__PURE__*/function () {\n  var _ref3 = _asyncToGenerator(function* (mintId, proofs) {\n    this.assureIsInitialized();\n    const browserSessionData = this.getBrowserSessionHandler().browserSessionData;\n    const browserSyncData = this.getBrowserSyncHandler().browserSyncData;\n    if (!browserSessionData || !browserSyncData) {\n      throw new Error('Browser session or sync data is undefined.');\n    }\n    const sessionMint = (browserSessionData.cashuMints ?? []).find(x => x.id === mintId);\n    const encryptedMintId = yield this.encrypt(mintId);\n    const syncMint = (browserSyncData.cashuMints ?? []).find(x => x.id === encryptedMintId);\n    if (!sessionMint || !syncMint) {\n      throw new Error('Cashu mint not found for proofs update.');\n    }\n    const now = new Date().toISOString();\n    // Calculate balance from proofs (sum of all proof amounts in satoshis)\n    const balance = proofs.reduce((sum, p) => sum + p.amount, 0);\n    // Update session data\n    sessionMint.proofs = proofs;\n    sessionMint.cachedBalance = balance;\n    sessionMint.cachedBalanceAt = now;\n    yield this.getBrowserSessionHandler().saveFullData(browserSessionData);\n    // Update sync data\n    syncMint.proofs = yield this.encrypt(JSON.stringify(proofs));\n    syncMint.cachedBalance = yield this.encrypt(balance.toString());\n    syncMint.cachedBalanceAt = yield this.encrypt(now);\n    yield this.getBrowserSyncHandler().saveAndSetPartialData_CashuMints({\n      cashuMints: browserSyncData.cashuMints ?? []\n    });\n  });\n  return function updateCashuMintProofs(_x3, _x4) {\n    return _ref3.apply(this, arguments);\n  };\n}();\nexport const encryptCashuMint = /*#__PURE__*/function () {\n  var _ref4 = _asyncToGenerator(function* (mint) {\n    const encrypted = {\n      id: yield this.encrypt(mint.id),\n      name: yield this.encrypt(mint.name),\n      mintUrl: yield this.encrypt(mint.mintUrl),\n      unit: yield this.encrypt(mint.unit),\n      createdAt: yield this.encrypt(mint.createdAt),\n      proofs: yield this.encrypt(JSON.stringify(mint.proofs))\n    };\n    if (mint.cachedBalance !== undefined) {\n      encrypted.cachedBalance = yield this.encrypt(mint.cachedBalance.toString());\n    }\n    if (mint.cachedBalanceAt) {\n      encrypted.cachedBalanceAt = yield this.encrypt(mint.cachedBalanceAt);\n    }\n    return encrypted;\n  });\n  return function encryptCashuMint(_x5) {\n    return _ref4.apply(this, arguments);\n  };\n}();\nexport const decryptCashuMint = /*#__PURE__*/function () {\n  var _ref5 = _asyncToGenerator(function* (mint, withLockedVault = undefined) {\n    if (typeof withLockedVault === 'undefined') {\n      // Normal decryption with unlocked vault\n      const proofsJson = yield this.decrypt(mint.proofs, 'string');\n      const decrypted = {\n        id: yield this.decrypt(mint.id, 'string'),\n        name: yield this.decrypt(mint.name, 'string'),\n        mintUrl: yield this.decrypt(mint.mintUrl, 'string'),\n        unit: yield this.decrypt(mint.unit, 'string'),\n        createdAt: yield this.decrypt(mint.createdAt, 'string'),\n        proofs: JSON.parse(proofsJson)\n      };\n      if (mint.cachedBalance) {\n        decrypted.cachedBalance = yield this.decrypt(mint.cachedBalance, 'number');\n      }\n      if (mint.cachedBalanceAt) {\n        decrypted.cachedBalanceAt = yield this.decrypt(mint.cachedBalanceAt, 'string');\n      }\n      return decrypted;\n    }\n    // v2: Use pre-derived key\n    if (withLockedVault.keyBase64) {\n      const proofsJson = yield this.decryptWithLockedVaultV2(mint.proofs, 'string', withLockedVault.iv, withLockedVault.keyBase64);\n      const decrypted = {\n        id: yield this.decryptWithLockedVaultV2(mint.id, 'string', withLockedVault.iv, withLockedVault.keyBase64),\n        name: yield this.decryptWithLockedVaultV2(mint.name, 'string', withLockedVault.iv, withLockedVault.keyBase64),\n        mintUrl: yield this.decryptWithLockedVaultV2(mint.mintUrl, 'string', withLockedVault.iv, withLockedVault.keyBase64),\n        unit: yield this.decryptWithLockedVaultV2(mint.unit, 'string', withLockedVault.iv, withLockedVault.keyBase64),\n        createdAt: yield this.decryptWithLockedVaultV2(mint.createdAt, 'string', withLockedVault.iv, withLockedVault.keyBase64),\n        proofs: JSON.parse(proofsJson)\n      };\n      if (mint.cachedBalance) {\n        decrypted.cachedBalance = yield this.decryptWithLockedVaultV2(mint.cachedBalance, 'number', withLockedVault.iv, withLockedVault.keyBase64);\n      }\n      if (mint.cachedBalanceAt) {\n        decrypted.cachedBalanceAt = yield this.decryptWithLockedVaultV2(mint.cachedBalanceAt, 'string', withLockedVault.iv, withLockedVault.keyBase64);\n      }\n      return decrypted;\n    }\n    // v1: Use password (PBKDF2)\n    const proofsJson = yield this.decryptWithLockedVault(mint.proofs, 'string', withLockedVault.iv, withLockedVault.password);\n    const decrypted = {\n      id: yield this.decryptWithLockedVault(mint.id, 'string', withLockedVault.iv, withLockedVault.password),\n      name: yield this.decryptWithLockedVault(mint.name, 'string', withLockedVault.iv, withLockedVault.password),\n      mintUrl: yield this.decryptWithLockedVault(mint.mintUrl, 'string', withLockedVault.iv, withLockedVault.password),\n      unit: yield this.decryptWithLockedVault(mint.unit, 'string', withLockedVault.iv, withLockedVault.password),\n      createdAt: yield this.decryptWithLockedVault(mint.createdAt, 'string', withLockedVault.iv, withLockedVault.password),\n      proofs: JSON.parse(proofsJson)\n    };\n    if (mint.cachedBalance) {\n      decrypted.cachedBalance = yield this.decryptWithLockedVault(mint.cachedBalance, 'number', withLockedVault.iv, withLockedVault.password);\n    }\n    if (mint.cachedBalanceAt) {\n      decrypted.cachedBalanceAt = yield this.decryptWithLockedVault(mint.cachedBalanceAt, 'string', withLockedVault.iv, withLockedVault.password);\n    }\n    return decrypted;\n  });\n  return function decryptCashuMint(_x6) {\n    return _ref5.apply(this, arguments);\n  };\n}();\nexport const decryptCashuMints = /*#__PURE__*/function () {\n  var _ref6 = _asyncToGenerator(function* (mints, withLockedVault = undefined) {\n    const decryptedMints = [];\n    for (const mint of mints) {\n      const decryptedMint = yield decryptCashuMint.call(this, mint, withLockedVault);\n      decryptedMints.push(decryptedMint);\n    }\n    return decryptedMints;\n  });\n  return function decryptCashuMints(_x7) {\n    return _ref6.apply(this, arguments);\n  };\n}();","map":null,"metadata":{},"sourceType":"module","externalDependencies":[]}