@gvnrdao/dh-lit-actions 0.0.286 → 0.0.288

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,6 +4,6 @@
4
4
  "minifiedSize": 1418,
5
5
  "compressionRatio": 0.4642992066490367,
6
6
  "hash": "b66a469b07b3ba5d753ff8726775188d32450b3e6a77b91404c8d5e8114be9e8",
7
- "buildTime": 1777647243098,
7
+ "buildTime": 1778141706904,
8
8
  "version": "0.1.0"
9
9
  }
@@ -4,6 +4,6 @@
4
4
  "minifiedSize": 856,
5
5
  "compressionRatio": 0.4751686082158185,
6
6
  "hash": "bc0bcb38452275c93fa74c5240ae496d7a3eef9d38e7fb0dd99c5202c554d5e3",
7
- "buildTime": 1777647243106,
7
+ "buildTime": 1778141706912,
8
8
  "version": "0.1.0"
9
9
  }
@@ -4,6 +4,6 @@
4
4
  "minifiedSize": 51555,
5
5
  "compressionRatio": 0.5640427205154874,
6
6
  "hash": "404fc4620f6b2ff31f233cdbe18290992498e63e45f38da76e3b6e7826e435b4",
7
- "buildTime": 1777647243352,
7
+ "buildTime": 1778141707074,
8
8
  "version": "0.1.0"
9
9
  }
@@ -4,6 +4,6 @@
4
4
  "minifiedSize": 4143,
5
5
  "compressionRatio": 0.5279170464904284,
6
6
  "hash": "25007f80b4ed3169470bf617d403971136be431eb29dfb09f78afe2494bf0138",
7
- "buildTime": 1777647243386,
7
+ "buildTime": 1778141707098,
8
8
  "version": "0.1.0"
9
9
  }
@@ -4,6 +4,6 @@
4
4
  "minifiedSize": 1695,
5
5
  "compressionRatio": 0.46292775665399244,
6
6
  "hash": "def4576ba6c382d145f996c4f950b5e39e58792415ee7fdd749e0d2bc61e8f0b",
7
- "buildTime": 1777647243394,
7
+ "buildTime": 1778141707107,
8
8
  "version": "0.1.0"
9
9
  }
@@ -4,6 +4,6 @@
4
4
  "minifiedSize": 2026,
5
5
  "compressionRatio": 0.48197391971362824,
6
6
  "hash": "349d51aa096d56b394f82de75ed34cd4eef72719969f7e5fe584f4582b2745fa",
7
- "buildTime": 1777647243402,
7
+ "buildTime": 1778141707119,
8
8
  "version": "0.1.0"
9
9
  }
@@ -4,6 +4,6 @@
4
4
  "minifiedSize": 25656,
5
5
  "compressionRatio": 0.5613309168006019,
6
6
  "hash": "c1dc107c830449d55e05a2777c072d7e0c62e2bfec4406d5d1bc8dab61eadf91",
7
- "buildTime": 1777647243527,
7
+ "buildTime": 1778141707198,
8
8
  "version": "0.1.0"
9
9
  }
@@ -4,6 +4,6 @@
4
4
  "minifiedSize": 72300,
5
5
  "compressionRatio": 0.5653062696904836,
6
6
  "hash": "888eef2c01d71a20fc6734fab41ec9f4eda4941774fe23ac44d1fcb6c21a8807",
7
- "buildTime": 1777647243778,
7
+ "buildTime": 1778141707357,
8
8
  "version": "0.1.0"
9
9
  }
@@ -4,6 +4,6 @@
4
4
  "minifiedSize": 832,
5
5
  "compressionRatio": 0.5026897788404064,
6
6
  "hash": "700f8992a20357ac718df78f82a75641e892d24bcb925357087cf062cf521ae0",
7
- "buildTime": 1777647243798,
7
+ "buildTime": 1778141707364,
8
8
  "version": "0.1.0"
9
9
  }
@@ -4,6 +4,6 @@
4
4
  "minifiedSize": 60611,
5
5
  "compressionRatio": 0.5876185551579147,
6
6
  "hash": "d2ee66d7fd30762d637d54f1dedb40d5d7bfe7dac9a92201e60b62de30e6aa80",
7
- "buildTime": 1777647244038,
7
+ "buildTime": 1778141707612,
8
8
  "version": "0.1.0"
9
9
  }
@@ -4,6 +4,6 @@
4
4
  "minifiedSize": 54906,
5
5
  "compressionRatio": 0.5623101757742437,
6
6
  "hash": "ffe0b955b64973aa155cc29dc9aeceb72a36459d5a2c734dca872ba936db6f8e",
7
- "buildTime": 1777647244209,
7
+ "buildTime": 1778141707773,
8
8
  "version": "0.1.0"
9
9
  }
@@ -4,6 +4,6 @@
4
4
  "minifiedSize": 44293,
5
5
  "compressionRatio": 0.577021878014076,
6
6
  "hash": "70b009f01b9051dee2db1e4030dd17d08dc53728d30b5e24cffd11d07acfcaf0",
7
- "buildTime": 1777647244962,
7
+ "buildTime": 1778141707903,
8
8
  "version": "0.1.0"
9
9
  }
@@ -4,6 +4,6 @@
4
4
  "minifiedSize": 3999,
5
5
  "compressionRatio": 0.5142128279883382,
6
6
  "hash": "baba8c31b73dbfe6425d8f9fd69c61f41a573e189a185962382b815a15fddb24",
7
- "buildTime": 1777647245049,
7
+ "buildTime": 1778141707926,
8
8
  "version": "0.1.0"
9
9
  }
@@ -4,6 +4,6 @@
4
4
  "minifiedSize": 11531,
5
5
  "compressionRatio": 0.573020810190328,
6
6
  "hash": "a49e9a4875e68a1b8f1c37bcc8284a28e0995652a7df8bad4f1853408d1c81c8",
7
- "buildTime": 1777647245134,
7
+ "buildTime": 1778141707960,
8
8
  "version": "0.1.0"
9
9
  }
@@ -4,6 +4,6 @@
4
4
  "minifiedSize": 2139,
5
5
  "compressionRatio": 0.45153846153846156,
6
6
  "hash": "6d5742652cd9a2c562db3f69663a133d38b95330cbb89b5e84876644c1c10d0a",
7
- "buildTime": 1777647245144,
7
+ "buildTime": 1778141707969,
8
8
  "version": "0.1.0"
9
9
  }
