Support to accept addresses from other blockchain

This commit is contained in:
sairajzero 2022-07-26 03:17:28 +05:30
parent 39ca525822
commit d6e30c6470
4 changed files with 63 additions and 14 deletions

View File

@ -48,12 +48,12 @@ function processIncomingData(data) {
function processDataFromUser(data) {
return new Promise((resolve, reject) => {
if (!floCrypto.validateFloID(data.receiverID))
if (!floCrypto.validateAddr(data.receiverID))
return reject(INVALID("Invalid receiverID"));
let closeNode = cloud.closestNode(data.receiverID);
if (!_list.serving.includes(closeNode))
return reject(INVALID("Incorrect Supernode"));
if (!floCrypto.validateFloID(data.senderID))
if (!floCrypto.validateAddr(data.senderID))
return reject(INVALID("Invalid senderID"));
if (data.senderID !== floCrypto.getFloID(data.pubKey))
return reject(INVALID("Invalid pubKey"));
@ -83,7 +83,7 @@ function processDataFromUser(data) {
function processRequestFromUser(request) {
return new Promise((resolve, reject) => {
if (!floCrypto.validateFloID(request.receiverID))
if (!floCrypto.validateAddr(request.receiverID))
return reject(INVALID("Invalid receiverID"));
let closeNode = cloud.closestNode(request.receiverID);
if (!_list.serving.includes(closeNode))
@ -96,7 +96,7 @@ function processRequestFromUser(request) {
function processTagFromUser(data) {
return new Promise((resolve, reject) => {
if (!floCrypto.validateFloID(data.receiverID))
if (!floCrypto.validateAddr(data.receiverID))
return reject(INVALID("Invalid receiverID"));
let closeNode = cloud.closestNode(data.receiverID);
if (!_list.serving.includes(closeNode))
@ -107,7 +107,7 @@ function processTagFromUser(data) {
result = result[0];
if (!(result.application in floGlobals.appList))
return reject(INVALID("Application not authorised"));
if (!floCrypto.validateFloID(data.requestorID) ||
if (!floCrypto.validateAddr(data.requestorID) ||
!floGlobals.appSubAdmins[result.application].includes(data.requestorID))
return reject(INVALID("Invalid requestorID"));
if (data.requestorID !== floCrypto.getFloID(data.pubKey))
@ -127,7 +127,7 @@ function processTagFromUser(data) {
function processNoteFromUser(data) {
return new Promise((resolve, reject) => {
if (!floCrypto.validateFloID(data.receiverID))
if (!floCrypto.validateAddr(data.receiverID))
return reject(INVALID("Invalid receiverID"));
let closeNode = cloud.closestNode(data.receiverID);
if (!_list.serving.includes(closeNode))

View File

@ -93,12 +93,59 @@ function K_Bucket(masterID, nodeList) {
};
}
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.closestNode = (id, N = 1) => kBucket.closestNode(id, N);
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);

View File

@ -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)

View File

@ -1,4 +1,4 @@
(function(EXPORTS) { //floCrypto v2.3.2
(function(EXPORTS) { //floCrypto v2.3.2a
/* FLO Crypto Operators */
'use strict';
const floCrypto = EXPORTS;
@ -230,8 +230,6 @@
} else if (address.length == 42 || address.length == 62) { //bech encoding
if (bech === false)
return false;
else if (typeof btc_api !== "object")
throw "btc_api library missing (lib_btc.js)";
let decode = coinjs.bech32_decode(address);
if (!decode)
return false;