"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.createSuccession=createSuccession,exports.verifySuccession=verifySuccession,exports.encodeSuccession=encodeSuccession,exports.decodeSuccession=decodeSuccession,exports.verifySuccessionChain=verifySuccessionChain;const identity_js_1=require("./identity.js"),shared_1=require("../_deps/shared/index.js");async function createSuccession(e,r,t,n,i,s=6048e5){const o=Date.now(),a=o,c=o+2592e6,u=`${e}||${t}||${i}||${a}||${c}||${s}||${o}`,_=(new TextEncoder).encode(u),d=await(0,identity_js_1.signMlDsa65)(r,_);if(!d.ok)return(0,shared_1.err)("SIGN_FAILED");const f=await(0,identity_js_1.signMlDsa65)(n,_);return f.ok?(0,shared_1.ok)({oldDid:e,newDid:t,rotation_sequence:i,effective_at:a,expires_at:c,grace_period_ms:s,timestamp:o,oldSignature:d.value,newSignature:f.value}):(0,shared_1.err)("SIGN_FAILED")}async function verifySuccession(e,r,t){const n=`${e.oldDid}||${e.newDid}||${e.rotation_sequence}||${e.effective_at}||${e.expires_at}||${e.grace_period_ms}||${e.timestamp}`,i=(new TextEncoder).encode(n),s=await(0,identity_js_1.verifyMlDsa65)(r,e.oldSignature,i);if(!s.ok)return(0,shared_1.err)("VERIFY_FAILED");const o=await(0,identity_js_1.verifyMlDsa65)(t,e.newSignature,i);if(!o.ok)return(0,shared_1.err)("VERIFY_FAILED");const a=s.value&&o.value;return(0,shared_1.ok)(a)}function encodeSuccession(e){return[e.oldDid,e.newDid,e.rotation_sequence.toString(),e.effective_at.toString(),e.expires_at.toString(),e.grace_period_ms.toString(),e.timestamp.toString(),Buffer.from(e.oldSignature).toString("base64"),Buffer.from(e.newSignature).toString("base64")].join("||")}function decodeSuccession(e){const r=e.split("||");if(9!==r.length)return(0,shared_1.err)("INVALID_FORMAT");const t=r[0],n=r[1],i=r[2],s=r[3],o=r[4],a=r[5],c=r[6],u=r[7],_=r[8];if(!(t&&n&&i&&s&&o&&a&&c&&u&&_))return(0,shared_1.err)("INVALID_FORMAT");const d=parseInt(i,10),f=parseInt(s,10),S=parseInt(o,10),p=parseInt(a,10),g=parseInt(c,10);if(isNaN(d)||d<0||isNaN(f)||f<0||isNaN(S)||S<0||isNaN(p)||p<0||isNaN(g)||g<0)return(0,shared_1.err)("INVALID_TIMESTAMP");try{return(0,shared_1.ok)({oldDid:t,newDid:n,rotation_sequence:d,effective_at:f,expires_at:S,grace_period_ms:p,timestamp:g,oldSignature:new Uint8Array(Buffer.from(u,"base64")),newSignature:new Uint8Array(Buffer.from(_,"base64"))})}catch{return(0,shared_1.err)("INVALID_FORMAT")}}function verifySuccessionChain(e){if(0===e.length)return(0,shared_1.ok)(!0);if(1===e.length)return(0,shared_1.ok)(!0);for(let r=1;r<e.length;r++){const t=e[r-1];if(e[r].rotation_sequence<=t.rotation_sequence)return(0,shared_1.err)("SUCCESSION_NOT_MONOTONIC")}return(0,shared_1.ok)(!0)}