commit
05c58b6027
3
.gitignore
vendored
3
.gitignore
vendored
@ -4,4 +4,5 @@
|
||||
/args/param.json
|
||||
/log.txt
|
||||
/bash_start*
|
||||
*test*
|
||||
*test*
|
||||
*.tmp*
|
||||
@ -38,7 +38,7 @@ Edit the values in `args/config.json` as required.
|
||||
"sql_host": "<sql-host>"
|
||||
}
|
||||
```
|
||||
- **private-key**: Private key of the cloud
|
||||
- **private-key**: Private key of the node
|
||||
- **port**: Port of the server to run on
|
||||
- **MySQL-username**: Username for MySQL
|
||||
- **MySQL-password**: Password for MySQL
|
||||
|
||||
@ -50,10 +50,10 @@ function processDataFromUser(data) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!floCrypto.validateAddr(data.receiverID))
|
||||
return reject(INVALID("Invalid receiverID"));
|
||||
let closeNode = kBucket.closestNode(data.receiverID);
|
||||
let closeNode = cloud.closestNode(data.receiverID);
|
||||
if (!_list.serving.includes(closeNode))
|
||||
return reject(INVALID("Incorrect Supernode"));
|
||||
if (!floCrypto.validateAddr(data.receiverID))
|
||||
if (!floCrypto.validateAddr(data.senderID))
|
||||
return reject(INVALID("Invalid senderID"));
|
||||
if (data.senderID !== floCrypto.getFloID(data.pubKey))
|
||||
return reject(INVALID("Invalid pubKey"));
|
||||
@ -85,7 +85,7 @@ function processRequestFromUser(request) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!floCrypto.validateAddr(request.receiverID))
|
||||
return reject(INVALID("Invalid receiverID"));
|
||||
let closeNode = kBucket.closestNode(request.receiverID);
|
||||
let closeNode = cloud.closestNode(request.receiverID);
|
||||
if (!_list.serving.includes(closeNode))
|
||||
return reject(INVALID("Incorrect Supernode"));
|
||||
DB.searchData(closeNode, request)
|
||||
@ -98,7 +98,7 @@ function processTagFromUser(data) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!floCrypto.validateAddr(data.receiverID))
|
||||
return reject(INVALID("Invalid receiverID"));
|
||||
let closeNode = kBucket.closestNode(data.receiverID);
|
||||
let closeNode = cloud.closestNode(data.receiverID);
|
||||
if (!_list.serving.includes(closeNode))
|
||||
return reject(INVALID("Incorrect Supernode"));
|
||||
DB.getData(closeNode, data.vectorClock).then(result => {
|
||||
@ -129,7 +129,7 @@ function processNoteFromUser(data) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!floCrypto.validateAddr(data.receiverID))
|
||||
return reject(INVALID("Invalid receiverID"));
|
||||
let closeNode = kBucket.closestNode(data.receiverID);
|
||||
let closeNode = cloud.closestNode(data.receiverID);
|
||||
if (!_list.serving.includes(closeNode))
|
||||
return reject(INVALID("Incorrect Supernode"));
|
||||
DB.getData(closeNode, data.vectorClock).then(result => {
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
'use strict';
|
||||
require('./lib/BuildKBucket');
|
||||
|
||||
module.exports = function K_Bucket(options = {}) {
|
||||
function K_Bucket(masterID, nodeList) {
|
||||
|
||||
const decodeID = function(floID) {
|
||||
let k = bitjs.Base58.decode(floID);
|
||||
@ -14,16 +13,14 @@ module.exports = function K_Bucket(options = {}) {
|
||||
return nodeIdNewInt8Array;
|
||||
};
|
||||
|
||||
const list = options.list || Object.keys(floGlobals.supernodes);
|
||||
const refID = options.masterID || floGlobals.SNStorageID;
|
||||
const _KB = new BuildKBucket({
|
||||
localNodeId: decodeID(refID)
|
||||
localNodeId: decodeID(masterID)
|
||||
});
|
||||
list.forEach(id => _KB.add({
|
||||
nodeList.forEach(id => _KB.add({
|
||||
id: decodeID(id),
|
||||
floID: id
|
||||
}));
|
||||
const _CO = list.map(sn => [_KB.distance(decodeID(refID), decodeID(sn)), sn])
|
||||
const _CO = nodeList.map(sn => [_KB.distance(decodeID(masterID), decodeID(sn)), sn])
|
||||
.sort((a, b) => a[0] - b[0])
|
||||
.map(a => a[1]);
|
||||
|
||||
@ -94,4 +91,70 @@ module.exports = function K_Bucket(options = {}) {
|
||||
.map(k => k.floID);
|
||||
return (N == 1 ? cNodes[0] : cNodes);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const blockchainPrefix = 0x23;
|
||||
|
||||
function proxyID(address) {
|
||||
if (!address)
|
||||
return;
|
||||
var bytes;
|
||||
if (address.length == 34) { //legacy encoding
|
||||
let decode = bitjs.Base58.decode(address);
|
||||
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();
|
||||
} else if (address.length == 42 || address.length == 62) { //bech encoding
|
||||
if (typeof coinjs !== 'function')
|
||||
throw "library missing (lib_btc.js)";
|
||||
let decode = coinjs.bech32_decode(address);
|
||||
if (decode) {
|
||||
bytes = decode.data;
|
||||
bytes.shift();
|
||||
bytes = coinjs.bech32_convert(bytes, 5, 8, false);
|
||||
if (address.length == 62) //for long bech, aggregate once more to get 160 bit
|
||||
bytes = coinjs.bech32_convert(bytes, 5, 8, false);
|
||||
}
|
||||
} else if (address.length == 66) { //public key hex
|
||||
bytes = ripemd160(Crypto.SHA256(Crypto.util.hexToBytes(address), {
|
||||
asBytes: true
|
||||
}));
|
||||
}
|
||||
if (!bytes)
|
||||
throw "Invalid address: " + address;
|
||||
else {
|
||||
bytes.unshift(blockchainPrefix);
|
||||
let hash = Crypto.SHA256(Crypto.SHA256(bytes, {
|
||||
asBytes: true
|
||||
}), {
|
||||
asBytes: true
|
||||
});
|
||||
return bitjs.Base58.encode(bytes.concat(hash.slice(0, 4)));
|
||||
}
|
||||
}
|
||||
|
||||
var kBucket;
|
||||
const cloud = module.exports = function Cloud(masterID, nodeList) {
|
||||
kBucket = new K_Bucket(masterID, nodeList);
|
||||
}
|
||||
|
||||
cloud.proxyID = (a) => proxyID(a);
|
||||
cloud.closestNode = (id, N = 1) => kBucket.closestNode(proxyID(id), N);
|
||||
cloud.prevNode = (id, N = 1) => kBucket.prevNode(id, N);
|
||||
cloud.nextNode = (id, N = 1) => kBucket.nextNode(id, N);
|
||||
cloud.innerNodes = (id1, id2) => kBucket.innerNodes(id1, id2);
|
||||
cloud.outterNodes = (id1, id2) => kBucket.outterNodes(id1, id2);
|
||||
Object.defineProperties(cloud, {
|
||||
kb: {
|
||||
get: () => kBucket
|
||||
},
|
||||
order: {
|
||||
get: () => kBucket.order
|
||||
}
|
||||
});
|
||||
@ -43,6 +43,7 @@ const B_struct = {
|
||||
};
|
||||
|
||||
const L_struct = {
|
||||
PROXY_ID: "proxyID",
|
||||
STATUS: "status_n",
|
||||
LOG_TIME: "log_time"
|
||||
};
|
||||
@ -169,9 +170,9 @@ function Database(user, password, dbname, host = 'localhost') {
|
||||
db.createTable = function(snID) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let statement = "CREATE TABLE IF NOT EXISTS _" + snID + " ( " +
|
||||
H_struct.VECTOR_CLOCK + " VARCHAR(52) NOT NULL, " +
|
||||
H_struct.SENDER_ID + " CHAR(34) NOT NULL, " +
|
||||
H_struct.RECEIVER_ID + " CHAR(34) NOT NULL, " +
|
||||
H_struct.VECTOR_CLOCK + " VARCHAR(88) NOT NULL, " +
|
||||
H_struct.SENDER_ID + " VARCHAR(72) NOT NULL, " +
|
||||
H_struct.RECEIVER_ID + " VARCHAR(72) NOT NULL, " +
|
||||
H_struct.APPLICATION + " TINYTEXT NOT NULL, " +
|
||||
H_struct.TYPE + " TINYTEXT, " +
|
||||
B_struct.MESSAGE + " LONGTEXT NOT NULL, " +
|
||||
@ -179,6 +180,7 @@ function Database(user, password, dbname, host = 'localhost') {
|
||||
B_struct.SIGNATURE + " VARCHAR(160) NOT NULL, " +
|
||||
H_struct.PUB_KEY + " CHAR(66) NOT NULL, " +
|
||||
B_struct.COMMENT + " TINYTEXT, " +
|
||||
L_struct.PROXY_ID + " CHAR(34), " +
|
||||
L_struct.STATUS + " INT NOT NULL, " +
|
||||
L_struct.LOG_TIME + " BIGINT NOT NULL, " +
|
||||
T_struct.TAG + " TINYTEXT, " +
|
||||
@ -210,6 +212,8 @@ function Database(user, password, dbname, host = 'localhost') {
|
||||
return new Promise((resolve, reject) => {
|
||||
data[L_struct.STATUS] = 1;
|
||||
data[L_struct.LOG_TIME] = Date.now();
|
||||
let proxyID = cloud.proxyID(data[H_struct.RECEIVER_ID]);
|
||||
data[L_struct.PROXY_ID] = proxyID !== data[H_struct.RECEIVER_ID] ? proxyID : null;
|
||||
let attr = Object.keys(H_struct).map(a => H_struct[a])
|
||||
.concat(Object.keys(B_struct).map(a => B_struct[a]))
|
||||
.concat(Object.keys(L_struct).map(a => L_struct[a]));
|
||||
@ -292,7 +296,7 @@ function Database(user, password, dbname, host = 'localhost') {
|
||||
if (request.afterTime)
|
||||
conditionArr.push(`${L_struct.LOG_TIME} > ${request.afterTime}`);
|
||||
conditionArr.push(`${H_struct.APPLICATION} = '${request.application}'`);
|
||||
conditionArr.push(`${H_struct.RECEIVER_ID} = '${request.receiverID}'`)
|
||||
conditionArr.push(`IFNULL(${L_struct.PROXY_ID}, ${H_struct.RECEIVER_ID}) = '${request.receiverID}'`);
|
||||
if (request.comment)
|
||||
conditionArr.push(`${B_struct.COMMENT} = '${request.comment}'`);
|
||||
if (request.type)
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
329
src/floCrypto.js
329
src/floCrypto.js
@ -1,28 +1,27 @@
|
||||
'use strict';
|
||||
(function(EXPORTS) { //floCrypto v2.3.2a
|
||||
/* FLO Crypto Operators */
|
||||
'use strict';
|
||||
const floCrypto = EXPORTS;
|
||||
|
||||
(function(GLOBAL) {
|
||||
var floCrypto = GLOBAL.floCrypto = {};
|
||||
const p = BigInteger("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F", 16);
|
||||
const ecparams = EllipticCurve.getSECCurveByName("secp256k1");
|
||||
|
||||
function exponent1() {
|
||||
return p.add(BigInteger.ONE).divide(BigInteger("4"));
|
||||
};
|
||||
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"));
|
||||
|
||||
function calculateY(x) {
|
||||
let exp = exponent1();
|
||||
// x is x value of public key in BigInteger format without 02 or 03 or 04 prefix
|
||||
return x.modPow(BigInteger("3"), p).add(BigInteger("7")).mod(p).modPow(exp, p);
|
||||
};
|
||||
return x.modPow(BigInteger("3"), p).add(BigInteger("7")).mod(p).modPow(exp, p)
|
||||
}
|
||||
|
||||
function getUncompressedPublicKey(compressedPublicKey) {
|
||||
// Fetch x from compressedPublicKey
|
||||
let pubKeyBytes = Crypto.util.hexToBytes(compressedPublicKey);
|
||||
const prefix = pubKeyBytes.shift(); // remove prefix
|
||||
const prefix = pubKeyBytes.shift() // remove prefix
|
||||
let prefix_modulus = prefix % 2;
|
||||
pubKeyBytes.unshift(0); // add prefix 0
|
||||
let x = new BigInteger(pubKeyBytes);
|
||||
let xDecimalValue = x.toString();
|
||||
pubKeyBytes.unshift(0) // add prefix 0
|
||||
let x = new BigInteger(pubKeyBytes)
|
||||
let xDecimalValue = x.toString()
|
||||
// Fetch y
|
||||
let y = calculateY(x);
|
||||
let yDecimalValue = y.toString();
|
||||
@ -35,143 +34,122 @@
|
||||
x: xDecimalValue,
|
||||
y: yDecimalValue
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
function getSenderPublicKeyString() {
|
||||
privateKey = ellipticCurveEncryption.senderRandom();
|
||||
senderPublicKeyString = ellipticCurveEncryption.senderPublicString(privateKey);
|
||||
let privateKey = ellipticCurveEncryption.senderRandom();
|
||||
var senderPublicKeyString = ellipticCurveEncryption.senderPublicString(privateKey);
|
||||
return {
|
||||
privateKey: privateKey,
|
||||
senderPublicKeyString: senderPublicKeyString
|
||||
};
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function deriveSharedKeySender(receiverCompressedPublicKey, senderPrivateKey) {
|
||||
try {
|
||||
let receiverPublicKeyString = getUncompressedPublicKey(receiverCompressedPublicKey);
|
||||
var senderDerivedKey = ellipticCurveEncryption.senderSharedKeyDerivation(
|
||||
receiverPublicKeyString.x, receiverPublicKeyString.y, senderPrivateKey);
|
||||
return senderDerivedKey;
|
||||
} catch (error) {
|
||||
return new Error(error);
|
||||
};
|
||||
};
|
||||
function deriveSharedKeySender(receiverPublicKeyHex, senderPrivateKey) {
|
||||
let receiverPublicKeyString = getUncompressedPublicKey(receiverPublicKeyHex);
|
||||
var senderDerivedKey = ellipticCurveEncryption.senderSharedKeyDerivation(
|
||||
receiverPublicKeyString.x, receiverPublicKeyString.y, senderPrivateKey);
|
||||
return senderDerivedKey;
|
||||
}
|
||||
|
||||
function deriveReceiverSharedKey(senderPublicKeyString, receiverPrivateKey) {
|
||||
function deriveSharedKeyReceiver(senderPublicKeyString, receiverPrivateKey) {
|
||||
return ellipticCurveEncryption.receiverSharedKeyDerivation(
|
||||
senderPublicKeyString.XValuePublicString,
|
||||
senderPublicKeyString.YValuePublicString, receiverPrivateKey);
|
||||
};
|
||||
senderPublicKeyString.XValuePublicString, senderPublicKeyString.YValuePublicString, receiverPrivateKey);
|
||||
}
|
||||
|
||||
function getReceiverPublicKeyString(privateKey) {
|
||||
return ellipticCurveEncryption.receiverPublicString(privateKey);
|
||||
};
|
||||
}
|
||||
|
||||
function wifToDecimal(pk_wif, isPubKeyCompressed = false) {
|
||||
let pk = Bitcoin.Base58.decode(pk_wif);
|
||||
pk.shift();
|
||||
pk.splice(-4, 4);
|
||||
let pk = Bitcoin.Base58.decode(pk_wif)
|
||||
pk.shift()
|
||||
pk.splice(-4, 4)
|
||||
//If the private key corresponded to a compressed public key, also drop the last byte (it should be 0x01).
|
||||
if (isPubKeyCompressed == true) pk.pop();
|
||||
pk.unshift(0);
|
||||
privateKeyDecimal = BigInteger(pk).toString();
|
||||
privateKeyHex = Crypto.util.bytesToHex(pk);
|
||||
if (isPubKeyCompressed == true) pk.pop()
|
||||
pk.unshift(0)
|
||||
let privateKeyDecimal = BigInteger(pk).toString()
|
||||
let privateKeyHex = Crypto.util.bytesToHex(pk)
|
||||
return {
|
||||
privateKeyDecimal: privateKeyDecimal,
|
||||
privateKeyHex: privateKeyHex
|
||||
};
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
//generate a random Interger within range
|
||||
floCrypto.randInt = function(min, max) {
|
||||
min = Math.ceil(min);
|
||||
max = Math.floor(max);
|
||||
return Math.floor(Math.random() * (max - min + 1)) + min;
|
||||
};
|
||||
}
|
||||
|
||||
//generate a random String within length (options : alphaNumeric chars only)
|
||||
floCrypto.randString = function(length, alphaNumeric = true) {
|
||||
var result = '';
|
||||
if (alphaNumeric)
|
||||
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
||||
else
|
||||
var characters =
|
||||
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_+-./*?@#&$<>=[]{}():';
|
||||
var characters = alphaNumeric ? 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' :
|
||||
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_+-./*?@#&$<>=[]{}():';
|
||||
for (var i = 0; i < length; i++)
|
||||
result += characters.charAt(Math.floor(Math.random() * characters.length));
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
//Encrypt Data using public-key
|
||||
floCrypto.encryptData = function(data, publicKeyHex) {
|
||||
floCrypto.encryptData = function(data, receiverPublicKeyHex) {
|
||||
var senderECKeyData = getSenderPublicKeyString();
|
||||
var senderDerivedKey = deriveSharedKeySender(
|
||||
publicKeyHex, senderECKeyData.privateKey);
|
||||
var senderDerivedKey = deriveSharedKeySender(receiverPublicKeyHex, senderECKeyData.privateKey);
|
||||
let senderKey = senderDerivedKey.XValue + senderDerivedKey.YValue;
|
||||
let secret = Crypto.AES.encrypt(data, senderKey);
|
||||
return {
|
||||
secret: secret,
|
||||
senderPublicKeyString: senderECKeyData.senderPublicKeyString
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
//Decrypt Data using private-key
|
||||
floCrypto.decryptData = function(data, privateKeyHex) {
|
||||
var receiverECKeyData = {};
|
||||
if (typeof privateKeyHex !== "string") throw new Error("No private key found.");
|
||||
let privateKey = wifToDecimal(privateKeyHex, true);
|
||||
if (typeof privateKey.privateKeyDecimal !== "string") throw new Error(
|
||||
"Failed to detremine your private key.");
|
||||
if (typeof privateKey.privateKeyDecimal !== "string") throw new Error("Failed to detremine your private key.");
|
||||
receiverECKeyData.privateKey = privateKey.privateKeyDecimal;
|
||||
var receiverDerivedKey = deriveReceiverSharedKey(
|
||||
data.senderPublicKeyString, receiverECKeyData.privateKey);
|
||||
var receiverDerivedKey = deriveSharedKeyReceiver(data.senderPublicKeyString, receiverECKeyData.privateKey);
|
||||
let receiverKey = receiverDerivedKey.XValue + receiverDerivedKey.YValue;
|
||||
let decryptMsg = Crypto.AES.decrypt(data.secret, receiverKey);
|
||||
return decryptMsg;
|
||||
};
|
||||
}
|
||||
|
||||
//Sign data using private-key
|
||||
floCrypto.signData = function(data, privateKeyHex) {
|
||||
var key = new Bitcoin.ECKey(privateKeyHex);
|
||||
if(key.priv === null)
|
||||
return false;
|
||||
key.setCompressed(true);
|
||||
//var privateKeyArr = key.getBitcoinPrivateKeyByteArray();
|
||||
//var privateKey = BigInteger.fromByteArrayUnsigned(privateKeyArr);
|
||||
var messageHash = Crypto.SHA256(data);
|
||||
var messageHashBigInteger = new BigInteger(messageHash);
|
||||
var messageSign = Bitcoin.ECDSA.sign(messageHashBigInteger, key.priv);
|
||||
var messageSign = Bitcoin.ECDSA.sign(messageHash, key.priv);
|
||||
var sighex = Crypto.util.bytesToHex(messageSign);
|
||||
return sighex;
|
||||
};
|
||||
}
|
||||
|
||||
//Verify signatue of the data using public-key
|
||||
floCrypto.verifySign = function(data, signatureHex, publicKeyHex) {
|
||||
var msgHash = Crypto.SHA256(data);
|
||||
var messageHashBigInteger = new BigInteger(msgHash);
|
||||
var sigBytes = Crypto.util.hexToBytes(signatureHex);
|
||||
var signature = Bitcoin.ECDSA.parseSig(sigBytes);
|
||||
var publicKeyPoint = ecparams.getCurve().decodePointHex(publicKeyHex);
|
||||
var verify = Bitcoin.ECDSA.verifyRaw(messageHashBigInteger,
|
||||
signature.r, signature.s, publicKeyPoint);
|
||||
var verify = Bitcoin.ECDSA.verify(msgHash, sigBytes, publicKeyPoint);
|
||||
return verify;
|
||||
};
|
||||
}
|
||||
|
||||
//Generates a new flo ID and returns private-key, public-key and floID
|
||||
floCrypto.generateNewID = function() {
|
||||
try {
|
||||
var key = new Bitcoin.ECKey(false);
|
||||
key.setCompressed(true);
|
||||
return {
|
||||
floID: key.getBitcoinAddress(),
|
||||
pubKey: key.getPubKeyHex(),
|
||||
privKey: key.getBitcoinWalletImportFormat()
|
||||
};
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
};
|
||||
};
|
||||
const generateNewID = floCrypto.generateNewID = function() {
|
||||
var key = new Bitcoin.ECKey(false);
|
||||
key.setCompressed(true);
|
||||
return {
|
||||
floID: key.getBitcoinAddress(),
|
||||
pubKey: key.getPubKeyHex(),
|
||||
privKey: key.getBitcoinWalletImportFormat()
|
||||
}
|
||||
}
|
||||
|
||||
Object.defineProperty(floCrypto, 'newID', {
|
||||
get: () => generateNewID()
|
||||
});
|
||||
|
||||
//Returns public-key from private-key
|
||||
floCrypto.getPubKeyHex = function(privateKeyHex) {
|
||||
@ -182,7 +160,7 @@
|
||||
return null;
|
||||
key.setCompressed(true);
|
||||
return key.getPubKeyHex();
|
||||
};
|
||||
}
|
||||
|
||||
//Returns flo-ID from public-key or private-key
|
||||
floCrypto.getFloID = function(keyHex) {
|
||||
@ -193,82 +171,171 @@
|
||||
if (key.priv == null)
|
||||
key.setPub(keyHex);
|
||||
return key.getBitcoinAddress();
|
||||
} catch (e) {
|
||||
} catch {
|
||||
return null;
|
||||
};
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
//Verify the private-key for the given public-key or flo-ID
|
||||
floCrypto.verifyPrivKey = function(privateKeyHex, publicHex_ID) {
|
||||
if (!privateKeyHex || !publicHex_ID)
|
||||
floCrypto.verifyPrivKey = function(privateKeyHex, pubKey_floID, isfloID = true) {
|
||||
if (!privateKeyHex || !pubKey_floID)
|
||||
return false;
|
||||
try {
|
||||
var key = new Bitcoin.ECKey(privateKeyHex);
|
||||
if (key.priv == null)
|
||||
return false;
|
||||
key.setCompressed(true);
|
||||
if (publicHex_ID === key.getBitcoinAddress())
|
||||
if (isfloID && pubKey_floID == key.getBitcoinAddress())
|
||||
return true;
|
||||
else if (publicHex_ID === key.getPubKeyHex())
|
||||
else if (!isfloID && pubKey_floID == key.getPubKeyHex())
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
};
|
||||
};
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
//Check if the given Address is valid or not
|
||||
floCrypto.validateAddr = function(inpAddr) {
|
||||
if (!inpAddr)
|
||||
//Check if the given flo-id is valid or not
|
||||
floCrypto.validateFloID = function(floID) {
|
||||
if (!floID)
|
||||
return false;
|
||||
try {
|
||||
var addr = new Bitcoin.Address(inpAddr);
|
||||
let addr = new Bitcoin.Address(floID);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
};
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
//Check if the given address (any blockchain) is valid or not
|
||||
floCrypto.validateAddr = function(address, std = true, bech = false) {
|
||||
if (address.length == 34) { //legacy or segwit encoding
|
||||
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])))
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
} else if (address.length == 42 || address.length == 62) { //bech encoding
|
||||
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])))
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
} 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 {
|
||||
if (str.length > 0) {
|
||||
var strHex = shamirSecretShare.str2hex(str);
|
||||
return shamirSecretShare.share(strHex, total_shares, threshold_limit);
|
||||
};
|
||||
var shares = shamirSecretShare.share(strHex, total_shares, threshold_limit);
|
||||
return shares;
|
||||
}
|
||||
return false;
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
//Returns the retrived secret by combining the shamirs shares
|
||||
const retrieveShamirSecret = floCrypto.retrieveShamirSecret = function(sharesArray) {
|
||||
try {
|
||||
if (sharesArray.length > 0) {
|
||||
var comb = shamirSecretShare.combine(sharesArray.slice(0, sharesArray.length));
|
||||
comb = shamirSecretShare.hex2str(comb);
|
||||
return comb;
|
||||
}
|
||||
return false;
|
||||
} catch {
|
||||
return false;
|
||||
};
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
//Verifies the shares and str
|
||||
floCrypto.verifyShamirsSecret = function(sharesArray, str) {
|
||||
if(str == false)
|
||||
if (!str)
|
||||
return null;
|
||||
else if (retrieveShamirSecret(sharesArray) === str)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
try {
|
||||
if (sharesArray.length > 0) {
|
||||
var comb = shamirSecretShare.combine(sharesArray.slice(0, sharesArray.length));
|
||||
return (shamirSecretShare.hex2str(comb) === str ? true : false);
|
||||
};
|
||||
return false;
|
||||
} catch {
|
||||
return false;
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
//Returns the retrived secret by combining the shamirs shares
|
||||
floCrypto.retrieveShamirSecret = function(sharesArray) {
|
||||
try {
|
||||
if (sharesArray.length > 0) {
|
||||
var comb = shamirSecretShare.combine(sharesArray.slice(0, sharesArray.length));
|
||||
return shamirSecretShare.hex2str(comb);
|
||||
};
|
||||
return false;
|
||||
} catch {
|
||||
return false;
|
||||
};
|
||||
};
|
||||
const validateASCII = floCrypto.validateASCII = function(string, bool = true) {
|
||||
if (typeof string !== "string")
|
||||
return null;
|
||||
if (bool) {
|
||||
let x;
|
||||
for (let i = 0; i < string.length; i++) {
|
||||
x = string.charCodeAt(i);
|
||||
if (x < 32 || x > 127)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
let x, invalids = {};
|
||||
for (let i = 0; i < string.length; i++) {
|
||||
x = string.charCodeAt(i);
|
||||
if (x < 32 || x > 127)
|
||||
if (x in invalids)
|
||||
invalids[string[i]].push(i)
|
||||
else
|
||||
invalids[string[i]] = [i];
|
||||
}
|
||||
if (Object.keys(invalids).length)
|
||||
return invalids;
|
||||
else
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
})(typeof global !== "undefined" ? global : window);
|
||||
floCrypto.convertToASCII = function(string, mode = 'soft-remove') {
|
||||
let chars = validateASCII(string, false);
|
||||
if (chars === true)
|
||||
return string;
|
||||
else if (chars === null)
|
||||
return null;
|
||||
let convertor, result = string,
|
||||
refAlt = {};
|
||||
ascii_alternatives.split('\n').forEach(a => refAlt[a[0]] = a.slice(2));
|
||||
mode = mode.toLowerCase();
|
||||
if (mode === "hard-unicode")
|
||||
convertor = (c) => `\\u${('000'+c.charCodeAt().toString(16)).slice(-4)}`;
|
||||
else if (mode === "soft-unicode")
|
||||
convertor = (c) => refAlt[c] || `\\u${('000'+c.charCodeAt().toString(16)).slice(-4)}`;
|
||||
else if (mode === "hard-remove")
|
||||
convertor = c => "";
|
||||
else if (mode === "soft-remove")
|
||||
convertor = c => refAlt[c] || "";
|
||||
else
|
||||
return null;
|
||||
for (let c in chars)
|
||||
result = result.replaceAll(c, convertor(c));
|
||||
return result;
|
||||
}
|
||||
|
||||
floCrypto.revertUnicode = function(string) {
|
||||
return string.replace(/\\u[\dA-F]{4}/gi,
|
||||
m => String.fromCharCode(parseInt(m.replace(/\\u/g, ''), 16)));
|
||||
}
|
||||
|
||||
})('object' === typeof module ? module.exports : window.floCrypto = {});
|
||||
@ -3,14 +3,7 @@ const floGlobals = {
|
||||
//Required for all
|
||||
blockchain: "FLO",
|
||||
|
||||
//Required for blockchain API operators
|
||||
apiURL: {
|
||||
FLO: ['https://explorer.mediciland.com/', 'https://livenet.flocha.in/', 'https://flosight.duckdns.org/', 'http://livenet-explorer.floexperiments.com'],
|
||||
FLO_TEST: ['https://testnet-flosight.duckdns.org', 'https://testnet.flocha.in/']
|
||||
},
|
||||
SNStorageID: "FNaN9McoBAEFUjkRmNQRYLmBF8SpS7Tgfk",
|
||||
//sendAmt: 0.001,
|
||||
//fee: 0.0005,
|
||||
|
||||
//Required for Supernode operations
|
||||
supernodes: {}, //each supnernode must be stored as floID : {uri:<uri>,pubKey:<publicKey>}
|
||||
|
||||
28
src/intra.js
28
src/intra.js
@ -177,7 +177,7 @@ function connectToActiveNode(snID, reverse = false) {
|
||||
connectToNode(snID)
|
||||
.then(ws => resolve(ws))
|
||||
.catch(error => {
|
||||
var next = (reverse ? kBucket.prevNode(snID) : kBucket.nextNode(snID));
|
||||
var next = (reverse ? cloud.prevNode(snID) : cloud.nextNode(snID));
|
||||
connectToActiveNode(next, reverse)
|
||||
.then(ws => resolve(ws))
|
||||
.catch(error => reject(error));
|
||||
@ -190,7 +190,7 @@ function connectToNextNode(curNode = myFloID) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (curNode === myFloID && !(myFloID in floGlobals.supernodes))
|
||||
return reject(`This (${myFloID}) is not a supernode`);
|
||||
let nextNodeID = kBucket.nextNode(curNode);
|
||||
let nextNodeID = cloud.nextNode(curNode);
|
||||
if (nextNodeID === myFloID)
|
||||
return reject("No other node online");
|
||||
connectToNode(nextNodeID).then(ws => {
|
||||
@ -336,7 +336,7 @@ function processTaskFromSupernode(packet, ws) {
|
||||
//Acknowledge handshake
|
||||
function handshakeMid(id, ws) {
|
||||
if (_prevNode.id && _prevNode.id in floGlobals.supernodes) {
|
||||
if (kBucket.innerNodes(_prevNode.id, myFloID).includes(id)) {
|
||||
if (cloud.innerNodes(_prevNode.id, myFloID).includes(id)) {
|
||||
//close existing prev-node connection
|
||||
_prevNode.send(packet_.construct({
|
||||
type: RECONNECT_NEXT_NODE
|
||||
@ -364,7 +364,7 @@ function handshakeMid(id, ws) {
|
||||
if (!_nextNode.id)
|
||||
reconnectNextNode();
|
||||
//Reorder storelist
|
||||
let nodes = kBucket.innerNodes(_prevNode.id, myFloID).concat(myFloID),
|
||||
let nodes = cloud.innerNodes(_prevNode.id, myFloID).concat(myFloID),
|
||||
req_sync = [],
|
||||
new_order = [];
|
||||
nodes.forEach(n => {
|
||||
@ -456,7 +456,7 @@ function reconnectNextNode() {
|
||||
function orderBackup(order) {
|
||||
let new_order = [],
|
||||
req_sync = [];
|
||||
let cur_serve = kBucket.innerNodes(_prevNode.id, myFloID).concat(myFloID);
|
||||
let cur_serve = cloud.innerNodes(_prevNode.id, myFloID).concat(myFloID);
|
||||
for (let n in order) {
|
||||
if (!cur_serve.includes(n) && order[n] + 1 !== _list[n] && n in floGlobals.supernodes) {
|
||||
if (order[n] >= floGlobals.sn_config.backupDepth)
|
||||
@ -546,7 +546,7 @@ function dataSyncIndication(snID, status, from) {
|
||||
|
||||
//Store (backup) data
|
||||
function storeBackupData(data, from, packet) {
|
||||
let closestNode = kBucket.closestNode(data.receiverID);
|
||||
let closestNode = cloud.closestNode(data.receiverID);
|
||||
if (_list.stored.includes(closestNode)) {
|
||||
DB.storeData(closestNode, data).then(_ => null).catch(e => console.error(e));
|
||||
if (_list[closestNode] < floGlobals.sn_config.backupDepth && _nextNode.id !== from)
|
||||
@ -556,7 +556,7 @@ function storeBackupData(data, from, packet) {
|
||||
|
||||
//Tag (backup) data
|
||||
function tagBackupData(data, from, packet) {
|
||||
let closestNode = kBucket.closestNode(data.receiverID);
|
||||
let closestNode = cloud.closestNode(data.receiverID);
|
||||
if (_list.stored.includes(closestNode)) {
|
||||
DB.storeTag(closestNode, data).then(_ => null).catch(e => console.error(e));
|
||||
if (_list[closestNode] < floGlobals.sn_config.backupDepth && _nextNode.id !== from)
|
||||
@ -566,7 +566,7 @@ function tagBackupData(data, from, packet) {
|
||||
|
||||
//Note (backup) data
|
||||
function noteBackupData(data, from, packet) {
|
||||
let closestNode = kBucket.closestNode(data.receiverID);
|
||||
let closestNode = cloud.closestNode(data.receiverID);
|
||||
if (_list.stored.includes(closestNode)) {
|
||||
DB.storeNote(closestNode, data).then(_ => null).catch(e => console.error(e));
|
||||
if (_list[closestNode] < floGlobals.sn_config.backupDepth && _nextNode.id !== from)
|
||||
@ -576,7 +576,7 @@ function noteBackupData(data, from, packet) {
|
||||
|
||||
//Store (migrated) data
|
||||
function storeMigratedData(data) {
|
||||
let closestNode = kBucket.closestNode(data.receiverID);
|
||||
let closestNode = cloud.closestNode(data.receiverID);
|
||||
if (_list.serving.includes(closestNode)) {
|
||||
DB.storeData(closestNode, data, true).then(_ => null).catch(e => console.error(e));
|
||||
_nextNode.send(packet_.construct({
|
||||
@ -588,7 +588,7 @@ function storeMigratedData(data) {
|
||||
|
||||
//Delete (migrated) data
|
||||
function deleteMigratedData(data, from, packet) {
|
||||
let closestNode = kBucket.closestNode(data.receiverID);
|
||||
let closestNode = cloud.closestNode(data.receiverID);
|
||||
if (data.snID !== closestNode && _list.stored.includes(data.snID)) {
|
||||
DB.deleteData(data.snID, data.vectorClock).then(_ => null).catch(e => console.error(e));
|
||||
if (_list[data.snID] < floGlobals.sn_config.backupDepth && _nextNode.id !== from)
|
||||
@ -626,14 +626,14 @@ function dataMigration(node_change, flag) {
|
||||
(node_change[n] ? new_nodes : del_nodes).push(n);
|
||||
if (_prevNode.id && del_nodes.includes(_prevNode.id))
|
||||
_prevNode.close();
|
||||
const old_kb = kBucket;
|
||||
const old_kb = cloud.kb;
|
||||
setTimeout(() => {
|
||||
//reconnect next node if current next node is deleted
|
||||
if (_nextNode.id) {
|
||||
if (del_nodes.includes(_nextNode.id))
|
||||
reconnectNextNode();
|
||||
else { //reconnect next node if there are newly added nodes in between self and current next node
|
||||
let innerNodes = kBucket.innerNodes(myFloID, _nextNode.id);
|
||||
let innerNodes = cloud.innerNodes(myFloID, _nextNode.id);
|
||||
if (new_nodes.filter(n => innerNodes.includes(n)).length)
|
||||
reconnectNextNode();
|
||||
};
|
||||
@ -667,7 +667,7 @@ dataMigration.process_del = async function(del_nodes, old_kb) {
|
||||
console.log(`START: Data migration for ${n}`);
|
||||
//TODO: efficiently handle large number of data instead of loading all into memory
|
||||
result.forEach(d => {
|
||||
let closest = kBucket.closestNode(d.receiverID);
|
||||
let closest = cloud.closestNode(d.receiverID);
|
||||
if (_list.serving.includes(closest)) {
|
||||
DB.storeData(closest, d, true).then(_ => null).catch(e => console.error(e));
|
||||
if (_nextNode.id)
|
||||
@ -716,7 +716,7 @@ dataMigration.process_new = async function(new_nodes) {
|
||||
DB.readAllData(n, 0).then(result => {
|
||||
//TODO: efficiently handle large number of data instead of loading all into memory
|
||||
result.forEach(d => {
|
||||
let closest = kBucket.closestNode(d.receiverID);
|
||||
let closest = cloud.closestNode(d.receiverID);
|
||||
if (new_nodes.includes(closest)) {
|
||||
if (_list.serving.includes(closest)) {
|
||||
DB.storeData(closest, d, true).then(_ => null).catch(e => console.error(e));
|
||||
|
||||
15097
src/lib.js
15097
src/lib.js
File diff suppressed because it is too large
Load Diff
@ -1,405 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
/*Kademlia DHT K-bucket implementation as a binary tree.*/
|
||||
(function(GLOBAL) {
|
||||
/**
|
||||
* Implementation of a Kademlia DHT k-bucket used for storing
|
||||
* contact (peer node) information.
|
||||
*
|
||||
* @extends EventEmitter
|
||||
*/
|
||||
GLOBAL.BuildKBucket = function BuildKBucket(options = {}) {
|
||||
/**
|
||||
* `options`:
|
||||
* `distance`: Function
|
||||
* `function (firstId, secondId) { return distance }` An optional
|
||||
* `distance` function that gets two `id` Uint8Arrays
|
||||
* and return distance (as number) between them.
|
||||
* `arbiter`: Function (Default: vectorClock arbiter)
|
||||
* `function (incumbent, candidate) { return contact; }` An optional
|
||||
* `arbiter` function that givent two `contact` objects with the same `id`
|
||||
* returns the desired object to be used for updating the k-bucket. For
|
||||
* more details, see [arbiter function](#arbiter-function).
|
||||
* `localNodeId`: Uint8Array An optional Uint8Array representing the local node id.
|
||||
* If not provided, a local node id will be created via `randomBytes(20)`.
|
||||
* `metadata`: Object (Default: {}) Optional satellite data to include
|
||||
* with the k-bucket. `metadata` property is guaranteed not be altered by,
|
||||
* it is provided as an explicit container for users of k-bucket to store
|
||||
* implementation-specific data.
|
||||
* `numberOfNodesPerKBucket`: Integer (Default: 20) The number of nodes
|
||||
* that a k-bucket can contain before being full or split.
|
||||
* `numberOfNodesToPing`: Integer (Default: 3) The number of nodes to
|
||||
* ping when a bucket that should not be split becomes full. KBucket will
|
||||
* emit a `ping` event that contains `numberOfNodesToPing` nodes that have
|
||||
* not been contacted the longest.
|
||||
*
|
||||
* @param {Object=} options optional
|
||||
*/
|
||||
|
||||
this.localNodeId = options.localNodeId || window.crypto.getRandomValues(new Uint8Array(20))
|
||||
this.numberOfNodesPerKBucket = options.numberOfNodesPerKBucket || 20
|
||||
this.numberOfNodesToPing = options.numberOfNodesToPing || 3
|
||||
this.distance = options.distance || this.distance
|
||||
// use an arbiter from options or vectorClock arbiter by default
|
||||
this.arbiter = options.arbiter || this.arbiter
|
||||
this.metadata = Object.assign({}, options.metadata)
|
||||
|
||||
this.createNode = function() {
|
||||
return {
|
||||
contacts: [],
|
||||
dontSplit: false,
|
||||
left: null,
|
||||
right: null
|
||||
}
|
||||
}
|
||||
|
||||
this.ensureInt8 = function(name, val) {
|
||||
if (!(val instanceof Uint8Array)) {
|
||||
throw new TypeError(name + ' is not a Uint8Array')
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Uint8Array} array1
|
||||
* @param {Uint8Array} array2
|
||||
* @return {Boolean}
|
||||
*/
|
||||
this.arrayEquals = function(array1, array2) {
|
||||
if (array1 === array2) {
|
||||
return true
|
||||
}
|
||||
if (array1.length !== array2.length) {
|
||||
return false
|
||||
}
|
||||
for (let i = 0, length = array1.length; i < length; ++i) {
|
||||
if (array1[i] !== array2[i]) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
this.ensureInt8('option.localNodeId as parameter 1', this.localNodeId)
|
||||
this.root = this.createNode()
|
||||
|
||||
/**
|
||||
* Default arbiter function for contacts with the same id. Uses
|
||||
* contact.vectorClock to select which contact to update the k-bucket with.
|
||||
* Contact with larger vectorClock field will be selected. If vectorClock is
|
||||
* the same, candidat will be selected.
|
||||
*
|
||||
* @param {Object} incumbent Contact currently stored in the k-bucket.
|
||||
* @param {Object} candidate Contact being added to the k-bucket.
|
||||
* @return {Object} Contact to updated the k-bucket with.
|
||||
*/
|
||||
this.arbiter = function(incumbent, candidate) {
|
||||
return incumbent.vectorClock > candidate.vectorClock ? incumbent : candidate
|
||||
}
|
||||
|
||||
/**
|
||||
* Default distance function. Finds the XOR
|
||||
* distance between firstId and secondId.
|
||||
*
|
||||
* @param {Uint8Array} firstId Uint8Array containing first id.
|
||||
* @param {Uint8Array} secondId Uint8Array containing second id.
|
||||
* @return {Number} Integer The XOR distance between firstId
|
||||
* and secondId.
|
||||
*/
|
||||
this.distance = function(firstId, secondId) {
|
||||
let distance = 0
|
||||
let i = 0
|
||||
const min = Math.min(firstId.length, secondId.length)
|
||||
const max = Math.max(firstId.length, secondId.length)
|
||||
for (; i < min; ++i) {
|
||||
distance = distance * 256 + (firstId[i] ^ secondId[i])
|
||||
}
|
||||
for (; i < max; ++i) distance = distance * 256 + 255
|
||||
return distance
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a contact to the k-bucket.
|
||||
*
|
||||
* @param {Object} contact the contact object to add
|
||||
*/
|
||||
this.add = function(contact) {
|
||||
this.ensureInt8('contact.id', (contact || {}).id)
|
||||
|
||||
let bitIndex = 0
|
||||
let node = this.root
|
||||
|
||||
while (node.contacts === null) {
|
||||
// this is not a leaf node but an inner node with 'low' and 'high'
|
||||
// branches; we will check the appropriate bit of the identifier and
|
||||
// delegate to the appropriate node for further processing
|
||||
node = this._determineNode(node, contact.id, bitIndex++)
|
||||
}
|
||||
|
||||
// check if the contact already exists
|
||||
const index = this._indexOf(node, contact.id)
|
||||
if (index >= 0) {
|
||||
this._update(node, index, contact)
|
||||
return this
|
||||
}
|
||||
|
||||
if (node.contacts.length < this.numberOfNodesPerKBucket) {
|
||||
node.contacts.push(contact)
|
||||
return this
|
||||
}
|
||||
|
||||
// the bucket is full
|
||||
if (node.dontSplit) {
|
||||
// we are not allowed to split the bucket
|
||||
// we need to ping the first this.numberOfNodesToPing
|
||||
// in order to determine if they are alive
|
||||
// only if one of the pinged nodes does not respond, can the new contact
|
||||
// be added (this prevents DoS flodding with new invalid contacts)
|
||||
return this
|
||||
}
|
||||
|
||||
this._split(node, bitIndex)
|
||||
return this.add(contact)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the n closest contacts to the provided node id. "Closest" here means:
|
||||
* closest according to the XOR metric of the contact node id.
|
||||
*
|
||||
* @param {Uint8Array} id Contact node id
|
||||
* @param {Number=} n Integer (Default: Infinity) The maximum number of
|
||||
* closest contacts to return
|
||||
* @return {Array} Array Maximum of n closest contacts to the node id
|
||||
*/
|
||||
this.closest = function(id, n = Infinity) {
|
||||
this.ensureInt8('id', id)
|
||||
|
||||
if ((!Number.isInteger(n) && n !== Infinity) || n <= 0) {
|
||||
throw new TypeError('n is not positive number')
|
||||
}
|
||||
|
||||
let contacts = []
|
||||
|
||||
for (let nodes = [this.root], bitIndex = 0; nodes.length > 0 && contacts.length < n;) {
|
||||
const node = nodes.pop()
|
||||
if (node.contacts === null) {
|
||||
const detNode = this._determineNode(node, id, bitIndex++)
|
||||
nodes.push(node.left === detNode ? node.right : node.left)
|
||||
nodes.push(detNode)
|
||||
} else {
|
||||
contacts = contacts.concat(node.contacts)
|
||||
}
|
||||
}
|
||||
|
||||
return contacts
|
||||
.map(a => [this.distance(a.id, id), a])
|
||||
.sort((a, b) => a[0] - b[0])
|
||||
.slice(0, n)
|
||||
.map(a => a[1])
|
||||
}
|
||||
|
||||
/**
|
||||
* Counts the total number of contacts in the tree.
|
||||
*
|
||||
* @return {Number} The number of contacts held in the tree
|
||||
*/
|
||||
this.count = function() {
|
||||
// return this.toArray().length
|
||||
let count = 0
|
||||
for (const nodes = [this.root]; nodes.length > 0;) {
|
||||
const node = nodes.pop()
|
||||
if (node.contacts === null) nodes.push(node.right, node.left)
|
||||
else count += node.contacts.length
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether the id at the bitIndex is 0 or 1.
|
||||
* Return left leaf if `id` at `bitIndex` is 0, right leaf otherwise
|
||||
*
|
||||
* @param {Object} node internal object that has 2 leafs: left and right
|
||||
* @param {Uint8Array} id Id to compare localNodeId with.
|
||||
* @param {Number} bitIndex Integer (Default: 0) The bit index to which bit
|
||||
* to check in the id Uint8Array.
|
||||
* @return {Object} left leaf if id at bitIndex is 0, right leaf otherwise.
|
||||
*/
|
||||
this._determineNode = function(node, id, bitIndex) {
|
||||
// *NOTE* remember that id is a Uint8Array and has granularity of
|
||||
// bytes (8 bits), whereas the bitIndex is the bit index (not byte)
|
||||
|
||||
// id's that are too short are put in low bucket (1 byte = 8 bits)
|
||||
// (bitIndex >> 3) finds how many bytes the bitIndex describes
|
||||
// bitIndex % 8 checks if we have extra bits beyond byte multiples
|
||||
// if number of bytes is <= no. of bytes described by bitIndex and there
|
||||
// are extra bits to consider, this means id has less bits than what
|
||||
// bitIndex describes, id therefore is too short, and will be put in low
|
||||
// bucket
|
||||
const bytesDescribedByBitIndex = bitIndex >> 3
|
||||
const bitIndexWithinByte = bitIndex % 8
|
||||
if ((id.length <= bytesDescribedByBitIndex) && (bitIndexWithinByte !== 0)) {
|
||||
return node.left
|
||||
}
|
||||
|
||||
const byteUnderConsideration = id[bytesDescribedByBitIndex]
|
||||
|
||||
// byteUnderConsideration is an integer from 0 to 255 represented by 8 bits
|
||||
// where 255 is 11111111 and 0 is 00000000
|
||||
// in order to find out whether the bit at bitIndexWithinByte is set
|
||||
// we construct (1 << (7 - bitIndexWithinByte)) which will consist
|
||||
// of all bits being 0, with only one bit set to 1
|
||||
// for example, if bitIndexWithinByte is 3, we will construct 00010000 by
|
||||
// (1 << (7 - 3)) -> (1 << 4) -> 16
|
||||
if (byteUnderConsideration & (1 << (7 - bitIndexWithinByte))) {
|
||||
return node.right
|
||||
}
|
||||
|
||||
return node.left
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a contact by its exact ID.
|
||||
* If this is a leaf, loop through the bucket contents and return the correct
|
||||
* contact if we have it or null if not. If this is an inner node, determine
|
||||
* which branch of the tree to traverse and repeat.
|
||||
*
|
||||
* @param {Uint8Array} id The ID of the contact to fetch.
|
||||
* @return {Object|Null} The contact if available, otherwise null
|
||||
*/
|
||||
this.get = function(id) {
|
||||
this.ensureInt8('id', id)
|
||||
|
||||
let bitIndex = 0
|
||||
|
||||
let node = this.root
|
||||
while (node.contacts === null) {
|
||||
node = this._determineNode(node, id, bitIndex++)
|
||||
}
|
||||
|
||||
// index of uses contact id for matching
|
||||
const index = this._indexOf(node, id)
|
||||
return index >= 0 ? node.contacts[index] : null
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the index of the contact with provided
|
||||
* id if it exists, returns -1 otherwise.
|
||||
*
|
||||
* @param {Object} node internal object that has 2 leafs: left and right
|
||||
* @param {Uint8Array} id Contact node id.
|
||||
* @return {Number} Integer Index of contact with provided id if it
|
||||
* exists, -1 otherwise.
|
||||
*/
|
||||
this._indexOf = function(node, id) {
|
||||
for (let i = 0; i < node.contacts.length; ++i) {
|
||||
if (this.arrayEquals(node.contacts[i].id, id)) return i
|
||||
}
|
||||
|
||||
return -1
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes contact with the provided id.
|
||||
*
|
||||
* @param {Uint8Array} id The ID of the contact to remove.
|
||||
* @return {Object} The k-bucket itself.
|
||||
*/
|
||||
this.remove = function(id) {
|
||||
this.ensureInt8('the id as parameter 1', id)
|
||||
|
||||
let bitIndex = 0
|
||||
let node = this.root
|
||||
|
||||
while (node.contacts === null) {
|
||||
node = this._determineNode(node, id, bitIndex++)
|
||||
}
|
||||
|
||||
const index = this._indexOf(node, id)
|
||||
if (index >= 0) {
|
||||
const contact = node.contacts.splice(index, 1)[0]
|
||||
}
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
* Splits the node, redistributes contacts to the new nodes, and marks the
|
||||
* node that was split as an inner node of the binary tree of nodes by
|
||||
* setting this.root.contacts = null
|
||||
*
|
||||
* @param {Object} node node for splitting
|
||||
* @param {Number} bitIndex the bitIndex to which byte to check in the
|
||||
* Uint8Array for navigating the binary tree
|
||||
*/
|
||||
this._split = function(node, bitIndex) {
|
||||
node.left = this.createNode()
|
||||
node.right = this.createNode()
|
||||
|
||||
// redistribute existing contacts amongst the two newly created nodes
|
||||
for (const contact of node.contacts) {
|
||||
this._determineNode(node, contact.id, bitIndex).contacts.push(contact)
|
||||
}
|
||||
|
||||
node.contacts = null // mark as inner tree node
|
||||
|
||||
// don't split the "far away" node
|
||||
// we check where the local node would end up and mark the other one as
|
||||
// "dontSplit" (i.e. "far away")
|
||||
const detNode = this._determineNode(node, this.localNodeId, bitIndex)
|
||||
const otherNode = node.left === detNode ? node.right : node.left
|
||||
otherNode.dontSplit = true
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all the contacts contained in the tree as an array.
|
||||
* If this is a leaf, return a copy of the bucket. `slice` is used so that we
|
||||
* don't accidentally leak an internal reference out that might be
|
||||
* accidentally misused. If this is not a leaf, return the union of the low
|
||||
* and high branches (themselves also as arrays).
|
||||
*
|
||||
* @return {Array} All of the contacts in the tree, as an array
|
||||
*/
|
||||
this.toArray = function() {
|
||||
let result = []
|
||||
for (const nodes = [this.root]; nodes.length > 0;) {
|
||||
const node = nodes.pop()
|
||||
if (node.contacts === null) nodes.push(node.right, node.left)
|
||||
else result = result.concat(node.contacts)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the contact selected by the arbiter.
|
||||
* If the selection is our old contact and the candidate is some new contact
|
||||
* then the new contact is abandoned (not added).
|
||||
* If the selection is our old contact and the candidate is our old contact
|
||||
* then we are refreshing the contact and it is marked as most recently
|
||||
* contacted (by being moved to the right/end of the bucket array).
|
||||
* If the selection is our new contact, the old contact is removed and the new
|
||||
* contact is marked as most recently contacted.
|
||||
*
|
||||
* @param {Object} node internal object that has 2 leafs: left and right
|
||||
* @param {Number} index the index in the bucket where contact exists
|
||||
* (index has already been computed in a previous
|
||||
* calculation)
|
||||
* @param {Object} contact The contact object to update.
|
||||
*/
|
||||
this._update = function(node, index, contact) {
|
||||
// sanity check
|
||||
if (!this.arrayEquals(node.contacts[index].id, contact.id)) {
|
||||
throw new Error('wrong index for _update')
|
||||
}
|
||||
|
||||
const incumbent = node.contacts[index]
|
||||
const selection = this.arbiter(incumbent, contact)
|
||||
// if the selection is our old contact and the candidate is some new
|
||||
// contact, then there is nothing to do
|
||||
if (selection === incumbent && incumbent !== contact) return
|
||||
|
||||
node.contacts.splice(index, 1) // remove old contact
|
||||
node.contacts.push(selection) // add more recent contact version
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
})(typeof global !== "undefined" ? global : window)
|
||||
12
src/main.js
12
src/main.js
@ -2,9 +2,9 @@ const config = require('../args/config.json');
|
||||
global.floGlobals = require("./floGlobals");
|
||||
require('./set_globals');
|
||||
require('./lib');
|
||||
const K_Bucket = require('./kBucket');
|
||||
require('./floCrypto');
|
||||
require('./floBlockchainAPI');
|
||||
global.cloud = require('./cloud');
|
||||
global.floCrypto = require('./floCrypto');
|
||||
global.floBlockchainAPI = require('./floBlockchainAPI');
|
||||
const Database = require("./database");
|
||||
const intra = require('./intra');
|
||||
const client = require('./client');
|
||||
@ -96,8 +96,8 @@ function refreshBlockchainData(base, flag) {
|
||||
return new Promise((resolve, reject) => {
|
||||
readSupernodeConfigFromAPI(base, flag).then(result => {
|
||||
console.log(result);
|
||||
global.kBucket = new K_Bucket();
|
||||
console.log("SNCO:", kBucket.order);
|
||||
cloud(floGlobals.SNStorageID, Object.keys(floGlobals.supernodes));
|
||||
console.log("NodeList (ordered):", cloud.order);
|
||||
readAppSubAdminListFromAPI(base)
|
||||
.then(result => console.log(result))
|
||||
.catch(warn => console.warn(warn))
|
||||
@ -252,7 +252,7 @@ function selfDiskMigration(node_change) {
|
||||
DB.dropTable(n).then(_ => null).catch(e => console.error(e));
|
||||
DB.readAllData(n, 0).then(result => {
|
||||
result.forEach(d => {
|
||||
let closest = kBucket.closestNode(d.receiverID);
|
||||
let closest = cloud.closestNode(d.receiverID);
|
||||
if (closest !== n)
|
||||
DB.deleteData(n, d.vectorClock).then(_ => null).catch(e => console.error(e));
|
||||
});
|
||||
|
||||
Loading…
Reference in New Issue
Block a user