ribc/scripts/floCrypto.min.js
2022-10-18 15:29:10 +05:30

1 line
6.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

(function(e){"use strict";function t(e){let t=y();return e.modPow(BigInteger("3"),c).add(BigInteger("7")).mod(c).modPow(t,c)}function r(e){let r=Crypto.util.hexToBytes(e);const n=r.shift();let i=n%2;r.unshift(0);let o=new BigInteger(r),u=o.toString(),s=t(o),l=s.toString(),a=s.mod(BigInteger("2")),y=a.toString()%2;return i!==y&&(l=s.negate().mod(c).toString()),{x:u,y:l}}function n(){let e=ellipticCurveEncryption.senderRandom();var t=ellipticCurveEncryption.senderPublicString(e);return{privateKey:e,senderPublicKeyString:t}}function i(e,t){let n=r(e);var i=ellipticCurveEncryption.senderSharedKeyDerivation(n.x,n.y,t);return i}function o(e,t){return ellipticCurveEncryption.receiverSharedKeyDerivation(e.XValuePublicString,e.YValuePublicString,t)}function u(e,t=!1){let r=Bitcoin.Base58.decode(e);r.shift(),r.splice(-4,4),1==t&&r.pop(),r.unshift(0);let n=BigInteger(r).toString(),i=Crypto.util.bytesToHex(r);return{privateKeyDecimal:n,privateKeyHex:i}}const s=e,c=BigInteger("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F",16),l=EllipticCurve.getSECCurveByName("secp256k1"),a=" '\n '\n“ \"\n” \"\n --\n— ---\n≥ >=\n≤ <=\n≠ !=\n× *\n÷ /\n← <-\n→ ->\n↔ <->\n⇒ =>\n⇐ <=\n⇔ <=>",y=()=>c.add(BigInteger.ONE).divide(BigInteger("4"));coinjs.compressed=!0,s.randInt=function(e,t){return e=Math.ceil(e),t=Math.floor(t),Math.floor(securedMathRandom()*(t-e+1))+e},s.randString=function(e,t=!0){for(var r="",n=t?"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789":"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_+-./*?@#&$<>=[]{}():",i=0;i<e;i++)r+=n.charAt(Math.floor(securedMathRandom()*n.length));return r},s.encryptData=function(e,t){var r=n(),o=i(t,r.privateKey);let u=o.XValue+o.YValue,s=Crypto.AES.encrypt(e,u);return{secret:s,senderPublicKeyString:r.senderPublicKeyString}},s.decryptData=function(e,t){var r={};if("string"!=typeof t)throw new Error("No private key found.");let n=u(t,!0);if("string"!=typeof n.privateKeyDecimal)throw new Error("Failed to detremine your private key.");r.privateKey=n.privateKeyDecimal;var i=o(e.senderPublicKeyString,r.privateKey);let s=i.XValue+i.YValue,c=Crypto.AES.decrypt(e.secret,s);return c},s.signData=function(e,t){var r=new Bitcoin.ECKey(t),n=Crypto.SHA256(e),i=Bitcoin.ECDSA.sign(n,r.priv),o=Crypto.util.bytesToHex(i);return o},s.verifySign=function(e,t,r){var n=Crypto.SHA256(e),i=Crypto.util.hexToBytes(t),o=l.getCurve().decodePointHex(r),u=Bitcoin.ECDSA.verify(n,i,o);return u};const d=s.generateNewID=function(){var e=new Bitcoin.ECKey(!1);return e.setCompressed(!0),{floID:e.getBitcoinAddress(),pubKey:e.getPubKeyHex(),privKey:e.getBitcoinWalletImportFormat()}};Object.defineProperties(s,{newID:{get:()=>d()},tmpID:{get:()=>{let e=Crypto.util.randomBytes(20);e.unshift(bitjs.pub);var t=Crypto.SHA256(Crypto.SHA256(e,{asBytes:!0}),{asBytes:!0}),r=t.slice(0,4);return bitjs.Base58.encode(e.concat(r))}}}),s.getPubKeyHex=function(e){if(!e)return null;var t=new Bitcoin.ECKey(e);return null==t.priv?null:(t.setCompressed(!0),t.getPubKeyHex())},s.getFloID=function(e){if(!e)return null;try{var t=new Bitcoin.ECKey(e);return null==t.priv&&t.setPub(e),t.getBitcoinAddress()}catch{return null}},s.getAddress=function(e,t=!1){if(!e)return;var r=new Bitcoin.ECKey(e);if(null==r.priv)return null;r.setCompressed(!0);let n=r.getPubKeyHex(),i=bitjs.Base58.decode(e)[0];switch(i){case coinjs.priv:return coinjs.bech32Address(n).address;case bitjs.priv:return bitjs.pubkey2address(n);default:return!t&&bitjs.pubkey2address(n)}},s.verifyPrivKey=function(e,t,r=!0){if(!e||!t)return!1;try{var n=new Bitcoin.ECKey(e);return null!=n.priv&&(n.setCompressed(!0),!(!r||t!=n.getBitcoinAddress())||!r&&t==n.getPubKeyHex())}catch{return null}},s.validateFloID=function(e){if(!e)return!1;try{new Bitcoin.Address(e);return!0}catch{return!1}},s.validateAddr=function(e,t=!0,r=!0){let n=f(e);return!!n&&(void 0!==n.version?0!=t&&!!(!0===t||!Array.isArray(t)&&t===n.version||Array.isArray(t)&&t.includes(n.version)):void 0!==n.bech_version&&(!1!==r&&!!(!0===r||!Array.isArray(r)&&r===n.bech_version||Array.isArray(r)&&r.includes(n.bech_version))))},s.verifyPubKey=function(e,t){let r=f(t),n=Crypto.util.bytesToHex(ripemd160(Crypto.SHA256(Crypto.util.hexToBytes(e),{asBytes:!0})));return!!r&&n===r.hex},s.toFloID=function(e){if(!e)return;let t=f(e);if(!t)return;t.bytes.unshift(bitjs.pub);let r=Crypto.SHA256(Crypto.SHA256(t.bytes,{asBytes:!0}),{asBytes:!0});return bitjs.Base58.encode(t.bytes.concat(r.slice(0,4)))},s.isSameAddr=function(e,t){if(!e||!t)return;let r=f(e),n=f(t);return!(!r||!n)&&r.hex===n.hex};const f=s.decodeAddr=function(e){if(e){if(33==e.length||34==e.length){let t=bitjs.Base58.decode(e),r=t.slice(0,t.length-4),n=t.slice(t.length-4),i=Crypto.SHA256(Crypto.SHA256(r,{asBytes:!0}),{asBytes:!0});return i[0]!=n[0]||i[1]!=n[1]||i[2]!=n[2]||i[3]!=n[3]?null:{version:r.shift(),hex:Crypto.util.bytesToHex(r),bytes:r}}if(42==e.length){let t=coinjs.bech32_decode(e);if(t){let e=t.data,r=e.shift();return e=coinjs.bech32_convert(e,5,8,!1),{bech_version:r,hrp:t.hrp,hex:Crypto.util.bytesToHex(e),bytes:e}}return null}}};s.createShamirsSecretShares=function(e,t,r){try{if(e.length>0){var n=shamirSecretShare.str2hex(e),i=shamirSecretShare.share(n,t,r);return i}return!1}catch{return!1}};const p=s.retrieveShamirSecret=function(e){try{if(e.length>0){var t=shamirSecretShare.combine(e.slice(0,e.length));return t=shamirSecretShare.hex2str(t),t}return!1}catch{return!1}};s.verifyShamirsSecret=function(e,t){return t?p(e)===t:null};const h=s.validateASCII=function(e,t=!0){if("string"!=typeof e)return null;if(t){let t;for(let r=0;r<e.length;r++)if(t=e.charCodeAt(r),t<32||t>127)return!1;return!0}{let t,r={};for(let n=0;n<e.length;n++)t=e.charCodeAt(n),(t<32||t>127)&&(t in r?r[e[n]].push(n):r[e[n]]=[n]);return!Object.keys(r).length||r}};s.convertToASCII=function(e,t="soft-remove"){let r=h(e,!1);if(!0===r)return e;if(null===r)return null;let n,i=e,o={};if(a.split("\n").forEach(e=>o[e[0]]=e.slice(2)),t=t.toLowerCase(),"hard-unicode"===t)n=(e=>`\\u${("000"+e.charCodeAt().toString(16)).slice(-4)}`);else if("soft-unicode"===t)n=(e=>o[e]||`\\u${("000"+e.charCodeAt().toString(16)).slice(-4)}`);else if("hard-remove"===t)n=(e=>"");else{if("soft-remove"!==t)return null;n=(e=>o[e]||"")}for(let e in r)i=i.replaceAll(e,n(e));return i},s.revertUnicode=function(e){return e.replace(/\\u[\dA-F]{4}/gi,e=>String.fromCharCode(parseInt(e.replace(/\\u/g,""),16)))}})("object"==typeof module?module.exports:window.floCrypto={});