import{BigInteger,SecureRandom}from"jsbn";import{ECCurveFp}from"./ec";const rng=new SecureRandom,{curve:curve,G:G,n:n}=generateEcparam();function getGlobalCurve(){return curve}function generateEcparam(){const e=new BigInteger("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF",16),F=new BigInteger("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC",16),t=new BigInteger("28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93",16),n=new ECCurveFp(e,F,t),r=n.decodePointHex("0432C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0");return{curve:n,G:r,n:new BigInteger("FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123",16)}}function generateKeyPairHex(e,F,t){const r=(e?new BigInteger(e,F,t):new BigInteger(n.bitLength(),rng)).mod(n.subtract(BigInteger.ONE)).add(BigInteger.ONE),o=leftPad(r.toString(16),64),c=G.multiply(r);return{privateKey:o,publicKey:"04"+leftPad(c.getX().toBigInteger().toString(16),64)+leftPad(c.getY().toBigInteger().toString(16),64)}}function compressPublicKeyHex(e){if(130!==e.length)throw new Error("Invalid public key to compress");const F=(e.length-2)/2,t=e.substr(2,F);let n="03";return new BigInteger(e.substr(F+2,F),16).mod(new BigInteger("2")).equals(BigInteger.ZERO)&&(n="02"),n+t}function utf8ToHex(e){const F=(e=unescape(encodeURIComponent(e))).length,t=[];for(let n=0;n<F;n++)t[n>>>2]|=(255&e.charCodeAt(n))<<24-n%4*8;const n=[];for(let e=0;e<F;e++){const F=t[e>>>2]>>>24-e%4*8&255;n.push((F>>>4).toString(16)),n.push((15&F).toString(16))}return n.join("")}function leftPad(e,F){return e.length>=F?e:new Array(F-e.length+1).join("0")+e}function arrayToHex(e){return e.map((e=>1===(e=e.toString(16)).length?"0"+e:e)).join("")}function arrayToUtf8(e){const F=[];let t=0;for(let n=0;n<2*e.length;n+=2)F[n>>>3]|=parseInt(e[t],10)<<24-n%8*4,t++;try{const t=[];for(let n=0;n<e.length;n++){const e=F[n>>>2]>>>24-n%4*8&255;t.push(String.fromCharCode(e))}return decodeURIComponent(escape(t.join("")))}catch(e){throw new Error("Malformed UTF-8 data")}}function hexToArray(e){const F=[];let t=e.length;t%2!=0&&(e=leftPad(e,t+1)),t=e.length;for(let n=0;n<t;n+=2)F.push(parseInt(e.substr(n,2),16));return F}function verifyPublicKey(e){const F=curve.decodePointHex(e);if(!F)return!1;const t=F.getX();return F.getY().square().equals(t.multiply(t.square()).add(t.multiply(curve.a)).add(curve.b))}function comparePublicKeyHex(e,F){const t=curve.decodePointHex(e);if(!t)return!1;const n=curve.decodePointHex(F);return!!n&&t.equals(n)}export{getGlobalCurve,generateEcparam,generateKeyPairHex,compressPublicKeyHex,utf8ToHex,leftPad,arrayToHex,arrayToUtf8,hexToArray,verifyPublicKey,comparePublicKeyHex};
//# sourceMappingURL=utils.js.map