import{c as P,j as u,cO as b,cP as j,cQ as d,V as x,r as k,C as q,ad as w,o as $}from"./index-DR3HTfO6.js";import{u as V}from"./index-CCxxisrt.js";import{E as R}from"./ErrorOutline-l3lhgB6z.js";import{r as E}from"./omit-Jga1bbMx.js";import{r as D}from"./index-D9fzFoDk.js";const I=P(u.jsx("path",{d:"M12 2 4 5v6.09c0 5.05 3.41 9.76 8 10.91 4.59-1.15 8-5.86 8-10.91V5zm-1.06 13.54L7.4 12l1.41-1.41 2.12 2.12 4.24-4.24 1.41 1.41z"}));var a={},h;function K(){if(h)return a;h=1;var l=a&&a.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(a,"__esModule",{value:!0}),a.signResponse=c,a.verifyResponse=S,a.verifyVault=_;const v=l(E),g=l(b()),n=D(),s=j();async function c(e,p){const o=(0,g.default)(e),y=await p.sign(o);return{...e,$signature:y}}function S(e,p){return e.$signature?p.verify((0,g.default)((0,v.default)(e,"$signature")),e.$signature):Promise.resolve(!1)}async function _(e,p,o=!1){if(!Array.isArray(e)||e.length===0){if(o)throw new Error("vaults list is empty");return""}for(let t=1;t<e.length;t++)if(e[t].at<=e[t-1].at){if(o)throw new Error("vaults are not in ascending order");return""}if(new Set(e.map(t=>t.did)).size!==e.length){if(o)throw new Error("vaults list has duplicate vaults");return""}for(let t=0;t<e.length;t++){const r=e[t];if(!(0,s.isFromPublicKey)(r.did,r.pk)){if(o)throw new Error(`vault did and pk mismatch: ${r.did}(${r.pk})`);return""}let f=d.from(`vault:${p}`);for(let i=0;i<=t;i++)f=d.concat([f,d.from(`:${e[i].did}`)]);if(!r.approverSig){if(o)throw new Error(`vault approve signature missing for ${r.did}`);return""}try{let i;if(t>0){const m=e[t-1];i=(0,n.fromPublicKey)(m.pk,(0,s.toTypeInfo)(m.did))}else{if(!r.approverPk||!r.approverDid||!(0,s.isFromPublicKey)(r.approverDid,r.approverPk)){if(o)throw new Error(`approver config missing for ${r.did}`);return""}i=(0,n.fromPublicKey)(r.approverPk,(0,s.toTypeInfo)(r.approverDid))}if(await i.verify(f,r.approverSig)===!1){if(o)throw new Error(`signature verify failed for ${r.did}`);return""}}catch(i){if(console.error(i),o)throw new Error(`vault approve verify failed: ${i.message}`);return""}if(!r.sig){if(o)throw new Error(`vault commit signature missing for ${r.did}`);return""}try{const i=(0,n.fromPublicKey)(r.pk,(0,s.toTypeInfo)(r.did));if(r.approverSig&&(f=d.concat([f,d.from(`:${r.approverSig}`)])),await i.verify(f,r.sig)===!1){if(o)throw new Error(`commit signature verify failed for ${r.did}`);return""}}catch(i){if(console.error(i),o)throw new Error(`vault commit verify failed: ${i.message}`);return""}}return e[e.length-1].did}return a}var T=K();function z({vaults:l,appPid:v}){const{t:g}=x(),[n,s]=V({verified:!1,verifying:!0,error:""});return k.useEffect(()=>{T.verifyVault(l,v,!0).then(c=>{s({verified:!!c,verifying:!1})}).catch(c=>{s({verified:!1,verifying:!1,error:c.message})})},[l,v,s]),n.verifying?u.jsx(q,{}):n.verified?u.jsx(w,{title:g("blocklet.config.vault.verified"),children:u.jsx(I,{fontSize:"small",color:"success"})}):u.jsx(w,{title:n.error,children:u.jsx(R,{color:"error"})})}z.propTypes={vaults:$.array.isRequired,appPid:$.string.isRequired};export{z as V};
