import{r as b}from"./vendor-react-CUXIiAnY.js";import{j as p,a6 as P,am as w,bZ as j,b_ as k,W as $}from"./vendor-mui-core-BPQ6AEW_.js";import{l as q}from"./vendor-hooks-PN9SWiQm.js";import{B as d}from"./vendor-utils-BgSaqjGx.js";import{r as x}from"./omit-CpvduH4b.js";import{aF as V,aG as R}from"./index-DY2wgD65.js";import{O as E,V as D}from"./vendor-ux-r9bApERu.js";var s={},h;function K(){if(h)return s;h=1;var u=s&&s.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(s,"__esModule",{value:!0}),s.signResponse=l,s.verifyResponse=_,s.verifyVault=S;const m=u(x),v=u(V()),n=R(),a=E();async function l(e,c){const o=(0,v.default)(e),y=await c.sign(o);return{...e,$signature:y}}function _(e,c){return e.$signature?c.verify((0,v.default)((0,m.default)(e,"$signature")),e.$signature):Promise.resolve(!1)}async function S(e,c,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,a.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:${c}`);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 g=e[t-1];i=(0,n.fromPublicKey)(g.pk,(0,a.toTypeInfo)(g.did))}else{if(!r.approverPk||!r.approverDid||!(0,a.isFromPublicKey)(r.approverDid,r.approverPk)){if(o)throw new Error(`approver config missing for ${r.did}`);return""}i=(0,n.fromPublicKey)(r.approverPk,(0,a.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,a.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 s}var T=K();function I({vaults:u,appPid:m}){const{t:v}=D(),[n,a]=q({verified:!1,verifying:!0,error:""});return b.useEffect(()=>{T.verifyVault(u,m,!0).then(l=>{a({verified:!!l,verifying:!1})}).catch(l=>{a({verified:!1,verifying:!1,error:l.message})})},[u,m,a]),n.verifying?p.jsx(P,{}):n.verified?p.jsx(w,{title:v("blocklet.config.vault.verified"),children:p.jsx(j,{fontSize:"small",color:"success"})}):p.jsx(w,{title:n.error,children:p.jsx(k,{color:"error"})})}I.propTypes={vaults:$.array.isRequired,appPid:$.string.isRequired};export{I as V};
