"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.signVaultRequest=signVaultRequest,exports.verifyVaultRequest=verifyVaultRequest;const shared_1=require("../_deps/shared/index.js"),crypto_utils_js_1=require("./crypto-utils.js");async function signVaultRequest(e,t,r){const n=Date.now(),s=generateNonce(),a=JSON.stringify(r),o=`POST\n${t}\n${n}\n${s}\n${await hashString(a)}`;let i;try{const t=(new TextEncoder).encode(o);i=await crypto.subtle.sign({name:"Ed25519"},e.privateKey,t)}catch{return(0,shared_1.err)("SIGN_FAILED")}return(0,shared_1.ok)({signature:(0,crypto_utils_js_1.toBase64)(new Uint8Array(i)),timestamp:n,nonce:s})}function generateNonce(){if("undefined"!=typeof crypto&&crypto.randomUUID)return crypto.randomUUID();const e=new Uint8Array(16);crypto.getRandomValues(e),e[6]=15&e[6]|64,e[8]=63&e[8]|128;const t=Array.from(e).map(e=>e.toString(16).padStart(2,"0")).join("");return[t.slice(0,8),t.slice(8,12),t.slice(12,16),t.slice(16,20),t.slice(20,32)].join("-")}async function hashString(e){const t=(new TextEncoder).encode(e),r=await crypto.subtle.digest("SHA-256",t);return(0,crypto_utils_js_1.toBase64)(new Uint8Array(r))}async function verifyVaultRequest(e,t,r,n,s,a,o){const i=Date.now();if(Math.abs(i-a)>3e5)return!1;const c=`POST\n${r}\n${a}\n${o}\n${await hashString(n)}`;let u;try{const e=new ArrayBuffer(t.byteLength);new Uint8Array(e).set(t),u=await crypto.subtle.importKey("raw",e,{name:"Ed25519"},!1,["verify"])}catch{return!1}try{const e=(new TextEncoder).encode(c),t=Uint8Array.from(atob(s),e=>e.charCodeAt(0));return await crypto.subtle.verify({name:"Ed25519"},u,t,e)}catch{return!1}}