@@ -1 +1 @@
1
- var _LIT_ACTION_=(()=>{var t=Object.defineProperty,e=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.prototype.hasOwnProperty,o={};((e,r)=>{for(var i in r)t(e,i,{get:r[i],enumerable:!0})})(o,{main:()=>main});var a=11e3,n=1000000000000000000n,s=1000000000000n,c=100000000000000n,l=60,u=10,d=8e3;function h(t){switch(t){case 0:return"PENDING_DEPOSIT";case 1:return"PENDING_MINT";case 2:return"ACTIVE";case 3:return"EXPIRED";case 4:return"LIQUIDATABLE";case 5:return"LIQUIDATED";case 6:return"REPAID";case 7:return"CLOSED";default:return`UNKNOWN(${t})`}}var p="partial-payment",g="DiamondHands.Protocol",m="1",f=(t=>(t.SEPOLIA="sepolia",t.ETHEREUM="ethereum",t.HARDHAT="hardhat",t))(f||{}),y={sepolia:"testnet",ethereum:"mainnet",hardhat:"regtest"},w=["infura.io","drpc.org"],b={ethereum:1,sepolia:11155111,hardhat:1337};function T(t){const e=t.toLowerCase().trim();if("sepolia"===e)return"sepolia";if("ethereum"===e||"mainnet"===e)return"ethereum";if("hardhat"===e)return"hardhat";throw new Error(`Unsupported EVM chain: "${t}". Supported chains: ${Object.values(f).join(", ")}`)}function P(t){return b[t]}function v(t){if(!0===t)return!0;if(!1===t)return!1;const e=globalThis;return!0===e.debugAction||(!0===e.LIT_ACTION_DEBUG||("true"===e.LIT_ACTION_DEBUG||function(){try{if("undefined"==typeof process||!process.env)return!1;const t=process.env.LIT_ACTION_DEBUG,e=process.env.DEBUG;return"1"===t||"true"===t||"1"===e||"true"===e}catch{return!1}}()))}function I(...t){v()&&console.log(...t)}var E=class{stepStart(t,e){}stepEnd(t,e){}log(t,e,...r){}warn(t,e){}error(t,e){}getElapsed(){return 0}getRemaining(){return 3e4}summary(){}getExecutionId(){return""}isDebugEnabled(){return!1}},A=class{constructor(t){this.stepStartTimes=new Map,this.stepDurations=new Map,this.TIMEOUT_MS=3e4,this.actionName=t,this.executionStartTime=Date.now();const e=Date.now(),r=Math.random().toString(36).substring(2,9);this.executionId=`exec_${e}_${r}`,this.logHeader()}logHeader(){console.log(`[${this.actionName}] ========================================`),console.log(`[${this.actionName}] Execution started`),console.log(`[${this.actionName}] Execution ID: ${this.executionId}`);void 0!==globalThis.litNodeContext?(console.log(`[${this.actionName}] Context: LIT Node`),console.log(`[${this.actionName}] Node: ${globalThis.litNodeContext?.nodeAddress||"unknown"}`)):console.log(`[${this.actionName}] Context: Browser/Test`),console.log(`[${this.actionName}] ========================================`)}stepStart(t,e){this.stepStartTimes.set(t,Date.now());const r=Date.now()-this.executionStartTime,i=this.TIMEOUT_MS-r;console.log(`[Step ${t}] ${e}...`),console.log(`[Step ${t}] Elapsed: ${r}ms | Remaining: ${i}ms`)}stepEnd(t,e=5e3){const r=this.stepStartTimes.get(t);if(!r)return console.warn(`[Step ${t}] \u26a0\ufe0f stepEnd called without matching stepStart`),void 0;const i=Date.now()-r;this.stepDurations.set(t,i),console.log(`[Step ${t}] Duration: ${i}ms`),i>e&&console.warn(`\u26a0\ufe0f [Step ${t}] Took ${i}ms (> ${e}ms threshold)`);const o=Date.now()-this.executionStartTime,a=this.TIMEOUT_MS-o;a<5e3&&console.warn(`\u26a0\ufe0f [Step ${t}] Time remaining: ${a}ms (approaching timeout)`)}log(t,e,...r){r.length>0?console.log(`[Step ${t}] ${e}`,...r):console.log(`[Step ${t}] ${e}`)}warn(t,e){console.warn(`\u26a0\ufe0f [Step ${t}] ${e}`)}error(t,e){console.error(`\u274c [Step ${t}] ${e}`)}getElapsed(){return Date.now()-this.executionStartTime}getRemaining(){return this.TIMEOUT_MS-this.getElapsed()}summary(){const t=Date.now()-this.executionStartTime,e=this.TIMEOUT_MS-t;if(console.log(`[${this.actionName}] ========================================`),console.log(`[${this.actionName}] Execution Summary`),console.log(`[${this.actionName}] Execution ID: ${this.executionId}`),console.log(`[${this.actionName}] Total time: ${(t/1e3).toFixed(2)}s`),console.log(`[${this.actionName}] Time remaining: ${e}ms`),this.stepDurations.size>0){console.log(`[${this.actionName}] Step breakdown:`);for(const[e,r]of this.stepDurations.entries()){const i=(r/t*100).toFixed(1);console.log(`[${this.actionName}] Step ${e}: ${r}ms (${i}%)`)}}t>2e4&&console.warn(`\u26a0\ufe0f [${this.actionName}] Slow execution: ${(t/1e3).toFixed(2)}s (${e}ms remaining)`),e<5e3&&console.warn(`\u26a0\ufe0f [${this.actionName}] CRITICAL: Only ${e}ms remaining before timeout!`),console.log(`[${this.actionName}] ========================================`)}getExecutionId(){return this.executionId}isDebugEnabled(){return!0}},S=class{static create(t="LIT Action",e){return v(e)?new A(t):new E}};function $(t){if(t instanceof Error)return t.message;if("string"==typeof t)return t;try{return JSON.stringify(t)}catch{return String(t)}}function C(t){return t instanceof Error&&"AbortError"===t.name}var M=class{constructor(t){this.config=t,this.timeout=t.timeout||15e3,this.logger=S.create("BitcoinDataProvider")}async getCurrentBlockHeight(){if(this.logger.stepStart("0","getCurrentBlockHeight"),this.config.rpcHelper){const t=await this.config.rpcHelper.getBlockCount();return this.logger.stepEnd("0"),t}const t=new AbortController,e=setTimeout((()=>t.abort()),this.timeout);try{const r=Date.now(),i=await fetch(`${this.config.providerUrl}/blocks/tip/height`,{signal:t.signal,headers:{Accept:"text/plain","User-Agent":"Mozilla/5.0 (compatible; DiamondHandsValidator/1.0)","ngrok-skip-browser-warning":"true"}});if(clearTimeout(e),I(`[BitcoinDataProvider] blocks/tip/height ${Date.now()-r}ms (timeout budget ${this.timeout}ms)`),!i.ok)throw new Error(`Failed to fetch block height: ${i.status} ${i.statusText}`);const o=await i.text();if(o.trim().startsWith("<!DOCTYPE")||o.trim().startsWith("<html"))throw new Error("Bitcoin RPC endpoint returned HTML instead of block height. URL may be pointing to web interface instead of API endpoint. Expected: plain text number, Got: HTML document. Check that URL ends with /api/esplora (not root /)");const a=parseInt(o.trim(),10);if(isNaN(a)){const t=o.length>200?o.substring(0,200)+"... (truncated)":o;throw new Error(`Invalid block height response: ${t}`)}return this.logger.stepEnd("0"),a}catch(t){if(clearTimeout(e),this.logger.stepEnd("0"),C(t))throw new Error(`Request timeout after ${this.timeout}ms`);throw t}}async getUTXOs(t){this.logger.log("0",`Original address: "${t}"`);const e=this.stripNetworkPrefix(t);if(this.logger.log("0",`Cleaned address: "${e}"`),this.config.rpcHelper)return await this.fetchUTXOsFromRPC(e);try{return await this.fetchUTXOsFromProvider(this.config.providerUrl,e)}catch(t){if(this.logger.warn("0",`Primary provider failed: ${$(t)}`),this.config.fallbackProviders&&this.config.fallbackProviders.length>0){const t=[...this.config.fallbackProviders].sort(((t,e)=>t.priority-e.priority));for(const r of t)try{return this.logger.log("0",`Trying fallback: ${r.name} (${r.url})`),await this.fetchUTXOsFromProvider(r.url,e)}catch(t){this.logger.warn("0",`Fallback ${r.name} failed: ${$(t)}`);continue}}throw new Error(`Failed to fetch UTXOs: ${$(t)}`)}}stripNetworkPrefix(t){return t.replace(/^(REGTEST_|TESTNET_|MAINNET_)/,"")}async fetchUTXOsFromRPC(t){if(!this.config.rpcHelper)throw new Error("RPC helper not configured");const e=this.config.rpcWallet||"";return(await this.config.rpcHelper.listUnspent(e,t)).map((t=>({txid:t.txid,vout:t.vout,satoshis:BigInt(Math.round(1e8*t.amount)),confirmations:t.confirmations})))}parseBlockstreamUTXOs(t,e){if(!Array.isArray(t))throw new Error("Invalid Blockstream response format: expected an array");const r=t,i=[];for(const t of r){let r=0;t.status&&"object"==typeof t.status?t.status.confirmed&&(r=void 0!==t.status.block_height&&t.status.block_height>0?e-t.status.block_height+1:1):void 0!==t.confirmations&&(r=t.confirmations);const o=t.txid,a=void 0!==t.vout?t.vout:t.n,n=void 0!==t.value?t.value:t.satoshis;if(void 0===o||void 0===a||void 0===n)throw new Error("Invalid UTXO row: missing txid, vout/n, or value/satoshis");i.push({txid:o,vout:a,satoshis:BigInt(n),confirmations:r})}return i}async fetchUTXOsFromProvider(t,e){this.logger.stepStart("1","fetchUTXOsFromProvider");const r=`${t}/address/${e}/utxos`;this.logger.log("1",`Fetching UTXOs from ${r}`);const i=new AbortController,o=setTimeout((()=>i.abort()),this.timeout);try{const t=Date.now(),[a,n]=await Promise.all([this.getCurrentBlockHeight(),fetch(r,{signal:i.signal,headers:{Accept:"application/json","Content-Type":"application/json","User-Agent":"Mozilla/5.0 (compatible; DiamondHandsValidator/1.0)","ngrok-skip-browser-warning":"true"}})]);if(clearTimeout(o),I(`[BitcoinDataProvider] parallel(tipHeight + GET utxos) wall ${Date.now()-t}ms endpoint tail .../${e.slice(0,12)}.../utxos (timeout budget ${this.timeout}ms)`),this.logger.log("1",`Current block height: ${a}`),!n.ok)throw new Error(`Bitcoin provider error: ${n.status} ${n.statusText}`);const s=await n.text();if(s.trim().startsWith("<!DOCTYPE")||s.trim().startsWith("<html"))throw new Error("Bitcoin RPC endpoint returned HTML instead of JSON. URL may be pointing to web interface instead of API endpoint. Expected: JSON array of UTXOs, Got: HTML document. Check that URL ends with /api/esplora (not root /)");let c;try{c=JSON.parse(s)}catch(t){const e=s.length>200?s.substring(0,200)+"... (truncated)":s;throw new Error(`Failed to parse JSON response: ${$(t)}. Response: ${e}`)}this.logger.log("1",`Raw API Response: ${JSON.stringify(c,null,2)}`);const l=this.parseBlockstreamUTXOs(c,a);return this.logger.stepEnd("1"),l}catch(t){if(clearTimeout(o),this.logger.stepEnd("1"),C(t))throw new Error(`Request timeout after ${this.timeout}ms`);throw t}}async getUTXOSet(t,e){let r=[],i=null;for(let e=1;e<=3;e++)try{this.logger.log("2",`UTXO query attempt ${e}/3...`),r=await this.getUTXOs(t),this.logger.log("2",`UTXO query succeeded on attempt ${e}`),i=null;break}catch(t){i=t instanceof Error?t:new Error($(t)),this.logger.error("2",`UTXO query failed on attempt ${e}: ${$(t)}`),e<3&&(this.logger.log("2","Waiting 500ms before retry..."),await new Promise((t=>setTimeout(t,500))))}if(i)throw new Error(`Failed to query UTXOs after 3 attempts: ${i.message}`);const o=r.reduce(((t,e)=>t+e.satoshis),0n),a=r.filter((t=>t.confirmations>=e)).reduce(((t,e)=>t+e.satoshis),0n),n=o-a;return{utxos:r,totalBalance:o,totalUTXOs:r.length,confirmedBalance:a,unconfirmedBalance:n}}async getBalance(t){const e=this.stripNetworkPrefix(t),r=`${this.config.providerUrl}/address/${e}`;this.logger.log("3",`Fetching balance from ${r}`);const i=new AbortController,o=setTimeout((()=>i.abort()),this.timeout);try{const t=Date.now(),e=await fetch(r,{signal:i.signal,headers:{Accept:"application/json","Content-Type":"application/json","User-Agent":"Mozilla/5.0 (compatible; DiamondHandsValidator/1.0)","ngrok-skip-browser-warning":"true"}});if(clearTimeout(o),I(`[BitcoinDataProvider] GET address chain_stats ${Date.now()-t}ms (timeout budget ${this.timeout}ms)`),!e.ok)throw new Error(`Bitcoin provider error: ${e.status} ${e.statusText}`);const a=await e.json();if(!a.chain_stats)throw new Error("Invalid response: missing chain_stats");const n=a.chain_stats.funded_txo_sum,s=a.chain_stats.spent_txo_sum;if(void 0===n||void 0===s)throw new Error(`Invalid response: missing required fields. funded_txo_sum: ${n}, spent_txo_sum: ${s}`);const c=BigInt(n-s);return this.logger.log("3",`Balance: ${c.toString()} sats`),this.logger.log("3",` - Funded: ${n} sats, Spent: ${s} sats`),c}catch(t){if(clearTimeout(o),C(t))throw new Error(`Request timeout after ${this.timeout}ms`);const r=$(t);throw this.logger.log("3",`Balance fetch failed: ${r}`),new Error(`Failed to fetch Bitcoin balance for address ${e}: ${r}`)}}async getTransaction(t){if(this.config.rpcHelper)try{const e=this.config.rpcWallet||"",r=await this.config.rpcHelper.getTransaction(e,t);return{txid:r.txid,confirmations:r.confirmations||0}}catch(t){const e=$(t),r=null!==t&&"object"==typeof t&&"code"in t&&"number"==typeof t.code?t.code:void 0;if(e.includes("Invalid or non-wallet transaction")||-5===r)return null;throw t}const e=new AbortController,r=setTimeout((()=>e.abort()),this.timeout);try{const i=Date.now(),o=await fetch(`${this.config.providerUrl}/tx/${t}`,{signal:e.signal});if(clearTimeout(r),I(`[BitcoinDataProvider] GET tx/${t.slice(0,10)}\u2026 ${Date.now()-i}ms (timeout budget ${this.timeout}ms)`),!o.ok){if(404===o.status)return null;throw new Error(`Bitcoin provider error: ${o.status} ${o.statusText}`)}const a=await o.json();return a.status?{txid:a.txid??t,confirmations:a.status.confirmed&&a.status.block_height?1:0}:null}catch(t){if(clearTimeout(r),C(t))throw new Error(`Request timeout after ${this.timeout}ms`);if($(t).includes("404"))return null;throw t}}async getTransactionOutputs(t){if(!this.config.providerUrl)return null;const e=new AbortController,r=setTimeout((()=>e.abort()),this.timeout);try{const i=await fetch(`${this.config.providerUrl}/tx/${t}`,{signal:e.signal});if(clearTimeout(r),!i.ok){if(404===i.status)return null;throw new Error(`Bitcoin provider error: ${i.status} ${i.statusText}`)}const o=await i.json();return o.vout&&Array.isArray(o.vout)?o.vout.map(((t,e)=>({index:e,scriptpubkey:t.scriptpubkey,scriptpubkey_address:t.scriptpubkey_address,value:t.value??0}))):null}catch(t){if(clearTimeout(r),C(t))throw new Error(`Request timeout after ${this.timeout}ms`);throw t}}},k=class t{constructor(e,r,i){if(r&&Array.isArray(r))this.sources=r;else{if(!Array.isArray(e))throw new Error("Price oracle requires a priceProviders array \u2014 defaults and fallbacks have been removed. Pass 3 distinct {name, apiKey} entries from the supported set: "+t.ACTIVE_PROVIDER_NAMES.join(", "));this.sources=this.buildSources(e)}this.sources.sort(((t,e)=>t.priority-e.priority)),this.validateProviderCount()}validateProviderCount(){if(this.sources.length<t.MIN_DISTINCT_PROVIDERS)throw new Error(`Price oracle requires at least ${t.MIN_DISTINCT_PROVIDERS} eligible providers for consensus. Currently configured: ${this.sources.length} provider(s). Supported active providers: ${t.ACTIVE_PROVIDER_NAMES.join(", ")}`)}buildSources(e){if(0===e.length)throw new Error("priceProviders must be a non-empty array of 3 distinct providers, each with an apiKey.");const r=[],i=new Set;let o=1;for(const a of e){if(!a||"string"!=typeof a.name)throw new Error("priceProviders entries must be {name, apiKey, apiSecret?} objects");const e=a.name.toLowerCase();if(!t.ACTIVE_PROVIDER_NAMES.includes(e))throw new Error(`Unsupported price provider "${a.name}". Supported: ${t.ACTIVE_PROVIDER_NAMES.join(", ")}`);if(i.has(e))throw new Error(`Duplicate price provider "${e}" \u2014 the oracle requires 3 DISTINCT providers so a single upstream failure or manipulation cannot compromise consensus. Use 3 different provider names from: ${t.ACTIVE_PROVIDER_NAMES.join(", ")}`);switch(i.add(e),e){case"binance":if(!a.apiKey)throw new Error("Binance provider requires an apiKey");r.push(this.createBinanceSource(a.apiKey,o++));break;case"coinbase":if(!a.apiKey)throw new Error("Coinbase provider requires an apiKey");r.push(this.createCoinbaseSource(a.apiKey,o++));break;case"cryptocompare":if(!a.apiKey)throw new Error("CryptoCompare provider requires an apiKey");r.push(this.createCryptoCompareSource(a.apiKey,o++));break;case"coinmarketcap":if(!a.apiKey)throw new Error("CoinMarketCap provider requires an apiKey");r.push(this.createCoinMarketCapSource(a.apiKey,o++));break;case"coingecko":if(!a.apiKey)throw new Error("CoinGecko provider requires an apiKey (Demo or Pro). Unauthenticated CoinGecko access is not accepted \u2014 the free tier is rate-limited and edge-cached, which breaks median consensus under load.");r.push(this.createCoinGeckoSource(a.apiKey,o++));break;default:throw new Error(`Unhandled provider: ${e}`)}}if(r.length<t.MIN_DISTINCT_PROVIDERS)throw new Error(`Insufficient price providers: need at least ${t.MIN_DISTINCT_PROVIDERS} distinct entries, got ${r.length}.`);return this.selectSampledSources(r)}createCryptoCompareSource(t,e){return{name:"CryptoCompare",fetchPrice:async()=>{const e=new URL("https://min-api.cryptocompare.com/data/price");e.searchParams.set("fsym","BTC"),e.searchParams.set("tsyms","USDT"),e.searchParams.set("api_key",t);const r=await(async t=>{const e=new AbortController,r=setTimeout((()=>e.abort()),5e3);try{const i=await fetch(t,{signal:e.signal,headers:{Accept:"application/json"}});if(clearTimeout(r),!i.ok)throw new Error(`HTTP ${i.status} ${i.statusText}`);return await i.json()}catch(t){if(clearTimeout(r),"AbortError"===t.name)throw new Error("Request timeout after 5000ms");throw t}})(e.toString()),i=Number(r?.USDT);if(!Number.isFinite(i)||i<=0)throw new Error("Invalid CryptoCompare price payload");return i},priority:e}}createFetchJson(){return async(t,e)=>{const r=new AbortController,i=setTimeout((()=>r.abort()),5e3);try{const i=await fetch(t,{headers:{Accept:"application/json",...e||{}},signal:r.signal});if(!i.ok)throw new Error(`HTTP ${i.status} ${i.statusText}`);return await i.json()}finally{clearTimeout(i)}}}getSelectionSeed(){const t="undefined"!=typeof process?process.env.PRICE_PROVIDER_SELECTION_SEED:void 0,e=t?Number(t):NaN;return Number.isFinite(e)?e:("undefined"!=typeof process,0,Math.floor(Date.now()+1e6*Math.random()))}selectSampledSources(t){const e=[...t];let r=this.getSelectionSeed()>>>0;for(let t=e.length-1;t>0;t--){const i=Math.floor((r=1664525*r+1013904223>>>0,r/4294967296*(t+1)));[e[t],e[i]]=[e[i],e[t]]}return e.slice(0,3).map(((t,e)=>({...t,priority:e+1})))}createCoinGeckoSource(t,e){if(!t)throw new Error("createCoinGeckoSource requires an apiKey");const r=this.createFetchJson();return{name:"CoinGecko",fetchPrice:async()=>{const e=t.startsWith("pro:"),i=e?t.slice(4):t,o=e?{"x-cg-pro-api-key":i}:{"x-cg-demo-api-key":i},a=new URL("https://api.coingecko.com/api/v3/simple/price");a.searchParams.set("ids","bitcoin"),a.searchParams.set("vs_currencies","usd");const n=await r(a.toString(),o),s=Number(n?.bitcoin?.usd);if(!Number.isFinite(s)||s<=0)throw new Error("Invalid CoinGecko price payload");return s},priority:e}}createBinanceSource(t,e){if(!t)throw new Error("createBinanceSource requires an apiKey");const r=this.createFetchJson();return{name:"Binance",fetchPrice:async()=>{const e=new URL("https://api.binance.com/api/v3/ticker/price");e.searchParams.set("symbol","BTCUSDT");const i=await r(e.toString(),{"X-MBX-APIKEY":t}),o=Number(i?.price);if(!Number.isFinite(o)||o<=0)throw new Error("Invalid Binance price payload");return o},priority:e}}createCoinbaseSource(t,e){if(!t)throw new Error("createCoinbaseSource requires an apiKey");const r=this.createFetchJson();return{name:"Coinbase",fetchPrice:async()=>{const e=await r("https://api.coinbase.com/v2/prices/BTC-USDT/spot",{"CB-ACCESS-KEY":t}),i=Number(e?.data?.amount);if(!Number.isFinite(i)||i<=0)throw new Error("Invalid Coinbase price payload");return i},priority:e}}createCoinMarketCapSource(t,e){const r=this.createFetchJson();return{name:"CoinMarketCap",fetchPrice:async()=>{const e=new URL("https://pro-api.coinmarketcap.com/v2/cryptocurrency/quotes/latest");e.searchParams.set("symbol","BTC"),e.searchParams.set("convert","USDT");const i=await r(e.toString(),{"X-CMC_PRO_API_KEY":t}),o=Number(i?.data?.BTC?.[0]?.quote?.USDT?.price??i?.data?.BTC?.quote?.USDT?.price);if(!Number.isFinite(o)||o<=0)throw new Error("Invalid CoinMarketCap price payload");return o},priority:e}}async getBTCPrice(){const t=Date.now();if(3!==this.sources.length)throw new Error(`Price oracle requires exactly 3 providers. Currently configured: ${this.sources.length}`);const e=new Promise(((t,e)=>{setTimeout((()=>{e(new Error("Price oracle global timeout after 8000ms"))}),8e3)})),r=Promise.all(this.sources.map((async t=>{const e=Date.now(),r=await t.fetchPrice();return console.log(`[Price Oracle] \u2705 [+${Date.now()-e}ms] ${t.name}: $${r.toLocaleString()}`),r}))),i=[...await Promise.race([r,e])].sort(((t,e)=>t-e)),o=i[1];if((i[2]-i[0])/i[0]>.01){const t=this.sources.map((t=>t.name)).join(", "),e=i.map((t=>`$${t.toLocaleString()}`)).join(", ");throw new Error(`Price consensus failed: prices not within 1% tolerance. Providers: [${t}]. Prices: [${e}]`)}const a=Math.round(100*o),n=1000000n*BigInt(a),s=Date.now()-t;return console.log(`[Price Oracle] Median price: $${o.toLocaleString()} \u2014 total time: ${s}ms`),console.log(`[Price Oracle] Price with 8 decimals: ${n}`),n}async getBTCPriceConsensus(){console.log("[Price Oracle] Fetching BTC price with consensus...");const t=(await Promise.allSettled(this.sources.map((async t=>{const e=await t.fetchPrice();return{source:t.name,price:e}})))).filter((t=>"fulfilled"===t.status)).map((t=>t.value));if(0===t.length)throw new Error("No price sources returned data");console.log(`[Price Oracle] Got prices from ${t.length}/${this.sources.length} sources:`),t.forEach((t=>{console.log(` ${t.source}: $${t.price.toLocaleString()}`)}));const e=t.map((t=>t.price));e.sort(((t,e)=>t-e));const r=e[Math.floor(e.length/2)],i=e[0],o=e[e.length-1]/i,a=t.filter((t=>Math.abs(t.price-r)/r<=.02));if(o>1.05&&a.length===t.length)throw new Error(`Price consensus failed: sources too dispersed (${(100*(o-1)).toFixed(1)}% spread)`);if(a.length<t.length){const e=t.filter((t=>!a.find((e=>e.source===t.source))));if(console.log(`[Price Oracle] \u26a0\ufe0f Detected ${e.length} outlier(s):`),e.forEach((t=>{const e=Math.abs(t.price-r)/r;console.log(` ${t.source}: $${t.price.toLocaleString()} (${(100*e).toFixed(1)}% deviation)`)})),a.length<2)throw new Error("Price consensus failed: insufficient valid sources after outlier removal");console.log(`[Price Oracle] \u2705 Outliers filtered, continuing with ${a.length} valid sources`);const i=a.map((t=>t.price));i.sort(((t,e)=>t-e));const o=i[Math.floor(i.length/2)];console.log(`[Price Oracle] \u2705 Consensus price (median): $${o.toLocaleString()}`);const n=Math.round(100*o);return 1000000n*BigInt(n)}console.log(`[Price Oracle] \u2705 Consensus price (median): $${r.toLocaleString()}`);const n=Math.round(100*r);return 1000000n*BigInt(n)}};k.ACTIVE_PROVIDER_NAMES=["cryptocompare","coinbase","binance","coinmarketcap","coingecko"],k.MIN_DISTINCT_PROVIDERS=3;var B=k;function x(t){return Math.floor(t/l)*l}function U(){return x(Math.floor(Date.now()/1e3))}async function D(){const t=Math.floor(Date.now()/1e3),e=t%l;if(!function(t){const e=(t??Math.floor(Date.now()/1e3))%l;return e<8||e>=l-8}(t))return;let r;r=e>=l-8?l-e+8:8-e,console.log(`[Quantum] Waiting ${r}s for safe moment (current time in quantum: ${e}s)`),await new Promise((t=>setTimeout(t,1e3*r))),console.log(`[Quantum] Safe quantum ready: ${U()}`)}var O=class t{static verifyAmountPositionActionAuthorizationStructure(t){if(!t||"object"!=typeof t)throw new Error('Missing or invalid "auth" parameter - must be an object');if(!t.positionId)throw new Error("auth.positionId is required");if("number"!=typeof t.chainId)throw new Error("auth.chainId must be a number");if("number"!=typeof t.timestamp)throw new Error("auth.timestamp must be a number");if(void 0===t.amount||null===t.amount)throw new Error("auth.amount is required");if(!t.action)throw new Error("auth.action is required");if(!t.signature)throw new Error("auth.signature is required");return t}static async recoverSigner(t,e){try{const r=ethers.utils.arrayify(t),i=ethers.utils.hashMessage(r);return ethers.utils.recoverAddress(i,e)}catch(t){throw new Error(`Failed to recover signer: ${t instanceof Error?t.message:String(t)}`)}}static async checkEIP1271(t,e,r,i){try{const o=new ethers.Contract(t,["function isValidSignature(bytes32,bytes) view returns (bytes4)"],i);return"0x1626ba7e"===await o.isValidSignature(e,r)}catch{return!1}}static async verifyActionAuthorization(t,e,r,i){const o=Math.floor(Date.now()/1e3);await D();const a=i?.strictCurrentQuantum?o:void 0;try{!function(t,e,r){const i=e??Math.floor(Date.now()/1e3),o=x(i),a=x(t);if(r?.strictCurrentQuantum){if(a!==o)throw new Error(`[Quantum] Signature must come from current quantum (strict mode). Signature quantum: ${a}, current quantum: ${o}`);return}const n=o-l,s=o+l;if(a!==n&&a!==o&&a!==s)throw new Error(`[Quantum] Signature outside 3-quantum window. Signature quantum: ${a}, Valid range: [${n} (past), ${o} (current), ${s} (next)]`);if(a===n){if(i<n+8)throw new Error(`[Quantum] Dead zone violation: Current time (${i}) is in first 8 seconds of PAST quantum (${n}). This is unsafe due to boundary instability.`)}else if(a===s&&i>=s+(l-8))throw new Error(`[Quantum] Dead zone violation: Current time (${i}) is in last 8 seconds of NEXT quantum (${s}). This is unsafe due to boundary instability.`)}(t.timestamp,a,{strictCurrentQuantum:!0===i?.strictCurrentQuantum})}catch{return!1}const n=e(),s=await this.recoverSigner(n,t.signature);return await r(s,n,t.signature)}static async verifyAmountPositionActionAuthorization(e,r,i){return this.verifyActionAuthorization(e,(()=>{const t=ethers.utils.keccak256(ethers.utils.toUtf8Bytes(e.action)),r="string"==typeof e.amount?BigInt(e.amount):e.amount,i=[e.positionId,e.timestamp,e.chainId,r.toString(),t];return ethers.utils.solidityKeccak256(["bytes32","uint256","uint256","uint256","bytes32"],i)}),(async(e,o,a)=>e.toLowerCase()===r.toLowerCase()||!!i&&t.checkEIP1271(r,o,a,i)))}static async verifyMintAuthorization(t,e,r){return"mint-ucd"===t.action&&this.verifyAmountPositionActionAuthorization(t,e,r)}static async verifyWithdrawAuthorization(e,r,i){if("withdraw-btc"!==e.action)return!1;return this.verifyActionAuthorization(e,(()=>{const t=ethers.utils.keccak256(ethers.utils.toUtf8Bytes(e.action)),r="string"==typeof e.amount?BigInt(e.amount):e.amount,i=[e.positionId,e.timestamp,e.chainId,r.toString(),e.destinationAddress,t];return ethers.utils.solidityKeccak256(["bytes32","uint256","uint256","uint256","string","bytes32"],i)}),(async(e,o,a)=>e.toLowerCase()===r.toLowerCase()||!!i&&t.checkEIP1271(r,o,a,i)))}static verifyTermPositionActionAuthorizationStructure(t){if(!t||"object"!=typeof t)throw new Error('Missing or invalid "auth" parameter - must be an object');if(!t.positionId)throw new Error("auth.positionId is required");if("number"!=typeof t.chainId)throw new Error("auth.chainId must be a number");if("number"!=typeof t.timestamp)throw new Error("auth.timestamp must be a number");if(void 0===t.newTerm||null===t.newTerm)throw new Error("auth.newTerm is required");if("number"!=typeof t.newTerm)throw new Error("auth.newTerm must be a number");if(!t.action)throw new Error("auth.action is required");if(!t.signature)throw new Error("auth.signature is required");return t}static async verifyTermPositionActionAuthorization(e,r,i){return this.verifyActionAuthorization(e,(()=>{const t=ethers.utils.keccak256(ethers.utils.toUtf8Bytes(e.action)),r=[e.positionId,e.timestamp,e.chainId,e.newTerm,t];return ethers.utils.solidityKeccak256(["bytes32","uint256","uint256","uint256","bytes32"],r)}),(async(e,o,a)=>e.toLowerCase()===r.toLowerCase()||!!i&&t.checkEIP1271(r,o,a,i)))}static async verifyExtendAuthorization(t,e,r){return"extend-position"===t.action&&this.verifyTermPositionActionAuthorization(t,e,r)}static async verifyBtcExecutionAuthorization(e,r,i){if("execute-btc-withdrawal"!==e.action)return!1;return this.verifyActionAuthorization(e,(()=>{const t=ethers.utils.keccak256(ethers.utils.toUtf8Bytes(e.action)),r=[e.positionId,e.timestamp,e.chainId,e.utxoIdentifier,e.networkFee,t];return ethers.utils.solidityKeccak256(["bytes32","uint256","uint256","string","uint256","bytes32"],r)}),(async(e,o,a)=>e.toLowerCase()===r.toLowerCase()||!!i&&t.checkEIP1271(r,o,a,i)),{strictCurrentQuantum:!0})}};function N(t,e){if(0===t)return{termDurationDays:0,termLengthDays:30*e,isExpired:!1,daysUntilExpiry:30*e,daysIntoGracePeriod:0};const r=30*e,i=Math.floor(Date.now()/1e3)-t,o=Math.floor(i/86400);return{termDurationDays:o,termLengthDays:r,isExpired:o>r,daysUntilExpiry:Math.max(0,r-o),daysIntoGracePeriod:Math.max(0,o-r)}}function R(t,e,r,i){if(!t)return r??13e3;const o=i??a,n=Math.min(e,30);return a+n*n*(o-a)/900}function q(t,e,r){const i=t*e/10000000000000000n;if(0n===r)return{collateralValueUsd:i,collateralRatioBps:Number.MAX_SAFE_INTEGER};return{collateralValueUsd:i,collateralRatioBps:Number(i*n*10000n/r)}}var L=class{constructor(t){this.termManagerAddress=t.termManagerAddress,this.loanOpsManagerAddress=t.loanOpsManagerAddress,this.liquidationManagerAddress=t.liquidationManagerAddress,this.chain=t.chain,this.chainId=t.chainId,this.rpcUrl=t.rpcUrl}async getLiquidationThreshold(){try{let t;t=this.rpcUrl?this.rpcUrl:await Lit.Actions.getRpcUrl({chain:this.chain});const e=new ethers.providers.StaticJsonRpcProvider({url:t,timeout:d},{name:"any",chainId:this.chainId}),r=[{inputs:[],name:"liquidationThreshold",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function"}],i=new ethers.Contract(this.loanOpsManagerAddress,r,e),o=await i.liquidationThreshold();return Number(o.toString())}catch(t){throw console.error("[ProtocolParameters] Error fetching liquidation threshold:",t.message),new Error(`Failed to fetch liquidation threshold: ${t.message}`)}}async getTermFees(t){try{let e;e=this.rpcUrl?this.rpcUrl:await Lit.Actions.getRpcUrl({chain:this.chain});const r=new ethers.providers.StaticJsonRpcProvider({url:e,timeout:d},{name:"any",chainId:this.chainId}),i=[{inputs:[{internalType:"uint256",name:"_termMonths",type:"uint256"}],name:"getTermFees",outputs:[{internalType:"uint88",name:"originationFee",type:"uint88"},{internalType:"uint88",name:"extensionFee",type:"uint88"}],stateMutability:"view",type:"function"}],o=new ethers.Contract(this.termManagerAddress,i,r),a=await o.getTermFees(t);return{originationFeeBps:Number(a.originationFee?.toString?.()??a[0]?.toString?.()??a[0]),extensionFeeBps:Number(a.extensionFee?.toString?.()??a[1]?.toString?.()??a[1])}}catch(t){throw console.error("[ProtocolParameters] Error fetching term fees:",t.message),new Error(`Failed to fetch term fees: ${t.message}`)}}async getAuthorizedSpendsHash(t){try{const e=this.rpcUrl||await Lit.Actions.getRpcUrl({chain:this.chain}),r=new ethers.providers.StaticJsonRpcProvider({url:e,timeout:d},{name:"any",chainId:this.chainId}),i=t.startsWith("0x")?t:`0x${t.padStart(64,"0")}`,o=new ethers.Contract(this.loanOpsManagerAddress,[{inputs:[{name:"positionId",type:"bytes32"}],name:"getAuthorizedSpendsHash",outputs:[{name:"",type:"bytes32"}],stateMutability:"view",type:"function"}],r);return await o.getAuthorizedSpendsHash(i)}catch(t){throw console.error("[ProtocolParameters] Error fetching authorized spends hash:",t.message),new Error(`Failed to fetch authorized spends hash: ${t.message}`)}}async getMaxEscalatedThreshold(){try{let t;t=this.rpcUrl?this.rpcUrl:await Lit.Actions.getRpcUrl({chain:this.chain});const e=new ethers.providers.StaticJsonRpcProvider({url:t,timeout:d},{name:"any",chainId:this.chainId}),r=[{inputs:[],name:"maxEscalatedThreshold",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function"}],i=new ethers.Contract(this.liquidationManagerAddress,r,e),o=await i.maxEscalatedThreshold();return Number(o.toString())}catch(t){throw console.error("[ProtocolParameters] Error fetching max escalated threshold:",t.message),new Error(`Failed to fetch max escalated threshold: ${t.message}`)}}};function _(t){return new L(t)}var F=class{constructor(t){this.config=t}async getVaultSnapshot(t){const e=await this.queryPositionState(t);console.log(`[Vault Snapshot] Raw vault address from contract: "${e.vaultAddress}"`);const r=!0===this.config.debugOverrides?.useStubbedBtcData,[i,o]=r?this.buildStubbedBtcSnapshotInputs(e):await Promise.all([this.config.vaultBalance.calculateTrustedBalance(t,e.vaultAddress),this.config.priceOracle.getBTCPriceConsensus()]),a=_({termManagerAddress:this.config.termManagerAddress,loanOpsManagerAddress:this.config.loanOpsManagerAddress,liquidationManagerAddress:this.config.liquidationManagerAddress,chain:this.config.chain,chainId:this.config.chainId||1,rpcUrl:this.config.rpcUrl}),n=this.config.termOverride??e.selectedTerm,[s,c,l]=await Promise.all([a.getLiquidationThreshold(),a.getMaxEscalatedThreshold(),a.getTermFees(n)]),u=N(e.termStartTimestamp,e.selectedTerm),d=R(u.isExpired,u.daysIntoGracePeriod,s,c),h=q(i.availableBalance,o,e.ucdDebt),p=h.collateralRatioBps<d,g=h.collateralRatioBps-d;return{positionId:e.positionId,pkpId:e.pkpId,borrower:e.borrower,vaultAddress:e.vaultAddress,ucdDebt:e.ucdDebt,termStartTimestamp:e.termStartTimestamp,selectedTerm:e.selectedTerm,status:e.status,expiryAt:e.expiryAt,previousExpiryAt:e.previousExpiryAt,totalTerm:e.totalTerm,totalBTCSats:i.totalBalance,totalUTXOs:i.totalUTXOs,authorizedSpendsSats:i.authorizedBalance,authorizedSpendsHash:i.authorizedSpendsHash,availableBTCSats:i.availableBalance,availableUTXOs:i.availableUTXOs,btcPriceUsd:o,collateralValueUsd:h.collateralValueUsd,collateralRatioBps:h.collateralRatioBps,termDurationDays:u.termDurationDays,termLengthDays:u.termLengthDays,isExpired:u.isExpired,daysUntilExpiry:u.daysUntilExpiry,daysIntoGracePeriod:u.daysIntoGracePeriod,currentLiquidationThreshold:d,isLiquidatable:p,marginToLiquidationBps:g,liquidationThresholdBps:s,originationFeeBps:l.originationFeeBps,extensionFeeBps:l.extensionFeeBps,timestamp:Date.now()}}async getVaultSnapshotFast(t){const e=await this.queryPositionState(t);console.log(`[Vault Snapshot] Raw vault address from contract: "${e.vaultAddress}"`);const r=!0===this.config.debugOverrides?.useStubbedBtcData,[i,o]=r?this.buildStubbedBtcSnapshotInputs(e):await Promise.all([this.config.vaultBalance.calculateTrustedBalance(t,e.vaultAddress,!0),this.config.priceOracle.getBTCPriceConsensus()]),a=_({termManagerAddress:this.config.termManagerAddress,loanOpsManagerAddress:this.config.loanOpsManagerAddress,liquidationManagerAddress:this.config.liquidationManagerAddress,chain:this.config.chain,chainId:this.config.chainId||1,rpcUrl:this.config.rpcUrl}),n=this.config.termOverride??e.selectedTerm,[s,c,l]=await Promise.all([a.getLiquidationThreshold(),a.getMaxEscalatedThreshold(),a.getTermFees(n)]),u=N(e.termStartTimestamp,e.selectedTerm),d=R(u.isExpired,u.daysIntoGracePeriod,s,c),h=q(i.availableBalance,o,e.ucdDebt),p=h.collateralRatioBps<d,g=h.collateralRatioBps-d;return{positionId:e.positionId,pkpId:e.pkpId,borrower:e.borrower,vaultAddress:e.vaultAddress,ucdDebt:e.ucdDebt,termStartTimestamp:e.termStartTimestamp,selectedTerm:e.selectedTerm,status:e.status,expiryAt:e.expiryAt,previousExpiryAt:e.previousExpiryAt,totalTerm:e.totalTerm,totalBTCSats:i.totalBalance,totalUTXOs:i.totalUTXOs,authorizedSpendsSats:i.authorizedBalance,authorizedSpendsHash:i.authorizedSpendsHash,availableBTCSats:i.availableBalance,availableUTXOs:i.availableUTXOs,btcPriceUsd:o,collateralValueUsd:h.collateralValueUsd,collateralRatioBps:h.collateralRatioBps,termDurationDays:u.termDurationDays,termLengthDays:u.termLengthDays,isExpired:u.isExpired,daysUntilExpiry:u.daysUntilExpiry,daysIntoGracePeriod:u.daysIntoGracePeriod,currentLiquidationThreshold:d,isLiquidatable:p,marginToLiquidationBps:g,liquidationThresholdBps:s,originationFeeBps:l.originationFeeBps,extensionFeeBps:l.extensionFeeBps,timestamp:Date.now()}}buildStubbedBtcSnapshotInputs(t){const e=BigInt(this.config.debugOverrides?.stubbedAvailableBtcSats??"10000000"),r=BigInt(this.config.debugOverrides?.stubbedBtcPriceUsd??"5000000000000");return[{totalUTXOs:[],totalBalance:e,authorizedUTXOs:[],authorizedBalance:0n,authorizedSpendsHash:this.config.debugOverrides?.stubbedAuthorizedSpendsHash??ethers.utils.keccak256(ethers.utils.defaultAbiCoder.encode(["bytes32","bytes32[]"],[t.positionId,[]])),availableUTXOs:[],availableBalance:e,vaultAddress:t.vaultAddress,positionId:t.positionId,timestamp:Date.now()},r]}async queryPositionState(t){const e=t.startsWith("0x")?t:`0x${t.padStart(64,"0")}`;let r;r=this.config.rpcUrl?this.config.rpcUrl:await Lit.Actions.getRpcUrl({chain:this.config.chain});const i=new ethers.providers.StaticJsonRpcProvider({url:r,timeout:d},this.config.chainId),o=new ethers.Contract(this.config.contractAddress,[{inputs:[],name:"core",outputs:[{internalType:"address",name:"",type:"address"}],stateMutability:"view",type:"function"}],i),a=await o.core(),n=new ethers.Contract(a,[{inputs:[{internalType:"bytes32",name:"positionId",type:"bytes32"}],name:"getPositionDetails",outputs:[{components:[{internalType:"bytes32",name:"positionId",type:"bytes32"},{internalType:"bytes32",name:"pkpId",type:"bytes32"},{internalType:"uint256",name:"ucdDebt",type:"uint256"},{internalType:"string",name:"vaultAddress",type:"string"},{internalType:"address",name:"borrower",type:"address"},{internalType:"uint40",name:"createdAt",type:"uint40"},{internalType:"uint40",name:"lastUpdated",type:"uint40"},{internalType:"uint16",name:"selectedTerm",type:"uint16"},{internalType:"uint40",name:"expiryAt",type:"uint40"},{internalType:"enum LoanStatusLib.LoanStatus",name:"status",type:"uint8"},{internalType:"uint40",name:"previousExpiryAt",type:"uint40"},{internalType:"uint16",name:"totalTerm",type:"uint16"}],internalType:"struct IPositionManagerCore.Position",name:"",type:"tuple"}],stateMutability:"view",type:"function"}],i),s=await n.getPositionDetails(e),c=Number(s.status);if(c<0||c>7)throw console.error("[VaultSnapshot] INVALID STATUS - ABI decoding error detected"),console.error(" Position ID:",e),console.error(" Contract:",this.config.contractAddress),console.error(" Status decoded:",c,"(expected 0-7)"),console.error(" Full position:",JSON.stringify(s,null,2)),new Error(`Invalid position status decoded from contract: ${c} (expected 0-7). This indicates an ABI decoding issue. Position: ${e}`);const l=Number(s.expiryAt),u=Number(s.selectedTerm),h=Number(s.totalTerm),p=Number(s.previousExpiryAt),g=(f=u,0===(m=l)?0:m-30*f*86400);var m,f;return{positionId:s.positionId,pkpId:s.pkpId,borrower:s.borrower,vaultAddress:this.parseVaultAddress(s.vaultAddress,T(this.config.chain)),ucdDebt:BigInt(s.ucdDebt.toString()),termStartTimestamp:g,selectedTerm:u,status:c,expiryAt:l,previousExpiryAt:p,totalTerm:h}}parseVaultAddress(t,e){try{const r=JSON.parse(t);if("object"==typeof r&&null!==r){if("sepolia"===e.toLowerCase())return r.regtest||r.testnet||r.mainnet;const t=function(t){return y[t]}(e);return"testnet"===t&&(r.testnet||r.regtest)||r.mainnet}}catch(t){}return t}async isLiquidatable(t){return(await this.getVaultSnapshot(t)).isLiquidatable}async getCollateralRatio(t){return(await this.getVaultSnapshot(t)).collateralRatioBps}async getAvailableBalance(t){return(await this.getVaultSnapshot(t)).availableBTCSats}async hasSufficientCollateral(t,e,r){const i=await this.getVaultSnapshot(t);return function(t,e,r,i){const o=e+r;return 0n===o||Number(t*n*10000n/o)>=i}(i.collateralValueUsd,i.ucdDebt,e,r)}};var z=class{constructor(t){this.config=t,this.bitcoinProvider=t.bitcoinProvider}async calculateBalance(t,e){const r=await this.bitcoinProvider.getBalance(e),i=await this.getAuthorizedSpendsFromContract(t),o=i.reduce(((t,e)=>t+e.satoshis),0n),a=r>o?r-o:0n;return{totalUTXOs:[],totalBalance:r,authorizedUTXOs:i,authorizedBalance:o,authorizedSpendsHash:this.computeAuthorizedSpendsHash(t,i),availableUTXOs:[],availableBalance:a,vaultAddress:e,positionId:t,timestamp:Date.now()}}async calculateTrustedBalance(t,e,r){if(r)return await this.calculateBalance(t,e);const i=this.config.minConfirmations||6,o=await this.bitcoinProvider.getUTXOSet(e,i),a=await this.getAuthorizedSpendsFromContract(t);for(const t of a){if(o.utxos.some((e=>e.txid===t.txid&&e.vout===t.vout)))throw new Error(`Authorized UTXO ${t.txid}:${t.vout} not yet spent. Transaction was authorized in smart contract but not signed and broadcasted to Bitcoin network. Complete the transaction by signing and broadcasting it.`)}const n=a.reduce(((t,e)=>t+e.satoshis),0n),s=o.utxos.filter((t=>!this.isUTXOAuthorized(t,a))),c=s.reduce(((t,e)=>t+e.satoshis),0n),l=this.computeAuthorizedSpendsHash(t,a);return{totalUTXOs:o.utxos,totalBalance:o.totalBalance,authorizedUTXOs:a,authorizedBalance:n,authorizedSpendsHash:l,availableUTXOs:s,availableBalance:c,vaultAddress:e,positionId:t,timestamp:Date.now()}}async getTrustedBalance(t,e){return(await this.calculateTrustedBalance(t,e)).availableBalance}async getAvailableUTXOs(t,e){return(await this.calculateTrustedBalance(t,e)).availableUTXOs}async isUTXOAvailable(t,e,r,i){return(await this.getAvailableUTXOs(t,e)).some((t=>t.txid===r&&t.vout===i))}isUTXOAuthorized(t,e){return e.some((e=>e.txid===t.txid&&e.vout===t.vout))}async getAuthorizedSpendsFromContract(t){const e=t.startsWith("0x")?t:`0x${t.padStart(64,"0")}`;let r;r=this.config.rpcUrl?this.config.rpcUrl:await Lit.Actions.getRpcUrl({chain:this.config.chain});const i=new ethers.providers.StaticJsonRpcProvider({url:r,timeout:d},this.config.chainId),o=new ethers.Contract(this.config.contractAddress,[{inputs:[{internalType:"bytes32",name:"positionId",type:"bytes32"}],name:"getAuthorizedSpends",outputs:[{components:[{internalType:"string",name:"txid",type:"string"},{internalType:"uint32",name:"vout",type:"uint32"},{internalType:"uint256",name:"satoshis",type:"uint256"},{internalType:"string",name:"targetAddress",type:"string"},{internalType:"uint256",name:"targetAmount",type:"uint256"},{internalType:"uint256",name:"authorizedAt",type:"uint256"}],internalType:"struct LoanOperationsManager.AuthorizedSpend[]",name:"",type:"tuple[]"}],stateMutability:"view",type:"function"}],i);return(await o.getAuthorizedSpends(e)).map((e=>({txid:e.txid,vout:Number(e.vout),satoshis:BigInt(e.satoshis.toString()),positionId:t,targetAddress:e.targetAddress,targetAmount:BigInt(e.targetAmount.toString()),timestamp:Number(e.authorizedAt)})))}computeAuthorizedSpendsHash(t,e){const r=t.startsWith("0x")?t:`0x${t.padStart(64,"0")}`;if(0===e.length)return ethers.utils.keccak256(ethers.utils.defaultAbiCoder.encode(["bytes32","bytes32[]"],[r,[]]));const i=e.map((t=>{const e=ethers.utils.keccak256(ethers.utils.toUtf8Bytes(t.txid)),r=ethers.utils.keccak256(ethers.utils.toUtf8Bytes(t.targetAddress));return ethers.utils.keccak256(ethers.utils.defaultAbiCoder.encode(["bytes32","uint32","uint256","bytes32","uint256","uint256"],[e,t.vout,t.satoshis.toString(),r,t.targetAmount.toString(),t.timestamp]))}));return ethers.utils.keccak256(ethers.utils.defaultAbiCoder.encode(["bytes32","bytes32[]"],[r,i]))}};var V="function getProtocolPauseStatus() view returns (tuple(bool positionManagerPaused, bool positionManagerLiquidationsPaused, bool positionManagerCorePaused, bool loanOperationsPaused, bool collateralManagerPaused, bool liquidationManagerPaused, bool circuitBreakerPaused, bool ucdControllerPaused, bool ucdTokenPaused, bool simplePsmPaused))",X={mintUcd:["positionManagerPaused","positionManagerCorePaused","loanOperationsPaused","circuitBreakerPaused","ucdControllerPaused","ucdTokenPaused","simplePsmPaused"],withdrawBtc:["positionManagerPaused","positionManagerCorePaused","loanOperationsPaused","collateralManagerPaused","circuitBreakerPaused","ucdControllerPaused","ucdTokenPaused","simplePsmPaused"],processPayment:["positionManagerPaused","positionManagerCorePaused","loanOperationsPaused","circuitBreakerPaused","ucdControllerPaused","ucdTokenPaused"],extendPosition:["positionManagerPaused","positionManagerCorePaused","loanOperationsPaused","circuitBreakerPaused","ucdControllerPaused","ucdTokenPaused","simplePsmPaused"],liquidation:["positionManagerLiquidationsPaused","liquidationManagerPaused","circuitBreakerPaused"],adminLiquidation:["positionManagerLiquidationsPaused","liquidationManagerPaused","circuitBreakerPaused"],btcTransactionSign:["positionManagerPaused","positionManagerCorePaused","loanOperationsPaused","collateralManagerPaused","circuitBreakerPaused"]};function H(t){return{positionManagerPaused:t.positionManagerPaused,positionManagerLiquidationsPaused:t.positionManagerLiquidationsPaused,positionManagerCorePaused:t.positionManagerCorePaused,loanOperationsPaused:t.loanOperationsPaused,collateralManagerPaused:t.collateralManagerPaused,liquidationManagerPaused:t.liquidationManagerPaused,circuitBreakerPaused:t.circuitBreakerPaused,ucdControllerPaused:t.ucdControllerPaused,ucdTokenPaused:t.ucdTokenPaused,simplePsmPaused:t.simplePsmPaused}}var K=new ethers.utils.Interface(["function views() view returns (address)","function paused() view returns (bool)","function liquidationPaused() view returns (bool)","function simplePsm() view returns (address)"]),j=new ethers.utils.Interface(["function core() view returns (address)","function loanOps() view returns (address)","function collateralManager() view returns (address)","function liquidationManager() view returns (address)","function circuitBreaker() view returns (address)"]),G=new ethers.utils.Interface(["function getUcdController() view returns (address)","function ucdToken() view returns (address)"]),J=new ethers.utils.Interface(["function paused() view returns (bool)"]);async function W(t,e,r){return t.call({to:e,data:r})}async function Q(t,e){const r=new ethers.utils.Interface([V]).encodeFunctionData("getProtocolPauseStatus",[]);let i;try{return function(t){const e=new ethers.utils.Interface([V]).decodeFunctionResult("getProtocolPauseStatus",t)[0];return Array.isArray(e)?H({positionManagerPaused:e[0],positionManagerLiquidationsPaused:e[1],positionManagerCorePaused:e[2],loanOperationsPaused:e[3],collateralManagerPaused:e[4],liquidationManagerPaused:e[5],circuitBreakerPaused:e[6],ucdControllerPaused:e[7],ucdTokenPaused:e[8],simplePsmPaused:e[9]}):H(e)}(await t.call({to:e,data:r}))}catch(t){i=t}try{return await async function(t,e){const r=await W(t,e,K.encodeFunctionData("views",[])),[i]=K.decodeFunctionResult("views",r),[o,a,n,s,c,l,u,d]=await Promise.all([W(t,e,K.encodeFunctionData("paused",[])),W(t,e,K.encodeFunctionData("liquidationPaused",[])),W(t,e,K.encodeFunctionData("simplePsm",[])),W(t,i,j.encodeFunctionData("core",[])),W(t,i,j.encodeFunctionData("loanOps",[])),W(t,i,j.encodeFunctionData("collateralManager",[])),W(t,i,j.encodeFunctionData("liquidationManager",[])),W(t,i,j.encodeFunctionData("circuitBreaker",[]))]),h=K.decodeFunctionResult("paused",o)[0],p=K.decodeFunctionResult("liquidationPaused",a)[0],g=K.decodeFunctionResult("simplePsm",n)[0],m=j.decodeFunctionResult("core",s)[0],f=j.decodeFunctionResult("loanOps",c)[0],y=j.decodeFunctionResult("collateralManager",l)[0],w=j.decodeFunctionResult("liquidationManager",u)[0],b=j.decodeFunctionResult("circuitBreaker",d)[0],T=await W(t,f,G.encodeFunctionData("getUcdController",[])),P=await W(t,f,G.encodeFunctionData("ucdToken",[])),v=G.decodeFunctionResult("getUcdController",T)[0],I=G.decodeFunctionResult("ucdToken",P)[0],E=async e=>{if(!e||e===ethers.constants.AddressZero)return!1;try{const r=await W(t,e,J.encodeFunctionData("paused",[]));return J.decodeFunctionResult("paused",r)[0]}catch{return!1}},[A,S,$,C,M,k,B,x]=await Promise.all([E(m),E(f),E(y),E(w),E(b),E(v),E(I),E(g)]);return H({positionManagerPaused:h,positionManagerLiquidationsPaused:p,positionManagerCorePaused:A,loanOperationsPaused:S,collateralManagerPaused:$,liquidationManagerPaused:C,circuitBreakerPaused:M,ucdControllerPaused:k,ucdTokenPaused:B,simplePsmPaused:x})}(t,e)}catch(t){const e=i instanceof Error?i.message:String(i),r=t instanceof Error?t.message:String(t);throw new Error(`getProtocolPauseStatus failed (aggregate: ${e}; decomposed: ${r})`)}}function Y(t){if("string"!=typeof t)throw new Error("positionId must be a string");const e=t.trim(),r=/^0x/i.test(e)?e.slice(2):e;if(0===r.length)throw new Error("positionId is empty");if(!/^[0-9a-fA-F]+$/.test(r))throw new Error("positionId must be hexadecimal (bytes32)");if(r.length>64)throw new Error("positionId exceeds 32 bytes");return"0x"+r.padStart(64,"0").toLowerCase()}var Z={hardhat:{chain:"hardhat",chainId:b.hardhat,bitcoinNetwork:y.hardhat,allowArbitraryBitcoinProvider:!0,allowArbitraryEvmRpc:!0,minBitcoinConfirmations:1},sepolia:{chain:"sepolia",chainId:b.sepolia,bitcoinNetwork:y.sepolia,allowArbitraryBitcoinProvider:!1,allowArbitraryEvmRpc:!1,minBitcoinConfirmations:1},ethereum:{chain:"ethereum",chainId:b.ethereum,bitcoinNetwork:y.ethereum,allowArbitraryBitcoinProvider:!1,allowArbitraryEvmRpc:!1,minBitcoinConfirmations:6}};var tt,et=[{inputs:[{internalType:"uint256",name:"chainId",type:"uint256"}],name:"getProviders",outputs:[{internalType:"bytes32[]",name:"ids",type:"bytes32[]"},{components:[{internalType:"bytes",name:"ciphertext",type:"bytes"},{internalType:"bytes32",name:"pkpId",type:"bytes32"},{internalType:"uint64",name:"addedAt",type:"uint64"}],internalType:"struct BitcoinProviderRegistry.ProviderEntry[]",name:"entries",type:"tuple[]"}],stateMutability:"view",type:"function"}];async function rt(t,e){const r=66===e.length?"0x"+e.slice(-40):e;try{const e=await Lit.Actions.Decrypt({pkpId:r,ciphertext:t}),i="string"==typeof e?e:String(e??"");if(!i)throw new Error("Decrypt returned empty plaintext");return i}catch(t){throw new Error(`Failed to decrypt Bitcoin provider URL (pkpId=${e}): ${t?.message??String(t)}`)}}async function it(t){const{policy:e,bitcoinProviderUrl:r,ethersProvider:i,registryAddress:o}=t;if(!r||"string"!=typeof r)throw new Error("resolveBitcoinProviderForPolicy: bitcoinProviderUrl is required");if(e.allowArbitraryBitcoinProvider)return{name:"Custom Provider",url:r,minConfirmations:e.minBitcoinConfirmations,network:e.bitcoinNetwork};if(!o)throw new Error(`Bitcoin provider registry address is required on chain "${e.chain}" \u2014 set contractAddresses.BitcoinProviderRegistry. Hardcoded allowlists have been removed (see audit C-1/H-6 fix).`);const a=await async function(t,e,r){if(!r||"string"!=typeof r)throw new Error("fetchBitcoinProviderEntries: registryAddress (BitcoinProviderRegistry) is required");if(!e)throw new Error("fetchBitcoinProviderEntries: ethers provider is required");const i=new ethers.Contract(r,et,e),[o,a]=await i.getProviders(t),n=[];for(let t=0;t<o.length;t++)n.push({providerId:o[t],ciphertext:a[t].ciphertext,pkpId:a[t].pkpId,addedAt:Number(a[t].addedAt)});return n}(e.chainId,i,o);if(0===a.length)throw new Error(`No Bitcoin providers registered for chainId=${e.chainId} in ${o}. Admin must addProvider(...) via AdminModule before LIT Actions can sign.`);const n=await async function(t,e){if(!e)throw new Error("matchAllowedProviderUrl: candidateUrl is required");for(const r of t)if(await rt(r.ciphertext,r.pkpId)===e)return r.providerId;return null}(a,r);if(!n)throw new Error(`Bitcoin provider URL not in any registered encrypted slot for chainId=${e.chainId}`);return{name:`Registry Provider ${n.slice(0,10)}`,url:r,minConfirmations:e.minBitcoinConfirmations,network:e.bitcoinNetwork}}async function main(t){for(const[e,r]of Object.entries(t))globalThis[e]=r;const e=S.create("Process Payment Validator");e.log("0","========================================");e.log("0","Payment Validator v1.0.2 (deployed: 2026-01-23T23:45:00Z)"),e.log("0","DEBUG: Received globalThis parameters:"),e.log("0","globalThis.contractAddresses:",JSON.stringify(globalThis.contractAddresses)),e.log("0","globalThis.positionId:",globalThis.positionId),e.log("0","globalThis.paymentAmount:",globalThis.paymentAmount),e.log("0","globalThis.priceProviders:",JSON.stringify(globalThis.priceProviders)),e.log("0","globalThis.priceProviders type:",typeof globalThis.priceProviders),e.log("0","globalThis.priceProviders isArray:",Array.isArray(globalThis.priceProviders)),e.log("0","All globalThis keys:",Object.keys(globalThis).filter((t=>!t.startsWith("_")&&"console"!==t&&"require"!==t&&"module"!==t&&"exports"!==t&&"process"!==t&&"Buffer"!==t&&"setTimeout"!==t&&"clearTimeout"!==t&&"setInterval"!==t&&"clearInterval"!==t))),e.log("0","========================================");let r,i,o,a,n,l,d="0";try{d="0a",e.log("0a","Validating configuration...");const t=globalThis.chain,y=globalThis.bitcoinProviderUrl,v=globalThis.positionId,E=globalThis.paymentAmount;if(!t)throw new Error('Missing required parameter: "chain"');if(!y)throw new Error('Missing required parameter: "bitcoinProviderUrl"');if(!v)throw new Error('Missing required parameter: "positionId"');if(!E)throw new Error('Missing required parameter: "paymentAmount"');const A=BigInt(E);if(A<=0n)throw new Error(`Payment amount must be positive, got: ${A.toString()} wei`);d="0b";const S=function(t){const e=T(t),r=Z[e];if(!r)throw new Error(`No chain policy registered for "${t}"`);return r}(t),$=S.chain,C=S.bitcoinNetwork;let k;I(` \u2705 Chain: ${t} (${$})`),I(` \u2705 Bitcoin Network: ${C}`),e.log("0",` \u2705 Payment Amount: ${A.toString()} wei`),d="0c",e.log("0c","Loading contract addresses...");const x=globalThis.contractAddresses;if(!x||"object"!=typeof x)throw new Error("contractAddresses is required (PositionManager, LoanOperationsManagerModule, LiquidationManager...).");if(i=x.PositionManager,o=x.LoanOperationsManagerModule,a=x.LiquidationManagerModule??x.LiquidationManager??"",n=x.BTCSpendAuthorizer||o,!i||!o||!a)throw new Error("Missing required contract addresses in contractAddresses object");e.log("0"," \u2705 Contract addresses loaded from parameters"),d="0d",e.log("0d","Initializing modules...");const D=globalThis.rpcUrl,N=P($);if(D||"hardhat"===t||"sepolia"===t){const i="hardhat"===t?{chainId:b.hardhat,name:"hardhat"}:"sepolia"===t?{chainId:b.sepolia,name:"sepolia"}:{chainId:N,name:t};e.log("0",` Using StaticJsonRpcProvider for ${t} to skip network detection`),e.log("0"," Network config:",i),r=new ethers.providers.StaticJsonRpcProvider(D,i)}else r=new ethers.providers.JsonRpcProvider(D);!function(t,e){const r=X[t],i=[];for(const t of r)e[t]&&i.push(t);if(i.length>0)throw new Error(JSON.stringify({code:"PROTOCOL_PAUSED",operation:t,paused:i,status:e}))}("processPayment",await Q(r,i)),e.log("0","[DEBUG] Before PriceOracleModule init:"),e.log("0"," globalThis.priceProviders:",JSON.stringify(globalThis.priceProviders)),e.log("0"," typeof globalThis.priceProviders:",typeof globalThis.priceProviders),e.log("0"," Array.isArray(globalThis.priceProviders):",Array.isArray(globalThis.priceProviders));const R=new B(globalThis.priceProviders,void 0);D&&"string"==typeof D&&!function(t,e){if(t.allowArbitraryEvmRpc)return;if(!e||"string"!=typeof e)throw new Error("validateEvmRpcForPolicy: rpcUrl must be a non-empty string");let r;try{r=new URL(e).hostname}catch(t){throw new Error(`Invalid rpcUrl: ${t?.message??String(t)}`)}if(!w.some((t=>r===t||r.endsWith(`.${t}`))))throw new Error(`Custom RPC domain "${r}" is not in the approved list: ${w.join(", ")}`)}(S,D),k=await it({policy:S,bitcoinProviderUrl:y,ethersProvider:r,registryAddress:globalThis.contractAddresses?.BitcoinProviderRegistry}),I(` \u2705 Bitcoin Provider: ${k.name} (min confs ${k.minConfirmations})`),e.log("0","[DEBUG] After PriceOracleModule init:"),e.log("0"," PriceOracleModule sources count:",R.sources?.length||0),e.log("0"," PriceOracleModule first source:",R.sources?.[0]?.name||"none");const q=new M({providerUrl:k.url}),L=(f={contractAddress:n,chain:t,chainId:N,rpcUrl:D,bitcoinProvider:q,minConfirmations:k.minConfirmations},new z(f));if(!globalThis.authMessage||!globalThis.userSignature)throw new Error("Missing authorization parameters: authMessage and userSignature are required");const _=Y(v);if(_!==Y(globalThis.authMessage.positionId))throw new Error("positionId must match authMessage.positionId (canonical bytes32: 0x + 64 hex, case-insensitive, short forms padded)");l=_,d="1",e.log("1","Getting vault snapshot...");const V=function(t){return new F(t)}({contractAddress:i,termManagerAddress:globalThis.contractAddresses?.TermManagerModule||i,loanOpsManagerAddress:o,liquidationManagerAddress:a,chain:t,chainId:N,rpcUrl:D,vaultBalance:L,priceOracle:R}),H=await V.getVaultSnapshot(l),K=H.btcPriceUsd;if(K<s||K>c)throw new Error(`Bitcoin price ${K} is outside acceptable range (${s} - ${c}). This may indicate oracle manipulation or stale price data. Please try again later.`);e.log("0",` \u2705 BTC price validation passed: $${(Number(K)/1e8).toFixed(2)}`),e.log("0"," \u2705 Vault snapshot retrieved"),e.log("0",` - Position status: ${h(H.status)}`),e.log("0",` - Total debt: ${H.ucdDebt.toString()} wei`),d="1.5",e.log("1.5","Authenticating caller authorization...");if(!await O.verifyAmountPositionActionAuthorization({positionId:globalThis.authMessage.positionId,timestamp:globalThis.authMessage.timestamp,chainId:globalThis.authMessage.chainId,amount:globalThis.authMessage.paymentAmount,action:globalThis.authMessage.action,signature:globalThis.userSignature},H.borrower))throw new Error("Caller not authorized to make payment on this position");e.log("0"," \u2705 Caller authorization validated"),d="2",e.log("2","Validating position state...");const j=h(H.status);if(!["PENDING_MINT","ACTIVE","EXPIRED","LIQUIDATABLE"].includes(j))throw new Error(`Invalid position status for payment: ${j}. Must be PENDING_MINT, ACTIVE, EXPIRED, or LIQUIDATABLE.`);e.log("0",` \u2705 Position status valid for payment: ${j}`),d="3",e.log("3","Validating payment amount...");const G=BigInt(H.ucdDebt.toString());if(A>G)throw new Error(`Payment amount ${A.toString()} wei exceeds total debt ${G.toString()} wei. Payment amount must be less than or equal to total debt.`);e.log("0",` \u2705 Payment amount valid: ${A.toString()} wei`),e.log("0",` - Total debt: ${G.toString()} wei`),d="3.25",e.log("0","[Step 3.25] Circuit breaker check..."),e.log("0"," \u26a0\ufe0f Circuit breaker validation deferred to contract (state-changing function)"),e.log("0"," \u2705 Payment amount within bounds for circuit breaker check"),d="3.5",e.log("0","[Step 3.5] UCD validation skipped (pre-validated by SDK)"),d="4",e.log("4","Collateral ratio validation skipped for repayment (handled off-chain / by business rules)"),d="5",e.log("5","Building authorization message...");const J=globalThis.quantumTimestamp,W=J?Number(J):U()+u;e.log("5",`Using quantum timestamp: ${W} ${J?"(from authorization)":"(generated)"}`);const tt=P($),et={name:g,version:m,chainId:tt,verifyingContract:ethers.utils.getAddress(o)},rt=ethers.utils.keccak256(ethers.utils.toUtf8Bytes(p)),ot={PartialPayment:[{name:"actionTag",type:"bytes32"},{name:"positionId",type:"bytes32"},{name:"quantumTimestamp",type:"uint256"},{name:"btcPrice",type:"uint256"},{name:"paymentAmount",type:"uint256"},{name:"borrower",type:"address"}]},at=ethers.utils._TypedDataEncoder.hash(et,ot,{actionTag:rt,positionId:l,quantumTimestamp:W,btcPrice:K.toString(),paymentAmount:A.toString(),borrower:ethers.utils.getAddress(H.borrower)}),nt=globalThis.publicKey;if(!nt)throw new Error('Missing required parameter: "publicKey"');if("string"!=typeof nt||0===nt.length)throw new Error("Invalid publicKey: expected non-empty string, got "+typeof nt);const st=nt;e.log("0",`[Step 6] PublicKey length: ${st.length}, starts with: ${st.substring(0,10)}...`),d="6",e.log("6","Signing authorization..."),e.log("0",` Message hash: ${at}`),e.log("0",` Message hash bytes length: ${ethers.utils.arrayify(at).length}`),e.log("0",` Public key: ${st.substring(0,20)}...`),e.log("0",` Public key length: ${st.length}`),e.log("0","======= PKP AUTHORIZATION DIAGNOSTICS ======="),e.log("0","globalThis.publicKey:",st?`${st.substring(0,20)}...`:"MISSING"),e.log("0","globalThis.publicKey length:",st?.length||0),e.log("0","globalThis.publicKey has 0x prefix:",st?.startsWith("0x")||!1);const ct=globalThis.pkpTokenId;e.log("0","globalThis.pkpTokenId:",ct||"NOT SET"),ct&&(e.log("0","globalThis.pkpTokenId type:",typeof ct),e.log("0","globalThis.pkpTokenId value:",ct),e.log("0","NOTE: pkpTokenId is used for session signature scoping in LitOps, not as a signEcdsa parameter."));const lt=globalThis.positionId;e.log("0","globalThis.positionId:",lt||"NOT SET");const ut=globalThis.ipfsId;e.log("0","globalThis.ipfsId:",ut||"NOT SET");try{const t=ethers.utils.arrayify("0x"+st),r=ethers.utils.computeAddress(t);e.log("0","Computed ETH Address from publicKey:",r)}catch(t){e.log("0","Could not compute ETH address from publicKey:",t instanceof Error?t.message:String(t))}e.log("0","Message hash to sign:",at),e.log("0","Signature name: paymentAuth"),e.log("0","=============================================");const dt=globalThis.pkpTokenId;let ht;try{"pkpTokenId"in globalThis&&delete globalThis.pkpTokenId;const t=globalThis.validatorWalletAddress;if(!t||!t.startsWith("0x"))throw new Error("validatorWalletAddress required \u2014 must be a createWallet-derived address");const e=await Lit.Actions.getPrivateKey({pkpId:t}),r="string"==typeof e?e.startsWith("0x")?e:`0x${e}`:String(e),i=new ethers.Wallet(r);ht=await i._signTypedData(et,ot,{actionTag:rt,positionId:l,quantumTimestamp:W,btcPrice:K.toString(),paymentAmount:A.toString(),borrower:ethers.utils.getAddress(H.borrower)})}finally{void 0!==dt&&(globalThis.pkpTokenId=dt)}return e.log("0"," \u2705 Signature obtained: "+(ht?"present":"missing")),e.log("0","\u2705 Complete"),{approved:!0,positionId:l,paymentAmount:A.toString(),btcPrice:K.toString(),messageHash:at,signature:ht,timestamp:W,validatorPkp:st}}catch(t){return console.error("[Process Payment Validator] Failed:",t.message),{approved:!1,reason:t.message||t.toString(),positionId:globalThis.positionId,timestamp:Date.now()}}var f}return tt=o,((o,a,n,s)=>{if(a&&"object"==typeof a||"function"==typeof a)for(let c of r(a))i.call(o,c)||c===n||t(o,c,{get:()=>a[c],enumerable:!(s=e(a,c))||s.enumerable});return o})(t({},"__esModule",{value:!0}),tt)})();if("object"==typeof _LIT_ACTION_)if("function"==typeof _LIT_ACTION_.main)var main=_LIT_ACTION_.main;else"function"==typeof _LIT_ACTION_.go&&_LIT_ACTION_.go();
1
+ var _LIT_ACTION_=(()=>{var t=Object.defineProperty,e=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.prototype.hasOwnProperty,o={};((e,r)=>{for(var i in r)t(e,i,{get:r[i],enumerable:!0})})(o,{main:()=>main});var a=11e3,n=1000000000000000000n,s=1000000000000n,c=100000000000000n,l=60,u=10,d=8e3;function h(t){switch(t){case 0:return"PENDING_DEPOSIT";case 1:return"PENDING_MINT";case 2:return"ACTIVE";case 3:return"EXPIRED";case 4:return"LIQUIDATABLE";case 5:return"LIQUIDATED";case 6:return"REPAID";case 7:return"CLOSED";default:return`UNKNOWN(${t})`}}var p="partial-payment",g="DiamondHands.Protocol",m="1",f=(t=>(t.SEPOLIA="sepolia",t.ETHEREUM="ethereum",t.HARDHAT="hardhat",t))(f||{}),y={sepolia:"testnet",ethereum:"mainnet",hardhat:"regtest"},w=["infura.io","drpc.org"],b={ethereum:1,sepolia:11155111,hardhat:1337};function T(t){const e=t.toLowerCase().trim();if("sepolia"===e)return"sepolia";if("ethereum"===e||"mainnet"===e)return"ethereum";if("hardhat"===e)return"hardhat";throw new Error(`Unsupported EVM chain: "${t}". Supported chains: ${Object.values(f).join(", ")}`)}function P(t){return b[t]}function v(t){if(!0===t)return!0;if(!1===t)return!1;const e=globalThis;return!0===e.debugAction||(!0===e.LIT_ACTION_DEBUG||("true"===e.LIT_ACTION_DEBUG||function(){try{if("undefined"==typeof process||!process.env)return!1;const t=process.env.LIT_ACTION_DEBUG,e=process.env.DEBUG;return"1"===t||"true"===t||"1"===e||"true"===e}catch{return!1}}()))}function I(...t){v()&&console.log(...t)}var E=class{stepStart(t,e){}stepEnd(t,e){}log(t,e,...r){}warn(t,e){}error(t,e){}getElapsed(){return 0}getRemaining(){return 3e4}summary(){}getExecutionId(){return""}isDebugEnabled(){return!1}},A=class{constructor(t){this.stepStartTimes=new Map,this.stepDurations=new Map,this.TIMEOUT_MS=3e4,this.actionName=t,this.executionStartTime=Date.now();const e=Date.now(),r=Math.random().toString(36).substring(2,9);this.executionId=`exec_${e}_${r}`,this.logHeader()}logHeader(){console.log(`[${this.actionName}] ========================================`),console.log(`[${this.actionName}] Execution started`),console.log(`[${this.actionName}] Execution ID: ${this.executionId}`);void 0!==globalThis.litNodeContext?(console.log(`[${this.actionName}] Context: LIT Node`),console.log(`[${this.actionName}] Node: ${globalThis.litNodeContext?.nodeAddress||"unknown"}`)):console.log(`[${this.actionName}] Context: Browser/Test`),console.log(`[${this.actionName}] ========================================`)}stepStart(t,e){this.stepStartTimes.set(t,Date.now());const r=Date.now()-this.executionStartTime,i=this.TIMEOUT_MS-r;console.log(`[Step ${t}] ${e}...`),console.log(`[Step ${t}] Elapsed: ${r}ms | Remaining: ${i}ms`)}stepEnd(t,e=5e3){const r=this.stepStartTimes.get(t);if(!r)return console.warn(`[Step ${t}] \u26a0\ufe0f stepEnd called without matching stepStart`),void 0;const i=Date.now()-r;this.stepDurations.set(t,i),console.log(`[Step ${t}] Duration: ${i}ms`),i>e&&console.warn(`\u26a0\ufe0f [Step ${t}] Took ${i}ms (> ${e}ms threshold)`);const o=Date.now()-this.executionStartTime,a=this.TIMEOUT_MS-o;a<5e3&&console.warn(`\u26a0\ufe0f [Step ${t}] Time remaining: ${a}ms (approaching timeout)`)}log(t,e,...r){r.length>0?console.log(`[Step ${t}] ${e}`,...r):console.log(`[Step ${t}] ${e}`)}warn(t,e){console.warn(`\u26a0\ufe0f [Step ${t}] ${e}`)}error(t,e){console.error(`\u274c [Step ${t}] ${e}`)}getElapsed(){return Date.now()-this.executionStartTime}getRemaining(){return this.TIMEOUT_MS-this.getElapsed()}summary(){const t=Date.now()-this.executionStartTime,e=this.TIMEOUT_MS-t;if(console.log(`[${this.actionName}] ========================================`),console.log(`[${this.actionName}] Execution Summary`),console.log(`[${this.actionName}] Execution ID: ${this.executionId}`),console.log(`[${this.actionName}] Total time: ${(t/1e3).toFixed(2)}s`),console.log(`[${this.actionName}] Time remaining: ${e}ms`),this.stepDurations.size>0){console.log(`[${this.actionName}] Step breakdown:`);for(const[e,r]of this.stepDurations.entries()){const i=(r/t*100).toFixed(1);console.log(`[${this.actionName}] Step ${e}: ${r}ms (${i}%)`)}}t>2e4&&console.warn(`\u26a0\ufe0f [${this.actionName}] Slow execution: ${(t/1e3).toFixed(2)}s (${e}ms remaining)`),e<5e3&&console.warn(`\u26a0\ufe0f [${this.actionName}] CRITICAL: Only ${e}ms remaining before timeout!`),console.log(`[${this.actionName}] ========================================`)}getExecutionId(){return this.executionId}isDebugEnabled(){return!0}},S=class{static create(t="LIT Action",e){return v(e)?new A(t):new E}};function $(t){if(t instanceof Error)return t.message;if("string"==typeof t)return t;try{return JSON.stringify(t)}catch{return String(t)}}function C(t){return t instanceof Error&&"AbortError"===t.name}var M=class{constructor(t){this.config=t,this.timeout=t.timeout||15e3,this.logger=S.create("BitcoinDataProvider")}async getCurrentBlockHeight(){if(this.logger.stepStart("0","getCurrentBlockHeight"),this.config.rpcHelper){const t=await this.config.rpcHelper.getBlockCount();return this.logger.stepEnd("0"),t}const t=new AbortController,e=setTimeout((()=>t.abort()),this.timeout);try{const r=Date.now(),i=await fetch(`${this.config.providerUrl}/blocks/tip/height`,{signal:t.signal,headers:{Accept:"text/plain","User-Agent":"Mozilla/5.0 (compatible; DiamondHandsValidator/1.0)","ngrok-skip-browser-warning":"true"}});if(clearTimeout(e),I(`[BitcoinDataProvider] blocks/tip/height ${Date.now()-r}ms (timeout budget ${this.timeout}ms)`),!i.ok)throw new Error(`Failed to fetch block height: ${i.status} ${i.statusText}`);const o=await i.text();if(o.trim().startsWith("<!DOCTYPE")||o.trim().startsWith("<html"))throw new Error("Bitcoin RPC endpoint returned HTML instead of block height. URL may be pointing to web interface instead of API endpoint. Expected: plain text number, Got: HTML document. Check that URL ends with /api/esplora (not root /)");const a=parseInt(o.trim(),10);if(isNaN(a)){const t=o.length>200?o.substring(0,200)+"... (truncated)":o;throw new Error(`Invalid block height response: ${t}`)}return this.logger.stepEnd("0"),a}catch(t){if(clearTimeout(e),this.logger.stepEnd("0"),C(t))throw new Error(`Request timeout after ${this.timeout}ms`);throw t}}async getUTXOs(t){this.logger.log("0",`Original address: "${t}"`);const e=this.stripNetworkPrefix(t);if(this.logger.log("0",`Cleaned address: "${e}"`),this.config.rpcHelper)return await this.fetchUTXOsFromRPC(e);try{return await this.fetchUTXOsFromProvider(this.config.providerUrl,e)}catch(t){if(this.logger.warn("0",`Primary provider failed: ${$(t)}`),this.config.fallbackProviders&&this.config.fallbackProviders.length>0){const t=[...this.config.fallbackProviders].sort(((t,e)=>t.priority-e.priority));for(const r of t)try{return this.logger.log("0",`Trying fallback: ${r.name} (${r.url})`),await this.fetchUTXOsFromProvider(r.url,e)}catch(t){this.logger.warn("0",`Fallback ${r.name} failed: ${$(t)}`);continue}}throw new Error(`Failed to fetch UTXOs: ${$(t)}`)}}stripNetworkPrefix(t){return t.replace(/^(REGTEST_|TESTNET_|MAINNET_)/,"")}async fetchUTXOsFromRPC(t){if(!this.config.rpcHelper)throw new Error("RPC helper not configured");const e=this.config.rpcWallet||"";return(await this.config.rpcHelper.listUnspent(e,t)).map((t=>({txid:t.txid,vout:t.vout,satoshis:BigInt(Math.round(1e8*t.amount)),confirmations:t.confirmations})))}parseBlockstreamUTXOs(t,e){if(!Array.isArray(t))throw new Error("Invalid Blockstream response format: expected an array");const r=t,i=[];for(const t of r){let r=0;t.status&&"object"==typeof t.status?t.status.confirmed&&(r=void 0!==t.status.block_height&&t.status.block_height>0?e-t.status.block_height+1:1):void 0!==t.confirmations&&(r=t.confirmations);const o=t.txid,a=void 0!==t.vout?t.vout:t.n,n=void 0!==t.value?t.value:t.satoshis;if(void 0===o||void 0===a||void 0===n)throw new Error("Invalid UTXO row: missing txid, vout/n, or value/satoshis");i.push({txid:o,vout:a,satoshis:BigInt(n),confirmations:r})}return i}async fetchUTXOsFromProvider(t,e){this.logger.stepStart("1","fetchUTXOsFromProvider");const r=`${t}/address/${e}/utxos`;this.logger.log("1",`Fetching UTXOs from ${r}`);const i=new AbortController,o=setTimeout((()=>i.abort()),this.timeout);try{const t=Date.now(),[a,n]=await Promise.all([this.getCurrentBlockHeight(),fetch(r,{signal:i.signal,headers:{Accept:"application/json","Content-Type":"application/json","User-Agent":"Mozilla/5.0 (compatible; DiamondHandsValidator/1.0)","ngrok-skip-browser-warning":"true"}})]);if(clearTimeout(o),I(`[BitcoinDataProvider] parallel(tipHeight + GET utxos) wall ${Date.now()-t}ms endpoint tail .../${e.slice(0,12)}.../utxos (timeout budget ${this.timeout}ms)`),this.logger.log("1",`Current block height: ${a}`),!n.ok)throw new Error(`Bitcoin provider error: ${n.status} ${n.statusText}`);const s=await n.text();if(s.trim().startsWith("<!DOCTYPE")||s.trim().startsWith("<html"))throw new Error("Bitcoin RPC endpoint returned HTML instead of JSON. URL may be pointing to web interface instead of API endpoint. Expected: JSON array of UTXOs, Got: HTML document. Check that URL ends with /api/esplora (not root /)");let c;try{c=JSON.parse(s)}catch(t){const e=s.length>200?s.substring(0,200)+"... (truncated)":s;throw new Error(`Failed to parse JSON response: ${$(t)}. Response: ${e}`)}this.logger.log("1",`Raw API Response: ${JSON.stringify(c,null,2)}`);const l=this.parseBlockstreamUTXOs(c,a);return this.logger.stepEnd("1"),l}catch(t){if(clearTimeout(o),this.logger.stepEnd("1"),C(t))throw new Error(`Request timeout after ${this.timeout}ms`);throw t}}async getUTXOSet(t,e){let r=[],i=null;for(let e=1;e<=3;e++)try{this.logger.log("2",`UTXO query attempt ${e}/3...`),r=await this.getUTXOs(t),this.logger.log("2",`UTXO query succeeded on attempt ${e}`),i=null;break}catch(t){i=t instanceof Error?t:new Error($(t)),this.logger.error("2",`UTXO query failed on attempt ${e}: ${$(t)}`),e<3&&(this.logger.log("2","Waiting 500ms before retry..."),await new Promise((t=>setTimeout(t,500))))}if(i)throw new Error(`Failed to query UTXOs after 3 attempts: ${i.message}`);const o=r.reduce(((t,e)=>t+e.satoshis),0n),a=r.filter((t=>t.confirmations>=e)).reduce(((t,e)=>t+e.satoshis),0n),n=o-a;return{utxos:r,totalBalance:o,totalUTXOs:r.length,confirmedBalance:a,unconfirmedBalance:n}}async getBalance(t){const e=this.stripNetworkPrefix(t),r=`${this.config.providerUrl}/address/${e}`;this.logger.log("3",`Fetching balance from ${r}`);const i=new AbortController,o=setTimeout((()=>i.abort()),this.timeout);try{const t=Date.now(),e=await fetch(r,{signal:i.signal,headers:{Accept:"application/json","Content-Type":"application/json","User-Agent":"Mozilla/5.0 (compatible; DiamondHandsValidator/1.0)","ngrok-skip-browser-warning":"true"}});if(clearTimeout(o),I(`[BitcoinDataProvider] GET address chain_stats ${Date.now()-t}ms (timeout budget ${this.timeout}ms)`),!e.ok)throw new Error(`Bitcoin provider error: ${e.status} ${e.statusText}`);const a=await e.json();if(!a.chain_stats)throw new Error("Invalid response: missing chain_stats");const n=a.chain_stats.funded_txo_sum,s=a.chain_stats.spent_txo_sum;if(void 0===n||void 0===s)throw new Error(`Invalid response: missing required fields. funded_txo_sum: ${n}, spent_txo_sum: ${s}`);const c=BigInt(n-s);return this.logger.log("3",`Balance: ${c.toString()} sats`),this.logger.log("3",` - Funded: ${n} sats, Spent: ${s} sats`),c}catch(t){if(clearTimeout(o),C(t))throw new Error(`Request timeout after ${this.timeout}ms`);const r=$(t);throw this.logger.log("3",`Balance fetch failed: ${r}`),new Error(`Failed to fetch Bitcoin balance for address ${e}: ${r}`)}}async getTransaction(t){if(this.config.rpcHelper)try{const e=this.config.rpcWallet||"",r=await this.config.rpcHelper.getTransaction(e,t);return{txid:r.txid,confirmations:r.confirmations||0}}catch(t){const e=$(t),r=null!==t&&"object"==typeof t&&"code"in t&&"number"==typeof t.code?t.code:void 0;if(e.includes("Invalid or non-wallet transaction")||-5===r)return null;throw t}const e=new AbortController,r=setTimeout((()=>e.abort()),this.timeout);try{const i=Date.now(),o=await fetch(`${this.config.providerUrl}/tx/${t}`,{signal:e.signal});if(clearTimeout(r),I(`[BitcoinDataProvider] GET tx/${t.slice(0,10)}\u2026 ${Date.now()-i}ms (timeout budget ${this.timeout}ms)`),!o.ok){if(404===o.status)return null;throw new Error(`Bitcoin provider error: ${o.status} ${o.statusText}`)}const a=await o.json();return a.status?{txid:a.txid??t,confirmations:a.status.confirmed&&a.status.block_height?1:0}:null}catch(t){if(clearTimeout(r),C(t))throw new Error(`Request timeout after ${this.timeout}ms`);if($(t).includes("404"))return null;throw t}}async getTransactionOutputs(t){if(!this.config.providerUrl)return null;const e=new AbortController,r=setTimeout((()=>e.abort()),this.timeout);try{const i=await fetch(`${this.config.providerUrl}/tx/${t}`,{signal:e.signal});if(clearTimeout(r),!i.ok){if(404===i.status)return null;throw new Error(`Bitcoin provider error: ${i.status} ${i.statusText}`)}const o=await i.json();return o.vout&&Array.isArray(o.vout)?o.vout.map(((t,e)=>({index:e,scriptpubkey:t.scriptpubkey,scriptpubkey_address:t.scriptpubkey_address,value:t.value??0}))):null}catch(t){if(clearTimeout(r),C(t))throw new Error(`Request timeout after ${this.timeout}ms`);throw t}}},k=class t{constructor(e,r,i){if(r&&Array.isArray(r))this.sources=r;else{if(!Array.isArray(e))throw new Error("Price oracle requires a priceProviders array \u2014 defaults and fallbacks have been removed. Pass 3 distinct {name, apiKey} entries from the supported set: "+t.ACTIVE_PROVIDER_NAMES.join(", "));this.sources=this.buildSources(e)}this.sources.sort(((t,e)=>t.priority-e.priority)),this.validateProviderCount()}validateProviderCount(){if(this.sources.length<t.MIN_DISTINCT_PROVIDERS)throw new Error(`Price oracle requires at least ${t.MIN_DISTINCT_PROVIDERS} eligible providers for consensus. Currently configured: ${this.sources.length} provider(s). Supported active providers: ${t.ACTIVE_PROVIDER_NAMES.join(", ")}`)}buildSources(e){if(0===e.length)throw new Error("priceProviders must be a non-empty array of 3 distinct providers, each with an apiKey.");const r=[],i=new Set;let o=1;for(const a of e){if(!a||"string"!=typeof a.name)throw new Error("priceProviders entries must be {name, apiKey, apiSecret?} objects");const e=a.name.toLowerCase();if(!t.ACTIVE_PROVIDER_NAMES.includes(e))throw new Error(`Unsupported price provider "${a.name}". Supported: ${t.ACTIVE_PROVIDER_NAMES.join(", ")}`);if(i.has(e))throw new Error(`Duplicate price provider "${e}" \u2014 the oracle requires 3 DISTINCT providers so a single upstream failure or manipulation cannot compromise consensus. Use 3 different provider names from: ${t.ACTIVE_PROVIDER_NAMES.join(", ")}`);switch(i.add(e),e){case"binance":if(!a.apiKey)throw new Error("Binance provider requires an apiKey");r.push(this.createBinanceSource(a.apiKey,o++));break;case"coinbase":if(!a.apiKey)throw new Error("Coinbase provider requires an apiKey");r.push(this.createCoinbaseSource(a.apiKey,o++));break;case"cryptocompare":if(!a.apiKey)throw new Error("CryptoCompare provider requires an apiKey");r.push(this.createCryptoCompareSource(a.apiKey,o++));break;case"coinmarketcap":if(!a.apiKey)throw new Error("CoinMarketCap provider requires an apiKey");r.push(this.createCoinMarketCapSource(a.apiKey,o++));break;case"coingecko":if(!a.apiKey)throw new Error("CoinGecko provider requires an apiKey (Demo or Pro). Unauthenticated CoinGecko access is not accepted \u2014 the free tier is rate-limited and edge-cached, which breaks median consensus under load.");r.push(this.createCoinGeckoSource(a.apiKey,o++));break;default:throw new Error(`Unhandled provider: ${e}`)}}if(r.length<t.MIN_DISTINCT_PROVIDERS)throw new Error(`Insufficient price providers: need at least ${t.MIN_DISTINCT_PROVIDERS} distinct entries, got ${r.length}.`);return this.selectSampledSources(r)}createCryptoCompareSource(t,e){return{name:"CryptoCompare",fetchPrice:async()=>{const e=new URL("https://min-api.cryptocompare.com/data/price");e.searchParams.set("fsym","BTC"),e.searchParams.set("tsyms","USDT"),e.searchParams.set("api_key",t);const r=await(async t=>{const e=new AbortController,r=setTimeout((()=>e.abort()),5e3);try{const i=await fetch(t,{signal:e.signal,headers:{Accept:"application/json"}});if(clearTimeout(r),!i.ok)throw new Error(`HTTP ${i.status} ${i.statusText}`);return await i.json()}catch(t){if(clearTimeout(r),"AbortError"===t.name)throw new Error("Request timeout after 5000ms");throw t}})(e.toString()),i=Number(r?.USDT);if(!Number.isFinite(i)||i<=0)throw new Error("Invalid CryptoCompare price payload");return i},priority:e}}createFetchJson(){return async(t,e)=>{const r=new AbortController,i=setTimeout((()=>r.abort()),5e3);try{const i=await fetch(t,{headers:{Accept:"application/json",...e||{}},signal:r.signal});if(!i.ok)throw new Error(`HTTP ${i.status} ${i.statusText}`);return await i.json()}finally{clearTimeout(i)}}}getSelectionSeed(){const t="undefined"!=typeof process?process.env.PRICE_PROVIDER_SELECTION_SEED:void 0,e=t?Number(t):NaN;return Number.isFinite(e)?e:("undefined"!=typeof process,0,Math.floor(Date.now()+1e6*Math.random()))}selectSampledSources(t){const e=[...t];let r=this.getSelectionSeed()>>>0;for(let t=e.length-1;t>0;t--){const i=Math.floor((r=1664525*r+1013904223>>>0,r/4294967296*(t+1)));[e[t],e[i]]=[e[i],e[t]]}return e.slice(0,3).map(((t,e)=>({...t,priority:e+1})))}createCoinGeckoSource(t,e){if(!t)throw new Error("createCoinGeckoSource requires an apiKey");const r=this.createFetchJson();return{name:"CoinGecko",fetchPrice:async()=>{const e=t.startsWith("pro:"),i=e?t.slice(4):t,o=e?{"x-cg-pro-api-key":i}:{"x-cg-demo-api-key":i},a=new URL("https://api.coingecko.com/api/v3/simple/price");a.searchParams.set("ids","bitcoin"),a.searchParams.set("vs_currencies","usd");const n=await r(a.toString(),o),s=Number(n?.bitcoin?.usd);if(!Number.isFinite(s)||s<=0)throw new Error("Invalid CoinGecko price payload");return s},priority:e}}createBinanceSource(t,e){if(!t)throw new Error("createBinanceSource requires an apiKey");const r=this.createFetchJson();return{name:"Binance",fetchPrice:async()=>{const e=new URL("https://api.binance.com/api/v3/ticker/price");e.searchParams.set("symbol","BTCUSDT");const i=await r(e.toString(),{"X-MBX-APIKEY":t}),o=Number(i?.price);if(!Number.isFinite(o)||o<=0)throw new Error("Invalid Binance price payload");return o},priority:e}}createCoinbaseSource(t,e){if(!t)throw new Error("createCoinbaseSource requires an apiKey");const r=this.createFetchJson();return{name:"Coinbase",fetchPrice:async()=>{const e=await r("https://api.coinbase.com/v2/prices/BTC-USDT/spot",{"CB-ACCESS-KEY":t}),i=Number(e?.data?.amount);if(!Number.isFinite(i)||i<=0)throw new Error("Invalid Coinbase price payload");return i},priority:e}}createCoinMarketCapSource(t,e){const r=this.createFetchJson();return{name:"CoinMarketCap",fetchPrice:async()=>{const e=new URL("https://pro-api.coinmarketcap.com/v2/cryptocurrency/quotes/latest");e.searchParams.set("symbol","BTC"),e.searchParams.set("convert","USDT");const i=await r(e.toString(),{"X-CMC_PRO_API_KEY":t}),o=Number(i?.data?.BTC?.[0]?.quote?.USDT?.price??i?.data?.BTC?.quote?.USDT?.price);if(!Number.isFinite(o)||o<=0)throw new Error("Invalid CoinMarketCap price payload");return o},priority:e}}async getBTCPrice(){const t=Date.now();if(3!==this.sources.length)throw new Error(`Price oracle requires exactly 3 providers. Currently configured: ${this.sources.length}`);const e=new Promise(((t,e)=>{setTimeout((()=>{e(new Error("Price oracle global timeout after 8000ms"))}),8e3)})),r=Promise.all(this.sources.map((async t=>{const e=Date.now(),r=await t.fetchPrice();return console.log(`[Price Oracle] \u2705 [+${Date.now()-e}ms] ${t.name}: $${r.toLocaleString()}`),r}))),i=[...await Promise.race([r,e])].sort(((t,e)=>t-e)),o=i[1];if((i[2]-i[0])/i[0]>.01){const t=this.sources.map((t=>t.name)).join(", "),e=i.map((t=>`$${t.toLocaleString()}`)).join(", ");throw new Error(`Price consensus failed: prices not within 1% tolerance. Providers: [${t}]. Prices: [${e}]`)}const a=Math.round(100*o),n=1000000n*BigInt(a),s=Date.now()-t;return console.log(`[Price Oracle] Median price: $${o.toLocaleString()} \u2014 total time: ${s}ms`),console.log(`[Price Oracle] Price with 8 decimals: ${n}`),n}async getBTCPriceConsensus(){console.log("[Price Oracle] Fetching BTC price with consensus...");const t=(await Promise.allSettled(this.sources.map((async t=>{const e=await t.fetchPrice();return{source:t.name,price:e}})))).filter((t=>"fulfilled"===t.status)).map((t=>t.value));if(0===t.length)throw new Error("No price sources returned data");console.log(`[Price Oracle] Got prices from ${t.length}/${this.sources.length} sources:`),t.forEach((t=>{console.log(` ${t.source}: $${t.price.toLocaleString()}`)}));const e=t.map((t=>t.price));e.sort(((t,e)=>t-e));const r=e[Math.floor(e.length/2)],i=e[0],o=e[e.length-1]/i,a=t.filter((t=>Math.abs(t.price-r)/r<=.02));if(o>1.05&&a.length===t.length)throw new Error(`Price consensus failed: sources too dispersed (${(100*(o-1)).toFixed(1)}% spread)`);if(a.length<t.length){const e=t.filter((t=>!a.find((e=>e.source===t.source))));if(console.log(`[Price Oracle] \u26a0\ufe0f Detected ${e.length} outlier(s):`),e.forEach((t=>{const e=Math.abs(t.price-r)/r;console.log(` ${t.source}: $${t.price.toLocaleString()} (${(100*e).toFixed(1)}% deviation)`)})),a.length<2)throw new Error("Price consensus failed: insufficient valid sources after outlier removal");console.log(`[Price Oracle] \u2705 Outliers filtered, continuing with ${a.length} valid sources`);const i=a.map((t=>t.price));i.sort(((t,e)=>t-e));const o=i[Math.floor(i.length/2)];console.log(`[Price Oracle] \u2705 Consensus price (median): $${o.toLocaleString()}`);const n=Math.round(100*o);return 1000000n*BigInt(n)}console.log(`[Price Oracle] \u2705 Consensus price (median): $${r.toLocaleString()}`);const n=Math.round(100*r);return 1000000n*BigInt(n)}};k.ACTIVE_PROVIDER_NAMES=["cryptocompare","coinbase","binance","coinmarketcap","coingecko"],k.MIN_DISTINCT_PROVIDERS=3;var B=k;function x(t){return Math.floor(t/l)*l}function U(){return x(Math.floor(Date.now()/1e3))}async function D(){const t=Math.floor(Date.now()/1e3),e=t%l;if(!function(t){const e=(t??Math.floor(Date.now()/1e3))%l;return e<8||e>=l-8}(t))return;let r;r=e>=l-8?l-e+8:8-e,console.log(`[Quantum] Waiting ${r}s for safe moment (current time in quantum: ${e}s)`),await new Promise((t=>setTimeout(t,1e3*r))),console.log(`[Quantum] Safe quantum ready: ${U()}`)}var O=class t{static verifyAmountPositionActionAuthorizationStructure(t){if(!t||"object"!=typeof t)throw new Error('Missing or invalid "auth" parameter - must be an object');if(!t.positionId)throw new Error("auth.positionId is required");if("number"!=typeof t.chainId)throw new Error("auth.chainId must be a number");if("number"!=typeof t.timestamp)throw new Error("auth.timestamp must be a number");if(void 0===t.amount||null===t.amount)throw new Error("auth.amount is required");if(!t.action)throw new Error("auth.action is required");if(!t.signature)throw new Error("auth.signature is required");return t}static async recoverSigner(t,e){try{const r=ethers.utils.arrayify(t),i=ethers.utils.hashMessage(r);return ethers.utils.recoverAddress(i,e)}catch(t){throw new Error(`Failed to recover signer: ${t instanceof Error?t.message:String(t)}`)}}static async checkEIP1271(t,e,r,i){try{const o=new ethers.Contract(t,["function isValidSignature(bytes32,bytes) view returns (bytes4)"],i);return"0x1626ba7e"===await o.isValidSignature(e,r)}catch{return!1}}static async verifyActionAuthorization(t,e,r,i){const o=Math.floor(Date.now()/1e3);await D();const a=i?.strictCurrentQuantum?o:void 0;try{!function(t,e,r){const i=e??Math.floor(Date.now()/1e3),o=x(i),a=x(t);if(r?.strictCurrentQuantum){if(a!==o)throw new Error(`[Quantum] Signature must come from current quantum (strict mode). Signature quantum: ${a}, current quantum: ${o}`);return}const n=o-l,s=o+l;if(a!==n&&a!==o&&a!==s)throw new Error(`[Quantum] Signature outside 3-quantum window. Signature quantum: ${a}, Valid range: [${n} (past), ${o} (current), ${s} (next)]`);if(a===n){if(i<n+8)throw new Error(`[Quantum] Dead zone violation: Current time (${i}) is in first 8 seconds of PAST quantum (${n}). This is unsafe due to boundary instability.`)}else if(a===s&&i>=s+(l-8))throw new Error(`[Quantum] Dead zone violation: Current time (${i}) is in last 8 seconds of NEXT quantum (${s}). This is unsafe due to boundary instability.`)}(t.timestamp,a,{strictCurrentQuantum:!0===i?.strictCurrentQuantum})}catch{return!1}const n=e(),s=await this.recoverSigner(n,t.signature);return await r(s,n,t.signature)}static async verifyAmountPositionActionAuthorization(e,r,i){return this.verifyActionAuthorization(e,(()=>{const t=ethers.utils.keccak256(ethers.utils.toUtf8Bytes(e.action)),r="string"==typeof e.amount?BigInt(e.amount):e.amount,i=[e.positionId,e.timestamp,e.chainId,r.toString(),t];return ethers.utils.solidityKeccak256(["bytes32","uint256","uint256","uint256","bytes32"],i)}),(async(e,o,a)=>e.toLowerCase()===r.toLowerCase()||!!i&&t.checkEIP1271(r,o,a,i)))}static async verifyMintAuthorization(t,e,r){return"mint-ucd"===t.action&&this.verifyAmountPositionActionAuthorization(t,e,r)}static async verifyWithdrawAuthorization(e,r,i){if("withdraw-btc"!==e.action)return!1;return this.verifyActionAuthorization(e,(()=>{const t=ethers.utils.keccak256(ethers.utils.toUtf8Bytes(e.action)),r="string"==typeof e.amount?BigInt(e.amount):e.amount,i=[e.positionId,e.timestamp,e.chainId,r.toString(),e.destinationAddress,t];return ethers.utils.solidityKeccak256(["bytes32","uint256","uint256","uint256","string","bytes32"],i)}),(async(e,o,a)=>e.toLowerCase()===r.toLowerCase()||!!i&&t.checkEIP1271(r,o,a,i)))}static verifyTermPositionActionAuthorizationStructure(t){if(!t||"object"!=typeof t)throw new Error('Missing or invalid "auth" parameter - must be an object');if(!t.positionId)throw new Error("auth.positionId is required");if("number"!=typeof t.chainId)throw new Error("auth.chainId must be a number");if("number"!=typeof t.timestamp)throw new Error("auth.timestamp must be a number");if(void 0===t.newTerm||null===t.newTerm)throw new Error("auth.newTerm is required");if("number"!=typeof t.newTerm)throw new Error("auth.newTerm must be a number");if(!t.action)throw new Error("auth.action is required");if(!t.signature)throw new Error("auth.signature is required");return t}static async verifyTermPositionActionAuthorization(e,r,i){return this.verifyActionAuthorization(e,(()=>{const t=ethers.utils.keccak256(ethers.utils.toUtf8Bytes(e.action)),r=[e.positionId,e.timestamp,e.chainId,e.newTerm,t];return ethers.utils.solidityKeccak256(["bytes32","uint256","uint256","uint256","bytes32"],r)}),(async(e,o,a)=>e.toLowerCase()===r.toLowerCase()||!!i&&t.checkEIP1271(r,o,a,i)))}static async verifyExtendAuthorization(t,e,r){return"extend-position"===t.action&&this.verifyTermPositionActionAuthorization(t,e,r)}static async verifyBtcExecutionAuthorization(e,r,i){if("execute-btc-withdrawal"!==e.action)return!1;return this.verifyActionAuthorization(e,(()=>{const t=ethers.utils.keccak256(ethers.utils.toUtf8Bytes(e.action)),r=[e.positionId,e.timestamp,e.chainId,e.utxoIdentifier,e.networkFee,t];return ethers.utils.solidityKeccak256(["bytes32","uint256","uint256","string","uint256","bytes32"],r)}),(async(e,o,a)=>e.toLowerCase()===r.toLowerCase()||!!i&&t.checkEIP1271(r,o,a,i)),{strictCurrentQuantum:!0})}};function N(t,e){if(0===t)return{termDurationDays:0,termLengthDays:30*e,isExpired:!1,daysUntilExpiry:30*e,daysIntoGracePeriod:0};const r=30*e,i=Math.floor(Date.now()/1e3)-t,o=Math.floor(i/86400);return{termDurationDays:o,termLengthDays:r,isExpired:o>r,daysUntilExpiry:Math.max(0,r-o),daysIntoGracePeriod:Math.max(0,o-r)}}function R(t,e,r,i){if(!t)return r??13e3;const o=i??a,n=Math.min(e,30);return a+n*n*(o-a)/900}function q(t,e,r){const i=t*e/10000000000000000n;if(0n===r)return{collateralValueUsd:i,collateralRatioBps:Number.MAX_SAFE_INTEGER};return{collateralValueUsd:i,collateralRatioBps:Number(i*n*10000n/r)}}var L=class{constructor(t){this.termManagerAddress=t.termManagerAddress,this.loanOpsManagerAddress=t.loanOpsManagerAddress,this.liquidationManagerAddress=t.liquidationManagerAddress,this.chain=t.chain,this.chainId=t.chainId,this.rpcUrl=t.rpcUrl}async getLiquidationThreshold(){try{let t;t=this.rpcUrl?this.rpcUrl:await Lit.Actions.getRpcUrl({chain:this.chain});const e=new ethers.providers.StaticJsonRpcProvider({url:t,timeout:d},{name:"any",chainId:this.chainId}),r=[{inputs:[],name:"liquidationThreshold",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function"}],i=new ethers.Contract(this.loanOpsManagerAddress,r,e),o=await i.liquidationThreshold();return Number(o.toString())}catch(t){throw console.error("[ProtocolParameters] Error fetching liquidation threshold:",t.message),new Error(`Failed to fetch liquidation threshold: ${t.message}`)}}async getTermFees(t){try{let e;e=this.rpcUrl?this.rpcUrl:await Lit.Actions.getRpcUrl({chain:this.chain});const r=new ethers.providers.StaticJsonRpcProvider({url:e,timeout:d},{name:"any",chainId:this.chainId}),i=[{inputs:[{internalType:"uint256",name:"_termMonths",type:"uint256"}],name:"getTermFees",outputs:[{internalType:"uint88",name:"originationFee",type:"uint88"},{internalType:"uint88",name:"extensionFee",type:"uint88"}],stateMutability:"view",type:"function"}],o=new ethers.Contract(this.termManagerAddress,i,r),a=await o.getTermFees(t);return{originationFeeBps:Number(a.originationFee?.toString?.()??a[0]?.toString?.()??a[0]),extensionFeeBps:Number(a.extensionFee?.toString?.()??a[1]?.toString?.()??a[1])}}catch(t){throw console.error("[ProtocolParameters] Error fetching term fees:",t.message),new Error(`Failed to fetch term fees: ${t.message}`)}}async getAuthorizedSpendsHash(t){try{const e=this.rpcUrl||await Lit.Actions.getRpcUrl({chain:this.chain}),r=new ethers.providers.StaticJsonRpcProvider({url:e,timeout:d},{name:"any",chainId:this.chainId}),i=t.startsWith("0x")?t:`0x${t.padStart(64,"0")}`,o=new ethers.Contract(this.loanOpsManagerAddress,[{inputs:[{name:"positionId",type:"bytes32"}],name:"getAuthorizedSpendsHash",outputs:[{name:"",type:"bytes32"}],stateMutability:"view",type:"function"}],r);return await o.getAuthorizedSpendsHash(i)}catch(t){throw console.error("[ProtocolParameters] Error fetching authorized spends hash:",t.message),new Error(`Failed to fetch authorized spends hash: ${t.message}`)}}async getMaxEscalatedThreshold(){try{let t;t=this.rpcUrl?this.rpcUrl:await Lit.Actions.getRpcUrl({chain:this.chain});const e=new ethers.providers.StaticJsonRpcProvider({url:t,timeout:d},{name:"any",chainId:this.chainId}),r=[{inputs:[],name:"maxEscalatedThreshold",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function"}],i=new ethers.Contract(this.liquidationManagerAddress,r,e),o=await i.maxEscalatedThreshold();return Number(o.toString())}catch(t){throw console.error("[ProtocolParameters] Error fetching max escalated threshold:",t.message),new Error(`Failed to fetch max escalated threshold: ${t.message}`)}}};function _(t){return new L(t)}var F=class{constructor(t){this.config=t}async getVaultSnapshot(t){const e=await this.queryPositionState(t);console.log(`[Vault Snapshot] Raw vault address from contract: "${e.vaultAddress}"`);const r=!0===this.config.debugOverrides?.useStubbedBtcData,[i,o]=r?this.buildStubbedBtcSnapshotInputs(e):await Promise.all([this.config.vaultBalance.calculateTrustedBalance(t,e.vaultAddress),this.config.priceOracle.getBTCPriceConsensus()]),a=_({termManagerAddress:this.config.termManagerAddress,loanOpsManagerAddress:this.config.loanOpsManagerAddress,liquidationManagerAddress:this.config.liquidationManagerAddress,chain:this.config.chain,chainId:this.config.chainId||1,rpcUrl:this.config.rpcUrl}),n=this.config.termOverride??e.selectedTerm,[s,c,l]=await Promise.all([a.getLiquidationThreshold(),a.getMaxEscalatedThreshold(),a.getTermFees(n)]),u=N(e.termStartTimestamp,e.selectedTerm),d=R(u.isExpired,u.daysIntoGracePeriod,s,c),h=q(i.availableBalance,o,e.ucdDebt),p=h.collateralRatioBps<d,g=h.collateralRatioBps-d;return{positionId:e.positionId,pkpId:e.pkpId,borrower:e.borrower,vaultAddress:e.vaultAddress,ucdDebt:e.ucdDebt,termStartTimestamp:e.termStartTimestamp,selectedTerm:e.selectedTerm,status:e.status,expiryAt:e.expiryAt,previousExpiryAt:e.previousExpiryAt,totalTerm:e.totalTerm,totalBTCSats:i.totalBalance,totalUTXOs:i.totalUTXOs,authorizedSpendsSats:i.authorizedBalance,authorizedSpendsHash:i.authorizedSpendsHash,availableBTCSats:i.availableBalance,availableUTXOs:i.availableUTXOs,btcPriceUsd:o,collateralValueUsd:h.collateralValueUsd,collateralRatioBps:h.collateralRatioBps,termDurationDays:u.termDurationDays,termLengthDays:u.termLengthDays,isExpired:u.isExpired,daysUntilExpiry:u.daysUntilExpiry,daysIntoGracePeriod:u.daysIntoGracePeriod,currentLiquidationThreshold:d,isLiquidatable:p,marginToLiquidationBps:g,liquidationThresholdBps:s,originationFeeBps:l.originationFeeBps,extensionFeeBps:l.extensionFeeBps,timestamp:Date.now()}}async getVaultSnapshotFast(t){const e=await this.queryPositionState(t);console.log(`[Vault Snapshot] Raw vault address from contract: "${e.vaultAddress}"`);const r=!0===this.config.debugOverrides?.useStubbedBtcData,[i,o]=r?this.buildStubbedBtcSnapshotInputs(e):await Promise.all([this.config.vaultBalance.calculateTrustedBalance(t,e.vaultAddress,!0),this.config.priceOracle.getBTCPriceConsensus()]),a=_({termManagerAddress:this.config.termManagerAddress,loanOpsManagerAddress:this.config.loanOpsManagerAddress,liquidationManagerAddress:this.config.liquidationManagerAddress,chain:this.config.chain,chainId:this.config.chainId||1,rpcUrl:this.config.rpcUrl}),n=this.config.termOverride??e.selectedTerm,[s,c,l]=await Promise.all([a.getLiquidationThreshold(),a.getMaxEscalatedThreshold(),a.getTermFees(n)]),u=N(e.termStartTimestamp,e.selectedTerm),d=R(u.isExpired,u.daysIntoGracePeriod,s,c),h=q(i.availableBalance,o,e.ucdDebt),p=h.collateralRatioBps<d,g=h.collateralRatioBps-d;return{positionId:e.positionId,pkpId:e.pkpId,borrower:e.borrower,vaultAddress:e.vaultAddress,ucdDebt:e.ucdDebt,termStartTimestamp:e.termStartTimestamp,selectedTerm:e.selectedTerm,status:e.status,expiryAt:e.expiryAt,previousExpiryAt:e.previousExpiryAt,totalTerm:e.totalTerm,totalBTCSats:i.totalBalance,totalUTXOs:i.totalUTXOs,authorizedSpendsSats:i.authorizedBalance,authorizedSpendsHash:i.authorizedSpendsHash,availableBTCSats:i.availableBalance,availableUTXOs:i.availableUTXOs,btcPriceUsd:o,collateralValueUsd:h.collateralValueUsd,collateralRatioBps:h.collateralRatioBps,termDurationDays:u.termDurationDays,termLengthDays:u.termLengthDays,isExpired:u.isExpired,daysUntilExpiry:u.daysUntilExpiry,daysIntoGracePeriod:u.daysIntoGracePeriod,currentLiquidationThreshold:d,isLiquidatable:p,marginToLiquidationBps:g,liquidationThresholdBps:s,originationFeeBps:l.originationFeeBps,extensionFeeBps:l.extensionFeeBps,timestamp:Date.now()}}buildStubbedBtcSnapshotInputs(t){const e=BigInt(this.config.debugOverrides?.stubbedAvailableBtcSats??"10000000"),r=BigInt(this.config.debugOverrides?.stubbedBtcPriceUsd??"5000000000000");return[{totalUTXOs:[],totalBalance:e,authorizedUTXOs:[],authorizedBalance:0n,authorizedSpendsHash:this.config.debugOverrides?.stubbedAuthorizedSpendsHash??ethers.utils.keccak256(ethers.utils.defaultAbiCoder.encode(["bytes32","bytes32[]"],[t.positionId,[]])),availableUTXOs:[],availableBalance:e,vaultAddress:t.vaultAddress,positionId:t.positionId,timestamp:Date.now()},r]}async queryPositionState(t){const e=t.startsWith("0x")?t:`0x${t.padStart(64,"0")}`;let r;r=this.config.rpcUrl?this.config.rpcUrl:await Lit.Actions.getRpcUrl({chain:this.config.chain});const i=new ethers.providers.StaticJsonRpcProvider({url:r,timeout:d},this.config.chainId),o=new ethers.Contract(this.config.contractAddress,[{inputs:[],name:"core",outputs:[{internalType:"address",name:"",type:"address"}],stateMutability:"view",type:"function"}],i),a=await o.core(),n=new ethers.Contract(a,[{inputs:[{internalType:"bytes32",name:"positionId",type:"bytes32"}],name:"getPositionDetails",outputs:[{components:[{internalType:"bytes32",name:"positionId",type:"bytes32"},{internalType:"bytes32",name:"pkpId",type:"bytes32"},{internalType:"uint256",name:"ucdDebt",type:"uint256"},{internalType:"string",name:"vaultAddress",type:"string"},{internalType:"address",name:"borrower",type:"address"},{internalType:"uint40",name:"createdAt",type:"uint40"},{internalType:"uint40",name:"lastUpdated",type:"uint40"},{internalType:"uint16",name:"selectedTerm",type:"uint16"},{internalType:"uint40",name:"expiryAt",type:"uint40"},{internalType:"enum LoanStatusLib.LoanStatus",name:"status",type:"uint8"},{internalType:"uint40",name:"previousExpiryAt",type:"uint40"},{internalType:"uint16",name:"totalTerm",type:"uint16"}],internalType:"struct IPositionManagerCore.Position",name:"",type:"tuple"}],stateMutability:"view",type:"function"}],i),s=await n.getPositionDetails(e),c=Number(s.status);if(c<0||c>7)throw console.error("[VaultSnapshot] INVALID STATUS - ABI decoding error detected"),console.error(" Position ID:",e),console.error(" Contract:",this.config.contractAddress),console.error(" Status decoded:",c,"(expected 0-7)"),console.error(" Full position:",JSON.stringify(s,null,2)),new Error(`Invalid position status decoded from contract: ${c} (expected 0-7). This indicates an ABI decoding issue. Position: ${e}`);const l=Number(s.expiryAt),u=Number(s.selectedTerm),h=Number(s.totalTerm),p=Number(s.previousExpiryAt),g=(f=u,0===(m=l)?0:m-30*f*86400);var m,f;return{positionId:s.positionId,pkpId:s.pkpId,borrower:s.borrower,vaultAddress:this.parseVaultAddress(s.vaultAddress,T(this.config.chain)),ucdDebt:BigInt(s.ucdDebt.toString()),termStartTimestamp:g,selectedTerm:u,status:c,expiryAt:l,previousExpiryAt:p,totalTerm:h}}parseVaultAddress(t,e){try{const r=JSON.parse(t);if("object"==typeof r&&null!==r){if("sepolia"===e.toLowerCase())return r.regtest||r.testnet||r.mainnet;const t=function(t){return y[t]}(e);return"testnet"===t&&(r.testnet||r.regtest)||r.mainnet}}catch(t){}return t}async isLiquidatable(t){return(await this.getVaultSnapshot(t)).isLiquidatable}async getCollateralRatio(t){return(await this.getVaultSnapshot(t)).collateralRatioBps}async getAvailableBalance(t){return(await this.getVaultSnapshot(t)).availableBTCSats}async hasSufficientCollateral(t,e,r){const i=await this.getVaultSnapshot(t);return function(t,e,r,i){const o=e+r;return 0n===o||Number(t*n*10000n/o)>=i}(i.collateralValueUsd,i.ucdDebt,e,r)}};var z=class{constructor(t){this.config=t,this.bitcoinProvider=t.bitcoinProvider}async calculateBalance(t,e){const r=await this.bitcoinProvider.getBalance(e),i=await this.getAuthorizedSpendsFromContract(t),o=i.reduce(((t,e)=>t+e.satoshis),0n),a=r>o?r-o:0n;return{totalUTXOs:[],totalBalance:r,authorizedUTXOs:i,authorizedBalance:o,authorizedSpendsHash:this.computeAuthorizedSpendsHash(t,i),availableUTXOs:[],availableBalance:a,vaultAddress:e,positionId:t,timestamp:Date.now()}}async calculateTrustedBalance(t,e,r){if(r)return await this.calculateBalance(t,e);const i=this.config.minConfirmations||6,o=await this.bitcoinProvider.getUTXOSet(e,i),a=await this.getAuthorizedSpendsFromContract(t);for(const t of a){if(o.utxos.some((e=>e.txid===t.txid&&e.vout===t.vout)))throw new Error(`Authorized UTXO ${t.txid}:${t.vout} not yet spent. Transaction was authorized in smart contract but not signed and broadcasted to Bitcoin network. Complete the transaction by signing and broadcasting it.`)}const n=a.reduce(((t,e)=>t+e.satoshis),0n),s=o.utxos.filter((t=>!this.isUTXOAuthorized(t,a))),c=s.reduce(((t,e)=>t+e.satoshis),0n),l=this.computeAuthorizedSpendsHash(t,a);return{totalUTXOs:o.utxos,totalBalance:o.totalBalance,authorizedUTXOs:a,authorizedBalance:n,authorizedSpendsHash:l,availableUTXOs:s,availableBalance:c,vaultAddress:e,positionId:t,timestamp:Date.now()}}async getTrustedBalance(t,e){return(await this.calculateTrustedBalance(t,e)).availableBalance}async getAvailableUTXOs(t,e){return(await this.calculateTrustedBalance(t,e)).availableUTXOs}async isUTXOAvailable(t,e,r,i){return(await this.getAvailableUTXOs(t,e)).some((t=>t.txid===r&&t.vout===i))}isUTXOAuthorized(t,e){return e.some((e=>e.txid===t.txid&&e.vout===t.vout))}async getAuthorizedSpendsFromContract(t){const e=t.startsWith("0x")?t:`0x${t.padStart(64,"0")}`;let r;r=this.config.rpcUrl?this.config.rpcUrl:await Lit.Actions.getRpcUrl({chain:this.config.chain});const i=new ethers.providers.StaticJsonRpcProvider({url:r,timeout:d},this.config.chainId),o=new ethers.Contract(this.config.contractAddress,[{inputs:[{internalType:"bytes32",name:"positionId",type:"bytes32"}],name:"getAuthorizedSpends",outputs:[{components:[{internalType:"string",name:"txid",type:"string"},{internalType:"uint32",name:"vout",type:"uint32"},{internalType:"uint256",name:"satoshis",type:"uint256"},{internalType:"string",name:"targetAddress",type:"string"},{internalType:"uint256",name:"targetAmount",type:"uint256"},{internalType:"uint256",name:"authorizedAt",type:"uint256"}],internalType:"struct LoanOperationsManager.AuthorizedSpend[]",name:"",type:"tuple[]"}],stateMutability:"view",type:"function"}],i);return(await o.getAuthorizedSpends(e)).map((e=>({txid:e.txid,vout:Number(e.vout),satoshis:BigInt(e.satoshis.toString()),positionId:t,targetAddress:e.targetAddress,targetAmount:BigInt(e.targetAmount.toString()),timestamp:Number(e.authorizedAt)})))}computeAuthorizedSpendsHash(t,e){const r=t.startsWith("0x")?t:`0x${t.padStart(64,"0")}`;if(0===e.length)return ethers.utils.keccak256(ethers.utils.defaultAbiCoder.encode(["bytes32","bytes32[]"],[r,[]]));const i=e.map((t=>{const e=ethers.utils.keccak256(ethers.utils.toUtf8Bytes(t.txid)),r=ethers.utils.keccak256(ethers.utils.toUtf8Bytes(t.targetAddress));return ethers.utils.keccak256(ethers.utils.defaultAbiCoder.encode(["bytes32","uint32","uint256","bytes32","uint256","uint256"],[e,t.vout,t.satoshis.toString(),r,t.targetAmount.toString(),t.timestamp]))}));return ethers.utils.keccak256(ethers.utils.defaultAbiCoder.encode(["bytes32","bytes32[]"],[r,i]))}};var V="function getProtocolPauseStatus() view returns (tuple(bool positionManagerPaused, bool positionManagerLiquidationsPaused, bool positionManagerCorePaused, bool loanOperationsPaused, bool collateralManagerPaused, bool liquidationManagerPaused, bool circuitBreakerPaused, bool ucdControllerPaused, bool ucdTokenPaused, bool simplePsmPaused))",X={mintUcd:["positionManagerPaused","positionManagerCorePaused","loanOperationsPaused","circuitBreakerPaused","ucdControllerPaused","ucdTokenPaused","simplePsmPaused"],withdrawBtc:["positionManagerPaused","positionManagerCorePaused","loanOperationsPaused","collateralManagerPaused","circuitBreakerPaused","ucdControllerPaused","ucdTokenPaused","simplePsmPaused"],processPayment:["positionManagerPaused","positionManagerCorePaused","loanOperationsPaused","circuitBreakerPaused","ucdControllerPaused","ucdTokenPaused"],extendPosition:["positionManagerPaused","positionManagerCorePaused","loanOperationsPaused","circuitBreakerPaused","ucdControllerPaused","ucdTokenPaused","simplePsmPaused"],liquidation:["positionManagerLiquidationsPaused","liquidationManagerPaused","circuitBreakerPaused"],adminLiquidation:["positionManagerLiquidationsPaused","liquidationManagerPaused","circuitBreakerPaused"],btcTransactionSign:["positionManagerPaused","positionManagerCorePaused","loanOperationsPaused","collateralManagerPaused","circuitBreakerPaused"]};function H(t){return{positionManagerPaused:t.positionManagerPaused,positionManagerLiquidationsPaused:t.positionManagerLiquidationsPaused,positionManagerCorePaused:t.positionManagerCorePaused,loanOperationsPaused:t.loanOperationsPaused,collateralManagerPaused:t.collateralManagerPaused,liquidationManagerPaused:t.liquidationManagerPaused,circuitBreakerPaused:t.circuitBreakerPaused,ucdControllerPaused:t.ucdControllerPaused,ucdTokenPaused:t.ucdTokenPaused,simplePsmPaused:t.simplePsmPaused}}var K=new ethers.utils.Interface(["function views() view returns (address)","function paused() view returns (bool)","function liquidationPaused() view returns (bool)","function simplePsm() view returns (address)"]),j=new ethers.utils.Interface(["function core() view returns (address)","function loanOps() view returns (address)","function collateralManager() view returns (address)","function liquidationManager() view returns (address)","function circuitBreaker() view returns (address)"]),G=new ethers.utils.Interface(["function getUcdController() view returns (address)","function ucdToken() view returns (address)"]),J=new ethers.utils.Interface(["function paused() view returns (bool)"]);async function W(t,e,r){return t.call({to:e,data:r})}async function Q(t,e){const r=new ethers.utils.Interface([V]).encodeFunctionData("getProtocolPauseStatus",[]);let i;try{return function(t){const e=new ethers.utils.Interface([V]).decodeFunctionResult("getProtocolPauseStatus",t)[0];return Array.isArray(e)?H({positionManagerPaused:e[0],positionManagerLiquidationsPaused:e[1],positionManagerCorePaused:e[2],loanOperationsPaused:e[3],collateralManagerPaused:e[4],liquidationManagerPaused:e[5],circuitBreakerPaused:e[6],ucdControllerPaused:e[7],ucdTokenPaused:e[8],simplePsmPaused:e[9]}):H(e)}(await t.call({to:e,data:r}))}catch(t){i=t}try{return await async function(t,e){const r=await W(t,e,K.encodeFunctionData("views",[])),[i]=K.decodeFunctionResult("views",r),[o,a,n,s,c,l,u,d]=await Promise.all([W(t,e,K.encodeFunctionData("paused",[])),W(t,e,K.encodeFunctionData("liquidationPaused",[])),W(t,e,K.encodeFunctionData("simplePsm",[])),W(t,i,j.encodeFunctionData("core",[])),W(t,i,j.encodeFunctionData("loanOps",[])),W(t,i,j.encodeFunctionData("collateralManager",[])),W(t,i,j.encodeFunctionData("liquidationManager",[])),W(t,i,j.encodeFunctionData("circuitBreaker",[]))]),h=K.decodeFunctionResult("paused",o)[0],p=K.decodeFunctionResult("liquidationPaused",a)[0],g=K.decodeFunctionResult("simplePsm",n)[0],m=j.decodeFunctionResult("core",s)[0],f=j.decodeFunctionResult("loanOps",c)[0],y=j.decodeFunctionResult("collateralManager",l)[0],w=j.decodeFunctionResult("liquidationManager",u)[0],b=j.decodeFunctionResult("circuitBreaker",d)[0],T=await W(t,f,G.encodeFunctionData("getUcdController",[])),P=await W(t,f,G.encodeFunctionData("ucdToken",[])),v=G.decodeFunctionResult("getUcdController",T)[0],I=G.decodeFunctionResult("ucdToken",P)[0],E=async e=>{if(!e||e===ethers.constants.AddressZero)return!1;try{const r=await W(t,e,J.encodeFunctionData("paused",[]));return J.decodeFunctionResult("paused",r)[0]}catch{return!1}},[A,S,$,C,M,k,B,x]=await Promise.all([E(m),E(f),E(y),E(w),E(b),E(v),E(I),E(g)]);return H({positionManagerPaused:h,positionManagerLiquidationsPaused:p,positionManagerCorePaused:A,loanOperationsPaused:S,collateralManagerPaused:$,liquidationManagerPaused:C,circuitBreakerPaused:M,ucdControllerPaused:k,ucdTokenPaused:B,simplePsmPaused:x})}(t,e)}catch(t){const e=i instanceof Error?i.message:String(i),r=t instanceof Error?t.message:String(t);throw new Error(`getProtocolPauseStatus failed (aggregate: ${e}; decomposed: ${r})`)}}function Y(t){if("string"!=typeof t)throw new Error("positionId must be a string");const e=t.trim(),r=/^0x/i.test(e)?e.slice(2):e;if(0===r.length)throw new Error("positionId is empty");if(!/^[0-9a-fA-F]+$/.test(r))throw new Error("positionId must be hexadecimal (bytes32)");if(r.length>64)throw new Error("positionId exceeds 32 bytes");return"0x"+r.padStart(64,"0").toLowerCase()}var Z={hardhat:{chain:"hardhat",chainId:b.hardhat,bitcoinNetwork:y.hardhat,allowArbitraryBitcoinProvider:!0,allowArbitraryEvmRpc:!0,minBitcoinConfirmations:1},sepolia:{chain:"sepolia",chainId:b.sepolia,bitcoinNetwork:y.sepolia,allowArbitraryBitcoinProvider:!1,allowArbitraryEvmRpc:!1,minBitcoinConfirmations:1},ethereum:{chain:"ethereum",chainId:b.ethereum,bitcoinNetwork:y.ethereum,allowArbitraryBitcoinProvider:!1,allowArbitraryEvmRpc:!1,minBitcoinConfirmations:6}};var tt,et=[{inputs:[{internalType:"uint256",name:"chainId",type:"uint256"}],name:"getProviders",outputs:[{internalType:"bytes32[]",name:"ids",type:"bytes32[]"},{components:[{internalType:"bytes",name:"ciphertext",type:"bytes"},{internalType:"bytes32",name:"pkpId",type:"bytes32"},{internalType:"uint64",name:"addedAt",type:"uint64"}],internalType:"struct BitcoinProviderRegistry.ProviderEntry[]",name:"entries",type:"tuple[]"}],stateMutability:"view",type:"function"}];async function rt(t,e){const r=66===e.length?"0x"+e.slice(-40):e;try{const e=await Lit.Actions.Decrypt({pkpId:r,ciphertext:t}),i="string"==typeof e?e:String(e??"");if(!i)throw new Error("Decrypt returned empty plaintext");return i}catch(t){throw new Error(`Failed to decrypt Bitcoin provider URL (pkpId=${e}): ${t?.message??String(t)}`)}}async function it(t){const{policy:e,bitcoinProviderUrl:r,ethersProvider:i,registryAddress:o}=t;if(!r||"string"!=typeof r)throw new Error("resolveBitcoinProviderForPolicy: bitcoinProviderUrl is required");if(e.allowArbitraryBitcoinProvider)return{name:"Custom Provider",url:r,minConfirmations:e.minBitcoinConfirmations,network:e.bitcoinNetwork};if(!o)throw new Error(`Bitcoin provider registry address is required on chain "${e.chain}" \u2014 set contractAddresses.BitcoinProviderRegistry. Hardcoded allowlists have been removed (see audit C-1/H-6 fix).`);const a=await async function(t,e,r){if(!r||"string"!=typeof r)throw new Error("fetchBitcoinProviderEntries: registryAddress (BitcoinProviderRegistry) is required");if(!e)throw new Error("fetchBitcoinProviderEntries: ethers provider is required");const i=new ethers.Contract(r,et,e),[o,a]=await i.getProviders(t),n=[];for(let t=0;t<o.length;t++)n.push({providerId:o[t],ciphertext:a[t].ciphertext,pkpId:a[t].pkpId,addedAt:Number(a[t].addedAt)});return n}(e.chainId,i,o);if(0===a.length)throw new Error(`No Bitcoin providers registered for chainId=${e.chainId} in ${o}. Admin must addProvider(...) via AdminModule before LIT Actions can sign.`);const n=await async function(t,e){if(!e)throw new Error("matchAllowedProviderUrl: candidateUrl is required");for(const r of t)if(await rt(r.ciphertext,r.pkpId)===e)return r.providerId;return null}(a,r);if(!n)throw new Error(`Bitcoin provider URL not in any registered encrypted slot for chainId=${e.chainId}`);return{name:`Registry Provider ${n.slice(0,10)}`,url:r,minConfirmations:e.minBitcoinConfirmations,network:e.bitcoinNetwork}}async function main(t){for(const[e,r]of Object.entries(t))globalThis[e]=r;const e=S.create("Process Payment Validator");e.log("0","========================================");e.log("0","Payment Validator v1.0.2 (deployed: 2026-01-23T23:45:00Z)"),e.log("0","DEBUG: Received globalThis parameters:"),e.log("0","globalThis.contractAddresses:",JSON.stringify(globalThis.contractAddresses)),e.log("0","globalThis.positionId:",globalThis.positionId),e.log("0","globalThis.paymentAmount:",globalThis.paymentAmount),e.log("0","globalThis.priceProviders:",JSON.stringify(globalThis.priceProviders)),e.log("0","globalThis.priceProviders type:",typeof globalThis.priceProviders),e.log("0","globalThis.priceProviders isArray:",Array.isArray(globalThis.priceProviders)),e.log("0","All globalThis keys:",Object.keys(globalThis).filter((t=>!t.startsWith("_")&&"console"!==t&&"require"!==t&&"module"!==t&&"exports"!==t&&"process"!==t&&"Buffer"!==t&&"setTimeout"!==t&&"clearTimeout"!==t&&"setInterval"!==t&&"clearInterval"!==t))),e.log("0","========================================");let r,i,o,a,n,l,d="0";try{d="0a",e.log("0a","Validating configuration...");const t=globalThis.chain,y=globalThis.bitcoinProviderUrl,v=globalThis.positionId,E=globalThis.paymentAmount;if(!t)throw new Error('Missing required parameter: "chain"');if(!y)throw new Error('Missing required parameter: "bitcoinProviderUrl"');if(!v)throw new Error('Missing required parameter: "positionId"');if(!E)throw new Error('Missing required parameter: "paymentAmount"');const A=BigInt(E);if(A<=0n)throw new Error(`Payment amount must be positive, got: ${A.toString()} wei`);d="0b";const S=function(t){const e=T(t),r=Z[e];if(!r)throw new Error(`No chain policy registered for "${t}"`);return r}(t),$=S.chain,C=S.bitcoinNetwork;let k;I(` \u2705 Chain: ${t} (${$})`),I(` \u2705 Bitcoin Network: ${C}`),e.log("0",` \u2705 Payment Amount: ${A.toString()} wei`),d="0c",e.log("0c","Loading contract addresses...");const x=globalThis.contractAddresses;if(!x||"object"!=typeof x)throw new Error("contractAddresses is required (PositionManager, LoanOperationsManagerModule, LiquidationManager...).");if(i=x.PositionManager,o=x.LoanOperationsManagerModule,a=x.LiquidationManagerModule??x.LiquidationManager??"",n=x.BTCSpendAuthorizer||o,!i||!o||!a)throw new Error("Missing required contract addresses in contractAddresses object");e.log("0"," \u2705 Contract addresses loaded from parameters"),d="0d",e.log("0d","Initializing modules...");const D=globalThis.rpcUrl,N=P($);if(D||"hardhat"===t||"sepolia"===t){const i="hardhat"===t?{chainId:b.hardhat,name:"hardhat"}:"sepolia"===t?{chainId:b.sepolia,name:"sepolia"}:{chainId:N,name:t};e.log("0",` Using StaticJsonRpcProvider for ${t} to skip network detection`),e.log("0"," Network config:",i),r=new ethers.providers.StaticJsonRpcProvider(D,i)}else r=new ethers.providers.JsonRpcProvider(D);!function(t,e){const r=X[t],i=[];for(const t of r)e[t]&&i.push(t);if(i.length>0)throw new Error(JSON.stringify({code:"PROTOCOL_PAUSED",operation:t,paused:i,status:e}))}("processPayment",await Q(r,i)),e.log("0","[DEBUG] Before PriceOracleModule init:"),e.log("0"," globalThis.priceProviders:",JSON.stringify(globalThis.priceProviders)),e.log("0"," typeof globalThis.priceProviders:",typeof globalThis.priceProviders),e.log("0"," Array.isArray(globalThis.priceProviders):",Array.isArray(globalThis.priceProviders));const R=new B(globalThis.priceProviders,void 0);D&&"string"==typeof D&&!function(t,e){if(t.allowArbitraryEvmRpc)return;if(!e||"string"!=typeof e)throw new Error("validateEvmRpcForPolicy: rpcUrl must be a non-empty string");let r;try{r=new URL(e).hostname}catch(t){throw new Error(`Invalid rpcUrl: ${t?.message??String(t)}`)}if(!w.some((t=>r===t||r.endsWith(`.${t}`))))throw new Error(`Custom RPC domain "${r}" is not in the approved list: ${w.join(", ")}`)}(S,D),k=await it({policy:S,bitcoinProviderUrl:y,ethersProvider:r,registryAddress:globalThis.contractAddresses?.BitcoinProviderRegistry}),I(` \u2705 Bitcoin Provider: ${k.name} (min confs ${k.minConfirmations})`),e.log("0","[DEBUG] After PriceOracleModule init:"),e.log("0"," PriceOracleModule sources count:",R.sources?.length||0),e.log("0"," PriceOracleModule first source:",R.sources?.[0]?.name||"none");const q=new M({providerUrl:k.url}),L=(f={contractAddress:n,chain:t,chainId:N,rpcUrl:D,bitcoinProvider:q,minConfirmations:k.minConfirmations},new z(f));if(!globalThis.authMessage||!globalThis.userSignature)throw new Error("Missing authorization parameters: authMessage and userSignature are required");const _=Y(v);if(_!==Y(globalThis.authMessage.positionId))throw new Error("positionId must match authMessage.positionId (canonical bytes32: 0x + 64 hex, case-insensitive, short forms padded)");l=_,d="1",e.log("1","Getting vault snapshot...");const V=function(t){return new F(t)}({contractAddress:i,termManagerAddress:globalThis.contractAddresses?.TermManagerModule||i,loanOpsManagerAddress:o,liquidationManagerAddress:a,chain:t,chainId:N,rpcUrl:D,vaultBalance:L,priceOracle:R}),H=await V.getVaultSnapshot(l),K=H.btcPriceUsd;if(K<s||K>c)throw new Error(`Bitcoin price ${K} is outside acceptable range (${s} - ${c}). This may indicate oracle manipulation or stale price data. Please try again later.`);e.log("0",` \u2705 BTC price validation passed: $${(Number(K)/1e8).toFixed(2)}`),e.log("0"," \u2705 Vault snapshot retrieved"),e.log("0",` - Position status: ${h(H.status)}`),e.log("0",` - Total debt: ${H.ucdDebt.toString()} wei`),d="1.5",e.log("1.5","Authenticating caller authorization...");if(!await O.verifyAmountPositionActionAuthorization({positionId:globalThis.authMessage.positionId,timestamp:globalThis.authMessage.timestamp,chainId:globalThis.authMessage.chainId,amount:globalThis.authMessage.paymentAmount,action:globalThis.authMessage.action,signature:globalThis.userSignature},H.borrower,r))throw new Error("Caller not authorized to make payment on this position");e.log("0"," \u2705 Caller authorization validated"),d="2",e.log("2","Validating position state...");const j=h(H.status);if(!["PENDING_MINT","ACTIVE","EXPIRED","LIQUIDATABLE"].includes(j))throw new Error(`Invalid position status for payment: ${j}. Must be PENDING_MINT, ACTIVE, EXPIRED, or LIQUIDATABLE.`);e.log("0",` \u2705 Position status valid for payment: ${j}`),d="3",e.log("3","Validating payment amount...");const G=BigInt(H.ucdDebt.toString());if(A>G)throw new Error(`Payment amount ${A.toString()} wei exceeds total debt ${G.toString()} wei. Payment amount must be less than or equal to total debt.`);e.log("0",` \u2705 Payment amount valid: ${A.toString()} wei`),e.log("0",` - Total debt: ${G.toString()} wei`),d="3.25",e.log("0","[Step 3.25] Circuit breaker check..."),e.log("0"," \u26a0\ufe0f Circuit breaker validation deferred to contract (state-changing function)"),e.log("0"," \u2705 Payment amount within bounds for circuit breaker check"),d="3.5",e.log("0","[Step 3.5] UCD validation skipped (pre-validated by SDK)"),d="4",e.log("4","Collateral ratio validation skipped for repayment (handled off-chain / by business rules)"),d="5",e.log("5","Building authorization message...");const J=globalThis.quantumTimestamp,W=J?Number(J):U()+u;e.log("5",`Using quantum timestamp: ${W} ${J?"(from authorization)":"(generated)"}`);const tt=P($),et={name:g,version:m,chainId:tt,verifyingContract:ethers.utils.getAddress(o)},rt=ethers.utils.keccak256(ethers.utils.toUtf8Bytes(p)),ot={PartialPayment:[{name:"actionTag",type:"bytes32"},{name:"positionId",type:"bytes32"},{name:"quantumTimestamp",type:"uint256"},{name:"btcPrice",type:"uint256"},{name:"paymentAmount",type:"uint256"},{name:"borrower",type:"address"}]},at=ethers.utils._TypedDataEncoder.hash(et,ot,{actionTag:rt,positionId:l,quantumTimestamp:W,btcPrice:K.toString(),paymentAmount:A.toString(),borrower:ethers.utils.getAddress(H.borrower)}),nt=globalThis.publicKey;if(!nt)throw new Error('Missing required parameter: "publicKey"');if("string"!=typeof nt||0===nt.length)throw new Error("Invalid publicKey: expected non-empty string, got "+typeof nt);const st=nt;e.log("0",`[Step 6] PublicKey length: ${st.length}, starts with: ${st.substring(0,10)}...`),d="6",e.log("6","Signing authorization..."),e.log("0",` Message hash: ${at}`),e.log("0",` Message hash bytes length: ${ethers.utils.arrayify(at).length}`),e.log("0",` Public key: ${st.substring(0,20)}...`),e.log("0",` Public key length: ${st.length}`),e.log("0","======= PKP AUTHORIZATION DIAGNOSTICS ======="),e.log("0","globalThis.publicKey:",st?`${st.substring(0,20)}...`:"MISSING"),e.log("0","globalThis.publicKey length:",st?.length||0),e.log("0","globalThis.publicKey has 0x prefix:",st?.startsWith("0x")||!1);const ct=globalThis.pkpTokenId;e.log("0","globalThis.pkpTokenId:",ct||"NOT SET"),ct&&(e.log("0","globalThis.pkpTokenId type:",typeof ct),e.log("0","globalThis.pkpTokenId value:",ct),e.log("0","NOTE: pkpTokenId is used for session signature scoping in LitOps, not as a signEcdsa parameter."));const lt=globalThis.positionId;e.log("0","globalThis.positionId:",lt||"NOT SET");const ut=globalThis.ipfsId;e.log("0","globalThis.ipfsId:",ut||"NOT SET");try{const t=ethers.utils.arrayify("0x"+st),r=ethers.utils.computeAddress(t);e.log("0","Computed ETH Address from publicKey:",r)}catch(t){e.log("0","Could not compute ETH address from publicKey:",t instanceof Error?t.message:String(t))}e.log("0","Message hash to sign:",at),e.log("0","Signature name: paymentAuth"),e.log("0","=============================================");const dt=globalThis.pkpTokenId;let ht;try{"pkpTokenId"in globalThis&&delete globalThis.pkpTokenId;const t=globalThis.validatorWalletAddress;if(!t||!t.startsWith("0x"))throw new Error("validatorWalletAddress required \u2014 must be a createWallet-derived address");const e=await Lit.Actions.getPrivateKey({pkpId:t}),r="string"==typeof e?e.startsWith("0x")?e:`0x${e}`:String(e),i=new ethers.Wallet(r);ht=await i._signTypedData(et,ot,{actionTag:rt,positionId:l,quantumTimestamp:W,btcPrice:K.toString(),paymentAmount:A.toString(),borrower:ethers.utils.getAddress(H.borrower)})}finally{void 0!==dt&&(globalThis.pkpTokenId=dt)}return e.log("0"," \u2705 Signature obtained: "+(ht?"present":"missing")),e.log("0","\u2705 Complete"),{approved:!0,positionId:l,paymentAmount:A.toString(),btcPrice:K.toString(),messageHash:at,signature:ht,timestamp:W,validatorPkp:st}}catch(t){return console.error("[Process Payment Validator] Failed:",t.message),{approved:!1,reason:t.message||t.toString(),positionId:globalThis.positionId,timestamp:Date.now()}}var f}return tt=o,((o,a,n,s)=>{if(a&&"object"==typeof a||"function"==typeof a)for(let c of r(a))i.call(o,c)||c===n||t(o,c,{get:()=>a[c],enumerable:!(s=e(a,c))||s.enumerable});return o})(t({},"__esModule",{value:!0}),tt)})();if("object"==typeof _LIT_ACTION_)if("function"==typeof _LIT_ACTION_.main)var main=_LIT_ACTION_.main;else"function"==typeof _LIT_ACTION_.go&&_LIT_ACTION_.go();
@@ -1 +1 @@
1
- c77a28d80a8122c73b634229b9024e3974eedf696e5db29db7b40dc9a53f9165
1
+ 2a3aa2ebb2fb379f3f4331457848d82ab3ef5429d4bc483132cf1c04480c9ffc
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "actionName": "process-payment-validator",
3
- "originalSize": 145551,
4
- "minifiedSize": 61412,
5
- "compressionRatio": 0.5780722908121552,
6
- "hash": "c77a28d80a8122c73b634229b9024e3974eedf696e5db29db7b40dc9a53f9165",
7
- "buildTime": 1777647246010,
3
+ "originalSize": 145646,
4
+ "minifiedSize": 61414,
5
+ "compressionRatio": 0.5783337681776362,
6
+ "hash": "2a3aa2ebb2fb379f3f4331457848d82ab3ef5429d4bc483132cf1c04480c9ffc",
7
+ "buildTime": 1778141708106,
8
8
  "version": "0.1.0"
