import{g as q,s as y}from"./chunk-DQIhPnbi.js";import{p as T}from"./chunk-BvQ1SBzo.js";import{c as K}from"./chunk-YE5gLsn5.js";const I="https://eth-beacon-chain.drpc.org",_=["https://eth-beacon-chain.drpc.org","https://ethereum-beacon-api.publicnode.com","https://lodestar-mainnet.chainsafe.io"];let M=1;const W=document.querySelector(".stepper"),R={1:document.querySelector('.step-panel[data-step="1"]'),2:document.querySelector('.step-panel[data-step="2"]'),3:document.querySelector('.step-panel[data-step="3"]')};function l(e){M=e;for(const[t,o]of Object.entries(R))o.hidden=Number(t)!==e;for(const t of W.querySelectorAll(".step")){const o=Number(t.dataset.step);t.classList.remove("current","done"),o<e?t.classList.add("done"):o===e&&t.classList.add("current")}}const b=document.getElementById("ipfs-dot"),w=document.getElementById("ipfs-status-text"),v=document.getElementById("ipfs-next"),C=document.getElementById("ipfs-recheck");let r=!1;async function U(){const e=R[1];b.classList.remove("ok","bad"),C.classList.add("spinning"),w.textContent="Checking Kubo gateway…";try{return await fetch("http://bafkqaaa.ipfs.localhost:8080/",{mode:"no-cors",cache:"no-store",signal:AbortSignal.timeout(2500)}),b.classList.add("ok"),w.textContent="Connected at localhost:8080",v.disabled=!1,e.classList.remove("state-bad"),e.classList.add("state-ok"),r=!0,!0}catch(t){return b.classList.add("bad"),w.textContent=`Not reachable: ${t instanceof Error?t.message:String(t)}`,v.disabled=!0,e.classList.remove("state-ok"),e.classList.add("state-bad"),r=!1,!1}finally{setTimeout(()=>C.classList.remove("spinning"),400)}}async function O(){const e=R[1];e.querySelector("#api-warning")?.remove();const t=await T();if(t.ok||t.kind.kind!=="cors")return;const o=chrome.runtime.id,c=`ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin '["chrome-extension://${o}"]' && ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods '["POST"]'`,p=["{","  ...",'  "API": {','    "HTTPHeaders": {',"      ...",'      "Access-Control-Allow-Methods": ["POST"],','      "Access-Control-Allow-Origin": [',"        ...,",`        "chrome-extension://${o}"`,"      ]","    }","  },","  ...","}"].join(`
`),n=document.createElement("details");n.id="api-warning",n.className="api-warning",n.innerHTML=`
    <summary class="api-warning-title">Optional: enable <a href="https://eip.tools/eip/4804" target="_blank" rel="noopener">ERC-4804</a> dapps</summary>
    <div class="api-warning-content">
      <p class="api-warning-body">
        Run once, restart Kubo. Standard <code>.eth</code>/IPFS sites already work without this.
      </p>
      <div class="cmd-block">
        <pre data-cmd="cmd"></pre>
        <button class="copy-btn" type="button" data-target="cmd">Copy</button>
      </div>
      <div class="api-walkthrough">
        <p class="api-walkthrough-label">No CLI? Use IPFS Desktop's UI</p>
        <ol>
          <li>Open IPFS Desktop → Settings → Kubo Config.</li>
          <li>
            Merge these two keys into <code>API.HTTPHeaders</code> (keep any
            entries already there):
            <div class="cmd-block json-block">
              <pre data-cmd="json"></pre>
              <button class="copy-btn" type="button" data-target="json">Copy</button>
            </div>
          </li>
          <li>Save and restart Kubo.</li>
        </ol>
      </div>
      <div class="api-warning-footer">
        <button class="recheck-btn" type="button" data-action="recheck-api">
          <svg class="btn-icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
            <path d="M13.5 3.5v3h-3" />
            <path d="M2.5 12.5v-3h3" />
            <path d="M3.5 6.5a5 5 0 0 1 9 -1.5l1 1.5M12.5 9.5a5 5 0 0 1 -9 1.5l-1 -1.5" />
          </svg>
          <span>Recheck</span>
        </button>
      </div>
    </div>
  `,n.querySelector('[data-cmd="cmd"]').textContent=c,n.querySelector('[data-cmd="json"]').innerHTML=K(p),n.querySelectorAll(".copy-btn").forEach(s=>{s.addEventListener("click",async()=>{const f=s.dataset.target;if(!f)return;const d=n.querySelector(`[data-cmd="${f}"]`)?.textContent??"";if(!d)return;try{await navigator.clipboard.writeText(d)}catch{return}const u=s.textContent??"Copy";s.textContent="Copied",setTimeout(()=>{s.textContent=u},1500)})}),n.querySelector('[data-action="recheck-api"]')?.addEventListener("click",async()=>{const s=n.querySelector('[data-action="recheck-api"]');if(s&&(s.disabled=!0),(await T()).ok){n.classList.add("ok"),n.open=!0;const d=n.querySelector(".api-warning-title");d&&(d.textContent="Kubo API is now allowing this extension");const u=n.querySelector(".api-warning-body");u&&(u.innerHTML='Setup complete. <a href="https://eip.tools/eip/4804" target="_blank" rel="noopener">ERC-4804</a> dapps will work after this onboarding finishes.'),n.querySelectorAll(".cmd-block, .recheck-btn, .api-walkthrough, .api-warning-footer").forEach(F=>F.remove());return}s&&(s.disabled=!1)});const g=e.querySelector(".panel-footer");g?e.insertBefore(n,g):e.appendChild(n)}C.addEventListener("click",async()=>{await U()&&await O()});v.addEventListener("click",()=>l(2));const L=document.getElementById("rpc-form"),z=document.getElementById("rpc-back"),A=document.getElementById("perm-preview"),a=L.elements.namedItem("url");function P(){A.innerHTML="";const e=[];try{const t=new URL(a.value);e.push(`${t.origin}  (your execution RPC — reads Ethereum state)`)}catch{e.push("<your execution RPC, once entered above>")}e.push(`${new URL(I).origin}  (Helios consensus RPC — reads beacon-chain light-client data)`);for(const t of e){const o=document.createElement("li");o.textContent=t,A.appendChild(o)}}a.addEventListener("input",P);P();z.addEventListener("click",()=>l(1));L.addEventListener("submit",async e=>{e.preventDefault();const t=String(new FormData(L).get("url")??"").trim();let o;try{o=new URL(t)}catch{a.classList.add("invalid"),a.focus();return}if(o.protocol!=="http:"&&o.protocol!=="https:"){a.classList.add("invalid"),a.focus(),alert("Only http:// or https:// URLs are accepted.");return}a.classList.remove("invalid");const{consensusRpc:c}=await q(),p=c||I,n=[`${o.origin}/*`];try{n.push(new URL(p).origin+"/*")}catch{}if(!await chrome.permissions.request({origins:n})){alert("Permission was not granted. The extension needs to reach both the execution RPC and the Helios consensus RPC.");return}await y({rpcUrl:t}),l(3),D()});const k=document.getElementById("helios-dot"),i=document.getElementById("helios-status-text"),E=document.getElementById("sync-title"),S=document.getElementById("sync-lede"),x=document.getElementById("sync-recovery"),B=document.getElementById("consensus-chips"),N=document.getElementById("adv-consensus-input"),j=document.getElementById("adv-checkpoint-input"),J=document.getElementById("adv-apply"),h=document.getElementById("finish"),V=document.getElementById("sync-back");let m=!1;function Y(){B.innerHTML="";for(const e of _){const t=document.createElement("button");t.type="button",t.className="chip",t.textContent=new URL(e).host,t.title=e,t.addEventListener("click",()=>{$(e,void 0)}),B.appendChild(t)}}Y();async function $(e,t){try{const o=new URL(e);if(o.protocol!=="http:"&&o.protocol!=="https:"){alert("Only http:// or https:// URLs are accepted.");return}const c=o.origin+"/*";if(!await chrome.permissions.contains({origins:[c]})&&!await chrome.permissions.request({origins:[c]})){alert("Permission not granted — Helios can't reach that host.");return}}catch{alert("That URL isn't valid.");return}await y({consensusRpc:e,checkpoint:t});try{await chrome.runtime.sendMessage({type:"shutdown-helios"})}catch{}E.textContent="Starting Helios",S.textContent="Retrying with updated consensus RPC…",x.hidden=!0,h.disabled=!0,D()}J.addEventListener("click",()=>{const e=N.value.trim()||I;let t=j.value.trim()||void 0;t&&!t.startsWith("0x")&&(t="0x"+t),$(e,t)});function H(e){if(k.classList.remove("ok","bad"),!e){i.textContent="waiting…";return}switch(e.state){case"idle":i.textContent="not yet started";break;case"booting":i.textContent="Booting Helios WASM…";break;case"syncing":i.textContent="Syncing with Ethereum consensus…";break;case"synced":i.textContent="Synced · verifying onchain reads locally",k.classList.add("ok"),E.textContent="You're all set",S.textContent="Helios is synced. Finish to open the dapp3 home page.",x.hidden=!0,h.disabled=!1,m||(m=!0,y({onboardingComplete:!0,interceptEthLimo:r,interceptW3Eth:r}));break;case"error":i.textContent=`Sync failed: ${e.error??"unknown error"}`,k.classList.add("bad"),E.textContent="Couldn't reach consensus",S.textContent="Helios couldn't start with the current beacon-chain RPC. Try another one below.",x.hidden=!1,h.disabled=!0;break}}async function D(){for(chrome.runtime.sendMessage({type:"boot-helios"}).catch(()=>{});M===3;){try{const e=await chrome.runtime.sendMessage({type:"get-helios-status"});H(e?.status??null)}catch{H(null)}await new Promise(e=>setTimeout(e,1e3))}}h.addEventListener("click",async()=>{m||(m=!0,await y({onboardingComplete:!0,interceptEthLimo:r,interceptW3Eth:r}));const e=chrome.runtime.getURL("home.html");location.replace(e)});V.addEventListener("click",async()=>{try{await chrome.runtime.sendMessage({type:"shutdown-helios"})}catch{}l(2)});(async()=>{await U()&&await O();const t=await q();a.value=t.rpcUrl??"https://eth.drpc.org",P(),N.value=t.consensusRpc??"",j.value=t.checkpoint??""})();l(1);
//# sourceMappingURL=onboarding-B_LhzPBJ.js.map
