@vbyte/btc-dev
Version:
Batteries-included toolset for plebian bitcoin development
11 lines • 146 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 w(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 y(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=w(e)),t(n)&&(n=w(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)(),N=Array.from({length:256},(e,t)=>t.toString(16).padStart(2,"0"));function B(e){if(z(e),T)return e.toHex();let t="";for(let n=0;n<e.length;n++)t+=N[e[n]];return t}const U=48,Z=57,R=65,C=70,j=97,F=102;function H(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 D(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=H(e.charCodeAt(i)),o=H(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){return"string"==typeof e&&(e=function(e){if("string"!=typeof e)throw new Error("string expected");return new Uint8Array((new TextEncoder).encode(e))}(e)),z(e),e}function V(...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 q{}function M(e){const t=t=>e().update(L(t)).digest(),n=e();return t.outputLen=n.outputLen,t.blockLen=n.blockLen,t.create=()=>e(),t}function K(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 W(e,t,n){return e&t^~e&n}function G(e,t,n){return e&t^e&n^t&n}class Y extends q{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=L(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 X=Uint32Array.from([1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225]),J=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]),Q=new Uint32Array(64);class ee extends Y{constructor(e=32){super(64,e,8,!1),this.A=0|X[0],this.B=0|X[1],this.C=0|X[2],this.D=0|X[3],this.E=0|X[4],this.F=0|X[5],this.G=0|X[6],this.H=0|X[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)Q[n]=e.getUint32(t,!1);for(let e=16;e<64;e++){const t=Q[e-15],n=Q[e-2],r=I(t,7)^I(t,18)^t>>>3,i=I(n,17)^I(n,19)^n>>>10;Q[e]=i+Q[e-7]+r+Q[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))+W(s,u,c)+J[e]+Q[e]|0,f=(I(n,2)^I(n,13)^I(n,22))+G(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(Q)}destroy(){this.set(0,0,0,0,0,0,0,0),S(this.buffer)}}const te=M(()=>new ee);class ne extends q{constructor(e,t){super(),this.finished=!1,this.destroyed=!1,O(e);const n=L(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 re=(e,t,n)=>new ne(e,t).update(n).digest();re.create=(e,t)=>new ne(e,t);
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
const ie=BigInt(0),oe=BigInt(1);function se(e,t){if("boolean"!=typeof t)throw new Error(e+" boolean expected, got "+t)}function ue(e){const t=e.toString(16);return 1&t.length?"0"+t:t}function ce(e){if("string"!=typeof e)throw new Error("hex string expected, got "+typeof e);return""===e?ie:BigInt("0x"+e)}function ae(e){return ce(B(e))}function fe(e){return z(e),ce(B(Uint8Array.from(e).reverse()))}function de(e,t){return D(e.toString(16).padStart(2*t,"0"))}function le(e,t){return de(e,t).reverse()}function he(e,t,n){let r;if("string"==typeof t)try{r=D(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 pe=e=>"bigint"==typeof e&&ie<=e;function me(e,t,n){return pe(e)&&pe(t)&&pe(n)&&t<=e&&e<n}function ge(e,t,n,r){if(!me(t,n,r))throw new Error("expected valid "+e+": "+n+" <= n < "+r+", got "+t)}function _e(e){let t;for(t=0;e>ie;e>>=oe,t+=1);return t}const we=e=>(oe<<BigInt(e))-oe;function ye(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 ve(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 be=BigInt(0),Ee=BigInt(1),xe=BigInt(2),ke=BigInt(3),ze=BigInt(4),Oe=BigInt(5),Pe=BigInt(7),Se=BigInt(8),Ae=BigInt(9),Ie=BigInt(16);function $e(e,t){const n=e%t;return n>=be?n:t+n}function Te(e,t,n){let r=e;for(;t-- >be;)r*=r,r%=n;return r}function Ne(e,t){if(e===be)throw new Error("invert: expected non-zero number");if(t<=be)throw new Error("invert: expected positive modulus, got "+t);let n=$e(e,t),r=t,i=be,o=Ee;for(;n!==be;){const e=r%n,t=i-o*(r/n);r=n,n=e,i=o,o=t}if(r!==Ee)throw new Error("invert: does not exist");return $e(i,t)}function Be(e,t,n){if(!e.eql(e.sqr(t),n))throw new Error("Cannot find square root")}function Ue(e,t){const n=(e.ORDER+Ee)/ze,r=e.pow(t,n);return Be(e,r,t),r}function Ze(e,t){const n=(e.ORDER-Oe)/Se,r=e.mul(t,xe),i=e.pow(r,n),o=e.mul(t,i),s=e.mul(e.mul(o,xe),i),u=e.mul(o,e.sub(s,e.ONE));return Be(e,u,t),u}function Re(e){if(e<ke)throw new Error("sqrt is not defined for small field");let t=e-Ee,n=0;for(;t%xe===be;)t/=xe,n++;let r=xe;const i=De(e);for(;1===He(i,r);)if(r++>1e3)throw new Error("Cannot find square root: probably non-prime P");if(1===n)return Ue;let o=i.pow(r,t);const s=(t+Ee)/xe;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=Ee<<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 Ce(e){return e%ze===ke?Ue:e%Se===Oe?Ze:e%Ie===Ae?function(e){const t=De(e),n=Re(e),r=n(t,t.neg(t.ONE)),i=n(t,r),o=n(t,t.neg(r)),s=(e+Pe)/Ie;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 Be(e,h,t),h}}(e):Re(e)}const je=["create","isValid","is0","neg","inv","sqrt","sqr","eql","add","sub","mul","pow","div","addN","subN","mulN","sqrN"];function Fe(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-Ee)/xe,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 De(e,t,n=!1,r={}){if(e<=be)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.modOnDecode&&(u=e.modOnDecode),s=e.allowedLengths}else"number"==typeof t&&(i=t),r.sqrt&&(o=r.sqrt);const{nBitLength:c,nByteLength:a}=function(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)}}(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:be,ONE:Ee,allowedLengths:s,create:t=>$e(t,e),isValid:t=>{if("bigint"!=typeof t)throw new Error("invalid field element: expected bigint, got "+typeof t);return be<=t&&t<e},is0:e=>e===be,isValidNot0:e=>!d.is0(e)&&d.isValid(e),isOdd:e=>(e&Ee)===Ee,neg:t=>$e(-t,e),eql:(e,t)=>e===t,sqr:t=>$e(t*t,e),add:(t,n)=>$e(t+n,e),sub:(t,n)=>$e(t-n,e),mul:(t,n)=>$e(t*n,e),pow:(e,t)=>function(e,t,n){if(n<be)throw new Error("invalid exponent, negatives unsupported");if(n===be)return e.ONE;if(n===Ee)return t;let r=e.ONE,i=t;for(;n>be;)n&Ee&&(r=e.mul(r,i)),i=e.sqr(i),n>>=Ee;return r}(d,e,t),div:(t,n)=>$e(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=Ce(e)),f(d,t))),toBytes:e=>n?le(e,a):de(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?fe(t):ae(t);if(u&&(i=$e(i,e)),!r&&!d.isValid(i))throw new Error("invalid field element: outside of range 0..ORDER");return i},invertBatch:e=>Fe(d,e),cmov:(e,t,n)=>n?t:e});return Object.freeze(d)}function Le(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 Ve(e){const t=Le(e);return t+Math.ceil(t/2)}function qe(e,t,n=!1){const r=e.length,i=Le(t),o=Ve(t);if(r<16||r<o||r>1024)throw new Error("expected "+o+"-1024 bytes of input, got "+r);const s=$e(n?fe(e):ae(e),t-Ee)+Ee;return n?le(s,i):de(s,i)}
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */const Me=BigInt(0),Ke=BigInt(1);function We(e,t){const n=t.negate();return e?n:t}function Ge(e,t){const n=Fe(e.Fp,t.map(e=>e.Z));return t.map((t,r)=>e.fromAffine(t.toAffine(n[r])))}function Ye(e,t){if(!Number.isSafeInteger(e)||e<=0||e>t)throw new Error("invalid window size, expected [1.."+t+"], got W="+e)}function Xe(e,t){Ye(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 Je(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+=Ke);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 Qe=new WeakMap,et=new WeakMap;function tt(e){return et.get(e)||1}function nt(e){if(e!==Me)throw new Error("invalid wNAF")}class rt{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>Me;)t&Ke&&(n=n.add(r)),r=r.double(),t>>=Ke;return n}precomputeWindow(e,t){const{windows:n,windowSize:r}=Xe(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=Xe(e,this.bits);for(let e=0;e<o.windows;e++){const{nextN:s,offset:u,isZero:c,isNeg:a,isNegF:f,offsetF:d}=Je(n,e,o);n=s,c?i=i.add(We(f,t[d])):r=r.add(We(a,t[u]))}return nt(n),{p:r,f:i}}wNAFUnsafe(e,t,n,r=this.ZERO){const i=Xe(e,this.bits);for(let e=0;e<i.windows&&n!==Me;e++){const{nextN:o,offset:s,isZero:u,isNeg:c}=Je(n,e,i);if(n=o,!u){const e=t[s];r=r.add(c?e.negate():e)}}return nt(n),r}getPrecomputes(e,t,n){let r=Qe.get(t);return r||(r=this.precomputeWindow(t,e),1!==e&&("function"==typeof n&&(r=n(r)),Qe.set(t,r))),r}cached(e,t,n){const r=tt(e);return this.wNAF(r,this.getPrecomputes(r,e,n),t)}unsafe(e,t,n,r){const i=tt(e);return 1===i?this._unsafeLadder(e,t,r):this.wNAFUnsafe(i,this.getPrecomputes(i,e,n),t,r)}createCache(e,t){Ye(t,this.bits),et.set(e,t),Qe.delete(e)}hasCache(e){return 1!==tt(e)}}function it(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=_e(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 ot(e,t){if(t){if(t.ORDER!==e)throw new Error("Field.ORDER must match order: Fp == p, Fn == n");return function(e){ye(e,je.reduce((e,t)=>(e[t]="function",e),{ORDER:"bigint",MASK:"bigint",BYTES:"number",BITS:"number"}))}(t),t}return De(e)}
/*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
const st=(e,t)=>(e+(e>=0?t:-t)/lt)/t;function ut(e){void 0!==e.lowS&&se("lowS",e.lowS),void 0!==e.prehash&&se("prehash",e.prehash)}class ct extends Error{constructor(e=""){super(e)}}const at={Err:ct,_tlv:{encode:(e,t)=>{const{Err:n}=at;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=ue(r);if(i.length/2&128)throw new n("tlv.encode: long form length too big");const o=r>127?ue(i.length/2|128):"";return ue(e)+o+i+t},decode(e,t){const{Err:n}=at;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}=at;if(e<ft)throw new t("integer: negative integers are not allowed");let n=ue(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}=at;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 ae(e)}},toSig(e){const{Err:t,_int:n,_tlv:r}=at,i=he("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}=at,r=t.encode(2,n.encode(e.r))+t.encode(2,n.encode(e.s));return t.encode(48,r)}},ft=BigInt(0),dt=BigInt(1),lt=BigInt(2),ht=BigInt(3),pt=BigInt(4);function mt(e,t){const{BYTES:n}=e;let r;if("bigint"==typeof t)r=t;else{let i=he("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 gt(e,t={}){const{Fp:n,Fn:r}=function(e,t,n={}){if(!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>Me))throw new Error(`CURVE.${e} must be positive bigint`)}const r=ot(t.p,n.Fp),i=ot(t.n,n.Fn),o=["Gx","Gy","a","b"];for(const e of o)if(!r.isValid(t[e]))throw new Error(`CURVE.${e} must be valid field element of CURVE.Fp`);return{Fp:r,Fn:i}}("weierstrass",e,t),{h:i,n:o}=e;ye(t,{},{allowInfinityPoint:"boolean",clearCofactor:"function",isTorsionFree:"function",fromBytes:"function",toBytes:"function",endo:"object",wrapPrivateKey:"boolean"});const{endo:s}=t;if(s&&(!n.is0(e.a)||"bigint"!=typeof s.beta||!Array.isArray(s.basises)))throw new Error('invalid endo: expected "beta": bigint and "basises": array');function u(){if(!n.isOdd)throw new Error("compression is not supported: Field does not have .isOdd()")}const c=t.toBytes||function(e,t,r){const{x:i,y:o}=t.toAffine(),s=n.toBytes(i);if(se("isCompressed",r),r){u();return V(_t(!n.isOdd(o)),s)}return V(Uint8Array.of(4),s,n.toBytes(o))},a=t.fromBytes||function(e){z(e);const t=n.BYTES,r=t+1,i=2*t+1,o=e.length,s=e[0],c=e.subarray(1);if(o!==r||2!==s&&3!==s){if(o===i&&4===s){const e=n.fromBytes(c.subarray(0*t,1*t)),r=n.fromBytes(c.subarray(1*t,2*t));if(!d(e,r))throw new Error("bad point: is not on curve");return{x:e,y:r}}throw new Error(`bad point: got length ${o}, expected compressed=${r} or uncompressed=${i}`)}{const e=n.fromBytes(c);if(!n.isValid(e))throw new Error("bad point: is not on curve, wrong x");const t=f(e);let r;try{r=n.sqrt(t)}catch(e){const t=e instanceof Error?": "+e.message:"";throw new Error("bad point: is not on curve, sqrt error"+t)}u();return!(1&~s)!==n.isOdd(r)&&(r=n.neg(r)),{x:e,y:r}}},f=function(e,t,n){return function(r){const i=e.sqr(r),o=e.mul(i,r);return e.add(e.add(o,e.mul(r,t)),n)}}(n,e.a,e.b);function d(e,t){const r=n.sqr(t),i=f(e);return n.eql(r,i)}if(!d(e.Gx,e.Gy))throw new Error("bad curve params: generator point");const l=n.mul(n.pow(e.a,ht),pt),h=n.mul(n.sqr(e.b),BigInt(27));if(n.is0(n.add(l,h)))throw new Error("bad curve params: a or b");function p(e,t,r=!1){if(!n.isValid(t)||r&&n.is0(t))throw new Error(`bad point coordinate ${e}`);return t}function m(e){if(!(e instanceof v))throw new Error("ProjectivePoint expected")}function g(e){if(!s||!s.basises)throw new Error("no endo");return function(e,t,n){const[[r,i],[o,s]]=t,u=st(s*e,n),c=st(-i*e,n);let a=e-u*r-c*o,f=-u*i-c*s;const d=a<ft,l=f<ft;d&&(a=-a),l&&(f=-f);const h=we(Math.ceil(_e(n)/2))+dt;if(a<ft||a>=h||f<ft||f>=h)throw new Error("splitScalar (endomorphism): failed, k="+e);return{k1neg:d,k1:a,k2neg:l,k2:f}}(e,s.basises,r.ORDER)}const _=ve((e,t)=>{const{X:r,Y:i,Z:o}=e;if(n.eql(o,n.ONE))return{x:r,y:i};const s=e.is0();null==t&&(t=s?n.ONE:n.inv(o));const u=n.mul(r,t),c=n.mul(i,t),a=n.mul(o,t);if(s)return{x:n.ZERO,y:n.ZERO};if(!n.eql(a,n.ONE))throw new Error("invZ was invalid");return{x:u,y:c}}),w=ve(e=>{if(e.is0()){if(t.allowInfinityPoint&&!n.is0(e.Y))return;throw new Error("bad point: ZERO")}const{x:r,y:i}=e.toAffine();if(!n.isValid(r)||!n.isValid(i))throw new Error("bad point: x or y not field elements");if(!d(r,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 y(e,t,r,i,o){return r=new v(n.mul(r.X,e),r.Y,r.Z),t=We(i,t),r=We(o,r),t.add(r)}class v{constructor(e,t,n){this.X=p("x",e),this.Y=p("y",t,!0),this.Z=p("z",n),Object.freeze(this)}static fromAffine(e){const{x:t,y:r}=e||{};if(!e||!n.isValid(t)||!n.isValid(r))throw new Error("invalid affine point");if(e instanceof v)throw new Error("projective point not allowed");return n.is0(t)&&n.is0(r)?v.ZERO:new v(t,r,n.ONE)}get x(){return this.toAffine().x}get y(){return this.toAffine().y}get px(){return this.X}get py(){return this.X}get pz(){return this.Z}static normalizeZ(e){return Ge(v,e)}static fromBytes(e){return z(e),v.fromHex(e)}static fromHex(e){const t=v.fromAffine(a(he("pointHex",e)));return t.assertValidity(),t}static fromPrivateKey(e){return v.BASE.multiply(mt(r,e))}static msm(e,t){return it(v,r,e,t)}_setWindowSize(e){this.precompute(e)}precompute(e=8,t=!0){return E.createCache(this,e),t||this.multiply(ht),this}assertValidity(){w(this)}hasEvenY(){const{y:e}=this.toAffine();if(!n.isOdd)throw new Error("Field doesn't support isOdd");return!n.isOdd(e)}equals(e){m(e);const{X:t,Y:r,Z:i}=this,{X:o,Y:s,Z:u}=e,c=n.eql(n.mul(t,u),n.mul(o,i)),a=n.eql(n.mul(r,u),n.mul(s,i));return c&&a}negate(){return new v(this.X,n.neg(this.Y),this.Z)}double(){const{a:t,b:r}=e,i=n.mul(r,ht),{X:o,Y:s,Z:u}=this;let c=n.ZERO,a=n.ZERO,f=n.ZERO,d=n.mul(o,o),l=n.mul(s,s),h=n.mul(u,u),p=n.mul(o,s);return p=n.add(p,p),f=n.mul(o,u),f=n.add(f,f),c=n.mul(t,f),a=n.mul(i,h),a=n.add(c,a),c=n.sub(l,a),a=n.add(l,a),a=n.mul(c,a),c=n.mul(p,c),f=n.mul(i,f),h=n.mul(t,h),p=n.sub(d,h),p=n.mul(t,p),p=n.add(p,f),f=n.add(d,d),d=n.add(f,d),d=n.add(d,h),d=n.mul(d,p),a=n.add(a,d),h=n.mul(s,u),h=n.add(h,h),d=n.mul(h,p),c=n.sub(c,d),f=n.mul(h,l),f=n.add(f,f),f=n.add(f,f),new v(c,a,f)}add(t){m(t);const{X:r,Y:i,Z:o}=this,{X:s,Y:u,Z:c}=t;let a=n.ZERO,f=n.ZERO,d=n.ZERO;const l=e.a,h=n.mul(e.b,ht);let p=n.mul(r,s),g=n.mul(i,u),_=n.mul(o,c),w=n.add(r,i),y=n.add(s,u);w=n.mul(w,y),y=n.add(p,g),w=n.sub(w,y),y=n.add(r,o);let b=n.add(s,c);return y=n.mul(y,b),b=n.add(p,_),y=n.sub(y,b),b=n.add(i,o),a=n.add(u,c),b=n.mul(b,a),a=n.add(g,_),b=n.sub(b,a),d=n.mul(l,y),a=n.mul(h,_),d=n.add(a,d),a=n.sub(g,d),d=n.add(g,d),f=n.mul(a,d),g=n.add(p,p),g=n.add(g,p),_=n.mul(l,_),y=n.mul(h,y),g=n.add(g,_),_=n.sub(p,_),_=n.mul(l,_),y=n.add(y,_),p=n.mul(g,y),f=n.add(f,p),p=n.mul(b,y),a=n.mul(w,a),a=n.sub(a,p),p=n.mul(w,g),d=n.mul(b,d),d=n.add(d,p),new v(a,f,d)}subtract(e){return this.add(e.negate())}is0(){return this.equals(v.ZERO)}multiply(e){const{endo:n}=t;if(!r.isValidNot0(e))throw new Error("invalid scalar: out of range");let i,o;const s=e=>E.cached(this,e,e=>Ge(v,e));if(n){const{k1neg:t,k1:r,k2neg:u,k2:c}=g(e),{p:a,f:f}=s(r),{p:d,f:l}=s(c);o=f.add(l),i=y(n.beta,a,d,t,u)}else{const{p:t,f:n}=s(e);i=t,o=n}return Ge(v,[i,o])[0]}multiplyUnsafe(e){const{endo:n}=t,i=this;if(!r.isValid(e))throw new Error("invalid scalar: out of range");if(e===ft||i.is0())return v.ZERO;if(e===dt)return i;if(E.hasCache(this))return this.multiply(e);if(n){const{k1neg:t,k1:r,k2neg:o,k2:s}=g(e),{p1:u,p2:c}=function(e,t,n,r){let i=t,o=e.ZERO,s=e.ZERO;for(;n>Me||r>Me;)n&Ke&&(o=o.add(i)),r&Ke&&(s=s.add(i)),i=i.double(),n>>=Ke,r>>=Ke;return{p1:o,p2:s}}(v,i,r,s);return y(n.beta,u,c,t,o)}return E.unsafe(i,e)}multiplyAndAddUnsafe(e,t,n){const r=this.multiplyUnsafe(t).add(e.multiplyUnsafe(n));return r.is0()?void 0:r}toAffine(e){return _(this,e)}isTorsionFree(){const{isTorsionFree:e}=t;return i===dt||(e?e(v,this):E.unsafe(this,o).is0())}clearCofactor(){const{clearCofactor:e}=t;return i===dt?this:e?e(v,this):this.multiplyUnsafe(i)}isSmallOrder(){return this.multiplyUnsafe(i).is0()}toBytes(e=!0){return se("isCompressed",e),this.assertValidity(),c(v,this,e)}toRawBytes(e=!0){return this.toBytes(e)}toHex(e=!0){return B(this.toBytes(e))}toString(){return`<Point ${this.is0()?"ZERO":this.toHex()}>`}}v.BASE=new v(e.Gx,e.Gy,n.ONE),v.ZERO=new v(n.ZERO,n.ONE,n.ZERO),v.Fp=n,v.Fn=r;const b=r.BITS,E=new rt(v,t.endo?Math.ceil(b/2):b);return v}function _t(e){return Uint8Array.of(e?2:3)}function wt(e,t,n={}){O(t),ye(n,{},{hmac:"function",lowS:"boolean",randomBytes:"function",bits2int:"function",bits2int_modN:"function"});const r=n.randomBytes||K,i=n.hmac||((e,...n)=>re(t,e,V(...n))),{Fp:o,Fn:s}=e,{ORDER:u,BITS:c}=s,a=Ve(u),f={secret:s.BYTES,public:1+o.BYTES,publicUncompressed:1+2*o.BYTES,signature:2*s.BYTES,seed:a};function d(e){return e>u>>dt}function l(e,t){if(!s.isValidNot0(t))throw new Error(`invalid signature ${e}: out of range 1..CURVE.n`)}class h{constructor(e,t,n){l("r",e),l("s",t),this.r=e,this.s=t,null!=n&&(this.recovery=n),Object.freeze(this)}static fromBytes(e,t="compact"){if("compact"===t){const t=s.BYTES;z(e,2*t);const n=e.subarray(0,t),r=e.subarray(t,2*t);return new h(s.fromBytes(n),s.fromBytes(r))}if("der"===t){z(e);const{r:t,s:n}=at.toSig(e);return new h(t,n)}throw new Error("invalid format")}static fromHex(e,t){return this.fromBytes(D(e),t)}addRecoveryBit(e){return new h(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*lt<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.fromHex(V(_t(!(1&c)),f)),l=s.inv(a),h=v(he("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 d(this.s)}normalizeS(){return this.hasHighS()?new h(this.r,s.neg(this.s),this.recovery):this}toBytes(e="compact"){if("compact"===e)return V(s.toBytes(this.r),s.toBytes(this.s));if("der"===e)return D(at.hexFromSig(this));throw new Error("invalid format")}toHex(e){return B(this.toBytes(e))}assertValidity(){}static fromCompact(e){return h.fromBytes(he("sig",e),"compact")}static fromDER(e){return h.fromBytes(he("sig",e),"der")}toDERRawBytes(){return this.toBytes("der")}toDERHex(){return B(this.toBytes("der"))}toCompactRawBytes(){return this.toBytes("compact")}toCompactHex(){return B(this.toBytes("compact"))}}function p(e){try{return!!mt(s,e)}catch(e){return!1}}function m(e=r(a)){return qe(e,u)}const g={isValidSecretKey:p,isValidPublicKey:function(t,n){try{const r=t.length;return(!0!==n||r===f.public)&&((!1!==n||r===f.publicUncompressed)&&!!e.fromBytes(t))}catch(e){return!1}},randomSecretKey:m,isValidPrivateKey:p,randomPrivateKey:m,normPrivateKeyToScalar:e=>mt(s,e),precompute:(t=8,n=e.BASE)=>n.precompute(t,!1)};function _(t,n=!0){return e.BASE.multiply(mt(s,t)).toBytes(n)}function w(t){if("bigint"==typeof t)return!1;if(t instanceof e)return!0;if(s.allowedLengths||f.secret===f.public)return;const n=he("key",t).length;return n===f.public||n===f.publicUncompressed}const y=n.bits2int||function(e){if(e.length>8192)throw new Error("input is too large");const t=ae(e),n=8*e.length-c;return n>0?t>>BigInt(n):t},v=n.bits2int_modN||function(e){return s.create(y(e))},b=we(c);function E(e){return ge("num < 2^"+c,e,ft,b),s.toBytes(e)}function k(n,i,o=P){if(["recovered","canonical"].some(e=>e in o))throw new Error("sign() legacy options not supported");let{lowS:u,prehash:c,extraEntropy:a}=o;null==u&&(u=!0),n=he("msgHash",n),ut(o),c&&(n=he("prehashed msgHash",t(n)));const l=v(n),p=mt(s,i),m=[E(p),E(l)];if(null!=a&&!1!==a){const e=!0===a?r(f.secret):a;m.push(he("extraEntropy",e))}const g=V(...m),_=l;return{seed:g,k2sig:function(t){const n=y(t);if(!s.isValidNot0(n))return;const r=s.inv(n),i=e.BASE.multiply(n).toAffine(),o=s.create(i.x);if(o===ft)return;const c=s.create(r*s.create(_+o*p));if(c===ft)return;let a=(i.x===o?0:2)|Number(i.y&dt),f=c;return u&&d(c)&&(f=function(e){return d(e)?s.neg(e):e}(c),a^=1),new h(o,f,a)}}}const P={lowS:n.lowS,prehash:!1},S={lowS:n.lowS,prehash:!1};return e.BASE.precompute(8),Object.freeze({keygen:function(e){const t=g.randomSecretKey(e);return{secretKey:t,publicKey:_(t)}},getPublicKey:_,sign:function(e,n,r=P){const{seed:o,k2sig:u}=k(e,n,r);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 V(...n)};return(e,t)=>{let n;for(c(),f(e);!(n=t(d()));)f();return c(),n}}(t.outputLen,s.BYTES,i)(o,u)},verify:function(n,r,i,o=S){const u=n;r=he("msgHash",r),i=he("publicKey",i),ut(o);const{lowS:c,prehash:a,format:f}=o;if("strict"in o)throw new Error("options.strict was renamed to lowS");let d,l;if(void 0===f){const e="string"==typeof u||x(u),t=!e&&null!==u&&"object"==typeof u&&"bigint"==typeof u.r&&"bigint"==typeof u.s;if(!e&&!t)throw new Error("invalid signature, expected Uint8Array, hex string or Signature instance");if(t)d=new h(u.r,u.s);else if(e){try{d=h.fromDER(u)}catch(e){if(!(e instanceof at.Err))throw e}if(!d)try{d=h.fromCompact(u)}catch(e){return!1}}}else if("compact"===f||"der"===f){if("string"!=typeof u&&!x(u))throw new Error('"der" / "compact" format expects Uint8Array signature');d=h.fromBytes(he("sig",u),f)}else{if("js"!==f)throw new Error('format must be "compact", "der" or "js"');if(!(u instanceof h))throw new Error('"js" format expects Signature instance');d=u}if(!d)return!1;try{if(l=e.fromHex(i),c&&d.hasHighS())return!1;a&&(r=t(r));const{r:n,s:o}=d,u=v(r),f=s.inv(o),h=s.create(u*f),p=s.create(n*f),m=e.BASE.multiplyUnsafe(h).add(l.multiplyUnsafe(p));if(m.is0())return!1;return s.create(m.x)===n}catch(e){return!1}},getSharedSecret:function(t,n,r=!0){if(!0===w(t))throw new Error("first arg must be private key");if(!1===w(n))throw new Error("second arg must be public key");const i=mt(s,t);return e.fromHex(n).multiply(i).toBytes(r)},utils:g,Point:e,Signature:h,info:{type:"weierstrass",lengths:f,publicKeyHasPrefix:!0}})}function yt(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:De(t.n,{BITS:e.nBitLength,allowedLengths:r,modOnDecode: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 vt(e){const{CURVE:t,curveOpts:n,hash:r,ecdsaOpts:i}=yt(e);return function(e,t){return Object.assign({},t,{ProjectivePoint:t.Point,CURVE:e})}(e,wt(gt(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 bt={p:BigInt("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"),n:BigInt("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141"),h:BigInt(1),a:BigInt(0),b:BigInt(7),Gx:BigInt("0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"),Gy:BigInt("0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8")},Et={beta:BigInt("0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee"),basises:[[BigInt("0x3086d221a7d46bcde86c90e49284eb15"),-BigInt("0xe4437ed6010e88286f547fa90abfe4c3")],[BigInt("0x114ca50f7a8e2f3f657c1108d9d44cfd8"),BigInt("0x3086d221a7d46bcde86c90e49284eb15")]]},xt=BigInt(0),kt=BigInt(1),zt=BigInt(2);function Ot(e){const t=bt.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=Te(f,n,t)*f%t,l=Te(d,n,t)*f%t,h=Te(l,zt,t)*a%t,p=Te(h,i,t)*h%t,m=Te(p,o,t)*p%t,g=Te(m,u,t)*m%t,_=Te(g,c,t)*g%t,w=Te(_,u,t)*m%t,y=Te(w,n,t)*f%t,v=Te(y,s,t)*p%t,b=Te(v,r,t)*a%t,E=Te(b,zt,t);if(!Pt.eql(Pt.sqr(E),e))throw new Error("Cannot find square root");return E}const Pt=De(bt.p,void 0,void 0,{sqrt:Ot}),St=function(e,t){const n=t=>vt({...e,hash:t});return{...n(t),create:n}}({...bt,Fp:Pt,lowS:!0,endo:Et},te),At={};function It(e,...t){let n=At[e];if(void 0===n){const t=te(Uint8Array.from(e,e=>e.charCodeAt(0)));n=V(t,t),At[e]=n}return te(V(n,...t))}const $t=e=>e.toBytes(!0).slice(1),Tt=e=>de(e,32),Nt=e=>$e(e,bt.p),Bt=e=>$e(e,bt.n),Ut=(()=>St.Point)(),Zt=e=>e%zt===xt;function Rt(e){let t=mt(Ut.Fn,e),n=Ut.BASE.multiply(t);return{scalar:Zt(n.y)?t:Bt(-t),bytes:$t(n)}}function Ct(e){ge("x",e,kt,bt.p);const t=Nt(e*e);let n=Ot(Nt(t*e+BigInt(7)));Zt(n)||(n=Nt(-n));const r=Ut.fromAffine({x:e,y:n});return r.assertValidity(),r}const jt=ae;function Ft(...e){return Bt(jt(It("BIP0340/challenge",...e)))}function Ht(e){return Rt(e).bytes}function Dt(e,t,n=K(32)){const r=he("message",e),{bytes:i,scalar:o}=Rt(t),s=he("auxRand",n,32),u=Tt(o^jt(It("BIP0340/aux",s))),c=It("BIP0340/nonce",u,i,r),a=Bt(jt(c));if(a===xt)throw new Error("sign failed: k is zero");const{bytes:f,scalar:d}=Rt(a),l=Ft(f,i,r),h=new Uint8Array(64);if(h.set(f,0),h.set(Tt(Bt(d+l*o)),32),!Lt(h,r,i))throw new Error("sign: Invalid signature produced");return h}function Lt(e,t,n){const r=he("signature",e,64),i=he("message",t),o=he("publicKey",n,32);try{const e=Ct(jt(o)),t=jt(r.subarray(0,32));if(!me(t,kt,bt.p))return!1;const n=jt(r.subarray(32,64));if(!me(n,kt,bt.n))return!1;const s=Ft(Tt(t),$t(e),i),u=Ut.BASE.multiplyUnsafe(n).add(e.multiplyUnsafe(Bt(-s))),{x:c,y:a}=u.toAffine();return!(u.is0()||!Zt(a)||c!==t)}catch(e){return!1}}const Vt=(()=>{const e=(e=K(48))=>qe(e,bt.n);return{keygen:function(t){const n=e(t);return{secretKey:n,publicKey:Ht(n)}},getPublicKey:Ht,sign:Dt,verify:Lt,Point:Ut,utils:{randomSecretKey:e,randomPrivateKey:e,taggedHash:It,lift_x:Ct,pointToBytes:$t,numberToBytesBE:de,bytesToNumberBE:ae,mod:$e},info:{type:"weierstrass",publicKeyHasPrefix:!1,lengths:{secret:32,public:32,signature:64,seed:48}}}})(),qt=BigInt(0),Mt=BigInt(2);De(St.CURVE.n,32,!0);const Kt=St.Point.BASE;function Wt(e,t,n,r=!1){const i=Yt(t).big,o=function(e){try{const t=Gt(e,"ecdsa");return St.Point.fromHex(t)}catch(t){throw new Error("invalid pubkey: "+e)}}(e),s=Kt.multiply(i);let u=o.add(s);r&&u.y%Mt!==qt&&(u=u.negate());return Gt(u.toHex(!0),n)}function Gt(e,t){try{const n=Yt(e);if(33===n.length&&"bip340"===t)return n.slice(1);if(32===n.length&&"ecdsa"===t){const e=g.num(2,1);return g.join([e,n])}return n}catch(t){throw new Error("invalid pubkey: "+String(e))}}function Yt(e){try{return g.bytes(e)}catch(t){throw new Error("invalid bytes: "+String(e))}}const Xt=Uint8Array.from([7,4,13,1,10,6,15,3,12,0,9,5,2,14,11,8]),Jt=(()=>Uint8Array.from(new Array(16).fill(0).map((e,t)=>t)))(),Qt=(()=>Jt.map(e=>(9*e+5)%16))(),en=(()=>{const e=[[Jt],[Qt]];for(let t=0;t<4;t++)for(let n of e)n.push(n[t].map(e=>Xt[e]));return e})(),tn=(()=>en[0])(),nn=(()=>en[1])(),rn=[[11,14,15,12,5,8,7,9,11,13,14,15,6,7,9,8],[12,13,11,15,6,9,9,7,12,15,11,13,7,8,7,7],[13,15,14,11,7,7,6,8,13,14,13,12,5,5,6,9],[14,11,12,14,8,6,5,5,15,12,15,14,9,9,8,6],[15,12,13,13,9,5,8,6,14,11,12,11,8,6,5,5]].map(e=>Uint8Array.from(e)),on=tn.map((e,t)=>e.map(e=>rn[t][e])),sn=nn.map((e,t)=>e.map(e=>rn[t][e])),un=Uint32Array.from([0,1518500249,1859775393,2400959708,2840853838]),cn=Uint32Array.from([1352829926,1548603684,1836072691,2053994217,0]);function an(e,t,n,r){return 0===e?t^n^r:1===e?t&n|~t&r:2===e?(t|~n)^r:3===