import{r as b}from"./vendor-react-CTtPmzVP.js";import{j as p,a6 as x,am as w,dw as P,bI as j,W as $}from"./vendor-mui-core-DnR6WYuU.js";import{x as k}from"./vendor-hooks-qxhUUbW5.js";import{B as d}from"./vendor-utils-CxWN1dT3.js";import{r as q}from"./omit-spWLSLbo.js";import{ce as V,cf as R}from"./index-BZxsCCR_.js";import{o as E,V as I}from"./vendor-ux-DTEmWsko.js";var a={},h;function D(){if(h)return a;h=1;var u=a&&a.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(a,"__esModule",{value:!0}),a.signResponse=l,a.verifyResponse=S,a.verifyVault=_;const m=u(q),v=u(V()),n=R(),s=E();async function l(e,c){const o=(0,v.default)(e),y=await c.sign(o);return{...e,$signature:y}}function S(e,c){return e.$signature?c.verify((0,v.default)((0,m.default)(e,"$signature")),e.$signature):Promise.resolve(!1)}async function _(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,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:${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,s.toTypeInfo)(g.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 K=D();function T({vaults:u,appPid:m}){const{t:v}=I(),[n,s]=k({verified:!1,verifying:!0,error:""});return b.useEffect(()=>{K.verifyVault(u,m,!0).then(l=>{s({verified:!!l,verifying:!1})}).catch(l=>{s({verified:!1,verifying:!1,error:l.message})})},[u,m,s]),n.verifying?p.jsx(x,{}):n.verified?p.jsx(w,{title:v("blocklet.config.vault.verified"),children:p.jsx(P,{fontSize:"small",color:"success"})}):p.jsx(w,{title:n.error,children:p.jsx(j,{color:"error"})})}T.propTypes={vaults:$.array.isRequired,appPid:$.string.isRequired};export{T as V};
