3 lines
23 KiB
JavaScript
3 lines
23 KiB
JavaScript
import{Keccak as t,SHA3 as r}from"sha3";import e from"base-x";import{Buffer as n}from"safe-buffer";import i from"big-integer";import o from"bs58";import{RIPEMD160 as f}from"ripemd160-min";import{blake2b as u}from"blakejs/blake2b";var a="ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",s=function(t){for(var r=t.toString(),e="",n=0;n<r.length;n+=2)e+=String.fromCharCode(parseInt(r.substr(n,2),16));return e},h=["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"],c=function(t){for(var r="",e=0;e<t.length;e++){var n=t[e];r+=h[n>>>4],r+=h[15&n]}return r},l=function(t){for(var r=new ArrayBuffer(5*t.length/8),e=new Uint8Array(r),n=0;n<t.length/8;n++){for(var i=[0,0,0,0,0,0,0,0],o=0;o<8;++o)i[o]=a.indexOf(t[8*n+o]);e[5*n+0]=i[0]<<3|i[1]>>2,e[5*n+1]=(3&i[1])<<6|i[2]<<1|i[3]>>4,e[5*n+2]=(15&i[3])<<4|i[4]>>1,e[5*n+3]=(1&i[4])<<7|i[5]<<2|i[6]>>3,e[5*n+4]=(7&i[6])<<5|i[7]}return e},d=function(t){var r=[],e=Math.floor(t.length/5),n=t.length%5;if(0!=n){for(var i=0;i<5-n;i++)t+="\0";e+=1}for(var o=0;o<e;o++)r.push(a.charAt(t.charCodeAt(5*o)>>3)),r.push(a.charAt((7&t.charCodeAt(5*o))<<2|t.charCodeAt(5*o+1)>>6)),r.push(a.charAt((63&t.charCodeAt(5*o+1))>>1)),r.push(a.charAt((1&t.charCodeAt(5*o+1))<<4|t.charCodeAt(5*o+2)>>4)),r.push(a.charAt((15&t.charCodeAt(5*o+2))<<1|t.charCodeAt(5*o+3)>>7)),r.push(a.charAt((127&t.charCodeAt(5*o+3))>>2)),r.push(a.charAt((3&t.charCodeAt(5*o+3))<<3|t.charCodeAt(5*o+4)>>5)),r.push(a.charAt(31&t.charCodeAt(5*o+4)));var f=0;1==n?f=6:2==n?f=4:3==n?f=3:4==n&&(f=1);for(var u=0;u<f;u++)r.pop();for(var s=0;s<f;s++)r.push("=");return r.join("")},p=function(t){var r=t.toString().toUpperCase().replace(/-/g,"");if(!r||40!==r.length)return!1;var e=v(l(r));return y(Buffer.from(e.slice(0,42),"hex"))===e.slice(42)},v=function(t){for(var r="",e=0;e<t.length;e++){var n=Math.round(t[e]).toString(16);1===n.length&&(n="0"+n),r+=n}return r},y=function(r){return new t(256).update(r).digest("hex").toString().substr(0,8)};function g(t,r){this._block=n.alloc(t),this._finalSize=r,this._blockSize=t,this._len=0}g.prototype.update=function(t,r){"string"==typeof t&&(t=n.from(t,r=r||"utf8"));for(var e=this._block,i=this._blockSize,o=t.length,f=this._len,u=0;u<o;){for(var a=f%i,s=Math.min(o-u,i-a),h=0;h<s;h++)e[a+h]=t[u+h];u+=s,(f+=s)%i==0&&this._update(e)}return this._len+=o,this},g.prototype.digest=function(t){var r=this._len%this._blockSize;this._block[r]=128,this._block.fill(0,r+1),r>=this._finalSize&&(this._update(this._block),this._block.fill(0));var e=8*this._len;if(e<=4294967295)this._block.writeUInt32BE(e,this._blockSize-4);else{var n=(4294967295&e)>>>0;this._block.writeUInt32BE((e-n)/4294967296,this._blockSize-8),this._block.writeUInt32BE(n,this._blockSize-4)}this._update(this._block);var i=this._hash();return t?i.toString(t):i},g.prototype._update=function(){throw new Error("_update must be implemented by subclass")};var m,w,b=[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],_=new Array(64);function B(){this.init(),this._w=_,g.call(this,64,56)}function E(t,r,e){return e^t&(r^e)}function I(t,r,e){return t&r|e&(t|r)}function x(t){return(t>>>2|t<<30)^(t>>>13|t<<19)^(t>>>22|t<<10)}function S(t){return(t>>>6|t<<26)^(t>>>11|t<<21)^(t>>>25|t<<7)}function A(t){return(t>>>7|t<<25)^(t>>>18|t<<14)^t>>>3}function z(t){return void 0!==t.length}m=B,(w=g)&&(m.super_=w,m.prototype=Object.create(w.prototype,{constructor:{value:m,enumerable:!1,writable:!0,configurable:!0}})),B.prototype.init=function(){return this._a=1779033703,this._b=3144134277,this._c=1013904242,this._d=2773480762,this._e=1359893119,this._f=2600822924,this._g=528734635,this._h=1541459225,this},B.prototype._update=function(t){for(var r,e=this._w,n=0|this._a,i=0|this._b,o=0|this._c,f=0|this._d,u=0|this._e,a=0|this._f,s=0|this._g,h=0|this._h,c=0;c<16;++c)e[c]=t.readInt32BE(4*c);for(;c<64;++c)e[c]=0|(((r=e[c-2])>>>17|r<<15)^(r>>>19|r<<13)^r>>>10)+e[c-7]+A(e[c-15])+e[c-16];for(var l=0;l<64;++l){var d=h+S(u)+E(u,a,s)+b[l]+e[l]|0,p=x(n)+I(n,i,o)|0;h=s,s=a,a=u,u=f+d|0,f=o,o=i,i=n,n=d+p|0}this._a=n+this._a|0,this._b=i+this._b|0,this._c=o+this._c|0,this._d=f+this._d|0,this._e=u+this._e|0,this._f=a+this._f|0,this._g=s+this._g|0,this._h=h+this._h|0},B.prototype._hash=function(){var t=n.allocUnsafe(32);return t.writeInt32BE(this._a,0),t.writeInt32BE(this._b,4),t.writeInt32BE(this._c,8),t.writeInt32BE(this._d,12),t.writeInt32BE(this._e,16),t.writeInt32BE(this._f,20),t.writeInt32BE(this._g,24),t.writeInt32BE(this._h,28),t};var k=new(function(){function t(t){this.sha256=t.sha256,this.alphabet=t.alphabet,this.codec=e(this.alphabet),this.base=this.alphabet.length}var r=t.prototype;return r.encodeChecked=function(t){var r=this.sha256(this.sha256(t)).slice(0,4);return this.encodeRaw(Buffer.from(function(){var t=[];return[].slice.call(arguments).forEach(function(r){if(z(r))for(var e=0;e<r.length;e++)t.push(r[e]);else t.push(r)}),t}(t,r)))},r.encodeRaw=function(t){return this.codec.encode(t)},r.decodeChecked=function(t){var r=this.decodeRaw(t);if(r.length<5)throw new Error("invalid_input_size: decoded data must have length >= 5");if(!this.verifyCheckSum(r))throw new Error("checksum_invalid");return r.slice(0,-4)},r.decodeRaw=function(t){return this.codec.decode(t)},r.verifyCheckSum=function(t){return function(t,r){if(t.length!==r.length)return!1;for(var e=0;e<t.length;e++)if(t[e]!==r[e])return!1;return!0}(this.sha256(this.sha256(t.slice(0,-4))).slice(0,4),t.slice(-4))},t}())({sha256:function(t){return(new B).update(Buffer.from(t)).digest()},alphabet:"rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz"}),O=function(t){return"0x"===t.slice(0,2)?t.slice(2):t},C=function(r,e){if(void 0===e&&(e=null),"string"!=typeof r)throw new Error("stripHexPrefix param must be type 'string', is currently type "+typeof r+".");for(var n,i=O(r).toLowerCase(),o=(n=(null!=e?e.toString()+"0x":"")+i,new t(256).update(n).digest()).toString("hex"),f="0x",u=0;u<i.length;u++)f+=parseInt(o[u],16)>=8?i[u].toUpperCase():i[u];return f};function U(t,r){return function(t){return/^0x[0-9a-fA-F]{40}$/.test(t)}(t)&&C(t,r)===t}var P=function(t,r){if(!t)throw r},L=function(t,r){if(!Buffer.isBuffer(t))throw r},j=i.one,R=i.zero,q=i(3);function M(t,r){return t.and(j.shiftLeft(r)).notEquals(R)}function F(t){return i.fromArray(Array.prototype.slice.call(t,0),256)}function T(t,r){var e=t.toArray(256).value,n=r?Math.max(r-e.length,0):0;return Buffer.from([].concat(Array(n).fill(0),e))}function G(t,r,e,n){P(void 0!==n,"Missing Z coordinate"),this.curve=t,this.x=r,this.y=e,this.z=n,this._zInv=null,this.compressed=!0}Object.defineProperty(G.prototype,"zInv",{get:function(){return null===this._zInv&&(this._zInv=this.z.modInv(this.curve.p)),this._zInv}}),Object.defineProperty(G.prototype,"affineX",{get:function(){return this.x.multiply(this.zInv).mod(this.curve.p)}}),Object.defineProperty(G.prototype,"affineY",{get:function(){return this.y.multiply(this.zInv).mod(this.curve.p)}}),G.fromAffine=function(t,r,e){return new G(t,r,e,j)},G.prototype.equals=function(t){return t===this||(this.curve.isInfinity(this)?this.curve.isInfinity(t):this.curve.isInfinity(t)?this.curve.isInfinity(this):0===t.y.multiply(this.z).subtract(this.y.multiply(t.z)).mod(this.curve.p).compare(R)&&0===t.x.multiply(this.z).subtract(this.x.multiply(t.z)).mod(this.curve.p).compare(R))},G.prototype.negate=function(){var t=this.curve.p.subtract(this.y);return new G(this.curve,this.x,t,this.z)},G.prototype.add=function(t){if(this.curve.isInfinity(this))return t;if(this.curve.isInfinity(t))return this;var r=this.x,e=this.y,n=t.x,i=t.y.multiply(this.z).subtract(e.multiply(t.z)).mod(this.curve.p),o=n.multiply(this.z).subtract(r.multiply(t.z)).mod(this.curve.p);if(0===o.compare(R))return 0===i.compare(R)?this.twice():this.curve.infinity;var f=o.square(),u=f.multiply(o),a=r.multiply(f),s=i.square().multiply(this.z),h=s.subtract(a.shiftLeft(1)).multiply(t.z).subtract(u).multiply(o).mod(this.curve.p),c=a.multiply(q).multiply(i).subtract(e.multiply(u)).subtract(s.multiply(i)).multiply(t.z).add(i.multiply(u)).mod(this.curve.p),l=u.multiply(this.z).multiply(t.z).mod(this.curve.p);return new G(this.curve,h,c,l)},G.prototype.twice=function(){if(this.curve.isInfinity(this))return this;if(0===this.y.compare(R))return this.curve.infinity;var t=this.x,r=this.y,e=r.multiply(this.z).mod(this.curve.p),n=e.multiply(r).mod(this.curve.p),i=this.curve.a,o=t.square().multiply(q);0!==i.compare(R)&&(o=o.add(this.z.square().multiply(i)));var f=(o=o.mod(this.curve.p)).square().subtract(t.shiftLeft(3).multiply(n)).shiftLeft(1).multiply(e).mod(this.curve.p),u=o.multiply(q).multiply(t).subtract(n.shiftLeft(1)).shiftLeft(2).multiply(n).subtract(o.pow(3)).mod(this.curve.p),a=e.pow(3).shiftLeft(3).mod(this.curve.p);return new G(this.curve,f,u,a)},G.prototype.multiply=function(t){if(this.curve.isInfinity(this))return this;if(0===t.compare(R))return this.curve.infinity;for(var r=t,e=r.multiply(q),n=this.negate(),i=this,o=e.bitLength()-2;o>0;--o){var f=M(e,o),u=M(r,o);i=i.twice(),f!==u&&(i=i.add(f?this:n))}return i},G.prototype.multiplyTwo=function(t,r,e){for(var n=Math.max(t.bitLength(),e.bitLength())-1,i=this.curve.infinity,o=this.add(r);n>=0;){var f=M(t,n),u=M(e,n);i=i.twice(),f?i=i.add(u?o:this):u&&(i=i.add(r)),--n}return i},G.prototype.getEncoded=function(t){if(null==t&&(t=this.compressed),this.curve.isInfinity(this))return new Buffer("00","hex");var r,e=this.affineX,n=this.affineY,i=this.curve.pLength;return t?(r=new Buffer(1+i)).writeUInt8(n.isEven()?2:3,0):((r=new Buffer(1+i+i)).writeUInt8(4,0),T(n,i).copy(r,1+i)),T(e,i).copy(r,1),r},G.decodeFrom=function(t,r){var e,n=r.readUInt8(0),i=4!==n,o=Math.floor((t.p.bitLength()+7)/8),f=F(r.slice(1,1+o));if(i)P(r.length===o+1,"Invalid sequence length"),P(2===n||3===n,"Invalid sequence tag"),e=t.pointFromX(3===n,f);else{P(r.length===1+o+o,"Invalid sequence length");var u=F(r.slice(1+o));e=G.fromAffine(t,f,u)}return e.compressed=i,e},G.prototype.toString=function(){return this.curve.isInfinity(this)?"(INFINITY)":"("+this.affineX.toString()+","+this.affineY.toString()+")"};var H=i.zero,N=i.one;function X(t,r,e,n,i,o,f){this.p=t,this.a=r,this.b=e,this.G=G.fromAffine(this,n,i),this.n=o,this.h=f,this.infinity=new G(this,null,null,H),this.pOverFour=t.add(N).shiftRight(2),this.pLength=Math.floor((this.p.bitLength()+7)/8)}X.prototype.pointFromX=function(t,r){var e=r.pow(3).add(this.a.multiply(r)).add(this.b).mod(this.p).modPow(this.pOverFour,this.p),n=e;return e.isEven()^!t&&(n=this.p.subtract(n)),G.fromAffine(this,r,n)},X.prototype.isInfinity=function(t){return t===this.infinity||0===t.z.compare(H)&&0!==t.y.compare(H)},X.prototype.isOnCurve=function(t){if(this.isInfinity(t))return!0;var r=t.affineX,e=t.affineY,n=this.a,i=this.b,o=this.p;if(r.compare(H)<0||r.compareTo(o)>=0)return!1;if(e.compare(H)<0||e.compareTo(o)>=0)return!1;var f=e.square().mod(o),u=r.pow(3).add(n.multiply(r)).add(i).mod(o);return f.equals(u)},X.prototype.validate=function(t){P(!this.isInfinity(t),"Point is at infinity"),P(this.isOnCurve(t),"Point is not on the curve");var r=t.multiply(this.n);return P(this.isInfinity(r),"Point is not a scalar multiple of G"),!0};var Y={p:"fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f",a:"00",b:"07",n:"fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141",h:"01",Gx:"79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798",Gy:"483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8"};function K(t,e){return new r(256).update(t).digest(e)}function Q(t){return Buffer.from((new f).update(t).digest())}function Z(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null;if(L(t,"expecting keyBuffer<Buffer>"),"sha256x2"===r){var e=K(K(t)).slice(0,4);return o.encode(Buffer.concat([t,e]))}var n=[t];r&&n.push(Buffer.from(r));var i=Q(Buffer.concat(n)).slice(0,4);return o.encode(Buffer.concat([t,i]))}function D(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null;P(t,"private key expected");var e,n=new Buffer(o.decode(t)),i=n.slice(-4),f=n.slice(0,-4);if("sha256x2"===r)e=K(K(f)).slice(0,4);else{var u=[f];r&&u.push(Buffer.from(r)),e=Q(Buffer.concat(u)).slice(0,4)}if(i.toString("hex")!==e.toString("hex"))throw new Error("Invalid checksum, "+"".concat(i.toString("hex")," != ").concat(e.toString("hex")));return f}var V=function(t){var r=Y;if(!r)return null;var e=i(r.p,16),n=i(r.a,16),o=i(r.b,16),f=i(r.n,16),u=i(r.h,16);return new X(e,n,o,i(r.Gx,16),i(r.Gy,16),f,u)}();function J(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"EOS";if("string"==typeof t)return J.fromString(t,r);if(Buffer.isBuffer(t))return J.fromBuffer(t);if("object"==typeof t&&t.Q)return J(t.Q);function e(){var r=arguments.length>0&&void 0!==arguments[0]?arguments[0]:t.compressed;return t.getEncoded(r)}function n(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"EOS";return t+Z(e())}function i(){return e().toString("hex")}return{Q:t,toString:n,toBuffer:e,toHex:i}}J.isValid=function(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"EOS";try{return J(t,r),!0}catch(t){return!1}},J.fromBuffer=function(t){return J(G.decodeFrom(V,t))},J.fromString=function(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"EOS";try{return J.fromStringOrThrow(t,r)}catch(t){return null}},J.fromStringOrThrow=function(t){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"EOS",e=t.match(/^PUB_([A-Za-z0-9]+)_([A-Za-z0-9]+)$/);if(null===e){var n=new RegExp("^"+r);return n.test(t)&&(t=t.substring(r.length)),J.fromBuffer(D(t))}var i=e[1],o=e[2];return J.fromBuffer(D(o,i))},J.fromHex=function(t){return J.fromBuffer(new Buffer(t,"hex"))},J.fromStringHex=function(t){return J.fromString(new Buffer(t,"hex"))};var W=e("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"),$=W.encode,tt=W.decode,rt=function(t){return void 0===t},et={ed25519PublicKey:48,ed25519SecretSeed:144,preAuthTx:152,sha256Hash:184},nt=function(t,r){if("string"!=typeof r)throw new TypeError("encoded argument must be of type String");var e=tt(r),n=e[0],i=e.slice(0,-2),o=i.slice(1),f=e.slice(-2);if(r!==$(e))throw new Error("invalid encoded string");var u=et[t];if(rt(u))throw new Error(t+' is not a valid version byte name. expected one of "accountId" or "seed"');if(n!==u)throw new Error("invalid version byte. expected "+u+", got "+n);var a=ot(i);if(!ft(a,f))throw new Error("invalid checksum");return Buffer.from(o)},it=function(t,r){if(null===r||rt(r))throw new Error("cannot encode null data");var e=et[t];if(rt(e))throw new Error(t+' is not a valid version byte name. expected one of "ed25519PublicKey", "ed25519SecretSeed", "preAuthTx", "sha256Hash"');r=Buffer.from(r);var n=Buffer.from([e]),i=Buffer.concat([n,r]),o=ot(i),f=Buffer.concat([i,o]);return $(f)},ot=function(t){var r=Buffer.alloc(2);return r.writeUInt16LE(function(t,r){Buffer.isBuffer(t)||(t=new Buffer);for(var e=0,n=0;n<t.length;n++){var i=e>>>8&255;i^=255&t[n],e=e<<8&65535,e^=i^=i>>>4,e^=i=i<<5&65535,e^=i=i<<7&65535}return e}(t),0),r},ft=function(t,r){if(t.length!==r.length)return!1;if(0===t.length)return!0;for(var e=0;e<t.length;e+=1)if(t[e]!==r[e])return!1;return!0};function ut(t){return(ut=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function at(t,r){return(at=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,r){return t.__proto__=r,t})(t,r)}function st(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){})),!0}catch(t){return!1}}function ht(t,r,e){return(ht=st()?Reflect.construct.bind():function(t,r,e){var n=[null];n.push.apply(n,r);var i=new(Function.bind.apply(t,n));return e&&at(i,e.prototype),i}).apply(null,arguments)}function ct(t){var r="function"==typeof Map?new Map:void 0;return(ct=function(t){if(null===t||-1===Function.toString.call(t).indexOf("[native code]"))return t;if("function"!=typeof t)throw new TypeError("Super expression must either be null or a function");if(void 0!==r){if(r.has(t))return r.get(t);r.set(t,e)}function e(){return ht(t,arguments,ut(this).constructor)}return e.prototype=Object.create(t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),at(e,t)})(t)}var lt=[0,1,2,42,43,68,69],dt=function(t){for(var r=new Uint8Array(t.length),e=0;e<t.length;e++)r[e]=t.charCodeAt(e);return r}("SS58PRE"),pt=function(t){var r,e;function n(){return t.apply(this,arguments)||this}return e=t,(r=n).prototype=Object.create(e.prototype),r.prototype.constructor=r,at(r,e),n.prototype.toJSON=function(){return{_type:"AccountIndex",data:this+0}},n}(ct(Number));function vt(t,r){t.length||(t=[t]),r.length||(r=[r]);var e=new Uint8Array(t.length+r.length);return e.set(t),e.set(r,t.length),e}function yt(t,r,e,n,i){var f;if(void 0===r&&(r=42),void 0===e&&(e=null),void 0===n&&(n=null),-1===lt.indexOf(r))throw new Error("Unknown ss58 address type",r);if("number"==typeof t||t instanceof pt){var a=t<256?1:t<65536?2:t<1?4:8;if(-1===[1,2,4,8].indexOf(n=n||a))throw new Error("Invalid length");if(n=Math.max(a,n),e&&"number"!=typeof e)throw new Error("Invalid checksum length");switch(n){case 1:e=1;break;case 2:e=[1,2].indexOf(e)+1||1;break;case 4:e=[1,2,3,4].indexOf(e)+1||1;break;case 8:e=[1,2,3,4,5,6,7,8].indexOf(e)+1||1}f=function(t,r){var e=!1;t<0&&(t=-t-1,e=!0);for(var n=new Uint8Array(r),i=0;i<r;++i)n[i]=t%256,e&&(n[i]=255&~n[i]),t/=256;return n}(t,n)}else{if(!(t instanceof Uint8Array)||32!==t.length&&35!==t.length)throw new Error("Unknown item to encode as ss58. Passing back.",t);e=2,i=f=35===t.length?t.slice(1,33):t}var s=u(vt(dt,1&r?i:vt(r,f))),h=vt(vt(r,f),s.slice(0,e));return o.encode(Buffer.from(h))}function gt(t,r){var e;void 0===r&&(r=0);try{e=o.decode(t)}catch(t){return null}if(-1===lt.indexOf(e[0]))return null;if(e.length<3)return null;var n,i=e.length<=3?1:e.length<=5?2:e.length<=9?4:e.length<=17?8:32,f=e.length-1-i,a=e.slice(1,1+i);e.slice(1+e.length),32===i&&(n=a);var s=i<32?new pt(function(t){var r=0,e=1;return t.forEach(function(t){r+=t*e,e*=256}),r}(a)):new Buffer.from(a);if(e[0]%1&&!n&&!r)return null;for(var h=u(vt(dt,e[0]%1?n||r(s):e.slice(0,1+i))),c=0;c<f;++c)if(h[c]!==e[1+i+c])return null;return s}function mt(t){var r=new Error;this.name=r.name="ValidationError",this.message=r.message=t,this.stack=r.stack}function wt(t,r){if(null==r&&(r="Assertion failed."),!t)throw new mt(r)}mt.prototype=Object.create(Error.prototype);var bt=function(){function t(t,r){this.hi=0|t,this.lo=0|r}return t.prototype.ushrn32=function(t){var r=this.lo;return 0==(t&=63)||(t<32?(r>>>=t,r|=this.hi<<32-t):r=this.hi>>>t-32),r},t}(),_t=Buffer.allocUnsafe(105),Bt="qpzry9x8gf2tvdw0s3jn54khce6mua7l",Et=[-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,15,-1,10,17,21,20,26,30,7,5,-1,-1,-1,-1,-1,-1,-1,29,-1,24,13,25,9,8,23,-1,18,22,31,27,19,-1,1,0,3,16,11,28,12,14,6,4,2,-1,-1,-1,-1,-1,-1,29,-1,24,13,25,9,8,23,-1,18,22,31,27,19,-1,1,0,3,16,11,28,12,14,6,4,2,-1,-1,-1,-1,-1],It=new bt(7,4294967295),xt=[new bt(152,4072443489),new bt(121,3077413346),new bt(243,1046459332),new bt(174,783016616),new bt(30,1329849456)];function St(t,r){var e=t,n=e.hi>>>3;e.hi&=It.hi,e.lo&=It.lo,e.hi<<=5,e.hi|=e.lo>>>27,e.lo<<=5;for(var i=0;i<xt.length;i++)n>>>i&1&&(e.hi^=xt[i].hi,e.lo^=xt[i].lo);return e.lo^=r,e}function At(t,r,e,n,i,o,f){wt(Buffer.isBuffer(t)),wt(r>>>0===r),wt(Buffer.isBuffer(e)),wt(n>>>0===n),wt((255&i)===i),wt((255&o)===o),wt("boolean"==typeof f);for(var u=(1<<o)-1,a=0,s=0;r<t.length;r++){var h=t[r];if(h>>>i!=0)throw new Error("Invalid bits.");for(a=a<<i|h,s+=i;s>=o;)e[n++]=a>>>(s-=o)&u}if(f)s&&(e[n++]=a<<o-s&u);else if(s>=i||a<<o-s&u)throw new Error("Invalid bits.");return wt(n<=e.length),e.slice(0,n)}function zt(t,r,e){if(wt("string"==typeof t),wt((15&r)===r,"Invalid cashaddr type."),wt(Buffer.isBuffer(e)),0===t.length||t.length>83)throw new Error("Invalid cashaddr prefix.");var n=function(t){switch(wt(t>>>0===t),t){case 20:return 0;case 24:return 1;case 28:return 2;case 32:return 3;case 40:return 4;case 48:return 5;case 56:return 6;case 64:return 7;default:throw new Error("Non standard length.")}}(e.length),i=Buffer.allocUnsafe(e.length+1);return i[0]=r<<3|n,e.copy(i,1),function(t,r){wt("string"==typeof t),wt(Buffer.isBuffer(r));for(var e=new bt(0,1),n="",i=!1,o=!1,f=0;f<t.length;f++){var u=t.charCodeAt(f);if(65280&u||u>>>5==0)throw new Error("Invalid cashaddr character.");if(u>=97&&u<=122)o=!0;else if(u>=65&&u<=90)i=!0,u=u-65+97;else if(u>=48&&u<=57)throw new Error("Invalid cashaddr prefix.");St(e,31&u),n+=String.fromCharCode(u)}if(o&&i)throw new Error("Invalid cashaddr prefix.");St(e,0),n+=":";for(var a=0;a<r.length;a++){var s=r[a];if(s>>>5!=0)throw new Error("Invalid cashaddr value.");St(e,s),n+=Bt[s]}for(var h=0;h<8;h++)St(e,0);e.lo^=1;for(var c=0;c<8;c++){var l=31&e.ushrn32(5*(7-c));n+=Bt[l]}return n}(t,At(i,0,_t,0,8,5,!0))}function kt(t,r){void 0===r&&(r="bitcoincash"),wt("string"==typeof t),wt("string"==typeof r);var e=function(t,r){if(wt("string"==typeof t),t.length<8||t.length>196)throw new Error("Invalid cashaddr data length.");for(var e=!1,n=!1,i=!1,o=0,f=0;f<t.length;f++){var u=t.charCodeAt(f);if(u>=97&&u<=122)e=!0;else if(u>=65&&u<=90)n=!0;else if(u>=48&&u<=57)i=!0;else{if(58!==u)throw new Error("Invalid cashaddr character.");if(i||0===f||f>83)throw new Error("Invalid cashaddr prefix.");if(0!==o)throw new Error("Invalid cashaddr separators.");o=f}}if(n&&e)throw new Error("Invalid cashaddr casing.");var a,s=new bt(0,1);0===o?a=r.toLowerCase():(a=t.substring(0,o).toLowerCase(),o+=1);for(var h=0;h<a.length;h++)St(s,31&(32|a.charCodeAt(h)));St(s,0);var c=t.length-o;if(c<=8||c>112)throw new Error("Invalid cashaddr data length.");for(var l=Buffer.allocUnsafe(c),d=o;d<t.length;d++){var p=t.charCodeAt(d),v=65408&p?-1:Et[p];if(-1===v)throw new Error("Invalid cashaddr character.");St(s,v),d+8<t.length&&(l[d-o]=v)}if(0!==s.hi||1!==s.lo||a!==r)throw new Error("Invalid cashaddr checksum.");return[a,l.slice(0,-8)]}(t,r),n=e[0],i=e[1],o=5*i.length&7;if(o>=5)throw new Error("Invalid padding in data.");if(i[i.length-1]&(1<<o)-1)throw new Error("Non zero padding.");var f=At(i,0,i,0,5,8,!1),u=f[0]>>>3&31,a=f.slice(1),s=20+4*(3&f[0]);if(4&f[0]&&(s*=2),s!==a.length)throw new Error("Invalid cashaddr data length.");return{prefix:n,type:u,hash:a}}var Ot=function(t){function r(r){var e=r.slice(0,-4),n=r.slice(-4),i=t(e);if(!(n[0]^i[0]|n[1]^i[1]|n[2]^i[2]|n[3]^i[3]))return e}return{bs58Encode:function(r){var e=t(r);return o.encode(Buffer.concat([r,e],r.length+4))},bs58Decode:function(t){var e=r(o.decode(t));if(!e)throw new Error("Invalid checksum");return e},decodeUnsafe:function(t){var e=o.decodeUnsafe(t);if(e)return r(e)}}}(function(t){var r=(new B).update(t).digest();return(new B).update(r).digest()}),Ct=Ot.bs58Decode,Ut=Ot.bs58Encode;export{l as b32decode,d as b32encode,Ct as bs58Decode,Ut as bs58Encode,ot as calculateChecksum,kt as cashaddrDecode,zt as cashaddrEncode,k as codec,nt as decodeCheck,it as encodeCheck,J as eosPublicKey,s as hex2a,p as isValid,U as isValidChecksumAddress,gt as ss58Decode,yt as ss58Encode,O as stripHexPrefix,C as toChecksumAddress,c as ua2hex};
|
|
//# sourceMappingURL=index.module.js.map
|