UNPKG

@bsv/message-box-client

Version:

A client for P2P messaging and payments

1 lines 350 kB
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.messageBoxClient=e():t.messageBoxClient=e()}(this,(()=>(()=>{"use strict";var t={d:(e,i)=>{for(var r in i)t.o(i,r)&&!t.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:i[r]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},e={};t.r(e),t.d(e,{MessageBoxClient:()=>Ks,PeerPayClient:()=>zs,STANDARD_PAYMENT_MESSAGEBOX:()=>js});var i={};t.r(i),t.d(i,{Decoder:()=>Ns,Encoder:()=>Ps,PacketType:()=>_s,protocol:()=>Es});class r{static zeros=["","0","00","000","0000","00000","000000","0000000","00000000","000000000","0000000000","00000000000","000000000000","0000000000000","00000000000000","000000000000000","0000000000000000","00000000000000000","000000000000000000","0000000000000000000","00000000000000000000","000000000000000000000","0000000000000000000000","00000000000000000000000","000000000000000000000000","0000000000000000000000000"];static groupSizes=[0,0,25,16,12,11,10,9,8,8,7,7,7,7,6,6,6,6,6,6,6,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5];static groupBases=[0,0,33554432,43046721,16777216,48828125,60466176,40353607,16777216,43046721,1e7,19487171,35831808,62748517,7529536,11390625,16777216,24137569,34012224,47045881,64e6,4084101,5153632,6436343,7962624,9765625,11881376,14348907,17210368,20511149,243e5,28629151,33554432,39135393,45435424,52521875,60466176];static wordSize=26;static WORD_SIZE_BIGINT=BigInt(r.wordSize);static WORD_MASK=(1n<<r.WORD_SIZE_BIGINT)-1n;static MAX_SAFE_INTEGER_BIGINT=BigInt(Number.MAX_SAFE_INTEGER);static MIN_SAFE_INTEGER_BIGINT=BigInt(Number.MIN_SAFE_INTEGER);static MAX_IMULN_ARG=67108863;static MAX_NUMBER_CONSTRUCTOR_MAG_BIGINT=(1n<<53n)-1n;_magnitude;_sign;_nominalWordLength;red;get negative(){return this._sign}set negative(t){this.assert(0===t||1===t,"Negative property must be 0 or 1");const e=1===t?1:0;0n===this._magnitude?this._sign=0:this._sign=e}get _computedWordsArray(){if(0n===this._magnitude)return[0];const t=[];let e=this._magnitude;for(;e>0n;)t.push(Number(e&r.WORD_MASK)),e>>=r.WORD_SIZE_BIGINT;return t.length>0?t:[0]}get words(){const t=this._computedWordsArray;if(this._nominalWordLength<=t.length)return t;const e=new Array(this._nominalWordLength).fill(0);for(let i=0;i<t.length;i++)e[i]=t[i];return e}set words(t){const e=this._sign;let i=0n;const s=t.length>0?t.length:1;for(let e=s-1;e>=0;e--){const s=void 0===t[e]?0:t[e];i=i<<r.WORD_SIZE_BIGINT|BigInt(s&Number(r.WORD_MASK))}this._magnitude=i,this._sign=e,this._nominalWordLength=s,this.normSign()}get length(){return Math.max(1,this._nominalWordLength)}static isBN(t){return t instanceof r||null!==t&&"object"==typeof t&&t.constructor?.wordSize===r.wordSize&&Array.isArray(t.words)}static max(t,e){return t.cmp(e)>0?t:e}static min(t,e){return t.cmp(e)<0?t:e}constructor(t=0,e=10,i="be"){if(this._magnitude=0n,this._sign=0,this._nominalWordLength=1,this.red=null,void 0===t&&(t=0),null===t)return void this._initializeState(0n,0);if("bigint"==typeof t)return this._initializeState(t<0n?-t:t,t<0n?1:0),void this.normSign();let r=e,s=i;if("le"!==e&&"be"!==e||(s=e,r=10),"number"!=typeof t)if(Array.isArray(t))this.initArray(t,s);else if("string"==typeof t){"hex"===r&&(r=16),this.assert("number"==typeof r&&r===(0|r)&&r>=2&&r<=36,"Base must be an integer between 2 and 36");const e=t.toString().replace(/\s+/g,"");let i=0,n=0;e.startsWith("-")?(i++,n=1):e.startsWith("+")&&i++;const a=e.substring(i);if(0===a.length)return this._initializeState(0n,1===n&&e.startsWith("-")?1:0),void this.normSign();if(16===r){let t;if("le"===s){const t=[];let e=a;e.length%2!=0&&(e="0"+e);for(let i=0;i<e.length;i+=2){const r=e.substring(i,i+2),s=parseInt(r,16);if(isNaN(s))throw new Error("Invalid character in "+e);t.push(s)}return this.initArray(t,"le"),this._sign=n,void this.normSign()}try{t=BigInt("0x"+a)}catch(t){throw new Error("Invalid character in "+a)}this._initializeState(t,n),this.normSign()}else try{if(this._parseBaseString(a,r),this._sign=n,this.normSign(),"le"===s){const t=this._sign;this.initArray(this.toArray("be"),"le"),this._sign=t,this.normSign()}}catch(t){const e=t;if(e.message.includes("Invalid character in string")||e.message.includes("Invalid digit for base")||e.message.startsWith("Invalid character:"))throw new Error("Invalid character");throw e}}else 0!==t?this.assert(!1,"Unsupported input type for BigNumber constructor"):this._initializeState(0n,0);else this.initNumber(t,s)}_bigIntToStringInBase(t,e){if(0n===t)return"0";if(e<2||e>36)throw new Error("Base must be between 2 and 36");let i="",r=t>0n?t:-t;const s=BigInt(e);for(;r>0n;)i="0123456789abcdefghijklmnopqrstuvwxyz"[Number(r%s)]+i,r/=s;return i}_parseBaseString(t,e){if(0===t.length)return this._magnitude=0n,void this._finishInitialization();this._magnitude=0n;const i=BigInt(e);let s=r.groupSizes[e],n=BigInt(r.groupBases[e]);0!==s&&0n!==n||(s=Math.floor(Math.log(67108863)/Math.log(e)),0===s&&(s=1),n=i**BigInt(s));let a=0;const o=t.length;let c=o%s;if(0===c&&o>0&&(c=s),c>0){const i=t.substring(a,a+c);this._magnitude=BigInt(this._parseBaseWord(i,e)),a+=c}for(;a<o;){const i=t.substring(a,a+s),r=BigInt(this._parseBaseWord(i,e));this._magnitude=this._magnitude*n+r,a+=s}this._finishInitialization()}_parseBaseWord(t,e){let i=0;for(let r=0;r<t.length;r++){const s=t.charCodeAt(r);let n;if(s>=48&&s<=57)n=s-48;else if(s>=65&&s<=90)n=s-65+10;else{if(!(s>=97&&s<=122))throw new Error("Invalid character: "+t[r]);n=s-97+10}if(n>=e)throw new Error("Invalid character");i=i*e+n}return i}_initializeState(t,e){this._magnitude=t,this._sign=0n===t?0:e,this._finishInitialization()}_finishInitialization(){if(0n===this._magnitude)this._nominalWordLength=1,this._sign=0;else{const t=this._magnitude.toString(2).length;this._nominalWordLength=Math.max(1,Math.ceil(t/r.wordSize))}}assert(t,e="Assertion failed"){if(!t)throw new Error(e)}initNumber(t,e="be"){if(this.assert(BigInt(Math.abs(t))<=r.MAX_NUMBER_CONSTRUCTOR_MAG_BIGINT,"The number is larger than 2 ^ 53 (unsafe)"),this.assert(t%1==0,"Number must be an integer for BigNumber conversion"),this._initializeState(BigInt(Math.abs(t)),t<0?1:0),"le"===e){const t=this._sign,e=this.toArray("be");this.initArray(e,"le"),this._sign=t,this.normSign()}return this}initArray(t,e){if(0===t.length)return this._initializeState(0n,0),this;let i=0n;if("be"===e)for(let e=0;e<t.length;e++)i=i<<8n|BigInt(255&t[e]);else for(let e=t.length-1;e>=0;e--)i=i<<8n|BigInt(255&t[e]);return this._initializeState(i,0),this}copy(t){t._magnitude=this._magnitude,t._sign=this._sign,t._nominalWordLength=this._nominalWordLength,t.red=this.red}static move(t,e){t._magnitude=e._magnitude,t._sign=e._sign,t._nominalWordLength=e._nominalWordLength,t.red=e.red}clone(){const t=new r(0n);return this.copy(t),t}expand(t){return this.assert(t>=0,"Expand size must be non-negative"),this._nominalWordLength=Math.max(this._nominalWordLength,t,1),this}strip(){return this._finishInitialization(),this.normSign()}normSign(){return 0n===this._magnitude&&(this._sign=0),this}inspect(){return(null!==this.red?"<BN-R: ":"<BN: ")+this.toString(16)+">"}_getMinimalHex(){return 0n===this._magnitude?"0":this._magnitude.toString(16)}toString(t=10,e=1){if(16===t||"hex"===t){let t=this._getMinimalHex();if(e>1)for("0"!==t&&t.length%2!=0&&(t="0"+t);t.length%e!=0;)t="0"+t;return(this.isNeg()?"-":"")+t}if("number"!=typeof t||t<2||t>36||t%1!=0)throw new Error("Base should be an integer between 2 and 36");return this.toBaseString(t,e)}toBaseString(t,e){if(0n===this._magnitude){let t="0";if(e>1)for(;t.length<e;)t="0"+t;return t}let i=r.groupSizes[t],s=BigInt(r.groupBases[t]);0!==i&&0n!==s||(i=Math.floor(Math.log(Number.MAX_SAFE_INTEGER)/Math.log(t)),0===i&&(i=1),s=BigInt(t)**BigInt(i));let n="",a=this._magnitude;for(;a>0n;){const e=a%s;a/=s;const o=this._bigIntToStringInBase(e,t);if(a>0n){const t=i-o.length;n=t>0&&t<r.zeros.length?r.zeros[t]+o+n:t>0?"0".repeat(t)+o+n:o+n}else n=o+n}if(e>0)for(;n.length<e;)n="0"+n;return(1===this._sign?"-":"")+n}toNumber(){const t=this._getSignedValue();if(t>r.MAX_SAFE_INTEGER_BIGINT||t<r.MIN_SAFE_INTEGER_BIGINT)throw new Error("Number can only safely store up to 53 bits");return Number(t)}toJSON(){const t=this._getMinimalHex();return(this.isNeg()?"-":"")+t}toArrayLikeGeneric(t,e){let i=this._magnitude,r=e?0:t.length-1;const s=e?1:-1;for(let e=0;e<t.length;++e){if(0n===i&&r>=0&&r<t.length)t[r]=0;else{if(!(r>=0&&r<t.length))break;t[r]=Number(0xffn&i)}i>>=8n,r+=s}}toArray(t="be",e){this.strip();const i=this.byteLength(),r=e??Math.max(1,i);this.assert(i<=r,"byte array longer than desired length"),this.assert(r>0,"Requested array length <= 0");const s=new Array(r).fill(0);return 0n===this._magnitude&&r>0?s:0n===this._magnitude&&0===r?[]:(this.toArrayLikeGeneric(s,"le"===t),s)}bitLength(){return 0n===this._magnitude?0:this._magnitude.toString(2).length}static toBitArray(t){const e=t.bitLength();if(0===e)return[];const i=new Array(e),r=t._magnitude;for(let t=0;t<e;t++)i[t]=0n!==(r>>BigInt(t)&1n)?1:0;return i}toBitArray(){return r.toBitArray(this)}zeroBits(){if(0n===this._magnitude)return 0;let t=0,e=this._magnitude;for(;0n===(1n&e)&&0n!==e;)t++,e>>=1n;return t}byteLength(){return 0n===this._magnitude?0:Math.ceil(this.bitLength()/8)}_getSignedValue(){return 1===this._sign?-this._magnitude:this._magnitude}_setValueFromSigned(t){t<0n?(this._magnitude=-t,this._sign=1):(this._magnitude=t,this._sign=0),this._finishInitialization(),this.normSign()}toTwos(t){this.assert(t>=0);const e=BigInt(t);let i=this._getSignedValue();1===this._sign&&0n!==this._magnitude&&(i=(1n<<e)+i),i&=(1n<<e)-1n;const s=new r(0n);return s._initializeState(i,0),s}fromTwos(t){this.assert(t>=0);const e=BigInt(t),i=this._magnitude;if(t>0&&0n!==(i>>e-1n&1n)&&0===this._sign){const t=i-(1n<<e),s=new r(0n);return s._setValueFromSigned(t),s}return this.clone()}isNeg(){return 1===this._sign&&0n!==this._magnitude}neg(){return this.clone().ineg()}ineg(){return 0n!==this._magnitude&&(this._sign=1===this._sign?0:1),this}_iuop(t,e){const i=e(this._magnitude,t._magnitude),r=e===((t,e)=>t^e);let s=this._nominalWordLength;return r&&(s=Math.max(this.length,t.length)),this._magnitude=i,this._finishInitialization(),r&&(this._nominalWordLength=Math.max(this._nominalWordLength,s)),this.strip()}iuor(t){return this._iuop(t,((t,e)=>t|e))}iuand(t){return this._iuop(t,((t,e)=>t&e))}iuxor(t){return this._iuop(t,((t,e)=>t^e))}_iop(t,e){return this.assert(0===this._sign&&0===t._sign),this._iuop(t,e)}ior(t){return this._iop(t,((t,e)=>t|e))}iand(t){return this._iop(t,((t,e)=>t&e))}ixor(t){return this._iop(t,((t,e)=>t^e))}_uop_new(t,e){return this.length>=t.length?this.clone()[e](t):t.clone()[e](this)}or(t){return this.assert(0===this._sign&&0===t._sign),this._uop_new(t,"iuor")}uor(t){return this._uop_new(t,"iuor")}and(t){return this.assert(0===this._sign&&0===t._sign),this._uop_new(t,"iuand")}uand(t){return this._uop_new(t,"iuand")}xor(t){return this.assert(0===this._sign&&0===t._sign),this._uop_new(t,"iuxor")}uxor(t){return this._uop_new(t,"iuxor")}inotn(t){this.assert("number"==typeof t&&t>=0);const e=(1n<<BigInt(t))-1n;this._magnitude=~this._magnitude&e;const i=0===t?1:Math.ceil(t/r.wordSize);return this._nominalWordLength=Math.max(1,i),this.strip(),this._nominalWordLength=Math.max(this._nominalWordLength,Math.max(1,i)),this}notn(t){return this.clone().inotn(t)}setn(t,e){this.assert("number"==typeof t&&t>=0);const i=BigInt(t);1===e||!0===e?this._magnitude|=1n<<i:this._magnitude&=~(1n<<i);const s=Math.floor(t/r.wordSize)+1;return this._nominalWordLength=Math.max(this._nominalWordLength,s),this._finishInitialization(),this.strip()}iadd(t){return this._setValueFromSigned(this._getSignedValue()+t._getSignedValue()),this}add(t){const e=new r(0n);return e._setValueFromSigned(this._getSignedValue()+t._getSignedValue()),e}isub(t){return this._setValueFromSigned(this._getSignedValue()-t._getSignedValue()),this}sub(t){const e=new r(0n);return e._setValueFromSigned(this._getSignedValue()-t._getSignedValue()),e}mul(t){const e=new r(0n);return e._magnitude=this._magnitude*t._magnitude,e._sign=0n===e._magnitude?0:this._sign^t._sign,e._nominalWordLength=this.length+t.length,e.red=null,e.normSign()}imul(t){return this._magnitude*=t._magnitude,this._sign=0n===this._magnitude?0:this._sign^t._sign,this._nominalWordLength=this.length+t.length,this.red=null,this.normSign()}imuln(t){return this.assert("number"==typeof t,"Assertion failed"),this.assert(Math.abs(t)<=r.MAX_IMULN_ARG,"Assertion failed"),this._setValueFromSigned(this._getSignedValue()*BigInt(t)),this}muln(t){return this.clone().imuln(t)}sqr(){const t=new r(0n);return t._magnitude=this._magnitude*this._magnitude,t._sign=0,t._nominalWordLength=2*this.length,t.red=null,t}isqr(){return this._magnitude*=this._magnitude,this._sign=0,this._nominalWordLength=2*this.length,this.red=null,this}pow(t){if(this.assert(0===t._sign,"Exponent for pow must be non-negative"),t.isZero())return new r(1n);const e=new r(1n),i=this.clone(),s=t.clone(),n=i.isNeg(),a=s.isOdd();for(n&&i.ineg();!s.isZero();)s.isOdd()&&e.imul(i),i.isqr(),s.iushrn(1);return n&&a&&e.ineg(),e}iushln(t){return this.assert("number"==typeof t&&t>=0),0===t?this:(this._magnitude<<=BigInt(t),this._finishInitialization(),this.strip())}ishln(t){return this.assert(0===this._sign,"ishln requires positive number"),this.iushln(t)}iushrn(t,e,i){if(this.assert("number"==typeof t&&t>=0),0===t)return null!=i&&i._initializeState(0n,0),this;if(null!=i){const e=(1n<<BigInt(t))-1n,r=this._magnitude&e;i._initializeState(r,0)}return this._magnitude>>=BigInt(t),this._finishInitialization(),this.strip()}ishrn(t,e,i){return this.assert(0===this._sign,"ishrn requires positive number"),this.iushrn(t,e,i)}shln(t){return this.clone().ishln(t)}ushln(t){return this.clone().iushln(t)}shrn(t){return this.clone().ishrn(t)}ushrn(t){return this.clone().iushrn(t)}testn(t){return this.assert("number"==typeof t&&t>=0),0n!==(this._magnitude>>BigInt(t)&1n)}imaskn(t){this.assert("number"==typeof t&&t>=0),this.assert(0===this._sign,"imaskn works only with positive numbers");const e=BigInt(t),i=0n===e?0n:(1n<<e)-1n;this._magnitude&=i;const s=0===t?1:Math.max(1,Math.ceil(t/r.wordSize));return this._nominalWordLength=s,this._finishInitialization(),this._nominalWordLength=Math.max(this._nominalWordLength,s),this.strip()}maskn(t){return this.clone().imaskn(t)}iaddn(t){return this.assert("number"==typeof t),this.assert(Math.abs(t)<=r.MAX_IMULN_ARG,"num is too large"),this._setValueFromSigned(this._getSignedValue()+BigInt(t)),this}_iaddn(t){return this.iaddn(t)}isubn(t){return this.assert("number"==typeof t),this.assert(Math.abs(t)<=r.MAX_IMULN_ARG,"Assertion failed"),this._setValueFromSigned(this._getSignedValue()-BigInt(t)),this}addn(t){return this.clone().iaddn(t)}subn(t){return this.clone().isubn(t)}iabs(){return this._sign=0,this}abs(){return this.clone().iabs()}divmod(t,e,i){if(this.assert(!t.isZero(),"Division by zero"),this.isZero()){const t=new r(0n);return{div:"mod"!==e?t:null,mod:"div"!==e?t:null}}const s=this._getSignedValue(),n=t._getSignedValue();let a=null,o=null;"mod"!==e&&(a=s/n),"div"!==e&&(o=s%n,!0===i&&o<0n&&(o+=n<0n?-n:n));const c=null!==a?new r(0n):null;null!==c&&null!==a&&c._setValueFromSigned(a);const h=null!==o?new r(0n):null;return null!==h&&null!==o&&h._setValueFromSigned(o),{div:c,mod:h}}div(t){return this.divmod(t,"div",!1).div}mod(t){return this.divmod(t,"mod",!1).mod}umod(t){return this.divmod(t,"mod",!0).mod}divRound(t){this.assert(!t.isZero());const e=this._getSignedValue(),i=t._getSignedValue();let s=e/i;const n=e%i;if(0n===n){const t=new r(0n);return t._setValueFromSigned(s),t}2n*(n<0n?-n:n)>=(i<0n?-i:i)&&(e>0n&&i>0n||e<0n&&i<0n?s+=1n:s-=1n);const a=new r(0n);return a._setValueFromSigned(s),a}modrn(t){this.assert(0!==t,"Division by zero in modrn");const e=BigInt(Math.abs(t));if(0n===e)throw new Error("Division by zero in modrn");const i=this._magnitude%e;return Number(t<0?-i:i)}idivn(t){return this.assert(0!==t),this.assert(Math.abs(t)<=r.MAX_IMULN_ARG,"num is too large"),this._setValueFromSigned(this._getSignedValue()/BigInt(t)),this}divn(t){return this.clone().idivn(t)}egcd(t){this.assert(0===t._sign,"p must not be negative"),this.assert(!t.isZero(),"p must not be zero");let e=this._getSignedValue(),i=t._magnitude,s=1n,n=0n,a=0n,o=1n;for(;0n!==i;){const t=e/i;let r=i;i=e%i,e=r,r=n,n=s-t*n,s=r,r=o,o=a-t*o,a=r}const c=new r(0n);c._setValueFromSigned(s);const h=new r(0n);h._setValueFromSigned(a);const d=new r(0n);return d._initializeState(e<0n?-e:e,0),{a:c,b:h,gcd:d}}gcd(t){let e=this._magnitude,i=t._magnitude;if(0n===e){const t=new r(0n);return t._setValueFromSigned(i),t.iabs()}if(0n===i){const t=new r(0n);return t._setValueFromSigned(e),t.iabs()}for(;0n!==i;){const t=e%i;e=i,i=t}const s=new r(0n);return s._initializeState(e,0),s}invm(t){this.assert(!t.isZero()&&0===t._sign,"Modulus for invm must be positive and non-zero");const e=this.egcd(t);if(!e.gcd.eqn(1))throw new Error("Inverse does not exist (numbers are not coprime).");return e.a.umod(t)}isEven(){return this._magnitude%2n===0n}isOdd(){return this._magnitude%2n===1n}andln(t){return this.assert(t>=0),Number(this._magnitude&BigInt(t))}bincn(t){this.assert("number"==typeof t&&t>=0);const e=1n<<BigInt(t);return this._setValueFromSigned(this._getSignedValue()+e),this}isZero(){return 0n===this._magnitude}cmpn(t){this.assert(Math.abs(t)<=r.MAX_IMULN_ARG,"Number is too big");const e=this._getSignedValue(),i=BigInt(t);return e<i?-1:e>i?1:0}cmp(t){const e=this._getSignedValue(),i=t._getSignedValue();return e<i?-1:e>i?1:0}ucmp(t){return this._magnitude<t._magnitude?-1:this._magnitude>t._magnitude?1:0}gtn(t){return 1===this.cmpn(t)}gt(t){return 1===this.cmp(t)}gten(t){return this.cmpn(t)>=0}gte(t){return this.cmp(t)>=0}ltn(t){return-1===this.cmpn(t)}lt(t){return-1===this.cmp(t)}lten(t){return this.cmpn(t)<=0}lte(t){return this.cmp(t)<=0}eqn(t){return 0===this.cmpn(t)}eq(t){return 0===this.cmp(t)}toRed(t){return this.assert(null==this.red,"Already a number in reduction context"),this.assert(0===this._sign,"toRed works only with positives"),t.convertTo(this).forceRed(t)}fromRed(){return this.assert(this.red,"fromRed works only with numbers in reduction context"),this.red.convertFrom(this)}forceRed(t){return this.red=t,this}redAdd(t){return this.assert(this.red,"redAdd works only with red numbers"),this.red.add(this,t)}redIAdd(t){return this.assert(this.red,"redIAdd works only with red numbers"),this.red.iadd(this,t)}redSub(t){return this.assert(this.red,"redSub works only with red numbers"),this.red.sub(this,t)}redISub(t){return this.assert(this.red,"redISub works only with red numbers"),this.red.isub(this,t)}redShl(t){return this.assert(this.red,"redShl works only with red numbers"),this.red.shl(this,t)}redMul(t){return this.assert(this.red,"redMul works only with red numbers"),this.red.verify2(this,t),this.red.mul(this,t)}redIMul(t){return this.assert(this.red,"redIMul works only with red numbers"),this.red.verify2(this,t),this.red.imul(this,t)}redSqr(){return this.assert(this.red,"redSqr works only with red numbers"),this.red.verify1(this),this.red.sqr(this)}redISqr(){return this.assert(this.red,"redISqr works only with red numbers"),this.red.verify1(this),this.red.isqr(this)}redSqrt(){return this.assert(this.red,"redSqrt works only with red numbers"),this.red.verify1(this),this.red.sqrt(this)}redInvm(){return this.assert(this.red,"redInvm works only with red numbers"),this.red.verify1(this),this.red.invm(this)}redNeg(){return this.assert(this.red,"redNeg works only with red numbers"),this.red.verify1(this),this.red.neg(this)}redPow(t){return this.assert(null!=this.red&&null==t.red,"redPow(normalNum)"),this.red.verify1(this),this.red.pow(this,t)}static fromHex(t,e){let i="be";return"little"!==e&&"le"!==e||(i="le"),new r(t,16,i)}toHex(t=0){if(this.isZero()&&0===t)return"";let e=this._getMinimalHex();"0"!==e&&e.length%2!=0&&(e="0"+e);const i=2*t;for(;e.length<i;)e="0"+e;return(this.isNeg()?"-":"")+e}static fromJSON(t){return new r(t,16)}static fromNumber(t){return new r(t)}static fromString(t,e){return new r(t,e)}static fromSm(t,e="big"){if(0===t.length)return new r(0n);let i=0,s="";if("little"===e){const e=t.length-1;let r=t[e];128&r&&(i=1,r&=127),s+=(r<16?"0":"")+r.toString(16);for(let i=e-1;i>=0;i--){const e=t[i];s+=(e<16?"0":"")+e.toString(16)}}else{let e=t[0];128&e&&(i=1,e&=127),s+=(e<16?"0":"")+e.toString(16);for(let e=1;e<t.length;e++){const i=t[e];s+=(i<16?"0":"")+i.toString(16)}}const n=""===s?0n:BigInt("0x"+s),a=new r(0n);return a._initializeState(n,i),a}toSm(t="big"){if(0n===this._magnitude)return 1===this._sign?[128]:[];let e=this._getMinimalHex();e.length%2!=0&&(e="0"+e);const i=e.length/2,r=new Array(i);for(let t=0,i=0;t<e.length;t+=2)r[i++]=parseInt(e.slice(t,t+2),16);return 1===this._sign?128&r[0]?r.unshift(128):r[0]|=128:128&r[0]&&r.unshift(0),"little"===t?r.reverse():r}static fromBits(t,e=!1){const i=t>>>24,s=8388607&t,n=!!(8388608&t);if(e&&n)throw new Error("negative bit set");if(0===i&&0===s){if(n&&e)throw new Error("negative bit set for zero value");return new r(0n)}const a=new r(s);return i<=3?a.iushrn(8*(3-i)):a.iushln(8*(i-3)),n&&a.ineg(),a}toBits(){if(this.strip(),this.isZero()&&!this.isNeg())return 0;const t=this.isNeg(),e=this.abs();let i=e.toArray("be"),r=0;for(;r<i.length-1&&0===i[r];)r++;i=i.slice(r);let s,n=i.length;if(0!==n||e.isZero()||(i=[0],n=1),e.isZero()&&(n=0,i=[]),0===n)s=0;else if(n<=3){s=0;for(let t=0;t<n;t++)s=s<<8|i[t]}else s=i[0]<<16|i[1]<<8|i[2];8388608&s&&n<=255&&(s>>>=8,n++);let a=n<<24|s;return t&&(a|=8388608),a>>>0}static fromScriptNum(t,e=!1,i){if(void 0!==i&&t.length>i)throw new Error("script number overflow");if(0===t.length)return new r(0n);if(e&&!(127&t[t.length-1]||!(t.length<=1)&&128&t[t.length-2]))throw new Error("non-minimally encoded script number");return r.fromSm(t,"little")}toScriptNum(){return this.toSm("little")}_invmp(t){this.assert(0===t._sign,"p must not be negative for _invmp"),this.assert(!t.isZero(),"p must not be zero for _invmp");let e=this.umod(t)._magnitude,i=t._magnitude,s=1n,n=0n;const a=t._magnitude;for(;e>1n&&i>1n;){let t=0;for(;0n===(e>>BigInt(t)&1n);)t++;if(t>0){e>>=BigInt(t);for(let e=0;e<t;++e)0n!==(1n&s)&&(s+=a),s>>=1n}let r=0;for(;0n===(i>>BigInt(r)&1n);)r++;if(r>0){i>>=BigInt(r);for(let t=0;t<r;++t)0n!==(1n&n)&&(n+=a),n>>=1n}e>=i?(e-=i,s-=n):(i-=e,n-=s)}let o;if(1n===e)o=s;else if(1n===i)o=n;else if(0n===e&&1n===i)o=n;else{if(0n!==i||1n!==e)throw new Error("_invmp: GCD is not 1, inverse does not exist. aVal="+e+", bVal="+i);o=s}o%=a,o<0n&&(o+=a);const c=new r(0n);return c._initializeState(o,0),c}mulTo(t,e){return e._magnitude=this._magnitude*t._magnitude,e._sign=0n===e._magnitude?0:this._sign^t._sign,e._nominalWordLength=this.length+t.length,e.red=null,e.normSign(),e}}class s{name;p;k;n;tmp;constructor(t,e){this.name=t,this.p=new r(e,16),this.n=this.p.bitLength(),this.k=new r(BigInt(1)).iushln(this.n).isub(this.p),this.tmp=this._tmp()}_tmp(){const t=new r(BigInt(0)),e=Math.ceil(this.n/r.wordSize);return t.expand(Math.max(1,e)),t}ireduce(t){const e=t;let i;do{this.split(e,this.tmp),this.imulK(e),e.iadd(this.tmp),i=e.bitLength()}while(i>this.n);const r=i<this.n?-1:e.ucmp(this.p);return 0===r?e.words=[0]:r>0&&e.isub(this.p),e.strip(),e}split(t,e){t.iushrn(this.n,0,e)}imulK(t){return t.imul(this.k)}}class n extends s{constructor(){super("k256","ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f")}split(t,e){const i=4194303,r=t.words,s=t.length,n=Math.min(s,9),a=new Array(n+(s>9?1:0)).fill(0);for(let t=0;t<n;t++)a[t]=r[t];let o=n;if(s<=9){const i=new Array(o);for(let t=0;t<o;++t)i[t]=a[t];return e.words=i,void(t.words=[0])}let c=r[9];a[o++]=c&i;const h=new Array(o);for(let t=0;t<o;++t)h[t]=a[t];e.words=h;const d=new Array(Math.max(1,s-9)).fill(0);let u=0;for(let t=10;t<s;t++){const e=0|r[t];u<d.length&&(d[u++]=(e&i)<<4|c>>>22),c=e}c>>>=22,u<d.length?d[u++]=c:0!==c&&d.length;const l=new Array(u);for(let t=0;t<u;++t)l[t]=d[t];t.words=l}imulK(t){const e=t.words,i=t.length,r=i+2,s=new Array(r).fill(0);for(let t=0;t<i;t++)s[t]=e[t];let n=0;for(let t=0;t<r;t++){const e=0|s[t];n+=977*e,s[t]=67108863&n,n=64*e+(n/67108864|0)}return t.words=s,t}}class a{prime;m;constructor(t){if("k256"===t){const t=new n;this.m=t.p,this.prime=t}else this.assert(t.gtn(1),"modulus must be greater than 1"),this.m=t,this.prime=null}assert(t,e="Assertion failed"){if(!t)throw new Error(e)}verify1(t){this.assert(0===t.negative,"red works only with positives"),this.assert(t.red,"red works only with red numbers")}verify2(t,e){this.assert(!(t.negative|e.negative),"red works only with positives"),this.assert(null!=t.red&&t.red===e.red,"red works only with red numbers")}imod(t){return null!=this.prime?this.prime.ireduce(t).forceRed(this):(r.move(t,t.umod(this.m).forceRed(this)),t)}neg(t){return t.isZero()?t.clone():this.m.sub(t).forceRed(this)}add(t,e){this.verify2(t,e);const i=t.add(e);return i.cmp(this.m)>=0&&i.isub(this.m),i.forceRed(this)}iadd(t,e){this.verify2(t,e);const i=t.iadd(e);return i.cmp(this.m)>=0&&i.isub(this.m),i}sub(t,e){this.verify2(t,e);const i=t.sub(e);return i.cmpn(0)<0&&i.iadd(this.m),i.forceRed(this)}isub(t,e){this.verify2(t,e);const i=t.isub(e);return i.cmpn(0)<0&&i.iadd(this.m),i}shl(t,e){return this.verify1(t),this.imod(t.ushln(e))}imul(t,e){return this.verify2(t,e),this.imod(t.imul(e))}mul(t,e){return this.verify2(t,e),this.imod(t.mul(e))}isqr(t){return this.imul(t,t.clone())}sqr(t){return this.mul(t,t)}sqrt(t){if(t.isZero())return t.clone();const e=this.m.andln(3);if(this.assert(e%2==1),3===e){const e=this.m.add(new r(1)).iushrn(2);return this.pow(t,e)}const i=this.m.subn(1);let s=0;for(;!i.isZero()&&0===i.andln(1);)s++,i.iushrn(1);this.assert(!i.isZero());const n=new r(1).toRed(this),a=n.redNeg(),o=this.m.subn(1).iushrn(1),c=this.m.bitLength(),h=new r(2*c*c).toRed(this);for(;0!==this.pow(h,o).cmp(a);)h.redIAdd(a);let d=this.pow(h,i),u=this.pow(t,i.addn(1).iushrn(1)),l=this.pow(t,i),f=s;for(;0!==l.cmp(n);){let t=l,e=0;for(;0!==t.cmp(n);e++)t=t.redSqr();this.assert(e<f);const i=this.pow(d,new r(1).iushln(f-e-1));u=u.redMul(i),d=i.redSqr(),l=l.redMul(d),f=e}return u}invm(t){const e=t._invmp(this.m);return 0!==e.negative?(e.negative=0,this.imod(e).redNeg()):this.imod(e)}pow(t,e){if(e.isZero())return new r(1).toRed(this);if(0===e.cmpn(1))return t.clone();const i=new Array(16);i[0]=new r(1).toRed(this),i[1]=t;let s=2;for(;s<i.length;s++)i[s]=this.mul(i[s-1],t);let n=i[0],a=0,o=0,c=e.bitLength()%26;for(0===c&&(c=26),s=e.length-1;s>=0;s--){const t=e.words[s];for(let e=c-1;e>=0;e--){const r=t>>e&1;n!==i[0]&&(n=this.sqr(n)),0!==r||0!==a?(a<<=1,a|=r,o++,(4===o||0===s&&0===e)&&(n=this.mul(n,i[a]),o=0,a=0)):o=0}c=26}return n}convertTo(t){const e=t.umod(this.m);return e===t?e.clone():e}convertFrom(t){const e=t.clone();return e.red=null,e}}class o extends a{shift;r;r2;rinv;minv;constructor(t){super(t),this.shift=this.m.bitLength(),this.shift%26!=0&&(this.shift+=26-this.shift%26),this.r=new r(1).iushln(this.shift),this.r2=this.imod(this.r.sqr()),this.rinv=this.r._invmp(this.m),this.minv=this.rinv.mul(this.r).isubn(1).div(this.m),this.minv=this.minv.umod(this.r),this.minv=this.r.sub(this.minv)}convertTo(t){return this.imod(t.ushln(this.shift))}convertFrom(t){const e=this.imod(t.mul(this.rinv));return e.red=null,e}imul(t,e){if(t.isZero()||e.isZero())return t.words[0]=0,t.length=1,t;const i=t.imul(e),r=i.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),s=i.isub(r).iushrn(this.shift);let n=s;return s.cmp(this.m)>=0?n=s.isub(this.m):s.cmpn(0)<0&&(n=s.iadd(this.m)),n.forceRed(this)}mul(t,e){if(t.isZero()||e.isZero())return new r(0).forceRed(this);const i=t.mul(e),s=i.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),n=i.isub(s).iushrn(this.shift);let a=n;return n.cmp(this.m)>=0?a=n.isub(this.m):n.cmpn(0)<0&&(a=n.iadd(this.m)),a.forceRed(this)}invm(t){return this.imod(t._invmp(this.m).mul(this.r2)).forceRed(this)}}class c{curve;type;precomputed;constructor(t){this.curve=new be,this.type=t,this.precomputed=null}}class h extends c{x;y;z;zOne;constructor(t,e,i){super("jacobian"),null===t&&null===e&&null===i?(this.x=this.curve.one,this.y=this.curve.one,this.z=new r(0)):(r.isBN(t)||(t=new r(t,16)),this.x=t,r.isBN(e)||(e=new r(e,16)),this.y=e,r.isBN(i)||(i=new r(i,16)),this.z=i),null==this.x.red&&(this.x=this.x.toRed(this.curve.red)),null==this.y.red&&(this.y=this.y.toRed(this.curve.red)),null==this.z.red&&(this.z=this.z.toRed(this.curve.red)),this.zOne=this.z===this.curve.one}toP(){if(this.isInfinity())return new fe(null,null);const t=this.z.redInvm(),e=t.redSqr(),i=this.x.redMul(e),r=this.y.redMul(e).redMul(t);return new fe(i,r)}neg(){return new h(this.x,this.y.redNeg(),this.z)}add(t){if(this.isInfinity())return t;if(t.isInfinity())return this;const e=t.z.redSqr(),i=this.z.redSqr(),r=this.x.redMul(e),s=t.x.redMul(i),n=this.y.redMul(e.redMul(t.z)),a=t.y.redMul(i.redMul(this.z)),o=r.redSub(s),c=n.redSub(a);if(0===o.cmpn(0))return 0!==c.cmpn(0)?new h(null,null,null):this.dbl();const d=o.redSqr(),u=d.redMul(o),l=r.redMul(d),f=c.redSqr().redIAdd(u).redISub(l).redISub(l),p=c.redMul(l.redISub(f)).redISub(n.redMul(u)),b=this.z.redMul(t.z).redMul(o);return new h(f,p,b)}mixedAdd(t){if(this.isInfinity())return t.toJ();if(t.isInfinity())return this;if(null===t.x||null===t.y)throw new Error("Point coordinates cannot be null");const e=this.z.redSqr(),i=this.x,r=t.x.redMul(e),s=this.y,n=t.y.redMul(e).redMul(this.z),a=i.redSub(r),o=s.redSub(n);if(0===a.cmpn(0))return 0!==o.cmpn(0)?new h(null,null,null):this.dbl();const c=a.redSqr(),d=c.redMul(a),u=i.redMul(c),l=o.redSqr().redIAdd(d).redISub(u).redISub(u),f=o.redMul(u.redISub(l)).redISub(s.redMul(d)),p=this.z.redMul(a);return new h(l,f,p)}dblp(t){if(0===t)return this;if(this.isInfinity())return this;if(void 0===t)return this.dbl();let e=this;for(let i=0;i<t;i++)e=e.dbl();return e}dbl(){if(this.isInfinity())return this;let t,e,i;if(this.zOne){const r=this.x.redSqr(),s=this.y.redSqr(),n=s.redSqr();let a=this.x.redAdd(s).redSqr().redISub(r).redISub(n);a=a.redIAdd(a);const o=r.redAdd(r).redIAdd(r),c=o.redSqr().redISub(a).redISub(a);let h=n.redIAdd(n);h=h.redIAdd(h),h=h.redIAdd(h),t=c,e=o.redMul(a.redISub(c)).redISub(h),i=this.y.redAdd(this.y)}else{const r=this.x.redSqr(),s=this.y.redSqr(),n=s.redSqr();let a=this.x.redAdd(s).redSqr().redISub(r).redISub(n);a=a.redIAdd(a);const o=r.redAdd(r).redIAdd(r),c=o.redSqr();let h=n.redIAdd(n);h=h.redIAdd(h),h=h.redIAdd(h),t=c.redISub(a).redISub(a),e=o.redMul(a.redISub(t)).redISub(h),i=this.y.redMul(this.z),i=i.redIAdd(i)}return new h(t,e,i)}eq(t){if("affine"===t.type)return this.eq(t.toJ());if(this===t)return!0;const e=this.z.redSqr(),i=t.z.redSqr();if(0!==this.x.redMul(i).redISub(t.x.redMul(e)).cmpn(0))return!1;const r=e.redMul(this.z),s=i.redMul(t.z);return 0===this.y.redMul(s).redISub(t.y.redMul(r)).cmpn(0)}eqXToP(t){const e=this.z.redSqr(),i=t.toRed(this.curve?.red).redMul(e);if(0===this.x.cmp(i))return!0;const r=t.clone();if(null===this.curve||null==this.curve.redN)throw new Error("Curve or redN is not initialized.");const s=this.curve.redN.redMul(e);for(;r.cmp(this.curve.p)<0;){if(r.iadd(this.curve.n),r.cmp(this.curve.p)>=0)return!1;if(i.redIAdd(s),0===this.x.cmp(i))return!0}return!1}inspect(){return this.isInfinity()?"<EC JPoint Infinity>":"<EC JPoint x: "+this.x.toString(16,2)+" y: "+this.y.toString(16,2)+" z: "+this.z.toString(16,2)+">"}isInfinity(){return 0===this.z.cmpn(0)}}const d=(t,e="Hash assertion failed")=>{if(!t)throw new Error(e)};class u{pending;pendingTotal;blockSize;outSize;endian;_delta8;_delta32;padLength;hmacStrength;constructor(t,e,i,r){this.pending=null,this.pendingTotal=0,this.blockSize=t,this.outSize=e,this.hmacStrength=i,this.padLength=r/8,this.endian="big",this._delta8=this.blockSize/8,this._delta32=this.blockSize/32}_update(t,e){throw new Error("Not implemented")}_digest(){throw new Error("Not implemented")}_digestHex(){throw new Error("Not implemented")}update(t,e){if(t=f(t,e),null==this.pending?this.pending=t:this.pending=this.pending.concat(t),this.pendingTotal+=t.length,this.pending.length>=this._delta8){const e=(t=this.pending).length%this._delta8;this.pending=t.slice(t.length-e,t.length),0===this.pending.length&&(this.pending=null),t=function(t,e,i,r){const s=i-0;d(s%4==0);const n=new Array(s/4);for(let e=0,i=0;e<n.length;e++,i+=4){let s;s="big"===r?t[i]<<24|t[i+1]<<16|t[i+2]<<8|t[i+3]:t[i+3]<<24|t[i+2]<<16|t[i+1]<<8|t[i],n[e]=s>>>0}return n}(t,0,t.length-e,this.endian);for(let e=0;e<t.length;e+=this._delta32)this._update(t,e)}return this}digest(){return this.update(this._pad()),d(null===this.pending),this._digest()}digestHex(){return this.update(this._pad()),d(null===this.pending),this._digestHex()}_pad(){let t=this.pendingTotal;const e=this._delta8,i=e-(t+this.padLength)%e,r=new Array(i+this.padLength);let s,n;for(r[0]=128,s=1;s<i;s++)r[s]=0;if(t<<=3,"big"===this.endian){for(n=8;n<this.padLength;n++)r[s++]=0;r[s++]=0,r[s++]=0,r[s++]=0,r[s++]=0,r[s++]=t>>>24&255,r[s++]=t>>>16&255,r[s++]=t>>>8&255,r[s++]=255&t}else for(r[s++]=255&t,r[s++]=t>>>8&255,r[s++]=t>>>16&255,r[s++]=t>>>24&255,r[s++]=0,r[s++]=0,r[s++]=0,r[s++]=0,n=8;n<this.padLength;n++)r[s++]=0;return r}}function l(t,e){return 55296==(64512&t.charCodeAt(e))&&!(e<0||e+1>=t.length)&&56320==(64512&t.charCodeAt(e+1))}function f(t,e){if(Array.isArray(t))return t.slice();if(!t)return[];const i=[];if("string"==typeof t)if("hex"!==e){let e=0;for(let r=0;r<t.length;r++){let s=t.charCodeAt(r);s<128?i[e++]=s:s<2048?(i[e++]=s>>6|192,i[e++]=63&s|128):l(t,r)?(s=65536+((1023&s)<<10)+(1023&t.charCodeAt(++r)),i[e++]=s>>18|240,i[e++]=s>>12&63|128,i[e++]=s>>6&63|128,i[e++]=63&s|128):(i[e++]=s>>12|224,i[e++]=s>>6&63|128,i[e++]=63&s|128)}}else{(t=t.replace(/[^a-z0-9]+/gi,"")).length%2!=0&&(t="0"+t);for(let e=0;e<t.length;e+=2)i.push(parseInt(t[e]+t[e+1],16))}else for(let e=0;e<t.length;e++)i[e]=0|t[e];return i}function p(t){return(t>>>24|t>>>8&65280|t<<8&16711680|(255&t)<<24)>>>0}function b(t,e){let i="";for(let s=0;s<t.length;s++){let n=t[s];"little"===e&&(n=p(n)),i+=7===(r=n.toString(16)).length?"0"+r:6===r.length?"00"+r:5===r.length?"000"+r:4===r.length?"0000"+r:3===r.length?"00000"+r:2===r.length?"000000"+r:1===r.length?"0000000"+r:r}var r;return i}function g(t){let e="";for(const i of t)e+=i.toString(16).padStart(2,"0");return e}function y(t,e){const i=new Array(4*t.length);for(let r=0,s=0;r<t.length;r++,s+=4){const n=t[r];"big"===e?(i[s]=n>>>24,i[s+1]=n>>>16&255,i[s+2]=n>>>8&255,i[s+3]=255&n):(i[s+3]=n>>>24,i[s+2]=n>>>16&255,i[s+1]=n>>>8&255,i[s]=255&n)}return i}function m(t,e){return t>>>e|t<<32-e}function w(t,e){return t<<e|t>>>32-e}function I(t,e){return t+e>>>0}function v(t,e,i){return t+e+i>>>0}function k(t,e,i,r){return t+e+i+r>>>0}function S(t,e,i,r,s){return t+e+i+r+s>>>0}function E(t,e,i,r){return 0===t?_(e,i,r):1===t||3===t?function(t,e,i){return t^e^i}(e,i,r):2===t?P(e,i,r):0}function _(t,e,i){return t&e^~t&i}function P(t,e,i){return t&e^t&i^e&i}function O(t){return m(t,2)^m(t,13)^m(t,22)}function N(t){return m(t,6)^m(t,11)^m(t,25)}function x(t){return m(t,17)^m(t,19)^t>>>10}const A=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,7,4,13,1,10,6,15,3,12,0,9,5,2,14,11,8,3,10,14,4,9,15,8,1,2,7,0,6,13,11,5,12,1,9,11,10,0,8,12,4,13,3,7,15,14,5,6,2,4,0,5,9,7,12,2,10,14,1,3,8,11,6,15,13],T=[5,14,7,0,9,2,11,4,13,6,15,8,1,10,3,12,6,11,3,7,0,13,5,10,14,15,8,12,4,9,1,2,15,5,1,3,7,14,6,9,11,8,12,2,10,0,4,13,8,6,4,1,3,11,15,0,5,12,2,13,9,7,10,14,12,15,10,4,1,5,8,7,6,2,13,14,0,3,9,11],C=[11,14,15,12,5,8,7,9,11,13,14,15,6,7,9,8,7,6,8,13,11,9,7,15,7,12,15,9,11,7,13,12,11,13,6,7,14,9,13,15,14,8,13,6,5,12,7,5,11,12,14,15,14,15,9,8,9,14,5,6,8,6,5,12,9,15,5,11,6,8,13,12,5,12,13,14,11,8,5,6],R=[8,9,9,11,13,15,15,5,7,7,8,11,14,14,12,6,9,13,15,7,12,8,9,11,7,7,12,7,6,15,13,11,9,7,15,11,8,6,6,14,12,13,5,14,13,13,7,5,15,5,8,11,14,14,6,14,6,9,12,9,12,5,15,8,8,5,12,9,12,5,14,6,8,13,6,5,15,13,11,11];function B(t,e,i,r){return t<=15?e^i^r:t<=31?e&i|~e&r:t<=47?(e|~i)^r:t<=63?e&r|i&~r:e^(i|~r)}function L(t){return t<=15?0:t<=31?1518500249:t<=47?1859775393:t<=63?2400959708:2840853838}function M(t){return t<=15?1352829926:t<=31?1548603684:t<=47?1836072691:t<=63?2053994217:0}class D extends u{h;constructor(){super(512,160,192,64),this.endian="little",this.h=[1732584193,4023233417,2562383102,271733878,3285377520],this.endian="little"}_update(t,e){let i,r=this.h[0],s=this.h[1],n=this.h[2],a=this.h[3],o=this.h[4],c=r,h=s,d=n,u=a,l=o;for(let f=0;f<80;f++)i=I(w(k(r,B(f,s,n,a),t[A[f]+e],L(f)),C[f]),o),r=o,o=a,a=w(n,10),n=s,s=i,i=I(w(k(c,B(79-f,h,d,u),t[T[f]+e],M(f)),R[f]),l),c=l,l=u,u=w(d,10),d=h,h=i;i=v(this.h[1],n,u),this.h[1]=v(this.h[2],a,l),this.h[2]=v(this.h[3],o,c),this.h[3]=v(this.h[4],r,h),this.h[4]=v(this.h[0],s,d),this.h[0]=i}_digest(){return y(this.h,"little")}_digestHex(){return b(this.h,"little")}}class F{h;constructor(){this.h=new It}update(t,e){const i=Uint8Array.from(f(t,e));return this.h.update(i),this}digest(){return Array.from(this.h.digest())}digestHex(){return g(this.h.digest())}}class H extends u{h;W;k;constructor(){super(512,160,80,64),this.k=[1518500249,1859775393,2400959708,3395469782],this.h=[1732584193,4023233417,2562383102,271733878,3285377520],this.W=new Array(80)}_update(t,e){const i=this.W;let r;for(void 0===e&&(e=0),r=0;r<16;r++)i[r]=t[e+r];for(;r<i.length;r++)i[r]=w(i[r-3]^i[r-8]^i[r-14]^i[r-16],1);let s=this.h[0],n=this.h[1],a=this.h[2],o=this.h[3],c=this.h[4];for(r=0;r<i.length;r++){const t=~~(r/20),e=S(w(s,5),E(t,n,a,o),c,i[r],this.k[t]);c=o,o=a,a=w(n,30),n=s,s=e}this.h[0]=I(this.h[0],s),this.h[1]=I(this.h[1],n),this.h[2]=I(this.h[2],a),this.h[3]=I(this.h[3],o),this.h[4]=I(this.h[4],c)}_digest(){return y(this.h,"big")}_digestHex(){return b(this.h,"big")}}class V{h;blockSize=64;outSize=32;constructor(t){const e=Uint8Array.from(f(t,"hex"));this.h=new At(vt,e)}update(t,e){return this.h.update(Uint8Array.from(f(t,e))),this}digest(){return Array.from(this.h.digest())}digestHex(){return g(this.h.digest())}}class q{h;blockSize=128;outSize=32;constructor(t){const e=Uint8Array.from(f(t,"hex"));this.h=new At(xt,e)}update(t,e){return this.h.update(Uint8Array.from(f(t,e))),this}digest(){return Array.from(this.h.digest())}digestHex(){return g(this.h.digest())}}const U=(t,e)=>(new F).update(t,e).digest(),K=(t,e)=>{const i=(new F).update(t,e).digest();return(new F).update(i).digest()},$=(t,e)=>{const i=(new F).update(t,e).digest();return(new D).update(i).digest()},j=(t,e,i)=>new V(t).update(e,i).digest();function z(t){if(!Number.isSafeInteger(t)||t<0)throw new Error(`positive integer expected, got ${t}`)}function W(t,...e){if(!((i=t)instanceof Uint8Array||ArrayBuffer.isView(i)&&"Uint8Array"===i.constructor.name))throw new Error("Uint8Array expected");var i;if(e.length>0&&!e.includes(t.length)){const i=e.join(",");throw new Error(`Uint8Array expected of length ${i}, got length=${t.length}`)}}function G(t,e=!0){if(!0===t.destroyed)throw new Error("Hash instance has been destroyed");if(e&&!0===t.finished)throw new Error("Hash#digest() has already been called")}function X(...t){for(let e=0;e<t.length;e++)t[e].fill(0)}function J(t){return new DataView(t.buffer,t.byteOffset,t.byteLength)}function Y(t){return"string"==typeof t&&(t=function(t){if("string"!=typeof t)throw new Error("string expected");return new Uint8Array((new TextEncoder).encode(t))}(t)),W(t),t}class Z{}function Q(t){const e=e=>t().update(Y(e)).digest(),i=t();return e.outputLen=i.outputLen,e.blockLen=i.blockLen,e.create=()=>t(),e}const tt=BigInt(2**32-1),et=BigInt(32);function it(t,e=!1){return e?{h:Number(t&tt),l:Number(t>>et&tt)}:{h:0|Number(t>>et&tt),l:0|Number(t&tt)}}const rt=(t,e,i)=>t>>>i,st=(t,e,i)=>t<<32-i|e>>>i,nt=(t,e,i)=>t>>>i|e<<32-i,at=(t,e,i)=>t<<32-i|e>>>i,ot=(t,e,i)=>t<<64-i|e>>>i-32,ct=(t,e,i)=>t>>>i-32|e<<64-i;function ht(t,e,i,r){const s=(e>>>0)+(r>>>0);return{h:t+i+(s/2**32|0)|0,l:0|s}}const dt=(t,e,i)=>(t>>>0)+(e>>>0)+(i>>>0),ut=(t,e,i,r)=>e+i+r+(t/2**32|0)|0,lt=(t,e,i,r)=>(t>>>0)+(e>>>0)+(i>>>0)+(r>>>0),ft=(t,e,i,r,s)=>e+i+r+s+(t/2**32|0)|0,pt=(t,e,i,r,s)=>(t>>>0)+(e>>>0)+(i>>>0)+(r>>>0)+(s>>>0),bt=(t,e,i,r,s,n)=>e+i+r+s+n+(t/2**32|0)|0;class gt extends Z{blockLen;outputLen;padOffset;isLE;buffer;view;finished=!1;length=0;pos=0;destroyed=!1;constructor(t,e,i,r){super(),this.blockLen=t,this.outputLen=e,this.padOffset=i,this.isLE=r,this.buffer=new Uint8Array(t),this.view=J(this.buffer)}update(t){G(this),W(t=Y(t));const{view:e,buffer:i,blockLen:r}=this,s=t.length;for(let n=0;n<s;){const a=Math.min(r-this.pos,s-n);if(a!==r)i.set(t.subarray(n,n+a),this.pos),this.pos+=a,n+=a,this.pos===r&&(this.process(e,0),this.pos=0);else{const e=J(t);for(;r<=s-n;n+=r)this.process(e,n)}}return this.length+=t.length,this.roundClean(),this}digestInto(t){G(this),function(t,e){W(t);const i=e.outputLen;if(t.length<i)throw new Error(`digestInto() expects output buffer of length at least ${i}`)}(t,this),this.finished=!0;const{buffer:e,view:i,blockLen:r,isLE:s}=this;let{pos:n}=this;e[n++]=128,X(this.buffer.subarray(n)),this.padOffset>r-n&&(this.process(i,0),n=0);for(let t=n;t<r;t++)e[t]=0;!function(t,e,i,r){if("function"==typeof t.setBigUint64)return t.setBigUint64(e,i,r);const s=BigInt(32),n=BigInt(4294967295),a=Number(i>>s&n),o=Number(i&n),c=r?4:0,h=r?0:4;t.setUint32(e+c,a,r),t.setUint32(e+h,o,r)}(i,r-8,BigInt(8*this.length),s),this.process(i,0);const a=J(t),o=this.outputLen;if(o%4!=0)throw new Error("_sha2: outputLen should be aligned to 32bit");const c=o/4,h=this.get();if(c>h.length)throw new Error("_sha2: outputLen bigger than state");for(let t=0;t<c;t++)a.setUint32(4*t,h[t],s)}digest(){const{buffer:t,outputLen:e}=this;this.digestInto(t);const i=t.slice(0,e);return this.destroy(),i}_cloneInto(t){t||=new this.constructor,t.set(...this.get());const{blockLen:e,buffer:i,length:r,finished:s,destroyed:n,pos:a}=this;return t.destroyed=n,t.finished=s,t.length=r,t.pos=a,r%e!=0&&t.buffer.set(i),t}clone(){return this._cloneInto()}}const yt=Uint32Array.from([1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225]),mt=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]),wt=new Uint32Array(64);class It extends gt{A=0|yt[0];B=0|yt[1];C=0|yt[2];D=0|yt[3];E=0|yt[4];F=0|yt[5];G=0|yt[6];H=0|yt[7];constructor(t=32){super(64,t,8,!1)}get(){const{A:t,B:e,C:i,D:r,E:s,F:n,G:a,H:o}=this;return[t,e,i,r,s,n,a,o]}set(t,e,i,r,s,n,a,o){this.A=0|t,this.B=0|e,this.C=0|i,this.D=0|r,this.E=0|s,this.F=0|n,this.G=0|a,this.H=0|o}process(t,e){for(let i=0;i<16;i++,e+=4)wt[i]=t.getUint32(e);for(let t=16;t<64;t++){const e=wt[t-15],r=wt[t-2],s=m(i=e,7)^m(i,18)^i>>>3,n=x(r);wt[t]=I(I(s,wt[t-7]),I(n,wt[t-16]))}var i;let{A:r,B:s,C:n,D:a,E:o,F:c,G:h,H:d}=this;for(let t=0;t<64;t++){const e=S(d,N(o),_(o,c,h),mt[t],wt[t]),i=I(O(r),P(r,s,n));d=h,h=c,c=o,o=I(a,e),a=n,n=s,s=r,r=I(e,i)}this.A=I(this.A,r),this.B=I(this.B,s),this.C=I(this.C,n),this.D=I(this.D,a),this.E=I(this.E,o),this.F=I(this.F,c),this.G=I(this.G,h),this.H=I(this.H,d)}roundClean(){X(wt)}destroy(){X(this.buffer),this.set(0,0,0,0,0,0,0,0)}}const vt=Q((()=>new It)),kt=Uint32Array.from([1779033703,4089235720,3144134277,2227873595,1013904242,4271175723,2773480762,1595750129,1359893119,2917565137,2600822924,725511199,528734635,4215389547,1541459225,327033209]),St=function(t,e=!1){const i=t.length,r=new Uint32Array(i),s=new Uint32Array(i);for(let n=0;n<i;n++){const{h:i,l:a}=it(t[n],e);r[n]=i,s[n]=a}return[r,s]}(["0x428a2f98d728ae22","0x7137449123ef65cd","0xb5c0fbcfec4d3b2f","0xe9b5dba58189dbbc","0x3956c25bf348b538","0x59f111f1b605d019","0x923f82a4af194f9b","0xab1c5ed5da6d8118","0xd807aa98a3030242","0x12835b0145706fbe","0x243185be4ee4b28c","0x550c7dc3d5ffb4e2","0x72be5d74f27b896f","0x80deb1fe3b1696b1","0x9bdc06a725c71235","0xc19bf174cf692694","0xe49b69c19ef14ad2","0xefbe4786384f25e3","0x0fc19dc68b8cd5b5","0x240ca1cc77ac9c65","0x2de92c6f592b0275","0x4a7484aa6ea6e483","0x5cb0a9dcbd41fbd4","0x76f988da831153b5","0x983e5152ee66dfab","0xa831c66d2db43210","0xb00327c898fb213f","0xbf597fc7beef0ee4","0xc6e00bf33da88fc2","0xd5a79147930aa725","0x06ca6351e003826f","0x142929670a0e6e70","0x27b70a8546d22ffc","0x2e1b21385c26c926","0x4d2c6dfc5ac42aed","0x53380d139d95b3df","0x650a73548baf63de","0x766a0abb3c77b2a8","0x81c2c92e47edaee6","0x92722c851482353b","0xa2bfe8a14cf10364","0xa81a664bbc423001","0xc24b8b70d0f89791","0xc76c51a30654be30","0xd192e819d6ef5218","0xd69906245565a910","0xf40e35855771202a","0x106aa07032bbd1b8","0x19a4c116b8d2d0c8","0x1e376c085141ab53","0x2748774cdf8eeb99","0x34b0bcb5e19b48a8","0x391c0cb3c5c95a63","0x4ed8aa4ae3418acb","0x5b9cca4f7763e373","0x682e6ff3d6b2b8a3","0x748f82ee5defb2fc","0x78a5636f43172f60","0x84c87814a1f0ab72","0x8cc702081a6439ec","0x90befffa23631e28","0xa4506cebde82bde9","0xbef9a3f7b2c67915","0xc67178f2e372532b","0xca273eceea26619c","0xd186b8c721c0c207","0xeada7dd6cde0eb1e","0xf57d4f7fee6ed178","0x06f067aa72176fba","0x0a637dc5a2c898a6","0x113f9804bef90dae","0x1b710b35131c471b","0x28db77f523047d84","0x32caab7b40c72493","0x3c9ebe0a15c9bebc","0x431d67c49c100d4c","0x4cc5d4becb3e42b6","0x597f299cfc657e2a","0x5fcb6fab3ad6faec","0x6c44198c4a475817"].map((t=>BigInt(t)))),Et=St[0],_t=St[1],Pt=new Uint32Array(80),Ot=new Uint32Array(80);class Nt extends gt{Ah=0|kt[0];Al=0|kt[1];Bh=0|kt[2];Bl=0|kt[3];Ch=0|kt[4];Cl=0|kt[5];Dh=0|kt[6];Dl=0|kt[7];Eh=0|kt[8];El=0|kt[9];Fh=0|kt[10];Fl=0|kt[11];Gh=0|kt[12];Gl=0|kt[13];Hh=0|kt[14];Hl=0|kt[15];constructor(t=64){super(128,t,16,!1)}get(){const{Ah:t,Al:e,Bh:i,Bl:r,Ch:s,Cl:n,Dh:a,Dl:o,Eh:c,El:h,Fh:d,Fl:u,Gh:l,Gl:f,Hh:p,Hl:b}=this;return[t,e,i,r,s,n,a,o,c,h,d,u,l,f,p,b]}set(t,e,i,r,s,n,a,o,c,h,d,u,l,f,p,b){this.Ah=0|t,this.Al=0|e,this.Bh=0|i,this.Bl=0|r,this.Ch=0|s,this.Cl=0|n,this.Dh=0|a,this.Dl=0|o,this.Eh=0|c,this.El=0|h,this.Fh=0|d,this.Fl=0|u,this.Gh=0|l,this.Gl=0|f,this.Hh=0|p,this.Hl=0|b}process(t,e){for(let i=0;i<16;i++,e+=4)Pt[i]=t.getUint32(e),Ot[i]=t.getUint32(e+=4);for(let t=16;t<80;t++){const e=0|Pt[t-15],i=0|Ot[t-15],r=nt(e,i,1)^nt(e,i,8)^rt(e,0,7),s=at(e,i,1)^at(e,i,8)^st(e,i,7),n=0|Pt[t-2],a=0|Ot[t-2],o=nt(n,a,19)^ot(n,a,61)^rt(n,0,6),c=at(n,a,19)^ct(n,a,61)^st(n,a,6),h=lt(s,c,Ot[t-7],Ot[t-16]),d=ft(h,r,o,Pt[t-7],Pt[t-16]);Pt[t]=0|d,Ot[t]=0|h}let{Ah:i,Al:r,Bh:s,Bl:n,Ch:a,Cl:o,Dh:c,Dl:h,Eh:d,El:u,Fh:l,Fl:f,Gh:p,Gl:b,Hh:g,Hl:y}=this;for(let t=0;t<80;t++){const e=nt(d,u,14)^nt(d,u,18)^ot(d,u,41),m=at(d,u,14)^at(d,u,18)^ct(d,u,41),w=d&l^~d&p,I=pt(y,m,u&f^~u&b,_t[t],Ot[t]),v=bt(I,g,e,w,Et[t],Pt[t]),k=0|I,S=nt(i,r,28)^ot(i,r,34)^ot(i,r,39),E=at(i,r,28)^ct(i,r,34)^ct(i,r,39),_=i&s^i&a^s&a,P=r&n^r&o^n&o;g=0|p,y=0|b,p=0|l,b=0|f,l=0|d,f=0|u,({h:d,l:u}=ht(0|c,0|h,0|v,0|k)),c=0|a,h=0|o,a=0|s,o=0|n,s=0|i,n=0|r;const O=dt(E,P,k);i=ut(O,S,_,v),r=0|O}({h:i,l:r}=ht(i,r,this.Ah,this.Al)),({h:s,l:n}=ht(s,n,this.Bh,this.Bl)),({h:a,l:o}=ht(a,o,this.Ch,this.Cl)),({h:c,l:h}=ht(c,h,this.Dh,this.Dl)),({h:d,l:u}=ht(d,u,this.Eh,this.El)),({h:l,l:f}=ht(l,f,this.Fh,this.Fl)),({h:p,l:b}=ht(p,b,this.Gh,this.Gl)),({h:g,l:y}=ht(g,y,this.Hh,this.Hl)),this.set(i,r,s,n,a,o,c,h,d,u,l,f,p,b,g,y)}roundClean(){X(Pt,Ot)}destroy(){X(this.buffer),this.set(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)}}const xt=Q((()=>new Nt));class At extends Z{oHash;iHash;blockLen;outputLen;finished=!1;destroyed=!1;constructor(t,e){super(),function(t){if("function"!=typeof t||"function"!=typeof t.create)throw new Error("Hash should be wrapped by utils.createHasher");z(t.outputLen),z(t.blockLen)}(t);const i=Y(e);if(this.iHash=t.create(),"function"!=typeof this.iHash.update)throw new Error("Expected instance of class which extends utils.Hash");this.blockLen=this.iHash.blockLen,this.outputLen=this.iHash.outputLen;const r=this.blockLen,s=new Uint8Array(r);s.set(i.length>r?t.create().update(i).digest():i);for(let t=0;t<s.length;t++)s[t]^=54;this.iHash.update(s),this.oHash=t.create();for(let t=0;t<s.length;t++)s[t]^=106;this.oHash.update(s),X(s)}update(t){return G(this),this.iHash.update(t),this}digestInto(t){G(this),W(t,this.outputLen),this.finished=!0,this.iHash.digestInto(t),this.oHash.update(t),this.oHash.digestInto(t),this.destroy()}digest(){const t=new Uint8Array(this.oHash.outputLen);return this.digestInto(t),t}_cloneInto(t){t||=Object.create(Object.getPrototypeOf(this),{});const{oHash:e,iHash:i,finished:r,destroyed:s,blockLen:n,outputLen:a}=this;return t.finished=r,t.destroyed=s,t.blockLen=n,t.outputLen=a,t.oHash=e._cloneInto(t.oHash??void 0),t.iHash=i._cloneInto(t.iHash??void 0),t}clone(){return this._cloneInto()}destroy(){this.destroyed=!0,this.oHash.destroy(),this.iHash.destroy()}}const Tt=t=>{let e="";for(const r of t)e+=(i=r.toString(16)).length%2==1?"0"+i:i;var i;return e},Ct=(t,e)=>{if(Array.isArray(t))return t.slice();if(void 0===t)return[];if("string"!=typeof t)return Array.from(t,(t=>0|t));switch(e){case"hex":return Rt(t);case"base64":return Bt(t);default:return function(t){const e=[];for(let i=0;i<t.length;i++){const r=t.codePointAt(i);if(void 0===r)throw new Error(`Index out of range: ${i}`);let s=r;s>65535?i++:s>=55296&&s<=57343&&(s=65533),s<=127?e.push(s):s<=2047?e.push(192|s>>6,128|63&s):s<=65535?e.push(224|s>>12,128|s>>6&63,128|63&s):e.push(240|s>>18,128|s>>12&63,128|s>>6&63,128|63&s)}return e}(t)}},Rt=t=>{(t=t.replace(/[^a-z0-9]+/gi,"")).length%2!=0&&(t="0"+t);const e=[];for(let i=0;i<t.l