UNPKG

@szfdiwang/platon-tools

Version:

PlatON utility library with built-in contract trading parameters

3 lines (2 loc) 4.33 kB
(function(s,f){typeof exports=="object"&&typeof module<"u"?module.exports=f():typeof define=="function"&&define.amd?define(f):(s=typeof globalThis<"u"?globalThis:s||self,s["PlatON-tools"]=f())})(this,function(){"use strict";function s(e){if(Array.isArray(e)){const t=[];let n=0;for(let o=0;o<e.length;o++){const i=s(e[o]);t.push(i),n+=i.length}return u(x(n,192),...t)}const r=E(e);return r.length===1&&r[0]<128?r:u(x(r.length,128),r)}function f(e,r,t){if(t>e.length)throw new Error("invalid RLP (safeSlice): end slice of Uint8Array out-of-bounds");return e.slice(r,t)}function y(e){if(e[0]===0)throw new Error("invalid RLP: extra zeros");return m(w(e))}function x(e,r){if(e<56)return Uint8Array.from([e+r]);const t=h(e),n=t.length/2,o=h(r+55+n);return Uint8Array.from(l(o+t))}function A(e,r=!1){if(typeof e>"u"||e===null||e.length===0)return Uint8Array.from([]);const t=E(e),n=d(t);if(r)return{data:n.data,remainder:n.remainder.slice()};if(n.remainder.length!==0)throw new Error("invalid RLP: remainder must be zero");return n.data}function d(e){let r,t,n,o,i;const c=[],a=e[0];if(a<=127)return{data:e.slice(0,1),remainder:e.subarray(1)};if(a<=183){if(r=a-127,a===128?n=Uint8Array.from([]):n=f(e,1,r),r===2&&n[0]<128)throw new Error("invalid RLP encoding: invalid prefix, single byte < 0x80 are not prefixed");return{data:n,remainder:e.subarray(r)}}else if(a<=191){if(t=a-182,e.length-1<t)throw new Error("invalid RLP: not enough bytes for string length");if(r=y(f(e,1,t)),r<=55)throw new Error("invalid RLP: expected string length to be greater than 55");return n=f(e,t,r+t),{data:n,remainder:e.subarray(r+t)}}else if(a<=247){for(r=a-191,o=f(e,1,r);o.length;)i=d(o),c.push(i.data),o=i.remainder;return{data:c,remainder:e.subarray(r)}}else{if(t=a-246,r=y(f(e,1,t)),r<56)throw new Error("invalid RLP: encoded list too short");const g=t+r;if(g>e.length)throw new Error("invalid RLP: total length is larger than the data");for(o=f(e,t,g);o.length;)i=d(o),c.push(i.data),o=i.remainder;return{data:c,remainder:e.subarray(g)}}}const B=Array.from({length:256},(e,r)=>r.toString(16).padStart(2,"0"));function w(e){let r="";for(let t=0;t<e.length;t++)r+=B[e[t]];return r}function m(e){const r=Number.parseInt(e,16);if(Number.isNaN(r))throw new Error("Invalid byte sequence");return r}function l(e){if(typeof e!="string")throw new TypeError("hexToBytes: expected string, got "+typeof e);if(e.length%2)throw new Error("hexToBytes: received invalid unpadded hex");const r=new Uint8Array(e.length/2);for(let t=0;t<r.length;t++){const n=t*2;r[t]=m(e.slice(n,n+2))}return r}function u(...e){if(e.length===1)return e[0];const r=e.reduce((n,o)=>n+o.length,0),t=new Uint8Array(r);for(let n=0,o=0;n<e.length;n++){const i=e[n];t.set(i,o),o+=i.length}return t}function b(e){return new TextEncoder().encode(e)}function h(e){if(e<0)throw new Error("Invalid integer as argument, must be unsigned!");const r=e.toString(16);return r.length%2?`0${r}`:r}function R(e){return e.length%2?`0${e}`:e}function p(e){return e.length>=2&&e[0]==="0"&&e[1]==="x"}function U(e){return typeof e!="string"?e:p(e)?e.slice(2):e}function E(e){if(e instanceof Uint8Array)return e;if(typeof e=="string")return p(e)?l(R(U(e))):b(e);if(typeof e=="number"||typeof e=="bigint")return e?l(h(e)):Uint8Array.from([]);if(e==null)return Uint8Array.from([]);throw new Error("toBytes: received unsupported type "+typeof e)}const T={bytesToHex:w,concatBytes:u,hexToBytes:l,utf8ToBytes:b},L={encode:s,decode:A},P=e=>{try{if(!e||isNaN(e))throw new Error("Please check the parameters");if(e>=1e3&&e<2e3)return"0x1000000000000000000000000000000000000002";if(e>=2e3&&e<3e3)return"0x1000000000000000000000000000000000000005";if(e>=3e3&&e<4e3)return"0x1000000000000000000000000000000000000004";if(e>=4e3&&e<5e3)return"0x1000000000000000000000000000000000000001";if(e>=5e3&&e<6e3)return"0x1000000000000000000000000000000000000006";throw new Error("The contract corresponding to code cannot be found")}catch(r){console.error("error:",r)}},H=e=>{const r=[];for(const n of e)r.push("0x"+T.bytesToHex(L.encode(n)));return"0x"+T.bytesToHex(L.encode(r))};return{getContractAddress:P,getParams:(e,r,t)=>{try{if(!r||!t||t.length===0)throw new Error("Function params error");return{from:r,to:P(e),data:H([e,...t])}}catch(n){console.error("error:",n)}}}}); //# sourceMappingURL=platon-tools.umd.js.map