diff --git a/floCloudAPI.js b/floCloudAPI.js index 402c0c5..1fafb51 100644 --- a/floCloudAPI.js +++ b/floCloudAPI.js @@ -1,4 +1,4 @@ -(function(EXPORTS) { //floCloudAPI v2.4.2c +(function(EXPORTS) { //floCloudAPI v2.4.2d /* FLO Cloud operations to send/request application data*/ 'use strict'; const floCloudAPI = EXPORTS; @@ -317,6 +317,7 @@ data => { data = objectifier(data); let filtered = {}, + proxy = proxyID(request.receiverID), r = request; for (let v in data) { let d = data[v]; @@ -325,7 +326,7 @@ (r.atVectorClock || !r.upperVectorClock || r.upperVectorClock >= v) && (!r.afterTime || r.afterTime < d.log_time) && r.application == d.application && - r.receiverID == d.receiverID && + (proxy == d.receiverID || proxy == d.proxyID) && (!r.comment || r.comment == d.comment) && (!r.type || r.type == d.type) && (!r.senderID || r.senderID.includes(d.senderID))) diff --git a/floCrypto.js b/floCrypto.js index 01921d1..025f698 100644 --- a/floCrypto.js +++ b/floCrypto.js @@ -1,4 +1,4 @@ -(function(EXPORTS) { //floCrypto v2.3.3b +(function(EXPORTS) { //floCrypto v2.3.3c /* FLO Crypto Operators */ 'use strict'; const floCrypto = EXPORTS; @@ -245,101 +245,96 @@ //Check if the given address (any blockchain) is valid or not floCrypto.validateAddr = function(address, std = true, bech = true) { - if (address.length == 33 || address.length == 34) { //legacy or segwit encoding - if (std === false) + let raw = decodeAddress(address); + if (!raw) + return false; + if (typeof raw.version !== 'undefined') { //legacy or segwit + if (std == false) return false; - 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; - else if (std === true || (!Array.isArray(std) && std === raw[0]) || (Array.isArray(std) && std.includes(raw[0]))) + else if (std === true || (!Array.isArray(std) && std === raw.version) || (Array.isArray(std) && std.includes(raw.version))) return true; else return false; - } else if (address.length == 42 || address.length == 62) { //bech encoding + } else if (typeof raw.bech_version !== 'undefined') { //bech32 if (bech === false) return false; - let decode = coinjs.bech32_decode(address); - if (!decode) - return false; - var raw = decode.data; - if (bech === true || (!Array.isArray(bech) && bech === raw[0]) || (Array.isArray(bech) && bech.includes(raw[0]))) + else if (bech === true || (!Array.isArray(bech) && bech === raw.bech_version) || (Array.isArray(bech) && bech.includes(raw.bech_version))) return true; else return false; - } else //unknown length + } else //unknown return false; } //Check the public-key for the address (any blockchain) floCrypto.verifyPubKey = function(pubKeyHex, address) { - let pub_hash = Crypto.util.bytesToHex(ripemd160(Crypto.SHA256(Crypto.util.hexToBytes(pubKeyHex), { - asBytes: true - }))); - if (address.length == 33 || 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, { + let raw = decodeAddress(address), + pub_hash = Crypto.util.bytesToHex(ripemd160(Crypto.SHA256(Crypto.util.hexToBytes(pubKeyHex), { 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; + }))); + return raw ? pub_hash === raw.hex : false; } //Convert the given address (any blockchain) to equivalent floID floCrypto.toFloID = function(address) { if (!address) return; - var bytes; - if (address.length == 33 || address.length == 34) { //legacy encoding + let raw = decodeAddress(address); + if (!raw) + return; + raw.bytes.unshift(bitjs.pub); + let hash = Crypto.SHA256(Crypto.SHA256(raw.bytes, { + asBytes: true + }), { + asBytes: true + }); + return bitjs.Base58.encode(raw.bytes.concat(hash.slice(0, 4))); + } + + //Checks if the given addresses (any blockchain) are same (w.r.t keys) + floCrypto.isSameAddr = function(addr1, addr2) { + if (!addr1 || !addr2) + return; + let raw1 = decodeAddress(addr1), + raw2 = decodeAddress(addr2); + if (!raw1 || !raw2) + return false; + else + return raw1.hex === raw2.hex; + } + + const decodeAddress = floCrypto.decodeAddr = function(address) { + if (!address) + return; + else if (address.length == 33 || address.length == 34) { //legacy encoding let decode = bitjs.Base58.decode(address); - bytes = decode.slice(0, decode.length - 4); + let bytes = decode.slice(0, decode.length - 4); let checksum = decode.slice(decode.length - 4), hash = Crypto.SHA256(Crypto.SHA256(bytes, { asBytes: true }), { asBytes: true }); - hash[0] != checksum[0] || hash[1] != checksum[1] || hash[2] != checksum[2] || hash[3] != checksum[3] ? - bytes = undefined : bytes.shift(); + return (hash[0] != checksum[0] || hash[1] != checksum[1] || hash[2] != checksum[2] || hash[3] != checksum[3]) ? null : { + version: bytes.shift(), + hex: Crypto.util.bytesToHex(bytes), + bytes + } } else if (address.length == 42) { //bech encoding let decode = coinjs.bech32_decode(address); if (decode) { - bytes = decode.data; - bytes.shift(); + let bytes = decode.data; + let bech_version = bytes.shift(); bytes = coinjs.bech32_convert(bytes, 5, 8, false); - } + return { + bech_version, + hrp: decode.hrp, + hex: Crypto.util.bytesToHex(bytes), + bytes + } + } else + return null; } - if (!bytes) - return; - bytes.unshift(bitjs.pub); - let hash = Crypto.SHA256(Crypto.SHA256(bytes, { - asBytes: true - }), { - asBytes: true - }); - return bitjs.Base58.encode(bytes.concat(hash.slice(0, 4))); } //Split the str using shamir's Secret and Returns the shares diff --git a/floDapps.js b/floDapps.js index 1da58b8..38ee0b4 100644 --- a/floDapps.js +++ b/floDapps.js @@ -1,4 +1,4 @@ -(function(EXPORTS) { //floDapps v2.3.2c +(function(EXPORTS) { //floDapps v2.3.2d /* General functions for FLO Dapps*/ 'use strict'; const floDapps = EXPORTS; @@ -63,7 +63,7 @@ return Crypto.AES.decrypt(data, raw_user.private); }, get db_name() { - return "floDapps#" + user.id; + return "floDapps#" + floCrypto.toFloID(user.id); }, lock() { user_private = user_priv_wrap; @@ -72,6 +72,30 @@ if (await user.private === raw_user.private) user_private = user_priv_raw; }, + get_contact(id) { + if (!user.contacts) + throw "Contacts not available"; + else if (user.contacts[id]) + return user.contacts[id]; + else { + let id_raw = floCrypto.decodeAddr(id).hex; + for (let i in user.contacts) + if (floCrypto.decodeAddr(i).hex == id_raw) + return user.contacts[i]; + } + }, + get_pubKey(id) { + if (!user.pubKeys) + throw "Contacts not available"; + else if (user.pubKeys[id]) + return user.pubKeys[id]; + else { + let id_raw = floCrypto.decodeAddr(id).hex; + for (let i in user.pubKeys) + if (floCrypto.decodeAddr(i).hex == id_raw) + return user.pubKeys[i]; + } + }, clear() { user_id = user_public = user_private = undefined; user_priv_raw = aes_key = undefined; @@ -475,7 +499,7 @@ floDapps.storeContact = function(floID, name) { return new Promise((resolve, reject) => { - if (!floCrypto.validateFloID(floID)) + if (!floCrypto.validateAddr(floID)) return reject("Invalid floID!") compactIDB.writeData("contacts", name, floID, user.db_name).then(result => { user.contacts[floID] = name; @@ -488,9 +512,9 @@ return new Promise((resolve, reject) => { if (floID in user.pubKeys) return resolve("pubKey already stored") - if (!floCrypto.validateFloID(floID)) + if (!floCrypto.validateAddr(floID)) return reject("Invalid floID!") - if (floCrypto.getFloID(pubKey) != floID) + if (!floCrypto.verifyPubKey(pubKey, floID)) return reject("Incorrect pubKey") compactIDB.writeData("pubKeys", pubKey, floID, user.db_name).then(result => { user.pubKeys[floID] = pubKey;