import{r as b}from"./vendor-react-Dvs43sk9.js";import{j as p,a6 as P,am as w,bZ as j,b_ as k,W as $}from"./vendor-mui-core-CnXrAOs3.js";import{l as q}from"./vendor-hooks-CjESGRlR.js";import{B as d}from"./vendor-utils-cJEl_UEd.js";import{r as x}from"./omit-hhiOJGRf.js";import{aE as V,aF as R}from"./index-Cq3xfsOS.js";import{O as E,V as D}from"./vendor-ux-5OlTXZBe.js";var s={},h;function I(){if(h)return s;h=1;var u=s&&s.__importDefault||function(r){return r&&r.__esModule?r:{default:r}};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(r,c){const o=(0,v.default)(r),y=await c.sign(o);return{...r,$signature:y}}function _(r,c){return r.$signature?c.verify((0,v.default)((0,m.default)(r,"$signature")),r.$signature):Promise.resolve(!1)}async function S(r,c,o=!1){if(!Array.isArray(r)||r.length===0){if(o)throw new Error("vaults list is empty");return""}for(let t=1;t<r.length;t++)if(r[t].at<=r[t-1].at){if(o)throw new Error("vaults are not in ascending order");return""}if(new Set(r.map(t=>t.did)).size!==r.length){if(o)throw new Error("vaults list has duplicate vaults");return""}for(let t=0;t<r.length;t++){const e=r[t];if(!(0,a.isFromPublicKey)(e.did,e.pk)){if(o)throw new Error(`vault did and pk mismatch: ${e.did}(${e.pk})`);return""}let f=d.from(`vault:${c}`);for(let i=0;i<=t;i++)f=d.concat([f,d.from(`:${r[i].did}`)]);if(!e.approverSig){if(o)throw new Error(`vault approve signature missing for ${e.did}`);return""}try{let i;if(t>0){const g=r[t-1];i=(0,n.fromPublicKey)(g.pk,(0,a.toTypeInfo)(g.did))}else{if(!e.approverPk||!e.approverDid||!(0,a.isFromPublicKey)(e.approverDid,e.approverPk)){if(o)throw new Error(`approver config missing for ${e.did}`);return""}i=(0,n.fromPublicKey)(e.approverPk,(0,a.toTypeInfo)(e.approverDid))}if(await i.verify(f,e.approverSig)===!1){if(o)throw new Error(`signature verify failed for ${e.did}`);return""}}catch(i){if(console.error(i),o)throw new Error(`vault approve verify failed: ${i.message}`);return""}if(!e.sig){if(o)throw new Error(`vault commit signature missing for ${e.did}`);return""}try{const i=(0,n.fromPublicKey)(e.pk,(0,a.toTypeInfo)(e.did));if(e.approverSig&&(f=d.concat([f,d.from(`:${e.approverSig}`)])),await i.verify(f,e.sig)===!1){if(o)throw new Error(`commit signature verify failed for ${e.did}`);return""}}catch(i){if(console.error(i),o)throw new Error(`vault commit verify failed: ${i.message}`);return""}}return r[r.length-1].did}return s}var K=I();function T({vaults:u,appPid:m}){const{t:v}=D(),[n,a]=q({verified:!1,verifying:!0,error:""});return b.useEffect(()=>{K.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"})})}T.propTypes={vaults:$.array.isRequired,appPid:$.string.isRequired};export{T as V};
