@vbyte/btc-dev
Version:
Batteries-included toolset for plebian bitcoin development
11 lines • 151 kB
JavaScript
var btc_dev=function(e){"use strict";var t,n;!function(e){function t(e){return null===e.match(/[^a-fA-F0-9]/)&&e.length%2==0}e.is_hex=t,e.is_bytes=function(e){return!("string"!=typeof e||!t(e))||(e instanceof Uint8Array||!(!Array.isArray(e)||!e.every(e=>"number"==typeof e)))}}(t||(t={})),function(e){e.within_size=function(e,t){if(e.length>t)throw new TypeError(`Data is larger than array size: ${e.length} > ${t}`)},e.is_hex=function(e){if(null!==e.match(/[^a-fA-f0-9]/))throw new TypeError("Invalid characters in hex string: "+e);if(e.length%2!=0)throw new Error(`Length of hex string is invalid: ${e.length}`)},e.is_bytes=function(e){if(!t.is_bytes(e))throw new Error("Bytes contains invalid elements: "+String(e))},e.is_json=function(e){try{JSON.parse(e)}catch{throw new TypeError("JSON string is invalid!")}},e.is_safe_int=function(e){if(e>Number.MAX_SAFE_INTEGER)throw new TypeError("Number exceeds safe bounds!")}}(n||(n={}));const r=BigInt(0),i=BigInt(255),o=BigInt(256);function s(e,t,n="be"){void 0===t&&(t=function(e){if(e<=0xffn)return 1;if(e<=0xffffn)return 2;if(e<=0xffffffffn)return 4;if(e<=0xffffffffffffffffn)return 8;if(e<=0xffffffffffffffffffffffffffffffffn)return 16;if(e<=0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffn)return 32;throw new TypeError("Must specify a fixed buffer size for bigints greater than 32 bytes.")}(e));const s="le"===n,u=new ArrayBuffer(t),c=new DataView(u);let a=s?0:t-1;for(;e>r;){const t=e&i,n=Number(t);s?c.setUint8(a++,n):c.setUint8(a--,n),e=(e-t)/o}return new Uint8Array(u)}function u(e,t){return"bigint"==typeof t?`${t}n`:t}function c(e,t){return"string"==typeof t&&/^[0-9]+n$/.test(t)?BigInt(t.slice(0,-1)):t}function a(e,t,n="be"){void 0===t&&(t=function(e){if(e<=255)return 1;if(e<=65535)return 2;if(e<=4294967295)return 4;throw new TypeError("Numbers larger than 4 bytes must specify a fixed size!")}(e));const r="le"===n,i=new ArrayBuffer(t),o=new DataView(i);let s=r?0:t-1;for(;e>0;){const t=255&e;r?o.setUint8(s++,e):o.setUint8(s--,e),e=(e-t)/256}return new Uint8Array(i)}function f(e,t,r="be"){t=function(e,t){n.is_hex(e);const r=e.length/2;void 0===t&&(t=r);if(r>t)throw new TypeError(`Hex string is larger than array size: ${r} > ${t}`);return t}(e,t);const i="be"===r,o=new ArrayBuffer(t),s=new DataView(o);let u=i?0:t-1;for(let t=0;t<e.length;t+=2){const n=e.substring(t,t+2),r=parseInt(n,16);i?s.setUint8(u++,r):s.setUint8(u--,r)}return new Uint8Array(o)}function d(e,t,r){if(e instanceof ArrayBuffer)return new Uint8Array(e);if(e instanceof Uint8Array)return function(e,t,r="le"){void 0===t&&(t=e.length);n.within_size(e,t);const i=new Uint8Array(t).fill(0),o="be"===r?0:t-e.length;return i.set(e,o),i}(e,t,r);if("string"==typeof e)return n.is_hex(e),f(e,t,r);if("bigint"==typeof e)return s(e,t,r);if("number"==typeof e)return a(e,t,r);throw new TypeError("Input type not supported:"+typeof e)}const l=new TextEncoder,h=new TextDecoder;function p(e){return l.encode(e)}function m(e){return h.decode(e)}class g extends Uint8Array{static{this.num=(e,t,n)=>new g(e,t,n)}static{this.big=(e,t,n)=>new g(e,t,n)}static{this.bin=(e,t,n)=>{const r=function(e){const t=e.split("").map(Number);if(t.length%8!=0)throw new Error(`Binary array is invalid length: ${e.length}`);const n=new Uint8Array(t.length/8);for(let e=0,r=0;e<t.length;e+=8,r++){let i=0;for(let n=0;n<8;n++)i|=t[e+n]<<7-n;n[r]=i}return n}(e);return new g(r,t,n)}}static{this.uint=(e,t,n)=>new g(e,t,n)}static{this.str=(e,t,n)=>{const r=p(e);return new g(r,t,n)}}static{this.hex=(e,t,r)=>(n.is_hex(e),new g(e,t,r))}static{this.bytes=(e,t,r)=>(n.is_bytes(e),new g(e,t,r))}static{this.json=(e,t)=>{t=t??u;const n=p(JSON.stringify(e,t));return new g(n)}}static{this.blob=(e,t,n)=>{const r=function(e,t,n){const r=e.length,i=n/t;if(n%t!==0)throw new TypeError(`Invalid parameters: ${n} % ${t} !== 0`);if(r!==n)throw new TypeError(`Invalid data stream: ${r} !== ${n}`);if(r%t!==0)throw new TypeError(`Invalid data stream: ${r} % ${t} !== 0`);const o=new Array(i);for(let n=0;n<i;n++){const r=n*t;o[n]=e.subarray(r,r+t)}return o}(d(e),t,n);return r.map(e=>new g(e))}}static{this.is_equal=(e,t)=>new g(e).hex===new g(t).hex}static{this.is_bytes=t.is_bytes}static{this.is_hex=t.is_hex}static random(e=32){const t=function(e=32){if(crypto&&"function"==typeof crypto.getRandomValues)return crypto.getRandomValues(new Uint8Array(e));const t=crypto;if(t&&void 0!==t.randomBytes&&"function"==typeof t.randomBytes)return t.randomBytes(e);throw new Error("getRandomValues from crypto library is undefined")}(e);return new g(t,e)}static now(){const e=Math.floor(Date.now()/1e3);return new g(e,4)}constructor(e,t,n){if(e instanceof g&&void 0===t)return e;super(d(e,t,n))}get arr(){return this.to_arr()}get big(){return this.to_big()}get bin(){return this.to_bin()}get hex(){return this.to_hex()}get num(){return this.to_num()}get str(){return this.to_str()}get uint(){return this.to_uint()}to_big(e="be"){return function(e){let t=BigInt(0);for(let n=e.length-1;n>=0;n--)t=t*o+BigInt(e[n]);return BigInt(t)}("be"===e?this.uint.reverse():this.uint)}to_hex(e="be"){return function(e){let t="";for(let n=0;n<e.length;n++)t+=e[n].toString(16).padStart(2,"0");return t}("be"===e?this.uint:this.uint.reverse())}to_json(e){void 0===e&&(e=c);const t=m(this);return JSON.parse(t,e)}to_num(e="be"){return function(e){let t=0;for(let r=e.length-1;r>=0;r--)t=256*t+e[r],n.is_safe_int(t);return t}("be"===e?this.uint.reverse():this.uint)}to_arr(){return[...this]}to_bin(){return function(e){const t=new Array(8*e.length);let n=0;for(const r of e){if(r>255)throw new Error(`Invalid byte value: ${r}. Byte values must be between 0 and 255.`);for(let e=7;e>=0;e--,n++)t[n]=r>>e&1}return t.join("")}(this)}to_str(){return m(this)}to_uint(){return new Uint8Array(this)}append(e){return g.join([this,new g(e)])}equals(e){return new g(e).hex===this.hex}prepend(e){return g.join([new g(e),this])}prefix_varint(e){if(0===this.length)throw new Error("buffer is empty");const t=g.varint(this.length,e);return this.prepend(t)}reverse(){return super.reverse(),this}set(e,t){this.set(e,t)}slice(e,t){const n=new Uint8Array(this).slice(e,t);return new g(n)}subarray(e,t){const n=new Uint8Array(this).subarray(e,t);return new g(n)}toJSON(){return this.hex}toString(){return this.hex}static from(e){return new g(Uint8Array.from(e))}static of(...e){return new g(Uint8Array.of(...e))}static join(e){const t=function(e){let t,n=0;const r=e.reduce((e,t)=>e+t.length,0),i=new Uint8Array(r);for(t=0;t<e.length;t++){const r=e[t];i.set(r,n),n+=r.length}return i}(e.map(e=>new g(e)));return new g(t)}static sort(e,t){const n=e.map(e=>new g(e,t).hex);return n.sort(),n.map(e=>g.hex(e,t))}static varint(e,t){if(e<253)return g.num(e,1);if(e<65536)return g.of(253,...g.num(e,2,t));if(e<4294967296)return g.of(254,...g.num(e,4,t));if(BigInt(e)<0x10000000000000000n)return g.of(255,...g.num(e,8,t));throw new Error(`Value is too large: ${e}`)}}class _{constructor(e){this.data=new g(e),this.size=this.data.length}peek(e){if(e>this.size)throw new Error(`Size greater than stream: ${e} > ${this.size}`);return new g(this.data.slice(0,e))}read(e){const t=this.peek(e);return this.data=this.data.slice(e),this.size=this.data.length,t}varint(e){const t=this.read(1).num;switch(!0){case t>=0&&t<253:return t;case 253===t:return this.read(2).to_num(e);case 254===t:return this.read(4).to_num(e);case 255===t:return this.read(8).to_num(e);default:throw new Error(`Varint is out of range: ${t}`)}}}function y(e){return e instanceof Map||Array.isArray(e)||"object"!=typeof e?e:Object.keys(e).sort().filter(([e,t])=>void 0!==t).reduce((t,n)=>(t[n]=e[n],t),{})}function w(e){return e instanceof Error?e.message:"string"==typeof e?e:String(e)}var v,b;!function(e){function t(e){return"object"==typeof e&&null!==e&&!Array.isArray(e)}function n(e){return"string"==typeof e}function r(e){return Number.isInteger(e)&&!Number.isNaN(e)}function i(e){return!!n(e)&&(null===e.match(/[^a-fA-F0-9]/)&&e.length%2==0)}e.exists=function(e){return null!=e},e.is_equal=function(e,t){return e===t},e.is_object=t,e.is_deep_equal=function(e,n){return t(e)&&(e=y(e)),t(n)&&(n=y(n)),String(e)===String(n)},e.has_items=function(e){return Array.isArray(e)&&e.length>0},e.is_string=n,e.is_number=r,e.is_bigint=function(e){return"bigint"==typeof e},e.is_uchar=function(e){return r(e)&&e>=0&&e<=255},e.is_ushort=function(e){return r(e)&&e>=0&&e<=65535},e.is_uint=function(e){return r(e)&&e>=0&&e<=4294967295},e.is_u8a=function(e){return e instanceof Uint8Array},e.is_bytes=function(e){return g.is_bytes(e)},e.is_base58=function(e){return"string"==typeof e&&/^[123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]+$/.test(e)},e.is_base64=function(e){return"string"==typeof e&&/^[a-zA-Z0-9+/]+={0,2}$/.test(e)},e.is_b64url=function(e){return"string"==typeof e&&/^[a-zA-Z0-9\-_]+={0,2}$/.test(e)},e.is_bech32=function(e){return"string"==typeof e&&/^[a-z]+1[023456789acdefghjklmnpqrstuvwxyz]+$/.test(e)},e.is_hex=i,e.is_hash=function(e){return n(e)&&i(e)&&64===e.length}}(v||(v={})),function(e){e.ok=function(e,t){if(!1===e)throw new Error(t??"Assertion failed!")},e.exists=function(e,t){if(!v.exists(e))throw new Error(t??"Value is null or undefined!")},e.is_empty=function(e,t){if(null!=e)throw new Error(t??"value is not null or undefined!")},e.is_instance=function(e,t,n){if(!(e instanceof t))throw new Error(n??`value is not an instance of ${t.name}`)},e.is_equal=function(e,t,n){if(!v.is_equal(e,t))throw new Error(n??`values are not equal: ${String(e)} !== ${String(t)}`)},e.is_object=function(e,t){if(!v.is_object(e))throw new Error(t??`value is not an object: ${String(e)}`)},e.is_deep_equal=function(e,t,n){if(!v.is_deep_equal(e,t))throw new Error(n??`values are not deep equal: ${String(e)} !== ${String(t)}`)},e.is_number=function(e){if(!v.is_number(e))throw new TypeError(`invalid number: ${String(e)}`)},e.is_bigint=function(e){if(!v.is_bigint(e))throw new TypeError(`invalid bigint: ${String(e)}`)},e.is_hex=function(e){if(!v.is_hex(e))throw new TypeError(`invalid hex: ${String(e)}`)},e.is_uchar=function(e){if(!v.is_uchar(e))throw new TypeError(`invalid unsignedchar: ${String(e)}`)},e.is_ushort=function(e){if(!v.is_ushort(e))throw new TypeError(`invalid unsigned short: ${String(e)}`)},e.is_uint=function(e){if(!v.is_uint(e))throw new TypeError(`invalid unsigned int: ${String(e)}`)},e.is_u8a=function(e){if(!v.is_u8a(e))throw new TypeError(`invalid Uint8Array: ${String(e)}`)},e.is_hash=function(e,t){if(!v.is_hash(e))throw new TypeError(t??`invalid hash: ${String(e)}`)},e.is_bytes=function(e,t){if(!v.is_bytes(e))throw new TypeError(t??`invalid bytes: ${String(e)}`)},e.size=function(e,t,n){const r=g.bytes(e);if(r.length!==t)throw new Error(n??`invalid input size: ${r.length} !== ${t}`)},e.has_items=function(e,t){if(!v.has_items(e))throw new Error(t??"array does not contain any items")},e.is_base58=function(e){if(!v.is_base58(e))throw new Error("invalid base58 string")},e.is_base64=function(e){if(!v.is_base64(e))throw new Error("invalid base64 string")},e.is_b64url=function(e){if(!v.is_b64url(e))throw new Error("invalid base64url string")},e.is_bech32=function(e){if(!v.is_bech32(e))throw new Error("invalid bech32 string")}}(b||(b={}));const E="object"==typeof globalThis&&"crypto"in globalThis?globalThis.crypto:void 0;
/*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */function x(e){return e instanceof Uint8Array||ArrayBuffer.isView(e)&&"Uint8Array"===e.constructor.name}function k(e){if(!Number.isSafeInteger(e)||e<0)throw new Error("positive integer expected, got "+e)}function z(e,...t){if(!x(e))throw new Error("Uint8Array expected");if(t.length>0&&!t.includes(e.length))throw new Error("Uint8Array expected of length "+t+", got length="+e.length)}function O(e){if("function"!=typeof e||"function"!=typeof e.create)throw new Error("Hash should be wrapped by utils.createHasher");k(e.outputLen),k(e.blockLen)}function P(e,t=!0){if(e.destroyed)throw new Error("Hash instance has been destroyed");if(t&&e.finished)throw new Error("Hash#digest() has already been called")}function S(...e){for(let t=0;t<e.length;t++)e[t].fill(0)}function A(e){return new DataView(e.buffer,e.byteOffset,e.byteLength)}function I(e,t){return e<<32-t|e>>>t}function $(e,t){return e<<t|e>>>32-t>>>0}const T=(()=>"function"==typeof Uint8Array.from([]).toHex&&"function"==typeof Uint8Array.fromHex)(),B=Array.from({length:256},(e,t)=>t.toString(16).padStart(2,"0"));function N(e){if(z(e),T)return e.toHex();let t="";for(let n=0;n<e.length;n++)t+=B[e[n]];return t}const U=48,Z=57,R=65,C=70,j=97,F=102;function D(e){return e>=U&&e<=Z?e-U:e>=R&&e<=C?e-(R-10):e>=j&&e<=F?e-(j-10):void 0}function H(e){if("string"!=typeof e)throw new Error("hex string expected, got "+typeof e);if(T)return Uint8Array.fromHex(e);const t=e.length,n=t/2;if(t%2)throw new Error("hex string expected, got unpadded hex of length "+t);const r=new Uint8Array(n);for(let t=0,i=0;t<n;t++,i+=2){const n=D(e.charCodeAt(i)),o=D(e.charCodeAt(i+1));if(void 0===n||void 0===o){const t=e[i]+e[i+1];throw new Error('hex string expected, got non-hex character "'+t+'" at index '+i)}r[t]=16*n+o}return r}function L(e){if("string"!=typeof e)throw new Error("string expected");return new Uint8Array((new TextEncoder).encode(e))}function V(e){return"string"==typeof e&&(e=L(e)),z(e),e}function q(...e){let t=0;for(let n=0;n<e.length;n++){const r=e[n];z(r),t+=r.length}const n=new Uint8Array(t);for(let t=0,r=0;t<e.length;t++){const i=e[t];n.set(i,r),r+=i.length}return n}class K{}function M(e){const t=t=>e().update(V(t)).digest(),n=e();return t.outputLen=n.outputLen,t.blockLen=n.blockLen,t.create=()=>e(),t}function W(e=32){if(E&&"function"==typeof E.getRandomValues)return E.getRandomValues(new Uint8Array(e));if(E&&"function"==typeof E.randomBytes)return Uint8Array.from(E.randomBytes(e));throw new Error("crypto.getRandomValues must be defined")}function G(e,t,n){return e&t^~e&n}function Y(e,t,n){return e&t^e&n^t&n}class X extends K{constructor(e,t,n,r){super(),this.finished=!1,this.length=0,this.pos=0,this.destroyed=!1,this.blockLen=e,this.outputLen=t,this.padOffset=n,this.isLE=r,this.buffer=new Uint8Array(e),this.view=A(this.buffer)}update(e){P(this),z(e=V(e));const{view:t,buffer:n,blockLen:r}=this,i=e.length;for(let o=0;o<i;){const s=Math.min(r-this.pos,i-o);if(s===r){const t=A(e);for(;r<=i-o;o+=r)this.process(t,o);continue}n.set(e.subarray(o,o+s),this.pos),this.pos+=s,o+=s,this.pos===r&&(this.process(t,0),this.pos=0)}return this.length+=e.length,this.roundClean(),this}digestInto(e){P(this),function(e,t){z(e);const n=t.outputLen;if(e.length<n)throw new Error("digestInto() expects output buffer of length at least "+n)}(e,this),this.finished=!0;const{buffer:t,view:n,blockLen:r,isLE:i}=this;let{pos:o}=this;t[o++]=128,S(this.buffer.subarray(o)),this.padOffset>r-o&&(this.process(n,0),o=0);for(let e=o;e<r;e++)t[e]=0;!function(e,t,n,r){if("function"==typeof e.setBigUint64)return e.setBigUint64(t,n,r);const i=BigInt(32),o=BigInt(4294967295),s=Number(n>>i&o),u=Number(n&o),c=r?4:0,a=r?0:4;e.setUint32(t+c,s,r),e.setUint32(t+a,u,r)}(n,r-8,BigInt(8*this.length),i),this.process(n,0);const s=A(e),u=this.outputLen;if(u%4)throw new Error("_sha2: outputLen should be aligned to 32bit");const c=u/4,a=this.get();if(c>a.length)throw new Error("_sha2: outputLen bigger than state");for(let e=0;e<c;e++)s.setUint32(4*e,a[e],i)}digest(){const{buffer:e,outputLen:t}=this;this.digestInto(e);const n=e.slice(0,t);return this.destroy(),n}_cloneInto(e){e||(e=new this.constructor),e.set(...this.get());const{blockLen:t,buffer:n,length:r,finished:i,destroyed:o,pos:s}=this;return e.destroyed=o,e.finished=i,e.length=r,e.pos=s,r%t&&e.buffer.set(n),e}clone(){return this._cloneInto()}}const J=Uint32Array.from([1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225]),Q=Uint32Array.from([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]),ee=new Uint32Array(64);class te extends X{constructor(e=32){super(64,e,8,!1),this.A=0|J[0],this.B=0|J[1],this.C=0|J[2],this.D=0|J[3],this.E=0|J[4],this.F=0|J[5],this.G=0|J[6],this.H=0|J[7]}get(){const{A:e,B:t,C:n,D:r,E:i,F:o,G:s,H:u}=this;return[e,t,n,r,i,o,s,u]}set(e,t,n,r,i,o,s,u){this.A=0|e,this.B=0|t,this.C=0|n,this.D=0|r,this.E=0|i,this.F=0|o,this.G=0|s,this.H=0|u}process(e,t){for(let n=0;n<16;n++,t+=4)ee[n]=e.getUint32(t,!1);for(let e=16;e<64;e++){const t=ee[e-15],n=ee[e-2],r=I(t,7)^I(t,18)^t>>>3,i=I(n,17)^I(n,19)^n>>>10;ee[e]=i+ee[e-7]+r+ee[e-16]|0}let{A:n,B:r,C:i,D:o,E:s,F:u,G:c,H:a}=this;for(let e=0;e<64;e++){const t=a+(I(s,6)^I(s,11)^I(s,25))+G(s,u,c)+Q[e]+ee[e]|0,f=(I(n,2)^I(n,13)^I(n,22))+Y(n,r,i)|0;a=c,c=u,u=s,s=o+t|0,o=i,i=r,r=n,n=t+f|0}n=n+this.A|0,r=r+this.B|0,i=i+this.C|0,o=o+this.D|0,s=s+this.E|0,u=u+this.F|0,c=c+this.G|0,a=a+this.H|0,this.set(n,r,i,o,s,u,c,a)}roundClean(){S(ee)}destroy(){this.set(0,0,0,0,0,0,0,0),S(this.buffer)}}const ne=M(()=>new te);class re extends K{constructor(e,t){super(),this.finished=!1,this.destroyed=!1,O(e);const n=V(t);if(this.iHash=e.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 r=this.blockLen,i=new Uint8Array(r);i.set(n.length>r?e.create().update(n).digest():n);for(let e=0;e<i.length;e++)i[e]^=54;this.iHash.update(i),this.oHash=e.create();for(let e=0;e<i.length;e++)i[e]^=106;this.oHash.update(i),S(i)}update(e){return P(this),this.iHash.update(e),this}digestInto(e){P(this),z(e,this.outputLen),this.finished=!0,this.iHash.digestInto(e),this.oHash.update(e),this.oHash.digestInto(e),this.destroy()}digest(){const e=new Uint8Array(this.oHash.outputLen);return this.digestInto(e),e}_cloneInto(e){e||(e=Object.create(Object.getPrototypeOf(this),{}));const{oHash:t,iHash:n,finished:r,destroyed:i,blockLen:o,outputLen:s}=this;return e.finished=r,e.destroyed=i,e.blockLen=o,e.outputLen=s,e.oHash=t._cloneInto(e.oHash),e.iHash=n._cloneInto(e.iHash),e}clone(){return this._cloneInto()}destroy(){this.destroyed=!0,this.oHash.destroy(),this.iHash.destroy()}}const ie=(e,t,n)=>new re(e,t).update(n).digest();ie.create=(e,t)=>new re(e,t);
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
const oe=BigInt(0),se=BigInt(1);function ue(e,t=""){if("boolean"!=typeof e){throw new Error((t&&`"${t}"`)+"expected boolean, got type="+typeof e)}return e}function ce(e,t,n=""){const r=x(e),i=e?.length,o=void 0!==t;if(!r||o&&i!==t){throw new Error((n&&`"${n}" `)+"expected Uint8Array"+(o?` of length ${t}`:"")+", got "+(r?`length=${i}`:"type="+typeof e))}return e}function ae(e){const t=e.toString(16);return 1&t.length?"0"+t:t}function fe(e){if("string"!=typeof e)throw new Error("hex string expected, got "+typeof e);return""===e?oe:BigInt("0x"+e)}function de(e){return fe(N(e))}function le(e){return z(e),fe(N(Uint8Array.from(e).reverse()))}function he(e,t){return H(e.toString(16).padStart(2*t,"0"))}function pe(e,t){return he(e,t).reverse()}function me(e,t,n){let r;if("string"==typeof t)try{r=H(t)}catch(t){throw new Error(e+" must be hex string or Uint8Array, cause: "+t)}else{if(!x(t))throw new Error(e+" must be hex string or Uint8Array");r=Uint8Array.from(t)}const i=r.length;if("number"==typeof n&&i!==n)throw new Error(e+" of length "+n+" expected, got "+i);return r}const ge=e=>"bigint"==typeof e&&oe<=e;function _e(e,t,n){return ge(e)&&ge(t)&&ge(n)&&t<=e&&e<n}function ye(e){let t;for(t=0;e>oe;e>>=se,t+=1);return t}const we=e=>(se<<BigInt(e))-se;function ve(e,t,n={}){if(!e||"object"!=typeof e)throw new Error("expected valid options object");function r(t,n,r){const i=e[t];if(r&&void 0===i)return;const o=typeof i;if(o!==n||null===i)throw new Error(`param "${t}" is invalid: expected ${n}, got ${o}`)}Object.entries(t).forEach(([e,t])=>r(e,t,!1)),Object.entries(n).forEach(([e,t])=>r(e,t,!0))}function be(e){const t=new WeakMap;return(n,...r)=>{const i=t.get(n);if(void 0!==i)return i;const o=e(n,...r);return t.set(n,o),o}}
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */const Ee=BigInt(0),xe=BigInt(1),ke=BigInt(2),ze=BigInt(3),Oe=BigInt(4),Pe=BigInt(5),Se=BigInt(7),Ae=BigInt(8),Ie=BigInt(9),$e=BigInt(16);function Te(e,t){const n=e%t;return n>=Ee?n:t+n}function Be(e,t,n){let r=e;for(;t-- >Ee;)r*=r,r%=n;return r}function Ne(e,t){if(e===Ee)throw new Error("invert: expected non-zero number");if(t<=Ee)throw new Error("invert: expected positive modulus, got "+t);let n=Te(e,t),r=t,i=Ee,o=xe;for(;n!==Ee;){const e=r%n,t=i-o*(r/n);r=n,n=e,i=o,o=t}if(r!==xe)throw new Error("invert: does not exist");return Te(i,t)}function Ue(e,t,n){if(!e.eql(e.sqr(t),n))throw new Error("Cannot find square root")}function Ze(e,t){const n=(e.ORDER+xe)/Oe,r=e.pow(t,n);return Ue(e,r,t),r}function Re(e,t){const n=(e.ORDER-Pe)/Ae,r=e.mul(t,ke),i=e.pow(r,n),o=e.mul(t,i),s=e.mul(e.mul(o,ke),i),u=e.mul(o,e.sub(s,e.ONE));return Ue(e,u,t),u}function Ce(e){if(e<ze)throw new Error("sqrt is not defined for small field");let t=e-xe,n=0;for(;t%ke===Ee;)t/=ke,n++;let r=ke;const i=Ve(e);for(;1===He(i,r);)if(r++>1e3)throw new Error("Cannot find square root: probably non-prime P");if(1===n)return Ze;let o=i.pow(r,t);const s=(t+xe)/ke;return function(e,r){if(e.is0(r))return r;if(1!==He(e,r))throw new Error("Cannot find square root");let i=n,u=e.mul(e.ONE,o),c=e.pow(r,t),a=e.pow(r,s);for(;!e.eql(c,e.ONE);){if(e.is0(c))return e.ZERO;let t=1,n=e.sqr(c);for(;!e.eql(n,e.ONE);)if(t++,n=e.sqr(n),t===i)throw new Error("Cannot find square root");const r=xe<<BigInt(i-t-1),o=e.pow(u,r);i=t,u=e.sqr(o),c=e.mul(c,u),a=e.mul(a,o)}return a}}function je(e){return e%Oe===ze?Ze:e%Ae===Pe?Re:e%$e===Ie?function(e){const t=Ve(e),n=Ce(e),r=n(t,t.neg(t.ONE)),i=n(t,r),o=n(t,t.neg(r)),s=(e+Se)/$e;return(e,t)=>{let n=e.pow(t,s),u=e.mul(n,r);const c=e.mul(n,i),a=e.mul(n,o),f=e.eql(e.sqr(u),t),d=e.eql(e.sqr(c),t);n=e.cmov(n,u,f),u=e.cmov(a,c,d);const l=e.eql(e.sqr(u),t),h=e.cmov(n,u,l);return Ue(e,h,t),h}}(e):Ce(e)}const Fe=["create","isValid","is0","neg","inv","sqrt","sqr","eql","add","sub","mul","pow","div","addN","subN","mulN","sqrN"];function De(e,t,n=!1){const r=new Array(t.length).fill(n?e.ZERO:void 0),i=t.reduce((t,n,i)=>e.is0(n)?t:(r[i]=t,e.mul(t,n)),e.ONE),o=e.inv(i);return t.reduceRight((t,n,i)=>e.is0(n)?t:(r[i]=e.mul(t,r[i]),e.mul(t,n)),o),r}function He(e,t){const n=(e.ORDER-xe)/ke,r=e.pow(t,n),i=e.eql(r,e.ONE),o=e.eql(r,e.ZERO),s=e.eql(r,e.neg(e.ONE));if(!i&&!o&&!s)throw new Error("invalid Legendre symbol result");return i?1:o?0:-1}function Le(e,t){void 0!==t&&k(t);const n=void 0!==t?t:e.toString(2).length;return{nBitLength:n,nByteLength:Math.ceil(n/8)}}function Ve(e,t,n=!1,r={}){if(e<=Ee)throw new Error("invalid field: expected ORDER > 0, got "+e);let i,o,s,u=!1;if("object"==typeof t&&null!=t){if(r.sqrt||n)throw new Error("cannot specify opts in two arguments");const e=t;e.BITS&&(i=e.BITS),e.sqrt&&(o=e.sqrt),"boolean"==typeof e.isLE&&(n=e.isLE),"boolean"==typeof e.modFromBytes&&(u=e.modFromBytes),s=e.allowedLengths}else"number"==typeof t&&(i=t),r.sqrt&&(o=r.sqrt);const{nBitLength:c,nByteLength:a}=Le(e,i);if(a>2048)throw new Error("invalid field: expected ORDER of <= 2048 bytes");let f;const d=Object.freeze({ORDER:e,isLE:n,BITS:c,BYTES:a,MASK:we(c),ZERO:Ee,ONE:xe,allowedLengths:s,create:t=>Te(t,e),isValid:t=>{if("bigint"!=typeof t)throw new Error("invalid field element: expected bigint, got "+typeof t);return Ee<=t&&t<e},is0:e=>e===Ee,isValidNot0:e=>!d.is0(e)&&d.isValid(e),isOdd:e=>(e&xe)===xe,neg:t=>Te(-t,e),eql:(e,t)=>e===t,sqr:t=>Te(t*t,e),add:(t,n)=>Te(t+n,e),sub:(t,n)=>Te(t-n,e),mul:(t,n)=>Te(t*n,e),pow:(e,t)=>function(e,t,n){if(n<Ee)throw new Error("invalid exponent, negatives unsupported");if(n===Ee)return e.ONE;if(n===xe)return t;let r=e.ONE,i=t;for(;n>Ee;)n&xe&&(r=e.mul(r,i)),i=e.sqr(i),n>>=xe;return r}(d,e,t),div:(t,n)=>Te(t*Ne(n,e),e),sqrN:e=>e*e,addN:(e,t)=>e+t,subN:(e,t)=>e-t,mulN:(e,t)=>e*t,inv:t=>Ne(t,e),sqrt:o||(t=>(f||(f=je(e)),f(d,t))),toBytes:e=>n?pe(e,a):he(e,a),fromBytes:(t,r=!0)=>{if(s){if(!s.includes(t.length)||t.length>a)throw new Error("Field.fromBytes: expected "+s+" bytes, got "+t.length);const e=new Uint8Array(a);e.set(t,n?0:e.length-t.length),t=e}if(t.length!==a)throw new Error("Field.fromBytes: expected "+a+" bytes, got "+t.length);let i=n?le(t):de(t);if(u&&(i=Te(i,e)),!r&&!d.isValid(i))throw new Error("invalid field element: outside of range 0..ORDER");return i},invertBatch:e=>De(d,e),cmov:(e,t,n)=>n?t:e});return Object.freeze(d)}function qe(e){if("bigint"!=typeof e)throw new Error("field order must be bigint");const t=e.toString(2).length;return Math.ceil(t/8)}function Ke(e){const t=qe(e);return t+Math.ceil(t/2)}function Me(e,t,n=!1){const r=e.length,i=qe(t),o=Ke(t);if(r<16||r<o||r>1024)throw new Error("expected "+o+"-1024 bytes of input, got "+r);const s=Te(n?le(e):de(e),t-xe)+xe;return n?pe(s,i):he(s,i)}
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */const We=BigInt(0),Ge=BigInt(1);function Ye(e,t){const n=t.negate();return e?n:t}function Xe(e,t){const n=De(e.Fp,t.map(e=>e.Z));return t.map((t,r)=>e.fromAffine(t.toAffine(n[r])))}function Je(e,t){if(!Number.isSafeInteger(e)||e<=0||e>t)throw new Error("invalid window size, expected [1.."+t+"], got W="+e)}function Qe(e,t){Je(e,t);const n=2**e;return{windows:Math.ceil(t/e)+1,windowSize:2**(e-1),mask:we(e),maxNumber:n,shiftBy:BigInt(e)}}function et(e,t,n){const{windowSize:r,mask:i,maxNumber:o,shiftBy:s}=n;let u=Number(e&i),c=e>>s;u>r&&(u-=o,c+=Ge);const a=t*r;return{nextN:c,offset:a+Math.abs(u)-1,isZero:0===u,isNeg:u<0,isNegF:t%2!=0,offsetF:a}}const tt=new WeakMap,nt=new WeakMap;function rt(e){return nt.get(e)||1}function it(e){if(e!==We)throw new Error("invalid wNAF")}class ot{constructor(e,t){this.BASE=e.BASE,this.ZERO=e.ZERO,this.Fn=e.Fn,this.bits=t}_unsafeLadder(e,t,n=this.ZERO){let r=e;for(;t>We;)t&Ge&&(n=n.add(r)),r=r.double(),t>>=Ge;return n}precomputeWindow(e,t){const{windows:n,windowSize:r}=Qe(t,this.bits),i=[];let o=e,s=o;for(let e=0;e<n;e++){s=o,i.push(s);for(let e=1;e<r;e++)s=s.add(o),i.push(s);o=s.double()}return i}wNAF(e,t,n){if(!this.Fn.isValid(n))throw new Error("invalid scalar");let r=this.ZERO,i=this.BASE;const o=Qe(e,this.bits);for(let e=0;e<o.windows;e++){const{nextN:s,offset:u,isZero:c,isNeg:a,isNegF:f,offsetF:d}=et(n,e,o);n=s,c?i=i.add(Ye(f,t[d])):r=r.add(Ye(a,t[u]))}return it(n),{p:r,f:i}}wNAFUnsafe(e,t,n,r=this.ZERO){const i=Qe(e,this.bits);for(let e=0;e<i.windows&&n!==We;e++){const{nextN:o,offset:s,isZero:u,isNeg:c}=et(n,e,i);if(n=o,!u){const e=t[s];r=r.add(c?e.negate():e)}}return it(n),r}getPrecomputes(e,t,n){let r=tt.get(t);return r||(r=this.precomputeWindow(t,e),1!==e&&("function"==typeof n&&(r=n(r)),tt.set(t,r))),r}cached(e,t,n){const r=rt(e);return this.wNAF(r,this.getPrecomputes(r,e,n),t)}unsafe(e,t,n,r){const i=rt(e);return 1===i?this._unsafeLadder(e,t,r):this.wNAFUnsafe(i,this.getPrecomputes(i,e,n),t,r)}createCache(e,t){Je(t,this.bits),nt.set(e,t),tt.delete(e)}hasCache(e){return 1!==rt(e)}}function st(e,t,n,r){!function(e,t){if(!Array.isArray(e))throw new Error("array expected");e.forEach((e,n)=>{if(!(e instanceof t))throw new Error("invalid point at index "+n)})}(n,e),function(e,t){if(!Array.isArray(e))throw new Error("array of scalars expected");e.forEach((e,n)=>{if(!t.isValid(e))throw new Error("invalid scalar at index "+n)})}(r,t);const i=n.length,o=r.length;if(i!==o)throw new Error("arrays of points and scalars must have equal length");const s=e.ZERO,u=ye(BigInt(i));let c=1;u>12?c=u-3:u>4?c=u-2:u>0&&(c=2);const a=we(c),f=new Array(Number(a)+1).fill(s);let d=s;for(let e=Math.floor((t.BITS-1)/c)*c;e>=0;e-=c){f.fill(s);for(let t=0;t<o;t++){const i=r[t],o=Number(i>>BigInt(e)&a);f[o]=f[o].add(n[t])}let t=s;for(let e=f.length-1,n=s;e>0;e--)n=n.add(f[e]),t=t.add(n);if(d=d.add(t),0!==e)for(let e=0;e<c;e++)d=d.double()}return d}function ut(e,t,n){if(t){if(t.ORDER!==e)throw new Error("Field.ORDER must match order: Fp == p, Fn == n");return function(e){ve(e,Fe.reduce((e,t)=>(e[t]="function",e),{ORDER:"bigint",MASK:"bigint",BYTES:"number",BITS:"number"}))}(t),t}return Ve(e,{isLE:n})}
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
const ct=(e,t)=>(e+(e>=0?t:-t)/mt)/t;function at(e){if(!["compact","recovered","der"].includes(e))throw new Error('Signature format must be "compact", "recovered", or "der"');return e}function ft(e,t){const n={};for(let r of Object.keys(t))n[r]=void 0===e[r]?t[r]:e[r];return ue(n.lowS,"lowS"),ue(n.prehash,"prehash"),void 0!==n.format&&at(n.format),n}class dt extends Error{constructor(e=""){super(e)}}const lt={Err:dt,_tlv:{encode:(e,t)=>{const{Err:n}=lt;if(e<0||e>256)throw new n("tlv.encode: wrong tag");if(1&t.length)throw new n("tlv.encode: unpadded data");const r=t.length/2,i=ae(r);if(i.length/2&128)throw new n("tlv.encode: long form length too big");const o=r>127?ae(i.length/2|128):"";return ae(e)+o+i+t},decode(e,t){const{Err:n}=lt;let r=0;if(e<0||e>256)throw new n("tlv.encode: wrong tag");if(t.length<2||t[r++]!==e)throw new n("tlv.decode: wrong tlv");const i=t[r++];let o=0;if(!!(128&i)){const e=127&i;if(!e)throw new n("tlv.decode(long): indefinite length not supported");if(e>4)throw new n("tlv.decode(long): byte length is too big");const s=t.subarray(r,r+e);if(s.length!==e)throw new n("tlv.decode: length bytes not complete");if(0===s[0])throw new n("tlv.decode(long): zero leftmost byte");for(const e of s)o=o<<8|e;if(r+=e,o<128)throw new n("tlv.decode(long): not minimal encoding")}else o=i;const s=t.subarray(r,r+o);if(s.length!==o)throw new n("tlv.decode: wrong value length");return{v:s,l:t.subarray(r+o)}}},_int:{encode(e){const{Err:t}=lt;if(e<ht)throw new t("integer: negative integers are not allowed");let n=ae(e);if(8&Number.parseInt(n[0],16)&&(n="00"+n),1&n.length)throw new t("unexpected DER parsing assertion: unpadded hex");return n},decode(e){const{Err:t}=lt;if(128&e[0])throw new t("invalid signature integer: negative");if(0===e[0]&&!(128&e[1]))throw new t("invalid signature integer: unnecessary leading zero");return de(e)}},toSig(e){const{Err:t,_int:n,_tlv:r}=lt,i=me("signature",e),{v:o,l:s}=r.decode(48,i);if(s.length)throw new t("invalid signature: left bytes after parsing");const{v:u,l:c}=r.decode(2,o),{v:a,l:f}=r.decode(2,c);if(f.length)throw new t("invalid signature: left bytes after parsing");return{r:n.decode(u),s:n.decode(a)}},hexFromSig(e){const{_tlv:t,_int:n}=lt,r=t.encode(2,n.encode(e.r))+t.encode(2,n.encode(e.s));return t.encode(48,r)}},ht=BigInt(0),pt=BigInt(1),mt=BigInt(2),gt=BigInt(3),_t=BigInt(4);function yt(e,t){const{BYTES:n}=e;let r;if("bigint"==typeof t)r=t;else{let i=me("private key",t);try{r=e.fromBytes(i)}catch(e){throw new Error(`invalid private key: expected ui8a of size ${n}, got ${typeof t}`)}}if(!e.isValidNot0(r))throw new Error("invalid private key: out of range [1..N-1]");return r}function wt(e,t={}){const n=function(e,t,n={},r){if(void 0===r&&(r="edwards"===e),!t||"object"!=typeof t)throw new Error(`expected valid ${e} CURVE object`);for(const e of["p","n","h"]){const n=t[e];if(!("bigint"==typeof n&&n>We))throw new Error(`CURVE.${e} must be positive bigint`)}const i=ut(t.p,n.Fp,r),o=ut(t.n,n.Fn,r),s=["Gx","Gy","a","b"];for(const e of s)if(!i.isValid(t[e]))throw new Error(`CURVE.${e} must be valid field element of CURVE.Fp`);return{CURVE:t=Object.freeze(Object.assign({},t)),Fp:i,Fn:o}}("weierstrass",e,t),{Fp:r,Fn:i}=n;let o=n.CURVE;const{h:s,n:u}=o;ve(t,{},{allowInfinityPoint:"boolean",clearCofactor:"function",isTorsionFree:"function",fromBytes:"function",toBytes:"function",endo:"object",wrapPrivateKey:"boolean"});const{endo:c}=t;if(c&&(!r.is0(o.a)||"bigint"!=typeof c.beta||!Array.isArray(c.basises)))throw new Error('invalid endo: expected "beta": bigint and "basises": array');const a=bt(r,i);function f(){if(!r.isOdd)throw new Error("compression is not supported: Field does not have .isOdd()")}const d=t.toBytes||function(e,t,n){const{x:i,y:o}=t.toAffine(),s=r.toBytes(i);if(ue(n,"isCompressed"),n){f();return q(vt(!r.isOdd(o)),s)}return q(Uint8Array.of(4),s,r.toBytes(o))},l=t.fromBytes||function(e){ce(e,void 0,"Point");const{publicKey:t,publicKeyUncompressed:n}=a,i=e.length,o=e[0],s=e.subarray(1);if(i!==t||2!==o&&3!==o){if(i===n&&4===o){const e=r.BYTES,t=r.fromBytes(s.subarray(0,e)),n=r.fromBytes(s.subarray(e,2*e));if(!p(t,n))throw new Error("bad point: is not on curve");return{x:t,y:n}}throw new Error(`bad point: got length ${i}, expected compressed=${t} or uncompressed=${n}`)}{const e=r.fromBytes(s);if(!r.isValid(e))throw new Error("bad point: is not on curve, wrong x");const t=h(e);let n;try{n=r.sqrt(t)}catch(e){const t=e instanceof Error?": "+e.message:"";throw new Error("bad point: is not on curve, sqrt error"+t)}f();return!(1&~o)!==r.isOdd(n)&&(n=r.neg(n)),{x:e,y:n}}};function h(e){const t=r.sqr(e),n=r.mul(t,e);return r.add(r.add(n,r.mul(e,o.a)),o.b)}function p(e,t){const n=r.sqr(t),i=h(e);return r.eql(n,i)}if(!p(o.Gx,o.Gy))throw new Error("bad curve params: generator point");const m=r.mul(r.pow(o.a,gt),_t),g=r.mul(r.sqr(o.b),BigInt(27));if(r.is0(r.add(m,g)))throw new Error("bad curve params: a or b");function _(e,t,n=!1){if(!r.isValid(t)||n&&r.is0(t))throw new Error(`bad point coordinate ${e}`);return t}function y(e){if(!(e instanceof x))throw new Error("ProjectivePoint expected")}function w(e){if(!c||!c.basises)throw new Error("no endo");return function(e,t,n){const[[r,i],[o,s]]=t,u=ct(s*e,n),c=ct(-i*e,n);let a=e-u*r-c*o,f=-u*i-c*s;const d=a<ht,l=f<ht;d&&(a=-a),l&&(f=-f);const h=we(Math.ceil(ye(n)/2))+pt;if(a<ht||a>=h||f<ht||f>=h)throw new Error("splitScalar (endomorphism): failed, k="+e);return{k1neg:d,k1:a,k2neg:l,k2:f}}(e,c.basises,i.ORDER)}const v=be((e,t)=>{const{X:n,Y:i,Z:o}=e;if(r.eql(o,r.ONE))return{x:n,y:i};const s=e.is0();null==t&&(t=s?r.ONE:r.inv(o));const u=r.mul(n,t),c=r.mul(i,t),a=r.mul(o,t);if(s)return{x:r.ZERO,y:r.ZERO};if(!r.eql(a,r.ONE))throw new Error("invZ was invalid");return{x:u,y:c}}),b=be(e=>{if(e.is0()){if(t.allowInfinityPoint&&!r.is0(e.Y))return;throw new Error("bad point: ZERO")}const{x:n,y:i}=e.toAffine();if(!r.isValid(n)||!r.isValid(i))throw new Error("bad point: x or y not field elements");if(!p(n,i))throw new Error("bad point: equation left != right");if(!e.isTorsionFree())throw new Error("bad point: not in prime-order subgroup");return!0});function E(e,t,n,i,o){return n=new x(r.mul(n.X,e),n.Y,n.Z),t=Ye(i,t),n=Ye(o,n),t.add(n)}class x{constructor(e,t,n){this.X=_("x",e),this.Y=_("y",t,!0),this.Z=_("z",n),Object.freeze(this)}static CURVE(){return o}static fromAffine(e){const{x:t,y:n}=e||{};if(!e||!r.isValid(t)||!r.isValid(n))throw new Error("invalid affine point");if(e instanceof x)throw new Error("projective point not allowed");return r.is0(t)&&r.is0(n)?x.ZERO:new x(t,n,r.ONE)}static fromBytes(e){const t=x.fromAffine(l(ce(e,void 0,"point")));return t.assertValidity(),t}static fromHex(e){return x.fromBytes(me("pointHex",e))}get x(){return this.toAffine().x}get y(){return this.toAffine().y}precompute(e=8,t=!0){return z.createCache(this,e),t||this.multiply(gt),this}assertValidity(){b(this)}hasEvenY(){const{y:e}=this.toAffine();if(!r.isOdd)throw new Error("Field doesn't support isOdd");return!r.isOdd(e)}equals(e){y(e);const{X:t,Y:n,Z:i}=this,{X:o,Y:s,Z:u}=e,c=r.eql(r.mul(t,u),r.mul(o,i)),a=r.eql(r.mul(n,u),r.mul(s,i));return c&&a}negate(){return new x(this.X,r.neg(this.Y),this.Z)}double(){const{a:e,b:t}=o,n=r.mul(t,gt),{X:i,Y:s,Z:u}=this;let c=r.ZERO,a=r.ZERO,f=r.ZERO,d=r.mul(i,i),l=r.mul(s,s),h=r.mul(u,u),p=r.mul(i,s);return p=r.add(p,p),f=r.mul(i,u),f=r.add(f,f),c=r.mul(e,f),a=r.mul(n,h),a=r.add(c,a),c=r.sub(l,a),a=r.add(l,a),a=r.mul(c,a),c=r.mul(p,c),f=r.mul(n,f),h=r.mul(e,h),p=r.sub(d,h),p=r.mul(e,p),p=r.add(p,f),f=r.add(d,d),d=r.add(f,d),d=r.add(d,h),d=r.mul(d,p),a=r.add(a,d),h=r.mul(s,u),h=r.add(h,h),d=r.mul(h,p),c=r.sub(c,d),f=r.mul(h,l),f=r.add(f,f),f=r.add(f,f),new x(c,a,f)}add(e){y(e);const{X:t,Y:n,Z:i}=this,{X:s,Y:u,Z:c}=e;let a=r.ZERO,f=r.ZERO,d=r.ZERO;const l=o.a,h=r.mul(o.b,gt);let p=r.mul(t,s),m=r.mul(n,u),g=r.mul(i,c),_=r.add(t,n),w=r.add(s,u);_=r.mul(_,w),w=r.add(p,m),_=r.sub(_,w),w=r.add(t,i);let v=r.add(s,c);return w=r.mul(w,v),v=r.add(p,g),w=r.sub(w,v),v=r.add(n,i),a=r.add(u,c),v=r.mul(v,a),a=r.add(m,g),v=r.sub(v,a),d=r.mul(l,w),a=r.mul(h,g),d=r.add(a,d),a=r.sub(m,d),d=r.add(m,d),f=r.mul(a,d),m=r.add(p,p),m=r.add(m,p),g=r.mul(l,g),w=r.mul(h,w),m=r.add(m,g),g=r.sub(p,g),g=r.mul(l,g),w=r.add(w,g),p=r.mul(m,w),f=r.add(f,p),p=r.mul(v,w),a=r.mul(_,a),a=r.sub(a,p),p=r.mul(_,m),d=r.mul(v,d),d=r.add(d,p),new x(a,f,d)}subtract(e){return this.add(e.negate())}is0(){return this.equals(x.ZERO)}multiply(e){const{endo:n}=t;if(!i.isValidNot0(e))throw new Error("invalid scalar: out of range");let r,o;const s=e=>z.cached(this,e,e=>Xe(x,e));if(n){const{k1neg:t,k1:i,k2neg:u,k2:c}=w(e),{p:a,f:f}=s(i),{p:d,f:l}=s(c);o=f.add(l),r=E(n.beta,a,d,t,u)}else{const{p:t,f:n}=s(e);r=t,o=n}return Xe(x,[r,o])[0]}multiplyUnsafe(e){const{endo:n}=t,r=this;if(!i.isValid(e))throw new Error("invalid scalar: out of range");if(e===ht||r.is0())return x.ZERO;if(e===pt)return r;if(z.hasCache(this))return this.multiply(e);if(n){const{k1neg:t,k1:i,k2neg:o,k2:s}=w(e),{p1:u,p2:c}=function(e,t,n,r){let i=t,o=e.ZERO,s=e.ZERO;for(;n>We||r>We;)n&Ge&&(o=o.add(i)),r&Ge&&(s=s.add(i)),i=i.double(),n>>=Ge,r>>=Ge;return{p1:o,p2:s}}(x,r,i,s);return E(n.beta,u,c,t,o)}return z.unsafe(r,e)}multiplyAndAddUnsafe(e,t,n){const r=this.multiplyUnsafe(t).add(e.multiplyUnsafe(n));return r.is0()?void 0:r}toAffine(e){return v(this,e)}isTorsionFree(){const{isTorsionFree:e}=t;return s===pt||(e?e(x,this):z.unsafe(this,u).is0())}clearCofactor(){const{clearCofactor:e}=t;return s===pt?this:e?e(x,this):this.multiplyUnsafe(s)}isSmallOrder(){return this.multiplyUnsafe(s).is0()}toBytes(e=!0){return ue(e,"isCompressed"),this.assertValidity(),d(x,this,e)}toHex(e=!0){return N(this.toBytes(e))}toString(){return`<Point ${this.is0()?"ZERO":this.toHex()}>`}get px(){return this.X}get py(){return this.X}get pz(){return this.Z}toRawBytes(e=!0){return this.toBytes(e)}_setWindowSize(e){this.precompute(e)}static normalizeZ(e){return Xe(x,e)}static msm(e,t){return st(x,i,e,t)}static fromPrivateKey(e){return x.BASE.multiply(yt(i,e))}}x.BASE=new x(o.Gx,o.Gy,r.ONE),x.ZERO=new x(r.ZERO,r.ONE,r.ZERO),x.Fp=r,x.Fn=i;const k=i.BITS,z=new ot(x,t.endo?Math.ceil(k/2):k);return x.BASE.precompute(8),x}function vt(e){return Uint8Array.of(e?2:3)}function bt(e,t){return{secretKey:t.BYTES,publicKey:1+e.BYTES,publicKeyUncompressed:1+2*e.BYTES,publicKeyHasPrefix:!0,signature:2*t.BYTES}}function Et(e,t,n={}){O(t),ve(n,{},{hmac:"function",lowS:"boolean",randomBytes:"function",bits2int:"function",bits2int_modN:"function"});const r=n.randomBytes||W,i=n.hmac||((e,...n)=>ie(t,e,q(...n))),{Fp:o,Fn:s}=e,{ORDER:u,BITS:c}=s,{keygen:a,getPublicKey:f,getSharedSecret:d,utils:l,lengths:h}=function(e,t={}){const{Fn:n}=e,r=t.randomBytes||W,i=Object.assign(bt(e.Fp,n),{seed:Ke(n.ORDER)});function o(e){try{return!!yt(n,e)}catch(e){return!1}}function s(e=r(i.seed)){return Me(ce(e,i.seed,"seed"),n.ORDER)}function u(t,r=!0){return e.BASE.multiply(yt(n,t)).toBytes(r)}function c(t){if("bigint"==typeof t)return!1;if(t instanceof e)return!0;const{secretKey:r,publicKey:o,publicKeyUncompressed:s}=i;if(n.allowedLengths||r===o)return;const u=me("key",t).length;return u===o||u===s}const a={isValidSecretKey:o,isValidPublicKey:function(t,n){const{publicKey:r,publicKeyUncompressed:o}=i;try{const i=t.length;return!(!0===n&&i!==r||!1===n&&i!==o||!e.fromBytes(t))}catch(e){return!1}},randomSecretKey:s,isValidPrivateKey:o,randomPrivateKey:s,normPrivateKeyToScalar:e=>yt(n,e),precompute:(t=8,n=e.BASE)=>n.precompute(t,!1)};return Object.freeze({getPublicKey:u,getSharedSecret:function(t,r,i=!0){if(!0===c(t))throw new Error("first arg must be private key");if(!1===c(r))throw new Error("second arg must be public key");const o=yt(n,t);return e.fromHex(r).multiply(o).toBytes(i)},keygen:function(e){const t=s(e);return{secretKey:t,publicKey:u(t)}},Point:e,utils:a,lengths:i})}(e,n),p={prehash:!1,lowS:"boolean"==typeof n.lowS&&n.lowS,format:void 0,extraEntropy:!1},m="compact";function g(e){return e>u>>pt}function _(e,t){if(!s.isValidNot0(t))throw new Error(`invalid signature ${e}: out of range 1..Point.Fn.ORDER`);return t}class y{constructor(e,t,n){this.r=_("r",e),this.s=_("s",t),null!=n&&(this.recovery=n),Object.freeze(this)}static fromBytes(e,t=m){let n;if(function(e,t){at(t);const n=h.signature;ce(e,"compact"===t?n:"recovered"===t?n+1:void 0,`${t} signature`)}(e,t),"der"===t){const{r:t,s:n}=lt.toSig(ce(e));return new y(t,n)}"recovered"===t&&(n=e[0],t="compact",e=e.subarray(1));const r=s.BYTES,i=e.subarray(0,r),o=e.subarray(r,2*r);return new y(s.fromBytes(i),s.fromBytes(o),n)}static fromHex(e,t){return this.fromBytes(H(e),t)}addRecoveryBit(e){return new y(this.r,this.s,e)}recoverPublicKey(t){const n=o.ORDER,{r:r,s:i,recovery:c}=this;if(null==c||![0,1,2,3].includes(c))throw new Error("recovery id invalid");if(u*mt<n&&c>1)throw new Error("recovery id is ambiguous for h>1 curve");const a=2===c||3===c?r+u:r;if(!o.isValid(a))throw new Error("recovery id 2 or 3 invalid");const f=o.toBytes(a),d=e.fromBytes(q(vt(!(1&c)),f)),l=s.inv(a),h=v(me("msgHash",t)),p=s.create(-h*l),m=s.create(i*l),g=e.BASE.multiplyUnsafe(p).add(d.multiplyUnsafe(m));if(g.is0())throw new Error("point at infinify");return g.assertValidity(),g}hasHighS(){return g(this.s)}toBytes(e=m){if(at(e),"der"===e)return H(lt.hexFromSig(this));const t=s.toBytes(this.r),n=s.toBytes(this.s);if("recovered"===e){if(null==this.recovery)throw new Error("recovery bit must be present");return q(Uint8Array.of(this.recovery),t,n)}return q(t,n)}toHex(e){return N(this.toBytes(e))}assertValidity(){}static fromCompact(e){return y.fromBytes(me("sig",e),"compact")}static fromDER(e){return y.fromBytes(me("sig",e),"der")}normalizeS(){return this.hasHighS()?new y(this.r,s.neg(this.s),this.recovery):this}toDERRawBytes(){return this.toBytes("der")}toDERHex(){return N(this.toBytes("der"))}toCompactRawBytes(){return this.toBytes("compact")}toCompactHex(){return N(this.toBytes("compact"))}}const w=n.bits2int||function(e){if(e.length>8192)throw new Error("input is too large");const t=de(e),n=8*e.length-c;return n>0?t>>BigInt(n):t},v=n.bits2int_modN||function(e){return s.create(w(e))},b=we(c);function E(e){return function(e,t,n,r){if(!_e(t,n,r))throw new Error("expected valid "+e+": "+n+" <= n < "+r+", got "+t)}("num < 2^"+c,e,ht,b),s.toBytes(e)}function k(e,n){return ce(e,void 0,"message"),n?ce(t(e),void 0,"prehashed message"):e}return Object.freeze({keygen:a,getPublicKey:f,getSharedSecret:d,utils:l,lengths:h,Point:e,sign:function(n,o,u={}){n=me("message",n);const{seed:c,k2sig:a}=function(t,n,i){if(["recovered","canonical"].some(e=>e in i))throw new Error("sign() legacy options not supported");const{lowS:o,prehash:u,extraEntropy:c}=ft(i,p);t=k(t,u);const a=v(t),f=yt(s,n),d=[E(f),E(a)];if(null!=c&&!1!==c){const e=!0===c?r(h.secretKey):c;d.push(me("extraEntropy",e))}const l=q(...d),m=a;return{seed:l,k2sig:function(t){const n=w(t);if(!s.isValidNot0(n))return;const r=s.inv(n),i=e.BASE.multiply(n).toAffine(),u=s.create(i.x);if(u===ht)return;const c=s.create(r*s.create(m+u*f));if(c===ht)return;let a=(i.x===u?0:2)|Number(i.y&pt),d=c;return o&&g(c)&&(d=s.neg(c),a^=1),new y(u,d,a)}}}(n,o,u);return function(e,t,n){if("number"!=typeof e||e<2)throw new Error("hashLen must be a number");if("number"!=typeof t||t<2)throw new Error("qByteLen must be a number");if("function"!=typeof n)throw new Error("hmacFn must be a function");const r=e=>new Uint8Array(e),i=e=>Uint8Array.of(e);let o=r(e),s=r(e),u=0;const c=()=>{o.fill(1),s.fill(0),u=0},a=(...e)=>n(s,o,...e),f=(e=r(0))=>{s=a(i(0),e),o=a(),0!==e.length&&(s=a(i(1),e),o=a())},d=()=>{if(u++>=1e3)throw new Error("drbg: tried 1000 values");let e=0;const n=[];for(;e<t;){o=a();const t=o.slice();n.push(t),e+=o.length}return q(...n)};return(e,t)=>{let n;for(c(),f(e);!(n=t(d()));)f();return c(),n}}(t.outputLen,s.BYTES,i)(c,a)},verify:function(t,n,r,i={}){const{lowS:o,prehash:u,format:c}=ft(i,p);if(r=me("publicKey",r),n=k(me("message",n),u),"strict"in i)throw new Error("options.strict was renamed to lowS");const a=void 0===c?function(e){let t;const n="string"==typeof e||x(e),r=!n&&null!==e&&"object"==typeof e&&"bigint"==typeof e.r&&"bigint"==typeof e.s;if(!n&&!r)throw new Error("invalid signature, expected Uint8Array, hex string or Signature instance");if(r)t=new y(e.r,e.s);else if(n){try{t=y.fromBytes(me("sig",e),"der")}catch(e){if(!(e instanceof lt.Err))throw e}if(!t)try{t=y.fromBytes(me("sig",e),"compact")}catch(e){return!1}}return t||!1}(t):y.fromBytes(me("sig",t),c);if(!1===a)return!1;try{const t=e.fromBytes(r);if(o&&a.hasHighS())return!1;const{r:i,s:u}=a,c=v(n),f=s.inv(u),d=s.create(c*f),l=s.create(i*f),h=e.BASE.multiplyUnsafe(d).add(t.multiplyUnsafe(l));if(h.is0())return!1;return s.create(h.x)===i}catch(e){return!1}},recoverPublicKey:function(e,t,n={}){const{prehash:r}=ft(n,p);return t=k(t,r),y.fromBytes(e,"recovered").recoverPublicKey(t).toBytes()},Signature:y,hash:t})}function xt(e){const{CURVE:t,curveOpts:n}=function(e){const t={a:e.a,b:e.b,p:e.Fp.ORDER,n:e.n,h:e.h,Gx:e.Gx,Gy:e.Gy},n=e.Fp;let r=e.allowedPrivateKeyLengths?Array.from(new Set(e.allowedPrivateKeyLengths.map(e=>Math.ceil(e/2)))):void 0;return{CURVE:t,curveOpts:{Fp:n,Fn:Ve(t.n,{BITS:e.nBitLength,allowedLengths:r,modFromBytes:e.wrapPrivateKey}),allowInfinityPoint:e.allowInfinityPoint,endo:e.endo,isTorsionFree:e.isTorsionFree,clearCofactor:e.clearCofactor,fromBytes:e.fromBytes,toBytes:e.toBytes}}}(e),r={hmac:e.hmac,randomBytes:e.randomBytes,lowS:e.lowS,bits2int:e.bits2int,bits2int_modN:e.bits2int_modN};return{CURVE:t,curveOpts:n,hash:e.hash,ecdsaOpts:r}}function kt(e){const{CURVE:t,curveOpts:n,hash:r,ecdsaOpts:i}=xt(e);return function(e,t){const n=t.Point;return Object.assign({},t,{ProjectivePoint:n,CURVE:Object.assign({},e,Le(n.Fn.ORDER,n.Fn.BITS))})}(e,Et(wt(t,n),r,i))}
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
const zt={p:BigInt("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"),n:BigInt("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141"),h:BigInt(1),a:BigInt(0),b:BigInt(7),Gx:BigInt("0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"),Gy:BigInt("0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8")},Ot={beta:BigInt("0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee"),basises:[[BigInt("0x3086d221a7d46bcde86c90e49284eb15"),-BigInt("0xe4437ed6010e88286f547fa90abfe4c3")],[BigInt("0x114ca50f7a8e2f3f657c1108d9d44cfd8"),BigInt("0x3086d221a7d46bcde86c90e49284eb15")]]},Pt=BigInt(0),St=BigInt(1),At=BigInt(2);const It=Ve(zt.p,{sqrt:function(e){const t=zt.p,n=BigInt(3),r=BigInt(6),i=BigInt(11),o=BigInt(22),s=BigInt(23),u=BigInt(44),c=BigInt(88),a=e*e*e%t,f=a*a*e%t,d=Be(f,n,t)*f%t,l=Be(d,n,t)*f%t,h=Be(l,At,t)*a%t,p=Be(h,i,t)*h%t,m=Be(p,o,t)*p%t,g=Be(m,u,t)*m%t,_=Be(g,c,t)*g%t,y=Be(_,u,t)*m%t,w=Be(y,n,t)*f%t,v=Be(w,s,t)*p%t,b=Be(v,r,t)*a%t,E=Be(b,At,t);if(!It.eql(It.sqr(E),e))throw new Error("Cannot find square root");return E}}),$t=function(e,t){const n=t=>kt({...e,hash:t});return{...n(t),create:n}}({...zt,Fp:It,lowS:!0,endo:Ot},ne),Tt={};function Bt(e,...t){let n=Tt[e];if(void 0===n){const t=ne(L(e));n=q(t,t),Tt[e]=n}return ne(q(n,...t))}const Nt=e=>e.toBytes(!0).slice(1),Ut=(()=>$t.Point)(),Zt=e=>e%At===Pt;function Rt(e){const{Fn:t,BASE:n}=Ut,r=yt(t,e),i=n.multiply(r);return{scalar:Zt(i.y)?r:t.neg(r),bytes:Nt(i)}}function Ct(e){const t=It;if(!t.isValidNot0(e))throw new Error("invalid x: Fail if x ≥ p");const n=t.create(e*e),r=t.create(n*e+BigInt(7));let i=t.sqrt(r);Zt(i)||(i=t.neg(i));const o=Ut.fromAffine({x:e,y:i});return o.assertValidity(),o}const jt=de;function Ft(...e){return Ut.Fn.create(jt(Bt("BIP0340/challenge",...e)))}function Dt(e){return Rt(e).bytes}function Ht(e,t,n=W(32)){const{Fn:r}=Ut,i=me("message",e),{bytes:o,scalar:s}=Rt(t),u=me("auxRand",n,32),c=r.toBytes(s^jt(Bt("BIP0340/aux",u))),a=Bt("BIP0340/nonce",c,o,i),{bytes:f,scalar:d}=Rt(a),l=Ft(f,o,i),h=new Uint8Array(64);if(h.set(f,0),h.set(r.toBytes(r.create(d+l*s)),32),!Lt(h,i,o))throw new Error("sign: Invalid signature produced");return h}function Lt(e,t,n){const{Fn:r,BASE:i}=Ut,o=me("signature",e,64),s=me("message",t),u=me("publicKey",n,32);try{const e=Ct(jt(u)),t=jt(o.subarray(0,32));if(!_e(t,St,zt.p))return!1;const n=jt(o.subarray(32,64));if(!_e(n,St,zt.n))return!1;const c=Ft(r.toBytes(t),Nt(e),s),a=i.multiplyUnsafe(n).add(e.multiplyUnsafe(r.neg(c))),{x:f,y:d}=a.toAffine();return!(a.is0()||!Zt(d)||f!==t)}catch(e){return!1}}const Vt=(()=>{const e=(e=W(48))=>Me(e,zt.n);return{keygen:function(t){const n=e(t);return{secretKey:n,publicKey:Dt(n)}},getPublicKey:Dt,sign:Ht,verify:Lt,Point:Ut,utils:{randomSecretKey:e,randomPrivateKey:e,taggedHash:Bt,lift_x:Ct,pointToBytes:Nt,numberToBytesBE:he,bytesToNumberBE:de,mod:Te},lengths:{secretKey:32,publicKey:32,publicKeyHasPrefix:!1,signature:64,seed:48}}})(),qt=BigInt(0),Kt=BigInt(2);Ve($t.CURVE.n,32,!0);const Mt=$t.Point.BASE;function Wt(e,t,n,r=!1){const i=Yt(t).big,o=function(e){try{const t=Gt(e,"ecdsa");return $t.Point.fromHex(t)}catch(t){throw new Error("invalid pubkey: "+e)}}(e),s=Mt.multiply(i);let u=o.add(s);r&&u.y%Kt!==qt&&(u=u.negate());return Gt(u.toHex(!0),n)}function Gt(e,t){try{c