UNPKG

@bsv/message-box-client

Version:

A client for P2P messaging and payments

1 lines 328 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:()=>Vr,PeerPayClient:()=>Kr,STANDARD_PAYMENT_MESSAGEBOX:()=>Hr});var i={};t.r(i),t.d(i,{Decoder:()=>Or,Encoder:()=>Er,PacketType:()=>_r,protocol:()=>Sr});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 n=t.length>0?t.length:1;for(let e=n-1;e>=0;e--){const n=void 0===t[e]?0:t[e];i=i<<r.WORD_SIZE_BIGINT|BigInt(n&Number(r.WORD_MASK))}this._magnitude=i,this._sign=e,this._nominalWordLength=n,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,n=i;if("le"!==e&&"be"!==e||(n=e,r=10),"number"!=typeof t)if(Array.isArray(t))this.initArray(t,n);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,s=0;e.startsWith("-")?(i++,s=1):e.startsWith("+")&&i++;const a=e.substring(i);if(0===a.length)return this._initializeState(0n,1===s&&e.startsWith("-")?1:0),void this.normSign();if(16===r){let t;if("le"===n){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),n=parseInt(r,16);if(isNaN(n))throw new Error("Invalid character in "+e);t.push(n)}return this.initArray(t,"le"),this._sign=s,void this.normSign()}try{t=BigInt("0x"+a)}catch(t){throw new Error("Invalid character in "+a)}this._initializeState(t,s),this.normSign()}else try{if(this._parseBaseString(a,r),this._sign=s,this.normSign(),"le"===n){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,n)}_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 n=BigInt(e);for(;r>0n;)i="0123456789abcdefghijklmnopqrstuvwxyz"[Number(r%n)]+i,r/=n;return i}_parseBaseString(t,e){if(0===t.length)return this._magnitude=0n,void this._finishInitialization();this._magnitude=0n;const i=BigInt(e);let n=r.groupSizes[e],s=BigInt(r.groupBases[e]);0!==n&&0n!==s||(n=Math.floor(Math.log(67108863)/Math.log(e)),0===n&&(n=1),s=i**BigInt(n));let a=0;const o=t.length;let c=o%n;if(0===c&&o>0&&(c=n),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+n),r=BigInt(this._parseBaseWord(i,e));this._magnitude=this._magnitude*s+r,a+=n}this._finishInitialization()}_parseBaseWord(t,e){let i=0;for(let r=0;r<t.length;r++){const n=t.charCodeAt(r);let s;if(n>=48&&n<=57)s=n-48;else if(n>=65&&n<=90)s=n-65+10;else{if(!(n>=97&&n<=122))throw new Error("Invalid character: "+t[r]);s=n-97+10}if(s>=e)throw new Error("Invalid character");i=i*e+s}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],n=BigInt(r.groupBases[t]);0!==i&&0n!==n||(i=Math.floor(Math.log(Number.MAX_SAFE_INTEGER)/Math.log(t)),0===i&&(i=1),n=BigInt(t)**BigInt(i));let s="",a=this._magnitude;for(;a>0n;){const e=a%n;a/=n;const o=this._bigIntToStringInBase(e,t);if(a>0n){const t=i-o.length;s=t>0&&t<r.zeros.length?r.zeros[t]+o+s:t>0?"0".repeat(t)+o+s:o+s}else s=o+s}if(e>0)for(;s.length<e;)s="0"+s;return(1===this._sign?"-":"")+s}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 n=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+=n}}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 n=new Array(r).fill(0);return 0n===this._magnitude&&r>0?n:0n===this._magnitude&&0===r?[]:(this.toArrayLikeGeneric(n,"le"===t),n)}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 n=new r(0n);return n._initializeState(i,0),n}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),n=new r(0n);return n._setValueFromSigned(t),n}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 n=this._nominalWordLength;return r&&(n=Math.max(this.length,t.length)),this._magnitude=i,this._finishInitialization(),r&&(this._nominalWordLength=Math.max(this._nominalWordLength,n)),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 n=Math.floor(t/r.wordSize)+1;return this._nominalWordLength=Math.max(this._nominalWordLength,n),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(),n=t.clone(),s=i.isNeg(),a=n.isOdd();for(s&&i.ineg();!n.isZero();)n.isOdd()&&e.imul(i),i.isqr(),n.iushrn(1);return s&&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 n=0===t?1:Math.max(1,Math.ceil(t/r.wordSize));return this._nominalWordLength=n,this._finishInitialization(),this._nominalWordLength=Math.max(this._nominalWordLength,n),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 n=this._getSignedValue(),s=t._getSignedValue();let a=null,o=null;"mod"!==e&&(a=n/s),"div"!==e&&(o=n%s,!0===i&&o<0n&&(o+=s<0n?-s:s));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 n=e/i;const s=e%i;if(0n===s){const t=new r(0n);return t._setValueFromSigned(n),t}2n*(s<0n?-s:s)>=(i<0n?-i:i)&&(e>0n&&i>0n||e<0n&&i<0n?n+=1n:n-=1n);const a=new r(0n);return a._setValueFromSigned(n),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,n=1n,s=0n,a=0n,o=1n;for(;0n!==i;){const t=e/i;let r=i;i=e%i,e=r,r=s,s=n-t*s,n=r,r=o,o=a-t*o,a=r}const c=new r(0n);c._setValueFromSigned(n);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 n=new r(0n);return n._initializeState(e,0),n}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,n="";if("little"===e){const e=t.length-1;let r=t[e];128&r&&(i=1,r&=127),n+=(r<16?"0":"")+r.toString(16);for(let i=e-1;i>=0;i--){const e=t[i];n+=(e<16?"0":"")+e.toString(16)}}else{let e=t[0];128&e&&(i=1,e&=127),n+=(e<16?"0":"")+e.toString(16);for(let e=1;e<t.length;e++){const i=t[e];n+=(i<16?"0":"")+i.toString(16)}}const s=""===n?0n:BigInt("0x"+n),a=new r(0n);return a._initializeState(s,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,n=8388607&t,s=!!(8388608&t);if(e&&s)throw new Error("negative bit set");if(0===i&&0===n){if(s&&e)throw new Error("negative bit set for zero value");return new r(0n)}const a=new r(n);return i<=3?a.iushrn(8*(3-i)):a.iushln(8*(i-3)),s&&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 n,s=i.length;if(0!==s||e.isZero()||(i=[0],s=1),e.isZero()&&(s=0,i=[]),0===s)n=0;else if(s<=3){n=0;for(let t=0;t<s;t++)n=n<<8|i[t]}else n=i[0]<<16|i[1]<<8|i[2];8388608&n&&s<=255&&(n>>>=8,s++);let a=s<<24|n;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,n=1n,s=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&n)&&(n+=a),n>>=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&s)&&(s+=a),s>>=1n}e>=i?(e-=i,n-=s):(i-=e,s-=n)}let o;if(1n===e)o=n;else if(1n===i)o=s;else if(0n===e&&1n===i)o=s;else{if(0n!==i||1n!==e)throw new Error("_invmp: GCD is not 1, inverse does not exist. aVal="+e+", bVal="+i);o=n}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 n{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 s extends n{constructor(){super("k256","ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff fffffffe fffffc2f")}split(t,e){const i=4194303,r=t.words,n=t.length,s=Math.min(n,9),a=new Array(s+(n>9?1:0)).fill(0);for(let t=0;t<s;t++)a[t]=r[t];let o=s;if(n<=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,n-9)).fill(0);let u=0;for(let t=10;t<n;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,n=new Array(r).fill(0);for(let t=0;t<i;t++)n[t]=e[t];let s=0;for(let t=0;t<r;t++){const e=0|n[t];s+=977*e,n[t]=67108863&s,s=64*e+(s/67108864|0)}return t.words=n,t}}class a{prime;m;constructor(t){if("k256"===t){const t=new s;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 n=0;for(;!i.isZero()&&0===i.andln(1);)n++,i.iushrn(1);this.assert(!i.isZero());const s=new r(1).toRed(this),a=s.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=n;for(;0!==l.cmp(s);){let t=l,e=0;for(;0!==t.cmp(s);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 n=2;for(;n<i.length;n++)i[n]=this.mul(i[n-1],t);let s=i[0],a=0,o=0,c=e.bitLength()%26;for(0===c&&(c=26),n=e.length-1;n>=0;n--){const t=e.words[n];for(let e=c-1;e>=0;e--){const r=t>>e&1;s!==i[0]&&(s=this.sqr(s)),0!==r||0!==a?(a<<=1,a|=r,o++,(4===o||0===n&&0===e)&&(s=this.mul(s,i[a]),o=0,a=0)):o=0}c=26}return s}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),n=i.isub(r).iushrn(this.shift);let s=n;return n.cmp(this.m)>=0?s=n.isub(this.m):n.cmpn(0)<0&&(s=n.iadd(this.m)),s.forceRed(this)}mul(t,e){if(t.isZero()||e.isZero())return new r(0).forceRed(this);const i=t.mul(e),n=i.maskn(this.shift).mul(this.minv).imaskn(this.shift).mul(this.m),s=i.isub(n).iushrn(this.shift);let a=s;return s.cmp(this.m)>=0?a=s.isub(this.m):s.cmpn(0)<0&&(a=s.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 It,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 ot(null,null);const t=this.z.redInvm(),e=t.redSqr(),i=this.x.redMul(e),r=this.y.redMul(e).redMul(t);return new ot(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),n=t.x.redMul(i),s=this.y.redMul(e.redMul(t.z)),a=t.y.redMul(i.redMul(this.z)),o=r.redSub(n),c=s.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(s.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),n=this.y,s=t.y.redMul(e).redMul(this.z),a=i.redSub(r),o=n.redSub(s);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(n.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(),n=this.y.redSqr(),s=n.redSqr();let a=this.x.redAdd(n).redSqr().redISub(r).redISub(s);a=a.redIAdd(a);const o=r.redAdd(r).redIAdd(r),c=o.redSqr().redISub(a).redISub(a);let h=s.redIAdd(s);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(),n=this.y.redSqr(),s=n.redSqr();let a=this.x.redAdd(n).redSqr().redISub(r).redISub(s);a=a.redIAdd(a);const o=r.redAdd(r).redIAdd(r),c=o.redSqr();let h=s.redIAdd(s);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),n=i.redMul(t.z);return 0===this.y.redMul(n).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 n=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(n),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 n=i-0;d(n%4==0);const s=new Array(n/4);for(let e=0,i=0;e<s.length;e++,i+=4){let n;n="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],s[e]=n>>>0}return s}(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 n,s;for(r[0]=128,n=1;n<i;n++)r[n]=0;if(t<<=3,"big"===this.endian){for(s=8;s<this.padLength;s++)r[n++]=0;r[n++]=0,r[n++]=0,r[n++]=0,r[n++]=0,r[n++]=t>>>24&255,r[n++]=t>>>16&255,r[n++]=t>>>8&255,r[n++]=255&t}else for(r[n++]=255&t,r[n++]=t>>>8&255,r[n++]=t>>>16&255,r[n++]=t>>>24&255,r[n++]=0,r[n++]=0,r[n++]=0,r[n++]=0,s=8;s<this.padLength;s++)r[n++]=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 n=t.charCodeAt(r);n<128?i[e++]=n:n<2048?(i[e++]=n>>6|192,i[e++]=63&n|128):l(t,r)?(n=65536+((1023&n)<<10)+(1023&t.charCodeAt(++r)),i[e++]=n>>18|240,i[e++]=n>>12&63|128,i[e++]=n>>6&63|128,i[e++]=63&n|128):(i[e++]=n>>12|224,i[e++]=n>>6&63|128,i[e++]=63&n|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 n=0;n<t.length;n++){let s=t[n];"little"===e&&(s=p(s)),i+=7===(r=s.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,e){const i=new Array(4*t.length);for(let r=0,n=0;r<t.length;r++,n+=4){const s=t[r];"big"===e?(i[n]=s>>>24,i[n+1]=s>>>16&255,i[n+2]=s>>>8&255,i[n+3]=255&s):(i[n+3]=s>>>24,i[n+2]=s>>>16&255,i[n+1]=s>>>8&255,i[n]=255&s)}return i}function y(t,e){return t>>>e|t<<32-e}function m(t,e){return t<<e|t>>>32-e}function w(t,e){return t+e>>>0}function I(t,e,i){return t+e+i>>>0}function v(t,e,i,r){return t+e+i+r>>>0}function k(t,e,i,r,n){return t+e+i+r+n>>>0}function S(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?E(e,i,r):void 0}function _(t,e,i){return t&e^~t&i}function E(t,e,i){return t&e^t&i^e&i}function P(t){return y(t,2)^y(t,13)^y(t,22)}function O(t){return y(t,6)^y(t,11)^y(t,25)}function N(t){return y(t,7)^y(t,18)^t>>>3}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],x=[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],C=[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 R(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 F(t){return t<=15?0:t<=31?1518500249:t<=47?1859775393:t<=63?2400959708:2840853838}function B(t){return t<=15?1352829926:t<=31?1548603684:t<=47?1836072691:t<=63?2053994217:0}class L 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],n=this.h[1],s=this.h[2],a=this.h[3],o=this.h[4],c=r,h=n,d=s,u=a,l=o;for(let f=0;f<80;f++)i=w(m(v(r,R(f,n,s,a),t[A[f]+e],F(f)),x[f]),o),r=o,o=a,a=m(s,10),s=n,n=i,i=w(m(v(c,R(79-f,h,d,u),t[T[f]+e],B(f)),C[f]),l),c=l,l=u,u=m(d,10),d=h,h=i;i=I(this.h[1],s,u),this.h[1]=I(this.h[2],a,l),this.h[2]=I(this.h[3],o,c),this.h[3]=I(this.h[4],r,h),this.h[4]=I(this.h[0],n,d),this.h[0]=i}_digest(){return g(this.h,"little")}_digestHex(){return b(this.h,"little")}}class D extends u{h;W;k;constructor(){super(512,256,192,64),this.h=[1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225],this.k=[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],this.W=new Array(64)}_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]=v(y(n=i[r-2],17)^y(n,19)^n>>>10,i[r-7],N(i[r-15]),i[r-16]);var n;let s=this.h[0],a=this.h[1],o=this.h[2],c=this.h[3],h=this.h[4],u=this.h[5],l=this.h[6],f=this.h[7];for(d(this.k.length===i.length),r=0;r<i.length;r++){const t=k(f,O(h),_(h,u,l),this.k[r],i[r]),e=w(P(s),E(s,a,o));f=l,l=u,u=h,h=w(c,t),c=o,o=a,a=s,s=w(t,e)}this.h[0]=w(this.h[0],s),this.h[1]=w(this.h[1],a),this.h[2]=w(this.h[2],o),this.h[3]=w(this.h[3],c),this.h[4]=w(this.h[4],h),this.h[5]=w(this.h[5],u),this.h[6]=w(this.h[6],l),this.h[7]=w(this.h[7],f)}_digest(){return g(this.h,"big")}_digestHex(){return b(this.h,"big")}}class M 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]=m(i[r-3]^i[r-8]^i[r-14]^i[r-16],1);let n=this.h[0],s=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=k(m(n,5),S(t,s,a,o),c,i[r],this.k[t]);c=o,o=a,a=m(s,30),s=n,n=e}this.h[0]=w(this.h[0],n),this.h[1]=w(this.h[1],s),this.h[2]=w(this.h[2],a),this.h[3]=w(this.h[3],o),this.h[4]=w(this.h[4],c)}_digest(){return g(this.h,"big")}_digestHex(){return b(this.h,"big")}}class V{inner;outer;blockSize=64;outSize=32;constructor(t){let e;for((t=f(t,"hex")).length>this.blockSize&&(t=(new D).update(t).digest()),d(t.length<=this.blockSize),e=t.length;e<this.blockSize;e++)t.push(0);for(e=0;e<t.length;e++)t[e]^=54;for(this.inner=(new D).update(t),e=0;e<t.length;e++)t[e]^=106;this.outer=(new D).update(t)}update(t,e){return this.inner.update(t,e),this}digest(){return this.outer.update(this.inner.digest()),this.outer.digest()}digestHex(){return this.outer.update(this.inner.digest()),this.outer.digestHex()}}const q=(t,e)=>(new D).update(t,e).digest(),H=(t,e)=>{const i=(new D).update(t,e).digest();return(new D).update(i).digest()},K=(t,e)=>{const i=(new D).update(t,e).digest();return(new L).update(i).digest()},U=(t,e,i)=>new V(t).update(e,i).digest(),$=t=>{let e="";for(const r of t)e+=(i=r.toString(16)).length%2==1?"0"+i:i;var i;return e},W=(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 z(t);case"base64":return j(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 n=r;n>65535?i++:n>=55296&&n<=57343&&(n=65533),n<=127?e.push(n):n<=2047?e.push(192|n>>6,128|63&n):n<=65535?e.push(224|n>>12,128|n>>6&63,128|63&n):e.push(240|n>>18,128|n>>12&63,128|n>>6&63,128|63&n)}return e}(t)}},z=t=>{(t=t.replace(/[^a-z0-9]+/gi,"")).length%2!=0&&(t="0"+t);const e=[];for(let i=0;i<t.length;i+=2)e.push(parseInt(t[i]+t[i+1],16));return e},j=t=>{const e=[];let i=0,r=0;for(const n of t.replace(/=+$/,""))i=i<<6|"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".indexOf(n),r+=6,r>=8&&(r-=8,e.push(i>>r&255),i&=(1<<r)-1);return e},X=t=>{let e="",i=0;for(let r=0;r<t.length;r++){const n=t[r];if(i>0)i--;else if(n<=127)e+=String.fromCharCode(n);else if(n>=192&&n<=223){i=1;const s=(31&n)<<6|63&t[r+1];e+=String.fromCharCode(s)}else if(n>=224&&n<=239){i=2;const s=(15&n)<<12|(63&t[r+1])<<6|63&t[r+2];e+=String.fromCharCode(s)}else if(n>=240&&n<=247){i=3;const s=(7&n)<<18|(63&t[r+1])<<12|(63&t[r+2])<<6|63&t[r+3],a=55296+(s-65536>>10),o=56320+(s-65536&1023);e+=String.fromCharCode(a,o)}}return e},G=(t,e)=>{switch(e){case"hex":return $(t);case"utf8":return X(t);default:return t}};function Y(t){const e="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";let i,r="";for(i=0;i<t.length;i+=3){const n=t[i],s=i+1<t.length?t[i+1]:0,a=i+2<t.length?t[i+2]:0,o=(3&n)<<4|s>>4,c=(15&s)<<2|a>>6,h=63&a;r+=e.charAt(n>>2)+e.charAt(o),r+=i+1<t.length?e.charAt(c):"=",r+=i+2<t.length?e.charAt(h):"="}return r}const J="123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz",Z=t=>{if(""===t||"string"!=typeof t)throw new Error(`Expected base58 string but got “${t}”`);const e=t.match(/[IOl0]/gmu);if(null!==e)throw new Error(`Invalid base58 character “${e.join("")}”`);const i=t.match(/^1+/gmu),r=null!==i?i[0].length:0,n=(t.length-r)*(Math.log(58)/Math.log(256))+1>>>0,s=new Uint8Array([...new Uint8Array(r),...(t.match(/./gmu)??[]).map((t=>J.indexOf(t))).reduce(((t,e)=>t.map((t=>{const i=58*t+e;return e=i>>8,i}))),new Uint8Array(n)).reverse().filter((a=!1,t=>a=a||t))]);var a;return[...s]},Q=t=>{const e=Array(256).fill(-1);for(let t=0;t<58;++t)e[J.charCodeAt(t)]=t;const i=[];for(const r of t){let t=r;for(let r=0;r<i.length;++r){const n=(e[i[r]]<<8)+t;i[r]=J.charCodeAt(n%58),t=n/58|0}for(;0!==t;)i.push(J.charCodeAt(t%58)),t=t/58|0}for(const e of t){if(0!==e)break;i.push("1".charCodeAt(0))}return i.reverse(),String.fromCharCode(...i)},tt=(t,e=[0])=>{let i=H([...e,...t]);return i=[...e,...t,...i.slice(0,4)],Q(i)},et=(t,e,i=1)=>{const r=Z(t);let n=r.slice(0,i),s=r.slice(i,-4),a=[...n,...s];return a=H(a),r.slice(-4).forEach(((t,e)=>{if(t!==a[e])throw new Error("Invalid checksum")})),"hex"===e&&(n=$(n),s=$(s)),{prefix:n,data:s}};class it{bufs;length;constructor(t){this.bufs=void 0!==t?t:[],this.length=0;for(const t of this.bufs)this.length+=t.length}getLength(){return this.length}toArray(){const t=this.length,e=new Array(t);let i=0;for(const t of this.bufs)for(const r of t)e[i++]=r;return e}write(t){return this.bufs.push(t),this.length+=t.length,this}writeReverse(t){const e=new Array(t.length);for(let i=0;i<e.length;i++)e[i]=t[t.length-1-i];return this.bufs.push(e),this.length+=e.length,this}writeUInt8(t){const e=new Array(1);return e[0]=t,this.write(e),this}writeInt8(t){const e=new Array(1);return e[0]=255&t,this.write(e),this}writeUInt16BE(t){const e=[t>>8&255,255&t];return this.bufs.push(e),this.length+=2,this}writeInt16BE(t){return this.writeUInt16BE(65535&t)}writeUInt16LE(t){const e=[255&t,t>>8&255];return this.bufs.push(e),this.length+=2,this}writeInt16LE(t){return this.writeUInt16LE(65535&t)}writeUInt32BE(t){const e=[t>>24&255,t>>16&255,t>>8&255,255&t];return this.bufs.push(e),this.length+=4,this}writeInt32BE(t){return this.writeUInt32BE(t>>>0)}writeUInt32LE(t){const e=[255&t,t>>8&255,t>>16&255,t>>24&255];return this.bufs.push(e),this.length+=4,this}writeInt32LE(t){return this.writeUInt32LE(t>>>0)}writeUInt64BEBn(t){const e=t.toArray("be",8);return this.write(e),this}writeUInt64LEBn(t){const e=t.toArray("be",8);return this.writeReverse(e),this}writeUInt64LE(t){const e=new r(t).toArray("be",8);return this.writeReverse(e),this}writeVarIntNum(t){const e=it.varIntNum(t);return this.write(e),this}writeVarIntBn(t){const e=it.varIntBn(t);return this.write(e),this}static varIntNum(t){let e;if(t<0)return this.varIntBn(new r(t));if(t<253)e=[t];else if(t<65536)e=[253,255&t,t>>8&255];else if(t<4294967296)e=[254,255&t,t>>8&255,t>>16&255,t>>24&255];else{const i=4294967295&t,r=4294967295&Math.floor(t/4294967296);e=[255,255&i,i>>8&255,i>>16&255,i>>24&255,255&r,r>>8&255,r>>16&255,r>>24&255]}return e}static varIntBn(t){let e;if(t.isNeg()&&(t=t.add(at)),t.ltn(253))e=[t.toNumber()];else if(t.ltn(65536)){const i=t.toNumber();e=[253,255&i,i>>8&255]}else if(t.lt(new r(4294967296))){const i=t.toNumber();e=[254,255&i,i>>8&255,i>>16&255,i>>24&255]}else{const i=new it;i.writeUInt8(255),i.writeUInt64LEBn(t),e=i.toArray()}return e}}class rt{bin;pos;length;constructor(t=[],e=0){this.bin=t,this.pos=e,this.length=t.length}eof(){return this.pos>=this.length}read(t=this.length){const e=this.pos,i=this.pos+t;return this.pos=i,this.bin.slice(e,i)}readReverse(t=this.length){const e=new Array(t);for(let i=0;i<t;i++)e[i]=this.bin[this.pos+t-1-i];return this.pos+=t,e}readUInt8(){const t=this.bin[this.pos];return this.pos+=1,t}readInt8(){const t=this.bin[this.pos];return this.pos+=1,128&t?t-256:t}readUInt16BE(){const t=this.bin[this.pos]<<8|this.bin[this.pos+1];return this.pos+=2,t}readInt16BE(){const t=this.readUInt16BE();return 32768&t?t-65536:t}readUInt16LE(){const t=this.bin[this.pos]|this.bin[this.pos+1]<<8;return this.pos+=2,t}readInt16LE(){const t=this.readUInt16LE();return 32768&t?t-65536:t}readUInt32BE(){const t=16777216*this.bin[this.pos]+(this.bin[this.pos+1]<<16|this.bin[this.pos+2]<<8|this.bin[this.pos+3]);return this.pos+=4,t}readInt32BE(){const t=this.readUInt32BE();return 2147483648&t?t-4294967296:t}readUInt32LE(){const t=(this.bin[this.pos]|this.bin[this.pos+1]<<8|this.bin[this.pos+2]<<16|this.bin[this.pos+3]<<24)>>>0;return this.pos+=4,t}readInt32LE(){const t=this.readUInt32LE();return 2147483648&t?t-4294967296:t}readUInt64BEBn(){const t=this.bin.slice(this.pos,this.pos+8),e=new r(t);return this.pos=this.pos+8,e}readUInt64LEBn(){const t=this.readReverse(8);return new r(t)}readInt64LEBn(){const t=this.readReverse(8);let e=new r(t);return e.gte(st)&&(e=e.sub(at)),e}readVarIntNum(t=!0){const e=this.readUInt8();let i;switch(e){case 253:return this.readUInt16LE();case 254:return this.readUInt32LE();case 255:if(i=t?this.readInt64LEBn():this.readUInt64LEBn(),i.lte(new r(2).pow(new r(53))))return i.toNumber();throw new Error("number too large to retain precision - use readVarIntBn");default:return e}}readVarInt(){switch(this.bin[this.pos]){case 253:return this.read(3);case 254:return this.read(5);case 255:return this.read(9);default:return this.read(1)}}readVarIntBn(){const t=this.readUInt8();switch(t){case 253:return new r(this.readUInt16LE());case 254:return new r(this.readUInt32LE());case 255:return this.readUInt64LEBn();default:return new r(t)}}}const nt=t=>{if(0===t.length)return t;const e=t[t.length-1];if(127&e)return t;if(1===t.length)return[];if(128&t[t.length-2])return t;for(let i=t.length-1;i>0;i--)if(0!==t[i-1])return 128&t[i-1]?(t[i]=e,t.slice(0,i+1)):(t[i-1]|=e,t.slice(0,i));return[]},st=new r(2).pow(new r(63)),at=new r(2).pow(new r(64));class ot extends c{static red=new a("k256");static a=new r(0).toRed(ot.red);static b=new r(7).toRed(ot.red);static zero=new r(0).toRed(ot.red);x;y;inf;static fromDER(t){if((4===t[0]||6===t[0]||7===t[0])&&t.length-1==64){if(6===t[0]){if(t[t.length-1]%2!=0)throw new Error("Point string value is wrong length")}else if(7===t[0]&&t[t.length-1]%2!=1)throw new Error("Point string value is wrong length");return new ot(t.slice(1,33),t.slice(33,65))}if((2===t[0]||3===t[0])&&t.length-1==32)return ot.fromX(t.slice(1,33),3===t[0]);throw new Error("Unknown point format")}static fromString(t){const e=W(t,"hex");return ot.fromDER(e)}static redSqrtOptimized(t){const e=ot.red.m.addn(1).iushrn(2);return t.redPow(e)}static fromX(t,e){function i(t,e){return(t%e+e)%e}function n(t,e,r){let n=BigInt(1);for(t=i(t,r);e>BigInt(0);)(e&BigInt(1))===BigInt(1)&&(n=i(n*t,r)),e>>=BigInt(1),t=i(t*t,r);return n}const s=BigInt("0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F"),a=BigInt(7);let o;if(t instanceof r)o=BigInt("0x"+t.toString(16));else if("string"==typeof t)o=BigInt("0x"+t);else if(Array.isArray(t))o=BigInt("0x"+$(t).padStart(64,"0"));else{if("number"!=typeof t)throw new Error("Invalid x-coordinate type");o=BigInt(t)}o=i(o,s);let c=function(t,e){const r=n(t,e+BigInt(1)>>BigInt(2),e);return i(r*r,e)===i(t,e)?r:null}(i(n(o,BigInt(3),s)+a,s),s);if(null===c)throw new Error("Invalid point");const h=c%BigInt(2)===BigInt(1);(e&&!h||!e&&h)&&(c=s-c);const d=new r(o.toString(16),16),u=new r(c.toString(16),16);return new ot(d,u)}static fromJSON(t,e){"string"==typeof t&&(t=JSON.parse(t));const i=new ot(t[0],t[1],e);if("object"!=typeof t[2])return i;const r=t=>new ot(t[0],t[1],e),n=t[2];return i.precomputed={beta:null,doubles:"object"==typeof n.doubles&&null!==n.doubles?{step:n.doubles.step,points:[i].concat(n.doubles.points.map(r))}:void 0,naf:"object"==typeof n.naf&&null!==n.naf?{wnd:n.naf.wnd,points:[i].concat(n.naf.points.map(r))}:void 0},i}constructor(t,e,i=!0){super("affine"),this.precomputed=null,null===t&&null===e?(this.x=null,this.y=null,this.inf=!0):(r.isBN(t)||(t=new r(t,16)),this.x=t,r.isBN(e)||(e=new r(e,16)),this.y=e,i&&(this.x.forceRed(this.curve.red),this.y.forceRed(this.curve.red)),null===this.x.red&