From f673d3f37a075adfc22fb2ab76ec91a7a8eda14b Mon Sep 17 00:00:00 2001 From: sairajzero Date: Wed, 27 Jul 2022 22:16:52 +0530 Subject: [PATCH] floCrypto v2.3.3a floCrypto.verifyPubKey: verify public key of any blockchain address - Parameters: 1. pubKeyHex - public key hex 2. address - blockchain address floCrypto.getAddress: get address respective to the blockchain - Parameters: 1. privateKeyHex - private key (WIF) 2. strict - (optional, default=false) if false returns floID when no match of privatekey prefix found. --- floCrypto.js | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/floCrypto.js b/floCrypto.js index e32882e..b252169 100644 --- a/floCrypto.js +++ b/floCrypto.js @@ -1,4 +1,4 @@ -(function(EXPORTS) { //floCrypto v2.3.2b +(function(EXPORTS) { //floCrypto v2.3.3a /* FLO Crypto Operators */ 'use strict'; const floCrypto = EXPORTS; @@ -7,6 +7,7 @@ const ecparams = EllipticCurve.getSECCurveByName("secp256k1"); const ascii_alternatives = `‘ '\n’ '\n“ "\n” "\n– --\n— ---\n≥ >=\n≤ <=\n≠ !=\n× *\n÷ /\n← <-\n→ ->\n↔ <->\n⇒ =>\n⇐ <=\n⇔ <=>`; const exponent1 = () => p.add(BigInteger.ONE).divide(BigInteger("4")); + coinjs.compressed = true; //defaulting coinjs compressed to true; function calculateY(x) { let exp = exponent1(); @@ -176,6 +177,25 @@ } } + floCrypto.getAddress = function(privateKeyHex, strict = false) { + if (!privateKeyHex) + return; + var key = new Bitcoin.ECKey(privateKeyHex); + if (key.priv == null) + return null; + key.setCompressed(true); + let pubKey = key.getPubKeyHex(), + version = bitjs.Base58.decode(privateKeyHex)[0]; + switch (version) { + case coinjs.priv: //BTC + return coinjs.bech32Address(pubKey).address; + case bitjs.priv: //FLO + return bitjs.pubkey2address(pubKey); + default: + return strict ? false : bitjs.pubkey2address(pubKey); //default to FLO address (if strict=false) + } + } + //Verify the private-key for the given public-key or flo-ID floCrypto.verifyPrivKey = function(privateKeyHex, pubKey_floID, isfloID = true) { if (!privateKeyHex || !pubKey_floID) @@ -242,6 +262,35 @@ return false; } + floCrypto.verifyPubKey = function(pubKeyHex, address) { + let pub_hash = Crypto.util.bytesToHex(ripemd160(Crypto.SHA256(Crypto.util.hexToBytes(pubKeyHex), { + asBytes: true + }))); + if (address.length == 34) { //legacy encoding + let decode = bitjs.Base58.decode(address); + var raw = decode.slice(0, decode.length - 4), + checksum = decode.slice(decode.length - 4); + var hash = Crypto.SHA256(Crypto.SHA256(raw, { + asBytes: true + }), { + asBytes: true + }); + if (hash[0] != checksum[0] || hash[1] != checksum[1] || hash[2] != checksum[2] || hash[3] != checksum[3]) + return false; + raw.shift(); + return pub_hash === Crypto.util.bytesToHex(raw); + } else if (address.length == 42 || address.length == 62) { //bech encoding + let decode = coinjs.bech32_decode(address); + if (!decode) + return false; + var raw = decode.data; + raw.shift(); + raw = coinjs.bech32_convert(raw, 5, 8, false); + return pub_hash === Crypto.util.bytesToHex(raw); + } else //unknown length + return false; + } + //Split the str using shamir's Secret and Returns the shares floCrypto.createShamirsSecretShares = function(str, total_shares, threshold_limit) { try {