9
9
  }
@@ -4,6 +4,6 @@
4
4
  "minifiedSize": 65506,
5
5
  "compressionRatio": 0.5764926458703734,
6
6
  "hash": "a42096083c705332dda82d5e669e5e1e258c1d9da0203593f6502a14d61f1fb7",
7
- "buildTime": 1777647246499,
7
+ "buildTime": 1778141708222,
8
8
  "version": "0.1.0"
9
9
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gvnrdao/dh-lit-actions",
3
- "version": "0.0.286",
3
+ "version": "0.0.288",
4
4
  "type": "commonjs",
5
5
  "description": "Diamond Hands Protocol LIT Actions - Deterministic, Auditable Builds",
6
6
  "main": "pkg-dist/pkg-src/index.js",
@@ -703,17 +703,17 @@ var DH_LIT_ACTIONS_CHIPOTLE = {
703
703
  }
704
704
  },
705
705
  processPaymentValidator: {
706
- cid: "QmWb6MxjnfCe2KboAEF83za8u528hLeuoAqE6JCTKcAqMZ",
706
+ cid: "QmR8949WAtUM9kYRMwesgLXmZjTwFBmogo921MNFNKXa5Z",
707
707
  authorizedCidHex: cidToHex(
708
- "QmWb6MxjnfCe2KboAEF83za8u528hLeuoAqE6JCTKcAqMZ"
708
+ "QmR8949WAtUM9kYRMwesgLXmZjTwFBmogo921MNFNKXa5Z"
709
709
  ),
710
710
  name: "Process Payment Validator",
711
711
  description: "Production Process Payment Validator",
712
712
  version: "1.0.0",
713
713
  deployed: true,
714
- deployedAt: 1777493900466,
715
- size: 61412,
716
- hash: "c77a28d80a8122c73b634229b9024e3974eedf696e5db29db7b40dc9a53f9165",
714
+ deployedAt: 1778105262871,
715
+ size: 61414,
716
+ hash: "2a3aa2ebb2fb379f3f4331457848d82ab3ef5429d4bc483132cf1c04480c9ffc",
717
717
  validatorWalletAddress: "0xc2ed5a59bd81eb4d1a19a0147b86ab69aa4c28ba",
718
718
  pkp: {
719
719
  publicKey: "0x04cfccb9b6c7addc79469c0e9fafebef4c70801302c62cdb26b610723a9b8527d23f98a40158c7340d83fd0e62d02b5ec74c12f7a86d98e8c6973b4811110f19ad",
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../pkg-src/utils/chunks/debug-logger.ts", "../../pkg-src/utils/chunks/session-signature-cache.ts", "../../pkg-src/index.ts", "../../node_modules/base-x/src/esm/index.js", "../../node_modules/bs58/src/esm/index.js", "../../pkg-src/utils/chunks/cid-utils.ts", "../../pkg-src/constants/chunks/lit-actions-registry.ts", "../../pkg-src/utils/chunks/lit-action-helpers.ts", "../../pkg-src/utils/chunks/pkp-setup.ts", "../../pkg-src/utils/chunks/connection-helpers.ts", "../../pkg-src/utils/chunks/error-classification.ts", "../../pkg-src/utils/index.ts"],
4
- "sourcesContent": ["/**\n * Enhanced Debug Logger for LIT Protocol Operations\n * Provides detailed logging for debugging network and protocol issues\n */\n\nexport enum LogLevel {\n ERROR = 0,\n WARN = 1,\n INFO = 2,\n DEBUG = 3,\n TRACE = 4\n}\n\nexport interface DebugConfig {\n level: LogLevel;\n enableTimestamps: boolean;\n enablePerformanceMetrics: boolean;\n enableRequestLogging: boolean;\n enableNetworkMetrics: boolean;\n}\n\nexport const DEFAULT_DEBUG_CONFIG: DebugConfig = {\n level: LogLevel.INFO,\n enableTimestamps: true,\n enablePerformanceMetrics: true,\n enableRequestLogging: true,\n enableNetworkMetrics: true\n};\n\nlet debugConfig: DebugConfig = { ...DEFAULT_DEBUG_CONFIG };\n\n/**\n * Configure debug logging\n */\nexport function configureDebugLogging(config: Partial<DebugConfig>): void {\n debugConfig = { ...debugConfig, ...config };\n}\n\n/**\n * Get current timestamp string\n */\nfunction getTimestamp(): string {\n return new Date().toISOString();\n}\n\n/**\n * Format log message with metadata\n */\nfunction formatMessage(level: string, component: string, message: string, metadata?: any): string {\n const timestamp = debugConfig.enableTimestamps ? `[${getTimestamp()}] ` : '';\n const metadataStr = metadata ? ` ${JSON.stringify(metadata)}` : '';\n return `${timestamp}[${level}] [${component}] ${message}${metadataStr}`;\n}\n\n/**\n * Performance metrics tracker\n */\nexport class PerformanceTracker {\n private startTime: number;\n private markers: Map<string, number> = new Map();\n\n constructor(private operationName: string) {\n this.startTime = Date.now();\n this.log('PERF', `Started: ${operationName}`);\n }\n\n mark(label: string): void {\n const elapsed = Date.now() - this.startTime;\n this.markers.set(label, elapsed);\n this.log('PERF', `${this.operationName} - ${label}: ${elapsed}ms`);\n }\n\n end(success: boolean = true): number {\n const totalTime = Date.now() - this.startTime;\n const status = success ? 'SUCCESS' : 'FAILED';\n this.log('PERF', `${this.operationName} - ${status}: ${totalTime}ms`);\n\n if (debugConfig.enablePerformanceMetrics && this.markers.size > 0) {\n console.log(`\uD83D\uDCCA Performance Breakdown for ${this.operationName}:`);\n for (const [label, time] of this.markers) {\n console.log(` ${label}: ${time}ms`);\n }\n console.log(` Total: ${totalTime}ms`);\n }\n\n return totalTime;\n }\n\n private log(level: string, message: string): void {\n if (debugConfig.enablePerformanceMetrics) {\n console.log(formatMessage(level, 'PERFORMANCE', message));\n }\n }\n}\n\n/**\n * Network metrics collector\n */\nexport interface NetworkMetrics {\n requestCount: number;\n successCount: number;\n failureCount: number;\n averageLatency: number;\n lastError?: string;\n startTime: number;\n}\n\nclass NetworkMetricsCollector {\n private metrics: Map<string, NetworkMetrics> = new Map();\n\n getMetrics(operation: string): NetworkMetrics {\n if (!this.metrics.has(operation)) {\n this.metrics.set(operation, {\n requestCount: 0,\n successCount: 0,\n failureCount: 0,\n averageLatency: 0,\n startTime: Date.now()\n });\n }\n return this.metrics.get(operation)!;\n }\n\n recordRequest(operation: string, success: boolean, latency: number, error?: string): void {\n const metrics = this.getMetrics(operation);\n metrics.requestCount++;\n\n if (success) {\n metrics.successCount++;\n // Update average latency\n metrics.averageLatency = (metrics.averageLatency * (metrics.successCount - 1) + latency) / metrics.successCount;\n } else {\n metrics.failureCount++;\n if (error) metrics.lastError = error;\n }\n\n if (debugConfig.enableNetworkMetrics) {\n const successRate = ((metrics.successCount / metrics.requestCount) * 100).toFixed(1);\n debugLog('NETWORK', `${operation} - Success: ${success}, Latency: ${latency}ms, Success Rate: ${successRate}%`);\n }\n }\n\n printSummary(): void {\n if (!debugConfig.enableNetworkMetrics) return;\n\n console.log('\\n\uD83D\uDCC8 Network Metrics Summary:');\n for (const [operation, metrics] of this.metrics) {\n const successRate = ((metrics.successCount / metrics.requestCount) * 100).toFixed(1);\n const runtime = Date.now() - metrics.startTime;\n console.log(` ${operation}:`);\n console.log(` Requests: ${metrics.requestCount}`);\n console.log(` Success Rate: ${successRate}%`);\n console.log(` Avg Latency: ${metrics.averageLatency.toFixed(0)}ms`);\n console.log(` Runtime: ${runtime}ms`);\n if (metrics.lastError) {\n console.log(` Last Error: ${metrics.lastError}`);\n }\n }\n }\n}\n\nexport const networkMetrics = new NetworkMetricsCollector();\n\n/**\n * Debug logging functions\n */\nexport function debugError(component: string, message: string, metadata?: any): void {\n if (debugConfig.level >= LogLevel.ERROR) {\n console.error(formatMessage('ERROR', component, message, metadata));\n }\n}\n\nexport function debugWarn(component: string, message: string, metadata?: any): void {\n if (debugConfig.level >= LogLevel.WARN) {\n console.warn(formatMessage('WARN', component, message, metadata));\n }\n}\n\nexport function debugInfo(component: string, message: string, metadata?: any): void {\n if (debugConfig.level >= LogLevel.INFO) {\n console.log(formatMessage('INFO', component, message, metadata));\n }\n}\n\nexport function debugLog(component: string, message: string, metadata?: any): void {\n if (debugConfig.level >= LogLevel.DEBUG) {\n console.log(formatMessage('DEBUG', component, message, metadata));\n }\n}\n\nexport function debugTrace(component: string, message: string, metadata?: any): void {\n if (debugConfig.level >= LogLevel.TRACE) {\n console.log(formatMessage('TRACE', component, message, metadata));\n }\n}\n\n/**\n * Request/Response logger for LIT Protocol operations\n */\nexport function logRequest(component: string, operation: string, params: any): void {\n if (!debugConfig.enableRequestLogging) return;\n\n debugLog(component, `\uD83D\uDD04 Request: ${operation}`, {\n operation,\n params: JSON.stringify(params, null, 2)\n });\n}\n\nexport function logResponse(component: string, operation: string, response: any, duration: number): void {\n if (!debugConfig.enableRequestLogging) return;\n\n debugLog(component, `\u2705 Response: ${operation} (${duration}ms)`, {\n operation,\n duration,\n response: typeof response === 'object' ? JSON.stringify(response, null, 2) : response\n });\n}\n\nexport function logError(component: string, operation: string, error: any, duration?: number): void {\n const durationStr = duration ? ` (${duration}ms)` : '';\n debugError(component, `\u274C Error: ${operation}${durationStr}`, {\n operation,\n duration,\n error: error?.message || error?.toString() || 'Unknown error',\n stack: error?.stack\n });\n}\n\n/**\n * Network connection logger\n */\nexport function logConnectionAttempt(network: string, attempt: number, maxAttempts: number): void {\n debugInfo('CONNECTION', `\uD83D\uDD17 Connecting to ${network} (attempt ${attempt}/${maxAttempts})`);\n}\n\nexport function logConnectionSuccess(network: string, latency: number): void {\n debugInfo('CONNECTION', `\u2705 Connected to ${network} (${latency}ms)`);\n networkMetrics.recordRequest(`connection-${network}`, true, latency);\n}\n\nexport function logConnectionFailure(network: string, error: string, attempt: number): void {\n debugError('CONNECTION', `\u274C Connection failed to ${network} (attempt ${attempt})`, { error });\n networkMetrics.recordRequest(`connection-${network}`, false, 0, error);\n}\n\n/**\n * PKP operation logger\n */\nexport function logPkpOperation(operation: string, pkpId?: string, details?: any): void {\n const pkpStr = pkpId ? ` (PKP: ${pkpId.slice(0, 8)}...)` : '';\n debugInfo('PKP', `\uD83D\uDD11 ${operation}${pkpStr}`, details);\n}\n\n/**\n * Session signature logger\n */\nexport function logSessionSignatures(count: number, expiration: string): void {\n debugInfo('SESSION', `\uD83D\uDD10 Generated ${count} session signatures (expires: ${expiration})`);\n}\n\n/**\n * LIT Action execution logger\n */\nexport function logLitActionExecution(cid: string, params: any): void {\n debugInfo('LIT_ACTION', `\u26A1 Executing LIT Action: ${cid}`, {\n cid,\n params\n });\n}\n\nexport function logLitActionResult(cid: string, success: boolean, duration: number, result?: any): void {\n const status = success ? '\u2705' : '\u274C';\n debugInfo('LIT_ACTION', `${status} LIT Action completed: ${cid} (${duration}ms)`, {\n cid,\n success,\n duration,\n result: success && result ? JSON.stringify(result, null, 2) : undefined\n });\n}\n\n/**\n * Test lifecycle logger\n */\nexport function logTestStart(testName: string): PerformanceTracker {\n debugInfo('TEST', `\uD83E\uDDEA Starting test: ${testName}`);\n return new PerformanceTracker(testName);\n}\n\nexport function logTestEnd(testName: string, success: boolean, duration: number): void {\n const status = success ? '\u2705' : '\u274C';\n debugInfo('TEST', `${status} Test completed: ${testName} (${duration}ms)`);\n networkMetrics.printSummary();\n}", "/**\n * Session Signature Cache for PKP Operations\n * Provides intelligent caching of session signatures to avoid regeneration\n */\n\nimport { debugInfo, debugWarn, debugLog } from './debug-logger';\n\n/**\n * Cache entry for session signatures\n */\ninterface SessionSignatureCacheEntry {\n sessionSigs: any;\n expiration: Date;\n pkpTokenId: string;\n litActionCid: string;\n signerAddress: string;\n network: string;\n createdAt: Date;\n}\n\n/**\n * Session signature cache configuration\n */\nexport interface SessionSignatureCacheConfig {\n maxEntries: number;\n bufferTimeMs: number; // Time before expiration to consider entry stale\n enableLogging: boolean;\n}\n\nexport const DEFAULT_CACHE_CONFIG: SessionSignatureCacheConfig = {\n maxEntries: 50,\n bufferTimeMs: 60000, // 1 minute buffer before expiration\n enableLogging: true\n};\n\n/**\n * In-memory session signature cache\n */\nclass SessionSignatureCache {\n private cache = new Map<string, SessionSignatureCacheEntry>();\n private config: SessionSignatureCacheConfig;\n\n constructor(config: Partial<SessionSignatureCacheConfig> = {}) {\n this.config = { ...DEFAULT_CACHE_CONFIG, ...config };\n }\n\n /**\n * Generate cache key from PKP and context\n */\n private generateCacheKey(\n pkpTokenId: string,\n litActionCid: string,\n signerAddress: string,\n network: string\n ): string {\n return `${signerAddress}-${network}-${pkpTokenId}-${litActionCid}`;\n }\n\n /**\n * Check if cache entry is still valid\n */\n private isEntryValid(entry: SessionSignatureCacheEntry): boolean {\n const now = new Date();\n const expirationWithBuffer = new Date(\n entry.expiration.getTime() - this.config.bufferTimeMs\n );\n\n return now < expirationWithBuffer;\n }\n\n /**\n * Clean expired entries from cache\n */\n private cleanExpiredEntries(): void {\n const now = new Date();\n let cleanedCount = 0;\n\n for (const [key, entry] of this.cache.entries()) {\n if (now >= entry.expiration) {\n this.cache.delete(key);\n cleanedCount++;\n }\n }\n\n if (cleanedCount > 0 && this.config.enableLogging) {\n debugInfo('CACHE', `\uD83E\uDDF9 Cleaned ${cleanedCount} expired session signature entries`);\n }\n }\n\n /**\n * Enforce cache size limits\n */\n private enforceCacheSize(): void {\n if (this.cache.size <= this.config.maxEntries) {\n return;\n }\n\n // Sort entries by creation time and remove oldest\n const entries = Array.from(this.cache.entries())\n .sort(([, a], [, b]) => a.createdAt.getTime() - b.createdAt.getTime());\n\n const toRemove = entries.slice(0, this.cache.size - this.config.maxEntries);\n\n for (const [key] of toRemove) {\n this.cache.delete(key);\n }\n\n if (this.config.enableLogging) {\n debugInfo('CACHE', `\uD83D\uDCE6 Removed ${toRemove.length} oldest cache entries to maintain size limit`);\n }\n }\n\n /**\n * Get cached session signatures if valid\n */\n get(\n pkpTokenId: string,\n litActionCid: string,\n signerAddress: string,\n network: string\n ): any | null {\n this.cleanExpiredEntries();\n\n const cacheKey = this.generateCacheKey(pkpTokenId, litActionCid, signerAddress, network);\n const entry = this.cache.get(cacheKey);\n\n if (!entry) {\n if (this.config.enableLogging) {\n debugLog('CACHE', `\u274C Session signatures cache miss: ${cacheKey}`);\n }\n return null;\n }\n\n if (!this.isEntryValid(entry)) {\n this.cache.delete(cacheKey);\n if (this.config.enableLogging) {\n debugWarn('CACHE', `\u23F0 Session signatures expired: ${cacheKey}`);\n }\n return null;\n }\n\n if (this.config.enableLogging) {\n const timeUntilExpiry = entry.expiration.getTime() - Date.now();\n debugInfo('CACHE', `\u2705 Session signatures cache hit: ${cacheKey} (expires in ${Math.round(timeUntilExpiry/1000)}s)`);\n }\n\n return entry.sessionSigs;\n }\n\n /**\n * Cache session signatures\n */\n set(\n pkpTokenId: string,\n litActionCid: string,\n signerAddress: string,\n network: string,\n sessionSigs: any,\n expiration: Date\n ): void {\n const cacheKey = this.generateCacheKey(pkpTokenId, litActionCid, signerAddress, network);\n\n const entry: SessionSignatureCacheEntry = {\n sessionSigs,\n expiration,\n pkpTokenId,\n litActionCid,\n signerAddress,\n network,\n createdAt: new Date()\n };\n\n this.cache.set(cacheKey, entry);\n\n if (this.config.enableLogging) {\n const timeUntilExpiry = expiration.getTime() - Date.now();\n debugInfo('CACHE', `\uD83D\uDCBE Cached session signatures: ${cacheKey} (expires in ${Math.round(timeUntilExpiry/1000)}s)`);\n }\n\n this.enforceCacheSize();\n }\n\n /**\n * Clear all cached session signatures\n */\n clear(): void {\n const size = this.cache.size;\n this.cache.clear();\n\n if (this.config.enableLogging) {\n debugInfo('CACHE', `\uD83D\uDDD1\uFE0F Cleared ${size} cached session signature entries`);\n }\n }\n\n /**\n * Get cache statistics\n */\n getStats() {\n this.cleanExpiredEntries();\n\n return {\n size: this.cache.size,\n maxEntries: this.config.maxEntries,\n utilizationPercent: Math.round((this.cache.size / this.config.maxEntries) * 100)\n };\n }\n\n /**\n * Remove specific cache entry\n */\n delete(\n pkpTokenId: string,\n litActionCid: string,\n signerAddress: string,\n network: string\n ): boolean {\n const cacheKey = this.generateCacheKey(pkpTokenId, litActionCid, signerAddress, network);\n const deleted = this.cache.delete(cacheKey);\n\n if (deleted && this.config.enableLogging) {\n debugInfo('CACHE', `\uD83D\uDDD1\uFE0F Removed session signatures from cache: ${cacheKey}`);\n }\n\n return deleted;\n }\n}\n\n// Global session signature cache instance\nexport const sessionSignatureCache = new SessionSignatureCache();\n\n/**\n * Enhanced session signature generator with caching\n */\nexport async function generateSessionSignaturesWithCache(\n litNodeClient: any,\n sessionConfig: any,\n pkpTokenId: string,\n litActionCid: string,\n signerAddress: string,\n network: string = 'chipotle'\n): Promise<any> {\n // Check cache first\n const cached = sessionSignatureCache.get(pkpTokenId, litActionCid, signerAddress, network);\n if (cached) {\n return cached;\n }\n\n // Generate new session signatures\n debugInfo('CACHE', '\uD83D\uDD10 Generating new session signatures (cache miss)...');\n const sessionSigs = await litNodeClient.getSessionSigs(sessionConfig);\n\n // Extract expiration from config\n const expiration = new Date(sessionConfig.expiration);\n\n // Cache the result\n sessionSignatureCache.set(pkpTokenId, litActionCid, signerAddress, network, sessionSigs, expiration);\n\n const sigCount = Object.keys(sessionSigs).length;\n debugInfo('CACHE', `\u2705 Generated and cached ${sigCount} session signatures`);\n\n return sessionSigs;\n}\n\n/**\n * Configure the global session signature cache\n */\nexport function configureSessionSignatureCache(config: Partial<SessionSignatureCacheConfig>): void {\n Object.assign(sessionSignatureCache['config'], config);\n}\n\n/**\n * Clear the global session signature cache\n */\nexport function clearSessionSignatureCache(): void {\n sessionSignatureCache.clear();\n}\n\n/**\n * Get session signature cache statistics\n */\nexport function getSessionSignatureCacheStats() {\n return sessionSignatureCache.getStats();\n}", "/**\n * dh-lit-actions Package\n * Diamond Hands Protocol LIT Actions Registry\n *\n * This package exports deployed LIT Action CIDs and metadata\n * for consumption by the Diamond Hands SDK and other applications.\n */\n\n// Export interfaces\nexport * from \"./interfaces\";\n\n// Export constants from registry\nexport * from \"./constants/chunks/lit-actions-registry\";\n\n// Export utilities\nexport * from \"./utils\";\n\n// Export NOLA executors (commented out - executors import from src/ which is outside rootDir)\n// export * from \"./executors\";\n\n// Default export\nexport { DH_LIT_ACTIONS_CHIPOTLE, CHIPOTLE_DEPLOYMENTS } from \"./constants/chunks/lit-actions-registry\";\n", "// base-x encoding / decoding\n// Copyright (c) 2018 base-x contributors\n// Copyright (c) 2014-2018 The Bitcoin Core developers (base58.cpp)\n// Distributed under the MIT software license, see the accompanying\n// file LICENSE or http://www.opensource.org/licenses/mit-license.php.\nfunction base (ALPHABET) {\n if (ALPHABET.length >= 255) { throw new TypeError('Alphabet too long') }\n const BASE_MAP = new Uint8Array(256)\n for (let j = 0; j < BASE_MAP.length; j++) {\n BASE_MAP[j] = 255\n }\n for (let i = 0; i < ALPHABET.length; i++) {\n const x = ALPHABET.charAt(i)\n const xc = x.charCodeAt(0)\n if (BASE_MAP[xc] !== 255) { throw new TypeError(x + ' is ambiguous') }\n BASE_MAP[xc] = i\n }\n const BASE = ALPHABET.length\n const LEADER = ALPHABET.charAt(0)\n const FACTOR = Math.log(BASE) / Math.log(256) // log(BASE) / log(256), rounded up\n const iFACTOR = Math.log(256) / Math.log(BASE) // log(256) / log(BASE), rounded up\n function encode (source) {\n // eslint-disable-next-line no-empty\n if (source instanceof Uint8Array) { } else if (ArrayBuffer.isView(source)) {\n source = new Uint8Array(source.buffer, source.byteOffset, source.byteLength)\n } else if (Array.isArray(source)) {\n source = Uint8Array.from(source)\n }\n if (!(source instanceof Uint8Array)) { throw new TypeError('Expected Uint8Array') }\n if (source.length === 0) { return '' }\n // Skip & count leading zeroes.\n let zeroes = 0\n let length = 0\n let pbegin = 0\n const pend = source.length\n while (pbegin !== pend && source[pbegin] === 0) {\n pbegin++\n zeroes++\n }\n // Allocate enough space in big-endian base58 representation.\n const size = ((pend - pbegin) * iFACTOR + 1) >>> 0\n const b58 = new Uint8Array(size)\n // Process the bytes.\n while (pbegin !== pend) {\n let carry = source[pbegin]\n // Apply \"b58 = b58 * 256 + ch\".\n let i = 0\n for (let it1 = size - 1; (carry !== 0 || i < length) && (it1 !== -1); it1--, i++) {\n carry += (256 * b58[it1]) >>> 0\n b58[it1] = (carry % BASE) >>> 0\n carry = (carry / BASE) >>> 0\n }\n if (carry !== 0) { throw new Error('Non-zero carry') }\n length = i\n pbegin++\n }\n // Skip leading zeroes in base58 result.\n let it2 = size - length\n while (it2 !== size && b58[it2] === 0) {\n it2++\n }\n // Translate the result into a string.\n let str = LEADER.repeat(zeroes)\n for (; it2 < size; ++it2) { str += ALPHABET.charAt(b58[it2]) }\n return str\n }\n function decodeUnsafe (source) {\n if (typeof source !== 'string') { throw new TypeError('Expected String') }\n if (source.length === 0) { return new Uint8Array() }\n let psz = 0\n // Skip and count leading '1's.\n let zeroes = 0\n let length = 0\n while (source[psz] === LEADER) {\n zeroes++\n psz++\n }\n // Allocate enough space in big-endian base256 representation.\n const size = (((source.length - psz) * FACTOR) + 1) >>> 0 // log(58) / log(256), rounded up.\n const b256 = new Uint8Array(size)\n // Process the characters.\n while (psz < source.length) {\n // Find code of next character\n const charCode = source.charCodeAt(psz)\n // Base map can not be indexed using char code\n if (charCode > 255) { return }\n // Decode character\n let carry = BASE_MAP[charCode]\n // Invalid character\n if (carry === 255) { return }\n let i = 0\n for (let it3 = size - 1; (carry !== 0 || i < length) && (it3 !== -1); it3--, i++) {\n carry += (BASE * b256[it3]) >>> 0\n b256[it3] = (carry % 256) >>> 0\n carry = (carry / 256) >>> 0\n }\n if (carry !== 0) { throw new Error('Non-zero carry') }\n length = i\n psz++\n }\n // Skip leading zeroes in b256.\n let it4 = size - length\n while (it4 !== size && b256[it4] === 0) {\n it4++\n }\n const vch = new Uint8Array(zeroes + (size - it4))\n let j = zeroes\n while (it4 !== size) {\n vch[j++] = b256[it4++]\n }\n return vch\n }\n function decode (string) {\n const buffer = decodeUnsafe(string)\n if (buffer) { return buffer }\n throw new Error('Non-base' + BASE + ' character')\n }\n return {\n encode,\n decodeUnsafe,\n decode\n }\n}\nexport default base\n", "import basex from 'base-x';\nvar ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';\nexport default basex(ALPHABET);\n", "import bs58 from \"bs58\";\n\n/**\n * Convert IPFS CID from base58 to hex format for contract authorization\n * This matches the format expected by LIT Protocol smart contracts\n */\nexport function cidToHex(cid: string): string {\n try {\n if (cid.startsWith(\"Qm\")) {\n // v0 CID - convert base58 to hex\n const bytes = bs58.decode(cid);\n return \"0x\" + Buffer.from(bytes).toString(\"hex\");\n } else if (cid.startsWith(\"0x\")) {\n // Already in hex format\n return cid;\n } else {\n // Unknown format - FAIL FAST\n throw new Error(`Unsupported CID format: ${cid}. Only CIDv0 (Qm...) and hex (0x...) formats are supported.`);\n }\n } catch (error) {\n if (error instanceof Error && error.message.includes('Unsupported CID format')) {\n // Re-throw our own errors\n throw error;\n }\n // bs58 decode or other conversion errors\n throw new Error(`Failed to convert CID ${cid} to hex: ${error instanceof Error ? error.message : String(error)}`);\n }\n}\n", "import { DiamondHandsLitActions } from \"../../interfaces\";\nimport { cidToHex } from \"../../utils/chunks/cid-utils\";\n\nexport type SupportedLitNetwork = \"chipotle\";\n\n/**\n * Diamond Hands LIT Actions Registry \u2014 Chipotle (new REST API)\n * Contains all deployed LIT Actions with their IPFS CIDs and PKP metadata\n */\nexport const DH_LIT_ACTIONS_CHIPOTLE: Partial<DiamondHandsLitActions> = {\n authorizationDummy: {\n cid: \"QmXPh5RTReLqjqZDRNhgVKi1TWMJK2vS9QSRPkWPhLBQJN\",\n authorizedCidHex: cidToHex(\n \"QmXPh5RTReLqjqZDRNhgVKi1TWMJK2vS9QSRPkWPhLBQJN\",\n ),\n name: \"Authorization Dummy\",\n description: \"Production Authorization Dummy\",\n version: \"1.0.0\",\n deployed: true,\n deployedAt: 1776707622939,\n size: 2026,\n hash: \"349d51aa096d56b394f82de75ed34cd4eef72719969f7e5fe584f4582b2745fa\",\n },\n authorizationDummyB: {\n cid: \"QmQXTcAU9tr4YAVrzHujEJw9moJgM1o5gnJU2ojcvhigua\",\n authorizedCidHex: cidToHex(\n \"QmQXTcAU9tr4YAVrzHujEJw9moJgM1o5gnJU2ojcvhigua\",\n ),\n name: \"Authorization Dummy B\",\n description: \"Production Authorization Dummy B\",\n version: \"1.0.0\",\n deployed: true,\n deployedAt: 1776707632987,\n size: 1695,\n hash: \"def4576ba6c382d145f996c4f950b5e39e58792415ee7fdd749e0d2bc61e8f0b\",\n },\n pkpValidator: {\n cid: \"QmTqsmXkjib7CRGFcEhmCs61Z3EZTaRPznaZDDLk5Wq9NX\",\n authorizedCidHex: cidToHex(\n \"QmTqsmXkjib7CRGFcEhmCs61Z3EZTaRPznaZDDLk5Wq9NX\",\n ),\n name: \"Pkp Validator\",\n description: \"Production Pkp Validator\",\n version: \"1.0.0\",\n deployed: true,\n deployedAt: 1777311468630,\n size: 3999,\n hash: \"baba8c31b73dbfe6425d8f9fd69c61f41a573e189a185962382b815a15fddb24\",\n validatorWalletAddress: \"0x9ee56687f7a984d5df7ab79330271a619ef56ddd\",\n pkp: {\n publicKey:\n \"0x04ec0e3acc40884e3f366ca469d08899eb640ad5259fa196b0238075086208a9f8732a3175704f35f0bcc28e07406cfb24c254efd1e7355640577330f0d2dd755f\",\n ethAddress: \"0x9eE56687F7a984D5DF7aB79330271a619ef56DdD\",\n },\n },\n btcTransactionSigner: {\n cid: \"QmZyn4MjYwrtVHfHJJ8cDnQLaQxkWKArnKzx5UaDV26rmt\",\n authorizedCidHex: cidToHex(\n \"QmZyn4MjYwrtVHfHJJ8cDnQLaQxkWKArnKzx5UaDV26rmt\",\n ),\n name: \"Btc Transaction Signer\",\n description: \"Production Btc Transaction Signer\",\n version: \"1.0.0\",\n deployed: true,\n deployedAt: 1777493882468,\n size: 25656,\n hash: \"c1dc107c830449d55e05a2777c072d7e0c62e2bfec4406d5d1bc8dab61eadf91\",\n },\n ucdMintValidator: {\n cid: \"QmUuQHr21xq7EVBVS4AM3TxU1LBC7G9yvsXdXjPFHJQVw7\",\n authorizedCidHex: cidToHex(\n \"QmUuQHr21xq7EVBVS4AM3TxU1LBC7G9yvsXdXjPFHJQVw7\",\n ),\n name: \"Ucd Mint Validator\",\n description: \"Production Ucd Mint Validator\",\n version: \"1.0.0\",\n deployed: true,\n deployedAt: 1777493891124,\n size: 65506,\n hash: \"a42096083c705332dda82d5e669e5e1e258c1d9da0203593f6502a14d61f1fb7\",\n validatorWalletAddress: \"0x36f3dd61c4c08a56d29ed2fd6d5f111b67b6a7a1\",\n pkp: {\n publicKey:\n \"0x041ab17cd91fc5c0b761eea6092d032807561b621b82c488826776e04a9158d61ba64d809a4729f0501289d980732cb06d8dfd06999dd9c8efd2f495dad78b31bb\",\n ethAddress: \"0x4D9299055093938d0CD2F26C42A87260CB50adD8\",\n },\n },\n processPaymentValidator: {\n cid: \"QmWb6MxjnfCe2KboAEF83za8u528hLeuoAqE6JCTKcAqMZ\",\n authorizedCidHex: cidToHex(\n \"QmWb6MxjnfCe2KboAEF83za8u528hLeuoAqE6JCTKcAqMZ\",\n ),\n name: \"Process Payment Validator\",\n description: \"Production Process Payment Validator\",\n version: \"1.0.0\",\n deployed: true,\n deployedAt: 1777493900466,\n size: 61412,\n hash: \"c77a28d80a8122c73b634229b9024e3974eedf696e5db29db7b40dc9a53f9165\",\n validatorWalletAddress: \"0xc2ed5a59bd81eb4d1a19a0147b86ab69aa4c28ba\",\n pkp: {\n publicKey:\n \"0x04cfccb9b6c7addc79469c0e9fafebef4c70801302c62cdb26b610723a9b8527d23f98a40158c7340d83fd0e62d02b5ec74c12f7a86d98e8c6973b4811110f19ad\",\n ethAddress: \"0x440Fd7B157766b95eb551825C06DF4f92E41D55b\",\n },\n },\n extendPositionValidator: {\n cid: \"QmV2uyKWNY54HHoC5FggF4nFqGNoDydQ6ceExA8ztmjHwz\",\n authorizedCidHex: cidToHex(\n \"QmV2uyKWNY54HHoC5FggF4nFqGNoDydQ6ceExA8ztmjHwz\",\n ),\n name: \"Extend Position Validator\",\n description: \"Production Extend Position Validator\",\n version: \"1.0.0\",\n deployed: true,\n deployedAt: 1777496426914,\n size: 60611,\n hash: \"d2ee66d7fd30762d637d54f1dedb40d5d7bfe7dac9a92201e60b62de30e6aa80\",\n validatorWalletAddress: \"0xb3f4271c475887a86a2f21446f0968c30cc74c97\",\n pkp: {\n publicKey:\n \"0x04e52338f6a0c3362800f1a94c4c8f08be5030dde0fa217388c53d1c1a4790f4443c9f2886b2d2d8f4b75a9a156e9b028011b1a1cf7648da0e556aa980ec692836\",\n ethAddress: \"0x4cb7651Ba27610991A5775486B5F4487F3Dd7cDA\",\n },\n },\n btcWithdrawal: {\n cid: \"QmdaeZ3inL9LiSkSAYV81ohqdYJ24n7awEpitHDHXKaJUm\",\n authorizedCidHex: cidToHex(\n \"QmdaeZ3inL9LiSkSAYV81ohqdYJ24n7awEpitHDHXKaJUm\",\n ),\n name: \"Btc Withdrawal\",\n description: \"Production Btc Withdrawal\",\n version: \"1.0.0\",\n deployed: true,\n deployedAt: 1777551892935,\n size: 72300,\n hash: \"888eef2c01d71a20fc6734fab41ec9f4eda4941774fe23ac44d1fcb6c21a8807\",\n validatorWalletAddress: \"0xbb137fbda353199e9419b698c57a742124d4987d\",\n pkp: {\n publicKey:\n \"0x043616787c5432415c24378c4ef48de2bcd6bb7b575b837e3cff09171802662da7105d79586c7659677a0ecbaddac4cce06cb2a11f69a16fa0c4d7002ac7d51a4d\",\n ethAddress: \"0x7b9316cAA00B257F7CE30F7F6979bd6867BA9eE2\",\n },\n },\n liquidationValidator: {\n cid: \"QmaDsh5t5J6s1TgYeoW8PPZXM1gniZWW3jfPyg1MHjPQ7Q\",\n authorizedCidHex: cidToHex(\n \"QmaDsh5t5J6s1TgYeoW8PPZXM1gniZWW3jfPyg1MHjPQ7Q\",\n ),\n name: \"Liquidation Validator\",\n description: \"Production Liquidation Validator\",\n version: \"1.0.0\",\n deployed: true,\n deployedAt: 1777493931019,\n size: 54906,\n hash: \"ffe0b955b64973aa155cc29dc9aeceb72a36459d5a2c734dca872ba936db6f8e\",\n validatorWalletAddress: \"0x1542f863ee0340f6a067573e80ae66dd9b3838fa\",\n pkp: {\n publicKey:\n \"0x041a241de58976c13e3d7ca5a18e494f7330151e3818706135d50176e78ffb673e58b11f0b67a87c73fade6c6b27d6e7a7377ea9f4c10bf31880f1d725f18cc6f8\",\n ethAddress: \"0x8770470620Db25bBf2BFBca1eF7Bdd6CF91ab1b5\",\n },\n },\n priceOracle: {\n cid: \"Qma6vTTnn3AFy3A4u3A7T5tT6eEp26vXt7rPHSWpmBpdsp\",\n authorizedCidHex: cidToHex(\n \"Qma6vTTnn3AFy3A4u3A7T5tT6eEp26vXt7rPHSWpmBpdsp\",\n ),\n name: \"Price Oracle\",\n description: \"Production Price Oracle (Chipotle main+getPrivateKey)\",\n version: \"1.1.0\",\n deployed: true,\n deployedAt: 1777311530589,\n size: 11531,\n hash: \"a49e9a4875e68a1b8f1c37bcc8284a28e0995652a7df8bad4f1853408d1c81c8\",\n validatorWalletAddress: \"0x831ddf3048547b983efe3ccbbb35a45a53191651\",\n pkp: {\n publicKey:\n \"0x043beaa1da47875601a8d6f430b1438ee2d4732eefd06e5c63a474bbdaf9e7df1797b1b951e041b627c2bf839788164825324c9cbd1e2d6f740fdd4bff69b1900b\",\n ethAddress: \"0x831DdF3048547B983EFE3ccBBB35a45a53191651\",\n },\n },\n loanVaultBtcBalance: {\n cid: \"QmWGEg79snhDQ5Fkm9pLhNmLKUJY5uXfp3t3276Jybritq\",\n authorizedCidHex: cidToHex(\n \"QmWGEg79snhDQ5Fkm9pLhNmLKUJY5uXfp3t3276Jybritq\",\n ),\n name: \"Loan Vault Btc Balance\",\n description: \"Production Loan Vault Btc Balance\",\n version: \"1.0.0\",\n deployed: true,\n deployedAt: 1777311539409,\n size: 44293,\n hash: \"70b009f01b9051dee2db1e4030dd17d08dc53728d30b5e24cffd11d07acfcaf0\",\n validatorWalletAddress: \"0x47feea74c8739d24b44faa67217f2f99970e15e7\",\n pkp: {\n publicKey:\n \"0x043616787c5432415c24378c4ef48de2bcd6bb7b575b837e3cff09171802662da7105d79586c7659677a0ecbaddac4cce06cb2a11f69a16fa0c4d7002ac7d51a4d\",\n ethAddress: \"0x7b9316cAA00B257F7CE30F7F6979bd6867BA9eE2\",\n },\n },\n adminLiquidationValidator: {\n cid: \"QmdeeDPqjh59HG3AF8LrvQkM8n3wbirRyyKBoY3XAzmi6b\",\n authorizedCidHex: cidToHex(\n \"QmdeeDPqjh59HG3AF8LrvQkM8n3wbirRyyKBoY3XAzmi6b\",\n ),\n name: \"Admin Liquidation Validator\",\n description: \"Production Admin Liquidation Validator\",\n version: \"1.0.0\",\n deployed: true,\n deployedAt: 1777493941746,\n size: 51555,\n hash: \"404fc4620f6b2ff31f233cdbe18290992498e63e45f38da76e3b6e7826e435b4\",\n validatorWalletAddress: \"0x2a83b78842d75358877deb8854ccc5bbbaa5012e\",\n pkp: {\n publicKey:\n \"0x04ee4316afb8538280ede6c56def9cbe791f18f96ccba97768b28e2966d9c46a9493a6b81f4571495c3ce56c2d0b33fe8cf63726cd12d395832ea54162b5c67801\",\n ethAddress: \"0x038B7a1918ae9121bC641236b7946a069BE21591\",\n },\n },\n alwaysSigner: {\n cid: \"QmXQjybSsdE19Da66to9Hh7X2dLFwpZPhjoqojxbpk7Nvs\",\n authorizedCidHex: cidToHex(\n \"QmXQjybSsdE19Da66to9Hh7X2dLFwpZPhjoqojxbpk7Nvs\",\n ),\n name: \"Always Signer\",\n description: \"Production Always Signer\",\n version: \"1.0.0\",\n deployed: true,\n deployedAt: 1776708176283,\n size: 4143,\n hash: \"25007f80b4ed3169470bf617d403971136be431eb29dfb09f78afe2494bf0138\",\n },\n};\n\nexport function getDeploymentsForNetwork(\n network: SupportedLitNetwork,\n): Partial<DiamondHandsLitActions> {\n if (network === \"chipotle\") return DH_LIT_ACTIONS_CHIPOTLE;\n throw new Error(`Unsupported LIT network: ${network}`);\n}\n\nexport const CHIPOTLE_DEPLOYMENTS = DH_LIT_ACTIONS_CHIPOTLE;\n", "import { LitActionConfig, LitActionName } from \"../../interfaces\";\nimport {\n getDeploymentsForNetwork,\n SupportedLitNetwork,\n} from \"../../constants/chunks/lit-actions-registry\";\n\n/**\n * Get LIT Action CID by name\n * @param actionName - Name of the LIT Action\n * @returns IPFS CID of the deployed action\n * @throws Error if action is not deployed\n */\nexport function getLitActionCID(\n litNetwork: SupportedLitNetwork = \"chipotle\",\n actionName: LitActionName,\n): string {\n const deployments = getDeploymentsForNetwork(litNetwork);\n const action = deployments[actionName];\n\n if (!action || !action.deployed || !action.cid) {\n throw new Error(\n `LIT Action '${actionName}' not deployed on network '${litNetwork}'. Check deployment status.`,\n );\n }\n return action.cid;\n}\n\n/**\n * Get LIT Action configuration by name\n * @param actionName - Name of the LIT Action\n * @returns Complete LIT Action configuration\n */\nexport function getLitActionConfig(\n actionName: LitActionName,\n litNetwork: SupportedLitNetwork = \"chipotle\",\n): LitActionConfig {\n const deployments = getDeploymentsForNetwork(litNetwork);\n const action = deployments[actionName];\n if (!action) {\n throw new Error(\n `LIT Action '${actionName}' not found in registry for network '${litNetwork}'.`,\n );\n }\n return action;\n}\n\n/**\n * Check if a LIT Action is deployed and ready to use\n * @param actionName - Name of the LIT Action\n * @returns True if deployed, false otherwise\n */\nexport function isLitActionDeployed(\n actionName: LitActionName,\n litNetwork: SupportedLitNetwork = \"chipotle\",\n): boolean {\n const deployments = getDeploymentsForNetwork(litNetwork);\n const action = deployments[actionName];\n return !!(action && action.deployed && action.cid);\n}\n\n/**\n * Validate that all required actions are deployed\n * @param requiredActions - Array of required action names\n * @returns True if all actions are deployed\n */\nexport function validateDeployedActions(\n requiredActions: LitActionName[],\n litNetwork: SupportedLitNetwork = \"chipotle\",\n): boolean {\n return requiredActions.every((actionName) =>\n isLitActionDeployed(actionName, litNetwork),\n );\n}\n\n/**\n * Get all deployed LIT Actions\n * @returns Array of deployed LIT Action configurations\n */\nexport function getDeployedActions(\n litNetwork: SupportedLitNetwork = \"chipotle\",\n): LitActionConfig[] {\n const deployments = getDeploymentsForNetwork(litNetwork);\n return Object.values(deployments).filter(\n (action) => action && action.deployed,\n );\n}\n", "import {\n getDeploymentsForNetwork,\n SupportedLitNetwork,\n} from \"../../constants/chunks/lit-actions-registry\";\n/**\n * Get the complete setup for PKP Validator testing\n * Returns both the LIT Action config and its associated PKP\n */\nexport function getPKPValidatorSetup(\n litNetwork: SupportedLitNetwork = \"chipotle\"\n) {\n const deployments = getDeploymentsForNetwork(litNetwork);\n const litAction = deployments.pkpValidator;\n\n if (!litAction || !litAction.pkp) {\n throw new Error(\n `PKP validator setup is not available for litNetwork='${litNetwork}'. ` +\n `Ensure the pkpValidator action (with pkp attached) is defined in the registry.`\n );\n }\n\n return {\n litAction,\n pkp: litAction.pkp!,\n };\n}\n", "/**\n * LIT Protocol Connection Helpers\n * Provides retry logic, error handling, and connection validation utilities\n * for robust LIT Protocol operations\n */\n\nimport {\n debugInfo,\n debugWarn,\n debugError,\n logConnectionAttempt,\n logConnectionSuccess,\n logConnectionFailure,\n logRequest,\n logResponse,\n logError,\n logPkpOperation,\n logLitActionExecution,\n logLitActionResult,\n networkMetrics,\n PerformanceTracker\n} from './debug-logger';\nimport {\n classifyError,\n getRetryConfigFromClassification,\n createErrorReport,\n litProtocolErrors\n} from './error-classification';\n\n/**\n * Retry configuration options\n */\nexport interface RetryConfig {\n maxAttempts: number;\n initialDelayMs: number;\n maxDelayMs: number;\n backoffMultiplier: number;\n retryableErrors: string[];\n}\n\n/**\n * Default retry configuration optimized for LIT Protocol operations\n */\nexport const DEFAULT_RETRY_CONFIG: RetryConfig = {\n maxAttempts: 5, // Increased from 3 for PKP operations\n initialDelayMs: 2000, // Increased from 1000ms for PKP operations\n maxDelayMs: 60000, // Increased from 30000ms for PKP operations\n backoffMultiplier: 2,\n retryableErrors: [\n 'request_timeout',\n 'network error',\n 'connection failed',\n 'ENOTFOUND',\n 'ECONNREFUSED',\n 'ETIMEDOUT',\n 'socket hang up',\n 'timeout',\n // PKP-specific errors\n 'signing shares',\n 'There was an error getting the signing shares',\n 'PKP validation',\n 'pkp validation failed',\n 'resource validation',\n 'ipfs propagation',\n 'session.*expired',\n 'NodeError',\n 'Response from the nodes',\n 'consensus',\n 'threshold'\n ]\n};\n\n/**\n * Connection validation result\n */\nexport interface ConnectionValidationResult {\n isValid: boolean;\n error?: string;\n latencyMs?: number;\n}\n\n/**\n * Sleep utility for retry delays\n */\nexport function sleep(ms: number): Promise<void> {\n return new Promise(resolve => setTimeout(resolve, ms));\n}\n\n/**\n * Check if an error is retryable using enhanced classification system\n */\nexport function isRetryableError(error: any, retryableErrors: string[] = DEFAULT_RETRY_CONFIG.retryableErrors): boolean {\n if (!error) return false;\n\n // Use enhanced error classification for LIT Protocol specific errors\n const classification = classifyError(error);\n\n // If classification determined retryability, use that\n if (classification.isRetryable !== undefined) {\n debugInfo('ERROR_CLASSIFICATION', `Error classified as: ${classification.category} (retryable: ${classification.isRetryable})`);\n return classification.isRetryable;\n }\n\n // Fallback to legacy pattern matching\n const errorMessage = error.message || error.toString() || '';\n const errorLower = errorMessage.toLowerCase();\n\n return retryableErrors.some(pattern =>\n errorLower.includes(pattern.toLowerCase())\n );\n}\n\n/**\n * Calculate exponential backoff delay with jitter\n */\nexport function calculateDelay(attempt: number, config: RetryConfig): number {\n const exponentialDelay = Math.min(\n config.initialDelayMs * Math.pow(config.backoffMultiplier, attempt - 1),\n config.maxDelayMs\n );\n\n // Add jitter (\u00B125%) to avoid thundering herd\n const jitter = exponentialDelay * 0.25 * (Math.random() - 0.5);\n return Math.max(100, exponentialDelay + jitter);\n}\n\n/**\n * Enhanced retry wrapper with intelligent error classification\n */\nexport async function withRetry<T>(\n operation: () => Promise<T>,\n config: Partial<RetryConfig> = {},\n operationName: string = 'operation'\n): Promise<T> {\n let finalConfig = { ...DEFAULT_RETRY_CONFIG, ...config };\n const tracker = new PerformanceTracker(`Retry ${operationName}`);\n let lastError: any;\n let errorClassification: any = null;\n\n for (let attempt = 1; attempt <= finalConfig.maxAttempts; attempt++) {\n try {\n debugInfo('RETRY', `\uD83D\uDD04 [${operationName}] Attempt ${attempt}/${finalConfig.maxAttempts}`);\n const startTime = Date.now();\n const result = await operation();\n const duration = Date.now() - startTime;\n\n debugInfo('RETRY', `\u2705 [${operationName}] Success after ${duration}ms (attempt ${attempt})`);\n networkMetrics.recordRequest(operationName, true, duration);\n tracker.end(true);\n return result;\n } catch (error) {\n lastError = error;\n const errorMessage = (error as any)?.message || (error as any)?.toString() || 'Unknown error';\n\n // Classify error and potentially adjust retry strategy\n errorClassification = classifyError(error);\n\n // Update config with intelligent retry settings if this is the first error\n if (attempt === 1 && errorClassification) {\n const intelligentConfig = getRetryConfigFromClassification(errorClassification);\n finalConfig = {\n ...finalConfig,\n ...intelligentConfig,\n maxAttempts: Math.min(finalConfig.maxAttempts, intelligentConfig.maxAttempts)\n };\n\n debugInfo('RETRY', `\uD83E\uDDE0 Intelligent retry strategy applied: ${errorClassification.category} (${errorClassification.retryStrategy})`);\n debugInfo('RETRY', `\uD83D\uDCCA Adjusted config: max=${finalConfig.maxAttempts}, delay=${finalConfig.initialDelayMs}ms`);\n }\n\n debugWarn('RETRY', `\u26A0\uFE0F [${operationName}] Attempt ${attempt} failed:`, {\n error: errorMessage,\n attempt,\n category: errorClassification.category,\n retryable: errorClassification.isRetryable\n });\n networkMetrics.recordRequest(operationName, false, 0, errorMessage);\n\n // Check if we've exceeded the (possibly adjusted) max attempts\n if (attempt >= finalConfig.maxAttempts) {\n debugError('RETRY', `\u274C [${operationName}] All attempts failed. Last error:`, { error: errorMessage });\n\n // Generate detailed error report\n if (errorClassification) {\n const errorReport = createErrorReport(error);\n debugError('RETRY', `\uD83D\uDCCB Error Analysis Report:\\n${errorReport}`);\n }\n\n tracker.end(false);\n break;\n }\n\n if (!errorClassification.isRetryable) {\n debugError('RETRY', `\uD83D\uDEAB [${operationName}] Non-retryable error (${errorClassification.category}):`, { error: errorMessage });\n\n // Show troubleshooting tips for non-retryable errors\n if (errorClassification.troubleshooting) {\n debugInfo('RETRY', `\uD83D\uDCA1 Troubleshooting suggestions:\\n ${errorClassification.troubleshooting.join('\\n ')}`);\n }\n\n tracker.end(false);\n break;\n }\n\n const delayMs = calculateDelay(attempt, finalConfig);\n debugInfo('RETRY', `\u23F3 [${operationName}] Waiting ${delayMs}ms before retry... (${errorClassification.retryStrategy} strategy)`);\n await sleep(delayMs);\n }\n }\n\n throw lastError;\n}\n\n/**\n * Enhanced LIT Node Client connector with retry logic and readiness validation\n */\nexport async function connectLitNodeClient(\n litNodeClient: any,\n config: Partial<RetryConfig> = {}\n): Promise<void> {\n const network = litNodeClient?.config?.litNetwork || 'unknown';\n\n return withRetry(\n async () => {\n if (!litNodeClient) {\n throw new Error('LIT Node Client is null or undefined');\n }\n\n const startTime = Date.now();\n logConnectionAttempt(network, 1, config.maxAttempts || DEFAULT_RETRY_CONFIG.maxAttempts);\n\n // First connect to the network\n await litNodeClient.connect();\n\n // Wait for client to be ready\n debugInfo('CONNECTION', `\u23F3 Waiting for LIT Node Client to be ready...`);\n const readyTimeout = 30000; // 30 seconds timeout for readiness\n const readyStartTime = Date.now();\n\n while (!litNodeClient.ready && (Date.now() - readyStartTime) < readyTimeout) {\n await sleep(500); // Check every 500ms\n }\n\n if (!litNodeClient.ready) {\n throw new Error(`LIT Node Client failed to become ready within ${readyTimeout}ms`);\n }\n\n const latency = Date.now() - startTime;\n debugInfo('CONNECTION', `\u2705 LIT Node Client is ready (${litNodeClient.ready})`);\n logConnectionSuccess(network, latency);\n },\n config,\n 'LIT Node Client Connection'\n );\n}\n\n/**\n * Enhanced LIT Contracts connector with retry logic\n */\nexport async function connectLitContracts(\n litContracts: any,\n config: Partial<RetryConfig> = {}\n): Promise<void> {\n return withRetry(\n async () => {\n if (!litContracts) {\n throw new Error('LIT Contracts is null or undefined');\n }\n\n console.log(`\uD83D\uDD17 Connecting to LIT Contracts: ${litContracts.network || 'unknown'}`);\n await litContracts.connect();\n console.log('\u2705 LIT Contracts connected successfully');\n },\n config,\n 'LIT Contracts Connection'\n );\n}\n\n/**\n * Validate LIT Node Client connection health\n */\nexport async function validateLitNodeConnection(litNodeClient: any): Promise<ConnectionValidationResult> {\n try {\n const startTime = Date.now();\n\n if (!litNodeClient) {\n return { isValid: false, error: 'LIT Node Client is null or undefined' };\n }\n\n // Try to get the latest block hash as a health check\n await litNodeClient.getLatestBlockhash();\n\n const latencyMs = Date.now() - startTime;\n return { isValid: true, latencyMs };\n } catch (error) {\n return {\n isValid: false,\n error: (error as any)?.message || (error as any)?.toString() || 'Unknown validation error'\n };\n }\n}\n\n/**\n * Enhanced session signature generator with retry logic and caching\n */\nexport async function generateSessionSignatures(\n litNodeClient: any,\n sessionConfig: any,\n config: Partial<RetryConfig> = {},\n pkpTokenId?: string,\n litActionCid?: string,\n signerAddress?: string,\n network: string = 'chipotle'\n): Promise<any> {\n // If caching parameters are provided, try cache first\n if (pkpTokenId && litActionCid && signerAddress) {\n const { sessionSignatureCache } = await import('./session-signature-cache');\n\n const cached = sessionSignatureCache.get(pkpTokenId, litActionCid, signerAddress, network);\n if (cached) {\n const sigCount = Object.keys(cached).length;\n debugInfo('SESSION', `\uD83D\uDD04 Using cached session signatures (${sigCount} sigs)`);\n return cached;\n }\n }\n\n return withRetry(\n async () => {\n debugInfo('SESSION', '\uD83D\uDD10 Generating new session signatures...');\n const sessionSigs = await litNodeClient.getSessionSigs(sessionConfig);\n const sigCount = Object.keys(sessionSigs).length;\n\n // Cache if parameters provided\n if (pkpTokenId && litActionCid && signerAddress) {\n const { sessionSignatureCache } = await import('./session-signature-cache');\n const expiration = new Date(sessionConfig.expiration);\n sessionSignatureCache.set(pkpTokenId, litActionCid, signerAddress, network, sessionSigs, expiration);\n }\n\n debugInfo('SESSION', `\u2705 Generated ${sigCount} session signatures`);\n return sessionSigs;\n },\n config,\n 'Session Signature Generation'\n );\n}\n\n/**\n * Validate LIT Node Client readiness before operations\n */\nexport async function validateLitNodeReadiness(litNodeClient: any): Promise<void> {\n if (!litNodeClient) {\n throw new Error('LIT Node Client is null or undefined');\n }\n\n // Check if client is connected\n if (!litNodeClient.ready) {\n debugWarn('CONNECTION', '\u26A0\uFE0F LIT Node Client not ready, attempting to reconnect...');\n await litNodeClient.connect();\n\n // Wait for readiness with timeout\n const readyTimeout = 15000; // 15 seconds\n const startTime = Date.now();\n\n while (!litNodeClient.ready && (Date.now() - startTime) < readyTimeout) {\n await sleep(500);\n }\n\n if (!litNodeClient.ready) {\n throw new Error('LIT Node Client is not ready for operations');\n }\n }\n\n debugInfo('CONNECTION', '\u2705 LIT Node Client readiness validated');\n}\n\n/**\n * Enhanced LIT Action executor with retry logic and readiness validation\n */\nexport async function executeLitAction(\n litNodeClient: any,\n executionConfig: any,\n config: Partial<RetryConfig> = {}\n): Promise<any> {\n const cid = executionConfig.ipfsId;\n\n return withRetry(\n async () => {\n const startTime = Date.now();\n\n // Validate client readiness before execution\n await validateLitNodeReadiness(litNodeClient);\n\n logLitActionExecution(cid, executionConfig.jsParams);\n logRequest('LIT_ACTION', 'executeJs', executionConfig);\n\n const result = await litNodeClient.executeJs(executionConfig);\n const duration = Date.now() - startTime;\n\n if (!result.response) {\n logError('LIT_ACTION', 'executeJs', new Error('No response from LIT Action'), duration);\n throw new Error('No response from LIT Action');\n }\n\n logResponse('LIT_ACTION', 'executeJs', result, duration);\n logLitActionResult(cid, true, duration, result);\n return result;\n },\n config,\n 'LIT Action Execution'\n );\n}\n\n/**\n * PKP operation wrapper with enhanced error handling\n */\nexport async function executePkpOperation<T>(\n operation: () => Promise<T>,\n operationName: string,\n config: Partial<RetryConfig> = {}\n): Promise<T> {\n const pkpConfig = {\n ...DEFAULT_RETRY_CONFIG,\n ...config,\n retryableErrors: [\n ...DEFAULT_RETRY_CONFIG.retryableErrors,\n 'pkp validation',\n 'signing shares',\n 'resource validation',\n 'ipfs propagation'\n ]\n };\n\n return withRetry(operation, pkpConfig, `PKP ${operationName}`);\n}\n\n/**\n * Network-specific timeout configurations\n */\nexport const NETWORK_TIMEOUTS = {\n chipotle: {\n connection: 60000,\n operation: 120000,\n pkpValidation: 180000,\n },\n} as const;\n\nexport function getNetworkTimeouts(network: string) {\n return NETWORK_TIMEOUTS[network as keyof typeof NETWORK_TIMEOUTS] || NETWORK_TIMEOUTS['chipotle'];\n}", "/**\n * Enhanced Error Classification for LIT Protocol Operations\n * Provides detailed error categorization and specific retry strategies\n */\n\n/**\n * Error categories for different types of failures\n */\nexport enum ErrorCategory {\n NETWORK = 'network',\n PROTOCOL = 'protocol',\n AUTHENTICATION = 'authentication',\n RESOURCE = 'resource',\n VALIDATION = 'validation',\n TIMEOUT = 'timeout',\n RATE_LIMIT = 'rate_limit',\n CONFIGURATION = 'configuration',\n UNKNOWN = 'unknown'\n}\n\n/**\n * Retry strategy types\n */\nexport enum RetryStrategy {\n AGGRESSIVE = 'aggressive', // Fast retries for transient issues\n CONSERVATIVE = 'conservative', // Slower retries for resource issues\n EXPONENTIAL = 'exponential', // Standard exponential backoff\n LINEAR = 'linear', // Linear delay increase\n NONE = 'none' // No retries\n}\n\n/**\n * Error classification result\n */\nexport interface ErrorClassification {\n category: ErrorCategory;\n retryStrategy: RetryStrategy;\n isRetryable: boolean;\n maxAttempts: number;\n baseDelayMs: number;\n maxDelayMs: number;\n description: string;\n troubleshooting?: string[];\n}\n\n/**\n * LIT Protocol specific error patterns\n */\nconst LIT_ERROR_PATTERNS = {\n // Network and connection errors\n network: [\n /network error/i,\n /connection failed/i,\n /connection refused/i,\n /connection timeout/i,\n /socket hang up/i,\n /enotfound/i,\n /econnrefused/i,\n /etimedout/i,\n /dns resolution failed/i,\n /request failed/i\n ],\n\n // Protocol specific errors\n protocol: [\n /lit protocol/i,\n /lit node/i,\n /signing shares/i,\n /node communication/i,\n /consensus/i,\n /threshold/i,\n /decryption/i,\n /signature verification/i,\n /There was an error getting the signing shares/i,\n /Response from the nodes/i,\n /NodeError/i,\n /PKP validation/i,\n /pkp validation failed/i\n ],\n\n // Authentication and authorization errors\n authentication: [\n /authentication/i,\n /authorization/i,\n /session.*expired/i,\n /invalid.*signature/i,\n /unauthorized/i,\n /access.*denied/i,\n /permission/i,\n /auth.*failed/i,\n /siwe/i,\n /session.*sig/i\n ],\n\n // Resource and validation errors\n resource: [\n /resource.*not.*found/i,\n /ipfs.*not.*found/i,\n /cid.*invalid/i,\n /pkp.*not.*found/i,\n /pkp.*validation/i,\n /resource.*validation/i,\n /action.*not.*permitted/i,\n /lit.*action.*not.*found/i\n ],\n\n // Validation errors\n validation: [\n /validation.*failed/i,\n /invalid.*input/i,\n /invalid.*parameter/i,\n /malformed/i,\n /schema.*validation/i,\n /type.*error/i,\n /missing.*required/i\n ],\n\n // Timeout specific errors\n timeout: [\n /timeout/i,\n /request.*timed.*out/i,\n /operation.*timed.*out/i,\n /deadline.*exceeded/i,\n /time.*limit.*exceeded/i\n ],\n\n // Rate limiting errors\n rateLimit: [\n /rate.*limit/i,\n /too.*many.*requests/i,\n /quota.*exceeded/i,\n /throttled/i,\n /429/,\n /rate.*exceeded/i\n ],\n\n // Configuration errors\n configuration: [\n /configuration/i,\n /config.*error/i,\n /environment/i,\n /missing.*env/i,\n /invalid.*config/i,\n /setup.*error/i\n ]\n};\n\n/**\n * Predefined retry strategies\n */\nconst RETRY_STRATEGIES = {\n [RetryStrategy.AGGRESSIVE]: {\n maxAttempts: 5,\n baseDelayMs: 500,\n maxDelayMs: 5000,\n multiplier: 1.5\n },\n [RetryStrategy.CONSERVATIVE]: {\n maxAttempts: 3,\n baseDelayMs: 3000,\n maxDelayMs: 30000,\n multiplier: 2.5\n },\n [RetryStrategy.EXPONENTIAL]: {\n maxAttempts: 4,\n baseDelayMs: 1000,\n maxDelayMs: 15000,\n multiplier: 2.0\n },\n [RetryStrategy.LINEAR]: {\n maxAttempts: 3,\n baseDelayMs: 2000,\n maxDelayMs: 10000,\n multiplier: 1.0\n },\n [RetryStrategy.NONE]: {\n maxAttempts: 1,\n baseDelayMs: 0,\n maxDelayMs: 0,\n multiplier: 1.0\n }\n};\n\n/**\n * Error classification rules\n */\nconst CLASSIFICATION_RULES: Record<ErrorCategory, ErrorClassification> = {\n [ErrorCategory.NETWORK]: {\n category: ErrorCategory.NETWORK,\n retryStrategy: RetryStrategy.AGGRESSIVE,\n isRetryable: true,\n ...RETRY_STRATEGIES[RetryStrategy.AGGRESSIVE],\n description: 'Network connectivity or communication error',\n troubleshooting: [\n 'Check internet connection',\n 'Verify LIT Protocol network status',\n 'Check firewall and proxy settings',\n 'Try connecting to different node endpoints'\n ]\n },\n\n [ErrorCategory.PROTOCOL]: {\n category: ErrorCategory.PROTOCOL,\n retryStrategy: RetryStrategy.CONSERVATIVE,\n isRetryable: true,\n ...RETRY_STRATEGIES[RetryStrategy.CONSERVATIVE],\n description: 'LIT Protocol specific operational error',\n troubleshooting: [\n 'Check LIT Protocol network status',\n 'Verify PKP configuration',\n 'Check signing shares availability',\n 'Wait for network consensus'\n ]\n },\n\n [ErrorCategory.AUTHENTICATION]: {\n category: ErrorCategory.AUTHENTICATION,\n retryStrategy: RetryStrategy.EXPONENTIAL,\n isRetryable: true,\n ...RETRY_STRATEGIES[RetryStrategy.EXPONENTIAL],\n maxAttempts: 2, // Override: limited retries for auth issues\n description: 'Authentication or authorization error',\n troubleshooting: [\n 'Check session signatures validity',\n 'Regenerate authentication tokens',\n 'Verify wallet permissions',\n 'Check PKP authorization status'\n ]\n },\n\n [ErrorCategory.RESOURCE]: {\n category: ErrorCategory.RESOURCE,\n retryStrategy: RetryStrategy.LINEAR,\n isRetryable: true,\n ...RETRY_STRATEGIES[RetryStrategy.LINEAR],\n maxAttempts: 2, // Override: limited retries for resource issues\n description: 'Resource not found or validation error',\n troubleshooting: [\n 'Verify resource CIDs are correct',\n 'Check IPFS availability',\n 'Confirm PKP exists and is accessible',\n 'Wait for resource propagation'\n ]\n },\n\n [ErrorCategory.VALIDATION]: {\n category: ErrorCategory.VALIDATION,\n retryStrategy: RetryStrategy.NONE,\n isRetryable: false,\n ...RETRY_STRATEGIES[RetryStrategy.NONE],\n description: 'Input validation or schema error',\n troubleshooting: [\n 'Check input parameters format',\n 'Verify required fields are provided',\n 'Validate data types and ranges',\n 'Review API documentation'\n ]\n },\n\n [ErrorCategory.TIMEOUT]: {\n category: ErrorCategory.TIMEOUT,\n retryStrategy: RetryStrategy.CONSERVATIVE,\n isRetryable: true,\n ...RETRY_STRATEGIES[RetryStrategy.CONSERVATIVE],\n description: 'Operation timeout error',\n troubleshooting: [\n 'Increase timeout values',\n 'Check network latency',\n 'Verify system resources',\n 'Split operations into smaller chunks'\n ]\n },\n\n [ErrorCategory.RATE_LIMIT]: {\n category: ErrorCategory.RATE_LIMIT,\n retryStrategy: RetryStrategy.CONSERVATIVE,\n isRetryable: true,\n ...RETRY_STRATEGIES[RetryStrategy.CONSERVATIVE],\n maxAttempts: 2, // Override: limited retries to avoid further rate limiting\n baseDelayMs: 5000, // Override: longer delays for rate limits\n description: 'Rate limit or quota exceeded',\n troubleshooting: [\n 'Reduce request frequency',\n 'Implement request batching',\n 'Check rate limit headers',\n 'Wait for quota reset'\n ]\n },\n\n [ErrorCategory.CONFIGURATION]: {\n category: ErrorCategory.CONFIGURATION,\n retryStrategy: RetryStrategy.NONE,\n isRetryable: false,\n ...RETRY_STRATEGIES[RetryStrategy.NONE],\n description: 'Configuration or setup error',\n troubleshooting: [\n 'Check environment variables',\n 'Verify configuration files',\n 'Review setup documentation',\n 'Check required dependencies'\n ]\n },\n\n [ErrorCategory.UNKNOWN]: {\n category: ErrorCategory.UNKNOWN,\n retryStrategy: RetryStrategy.EXPONENTIAL,\n isRetryable: true,\n ...RETRY_STRATEGIES[RetryStrategy.EXPONENTIAL],\n maxAttempts: 2, // Override: conservative retries for unknown errors\n description: 'Unknown or unclassified error',\n troubleshooting: [\n 'Check logs for more details',\n 'Verify system status',\n 'Try again with debug logging',\n 'Contact support if persists'\n ]\n }\n};\n\n/**\n * Classify an error and determine retry strategy\n */\nexport function classifyError(error: any): ErrorClassification {\n const errorMessage = error?.message || error?.toString() || '';\n const errorLower = errorMessage.toLowerCase();\n\n // Check each error category\n for (const [category, patterns] of Object.entries(LIT_ERROR_PATTERNS)) {\n for (const pattern of patterns) {\n if (pattern.test(errorMessage) || pattern.test(errorLower)) {\n const classification = CLASSIFICATION_RULES[category as ErrorCategory];\n return {\n ...classification,\n description: `${classification.description}: ${errorMessage.slice(0, 100)}`\n };\n }\n }\n }\n\n // Default to unknown category\n const unknownClassification = CLASSIFICATION_RULES[ErrorCategory.UNKNOWN];\n return {\n ...unknownClassification,\n description: `${unknownClassification.description}: ${errorMessage.slice(0, 100)}`\n };\n}\n\n/**\n * Get retry configuration from error classification\n */\nexport function getRetryConfigFromClassification(classification: ErrorClassification) {\n return {\n maxAttempts: classification.maxAttempts,\n initialDelayMs: classification.baseDelayMs,\n maxDelayMs: classification.maxDelayMs,\n backoffMultiplier: RETRY_STRATEGIES[classification.retryStrategy].multiplier,\n retryableErrors: ['*'] // Classification already determined retryability\n };\n}\n\n/**\n * Enhanced error analyzer for detailed error information\n */\nexport function analyzeError(error: any) {\n const classification = classifyError(error);\n\n return {\n ...classification,\n originalError: error,\n errorMessage: error?.message || error?.toString() || 'Unknown error',\n errorStack: error?.stack,\n timestamp: new Date().toISOString(),\n retryConfig: getRetryConfigFromClassification(classification)\n };\n}\n\n/**\n * Create a human-readable error report\n */\nexport function createErrorReport(error: any) {\n const analysis = analyzeError(error);\n\n const report = [\n `\uD83D\uDD0D Error Analysis Report`,\n `=====================================`,\n `Category: ${analysis.category.toUpperCase()}`,\n `Retryable: ${analysis.isRetryable ? '\u2705 Yes' : '\u274C No'}`,\n `Strategy: ${analysis.retryStrategy}`,\n `Max Attempts: ${analysis.maxAttempts}`,\n `Description: ${analysis.description}`,\n `Timestamp: ${analysis.timestamp}`,\n ``,\n `Original Error: ${analysis.errorMessage}`,\n ``\n ];\n\n if (analysis.troubleshooting && analysis.troubleshooting.length > 0) {\n report.push(`\uD83D\uDEE0\uFE0F Troubleshooting Steps:`);\n analysis.troubleshooting.forEach((step, index) => {\n report.push(` ${index + 1}. ${step}`);\n });\n report.push('');\n }\n\n return report.join('\\n');\n}\n\n/**\n * LIT Protocol specific error matchers\n */\nexport const litProtocolErrors = {\n isNetworkError: (error: any) => classifyError(error).category === ErrorCategory.NETWORK,\n isProtocolError: (error: any) => classifyError(error).category === ErrorCategory.PROTOCOL,\n isAuthenticationError: (error: any) => classifyError(error).category === ErrorCategory.AUTHENTICATION,\n isResourceError: (error: any) => classifyError(error).category === ErrorCategory.RESOURCE,\n isTimeoutError: (error: any) => classifyError(error).category === ErrorCategory.TIMEOUT,\n isRateLimitError: (error: any) => classifyError(error).category === ErrorCategory.RATE_LIMIT,\n isConfigurationError: (error: any) => classifyError(error).category === ErrorCategory.CONFIGURATION,\n isRetryable: (error: any) => classifyError(error).isRetryable\n};", "/**\n * Diamond Hands LIT Actions Utilities\n * All utility functions for working with deployed LIT Actions\n */\n\nexport * from \"./chunks/lit-action-helpers\";\nexport * from \"./chunks/pkp-setup\";\nexport * from \"./chunks/cid-utils\";\nexport * from \"./chunks/connection-helpers\";\nexport * from \"./chunks/debug-logger\";\nexport * from \"./chunks/error-classification\";\nexport * from \"./chunks/session-signature-cache\";\n"],
4
+ "sourcesContent": ["/**\n * Enhanced Debug Logger for LIT Protocol Operations\n * Provides detailed logging for debugging network and protocol issues\n */\n\nexport enum LogLevel {\n ERROR = 0,\n WARN = 1,\n INFO = 2,\n DEBUG = 3,\n TRACE = 4\n}\n\nexport interface DebugConfig {\n level: LogLevel;\n enableTimestamps: boolean;\n enablePerformanceMetrics: boolean;\n enableRequestLogging: boolean;\n enableNetworkMetrics: boolean;\n}\n\nexport const DEFAULT_DEBUG_CONFIG: DebugConfig = {\n level: LogLevel.INFO,\n enableTimestamps: true,\n enablePerformanceMetrics: true,\n enableRequestLogging: true,\n enableNetworkMetrics: true\n};\n\nlet debugConfig: DebugConfig = { ...DEFAULT_DEBUG_CONFIG };\n\n/**\n * Configure debug logging\n */\nexport function configureDebugLogging(config: Partial<DebugConfig>): void {\n debugConfig = { ...debugConfig, ...config };\n}\n\n/**\n * Get current timestamp string\n */\nfunction getTimestamp(): string {\n return new Date().toISOString();\n}\n\n/**\n * Format log message with metadata\n */\nfunction formatMessage(level: string, component: string, message: string, metadata?: any): string {\n const timestamp = debugConfig.enableTimestamps ? `[${getTimestamp()}] ` : '';\n const metadataStr = metadata ? ` ${JSON.stringify(metadata)}` : '';\n return `${timestamp}[${level}] [${component}] ${message}${metadataStr}`;\n}\n\n/**\n * Performance metrics tracker\n */\nexport class PerformanceTracker {\n private startTime: number;\n private markers: Map<string, number> = new Map();\n\n constructor(private operationName: string) {\n this.startTime = Date.now();\n this.log('PERF', `Started: ${operationName}`);\n }\n\n mark(label: string): void {\n const elapsed = Date.now() - this.startTime;\n this.markers.set(label, elapsed);\n this.log('PERF', `${this.operationName} - ${label}: ${elapsed}ms`);\n }\n\n end(success: boolean = true): number {\n const totalTime = Date.now() - this.startTime;\n const status = success ? 'SUCCESS' : 'FAILED';\n this.log('PERF', `${this.operationName} - ${status}: ${totalTime}ms`);\n\n if (debugConfig.enablePerformanceMetrics && this.markers.size > 0) {\n console.log(`\uD83D\uDCCA Performance Breakdown for ${this.operationName}:`);\n for (const [label, time] of this.markers) {\n console.log(` ${label}: ${time}ms`);\n }\n console.log(` Total: ${totalTime}ms`);\n }\n\n return totalTime;\n }\n\n private log(level: string, message: string): void {\n if (debugConfig.enablePerformanceMetrics) {\n console.log(formatMessage(level, 'PERFORMANCE', message));\n }\n }\n}\n\n/**\n * Network metrics collector\n */\nexport interface NetworkMetrics {\n requestCount: number;\n successCount: number;\n failureCount: number;\n averageLatency: number;\n lastError?: string;\n startTime: number;\n}\n\nclass NetworkMetricsCollector {\n private metrics: Map<string, NetworkMetrics> = new Map();\n\n getMetrics(operation: string): NetworkMetrics {\n if (!this.metrics.has(operation)) {\n this.metrics.set(operation, {\n requestCount: 0,\n successCount: 0,\n failureCount: 0,\n averageLatency: 0,\n startTime: Date.now()\n });\n }\n return this.metrics.get(operation)!;\n }\n\n recordRequest(operation: string, success: boolean, latency: number, error?: string): void {\n const metrics = this.getMetrics(operation);\n metrics.requestCount++;\n\n if (success) {\n metrics.successCount++;\n // Update average latency\n metrics.averageLatency = (metrics.averageLatency * (metrics.successCount - 1) + latency) / metrics.successCount;\n } else {\n metrics.failureCount++;\n if (error) metrics.lastError = error;\n }\n\n if (debugConfig.enableNetworkMetrics) {\n const successRate = ((metrics.successCount / metrics.requestCount) * 100).toFixed(1);\n debugLog('NETWORK', `${operation} - Success: ${success}, Latency: ${latency}ms, Success Rate: ${successRate}%`);\n }\n }\n\n printSummary(): void {\n if (!debugConfig.enableNetworkMetrics) return;\n\n console.log('\\n\uD83D\uDCC8 Network Metrics Summary:');\n for (const [operation, metrics] of this.metrics) {\n const successRate = ((metrics.successCount / metrics.requestCount) * 100).toFixed(1);\n const runtime = Date.now() - metrics.startTime;\n console.log(` ${operation}:`);\n console.log(` Requests: ${metrics.requestCount}`);\n console.log(` Success Rate: ${successRate}%`);\n console.log(` Avg Latency: ${metrics.averageLatency.toFixed(0)}ms`);\n console.log(` Runtime: ${runtime}ms`);\n if (metrics.lastError) {\n console.log(` Last Error: ${metrics.lastError}`);\n }\n }\n }\n}\n\nexport const networkMetrics = new NetworkMetricsCollector();\n\n/**\n * Debug logging functions\n */\nexport function debugError(component: string, message: string, metadata?: any): void {\n if (debugConfig.level >= LogLevel.ERROR) {\n console.error(formatMessage('ERROR', component, message, metadata));\n }\n}\n\nexport function debugWarn(component: string, message: string, metadata?: any): void {\n if (debugConfig.level >= LogLevel.WARN) {\n console.warn(formatMessage('WARN', component, message, metadata));\n }\n}\n\nexport function debugInfo(component: string, message: string, metadata?: any): void {\n if (debugConfig.level >= LogLevel.INFO) {\n console.log(formatMessage('INFO', component, message, metadata));\n }\n}\n\nexport function debugLog(component: string, message: string, metadata?: any): void {\n if (debugConfig.level >= LogLevel.DEBUG) {\n console.log(formatMessage('DEBUG', component, message, metadata));\n }\n}\n\nexport function debugTrace(component: string, message: string, metadata?: any): void {\n if (debugConfig.level >= LogLevel.TRACE) {\n console.log(formatMessage('TRACE', component, message, metadata));\n }\n}\n\n/**\n * Request/Response logger for LIT Protocol operations\n */\nexport function logRequest(component: string, operation: string, params: any): void {\n if (!debugConfig.enableRequestLogging) return;\n\n debugLog(component, `\uD83D\uDD04 Request: ${operation}`, {\n operation,\n params: JSON.stringify(params, null, 2)\n });\n}\n\nexport function logResponse(component: string, operation: string, response: any, duration: number): void {\n if (!debugConfig.enableRequestLogging) return;\n\n debugLog(component, `\u2705 Response: ${operation} (${duration}ms)`, {\n operation,\n duration,\n response: typeof response === 'object' ? JSON.stringify(response, null, 2) : response\n });\n}\n\nexport function logError(component: string, operation: string, error: any, duration?: number): void {\n const durationStr = duration ? ` (${duration}ms)` : '';\n debugError(component, `\u274C Error: ${operation}${durationStr}`, {\n operation,\n duration,\n error: error?.message || error?.toString() || 'Unknown error',\n stack: error?.stack\n });\n}\n\n/**\n * Network connection logger\n */\nexport function logConnectionAttempt(network: string, attempt: number, maxAttempts: number): void {\n debugInfo('CONNECTION', `\uD83D\uDD17 Connecting to ${network} (attempt ${attempt}/${maxAttempts})`);\n}\n\nexport function logConnectionSuccess(network: string, latency: number): void {\n debugInfo('CONNECTION', `\u2705 Connected to ${network} (${latency}ms)`);\n networkMetrics.recordRequest(`connection-${network}`, true, latency);\n}\n\nexport function logConnectionFailure(network: string, error: string, attempt: number): void {\n debugError('CONNECTION', `\u274C Connection failed to ${network} (attempt ${attempt})`, { error });\n networkMetrics.recordRequest(`connection-${network}`, false, 0, error);\n}\n\n/**\n * PKP operation logger\n */\nexport function logPkpOperation(operation: string, pkpId?: string, details?: any): void {\n const pkpStr = pkpId ? ` (PKP: ${pkpId.slice(0, 8)}...)` : '';\n debugInfo('PKP', `\uD83D\uDD11 ${operation}${pkpStr}`, details);\n}\n\n/**\n * Session signature logger\n */\nexport function logSessionSignatures(count: number, expiration: string): void {\n debugInfo('SESSION', `\uD83D\uDD10 Generated ${count} session signatures (expires: ${expiration})`);\n}\n\n/**\n * LIT Action execution logger\n */\nexport function logLitActionExecution(cid: string, params: any): void {\n debugInfo('LIT_ACTION', `\u26A1 Executing LIT Action: ${cid}`, {\n cid,\n params\n });\n}\n\nexport function logLitActionResult(cid: string, success: boolean, duration: number, result?: any): void {\n const status = success ? '\u2705' : '\u274C';\n debugInfo('LIT_ACTION', `${status} LIT Action completed: ${cid} (${duration}ms)`, {\n cid,\n success,\n duration,\n result: success && result ? JSON.stringify(result, null, 2) : undefined\n });\n}\n\n/**\n * Test lifecycle logger\n */\nexport function logTestStart(testName: string): PerformanceTracker {\n debugInfo('TEST', `\uD83E\uDDEA Starting test: ${testName}`);\n return new PerformanceTracker(testName);\n}\n\nexport function logTestEnd(testName: string, success: boolean, duration: number): void {\n const status = success ? '\u2705' : '\u274C';\n debugInfo('TEST', `${status} Test completed: ${testName} (${duration}ms)`);\n networkMetrics.printSummary();\n}", "/**\n * Session Signature Cache for PKP Operations\n * Provides intelligent caching of session signatures to avoid regeneration\n */\n\nimport { debugInfo, debugWarn, debugLog } from './debug-logger';\n\n/**\n * Cache entry for session signatures\n */\ninterface SessionSignatureCacheEntry {\n sessionSigs: any;\n expiration: Date;\n pkpTokenId: string;\n litActionCid: string;\n signerAddress: string;\n network: string;\n createdAt: Date;\n}\n\n/**\n * Session signature cache configuration\n */\nexport interface SessionSignatureCacheConfig {\n maxEntries: number;\n bufferTimeMs: number; // Time before expiration to consider entry stale\n enableLogging: boolean;\n}\n\nexport const DEFAULT_CACHE_CONFIG: SessionSignatureCacheConfig = {\n maxEntries: 50,\n bufferTimeMs: 60000, // 1 minute buffer before expiration\n enableLogging: true\n};\n\n/**\n * In-memory session signature cache\n */\nclass SessionSignatureCache {\n private cache = new Map<string, SessionSignatureCacheEntry>();\n private config: SessionSignatureCacheConfig;\n\n constructor(config: Partial<SessionSignatureCacheConfig> = {}) {\n this.config = { ...DEFAULT_CACHE_CONFIG, ...config };\n }\n\n /**\n * Generate cache key from PKP and context\n */\n private generateCacheKey(\n pkpTokenId: string,\n litActionCid: string,\n signerAddress: string,\n network: string\n ): string {\n return `${signerAddress}-${network}-${pkpTokenId}-${litActionCid}`;\n }\n\n /**\n * Check if cache entry is still valid\n */\n private isEntryValid(entry: SessionSignatureCacheEntry): boolean {\n const now = new Date();\n const expirationWithBuffer = new Date(\n entry.expiration.getTime() - this.config.bufferTimeMs\n );\n\n return now < expirationWithBuffer;\n }\n\n /**\n * Clean expired entries from cache\n */\n private cleanExpiredEntries(): void {\n const now = new Date();\n let cleanedCount = 0;\n\n for (const [key, entry] of this.cache.entries()) {\n if (now >= entry.expiration) {\n this.cache.delete(key);\n cleanedCount++;\n }\n }\n\n if (cleanedCount > 0 && this.config.enableLogging) {\n debugInfo('CACHE', `\uD83E\uDDF9 Cleaned ${cleanedCount} expired session signature entries`);\n }\n }\n\n /**\n * Enforce cache size limits\n */\n private enforceCacheSize(): void {\n if (this.cache.size <= this.config.maxEntries) {\n return;\n }\n\n // Sort entries by creation time and remove oldest\n const entries = Array.from(this.cache.entries())\n .sort(([, a], [, b]) => a.createdAt.getTime() - b.createdAt.getTime());\n\n const toRemove = entries.slice(0, this.cache.size - this.config.maxEntries);\n\n for (const [key] of toRemove) {\n this.cache.delete(key);\n }\n\n if (this.config.enableLogging) {\n debugInfo('CACHE', `\uD83D\uDCE6 Removed ${toRemove.length} oldest cache entries to maintain size limit`);\n }\n }\n\n /**\n * Get cached session signatures if valid\n */\n get(\n pkpTokenId: string,\n litActionCid: string,\n signerAddress: string,\n network: string\n ): any | null {\n this.cleanExpiredEntries();\n\n const cacheKey = this.generateCacheKey(pkpTokenId, litActionCid, signerAddress, network);\n const entry = this.cache.get(cacheKey);\n\n if (!entry) {\n if (this.config.enableLogging) {\n debugLog('CACHE', `\u274C Session signatures cache miss: ${cacheKey}`);\n }\n return null;\n }\n\n if (!this.isEntryValid(entry)) {\n this.cache.delete(cacheKey);\n if (this.config.enableLogging) {\n debugWarn('CACHE', `\u23F0 Session signatures expired: ${cacheKey}`);\n }\n return null;\n }\n\n if (this.config.enableLogging) {\n const timeUntilExpiry = entry.expiration.getTime() - Date.now();\n debugInfo('CACHE', `\u2705 Session signatures cache hit: ${cacheKey} (expires in ${Math.round(timeUntilExpiry/1000)}s)`);\n }\n\n return entry.sessionSigs;\n }\n\n /**\n * Cache session signatures\n */\n set(\n pkpTokenId: string,\n litActionCid: string,\n signerAddress: string,\n network: string,\n sessionSigs: any,\n expiration: Date\n ): void {\n const cacheKey = this.generateCacheKey(pkpTokenId, litActionCid, signerAddress, network);\n\n const entry: SessionSignatureCacheEntry = {\n sessionSigs,\n expiration,\n pkpTokenId,\n litActionCid,\n signerAddress,\n network,\n createdAt: new Date()\n };\n\n this.cache.set(cacheKey, entry);\n\n if (this.config.enableLogging) {\n const timeUntilExpiry = expiration.getTime() - Date.now();\n debugInfo('CACHE', `\uD83D\uDCBE Cached session signatures: ${cacheKey} (expires in ${Math.round(timeUntilExpiry/1000)}s)`);\n }\n\n this.enforceCacheSize();\n }\n\n /**\n * Clear all cached session signatures\n */\n clear(): void {\n const size = this.cache.size;\n this.cache.clear();\n\n if (this.config.enableLogging) {\n debugInfo('CACHE', `\uD83D\uDDD1\uFE0F Cleared ${size} cached session signature entries`);\n }\n }\n\n /**\n * Get cache statistics\n */\n getStats() {\n this.cleanExpiredEntries();\n\n return {\n size: this.cache.size,\n maxEntries: this.config.maxEntries,\n utilizationPercent: Math.round((this.cache.size / this.config.maxEntries) * 100)\n };\n }\n\n /**\n * Remove specific cache entry\n */\n delete(\n pkpTokenId: string,\n litActionCid: string,\n signerAddress: string,\n network: string\n ): boolean {\n const cacheKey = this.generateCacheKey(pkpTokenId, litActionCid, signerAddress, network);\n const deleted = this.cache.delete(cacheKey);\n\n if (deleted && this.config.enableLogging) {\n debugInfo('CACHE', `\uD83D\uDDD1\uFE0F Removed session signatures from cache: ${cacheKey}`);\n }\n\n return deleted;\n }\n}\n\n// Global session signature cache instance\nexport const sessionSignatureCache = new SessionSignatureCache();\n\n/**\n * Enhanced session signature generator with caching\n */\nexport async function generateSessionSignaturesWithCache(\n litNodeClient: any,\n sessionConfig: any,\n pkpTokenId: string,\n litActionCid: string,\n signerAddress: string,\n network: string = 'chipotle'\n): Promise<any> {\n // Check cache first\n const cached = sessionSignatureCache.get(pkpTokenId, litActionCid, signerAddress, network);\n if (cached) {\n return cached;\n }\n\n // Generate new session signatures\n debugInfo('CACHE', '\uD83D\uDD10 Generating new session signatures (cache miss)...');\n const sessionSigs = await litNodeClient.getSessionSigs(sessionConfig);\n\n // Extract expiration from config\n const expiration = new Date(sessionConfig.expiration);\n\n // Cache the result\n sessionSignatureCache.set(pkpTokenId, litActionCid, signerAddress, network, sessionSigs, expiration);\n\n const sigCount = Object.keys(sessionSigs).length;\n debugInfo('CACHE', `\u2705 Generated and cached ${sigCount} session signatures`);\n\n return sessionSigs;\n}\n\n/**\n * Configure the global session signature cache\n */\nexport function configureSessionSignatureCache(config: Partial<SessionSignatureCacheConfig>): void {\n Object.assign(sessionSignatureCache['config'], config);\n}\n\n/**\n * Clear the global session signature cache\n */\nexport function clearSessionSignatureCache(): void {\n sessionSignatureCache.clear();\n}\n\n/**\n * Get session signature cache statistics\n */\nexport function getSessionSignatureCacheStats() {\n return sessionSignatureCache.getStats();\n}", "/**\n * dh-lit-actions Package\n * Diamond Hands Protocol LIT Actions Registry\n *\n * This package exports deployed LIT Action CIDs and metadata\n * for consumption by the Diamond Hands SDK and other applications.\n */\n\n// Export interfaces\nexport * from \"./interfaces\";\n\n// Export constants from registry\nexport * from \"./constants/chunks/lit-actions-registry\";\n\n// Export utilities\nexport * from \"./utils\";\n\n// Export NOLA executors (commented out - executors import from src/ which is outside rootDir)\n// export * from \"./executors\";\n\n// Default export\nexport { DH_LIT_ACTIONS_CHIPOTLE, CHIPOTLE_DEPLOYMENTS } from \"./constants/chunks/lit-actions-registry\";\n", "// base-x encoding / decoding\n// Copyright (c) 2018 base-x contributors\n// Copyright (c) 2014-2018 The Bitcoin Core developers (base58.cpp)\n// Distributed under the MIT software license, see the accompanying\n// file LICENSE or http://www.opensource.org/licenses/mit-license.php.\nfunction base (ALPHABET) {\n if (ALPHABET.length >= 255) { throw new TypeError('Alphabet too long') }\n const BASE_MAP = new Uint8Array(256)\n for (let j = 0; j < BASE_MAP.length; j++) {\n BASE_MAP[j] = 255\n }\n for (let i = 0; i < ALPHABET.length; i++) {\n const x = ALPHABET.charAt(i)\n const xc = x.charCodeAt(0)\n if (BASE_MAP[xc] !== 255) { throw new TypeError(x + ' is ambiguous') }\n BASE_MAP[xc] = i\n }\n const BASE = ALPHABET.length\n const LEADER = ALPHABET.charAt(0)\n const FACTOR = Math.log(BASE) / Math.log(256) // log(BASE) / log(256), rounded up\n const iFACTOR = Math.log(256) / Math.log(BASE) // log(256) / log(BASE), rounded up\n function encode (source) {\n // eslint-disable-next-line no-empty\n if (source instanceof Uint8Array) { } else if (ArrayBuffer.isView(source)) {\n source = new Uint8Array(source.buffer, source.byteOffset, source.byteLength)\n } else if (Array.isArray(source)) {\n source = Uint8Array.from(source)\n }\n if (!(source instanceof Uint8Array)) { throw new TypeError('Expected Uint8Array') }\n if (source.length === 0) { return '' }\n // Skip & count leading zeroes.\n let zeroes = 0\n let length = 0\n let pbegin = 0\n const pend = source.length\n while (pbegin !== pend && source[pbegin] === 0) {\n pbegin++\n zeroes++\n }\n // Allocate enough space in big-endian base58 representation.\n const size = ((pend - pbegin) * iFACTOR + 1) >>> 0\n const b58 = new Uint8Array(size)\n // Process the bytes.\n while (pbegin !== pend) {\n let carry = source[pbegin]\n // Apply \"b58 = b58 * 256 + ch\".\n let i = 0\n for (let it1 = size - 1; (carry !== 0 || i < length) && (it1 !== -1); it1--, i++) {\n carry += (256 * b58[it1]) >>> 0\n b58[it1] = (carry % BASE) >>> 0\n carry = (carry / BASE) >>> 0\n }\n if (carry !== 0) { throw new Error('Non-zero carry') }\n length = i\n pbegin++\n }\n // Skip leading zeroes in base58 result.\n let it2 = size - length\n while (it2 !== size && b58[it2] === 0) {\n it2++\n }\n // Translate the result into a string.\n let str = LEADER.repeat(zeroes)\n for (; it2 < size; ++it2) { str += ALPHABET.charAt(b58[it2]) }\n return str\n }\n function decodeUnsafe (source) {\n if (typeof source !== 'string') { throw new TypeError('Expected String') }\n if (source.length === 0) { return new Uint8Array() }\n let psz = 0\n // Skip and count leading '1's.\n let zeroes = 0\n let length = 0\n while (source[psz] === LEADER) {\n zeroes++\n psz++\n }\n // Allocate enough space in big-endian base256 representation.\n const size = (((source.length - psz) * FACTOR) + 1) >>> 0 // log(58) / log(256), rounded up.\n const b256 = new Uint8Array(size)\n // Process the characters.\n while (psz < source.length) {\n // Find code of next character\n const charCode = source.charCodeAt(psz)\n // Base map can not be indexed using char code\n if (charCode > 255) { return }\n // Decode character\n let carry = BASE_MAP[charCode]\n // Invalid character\n if (carry === 255) { return }\n let i = 0\n for (let it3 = size - 1; (carry !== 0 || i < length) && (it3 !== -1); it3--, i++) {\n carry += (BASE * b256[it3]) >>> 0\n b256[it3] = (carry % 256) >>> 0\n carry = (carry / 256) >>> 0\n }\n if (carry !== 0) { throw new Error('Non-zero carry') }\n length = i\n psz++\n }\n // Skip leading zeroes in b256.\n let it4 = size - length\n while (it4 !== size && b256[it4] === 0) {\n it4++\n }\n const vch = new Uint8Array(zeroes + (size - it4))\n let j = zeroes\n while (it4 !== size) {\n vch[j++] = b256[it4++]\n }\n return vch\n }\n function decode (string) {\n const buffer = decodeUnsafe(string)\n if (buffer) { return buffer }\n throw new Error('Non-base' + BASE + ' character')\n }\n return {\n encode,\n decodeUnsafe,\n decode\n }\n}\nexport default base\n", "import basex from 'base-x';\nvar ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';\nexport default basex(ALPHABET);\n", "import bs58 from \"bs58\";\n\n/**\n * Convert IPFS CID from base58 to hex format for contract authorization\n * This matches the format expected by LIT Protocol smart contracts\n */\nexport function cidToHex(cid: string): string {\n try {\n if (cid.startsWith(\"Qm\")) {\n // v0 CID - convert base58 to hex\n const bytes = bs58.decode(cid);\n return \"0x\" + Buffer.from(bytes).toString(\"hex\");\n } else if (cid.startsWith(\"0x\")) {\n // Already in hex format\n return cid;\n } else {\n // Unknown format - FAIL FAST\n throw new Error(`Unsupported CID format: ${cid}. Only CIDv0 (Qm...) and hex (0x...) formats are supported.`);\n }\n } catch (error) {\n if (error instanceof Error && error.message.includes('Unsupported CID format')) {\n // Re-throw our own errors\n throw error;\n }\n // bs58 decode or other conversion errors\n throw new Error(`Failed to convert CID ${cid} to hex: ${error instanceof Error ? error.message : String(error)}`);\n }\n}\n", "import { DiamondHandsLitActions } from \"../../interfaces\";\nimport { cidToHex } from \"../../utils/chunks/cid-utils\";\n\nexport type SupportedLitNetwork = \"chipotle\";\n\n/**\n * Diamond Hands LIT Actions Registry \u2014 Chipotle (new REST API)\n * Contains all deployed LIT Actions with their IPFS CIDs and PKP metadata\n */\nexport const DH_LIT_ACTIONS_CHIPOTLE: Partial<DiamondHandsLitActions> = {\n authorizationDummy: {\n cid: \"QmXPh5RTReLqjqZDRNhgVKi1TWMJK2vS9QSRPkWPhLBQJN\",\n authorizedCidHex: cidToHex(\n \"QmXPh5RTReLqjqZDRNhgVKi1TWMJK2vS9QSRPkWPhLBQJN\",\n ),\n name: \"Authorization Dummy\",\n description: \"Production Authorization Dummy\",\n version: \"1.0.0\",\n deployed: true,\n deployedAt: 1776707622939,\n size: 2026,\n hash: \"349d51aa096d56b394f82de75ed34cd4eef72719969f7e5fe584f4582b2745fa\",\n },\n authorizationDummyB: {\n cid: \"QmQXTcAU9tr4YAVrzHujEJw9moJgM1o5gnJU2ojcvhigua\",\n authorizedCidHex: cidToHex(\n \"QmQXTcAU9tr4YAVrzHujEJw9moJgM1o5gnJU2ojcvhigua\",\n ),\n name: \"Authorization Dummy B\",\n description: \"Production Authorization Dummy B\",\n version: \"1.0.0\",\n deployed: true,\n deployedAt: 1776707632987,\n size: 1695,\n hash: \"def4576ba6c382d145f996c4f950b5e39e58792415ee7fdd749e0d2bc61e8f0b\",\n },\n pkpValidator: {\n cid: \"QmTqsmXkjib7CRGFcEhmCs61Z3EZTaRPznaZDDLk5Wq9NX\",\n authorizedCidHex: cidToHex(\n \"QmTqsmXkjib7CRGFcEhmCs61Z3EZTaRPznaZDDLk5Wq9NX\",\n ),\n name: \"Pkp Validator\",\n description: \"Production Pkp Validator\",\n version: \"1.0.0\",\n deployed: true,\n deployedAt: 1777311468630,\n size: 3999,\n hash: \"baba8c31b73dbfe6425d8f9fd69c61f41a573e189a185962382b815a15fddb24\",\n validatorWalletAddress: \"0x9ee56687f7a984d5df7ab79330271a619ef56ddd\",\n pkp: {\n publicKey:\n \"0x04ec0e3acc40884e3f366ca469d08899eb640ad5259fa196b0238075086208a9f8732a3175704f35f0bcc28e07406cfb24c254efd1e7355640577330f0d2dd755f\",\n ethAddress: \"0x9eE56687F7a984D5DF7aB79330271a619ef56DdD\",\n },\n },\n btcTransactionSigner: {\n cid: \"QmZyn4MjYwrtVHfHJJ8cDnQLaQxkWKArnKzx5UaDV26rmt\",\n authorizedCidHex: cidToHex(\n \"QmZyn4MjYwrtVHfHJJ8cDnQLaQxkWKArnKzx5UaDV26rmt\",\n ),\n name: \"Btc Transaction Signer\",\n description: \"Production Btc Transaction Signer\",\n version: \"1.0.0\",\n deployed: true,\n deployedAt: 1777493882468,\n size: 25656,\n hash: \"c1dc107c830449d55e05a2777c072d7e0c62e2bfec4406d5d1bc8dab61eadf91\",\n },\n ucdMintValidator: {\n cid: \"QmUuQHr21xq7EVBVS4AM3TxU1LBC7G9yvsXdXjPFHJQVw7\",\n authorizedCidHex: cidToHex(\n \"QmUuQHr21xq7EVBVS4AM3TxU1LBC7G9yvsXdXjPFHJQVw7\",\n ),\n name: \"Ucd Mint Validator\",\n description: \"Production Ucd Mint Validator\",\n version: \"1.0.0\",\n deployed: true,\n deployedAt: 1777493891124,\n size: 65506,\n hash: \"a42096083c705332dda82d5e669e5e1e258c1d9da0203593f6502a14d61f1fb7\",\n validatorWalletAddress: \"0x36f3dd61c4c08a56d29ed2fd6d5f111b67b6a7a1\",\n pkp: {\n publicKey:\n \"0x041ab17cd91fc5c0b761eea6092d032807561b621b82c488826776e04a9158d61ba64d809a4729f0501289d980732cb06d8dfd06999dd9c8efd2f495dad78b31bb\",\n ethAddress: \"0x4D9299055093938d0CD2F26C42A87260CB50adD8\",\n },\n },\n processPaymentValidator: {\n cid: \"QmR8949WAtUM9kYRMwesgLXmZjTwFBmogo921MNFNKXa5Z\",\n authorizedCidHex: cidToHex(\n \"QmR8949WAtUM9kYRMwesgLXmZjTwFBmogo921MNFNKXa5Z\",\n ),\n name: \"Process Payment Validator\",\n description: \"Production Process Payment Validator\",\n version: \"1.0.0\",\n deployed: true,\n deployedAt: 1778105262871,\n size: 61414,\n hash: \"2a3aa2ebb2fb379f3f4331457848d82ab3ef5429d4bc483132cf1c04480c9ffc\",\n validatorWalletAddress: \"0xc2ed5a59bd81eb4d1a19a0147b86ab69aa4c28ba\",\n pkp: {\n publicKey:\n \"0x04cfccb9b6c7addc79469c0e9fafebef4c70801302c62cdb26b610723a9b8527d23f98a40158c7340d83fd0e62d02b5ec74c12f7a86d98e8c6973b4811110f19ad\",\n ethAddress: \"0x440Fd7B157766b95eb551825C06DF4f92E41D55b\",\n },\n },\n extendPositionValidator: {\n cid: \"QmV2uyKWNY54HHoC5FggF4nFqGNoDydQ6ceExA8ztmjHwz\",\n authorizedCidHex: cidToHex(\n \"QmV2uyKWNY54HHoC5FggF4nFqGNoDydQ6ceExA8ztmjHwz\",\n ),\n name: \"Extend Position Validator\",\n description: \"Production Extend Position Validator\",\n version: \"1.0.0\",\n deployed: true,\n deployedAt: 1777496426914,\n size: 60611,\n hash: \"d2ee66d7fd30762d637d54f1dedb40d5d7bfe7dac9a92201e60b62de30e6aa80\",\n validatorWalletAddress: \"0xb3f4271c475887a86a2f21446f0968c30cc74c97\",\n pkp: {\n publicKey:\n \"0x04e52338f6a0c3362800f1a94c4c8f08be5030dde0fa217388c53d1c1a4790f4443c9f2886b2d2d8f4b75a9a156e9b028011b1a1cf7648da0e556aa980ec692836\",\n ethAddress: \"0x4cb7651Ba27610991A5775486B5F4487F3Dd7cDA\",\n },\n },\n btcWithdrawal: {\n cid: \"QmdaeZ3inL9LiSkSAYV81ohqdYJ24n7awEpitHDHXKaJUm\",\n authorizedCidHex: cidToHex(\n \"QmdaeZ3inL9LiSkSAYV81ohqdYJ24n7awEpitHDHXKaJUm\",\n ),\n name: \"Btc Withdrawal\",\n description: \"Production Btc Withdrawal\",\n version: \"1.0.0\",\n deployed: true,\n deployedAt: 1777551892935,\n size: 72300,\n hash: \"888eef2c01d71a20fc6734fab41ec9f4eda4941774fe23ac44d1fcb6c21a8807\",\n validatorWalletAddress: \"0xbb137fbda353199e9419b698c57a742124d4987d\",\n pkp: {\n publicKey:\n \"0x043616787c5432415c24378c4ef48de2bcd6bb7b575b837e3cff09171802662da7105d79586c7659677a0ecbaddac4cce06cb2a11f69a16fa0c4d7002ac7d51a4d\",\n ethAddress: \"0x7b9316cAA00B257F7CE30F7F6979bd6867BA9eE2\",\n },\n },\n liquidationValidator: {\n cid: \"QmaDsh5t5J6s1TgYeoW8PPZXM1gniZWW3jfPyg1MHjPQ7Q\",\n authorizedCidHex: cidToHex(\n \"QmaDsh5t5J6s1TgYeoW8PPZXM1gniZWW3jfPyg1MHjPQ7Q\",\n ),\n name: \"Liquidation Validator\",\n description: \"Production Liquidation Validator\",\n version: \"1.0.0\",\n deployed: true,\n deployedAt: 1777493931019,\n size: 54906,\n hash: \"ffe0b955b64973aa155cc29dc9aeceb72a36459d5a2c734dca872ba936db6f8e\",\n validatorWalletAddress: \"0x1542f863ee0340f6a067573e80ae66dd9b3838fa\",\n pkp: {\n publicKey:\n \"0x041a241de58976c13e3d7ca5a18e494f7330151e3818706135d50176e78ffb673e58b11f0b67a87c73fade6c6b27d6e7a7377ea9f4c10bf31880f1d725f18cc6f8\",\n ethAddress: \"0x8770470620Db25bBf2BFBca1eF7Bdd6CF91ab1b5\",\n },\n },\n priceOracle: {\n cid: \"Qma6vTTnn3AFy3A4u3A7T5tT6eEp26vXt7rPHSWpmBpdsp\",\n authorizedCidHex: cidToHex(\n \"Qma6vTTnn3AFy3A4u3A7T5tT6eEp26vXt7rPHSWpmBpdsp\",\n ),\n name: \"Price Oracle\",\n description: \"Production Price Oracle (Chipotle main+getPrivateKey)\",\n version: \"1.1.0\",\n deployed: true,\n deployedAt: 1777311530589,\n size: 11531,\n hash: \"a49e9a4875e68a1b8f1c37bcc8284a28e0995652a7df8bad4f1853408d1c81c8\",\n validatorWalletAddress: \"0x831ddf3048547b983efe3ccbbb35a45a53191651\",\n pkp: {\n publicKey:\n \"0x043beaa1da47875601a8d6f430b1438ee2d4732eefd06e5c63a474bbdaf9e7df1797b1b951e041b627c2bf839788164825324c9cbd1e2d6f740fdd4bff69b1900b\",\n ethAddress: \"0x831DdF3048547B983EFE3ccBBB35a45a53191651\",\n },\n },\n loanVaultBtcBalance: {\n cid: \"QmWGEg79snhDQ5Fkm9pLhNmLKUJY5uXfp3t3276Jybritq\",\n authorizedCidHex: cidToHex(\n \"QmWGEg79snhDQ5Fkm9pLhNmLKUJY5uXfp3t3276Jybritq\",\n ),\n name: \"Loan Vault Btc Balance\",\n description: \"Production Loan Vault Btc Balance\",\n version: \"1.0.0\",\n deployed: true,\n deployedAt: 1777311539409,\n size: 44293,\n hash: \"70b009f01b9051dee2db1e4030dd17d08dc53728d30b5e24cffd11d07acfcaf0\",\n validatorWalletAddress: \"0x47feea74c8739d24b44faa67217f2f99970e15e7\",\n pkp: {\n publicKey:\n \"0x043616787c5432415c24378c4ef48de2bcd6bb7b575b837e3cff09171802662da7105d79586c7659677a0ecbaddac4cce06cb2a11f69a16fa0c4d7002ac7d51a4d\",\n ethAddress: \"0x7b9316cAA00B257F7CE30F7F6979bd6867BA9eE2\",\n },\n },\n adminLiquidationValidator: {\n cid: \"QmdeeDPqjh59HG3AF8LrvQkM8n3wbirRyyKBoY3XAzmi6b\",\n authorizedCidHex: cidToHex(\n \"QmdeeDPqjh59HG3AF8LrvQkM8n3wbirRyyKBoY3XAzmi6b\",\n ),\n name: \"Admin Liquidation Validator\",\n description: \"Production Admin Liquidation Validator\",\n version: \"1.0.0\",\n deployed: true,\n deployedAt: 1777493941746,\n size: 51555,\n hash: \"404fc4620f6b2ff31f233cdbe18290992498e63e45f38da76e3b6e7826e435b4\",\n validatorWalletAddress: \"0x2a83b78842d75358877deb8854ccc5bbbaa5012e\",\n pkp: {\n publicKey:\n \"0x04ee4316afb8538280ede6c56def9cbe791f18f96ccba97768b28e2966d9c46a9493a6b81f4571495c3ce56c2d0b33fe8cf63726cd12d395832ea54162b5c67801\",\n ethAddress: \"0x038B7a1918ae9121bC641236b7946a069BE21591\",\n },\n },\n alwaysSigner: {\n cid: \"QmXQjybSsdE19Da66to9Hh7X2dLFwpZPhjoqojxbpk7Nvs\",\n authorizedCidHex: cidToHex(\n \"QmXQjybSsdE19Da66to9Hh7X2dLFwpZPhjoqojxbpk7Nvs\",\n ),\n name: \"Always Signer\",\n description: \"Production Always Signer\",\n version: \"1.0.0\",\n deployed: true,\n deployedAt: 1776708176283,\n size: 4143,\n hash: \"25007f80b4ed3169470bf617d403971136be431eb29dfb09f78afe2494bf0138\",\n },\n};\n\nexport function getDeploymentsForNetwork(\n network: SupportedLitNetwork,\n): Partial<DiamondHandsLitActions> {\n if (network === \"chipotle\") return DH_LIT_ACTIONS_CHIPOTLE;\n throw new Error(`Unsupported LIT network: ${network}`);\n}\n\nexport const CHIPOTLE_DEPLOYMENTS = DH_LIT_ACTIONS_CHIPOTLE;\n", "import { LitActionConfig, LitActionName } from \"../../interfaces\";\nimport {\n getDeploymentsForNetwork,\n SupportedLitNetwork,\n} from \"../../constants/chunks/lit-actions-registry\";\n\n/**\n * Get LIT Action CID by name\n * @param actionName - Name of the LIT Action\n * @returns IPFS CID of the deployed action\n * @throws Error if action is not deployed\n */\nexport function getLitActionCID(\n litNetwork: SupportedLitNetwork = \"chipotle\",\n actionName: LitActionName,\n): string {\n const deployments = getDeploymentsForNetwork(litNetwork);\n const action = deployments[actionName];\n\n if (!action || !action.deployed || !action.cid) {\n throw new Error(\n `LIT Action '${actionName}' not deployed on network '${litNetwork}'. Check deployment status.`,\n );\n }\n return action.cid;\n}\n\n/**\n * Get LIT Action configuration by name\n * @param actionName - Name of the LIT Action\n * @returns Complete LIT Action configuration\n */\nexport function getLitActionConfig(\n actionName: LitActionName,\n litNetwork: SupportedLitNetwork = \"chipotle\",\n): LitActionConfig {\n const deployments = getDeploymentsForNetwork(litNetwork);\n const action = deployments[actionName];\n if (!action) {\n throw new Error(\n `LIT Action '${actionName}' not found in registry for network '${litNetwork}'.`,\n );\n }\n return action;\n}\n\n/**\n * Check if a LIT Action is deployed and ready to use\n * @param actionName - Name of the LIT Action\n * @returns True if deployed, false otherwise\n */\nexport function isLitActionDeployed(\n actionName: LitActionName,\n litNetwork: SupportedLitNetwork = \"chipotle\",\n): boolean {\n const deployments = getDeploymentsForNetwork(litNetwork);\n const action = deployments[actionName];\n return !!(action && action.deployed && action.cid);\n}\n\n/**\n * Validate that all required actions are deployed\n * @param requiredActions - Array of required action names\n * @returns True if all actions are deployed\n */\nexport function validateDeployedActions(\n requiredActions: LitActionName[],\n litNetwork: SupportedLitNetwork = \"chipotle\",\n): boolean {\n return requiredActions.every((actionName) =>\n isLitActionDeployed(actionName, litNetwork),\n );\n}\n\n/**\n * Get all deployed LIT Actions\n * @returns Array of deployed LIT Action configurations\n */\nexport function getDeployedActions(\n litNetwork: SupportedLitNetwork = \"chipotle\",\n): LitActionConfig[] {\n const deployments = getDeploymentsForNetwork(litNetwork);\n return Object.values(deployments).filter(\n (action) => action && action.deployed,\n );\n}\n", "import {\n getDeploymentsForNetwork,\n SupportedLitNetwork,\n} from \"../../constants/chunks/lit-actions-registry\";\n/**\n * Get the complete setup for PKP Validator testing\n * Returns both the LIT Action config and its associated PKP\n */\nexport function getPKPValidatorSetup(\n litNetwork: SupportedLitNetwork = \"chipotle\"\n) {\n const deployments = getDeploymentsForNetwork(litNetwork);\n const litAction = deployments.pkpValidator;\n\n if (!litAction || !litAction.pkp) {\n throw new Error(\n `PKP validator setup is not available for litNetwork='${litNetwork}'. ` +\n `Ensure the pkpValidator action (with pkp attached) is defined in the registry.`\n );\n }\n\n return {\n litAction,\n pkp: litAction.pkp!,\n };\n}\n", "/**\n * LIT Protocol Connection Helpers\n * Provides retry logic, error handling, and connection validation utilities\n * for robust LIT Protocol operations\n */\n\nimport {\n debugInfo,\n debugWarn,\n debugError,\n logConnectionAttempt,\n logConnectionSuccess,\n logConnectionFailure,\n logRequest,\n logResponse,\n logError,\n logPkpOperation,\n logLitActionExecution,\n logLitActionResult,\n networkMetrics,\n PerformanceTracker\n} from './debug-logger';\nimport {\n classifyError,\n getRetryConfigFromClassification,\n createErrorReport,\n litProtocolErrors\n} from './error-classification';\n\n/**\n * Retry configuration options\n */\nexport interface RetryConfig {\n maxAttempts: number;\n initialDelayMs: number;\n maxDelayMs: number;\n backoffMultiplier: number;\n retryableErrors: string[];\n}\n\n/**\n * Default retry configuration optimized for LIT Protocol operations\n */\nexport const DEFAULT_RETRY_CONFIG: RetryConfig = {\n maxAttempts: 5, // Increased from 3 for PKP operations\n initialDelayMs: 2000, // Increased from 1000ms for PKP operations\n maxDelayMs: 60000, // Increased from 30000ms for PKP operations\n backoffMultiplier: 2,\n retryableErrors: [\n 'request_timeout',\n 'network error',\n 'connection failed',\n 'ENOTFOUND',\n 'ECONNREFUSED',\n 'ETIMEDOUT',\n 'socket hang up',\n 'timeout',\n // PKP-specific errors\n 'signing shares',\n 'There was an error getting the signing shares',\n 'PKP validation',\n 'pkp validation failed',\n 'resource validation',\n 'ipfs propagation',\n 'session.*expired',\n 'NodeError',\n 'Response from the nodes',\n 'consensus',\n 'threshold'\n ]\n};\n\n/**\n * Connection validation result\n */\nexport interface ConnectionValidationResult {\n isValid: boolean;\n error?: string;\n latencyMs?: number;\n}\n\n/**\n * Sleep utility for retry delays\n */\nexport function sleep(ms: number): Promise<void> {\n return new Promise(resolve => setTimeout(resolve, ms));\n}\n\n/**\n * Check if an error is retryable using enhanced classification system\n */\nexport function isRetryableError(error: any, retryableErrors: string[] = DEFAULT_RETRY_CONFIG.retryableErrors): boolean {\n if (!error) return false;\n\n // Use enhanced error classification for LIT Protocol specific errors\n const classification = classifyError(error);\n\n // If classification determined retryability, use that\n if (classification.isRetryable !== undefined) {\n debugInfo('ERROR_CLASSIFICATION', `Error classified as: ${classification.category} (retryable: ${classification.isRetryable})`);\n return classification.isRetryable;\n }\n\n // Fallback to legacy pattern matching\n const errorMessage = error.message || error.toString() || '';\n const errorLower = errorMessage.toLowerCase();\n\n return retryableErrors.some(pattern =>\n errorLower.includes(pattern.toLowerCase())\n );\n}\n\n/**\n * Calculate exponential backoff delay with jitter\n */\nexport function calculateDelay(attempt: number, config: RetryConfig): number {\n const exponentialDelay = Math.min(\n config.initialDelayMs * Math.pow(config.backoffMultiplier, attempt - 1),\n config.maxDelayMs\n );\n\n // Add jitter (\u00B125%) to avoid thundering herd\n const jitter = exponentialDelay * 0.25 * (Math.random() - 0.5);\n return Math.max(100, exponentialDelay + jitter);\n}\n\n/**\n * Enhanced retry wrapper with intelligent error classification\n */\nexport async function withRetry<T>(\n operation: () => Promise<T>,\n config: Partial<RetryConfig> = {},\n operationName: string = 'operation'\n): Promise<T> {\n let finalConfig = { ...DEFAULT_RETRY_CONFIG, ...config };\n const tracker = new PerformanceTracker(`Retry ${operationName}`);\n let lastError: any;\n let errorClassification: any = null;\n\n for (let attempt = 1; attempt <= finalConfig.maxAttempts; attempt++) {\n try {\n debugInfo('RETRY', `\uD83D\uDD04 [${operationName}] Attempt ${attempt}/${finalConfig.maxAttempts}`);\n const startTime = Date.now();\n const result = await operation();\n const duration = Date.now() - startTime;\n\n debugInfo('RETRY', `\u2705 [${operationName}] Success after ${duration}ms (attempt ${attempt})`);\n networkMetrics.recordRequest(operationName, true, duration);\n tracker.end(true);\n return result;\n } catch (error) {\n lastError = error;\n const errorMessage = (error as any)?.message || (error as any)?.toString() || 'Unknown error';\n\n // Classify error and potentially adjust retry strategy\n errorClassification = classifyError(error);\n\n // Update config with intelligent retry settings if this is the first error\n if (attempt === 1 && errorClassification) {\n const intelligentConfig = getRetryConfigFromClassification(errorClassification);\n finalConfig = {\n ...finalConfig,\n ...intelligentConfig,\n maxAttempts: Math.min(finalConfig.maxAttempts, intelligentConfig.maxAttempts)\n };\n\n debugInfo('RETRY', `\uD83E\uDDE0 Intelligent retry strategy applied: ${errorClassification.category} (${errorClassification.retryStrategy})`);\n debugInfo('RETRY', `\uD83D\uDCCA Adjusted config: max=${finalConfig.maxAttempts}, delay=${finalConfig.initialDelayMs}ms`);\n }\n\n debugWarn('RETRY', `\u26A0\uFE0F [${operationName}] Attempt ${attempt} failed:`, {\n error: errorMessage,\n attempt,\n category: errorClassification.category,\n retryable: errorClassification.isRetryable\n });\n networkMetrics.recordRequest(operationName, false, 0, errorMessage);\n\n // Check if we've exceeded the (possibly adjusted) max attempts\n if (attempt >= finalConfig.maxAttempts) {\n debugError('RETRY', `\u274C [${operationName}] All attempts failed. Last error:`, { error: errorMessage });\n\n // Generate detailed error report\n if (errorClassification) {\n const errorReport = createErrorReport(error);\n debugError('RETRY', `\uD83D\uDCCB Error Analysis Report:\\n${errorReport}`);\n }\n\n tracker.end(false);\n break;\n }\n\n if (!errorClassification.isRetryable) {\n debugError('RETRY', `\uD83D\uDEAB [${operationName}] Non-retryable error (${errorClassification.category}):`, { error: errorMessage });\n\n // Show troubleshooting tips for non-retryable errors\n if (errorClassification.troubleshooting) {\n debugInfo('RETRY', `\uD83D\uDCA1 Troubleshooting suggestions:\\n ${errorClassification.troubleshooting.join('\\n ')}`);\n }\n\n tracker.end(false);\n break;\n }\n\n const delayMs = calculateDelay(attempt, finalConfig);\n debugInfo('RETRY', `\u23F3 [${operationName}] Waiting ${delayMs}ms before retry... (${errorClassification.retryStrategy} strategy)`);\n await sleep(delayMs);\n }\n }\n\n throw lastError;\n}\n\n/**\n * Enhanced LIT Node Client connector with retry logic and readiness validation\n */\nexport async function connectLitNodeClient(\n litNodeClient: any,\n config: Partial<RetryConfig> = {}\n): Promise<void> {\n const network = litNodeClient?.config?.litNetwork || 'unknown';\n\n return withRetry(\n async () => {\n if (!litNodeClient) {\n throw new Error('LIT Node Client is null or undefined');\n }\n\n const startTime = Date.now();\n logConnectionAttempt(network, 1, config.maxAttempts || DEFAULT_RETRY_CONFIG.maxAttempts);\n\n // First connect to the network\n await litNodeClient.connect();\n\n // Wait for client to be ready\n debugInfo('CONNECTION', `\u23F3 Waiting for LIT Node Client to be ready...`);\n const readyTimeout = 30000; // 30 seconds timeout for readiness\n const readyStartTime = Date.now();\n\n while (!litNodeClient.ready && (Date.now() - readyStartTime) < readyTimeout) {\n await sleep(500); // Check every 500ms\n }\n\n if (!litNodeClient.ready) {\n throw new Error(`LIT Node Client failed to become ready within ${readyTimeout}ms`);\n }\n\n const latency = Date.now() - startTime;\n debugInfo('CONNECTION', `\u2705 LIT Node Client is ready (${litNodeClient.ready})`);\n logConnectionSuccess(network, latency);\n },\n config,\n 'LIT Node Client Connection'\n );\n}\n\n/**\n * Enhanced LIT Contracts connector with retry logic\n */\nexport async function connectLitContracts(\n litContracts: any,\n config: Partial<RetryConfig> = {}\n): Promise<void> {\n return withRetry(\n async () => {\n if (!litContracts) {\n throw new Error('LIT Contracts is null or undefined');\n }\n\n console.log(`\uD83D\uDD17 Connecting to LIT Contracts: ${litContracts.network || 'unknown'}`);\n await litContracts.connect();\n console.log('\u2705 LIT Contracts connected successfully');\n },\n config,\n 'LIT Contracts Connection'\n );\n}\n\n/**\n * Validate LIT Node Client connection health\n */\nexport async function validateLitNodeConnection(litNodeClient: any): Promise<ConnectionValidationResult> {\n try {\n const startTime = Date.now();\n\n if (!litNodeClient) {\n return { isValid: false, error: 'LIT Node Client is null or undefined' };\n }\n\n // Try to get the latest block hash as a health check\n await litNodeClient.getLatestBlockhash();\n\n const latencyMs = Date.now() - startTime;\n return { isValid: true, latencyMs };\n } catch (error) {\n return {\n isValid: false,\n error: (error as any)?.message || (error as any)?.toString() || 'Unknown validation error'\n };\n }\n}\n\n/**\n * Enhanced session signature generator with retry logic and caching\n */\nexport async function generateSessionSignatures(\n litNodeClient: any,\n sessionConfig: any,\n config: Partial<RetryConfig> = {},\n pkpTokenId?: string,\n litActionCid?: string,\n signerAddress?: string,\n network: string = 'chipotle'\n): Promise<any> {\n // If caching parameters are provided, try cache first\n if (pkpTokenId && litActionCid && signerAddress) {\n const { sessionSignatureCache } = await import('./session-signature-cache');\n\n const cached = sessionSignatureCache.get(pkpTokenId, litActionCid, signerAddress, network);\n if (cached) {\n const sigCount = Object.keys(cached).length;\n debugInfo('SESSION', `\uD83D\uDD04 Using cached session signatures (${sigCount} sigs)`);\n return cached;\n }\n }\n\n return withRetry(\n async () => {\n debugInfo('SESSION', '\uD83D\uDD10 Generating new session signatures...');\n const sessionSigs = await litNodeClient.getSessionSigs(sessionConfig);\n const sigCount = Object.keys(sessionSigs).length;\n\n // Cache if parameters provided\n if (pkpTokenId && litActionCid && signerAddress) {\n const { sessionSignatureCache } = await import('./session-signature-cache');\n const expiration = new Date(sessionConfig.expiration);\n sessionSignatureCache.set(pkpTokenId, litActionCid, signerAddress, network, sessionSigs, expiration);\n }\n\n debugInfo('SESSION', `\u2705 Generated ${sigCount} session signatures`);\n return sessionSigs;\n },\n config,\n 'Session Signature Generation'\n );\n}\n\n/**\n * Validate LIT Node Client readiness before operations\n */\nexport async function validateLitNodeReadiness(litNodeClient: any): Promise<void> {\n if (!litNodeClient) {\n throw new Error('LIT Node Client is null or undefined');\n }\n\n // Check if client is connected\n if (!litNodeClient.ready) {\n debugWarn('CONNECTION', '\u26A0\uFE0F LIT Node Client not ready, attempting to reconnect...');\n await litNodeClient.connect();\n\n // Wait for readiness with timeout\n const readyTimeout = 15000; // 15 seconds\n const startTime = Date.now();\n\n while (!litNodeClient.ready && (Date.now() - startTime) < readyTimeout) {\n await sleep(500);\n }\n\n if (!litNodeClient.ready) {\n throw new Error('LIT Node Client is not ready for operations');\n }\n }\n\n debugInfo('CONNECTION', '\u2705 LIT Node Client readiness validated');\n}\n\n/**\n * Enhanced LIT Action executor with retry logic and readiness validation\n */\nexport async function executeLitAction(\n litNodeClient: any,\n executionConfig: any,\n config: Partial<RetryConfig> = {}\n): Promise<any> {\n const cid = executionConfig.ipfsId;\n\n return withRetry(\n async () => {\n const startTime = Date.now();\n\n // Validate client readiness before execution\n await validateLitNodeReadiness(litNodeClient);\n\n logLitActionExecution(cid, executionConfig.jsParams);\n logRequest('LIT_ACTION', 'executeJs', executionConfig);\n\n const result = await litNodeClient.executeJs(executionConfig);\n const duration = Date.now() - startTime;\n\n if (!result.response) {\n logError('LIT_ACTION', 'executeJs', new Error('No response from LIT Action'), duration);\n throw new Error('No response from LIT Action');\n }\n\n logResponse('LIT_ACTION', 'executeJs', result, duration);\n logLitActionResult(cid, true, duration, result);\n return result;\n },\n config,\n 'LIT Action Execution'\n );\n}\n\n/**\n * PKP operation wrapper with enhanced error handling\n */\nexport async function executePkpOperation<T>(\n operation: () => Promise<T>,\n operationName: string,\n config: Partial<RetryConfig> = {}\n): Promise<T> {\n const pkpConfig = {\n ...DEFAULT_RETRY_CONFIG,\n ...config,\n retryableErrors: [\n ...DEFAULT_RETRY_CONFIG.retryableErrors,\n 'pkp validation',\n 'signing shares',\n 'resource validation',\n 'ipfs propagation'\n ]\n };\n\n return withRetry(operation, pkpConfig, `PKP ${operationName}`);\n}\n\n/**\n * Network-specific timeout configurations\n */\nexport const NETWORK_TIMEOUTS = {\n chipotle: {\n connection: 60000,\n operation: 120000,\n pkpValidation: 180000,\n },\n} as const;\n\nexport function getNetworkTimeouts(network: string) {\n return NETWORK_TIMEOUTS[network as keyof typeof NETWORK_TIMEOUTS] || NETWORK_TIMEOUTS['chipotle'];\n}", "/**\n * Enhanced Error Classification for LIT Protocol Operations\n * Provides detailed error categorization and specific retry strategies\n */\n\n/**\n * Error categories for different types of failures\n */\nexport enum ErrorCategory {\n NETWORK = 'network',\n PROTOCOL = 'protocol',\n AUTHENTICATION = 'authentication',\n RESOURCE = 'resource',\n VALIDATION = 'validation',\n TIMEOUT = 'timeout',\n RATE_LIMIT = 'rate_limit',\n CONFIGURATION = 'configuration',\n UNKNOWN = 'unknown'\n}\n\n/**\n * Retry strategy types\n */\nexport enum RetryStrategy {\n AGGRESSIVE = 'aggressive', // Fast retries for transient issues\n CONSERVATIVE = 'conservative', // Slower retries for resource issues\n EXPONENTIAL = 'exponential', // Standard exponential backoff\n LINEAR = 'linear', // Linear delay increase\n NONE = 'none' // No retries\n}\n\n/**\n * Error classification result\n */\nexport interface ErrorClassification {\n category: ErrorCategory;\n retryStrategy: RetryStrategy;\n isRetryable: boolean;\n maxAttempts: number;\n baseDelayMs: number;\n maxDelayMs: number;\n description: string;\n troubleshooting?: string[];\n}\n\n/**\n * LIT Protocol specific error patterns\n */\nconst LIT_ERROR_PATTERNS = {\n // Network and connection errors\n network: [\n /network error/i,\n /connection failed/i,\n /connection refused/i,\n /connection timeout/i,\n /socket hang up/i,\n /enotfound/i,\n /econnrefused/i,\n /etimedout/i,\n /dns resolution failed/i,\n /request failed/i\n ],\n\n // Protocol specific errors\n protocol: [\n /lit protocol/i,\n /lit node/i,\n /signing shares/i,\n /node communication/i,\n /consensus/i,\n /threshold/i,\n /decryption/i,\n /signature verification/i,\n /There was an error getting the signing shares/i,\n /Response from the nodes/i,\n /NodeError/i,\n /PKP validation/i,\n /pkp validation failed/i\n ],\n\n // Authentication and authorization errors\n authentication: [\n /authentication/i,\n /authorization/i,\n /session.*expired/i,\n /invalid.*signature/i,\n /unauthorized/i,\n /access.*denied/i,\n /permission/i,\n /auth.*failed/i,\n /siwe/i,\n /session.*sig/i\n ],\n\n // Resource and validation errors\n resource: [\n /resource.*not.*found/i,\n /ipfs.*not.*found/i,\n /cid.*invalid/i,\n /pkp.*not.*found/i,\n /pkp.*validation/i,\n /resource.*validation/i,\n /action.*not.*permitted/i,\n /lit.*action.*not.*found/i\n ],\n\n // Validation errors\n validation: [\n /validation.*failed/i,\n /invalid.*input/i,\n /invalid.*parameter/i,\n /malformed/i,\n /schema.*validation/i,\n /type.*error/i,\n /missing.*required/i\n ],\n\n // Timeout specific errors\n timeout: [\n /timeout/i,\n /request.*timed.*out/i,\n /operation.*timed.*out/i,\n /deadline.*exceeded/i,\n /time.*limit.*exceeded/i\n ],\n\n // Rate limiting errors\n rateLimit: [\n /rate.*limit/i,\n /too.*many.*requests/i,\n /quota.*exceeded/i,\n /throttled/i,\n /429/,\n /rate.*exceeded/i\n ],\n\n // Configuration errors\n configuration: [\n /configuration/i,\n /config.*error/i,\n /environment/i,\n /missing.*env/i,\n /invalid.*config/i,\n /setup.*error/i\n ]\n};\n\n/**\n * Predefined retry strategies\n */\nconst RETRY_STRATEGIES = {\n [RetryStrategy.AGGRESSIVE]: {\n maxAttempts: 5,\n baseDelayMs: 500,\n maxDelayMs: 5000,\n multiplier: 1.5\n },\n [RetryStrategy.CONSERVATIVE]: {\n maxAttempts: 3,\n baseDelayMs: 3000,\n maxDelayMs: 30000,\n multiplier: 2.5\n },\n [RetryStrategy.EXPONENTIAL]: {\n maxAttempts: 4,\n baseDelayMs: 1000,\n maxDelayMs: 15000,\n multiplier: 2.0\n },\n [RetryStrategy.LINEAR]: {\n maxAttempts: 3,\n baseDelayMs: 2000,\n maxDelayMs: 10000,\n multiplier: 1.0\n },\n [RetryStrategy.NONE]: {\n maxAttempts: 1,\n baseDelayMs: 0,\n maxDelayMs: 0,\n multiplier: 1.0\n }\n};\n\n/**\n * Error classification rules\n */\nconst CLASSIFICATION_RULES: Record<ErrorCategory, ErrorClassification> = {\n [ErrorCategory.NETWORK]: {\n category: ErrorCategory.NETWORK,\n retryStrategy: RetryStrategy.AGGRESSIVE,\n isRetryable: true,\n ...RETRY_STRATEGIES[RetryStrategy.AGGRESSIVE],\n description: 'Network connectivity or communication error',\n troubleshooting: [\n 'Check internet connection',\n 'Verify LIT Protocol network status',\n 'Check firewall and proxy settings',\n 'Try connecting to different node endpoints'\n ]\n },\n\n [ErrorCategory.PROTOCOL]: {\n category: ErrorCategory.PROTOCOL,\n retryStrategy: RetryStrategy.CONSERVATIVE,\n isRetryable: true,\n ...RETRY_STRATEGIES[RetryStrategy.CONSERVATIVE],\n description: 'LIT Protocol specific operational error',\n troubleshooting: [\n 'Check LIT Protocol network status',\n 'Verify PKP configuration',\n 'Check signing shares availability',\n 'Wait for network consensus'\n ]\n },\n\n [ErrorCategory.AUTHENTICATION]: {\n category: ErrorCategory.AUTHENTICATION,\n retryStrategy: RetryStrategy.EXPONENTIAL,\n isRetryable: true,\n ...RETRY_STRATEGIES[RetryStrategy.EXPONENTIAL],\n maxAttempts: 2, // Override: limited retries for auth issues\n description: 'Authentication or authorization error',\n troubleshooting: [\n 'Check session signatures validity',\n 'Regenerate authentication tokens',\n 'Verify wallet permissions',\n 'Check PKP authorization status'\n ]\n },\n\n [ErrorCategory.RESOURCE]: {\n category: ErrorCategory.RESOURCE,\n retryStrategy: RetryStrategy.LINEAR,\n isRetryable: true,\n ...RETRY_STRATEGIES[RetryStrategy.LINEAR],\n maxAttempts: 2, // Override: limited retries for resource issues\n description: 'Resource not found or validation error',\n troubleshooting: [\n 'Verify resource CIDs are correct',\n 'Check IPFS availability',\n 'Confirm PKP exists and is accessible',\n 'Wait for resource propagation'\n ]\n },\n\n [ErrorCategory.VALIDATION]: {\n category: ErrorCategory.VALIDATION,\n retryStrategy: RetryStrategy.NONE,\n isRetryable: false,\n ...RETRY_STRATEGIES[RetryStrategy.NONE],\n description: 'Input validation or schema error',\n troubleshooting: [\n 'Check input parameters format',\n 'Verify required fields are provided',\n 'Validate data types and ranges',\n 'Review API documentation'\n ]\n },\n\n [ErrorCategory.TIMEOUT]: {\n category: ErrorCategory.TIMEOUT,\n retryStrategy: RetryStrategy.CONSERVATIVE,\n isRetryable: true,\n ...RETRY_STRATEGIES[RetryStrategy.CONSERVATIVE],\n description: 'Operation timeout error',\n troubleshooting: [\n 'Increase timeout values',\n 'Check network latency',\n 'Verify system resources',\n 'Split operations into smaller chunks'\n ]\n },\n\n [ErrorCategory.RATE_LIMIT]: {\n category: ErrorCategory.RATE_LIMIT,\n retryStrategy: RetryStrategy.CONSERVATIVE,\n isRetryable: true,\n ...RETRY_STRATEGIES[RetryStrategy.CONSERVATIVE],\n maxAttempts: 2, // Override: limited retries to avoid further rate limiting\n baseDelayMs: 5000, // Override: longer delays for rate limits\n description: 'Rate limit or quota exceeded',\n troubleshooting: [\n 'Reduce request frequency',\n 'Implement request batching',\n 'Check rate limit headers',\n 'Wait for quota reset'\n ]\n },\n\n [ErrorCategory.CONFIGURATION]: {\n category: ErrorCategory.CONFIGURATION,\n retryStrategy: RetryStrategy.NONE,\n isRetryable: false,\n ...RETRY_STRATEGIES[RetryStrategy.NONE],\n description: 'Configuration or setup error',\n troubleshooting: [\n 'Check environment variables',\n 'Verify configuration files',\n 'Review setup documentation',\n 'Check required dependencies'\n ]\n },\n\n [ErrorCategory.UNKNOWN]: {\n category: ErrorCategory.UNKNOWN,\n retryStrategy: RetryStrategy.EXPONENTIAL,\n isRetryable: true,\n ...RETRY_STRATEGIES[RetryStrategy.EXPONENTIAL],\n maxAttempts: 2, // Override: conservative retries for unknown errors\n description: 'Unknown or unclassified error',\n troubleshooting: [\n 'Check logs for more details',\n 'Verify system status',\n 'Try again with debug logging',\n 'Contact support if persists'\n ]\n }\n};\n\n/**\n * Classify an error and determine retry strategy\n */\nexport function classifyError(error: any): ErrorClassification {\n const errorMessage = error?.message || error?.toString() || '';\n const errorLower = errorMessage.toLowerCase();\n\n // Check each error category\n for (const [category, patterns] of Object.entries(LIT_ERROR_PATTERNS)) {\n for (const pattern of patterns) {\n if (pattern.test(errorMessage) || pattern.test(errorLower)) {\n const classification = CLASSIFICATION_RULES[category as ErrorCategory];\n return {\n ...classification,\n description: `${classification.description}: ${errorMessage.slice(0, 100)}`\n };\n }\n }\n }\n\n // Default to unknown category\n const unknownClassification = CLASSIFICATION_RULES[ErrorCategory.UNKNOWN];\n return {\n ...unknownClassification,\n description: `${unknownClassification.description}: ${errorMessage.slice(0, 100)}`\n };\n}\n\n/**\n * Get retry configuration from error classification\n */\nexport function getRetryConfigFromClassification(classification: ErrorClassification) {\n return {\n maxAttempts: classification.maxAttempts,\n initialDelayMs: classification.baseDelayMs,\n maxDelayMs: classification.maxDelayMs,\n backoffMultiplier: RETRY_STRATEGIES[classification.retryStrategy].multiplier,\n retryableErrors: ['*'] // Classification already determined retryability\n };\n}\n\n/**\n * Enhanced error analyzer for detailed error information\n */\nexport function analyzeError(error: any) {\n const classification = classifyError(error);\n\n return {\n ...classification,\n originalError: error,\n errorMessage: error?.message || error?.toString() || 'Unknown error',\n errorStack: error?.stack,\n timestamp: new Date().toISOString(),\n retryConfig: getRetryConfigFromClassification(classification)\n };\n}\n\n/**\n * Create a human-readable error report\n */\nexport function createErrorReport(error: any) {\n const analysis = analyzeError(error);\n\n const report = [\n `\uD83D\uDD0D Error Analysis Report`,\n `=====================================`,\n `Category: ${analysis.category.toUpperCase()}`,\n `Retryable: ${analysis.isRetryable ? '\u2705 Yes' : '\u274C No'}`,\n `Strategy: ${analysis.retryStrategy}`,\n `Max Attempts: ${analysis.maxAttempts}`,\n `Description: ${analysis.description}`,\n `Timestamp: ${analysis.timestamp}`,\n ``,\n `Original Error: ${analysis.errorMessage}`,\n ``\n ];\n\n if (analysis.troubleshooting && analysis.troubleshooting.length > 0) {\n report.push(`\uD83D\uDEE0\uFE0F Troubleshooting Steps:`);\n analysis.troubleshooting.forEach((step, index) => {\n report.push(` ${index + 1}. ${step}`);\n });\n report.push('');\n }\n\n return report.join('\\n');\n}\n\n/**\n * LIT Protocol specific error matchers\n */\nexport const litProtocolErrors = {\n isNetworkError: (error: any) => classifyError(error).category === ErrorCategory.NETWORK,\n isProtocolError: (error: any) => classifyError(error).category === ErrorCategory.PROTOCOL,\n isAuthenticationError: (error: any) => classifyError(error).category === ErrorCategory.AUTHENTICATION,\n isResourceError: (error: any) => classifyError(error).category === ErrorCategory.RESOURCE,\n isTimeoutError: (error: any) => classifyError(error).category === ErrorCategory.TIMEOUT,\n isRateLimitError: (error: any) => classifyError(error).category === ErrorCategory.RATE_LIMIT,\n isConfigurationError: (error: any) => classifyError(error).category === ErrorCategory.CONFIGURATION,\n isRetryable: (error: any) => classifyError(error).isRetryable\n};", "/**\n * Diamond Hands LIT Actions Utilities\n * All utility functions for working with deployed LIT Actions\n */\n\nexport * from \"./chunks/lit-action-helpers\";\nexport * from \"./chunks/pkp-setup\";\nexport * from \"./chunks/cid-utils\";\nexport * from \"./chunks/connection-helpers\";\nexport * from \"./chunks/debug-logger\";\nexport * from \"./chunks/error-classification\";\nexport * from \"./chunks/session-signature-cache\";\n"],
5
5
  "mappings": ";;;;;;;;;;;;;;;;;;;;;;;AAkCO,SAAS,sBAAsB,QAAoC;AACxE,gBAAc,EAAE,GAAG,aAAa,GAAG,OAAO;AAC5C;AAKA,SAAS,eAAuB;AAC9B,UAAO,oBAAI,KAAK,GAAE,YAAY;AAChC;AAKA,SAAS,cAAc,OAAe,WAAmB,SAAiB,UAAwB;AAChG,QAAM,YAAY,YAAY,mBAAmB,IAAI,aAAa,CAAC,OAAO;AAC1E,QAAM,cAAc,WAAW,IAAI,KAAK,UAAU,QAAQ,CAAC,KAAK;AAChE,SAAO,GAAG,SAAS,IAAI,KAAK,MAAM,SAAS,KAAK,OAAO,GAAG,WAAW;AACvE;AAkHO,SAAS,WAAW,WAAmB,SAAiB,UAAsB;AACnF,MAAI,YAAY,SAAS,eAAgB;AACvC,YAAQ,MAAM,cAAc,SAAS,WAAW,SAAS,QAAQ,CAAC;AAAA,EACpE;AACF;AAEO,SAAS,UAAU,WAAmB,SAAiB,UAAsB;AAClF,MAAI,YAAY,SAAS,cAAe;AACtC,YAAQ,KAAK,cAAc,QAAQ,WAAW,SAAS,QAAQ,CAAC;AAAA,EAClE;AACF;AAEO,SAAS,UAAU,WAAmB,SAAiB,UAAsB;AAClF,MAAI,YAAY,SAAS,cAAe;AACtC,YAAQ,IAAI,cAAc,QAAQ,WAAW,SAAS,QAAQ,CAAC;AAAA,EACjE;AACF;AAEO,SAAS,SAAS,WAAmB,SAAiB,UAAsB;AACjF,MAAI,YAAY,SAAS,eAAgB;AACvC,YA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