From 4e9af8060af622346ec846ca942d22c76e9930ae Mon Sep 17 00:00:00 2001 From: sairajzero Date: Sat, 19 Sep 2020 03:39:19 +0530 Subject: [PATCH 1/5] stdop Improvement STD_OP floGlobals - combined vectorClock (for objectData) and generalVC (for generalData) into lastVC. init - Bug fixed: floData of 16 character dropped by floSight. floCrypto - Improved getFloIDfromPubkeyHex to getFloID: return floID from either pubKey or privKey. - verifyPrivKey, validateAddr return false if arguments are empty. floSupernode - Removed floSupernode module. - Kbucket submodule moved to floCloudAPI Kbucket (subModule) - Improved KBucket module - Kbucket cyclic array (SNCO) is formed (w.r.t. distance) when kBucket is launched - nextNode, prevNode are determined by SNCO compactIDB - Improved searchData (accepts option parameter instead of patternEval) .options (lowerKey, upperKey, atKey, patternEval, lastOnly) floCloudAPI - Improved data signature during sending. .current format ( ["receiverID", "time", "application", "type", "message", "comment"]) - Adding live request: .requesting data now accepts option callback .when callback option is passed, the request becomes live request .ie, cloud automatically forwards new data received by the requested floID .callback function is called with new data is received .callback supported functions: requestApplicationData, requestGeneralData, requestObjectData - Adding feature to delete from cloud. .data received can now be deleted from cloud .signature is required for verification (thus requires privKey logged in) .can only delete own data (received only) floDapps - merged manageSubAdmins and setApplicationSettings into manageAppConfig(adminPrivKey, addList, rmList, settings) - Improved getNextGeneralData .getNextGeneralData can now sub-categorize comment (even when generalData is request for all comment) .getNextGeneralData now keeps a track pointer of previously called. [vectorClock (ie, second parameter) must be null, default is null] --- standard_Operations.html | 1606 ++++++++++++++++---------------------- 1 file changed, 683 insertions(+), 923 deletions(-) diff --git a/standard_Operations.html b/standard_Operations.html index 8e9b0a3..a3756d6 100644 --- a/standard_Operations.html +++ b/standard_Operations.html @@ -28,10 +28,9 @@ //for cloud apps subAdmins: [], application: "TEST_MODE", - vectorClock: {}, appObjects: {}, generalData: {}, - generalVC: {} + lastVC: {} } @@ -4932,12 +4931,12 @@ if (typeof Crypto == "undefined" || !Crypto.util) { floDataCount = this.floData.length; //flochange -- creating unique data character count logic for floData. This string is prefixed before actual floData string in Raw Transaction - if (floDataCount <= 16) { + if (floDataCount < 16) { floDataCountString = floDataCount.toString(16); floDataCountString = "0"+ floDataCountString; } else if (floDataCount < 253) { floDataCountString = floDataCount.toString(16); - } else if (floDataCount <= 1023) { + } else if (floDataCount <= 1040) { floDataCountAdjusted = (floDataCount - 253) + parseInt("0xfd00fd"); floDataCountStringAdjusted = floDataCountAdjusted.toString(16); floDataCountString = floDataCountStringAdjusted.substr(0,2)+ floDataCountStringAdjusted.substr(4,2)+ floDataCountStringAdjusted.substr(2,2); @@ -6739,27 +6738,22 @@ Bitcoin.Util = { addedDiff(original, updatedObj) returns only the values added to the updated object deletedDiff(original, updatedObj) returns only the values deleted in the updated object updatedDiff(original, updatedObj) returns only the values that have been changed in the updated object - findDifference(original, updatedObj) returns an object with the added, deleted and updated differences - mergeRecurcive(original, diff) this will get you a new object that will merge all the changes between the old object and the new object + findDiff(original, updatedObj) returns an object with the added, deleted and updated differences + mergeRecursive(original, diff) returns a new object that will merge difference to old object + mergeDiff(original, allDiff) returns a new object from original object merged with all differences (allDiff is returned object of findDiff) */ (function(){ const isDate = d => d instanceof Date; const isEmpty = o => Object.keys(o).length === 0; const isObject = o => o != null && typeof o === 'object'; - const properObject = o => isObject(o) && !o.hasOwnProperty ? { ...o } : o; - - + const properObject = o => isObject(o) && !o.hasOwnProperty ? { ...o } : o; const getLargerArray = (l, r) => l.length > r.length ? l : r; - + const preserve = (diff, left, right) => { - if (!isObject(diff)) return diff; - return Object.keys(diff).reduce((acc, key) => { - const leftArray = left[key]; const rightArray = right[key]; - if (Array.isArray(leftArray) && Array.isArray(rightArray)) { const array = [...getLargerArray(leftArray, rightArray)]; return { @@ -6769,13 +6763,11 @@ Bitcoin.Util = { acc2[index] = preserve(diff[key][index], leftArray[index], rightArray[index]); // diff recurse and check for nested arrays return acc2; } - delete acc2[index]; // no diff aka empty return acc2; }, array) }; } - return { ...acc, [key]: diff[key] @@ -6783,31 +6775,21 @@ Bitcoin.Util = { }, {}); }; - const updatedDiff = (lhs, rhs) => { - if (lhs === rhs) return {}; - if (!isObject(lhs) || !isObject(rhs)) return rhs; - const l = properObject(lhs); const r = properObject(rhs); - if (isDate(l) || isDate(r)) { if (l.valueOf() == r.valueOf()) return {}; return r; } - return Object.keys(r).reduce((acc, key) => { - if (l.hasOwnProperty(key)) { const difference = updatedDiff(l[key], r[key]); - if (isObject(difference) && isEmpty(difference) && !isDate(difference)) return acc; - return { ...acc, [key]: difference }; } - return acc; }, {}); }; @@ -6815,123 +6797,90 @@ Bitcoin.Util = { const diff = (lhs, rhs) => { if (lhs === rhs) return {}; // equal return no diff - if (!isObject(lhs) || !isObject(rhs)) return rhs; // return updated rhs - const l = properObject(lhs); const r = properObject(rhs); - const deletedValues = Object.keys(l).reduce((acc, key) => { return r.hasOwnProperty(key) ? acc : { ...acc, [key]: null }; }, {}); - if (isDate(l) || isDate(r)) { if (l.valueOf() == r.valueOf()) return {}; return r; } - return Object.keys(r).reduce((acc, key) => { if (!l.hasOwnProperty(key)) return { ...acc, [key]: r[key] }; // return added r key - const difference = diff(l[key], r[key]); - if (isObject(difference) && isEmpty(difference) && !isDate(difference)) return acc; // return no diff - return { ...acc, [key]: difference }; // return updated key }, deletedValues); }; const addedDiff = (lhs, rhs) => { - if (lhs === rhs || !isObject(lhs) || !isObject(rhs)) return {}; - const l = properObject(lhs); const r = properObject(rhs); - return Object.keys(r).reduce((acc, key) => { if (l.hasOwnProperty(key)) { const difference = addedDiff(l[key], r[key]); - if (isObject(difference) && isEmpty(difference)) return acc; - return { ...acc, [key]: difference }; } - return { ...acc, [key]: r[key] }; }, {}); }; - const arrayDiff = (lhs, rhs) => { if (lhs === rhs) return {}; // equal return no diff - if (!isObject(lhs) || !isObject(rhs)) return rhs; // return updated rhs - const l = properObject(lhs); const r = properObject(rhs); - const deletedValues = Object.keys(l).reduce((acc, key) => { return r.hasOwnProperty(key) ? acc : { ...acc, [key]: null }; }, {}); - if (isDate(l) || isDate(r)) { if (l.valueOf() == r.valueOf()) return {}; return r; } - if (Array.isArray(r) && Array.isArray(l)) { const deletedValues = l.reduce((acc, item, index) => { return r.hasOwnProperty(index) ? acc.concat(item) : acc.concat(null); }, []); - return r.reduce((acc, rightItem, index) => { if (!deletedValues.hasOwnProperty(index)) { return acc.concat(rightItem); } - const leftItem = l[index]; const difference = diff(rightItem, leftItem); - if (isObject(difference) && isEmpty(difference) && !isDate(difference)) { delete acc[index]; return acc; // return no diff } - return acc.slice(0, index).concat(rightItem).concat(acc.slice(index + 1)); // return updated key }, deletedValues); } return Object.keys(r).reduce((acc, key) => { if (!l.hasOwnProperty(key)) return { ...acc, [key]: r[key] }; // return added r key - const difference = diff(l[key], r[key]); - if (isObject(difference) && isEmpty(difference) && !isDate(difference)) return acc; // return no diff - return { ...acc, [key]: difference }; // return updated key }, deletedValues); }; const deletedDiff = (lhs, rhs) => { if (lhs === rhs || !isObject(lhs) || !isObject(rhs)) return {}; - const l = properObject(lhs); const r = properObject(rhs); - return Object.keys(l).reduce((acc, key) => { if (r.hasOwnProperty(key)) { const difference = deletedDiff(l[key], r[key]); - if (isObject(difference) && isEmpty(difference)) return acc; - return { ...acc, [key]: difference }; } - return { ...acc, [key]: null }; }, {}); }; - - window.findDifference = (lhs, rhs) => ({ + window.findDiff = (lhs, rhs) => ({ added: addedDiff(lhs, rhs), deleted: deletedDiff(lhs, rhs), updated: updatedDiff(lhs, rhs), @@ -6940,22 +6889,17 @@ Bitcoin.Util = { const mergeRecursive = (obj1, obj2) => { for (var p in obj2) { try { - if(obj2[p].constructor == Object) { + if(obj2[p].constructor == Object) obj1[p] = mergeRecursive(obj1[p], obj2[p]); - } // Property in destination object set; update its value. else if (Ext.isArray(obj2[p])) { // obj1[p] = []; - if (obj2[p].length < 1) { + if (obj2[p].length < 1) obj1[p] = obj2[p]; - } - else { + else obj1[p] = mergeRecursive(obj1[p], obj2[p]); - } - - }else{ + }else obj1[p] = obj2[p]; - } } catch (e) { // Property in destination object not set; create it and set its value. obj1[p] = obj2[p]; @@ -6964,19 +6908,6 @@ Bitcoin.Util = { return obj1; } - /* - var test = { - foo : { - bar : { - baz : null - } - }, - bar : 1 - }; - cleanse(test); - - Rohit: Added a small fix for object being entered as Array*/ - const cleanse = (obj) => { Object.keys(obj).forEach(key => { var value = obj[key]; @@ -6984,34 +6915,28 @@ Bitcoin.Util = { // Recurse... cleanse(value); // ...and remove if now "empty" (NOTE: insert your definition of "empty" here) - if (!Object.keys(value).length) { - delete obj[key] - } - } - else if (value === null) { - // null, remove it - delete obj[key]; - + //if (!Object.keys(value).length) + // delete obj[key]; } + else if (value === null) + delete obj[key];// null, remove it }); - - if(obj.constructor.toString().indexOf("Array") != -1) {obj = obj.filter(function (el) { - return el != null; - });} - - return obj; + if(obj.constructor.toString().indexOf("Array") != -1) {obj = obj.filter(function (el) { + return el != null; + });} + return obj; } - /*obj is original object or array, diff is the output of findDifference */ - window.mergeDifference = (obj,diff) => { + /*obj is original object or array, diff is the output of findDiff */ + window.mergeDiff = (obj, diff) => { if(Object.keys(diff.updated).length !== 0) - obj = mergeRecursive(obj,diff.updated) + obj = mergeRecursive(obj, diff.updated) if(Object.keys(diff.deleted).length !== 0){ - obj = mergeRecursive(obj,diff.deleted) + obj = mergeRecursive(obj, diff.deleted) obj = cleanse(obj) } if(Object.keys(diff.added).length !== 0) - obj = mergeRecursive(obj,diff.added) + obj = mergeRecursive(obj, diff.added) return obj } })(); @@ -7125,7 +7050,7 @@ Bitcoin.Util = { }, //generate a random String within length (options : alphaNumeric chars only) - randString: function (length, alphaNumeric = false) { + randString: function (length, alphaNumeric = true) { var result = ''; if (alphaNumeric) var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; @@ -7217,22 +7142,24 @@ Bitcoin.Util = { //Returns public-key from private-key getPubKeyHex: function (privateKeyHex) { - if(!privateKeyHex) + if (!privateKeyHex) return null; var key = new Bitcoin.ECKey(privateKeyHex); - if (key.priv == null) + if (key.priv == null) return null; key.setCompressed(true); - var pubkeyHex = key.getPubKeyHex(); - return pubkeyHex; + return key.getPubKeyHex(); }, - //Returns flo-ID from public-key - getFloIDfromPubkeyHex: function (pubkeyHex) { + //Returns flo-ID from public-key or private-key + getFloID: function (keyHex) { + if (!keyHex) + return null; try { - var key = new Bitcoin.ECKey().setPub(pubkeyHex); - var floID = key.getBitcoinAddress(); - return floID; + var key = new Bitcoin.ECKey(keyHex); + if (key.priv == null) + key.setPub(keyHex); + return key.getBitcoinAddress(); } catch (e) { return null; } @@ -7240,6 +7167,8 @@ Bitcoin.Util = { //Verify the private-key for the given public-key or flo-ID verifyPrivKey: function (privateKeyHex, pubKey_floID, isfloID = true) { + if (!privateKeyHex || !pubKey_floID) + return false; try { var key = new Bitcoin.ECKey(privateKeyHex); if (key.priv == null) @@ -7258,6 +7187,8 @@ Bitcoin.Util = { //Check if the given Address is valid or not validateAddr: function (inpAddr) { + if (!inpAddr) + return false; try { var addr = new Bitcoin.Address(inpAddr); return true; @@ -7502,8 +7433,7 @@ Bitcoin.Util = { if (!key) invalids.InvalidSenderPrivKeys.push(key); else { - let floID = floCrypto.getFloIDfromPubkeyHex(floCrypto - .getPubKeyHex(key)); + let floID = floCrypto.getFloID(key); senders[floID] = { wif: key } @@ -7522,12 +7452,11 @@ Bitcoin.Util = { invalids.InvalidSenderPrivKeys.push(key); else { if (typeof senderPrivKeys[key] !== 'number' || senderPrivKeys[ - key] <= 0) + key] <= 0) invalids.InvalidSenderAmountFor.push(key) else inputVal += senderPrivKeys[key]; - let floID = floCrypto.getFloIDfromPubkeyHex(floCrypto.getPubKeyHex( - key)); + let floID = floCrypto.getFloID(key); senders[floID] = { wif: key, coins: senderPrivKeys[key] @@ -7738,643 +7667,6 @@ Bitcoin.Util = { } } -