UNPKG

@cmdcode/musig2

Version:

Create digital signatures based on the Musig2 protocol.

12 lines 58.1 kB
var musig2=function(t){"use strict";const e={key_tweaks:[]};function n(t={}){return{...e,...t}}function r(t){if(!Number.isSafeInteger(t)||t<0)throw new Error(`Wrong positive integer: ${t}`)}function o(t,...e){if(!((n=t)instanceof Uint8Array||null!=n&&"object"==typeof n&&"Uint8Array"===n.constructor.name))throw new Error("Expected Uint8Array");var n;if(e.length>0&&!e.includes(t.length))throw new Error(`Expected Uint8Array of length ${e}, not of length=${t.length}`)}function i(t,e=!0){if(t.destroyed)throw new Error("Hash instance has been destroyed");if(e&&t.finished)throw new Error("Hash#digest() has already been called")}const s="object"==typeof globalThis&&"crypto"in globalThis?globalThis.crypto:void 0; /*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */function f(t){return t instanceof Uint8Array||null!=t&&"object"==typeof t&&"Uint8Array"===t.constructor.name}const u=t=>new DataView(t.buffer,t.byteOffset,t.byteLength),a=(t,e)=>t<<32-e|t>>>e;if(!(68===new Uint8Array(new Uint32Array([287454020]).buffer)[0]))throw new Error("Non little-endian hardware is not supported");function c(t){if("string"==typeof t&&(t=function(t){if("string"!=typeof t)throw new Error("utf8ToBytes expected string, got "+typeof t);return new Uint8Array((new TextEncoder).encode(t))}(t)),!f(t))throw new Error("expected Uint8Array, got "+typeof t);return t}class h{clone(){return this._cloneInto()}}function l(t){const e=e=>t().update(c(e)).digest(),n=t();return e.outputLen=n.outputLen,e.blockLen=n.blockLen,e.create=()=>t(),e}function d(t=32){if(s&&"function"==typeof s.getRandomValues)return s.getRandomValues(new Uint8Array(t));throw new Error("crypto.getRandomValues must be defined")}class g extends h{constructor(t,e,n,r){super(),this.blockLen=t,this.outputLen=e,this.padOffset=n,this.isLE=r,this.finished=!1,this.length=0,this.pos=0,this.destroyed=!1,this.buffer=new Uint8Array(t),this.view=u(this.buffer)}update(t){i(this);const{view:e,buffer:n,blockLen:r}=this,o=(t=c(t)).length;for(let i=0;i<o;){const s=Math.min(r-this.pos,o-i);if(s!==r)n.set(t.subarray(i,i+s),this.pos),this.pos+=s,i+=s,this.pos===r&&(this.process(e,0),this.pos=0);else{const e=u(t);for(;r<=o-i;i+=r)this.process(e,i)}}return this.length+=t.length,this.roundClean(),this}digestInto(t){i(this),function(t,e){o(t);const n=e.outputLen;if(t.length<n)throw new Error(`digestInto() expects output buffer of length at least ${n}`)}(t,this),this.finished=!0;const{buffer:e,view:n,blockLen:r,isLE:s}=this;let{pos:f}=this;e[f++]=128,this.buffer.subarray(f).fill(0),this.padOffset>r-f&&(this.process(n,0),f=0);for(let t=f;t<r;t++)e[t]=0;!function(t,e,n,r){if("function"==typeof t.setBigUint64)return t.setBigUint64(e,n,r);const o=BigInt(32),i=BigInt(4294967295),s=Number(n>>o&i),f=Number(n&i),u=r?4:0,a=r?0:4;t.setUint32(e+u,s,r),t.setUint32(e+a,f,r)}(n,r-8,BigInt(8*this.length),s),this.process(n,0);const a=u(t),c=this.outputLen;if(c%4)throw new Error("_sha2: outputLen should be aligned to 32bit");const h=c/4,l=this.get();if(h>l.length)throw new Error("_sha2: outputLen bigger than state");for(let t=0;t<h;t++)a.setUint32(4*t,l[t],s)}digest(){const{buffer:t,outputLen:e}=this;this.digestInto(t);const n=t.slice(0,e);return this.destroy(),n}_cloneInto(t){t||(t=new this.constructor),t.set(...this.get());const{blockLen:e,buffer:n,length:r,finished:o,destroyed:i,pos:s}=this;return t.length=r,t.pos=s,t.finished=o,t.destroyed=i,r%e&&t.buffer.set(n),t}}const p=(t,e,n)=>t&e^t&n^e&n,y=new Uint32Array([1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298]),w=new Uint32Array([1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225]),b=new Uint32Array(64);class m extends g{constructor(){super(64,32,8,!1),this.A=0|w[0],this.B=0|w[1],this.C=0|w[2],this.D=0|w[3],this.E=0|w[4],this.F=0|w[5],this.G=0|w[6],this.H=0|w[7]}get(){const{A:t,B:e,C:n,D:r,E:o,F:i,G:s,H:f}=this;return[t,e,n,r,o,i,s,f]}set(t,e,n,r,o,i,s,f){this.A=0|t,this.B=0|e,this.C=0|n,this.D=0|r,this.E=0|o,this.F=0|i,this.G=0|s,this.H=0|f}process(t,e){for(let n=0;n<16;n++,e+=4)b[n]=t.getUint32(e,!1);for(let t=16;t<64;t++){const e=b[t-15],n=b[t-2],r=a(e,7)^a(e,18)^e>>>3,o=a(n,17)^a(n,19)^n>>>10;b[t]=o+b[t-7]+r+b[t-16]|0}let{A:n,B:r,C:o,D:i,E:s,F:f,G:u,H:c}=this;for(let t=0;t<64;t++){const e=c+(a(s,6)^a(s,11)^a(s,25))+((h=s)&f^~h&u)+y[t]+b[t]|0,l=(a(n,2)^a(n,13)^a(n,22))+p(n,r,o)|0;c=u,u=f,f=s,s=i+e|0,i=o,o=r,r=n,n=e+l|0}var h;n=n+this.A|0,r=r+this.B|0,o=o+this.C|0,i=i+this.D|0,s=s+this.E|0,f=f+this.F|0,u=u+this.G|0,c=c+this.H|0,this.set(n,r,o,i,s,f,u,c)}roundClean(){b.fill(0)}destroy(){this.set(0,0,0,0,0,0,0,0),this.buffer.fill(0)}}const E=l((()=>new m));function x(t){if(t>Number.MAX_SAFE_INTEGER)throw new TypeError("Number exceeds safe bounds!")}function _(t,e){if(t!==e)throw new TypeError(`Bech32 prefix does not match: ${t} !== ${e}`)}const A=new TextEncoder,v=new TextDecoder;function B(t){return A.encode(t)}function I(t){return v.decode(t)}function S(t,e){!function(t){if(null!==t.match(/[^a-fA-f0-9]/))throw new TypeError("Invalid characters in hex string: "+t);if(t.length%2!=0)throw new Error(`Length of hex string is invalid: ${t.length}`)}(t);const n=t.length/2;if(void 0===e&&(e=n),n>e)throw new TypeError(`Hex string is larger than array size: ${n} > ${e}`);return e} /*! scure-base - MIT License (c) 2022 Paul Miller (paulmillr.com) */ function U(t){return t instanceof Uint8Array||null!=t&&"object"==typeof t&&"Uint8Array"===t.constructor.name}function $(...t){const e=t=>t,n=(t,e)=>n=>t(e(n));return{encode:t.map((t=>t.encode)).reduceRight(n,e),decode:t.map((t=>t.decode)).reduce(n,e)}}function k(t){return{encode:e=>{if(!Array.isArray(e)||e.length&&"number"!=typeof e[0])throw new Error("alphabet.encode input should be an array of numbers");return e.map((e=>{if(e<0||e>=t.length)throw new Error(`Digit index outside alphabet: ${e} (alphabet: ${t.length})`);return t[e]}))},decode:e=>{if(!Array.isArray(e)||e.length&&"string"!=typeof e[0])throw new Error("alphabet.decode input should be array of strings");return e.map((e=>{if("string"!=typeof e)throw new Error(`alphabet.decode: not string element=${e}`);const n=t.indexOf(e);if(-1===n)throw new Error(`Unknown letter: "${e}". Allowed: ${t}`);return n}))}}}function O(t=""){if("string"!=typeof t)throw new Error("join separator should be string");return{encode:e=>{if(!Array.isArray(e)||e.length&&"string"!=typeof e[0])throw new Error("join.encode input should be array of strings");for(let t of e)if("string"!=typeof t)throw new Error(`join.encode: non-string input=${t}`);return e.join(t)},decode:e=>{if("string"!=typeof e)throw new Error("join.decode input should be string");return e.split(t)}}}function R(t,e,n){if(e<2)throw new Error(`convertRadix: wrong from=${e}, base cannot be less than 2`);if(n<2)throw new Error(`convertRadix: wrong to=${n}, base cannot be less than 2`);if(!Array.isArray(t))throw new Error("convertRadix: data should be array");if(!t.length)return[];let r=0;const o=[],i=Array.from(t);for(i.forEach((t=>{if(t<0||t>=e)throw new Error(`Wrong integer: ${t}`)}));;){let t=0,s=!0;for(let o=r;o<i.length;o++){const f=i[o],u=e*t+f;if(!Number.isSafeInteger(u)||e*t/e!==t||u-f!=e*t)throw new Error("convertRadix: carry overflow");t=u%n;const a=Math.floor(u/n);if(i[o]=a,!Number.isSafeInteger(a)||a*n+t!==u)throw new Error("convertRadix: carry overflow");s&&(a?s=!1:r=o)}if(o.push(t),s)break}for(let e=0;e<t.length-1&&0===t[e];e++)o.push(0);return o.reverse()}const N=(t,e)=>e?N(e,t%e):t,T=(t,e)=>t+(e-N(t,e));function L(t,e,n,r){if(!Array.isArray(t))throw new Error("convertRadix2: data should be array");if(e<=0||e>32)throw new Error(`convertRadix2: wrong from=${e}`);if(n<=0||n>32)throw new Error(`convertRadix2: wrong to=${n}`);if(T(e,n)>32)throw new Error(`convertRadix2: carry overflow from=${e} to=${n} carryBits=${T(e,n)}`);let o=0,i=0;const s=2**n-1,f=[];for(const r of t){if(r>=2**e)throw new Error(`convertRadix2: invalid data word=${r} from=${e}`);if(o=o<<e|r,i+e>32)throw new Error(`convertRadix2: carry overflow pos=${i} from=${e}`);for(i+=e;i>=n;i-=n)f.push((o>>i-n&s)>>>0);o&=2**i-1}if(o=o<<n-i&s,!r&&i>=e)throw new Error("Excess padding");if(!r&&o)throw new Error(`Non-zero padding: ${o}`);return r&&i>0&&f.push(o>>>0),f}function j(t,e=!1){if(t<=0||t>32)throw new Error("radix2: bits should be in (0..32]");if(T(8,t)>32||T(t,8)>32)throw new Error("radix2: carry overflow");return{encode:n=>{if(!U(n))throw new Error("radix2.encode input should be Uint8Array");return L(Array.from(n),8,t,!e)},decode:n=>{if(!Array.isArray(n)||n.length&&"number"!=typeof n[0])throw new Error("radix2.decode input should be array of numbers");return Uint8Array.from(L(n,t,8,e))}}}function q(t){if("function"!=typeof t)throw new Error("unsafeWrapper fn should be function");return function(...e){try{return t.apply(null,e)}catch(t){}}}const z=$(j(6),k("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"),function(t,e="="){if("string"!=typeof e)throw new Error("padding chr should be string");return{encode(n){if(!Array.isArray(n)||n.length&&"string"!=typeof n[0])throw new Error("padding.encode input should be array of strings");for(let t of n)if("string"!=typeof t)throw new Error(`padding.encode: non-string input=${t}`);for(;n.length*t%8;)n.push(e);return n},decode(n){if(!Array.isArray(n)||n.length&&"string"!=typeof n[0])throw new Error("padding.encode input should be array of strings");for(let t of n)if("string"!=typeof t)throw new Error(`padding.decode: non-string input=${t}`);let r=n.length;if(r*t%8)throw new Error("Invalid padding: string should have whole number of bytes");for(;r>0&&n[r-1]===e;r--)if(!((r-1)*t%8))throw new Error("Invalid padding: string has too much padding");return n.slice(0,r)}}}(6),O("")),H=$(j(6),k("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"),O("")),C=t=>{return $((e=58,{encode:t=>{if(!U(t))throw new Error("radix.encode input should be Uint8Array");return R(Array.from(t),256,e)},decode:t=>{if(!Array.isArray(t)||t.length&&"number"!=typeof t[0])throw new Error("radix.decode input should be array of numbers");return Uint8Array.from(R(t,e,256))}}),k(t),O(""));var e},P=C("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"),V=t=>$(function(t,e){if("function"!=typeof e)throw new Error("checksum fn should be function");return{encode(n){if(!U(n))throw new Error("checksum.encode: input should be Uint8Array");const r=e(n).slice(0,t),o=new Uint8Array(n.length+t);return o.set(n),o.set(r,n.length),o},decode(n){if(!U(n))throw new Error("checksum.decode: input should be Uint8Array");const r=n.slice(0,-t),o=e(r).slice(0,t),i=n.slice(-t);for(let e=0;e<t;e++)if(o[e]!==i[e])throw new Error("Invalid checksum");return r}}}(4,(e=>t(t(e)))),P),F=$(k("qpzry9x8gf2tvdw0s3jn54khce6mua7l"),O("")),D=[996825010,642813549,513874426,1027748829,705979059];function Z(t){const e=t>>25;let n=(33554431&t)<<5;for(let t=0;t<D.length;t++)1==(e>>t&1)&&(n^=D[t]);return n}function W(t,e,n=1){const r=t.length;let o=1;for(let e=0;e<r;e++){const n=t.charCodeAt(e);if(n<33||n>126)throw new Error(`Invalid prefix (${t})`);o=Z(o)^n>>5}o=Z(o);for(let e=0;e<r;e++)o=Z(o)^31&t.charCodeAt(e);for(let t of e)o=Z(o)^t;for(let t=0;t<6;t++)o=Z(o);return o^=n,F.encode(L([o%2**30],30,5,!1))}function G(t){const e="bech32"===t?1:734539939,n=j(5),r=n.decode,o=n.encode,i=q(r);function s(t,n=90){if("string"!=typeof t)throw new Error("bech32.decode input should be string, not "+typeof t);if(t.length<8||!1!==n&&t.length>n)throw new TypeError(`Wrong string length: ${t.length} (${t}). Expected (8..${n})`);const r=t.toLowerCase();if(t!==r&&t!==t.toUpperCase())throw new Error("String must be lowercase or uppercase");const o=(t=r).lastIndexOf("1");if(0===o||-1===o)throw new Error('Letter "1" must be present between prefix and data only');const i=t.slice(0,o),s=t.slice(o+1);if(s.length<6)throw new Error("Data must be at least 6 characters long");const f=F.decode(s).slice(0,-6),u=W(i,f,e);if(!s.endsWith(u))throw new Error(`Invalid checksum in ${t}: expected "${u}"`);return{prefix:i,words:f}}return{encode:function(t,n,r=90){if("string"!=typeof t)throw new Error("bech32.encode prefix should be string, not "+typeof t);if(!Array.isArray(n)||n.length&&"number"!=typeof n[0])throw new Error("bech32.encode words should be array of numbers, not "+typeof n);const o=t.length+7+n.length;if(!1!==r&&o>r)throw new TypeError(`Length ${o} exceeds limit ${r}`);const i=t.toLowerCase(),s=W(i,n,e);return`${i}1${F.encode(n)}${s}`},decode:s,decodeToBytes:function(t){const{prefix:e,words:n}=s(t,!1);return{prefix:e,words:n,bytes:r(n)}},decodeUnsafe:q(s),fromWords:r,fromWordsUnsafe:i,toWords:o}}const K=G("bech32"),Y=G("bech32m"),M=t=>V(E).encode(t),J=t=>V(E).decode(t),X=t=>z.encode(t),Q=t=>z.decode(t),tt=t=>H.encode(t),et=t=>H.decode(t),nt={to_words:K.toWords,to_bytes:K.fromWords,encode:(t,e,n=!1)=>K.encode(t,e,n),decode:(t,e=!1)=>{const{prefix:n,words:r}=K.decode(t,e);return{prefix:n,words:r}}},rt={to_words:Y.toWords,to_bytes:Y.fromWords,encode:(t,e,n=!1)=>Y.encode(t,e,n),decode:(t,e=!1)=>{const{prefix:n,words:r}=Y.decode(t,e);return{prefix:n,words:r}}},ot=BigInt(0),it=BigInt(255),st=BigInt(256);function ft(t,e,n="be"){void 0===e&&(e=function(t){if(t<=0xffn)return 1;if(t<=0xffffn)return 2;if(t<=0xffffffffn)return 4;if(t<=0xffffffffffffffffn)return 8;if(t<=0xffffffffffffffffffffffffffffffffn)return 16;if(t<=0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffn)return 32;throw new TypeError("Must specify a fixed buffer size for bigints greater than 32 bytes.")}(t));const r="le"===n,o=new ArrayBuffer(e),i=new DataView(o);let s=r?0:e-1;for(;t>ot;){const e=t&it,n=Number(e);r?i.setUint8(s++,n):i.setUint8(s--,n),t=(t-e)/st}return new Uint8Array(o)}function ut(t,e,n="be"){void 0===e&&(e=function(t){if(t<=255)return 1;if(t<=65535)return 2;if(t<=4294967295)return 4;throw new TypeError("Numbers larger than 4 bytes must specify a fixed size!")}(t));const r="le"===n,o=new ArrayBuffer(e),i=new DataView(o);let s=r?0:e-1;for(;t>0;){const e=255&t;r?i.setUint8(s++,t):i.setUint8(s--,t),t=(t-e)/256}return new Uint8Array(o)}function at(t){return null===t.match(/[^a-fA-F0-9]/)&&t.length%2==0}function ct(t){return!("string"!=typeof t||!at(t))||("number"==typeof t||"bigint"==typeof t||t instanceof Uint8Array||!(!Array.isArray(t)||!t.every((t=>"number"==typeof t))))}function ht(t,e,n="be"){void 0===e&&(e=t.length),function(t,e){if(t.length>e)throw new TypeError(`Data is larger than array size: ${t.length} > ${e}`)}(t,e);const r=new Uint8Array(e).fill(0),o="be"===n?0:e-t.length;return r.set(t,o),r}function lt(t){let e,n=0;const r=t.reduce(((t,e)=>t+e.length),0),o=new Uint8Array(r);for(e=0;e<t.length;e++){const r=t[e];o.set(r,n),n+=r.length}return o}function dt(t,e){return"bigint"==typeof e?`${e}n`:e}function gt(t,e){return"string"==typeof e&&/^[0-9]+n$/.test(e)?BigInt(e.slice(0,-1)):e}function pt(t,e,n){if(t instanceof ArrayBuffer)return new Uint8Array(t);if(t instanceof Uint8Array)return ht(t,e,n);if(Array.isArray(t)){return lt(t.map((t=>pt(t,e,n))))}if("string"==typeof t)return function(t,e,n="le"){e=S(t,e);const r="le"===n,o=new ArrayBuffer(e),i=new DataView(o);let s=r?0:e-1;for(let e=0;e<t.length;e+=2){const n=t.substring(e,e+2),o=parseInt(n,16);r?i.setUint8(s++,o):i.setUint8(s--,o)}return new Uint8Array(o)}(t,e,n);if("bigint"==typeof t)return ft(t,e,n);if("number"==typeof t)return ut(t,e,n);if("boolean"==typeof t)return Uint8Array.of(t?1:0);throw new TypeError("Unsupported format:"+String(typeof t))}class yt extends Uint8Array{static{this.num=wt}static{this.big=mt}static{this.bin=bt}static{this.raw=Et}static{this.str=xt}static{this.hex=_t}static{this.bytes=Ot}static{this.json=At}static{this.base64=vt}static{this.b64url=Bt}static{this.bech32=It}static{this.bech32m=St}static{this.b58chk=Ut}static{this.encode=B}static{this.decode=I}static{this.parse=$t}static{this.is_bytes=ct}static{this.is_hex=at}static{this.is_equal=kt}static random(t=32){const e=d(t);return new yt(e,t)}static now(t=4){const e=Math.floor(Date.now()/1e3);return new yt(e,t)}constructor(t,e,n){if(t instanceof yt&&void 0===e)return t;super(pt(t,e,n))}get arr(){return[...this]}get num(){return this.to_num()}get big(){return this.to_big()}get str(){return this.to_str()}get hex(){return this.to_hex()}get raw(){return new Uint8Array(this)}get bin(){return this.to_bin()}get b58chk(){return this.to_b58chk()}get base64(){return this.to_base64()}get b64url(){return this.to_b64url()}get digest(){return this.to_hash()}get id(){return this.to_hash().hex}get stream(){return new Rt(this)}to_num(t="be"){return function(t){let e=0;for(let n=t.length-1;n>=0;n--)e=256*e+t[n],x(e);return e}("be"===t?this.reverse():this)}to_big(t="be"){return function(t){let e=BigInt(0);for(let n=t.length-1;n>=0;n--)e=e*st+BigInt(t[n]);return BigInt(e)}("be"===t?this.reverse():this)}to_bin(){return function(t){const e=new Array(8*t.length);let n=0;for(const r of t){if(r>255)throw new Error(`Invalid byte value: ${r}. Byte values must be between 0 and 255.`);for(let t=7;t>=0;t--,n++)e[n]=r>>t&1}return e.join("")}(this)}to_hash(){const t=E(this);return new yt(t)}to_json(t){void 0===t&&(t=gt);const e=I(this);return JSON.parse(e,t)}to_bech32(t,e){const{encode:n,to_words:r}=nt;return n(t,r(this),e)}to_bech32m(t,e){const{encode:n,to_words:r}=rt;return n(t,r(this),e)}to_str(){return I(this)}to_hex(){return function(t){let e="";for(let n=0;n<t.length;n++)e+=t[n].toString(16).padStart(2,"0");return e}(this)}to_bytes(){return new Uint8Array(this)}to_b58chk(){return M(this)}to_base64(){return X(this)}to_b64url(){return tt(this)}append(t){return yt.join([this,yt.bytes(t)])}equals(t){return Ot(t).hex===this.hex}prepend(t){return yt.join([yt.bytes(t),this])}reverse(){const t=new Uint8Array(this).reverse();return new yt(t)}slice(t,e){const n=new Uint8Array(this).slice(t,e);return new yt(n)}set(t,e){this.set(t,e)}subarray(t,e){const n=new Uint8Array(this).subarray(t,e);return new yt(n)}write(t,e){const n=yt.bytes(t);this.set(n,e)}add_varint(t){const e=yt.calc_varint(this.length,t);return yt.join([e,this])}toJSON(){return this.hex}toString(){return this.hex}static from(t){return new yt(Uint8Array.from(t))}static of(...t){return new yt(Uint8Array.of(...t))}static join(t){const e=lt(t.map((t=>yt.bytes(t))));return new yt(e)}static sort(t,e){const n=t.map((t=>Ot(t,e).hex));return n.sort(),n.map((t=>yt.hex(t,e)))}static calc_varint(t,e){if(t<253)return yt.num(t,1);if(t<65536)return yt.of(253,...yt.num(t,2,e));if(t<4294967296)return yt.of(254,...yt.num(t,4,e));if(BigInt(t)<0x10000000000000000n)return yt.of(255,...yt.num(t,8,e));throw new Error(`Value is too large: ${t}`)}}function wt(t,e,n){return new yt(t,e,n)}function bt(t,e,n){return new yt(function(t){const e=t.split("").map(Number);if(e.length%8!=0)throw new Error(`Binary array is invalid length: ${t.length}`);const n=new Uint8Array(e.length/8);for(let t=0,r=0;t<e.length;t+=8,r++){let o=0;for(let n=0;n<8;n++)o|=e[t+n]<<7-n;n[r]=o}return n}(t),e,n)}function mt(t,e,n){return new yt(t,e,n)}function Et(t,e,n){return new yt(t,e,n)}function xt(t,e,n){return new yt(B(t),e,n)}function _t(t,e,n){return new yt(t,e,n)}function At(t,e){void 0===e&&(e=dt);const n=JSON.stringify(t,e);return new yt(B(n))}function vt(t){return new yt(Q(t))}function Bt(t){return new yt(et(t))}function It(t,e,n){const{decode:r,to_bytes:o}=nt,{prefix:i,words:s}=r(t,e),f=o(s);return"string"==typeof n&&_(i,n),new yt(f)}function St(t,e,n){const{decode:r,to_bytes:o}=rt,{prefix:i,words:s}=r(t,e),f=o(s);return"string"==typeof n&&_(i,n),new yt(f)}function Ut(t){return new yt(J(t))}function $t(t,e,n){const r=function(t,e,n){const r=t.length,o=n/e;if(n%e!=0)throw new TypeError(`Invalid parameters: ${n} % ${e} !== 0`);if(r!==n)throw new TypeError(`Invalid data stream: ${r} !== ${n}`);if(r%e!=0)throw new TypeError(`Invalid data stream: ${r} % ${e} !== 0`);const i=new Array(o);for(let n=0;n<o;n++){const r=n*e;i[n]=t.subarray(r,r+e)}return i}(pt(t),e,n);return r.map((t=>yt.bytes(t)))}function kt(t,e){return new yt(t).hex===new yt(e).hex}function Ot(t,e,n){return new yt(t,e,n)}class Rt{constructor(t){this.data=yt.bytes(t),this.size=this.data.length}peek(t){if(t>this.size)throw new Error(`Size greater than stream: ${t} > ${this.size}`);return new yt(this.data.slice(0,t))}read(t){const e=this.peek(t);return this.data=this.data.slice(t),this.size=this.data.length,e}read_varint(t){const e=this.read(1).num;switch(!0){case e>=0&&e<253:return e;case 253===e:return this.read(2).to_num(t);case 254===e:return this.read(4).to_num(t);case 255===e:return this.read(8).to_num(t);default:throw new Error(`Varint is out of range: ${e}`)}}}class Nt extends h{constructor(t,e){super(),this.finished=!1,this.destroyed=!1,function(t){if("function"!=typeof t||"function"!=typeof t.create)throw new Error("Hash should be wrapped by utils.wrapConstructor");r(t.outputLen),r(t.blockLen)}(t);const n=c(e);if(this.iHash=t.create(),"function"!=typeof this.iHash.update)throw new Error("Expected instance of class which extends utils.Hash");this.blockLen=this.iHash.blockLen,this.outputLen=this.iHash.outputLen;const o=this.blockLen,i=new Uint8Array(o);i.set(n.length>o?t.create().update(n).digest():n);for(let t=0;t<i.length;t++)i[t]^=54;this.iHash.update(i),this.oHash=t.create();for(let t=0;t<i.length;t++)i[t]^=106;this.oHash.update(i),i.fill(0)}update(t){return i(this),this.iHash.update(t),this}digestInto(t){i(this),o(t,this.outputLen),this.finished=!0,this.iHash.digestInto(t),this.oHash.update(t),this.oHash.digestInto(t),this.destroy()}digest(){const t=new Uint8Array(this.oHash.outputLen);return this.digestInto(t),t}_cloneInto(t){t||(t=Object.create(Object.getPrototypeOf(this),{}));const{oHash:e,iHash:n,finished:r,destroyed:o,blockLen:i,outputLen:s}=this;return t.finished=r,t.destroyed=o,t.blockLen=i,t.outputLen=s,t.oHash=e._cloneInto(t.oHash),t.iHash=n._cloneInto(t.iHash),t}destroy(){this.destroyed=!0,this.oHash.destroy(),this.iHash.destroy()}}const Tt=(t,e,n)=>new Nt(t,e).update(n).digest();function Lt(t,...e){const n=function(t){const e=yt.str(t).digest;return yt.join([e,e])}(t);return yt.join([n,...e]).digest} /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */Tt.create=(t,e)=>new Nt(t,e);const jt=BigInt(0),qt=BigInt(1),zt=BigInt(2);function Ht(t){return t instanceof Uint8Array||null!=t&&"object"==typeof t&&"Uint8Array"===t.constructor.name}const Ct=Array.from({length:256},((t,e)=>e.toString(16).padStart(2,"0")));function Pt(t){if(!Ht(t))throw new Error("Uint8Array expected");let e="";for(let n=0;n<t.length;n++)e+=Ct[t[n]];return e}function Vt(t){const e=t.toString(16);return 1&e.length?`0${e}`:e}function Ft(t){if("string"!=typeof t)throw new Error("hex string expected, got "+typeof t);return BigInt(""===t?"0":`0x${t}`)}const Dt={_0:48,_9:57,_A:65,_F:70,_a:97,_f:102};function Zt(t){return t>=Dt._0&&t<=Dt._9?t-Dt._0:t>=Dt._A&&t<=Dt._F?t-(Dt._A-10):t>=Dt._a&&t<=Dt._f?t-(Dt._a-10):void 0}function Wt(t){if("string"!=typeof t)throw new Error("hex string expected, got "+typeof t);const e=t.length,n=e/2;if(e%2)throw new Error("padded hex string expected, got unpadded hex of length "+e);const r=new Uint8Array(n);for(let e=0,o=0;e<n;e++,o+=2){const n=Zt(t.charCodeAt(o)),i=Zt(t.charCodeAt(o+1));if(void 0===n||void 0===i){const e=t[o]+t[o+1];throw new Error('hex string expected, got non-hex character "'+e+'" at index '+o)}r[e]=16*n+i}return r}function Gt(t){return Ft(Pt(t))}function Kt(t){if(!Ht(t))throw new Error("Uint8Array expected");return Ft(Pt(Uint8Array.from(t).reverse()))}function Yt(t,e){return Wt(t.toString(16).padStart(2*e,"0"))}function Mt(t,e){return Yt(t,e).reverse()}function Jt(t,e,n){let r;if("string"==typeof e)try{r=Wt(e)}catch(n){throw new Error(`${t} must be valid hex string, got "${e}". Cause: ${n}`)}else{if(!Ht(e))throw new Error(`${t} must be hex string or Uint8Array`);r=Uint8Array.from(e)}const o=r.length;if("number"==typeof n&&o!==n)throw new Error(`${t} expected ${n} bytes, got ${o}`);return r}function Xt(...t){let e=0;for(let n=0;n<t.length;n++){const r=t[n];if(!Ht(r))throw new Error("Uint8Array expected");e+=r.length}let n=new Uint8Array(e),r=0;for(let e=0;e<t.length;e++){const o=t[e];n.set(o,r),r+=o.length}return n}const Qt=t=>(zt<<BigInt(t-1))-qt,te=t=>new Uint8Array(t),ee=t=>Uint8Array.from(t);function ne(t,e,n){if("number"!=typeof t||t<2)throw new Error("hashLen must be a number");if("number"!=typeof e||e<2)throw new Error("qByteLen must be a number");if("function"!=typeof n)throw new Error("hmacFn must be a function");let r=te(t),o=te(t),i=0;const s=()=>{r.fill(1),o.fill(0),i=0},f=(...t)=>n(o,r,...t),u=(t=te())=>{o=f(ee([0]),t),r=f(),0!==t.length&&(o=f(ee([1]),t),r=f())},a=()=>{if(i++>=1e3)throw new Error("drbg: tried 1000 values");let t=0;const n=[];for(;t<e;){r=f();const e=r.slice();n.push(e),t+=r.length}return Xt(...n)};return(t,e)=>{let n;for(s(),u(t);!(n=e(a()));)u();return s(),n}}const re={bigint:t=>"bigint"==typeof t,function:t=>"function"==typeof t,boolean:t=>"boolean"==typeof t,string:t=>"string"==typeof t,stringOrUint8Array:t=>"string"==typeof t||Ht(t),isSafeInteger:t=>Number.isSafeInteger(t),array:t=>Array.isArray(t),field:(t,e)=>e.Fp.isValid(t),hash:t=>"function"==typeof t&&Number.isSafeInteger(t.outputLen)};function oe(t,e,n={}){const r=(e,n,r)=>{const o=re[n];if("function"!=typeof o)throw new Error(`Invalid validator "${n}", expected function`);const i=t[e];if(!(r&&void 0===i||o(i,t)))throw new Error(`Invalid param ${String(e)}=${i} (${typeof i}), expected ${n}`)};for(const[t,n]of Object.entries(e))r(t,n,!1);for(const[t,e]of Object.entries(n))r(t,e,!0);return t}var ie=Object.freeze({__proto__:null,bitGet:function(t,e){return t>>BigInt(e)&qt},bitLen:function(t){let e;for(e=0;t>jt;t>>=qt,e+=1);return e},bitMask:Qt,bitSet:(t,e,n)=>t|(n?qt:jt)<<BigInt(e),bytesToHex:Pt,bytesToNumberBE:Gt,bytesToNumberLE:Kt,concatBytes:Xt,createHmacDrbg:ne,ensureBytes:Jt,equalBytes:function(t,e){if(t.length!==e.length)return!1;let n=0;for(let r=0;r<t.length;r++)n|=t[r]^e[r];return 0===n},hexToBytes:Wt,hexToNumber:Ft,isBytes:Ht,numberToBytesBE:Yt,numberToBytesLE:Mt,numberToHexUnpadded:Vt,numberToVarBytesBE:function(t){return Wt(Vt(t))},utf8ToBytes:function(t){if("string"!=typeof t)throw new Error("utf8ToBytes expected string, got "+typeof t);return new Uint8Array((new TextEncoder).encode(t))},validateObject:oe}); /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */const se=BigInt(0),fe=BigInt(1),ue=BigInt(2),ae=BigInt(3),ce=BigInt(4),he=BigInt(5),le=BigInt(8);function de(t,e){const n=t%e;return n>=se?n:e+n}function ge(t,e,n){if(n<=se||e<se)throw new Error("Expected power/modulo > 0");if(n===fe)return se;let r=fe;for(;e>se;)e&fe&&(r=r*t%n),t=t*t%n,e>>=fe;return r}function pe(t,e,n){let r=t;for(;e-- >se;)r*=r,r%=n;return r}function ye(t,e){if(t===se||e<=se)throw new Error(`invert: expected positive integers, got n=${t} mod=${e}`);let n=de(t,e),r=e,o=se,i=fe;for(;n!==se;){const t=r%n,e=o-i*(r/n);r=n,n=t,o=i,i=e}if(r!==fe)throw new Error("invert: does not exist");return de(o,e)}function we(t){if(t%ce===ae){const e=(t+fe)/ce;return function(t,n){const r=t.pow(n,e);if(!t.eql(t.sqr(r),n))throw new Error("Cannot find square root");return r}}if(t%le===he){const e=(t-he)/le;return function(t,n){const r=t.mul(n,ue),o=t.pow(r,e),i=t.mul(n,o),s=t.mul(t.mul(i,ue),o),f=t.mul(i,t.sub(s,t.ONE));if(!t.eql(t.sqr(f),n))throw new Error("Cannot find square root");return f}}return function(t){const e=(t-fe)/ue;let n,r,o;for(n=t-fe,r=0;n%ue===se;n/=ue,r++);for(o=ue;o<t&&ge(o,e,t)!==t-fe;o++);if(1===r){const e=(t+fe)/ce;return function(t,n){const r=t.pow(n,e);if(!t.eql(t.sqr(r),n))throw new Error("Cannot find square root");return r}}const i=(n+fe)/ue;return function(t,s){if(t.pow(s,e)===t.neg(t.ONE))throw new Error("Cannot find square root");let f=r,u=t.pow(t.mul(t.ONE,o),n),a=t.pow(s,i),c=t.pow(s,n);for(;!t.eql(c,t.ONE);){if(t.eql(c,t.ZERO))return t.ZERO;let e=1;for(let n=t.sqr(c);e<f&&!t.eql(n,t.ONE);e++)n=t.sqr(n);const n=t.pow(u,fe<<BigInt(f-e-1));u=t.sqr(n),a=t.mul(a,n),c=t.mul(c,u),f=e}return a}}(t)}BigInt(9),BigInt(16);const be=["create","isValid","is0","neg","inv","sqrt","sqr","eql","add","sub","mul","pow","div","addN","subN","mulN","sqrN"];function me(t,e){const n=void 0!==e?e:t.toString(2).length;return{nBitLength:n,nByteLength:Math.ceil(n/8)}}function Ee(t,e,n=!1,r={}){if(t<=se)throw new Error(`Expected Field ORDER > 0, got ${t}`);const{nBitLength:o,nByteLength:i}=me(t,e);if(i>2048)throw new Error("Field lengths over 2048 bytes are not supported");const s=we(t),f=Object.freeze({ORDER:t,BITS:o,BYTES:i,MASK:Qt(o),ZERO:se,ONE:fe,create:e=>de(e,t),isValid:e=>{if("bigint"!=typeof e)throw new Error("Invalid field element: expected bigint, got "+typeof e);return se<=e&&e<t},is0:t=>t===se,isOdd:t=>(t&fe)===fe,neg:e=>de(-e,t),eql:(t,e)=>t===e,sqr:e=>de(e*e,t),add:(e,n)=>de(e+n,t),sub:(e,n)=>de(e-n,t),mul:(e,n)=>de(e*n,t),pow:(t,e)=>function(t,e,n){if(n<se)throw new Error("Expected power > 0");if(n===se)return t.ONE;if(n===fe)return e;let r=t.ONE,o=e;for(;n>se;)n&fe&&(r=t.mul(r,o)),o=t.sqr(o),n>>=fe;return r}(f,t,e),div:(e,n)=>de(e*ye(n,t),t),sqrN:t=>t*t,addN:(t,e)=>t+e,subN:(t,e)=>t-e,mulN:(t,e)=>t*e,inv:e=>ye(e,t),sqrt:r.sqrt||(t=>s(f,t)),invertBatch:t=>function(t,e){const n=new Array(e.length),r=e.reduce(((e,r,o)=>t.is0(r)?e:(n[o]=e,t.mul(e,r))),t.ONE),o=t.inv(r);return e.reduceRight(((e,r,o)=>t.is0(r)?e:(n[o]=t.mul(e,n[o]),t.mul(e,r))),o),n}(f,t),cmov:(t,e,n)=>n?e:t,toBytes:t=>n?Mt(t,i):Yt(t,i),fromBytes:t=>{if(t.length!==i)throw new Error(`Fp.fromBytes: expected ${i}, got ${t.length}`);return n?Kt(t):Gt(t)}});return Object.freeze(f)}function xe(t){if("bigint"!=typeof t)throw new Error("field order must be bigint");const e=t.toString(2).length;return Math.ceil(e/8)}function _e(t){const e=xe(t);return e+Math.ceil(e/2)} /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */ const Ae=BigInt(0),ve=BigInt(1);function Be(t){return oe(t.Fp,be.reduce(((t,e)=>(t[e]="function",t)),{ORDER:"bigint",MASK:"bigint",BYTES:"isSafeInteger",BITS:"isSafeInteger"})),oe(t,{n:"bigint",h:"bigint",Gx:"field",Gy:"field"},{nBitLength:"isSafeInteger",nByteLength:"isSafeInteger"}),Object.freeze({...me(t.n,t.nBitLength),...t,p:t.Fp.ORDER})} /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */const{bytesToNumberBE:Ie,hexToBytes:Se}=ie,Ue={Err:class extends Error{constructor(t=""){super(t)}},_parseInt(t){const{Err:e}=Ue;if(t.length<2||2!==t[0])throw new e("Invalid signature integer tag");const n=t[1],r=t.subarray(2,n+2);if(!n||r.length!==n)throw new e("Invalid signature integer: wrong length");if(128&r[0])throw new e("Invalid signature integer: negative");if(0===r[0]&&!(128&r[1]))throw new e("Invalid signature integer: unnecessary leading zero");return{d:Ie(r),l:t.subarray(n+2)}},toSig(t){const{Err:e}=Ue,n="string"==typeof t?Se(t):t;if(!Ht(n))throw new Error("ui8a expected");let r=n.length;if(r<2||48!=n[0])throw new e("Invalid signature tag");if(n[1]!==r-2)throw new e("Invalid signature: incorrect length");const{d:o,l:i}=Ue._parseInt(n.subarray(2)),{d:s,l:f}=Ue._parseInt(i);if(f.length)throw new e("Invalid signature: left bytes after parsing");return{r:o,s:s}},hexFromSig(t){const e=t=>8&Number.parseInt(t[0],16)?"00"+t:t,n=t=>{const e=t.toString(16);return 1&e.length?`0${e}`:e},r=e(n(t.s)),o=e(n(t.r)),i=r.length/2,s=o.length/2,f=n(i),u=n(s);return`30${n(s+i+4)}02${u}${o}02${f}${r}`}},$e=BigInt(0),ke=BigInt(1);BigInt(2);const Oe=BigInt(3);function Re(t){const e=function(t){const e=Be(t);oe(e,{a:"field",b:"field"},{allowedPrivateKeyLengths:"array",wrapPrivateKey:"boolean",isTorsionFree:"function",clearCofactor:"function",allowInfinityPoint:"boolean",fromBytes:"function",toBytes:"function"});const{endo:n,Fp:r,a:o}=e;if(n){if(!r.eql(o,r.ZERO))throw new Error("Endomorphism can only be defined for Koblitz curves that have a=0");if("object"!=typeof n||"bigint"!=typeof n.beta||"function"!=typeof n.splitScalar)throw new Error("Expected endomorphism with beta: bigint and splitScalar: function")}return Object.freeze({...e})}(t),{Fp:n}=e,r=e.toBytes||((t,e,r)=>{const o=e.toAffine();return Xt(Uint8Array.from([4]),n.toBytes(o.x),n.toBytes(o.y))}),o=e.fromBytes||(t=>{const e=t.subarray(1);return{x:n.fromBytes(e.subarray(0,n.BYTES)),y:n.fromBytes(e.subarray(n.BYTES,2*n.BYTES))}});function i(t){const{a:r,b:o}=e,i=n.sqr(t),s=n.mul(i,t);return n.add(n.add(s,n.mul(t,r)),o)}if(!n.eql(n.sqr(e.Gy),i(e.Gx)))throw new Error("bad generator point: equation left != right");function s(t){return"bigint"==typeof t&&$e<t&&t<e.n}function f(t){if(!s(t))throw new Error("Expected valid bigint: 0 < bigint < curve.n")}function u(t){const{allowedPrivateKeyLengths:n,nByteLength:r,wrapPrivateKey:o,n:i}=e;if(n&&"bigint"!=typeof t){if(Ht(t)&&(t=Pt(t)),"string"!=typeof t||!n.includes(t.length))throw new Error("Invalid key");t=t.padStart(2*r,"0")}let s;try{s="bigint"==typeof t?t:Gt(Jt("private key",t,r))}catch(e){throw new Error(`private key must be ${r} bytes, hex or bigint, not ${typeof t}`)}return o&&(s=de(s,i)),f(s),s}const a=new Map;function c(t){if(!(t instanceof h))throw new Error("ProjectivePoint expected")}class h{constructor(t,e,r){if(this.px=t,this.py=e,this.pz=r,null==t||!n.isValid(t))throw new Error("x required");if(null==e||!n.isValid(e))throw new Error("y required");if(null==r||!n.isValid(r))throw new Error("z required")}static fromAffine(t){const{x:e,y:r}=t||{};if(!t||!n.isValid(e)||!n.isValid(r))throw new Error("invalid affine point");if(t instanceof h)throw new Error("projective point not allowed");const o=t=>n.eql(t,n.ZERO);return o(e)&&o(r)?h.ZERO:new h(e,r,n.ONE)}get x(){return this.toAffine().x}get y(){return this.toAffine().y}static normalizeZ(t){const e=n.invertBatch(t.map((t=>t.pz)));return t.map(((t,n)=>t.toAffine(e[n]))).map(h.fromAffine)}static fromHex(t){const e=h.fromAffine(o(Jt("pointHex",t)));return e.assertValidity(),e}static fromPrivateKey(t){return h.BASE.multiply(u(t))}_setWindowSize(t){this._WINDOW_SIZE=t,a.delete(this)}assertValidity(){if(this.is0()){if(e.allowInfinityPoint&&!n.is0(this.py))return;throw new Error("bad point: ZERO")}const{x:t,y:r}=this.toAffine();if(!n.isValid(t)||!n.isValid(r))throw new Error("bad point: x or y not FE");const o=n.sqr(r),s=i(t);if(!n.eql(o,s))throw new Error("bad point: equation left != right");if(!this.isTorsionFree())throw new Error("bad point: not in prime-order subgroup")}hasEvenY(){const{y:t}=this.toAffine();if(n.isOdd)return!n.isOdd(t);throw new Error("Field doesn't support isOdd")}equals(t){c(t);const{px:e,py:r,pz:o}=this,{px:i,py:s,pz:f}=t,u=n.eql(n.mul(e,f),n.mul(i,o)),a=n.eql(n.mul(r,f),n.mul(s,o));return u&&a}negate(){return new h(this.px,n.neg(this.py),this.pz)}double(){const{a:t,b:r}=e,o=n.mul(r,Oe),{px:i,py:s,pz:f}=this;let u=n.ZERO,a=n.ZERO,c=n.ZERO,l=n.mul(i,i),d=n.mul(s,s),g=n.mul(f,f),p=n.mul(i,s);return p=n.add(p,p),c=n.mul(i,f),c=n.add(c,c),u=n.mul(t,c),a=n.mul(o,g),a=n.add(u,a),u=n.sub(d,a),a=n.add(d,a),a=n.mul(u,a),u=n.mul(p,u),c=n.mul(o,c),g=n.mul(t,g),p=n.sub(l,g),p=n.mul(t,p),p=n.add(p,c),c=n.add(l,l),l=n.add(c,l),l=n.add(l,g),l=n.mul(l,p),a=n.add(a,l),g=n.mul(s,f),g=n.add(g,g),l=n.mul(g,p),u=n.sub(u,l),c=n.mul(g,d),c=n.add(c,c),c=n.add(c,c),new h(u,a,c)}add(t){c(t);const{px:r,py:o,pz:i}=this,{px:s,py:f,pz:u}=t;let a=n.ZERO,l=n.ZERO,d=n.ZERO;const g=e.a,p=n.mul(e.b,Oe);let y=n.mul(r,s),w=n.mul(o,f),b=n.mul(i,u),m=n.add(r,o),E=n.add(s,f);m=n.mul(m,E),E=n.add(y,w),m=n.sub(m,E),E=n.add(r,i);let x=n.add(s,u);return E=n.mul(E,x),x=n.add(y,b),E=n.sub(E,x),x=n.add(o,i),a=n.add(f,u),x=n.mul(x,a),a=n.add(w,b),x=n.sub(x,a),d=n.mul(g,E),a=n.mul(p,b),d=n.add(a,d),a=n.sub(w,d),d=n.add(w,d),l=n.mul(a,d),w=n.add(y,y),w=n.add(w,y),b=n.mul(g,b),E=n.mul(p,E),w=n.add(w,b),b=n.sub(y,b),b=n.mul(g,b),E=n.add(E,b),y=n.mul(w,E),l=n.add(l,y),y=n.mul(x,E),a=n.mul(m,a),a=n.sub(a,y),y=n.mul(m,w),d=n.mul(x,d),d=n.add(d,y),new h(a,l,d)}subtract(t){return this.add(t.negate())}is0(){return this.equals(h.ZERO)}wNAF(t){return d.wNAFCached(this,a,t,(t=>{const e=n.invertBatch(t.map((t=>t.pz)));return t.map(((t,n)=>t.toAffine(e[n]))).map(h.fromAffine)}))}multiplyUnsafe(t){const r=h.ZERO;if(t===$e)return r;if(f(t),t===ke)return this;const{endo:o}=e;if(!o)return d.unsafeLadder(this,t);let{k1neg:i,k1:s,k2neg:u,k2:a}=o.splitScalar(t),c=r,l=r,g=this;for(;s>$e||a>$e;)s&ke&&(c=c.add(g)),a&ke&&(l=l.add(g)),g=g.double(),s>>=ke,a>>=ke;return i&&(c=c.negate()),u&&(l=l.negate()),l=new h(n.mul(l.px,o.beta),l.py,l.pz),c.add(l)}multiply(t){f(t);let r,o,i=t;const{endo:s}=e;if(s){const{k1neg:t,k1:e,k2neg:f,k2:u}=s.splitScalar(i);let{p:a,f:c}=this.wNAF(e),{p:l,f:g}=this.wNAF(u);a=d.constTimeNegate(t,a),l=d.constTimeNegate(f,l),l=new h(n.mul(l.px,s.beta),l.py,l.pz),r=a.add(l),o=c.add(g)}else{const{p:t,f:e}=this.wNAF(i);r=t,o=e}return h.normalizeZ([r,o])[0]}multiplyAndAddUnsafe(t,e,n){const r=h.BASE,o=(t,e)=>e!==$e&&e!==ke&&t.equals(r)?t.multiply(e):t.multiplyUnsafe(e),i=o(this,e).add(o(t,n));return i.is0()?void 0:i}toAffine(t){const{px:e,py:r,pz:o}=this,i=this.is0();null==t&&(t=i?n.ONE:n.inv(o));const s=n.mul(e,t),f=n.mul(r,t),u=n.mul(o,t);if(i)return{x:n.ZERO,y:n.ZERO};if(!n.eql(u,n.ONE))throw new Error("invZ was invalid");return{x:s,y:f}}isTorsionFree(){const{h:t,isTorsionFree:n}=e;if(t===ke)return!0;if(n)return n(h,this);throw new Error("isTorsionFree() has not been declared for the elliptic curve")}clearCofactor(){const{h:t,clearCofactor:n}=e;return t===ke?this:n?n(h,this):this.multiplyUnsafe(e.h)}toRawBytes(t=!0){return this.assertValidity(),r(h,this,t)}toHex(t=!0){return Pt(this.toRawBytes(t))}}h.BASE=new h(e.Gx,e.Gy,n.ONE),h.ZERO=new h(n.ZERO,n.ONE,n.ZERO);const l=e.nBitLength,d=function(t,e){const n=(t,e)=>{const n=e.negate();return t?n:e},r=t=>({windows:Math.ceil(e/t)+1,windowSize:2**(t-1)});return{constTimeNegate:n,unsafeLadder(e,n){let r=t.ZERO,o=e;for(;n>Ae;)n&ve&&(r=r.add(o)),o=o.double(),n>>=ve;return r},precomputeWindow(t,e){const{windows:n,windowSize:o}=r(e),i=[];let s=t,f=s;for(let t=0;t<n;t++){f=s,i.push(f);for(let t=1;t<o;t++)f=f.add(s),i.push(f);s=f.double()}return i},wNAF(e,o,i){const{windows:s,windowSize:f}=r(e);let u=t.ZERO,a=t.BASE;const c=BigInt(2**e-1),h=2**e,l=BigInt(e);for(let t=0;t<s;t++){const e=t*f;let r=Number(i&c);i>>=l,r>f&&(r-=h,i+=ve);const s=e,d=e+Math.abs(r)-1,g=t%2!=0,p=r<0;0===r?a=a.add(n(g,o[s])):u=u.add(n(p,o[d]))}return{p:u,f:a}},wNAFCached(t,e,n,r){const o=t._WINDOW_SIZE||1;let i=e.get(t);return i||(i=this.precomputeWindow(t,o),1!==o&&e.set(t,r(i))),this.wNAF(o,i,n)}}}(h,e.endo?Math.ceil(l/2):l);return{CURVE:e,ProjectivePoint:h,normPrivateKeyToScalar:u,weierstrassEquation:i,isWithinCurveOrder:s}}function Ne(t){const e=function(t){const e=Be(t);return oe(e,{hash:"hash",hmac:"function",randomBytes:"function"},{bits2int:"function",bits2int_modN:"function",lowS:"boolean"}),Object.freeze({lowS:!0,...e})}(t),{Fp:n,n:r}=e,o=n.BYTES+1,i=2*n.BYTES+1;function s(t){return de(t,r)}function f(t){return ye(t,r)}const{ProjectivePoint:u,normPrivateKeyToScalar:a,weierstrassEquation:c,isWithinCurveOrder:h}=Re({...e,toBytes(t,e,r){const o=e.toAffine(),i=n.toBytes(o.x),s=Xt;return r?s(Uint8Array.from([e.hasEvenY()?2:3]),i):s(Uint8Array.from([4]),i,n.toBytes(o.y))},fromBytes(t){const e=t.length,r=t[0],s=t.subarray(1);if(e!==o||2!==r&&3!==r){if(e===i&&4===r){return{x:n.fromBytes(s.subarray(0,n.BYTES)),y:n.fromBytes(s.subarray(n.BYTES,2*n.BYTES))}}throw new Error(`Point of length ${e} was invalid. Expected ${o} compressed bytes or ${i} uncompressed bytes`)}{const t=Gt(s);if(!($e<(f=t)&&f<n.ORDER))throw new Error("Point is not on curve");const e=c(t);let o=n.sqrt(e);return 1==(1&r)!==((o&ke)===ke)&&(o=n.neg(o)),{x:t,y:o}}var f}}),l=t=>Pt(Yt(t,e.nByteLength));function d(t){return t>r>>ke}const g=(t,e,n)=>Gt(t.slice(e,n));class p{constructor(t,e,n){this.r=t,this.s=e,this.recovery=n,this.assertValidity()}static fromCompact(t){const n=e.nByteLength;return t=Jt("compactSignature",t,2*n),new p(g(t,0,n),g(t,n,2*n))}static fromDER(t){const{r:e,s:n}=Ue.toSig(Jt("DER",t));return new p(e,n)}assertValidity(){if(!h(this.r))throw new Error("r must be 0 < r < CURVE.n");if(!h(this.s))throw new Error("s must be 0 < s < CURVE.n")}addRecoveryBit(t){return new p(this.r,this.s,t)}recoverPublicKey(t){const{r:r,s:o,recovery:i}=this,a=m(Jt("msgHash",t));if(null==i||![0,1,2,3].includes(i))throw new Error("recovery id invalid");const c=2===i||3===i?r+e.n:r;if(c>=n.ORDER)throw new Error("recovery id 2 or 3 invalid");const h=0==(1&i)?"02":"03",d=u.fromHex(h+l(c)),g=f(c),p=s(-a*g),y=s(o*g),w=u.BASE.multiplyAndAddUnsafe(d,p,y);if(!w)throw new Error("point at infinify");return w.assertValidity(),w}hasHighS(){return d(this.s)}normalizeS(){return this.hasHighS()?new p(this.r,s(-this.s),this.recovery):this}toDERRawBytes(){return Wt(this.toDERHex())}toDERHex(){return Ue.hexFromSig({r:this.r,s:this.s})}toCompactRawBytes(){return Wt(this.toCompactHex())}toCompactHex(){return l(this.r)+l(this.s)}}const y={isValidPrivateKey(t){try{return a(t),!0}catch(t){return!1}},normPrivateKeyToScalar:a,randomPrivateKey:()=>{const t=_e(e.n);return function(t,e,n=!1){const r=t.length,o=xe(e),i=_e(e);if(r<16||r<i||r>1024)throw new Error(`expected ${i}-1024 bytes of input, got ${r}`);const s=de(n?Gt(t):Kt(t),e-fe)+fe;return n?Mt(s,o):Yt(s,o)}(e.randomBytes(t),e.n)},precompute:(t=8,e=u.BASE)=>(e._setWindowSize(t),e.multiply(BigInt(3)),e)};function w(t){const e=Ht(t),n="string"==typeof t,r=(e||n)&&t.length;return e?r===o||r===i:n?r===2*o||r===2*i:t instanceof u}const b=e.bits2int||function(t){const n=Gt(t),r=8*t.length-e.nBitLength;return r>0?n>>BigInt(r):n},m=e.bits2int_modN||function(t){return s(b(t))},E=Qt(e.nBitLength);function x(t){if("bigint"!=typeof t)throw new Error("bigint expected");if(!($e<=t&&t<E))throw new Error(`bigint expected < 2^${e.nBitLength}`);return Yt(t,e.nByteLength)}function _(t,r,o=A){if(["recovered","canonical"].some((t=>t in o)))throw new Error("sign() legacy options not supported");const{hash:i,randomBytes:c}=e;let{lowS:l,prehash:g,extraEntropy:y}=o;null==l&&(l=!0),t=Jt("msgHash",t),g&&(t=Jt("prehashed msgHash",i(t)));const w=m(t),E=a(r),_=[x(E),x(w)];if(null!=y){const t=!0===y?c(n.BYTES):y;_.push(Jt("extraEntropy",t))}const v=Xt(..._),B=w;return{seed:v,k2sig:function(t){const e=b(t);if(!h(e))return;const n=f(e),r=u.BASE.multiply(e).toAffine(),o=s(r.x);if(o===$e)return;const i=s(n*s(B+o*E));if(i===$e)return;let a=(r.x===o?0:2)|Number(r.y&ke),c=i;return l&&d(i)&&(c=function(t){return d(t)?s(-t):t}(i),a^=1),new p(o,c,a)}}}const A={lowS:e.lowS,prehash:!1},v={lowS:e.lowS,prehash:!1};return u.BASE._setWindowSize(8),{CURVE:e,getPublicKey:function(t,e=!0){return u.fromPrivateKey(t).toRawBytes(e)},getSharedSecret:function(t,e,n=!0){if(w(t))throw new Error("first arg must be private key");if(!w(e))throw new Error("second arg must be public key");return u.fromHex(e).multiply(a(t)).toRawBytes(n)},sign:function(t,n,r=A){const{seed:o,k2sig:i}=_(t,n,r),s=e;return ne(s.hash.outputLen,s.nByteLength,s.hmac)(o,i)},verify:function(t,n,r,o=v){const i=t;if(n=Jt("msgHash",n),r=Jt("publicKey",r),"strict"in o)throw new Error("options.strict was renamed to lowS");const{lowS:a,prehash:c}=o;let h,l;try{if("string"==typeof i||Ht(i))try{h=p.fromDER(i)}catch(t){if(!(t instanceof Ue.Err))throw t;h=p.fromCompact(i)}else{if("object"!=typeof i||"bigint"!=typeof i.r||"bigint"!=typeof i.s)throw new Error("PARSE");{const{r:t,s:e}=i;h=new p(t,e)}}l=u.fromHex(r)}catch(t){if("PARSE"===t.message)throw new Error("signature must be Signature instance, Uint8Array or hex string");return!1}if(a&&h.hasHighS())return!1;c&&(n=e.hash(n));const{r:d,s:g}=h,y=m(n),w=f(g),b=s(y*w),E=s(d*w),x=u.BASE.multiplyAndAddUnsafe(l,b,E)?.toAffine();return!!x&&s(x.x)===d},ProjectivePoint:u,Signature:p,utils:y}} /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */function Te(t){return{hash:t,hmac:(e,...n)=>Tt(t,e,function(...t){let e=0;for(let n=0;n<t.length;n++){const r=t[n];if(!f(r))throw new Error("Uint8Array expected");e+=r.length}const n=new Uint8Array(e);for(let e=0,r=0;e<t.length;e++){const o=t[e];n.set(o,r),r+=o.length}return n}(...n)),randomBytes:d}}BigInt(4); /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */ const Le=BigInt("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"),je=BigInt("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141"),qe=BigInt(1),ze=BigInt(2),He=(t,e)=>(t+e/ze)/e;const Ce=Ee(Le,void 0,void 0,{sqrt:function(t){const e=Le,n=BigInt(3),r=BigInt(6),o=BigInt(11),i=BigInt(22),s=BigInt(23),f=BigInt(44),u=BigInt(88),a=t*t*t%e,c=a*a*t%e,h=pe(c,n,e)*c%e,l=pe(h,n,e)*c%e,d=pe(l,ze,e)*a%e,g=pe(d,o,e)*d%e,p=pe(g,i,e)*g%e,y=pe(p,f,e)*p%e,w=pe(y,u,e)*y%e,b=pe(w,f,e)*p%e,m=pe(b,n,e)*c%e,E=pe(m,s,e)*g%e,x=pe(E,r,e)*a%e,_=pe(x,ze,e);if(!Ce.eql(Ce.sqr(_),t))throw new Error("Cannot find square root");return _}}),Pe=function(t,e){const n=e=>Ne({...t,...Te(e)});return Object.freeze({...n(e),create:n})}({a:BigInt(0),b:BigInt(7),Fp:Ce,n:je,Gx:BigInt("55066263022277343669578718895168534326250603453777594175500187360389116729240"),Gy:BigInt("32670510020758816978083085130507043184471273380659243275938904335757337482424"),h:BigInt(1),lowS:!0,endo:{beta:BigInt("0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee"),splitScalar:t=>{const e=je,n=BigInt("0x3086d221a7d46bcde86c90e49284eb15"),r=-qe*BigInt("0xe4437ed6010e88286f547fa90abfe4c3"),o=BigInt("0x114ca50f7a8e2f3f657c1108d9d44cfd8"),i=n,s=BigInt("0x100000000000000000000000000000000"),f=He(i*t,e),u=He(-r*t,e);let a=de(t-f*n-u*o,e),c=de(-f*r-u*i,e);const h=a>s,l=c>s;if(h&&(a=e-a),l&&(c=e-c),a>s||c>s)throw new Error("splitScalar: Endomorphism failed, k="+t);return{k1neg:h,k1:a,k2neg:l,k2:c}}}},E);BigInt(0);const Ve=Pe.CURVE,Fe=Ve.n,De=Ve.p,Ze={x:Ve.Gx,y:Ve.Gy},We=BigInt(0),Ge=BigInt(1),Ke=BigInt(2),Ye=BigInt(3),Me=BigInt(4);var Je=Object.freeze({__proto__:null,_0n:We,_1n:Ge,_2n:Ke,_3n:Ye,_4n:Me,_G:Ze,_N:Fe,_P:De});const Xe=Pe.ProjectivePoint;function Qe(t){return new Xe(t.x,t.y,Ge).hasEvenY()}function tn(t){if(!function(t){if(!function(t){const e=t;return"object"==typeof e&&null!==e&&"bigint"==typeof e.x&&"bigint"==typeof e.y}(t))return!1;const e=new Xe(t.x,t.y,Ge);try{return e.assertValidity(),!0}catch{return!1}}(t))throw new Error("ECC point is invalid: "+String(t))}function en(t,e){if(null===t)return e;if(null===e)return t;const n=new Xe(t.x,t.y,Ge),r=new Xe(e.x,e.y,Ge);try{const t=n.add(r);return t.assertValidity(),{x:t.x,y:t.y}}catch{return null}}function nn(t,e){if(null===t)return null;try{const n=yt.bytes(e),r=new Xe(t.x,t.y,Ge).multiply(n.big);return r.assertValidity(),{x:r.x,y:r.y}}catch{return null}}function rn(t,e=!1){let n=yt.bytes(t);32===n.length&&(n=n.prepend(2)),e&&2!==n[0]&&(n[0]=2);const r=Xe.fromHex(n.hex);return r.assertValidity(),{x:r.x,y:r.y}}function on(t){const e=yt.big(t.x,32),n=Qe(t)?2:3;return yt.join([n,e])}const sn=Ee(Fe,32,!0),fn=t=>de(t,Fe),un=(t,e)=>ge(t,e,Fe);we(Fe),we(De);const an=t=>"bigint"==typeof t&&We<t&&t<Fe;function cn(t,e=!1){if(!e)return!1;throw new Error(t)}const hn=Pe.ProjectivePoint;class ln extends Uint8Array{static{this.N=Fe}static add(t){return t.map((t=>ln.mod(t))).reduce(((t,e)=>t.add(e)))}static mod(t){return new ln(t)}static mul(t){return t.map((t=>ln.mod(t))).reduce(((t,e)=>t.mul(e)))}static is_valid(t,e){return function(t,e){return"bigint"==typeof t&&We<t&&t<Fe||cn("x value is not in the field!",e),!0}(yt.bytes(t,32).big,e)}static random(){return ln.mod(yt.random(32))}constructor(t){const e=fn(function(t){if(t instanceof ln)return t.big;if(t instanceof dn)return t.x.big;if(t instanceof Uint8Array)return yt.raw(t).big;if("string"==typeof t)return yt.hex(t).big;if("number"==typeof t)return yt.num(t).big;if("bigint"==typeof t)return BigInt(t);throw TypeError("Invalid input type:"+typeof t)}(t));ln.is_valid(e,!0),super(yt.big(e,32),32)}get buff(){return new yt(this)}get raw(){return this.buff.raw}get big(){return this.buff.big}get hex(){return this.buff.hex}get point(){return this.generate()}get hasOddY(){return this.point.hasOddY}get negated(){return this.hasOddY?this.negate():this}gt(t){return new ln(t).big>this.big}lt(t){return new ln(t).big<this.big}eq(t){return new ln(t).big===this.big}ne(t){return new ln(t).big!==this.big}add(t){const e=ln.mod(t),n=sn.add(this.big,e.big);return new ln(n)}sub(t){const e=ln.mod(t),n=sn.sub(this.big,e.big);return new ln(n)}mul(t){const e=ln.mod(t),n=sn.mul(this.big,e.big);return new ln(n)}pow(t){const e=ln.mod(t),n=sn.pow(this.big,e.big);return new ln(n)}div(t){const e=ln.mod(t),n=sn.div(this.big,e.big);return new ln(n)}negate(){return new ln(ln.N-this.big)}generate(){const t=Pe.ProjectivePoint.BASE.multiply(this.big);return dn.import(t)}}class dn{static{this.P=De}static{this.G=new dn(Ze.x,Ze.y)}static{this.curve=Pe.CURVE}static{this.base=Pe.ProjectivePoint.BASE}static from_x(t,e=!1){let n=function(t){if(t instanceof ln)return t.point.buff;if(t instanceof dn)return t.buff;if(t instanceof Uint8Array||"string"==typeof t)return yt.bytes(t);if("number"==typeof t||"bigint"==typeof t)return yt.bytes(t,32);throw new TypeError("Unknown type: "+typeof t)}(t);32===n.length?n=n.prepend(2):e&&(n[0]=2),function(t,e,n){const r=yt.bytes(t);r.length===e||cn(`Invalid byte size: ${r.hex} !== ${e}`,n)}(n,33);const r=hn.fromHex(n.hex);return r.assertValidity(),new dn(r.x,r.y)}static generate(t){const e=ln.mod(t),n=dn.base.multiply(e.big);return dn.import(n)}static{this.mul=dn.generate}static import(t){const e=t in