import{s as p,g as T}from"./chunk-CwBz3wv4.js";import{g as $,a as Q,p as O,b as X,n as Z,D as tt,i as et,c as nt,d as st}from"./chunk-CMnPkCrZ.js";import{c as ot}from"./chunk-YE5gLsn5.js";const A="https://eth-beacon-chain.drpc.org",at=["https://eth-beacon-chain.drpc.org","https://ethereum-beacon-api.publicnode.com","https://lodestar-mainnet.chainsafe.io"];let j=1;const it=document.querySelector(".stepper"),H={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 u(t){j=t;for(const[e,n]of Object.entries(H))n.hidden=Number(e)!==t;for(const e of it.querySelectorAll(".step")){const n=Number(e.dataset.step);e.classList.remove("current","done"),n<t?e.classList.add("done"):n===t&&e.classList.add("current")}}const k=document.getElementById("ipfs-dot"),C=document.getElementById("ipfs-status-text"),L=document.getElementById("ipfs-next"),S=document.getElementById("ipfs-recheck"),rt=document.getElementById("ipfs-gateway-form"),g=document.getElementById("ipfs-gateway-host"),w=document.getElementById("ipfs-gateway-port"),E=document.getElementById("ipfs-gateway-save"),ct=document.getElementById("ipfs-gateway-preview"),a=document.getElementById("ipfs-gateway-status");let r=!1;function G(t){ct.textContent=`Probe URL: ${nt(t)}`}function dt(t){const e=$(t);g.value=e.host,w.value=String(e.port),G(e)}function K(){const t=X(g.value),e=t?.port??Z(w.value);return t?e?{host:t.host,port:e}:(a.textContent="Enter a port from 1 to 65535.",null):(a.textContent="Enter a gateway host, or an http:// gateway URL without a path.",null)}function _(){const t=K();t&&(a.textContent="",G(t))}async function lt(t){if(t.host===tt)return!0;const e=et(t);try{return await chrome.permissions.contains({origins:e})?!0:await chrome.permissions.request({origins:e})}catch{return!1}}async function U(){const t=$(await T()),e=H[1];k.classList.remove("ok","bad"),S.classList.add("spinning"),C.textContent="Checking Kubo gateway…";try{if(!await Q(t,{force:!0}))throw new Error("gateway probe failed");return k.classList.add("ok"),C.textContent=`Connected at ${t.host}:${t.port}`,L.disabled=!1,e.classList.remove("state-bad"),e.classList.add("state-ok"),r=!0,!0}catch(n){return k.classList.add("bad"),C.textContent=`Not reachable: ${n instanceof Error?n.message:String(n)}`,L.disabled=!0,e.classList.remove("state-ok"),e.classList.add("state-bad"),r=!1,!1}finally{setTimeout(()=>S.classList.remove("spinning"),400)}}async function q(){const t=H[1];t.querySelector("#api-warning")?.remove();const e=await O();if(e.ok||e.kind.kind!=="cors")return;const n=chrome.runtime.id,c=`ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin '["chrome-extension://${n}"]' && ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods '["POST"]'`,h=["{","  ...",'  "API": {','    "HTTPHeaders": {',"      ...",'      "Access-Control-Allow-Methods": ["POST"],','      "Access-Control-Allow-Origin": [',"        ...,",`        "chrome-extension://${n}"`,"      ]","    }","  },","  ...","}"].join(`
`),s=document.createElement("details");s.id="api-warning",s.className="api-warning",s.innerHTML=`
    <summary class="api-warning-title">Optional: enable <a href="https://eip.tools/eip/4804" target="_blank" rel="noopener">ERC-4804</a> dapps and IPFS auto-pinning</summary>
    <div class="api-warning-content">
      <p class="api-warning-body">
        Run once, restart Kubo. Standard <code>.eth</code>/<code>.gwei</code>/IPFS sites already work without this; ERC-4804 dapps and optional IPFS auto-pinning need it.
      </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>
  `,s.querySelector('[data-cmd="cmd"]').textContent=c,s.querySelector('[data-cmd="json"]').innerHTML=ot(h),s.querySelectorAll(".copy-btn").forEach(o=>{o.addEventListener("click",async()=>{const v=o.dataset.target;if(!v)return;const l=s.querySelector(`[data-cmd="${v}"]`)?.textContent??"";if(!l)return;try{await navigator.clipboard.writeText(l)}catch{return}const m=o.textContent??"Copy";o.textContent="Copied",setTimeout(()=>{o.textContent=m},1500)})}),s.querySelector('[data-action="recheck-api"]')?.addEventListener("click",async()=>{const o=s.querySelector('[data-action="recheck-api"]');if(o&&(o.disabled=!0),(await O()).ok){s.classList.add("ok"),s.open=!0;const l=s.querySelector(".api-warning-title");l&&(l.textContent="Kubo API is now allowing this extension");const m=s.querySelector(".api-warning-body");m&&(m.innerHTML='Setup complete. <a href="https://eip.tools/eip/4804" target="_blank" rel="noopener">ERC-4804</a> dapps will work after this onboarding finishes.'),s.querySelectorAll(".cmd-block, .recheck-btn, .api-walkthrough, .api-warning-footer").forEach(V=>V.remove());return}o&&(o.disabled=!1)});const b=t.querySelector(".panel-footer");b?t.insertBefore(s,b):t.appendChild(s)}S.addEventListener("click",async()=>{await U()&&await q()});g.addEventListener("input",_);w.addEventListener("input",_);rt.addEventListener("submit",async t=>{t.preventDefault();const e=K();if(!e)return;if(E.disabled=!0,a.textContent="Requesting gateway permission…",!await lt(e)){a.textContent="Permission denied — dapp3 can't reach that gateway host.",E.disabled=!1;return}a.textContent="Saving and checking gateway…",await p({ipfsGatewayHost:e.host,ipfsGatewayPort:e.port}),g.value=e.host,w.value=String(e.port),st(),G(e),await U()?(await q(),a.textContent="Saved. Gateway is reachable."):a.textContent="Saved, but the gateway did not answer.",E.disabled=!1});L.addEventListener("click",()=>u(2));const x=document.getElementById("rpc-form"),pt=document.getElementById("rpc-back"),F=document.getElementById("perm-preview"),i=x.elements.namedItem("url");function M(){F.innerHTML="";const t=[];try{const e=new URL(i.value);t.push(`${e.origin}  (your execution RPC — reads Ethereum state)`)}catch{t.push("<your execution RPC, once entered above>")}t.push(`${new URL(A).origin}  (Helios consensus RPC — reads beacon-chain light-client data)`);for(const e of t){const n=document.createElement("li");n.textContent=e,F.appendChild(n)}}i.addEventListener("input",M);M();pt.addEventListener("click",()=>u(1));x.addEventListener("submit",async t=>{t.preventDefault();const e=String(new FormData(x).get("url")??"").trim();let n;try{n=new URL(e)}catch{i.classList.add("invalid"),i.focus();return}if(n.protocol!=="http:"&&n.protocol!=="https:"){i.classList.add("invalid"),i.focus(),alert("Only http:// or https:// URLs are accepted.");return}i.classList.remove("invalid");const{consensusRpc:c}=await T(),h=c||A,s=[`${n.origin}/*`];try{s.push(new URL(h).origin+"/*")}catch{}if(!await chrome.permissions.request({origins:s})){alert("Permission was not granted. The extension needs to reach both the execution RPC and the Helios consensus RPC.");return}await p({rpcUrl:e}),u(3),J()});const I=document.getElementById("helios-dot"),d=document.getElementById("helios-status-text"),P=document.getElementById("sync-title"),R=document.getElementById("sync-lede"),B=document.getElementById("sync-recovery"),D=document.getElementById("consensus-chips"),W=document.getElementById("adv-consensus-input"),z=document.getElementById("adv-checkpoint-input"),ut=document.getElementById("adv-apply"),y=document.getElementById("finish"),ht=document.getElementById("sync-back");let f=!1;function mt(){D.innerHTML="";for(const t of at){const e=document.createElement("button");e.type="button",e.className="chip",e.textContent=new URL(t).host,e.title=t,e.addEventListener("click",()=>{Y(t,void 0)}),D.appendChild(e)}}mt();async function Y(t,e){try{const n=new URL(t);if(n.protocol!=="http:"&&n.protocol!=="https:"){alert("Only http:// or https:// URLs are accepted.");return}const c=n.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 p({consensusRpc:t,checkpoint:e});try{await chrome.runtime.sendMessage({type:"shutdown-helios"})}catch{}P.textContent="Starting Helios",R.textContent="Retrying with updated consensus RPC…",B.hidden=!0,y.disabled=!0,J()}ut.addEventListener("click",()=>{const t=W.value.trim()||A;let e=z.value.trim()||void 0;e&&!e.startsWith("0x")&&(e="0x"+e),Y(t,e)});function N(t){if(I.classList.remove("ok","bad"),!t){d.textContent="waiting…";return}switch(t.state){case"idle":d.textContent="not yet started";break;case"booting":d.textContent="Booting Helios WASM…";break;case"syncing":d.textContent="Syncing with Ethereum consensus…";break;case"synced":d.textContent="Synced · verifying onchain reads locally",I.classList.add("ok"),P.textContent="You're all set",R.textContent="Helios is synced. Finish to open the dapp3 home page.",B.hidden=!0,y.disabled=!1,f||(f=!0,p({onboardingComplete:!0,interceptEthLimo:r,interceptW3Eth:r,interceptGweiDomains:r}));break;case"error":d.textContent=`Sync failed: ${t.error??"unknown error"}`,I.classList.add("bad"),P.textContent="Couldn't reach consensus",R.textContent="Helios couldn't start with the current beacon-chain RPC. Try another one below.",B.hidden=!1,y.disabled=!0;break}}async function J(){for(chrome.runtime.sendMessage({type:"boot-helios"}).catch(()=>{});j===3;){try{const t=await chrome.runtime.sendMessage({type:"get-helios-status"});N(t?.status??null)}catch{N(null)}await new Promise(t=>setTimeout(t,1e3))}}y.addEventListener("click",async()=>{f||(f=!0,await p({onboardingComplete:!0,interceptEthLimo:r,interceptW3Eth:r,interceptGweiDomains:r}));const t=chrome.runtime.getURL("home.html");location.replace(t)});ht.addEventListener("click",async()=>{try{await chrome.runtime.sendMessage({type:"shutdown-helios"})}catch{}u(2)});(async()=>{const t=await T();dt(t),await U()&&await q(),i.value=t.rpcUrl??"https://eth.drpc.org",M(),W.value=t.consensusRpc??"",z.value=t.checkpoint??""})();u(1);
//# sourceMappingURL=onboarding-Bqepy9jR.js.map
