commit
61f9a03acc
733
css/main.css
733
css/main.css
File diff suppressed because it is too large
Load Diff
2
css/main.min.css
vendored
2
css/main.min.css
vendored
File diff suppressed because one or more lines are too long
1498
css/main.scss
1498
css/main.scss
File diff suppressed because it is too large
Load Diff
12044
index.html
12044
index.html
File diff suppressed because it is too large
Load Diff
262
scripts/bobs-fund.js
Normal file
262
scripts/bobs-fund.js
Normal file
@ -0,0 +1,262 @@
|
|||||||
|
const bobsFund = (function () {
|
||||||
|
const productStr = "Bobs Fund";
|
||||||
|
|
||||||
|
const magnitude = m => {
|
||||||
|
switch (m) {
|
||||||
|
case "thousand": return 1000;
|
||||||
|
case "lakh": case "lakhs": return 100000;
|
||||||
|
case "million": return 1000000;
|
||||||
|
case "crore": case "crores": return 10000000;
|
||||||
|
default: return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const parseNumber = (str) => {
|
||||||
|
let n = 0,
|
||||||
|
g = 0;
|
||||||
|
str.toLowerCase().replace(/,/g, '').split(" ").forEach(s => {
|
||||||
|
if (!isNaN(s))
|
||||||
|
g = parseFloat(s);
|
||||||
|
else {
|
||||||
|
let m = magnitude(s);
|
||||||
|
if (m !== null) {
|
||||||
|
n += m * g;
|
||||||
|
g = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return n + g;
|
||||||
|
}
|
||||||
|
const parsePeriod = (str) => {
|
||||||
|
let P = '', n = 0;
|
||||||
|
str.toLowerCase().replace(/,/g, '').split(" ").forEach(s => {
|
||||||
|
if (!isNaN(s))
|
||||||
|
n = parseFloat(s);
|
||||||
|
else switch (s) {
|
||||||
|
case "year(s)": case "year": case "years": P += (n + 'Y'); n = 0; break;
|
||||||
|
case "month(s)": case "month": case "months": P += (n + 'M'); n = 0; break;
|
||||||
|
case "day(s)": case "day": case "days": P += (n + 'D'); n = 0; break;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return P;
|
||||||
|
}
|
||||||
|
const dateFormat = (date = null) => {
|
||||||
|
let d = (date ? new Date(date) : new Date()).toDateString();
|
||||||
|
return [d.substring(8, 10), d.substring(4, 7), d.substring(11, 15)].join(" ");
|
||||||
|
}
|
||||||
|
|
||||||
|
const dateAdder = function (start_date, duration) {
|
||||||
|
let date = new Date(start_date);
|
||||||
|
let y = parseInt(duration.match(/\d+Y/)),
|
||||||
|
m = parseInt(duration.match(/\d+M/)),
|
||||||
|
d = parseInt(duration.match(/\d+D/));
|
||||||
|
if (!isNaN(y))
|
||||||
|
date.setFullYear(date.getFullYear() + y);
|
||||||
|
if (!isNaN(m))
|
||||||
|
date.setMonth(date.getMonth() + m);
|
||||||
|
if (!isNaN(d))
|
||||||
|
date.setDate(date.getDate() + d);
|
||||||
|
return date;
|
||||||
|
}
|
||||||
|
|
||||||
|
function calcNetValue(BTC_base, BTC_net, USD_base, USD_net, amount, fee) {
|
||||||
|
let gain, interest, net;
|
||||||
|
gain = (BTC_net - BTC_base) / BTC_base;
|
||||||
|
interest = gain * (1 - fee)
|
||||||
|
net = amount / USD_base;
|
||||||
|
net += net * interest;
|
||||||
|
return net * USD_net;
|
||||||
|
}
|
||||||
|
|
||||||
|
function stringify_main(BTC_base, USD_base, start_date, duration, investments, fee = 0, tapoutWindow = null, tapoutInterval = null) {
|
||||||
|
let result = [
|
||||||
|
`${productStr}`,
|
||||||
|
`Base Value: ${BTC_base} USD`,
|
||||||
|
`USD INR rate at start: ${USD_base}`,
|
||||||
|
`Start date: ${dateFormat(start_date)}`,
|
||||||
|
`Duration: ${duration}`,
|
||||||
|
`Management Fee: ${fee != 0 ? fee + "%" : "0 (Zero)"}`
|
||||||
|
];
|
||||||
|
if (tapoutInterval) {
|
||||||
|
if (Array.isArray(tapoutInterval)) {
|
||||||
|
let x = tapoutInterval.pop(),
|
||||||
|
y = tapoutInterval.join(", ")
|
||||||
|
tapoutInterval = `${y} and ${x}`
|
||||||
|
}
|
||||||
|
result.push(`Tapout availability: ${tapoutWindow} after ${tapoutInterval}`);
|
||||||
|
}
|
||||||
|
result.push(`Investment(s) (INR): ${investments.map(f => `${f[0].trim()}-${f[1].trim()}`).join("; ")}`);
|
||||||
|
return result.join("|");
|
||||||
|
}
|
||||||
|
|
||||||
|
function stringify_continue(fund_id, investments) {
|
||||||
|
return [
|
||||||
|
`${productStr}`,
|
||||||
|
`continue: ${fund_id}`,
|
||||||
|
`Investment(s) (INR): ${investments.map(f => `${f[0].trim()}-${f[1].trim()}`).join("; ")}`
|
||||||
|
].join("|");
|
||||||
|
}
|
||||||
|
|
||||||
|
function stringify_end(fund_id, floID, end_date, BTC_net, USD_net, amount, ref_sign, payment_ref) {
|
||||||
|
return [
|
||||||
|
`${productStr}`,
|
||||||
|
`close: ${fund_id}`,
|
||||||
|
`Investor: ${floID}`,
|
||||||
|
`End value: ${BTC_net} USD`,
|
||||||
|
`Date of withdrawal: ${dateFormat(end_date)}`,
|
||||||
|
`USD INR rate at end: ${USD_net}`,
|
||||||
|
`Amount withdrawn: Rs ${amount} via ${payment_ref}`,
|
||||||
|
`Reference: ${ref_sign}`
|
||||||
|
].join("|");
|
||||||
|
}
|
||||||
|
|
||||||
|
function parse_details(data) {
|
||||||
|
let funds = {};
|
||||||
|
funds.investments = {};
|
||||||
|
if (!Array.isArray(data))
|
||||||
|
data = [data];
|
||||||
|
data.forEach((fd, i) => {
|
||||||
|
if (!/close: [a-z0-9]{64}\|/.test(fd)) { // not a closing tx
|
||||||
|
let cont = /continue: [a-z0-9]{64}\|/.test(fd);
|
||||||
|
fd.split("|").forEach(d => {
|
||||||
|
d = d.split(': ');
|
||||||
|
if (["invesment(s) (inr)", "investment(s) (inr)"].includes(d[0].toLowerCase()))
|
||||||
|
d[1].split(";").forEach(a => {
|
||||||
|
a = a.split("-");
|
||||||
|
let floID = a[0].replace(/\s/g, ''); //for removing spaces (trailing) if any
|
||||||
|
funds["investments"][floID] = funds["investments"][floID] || {};
|
||||||
|
funds["investments"][floID].amount = parseNumber(a[1]);
|
||||||
|
funds["investments"][floID].i = i;
|
||||||
|
});
|
||||||
|
else if (!cont)
|
||||||
|
switch (d[0].toLowerCase()) {
|
||||||
|
case "start date":
|
||||||
|
funds["start_date"] = new Date(d[1]); break;
|
||||||
|
case "base value":
|
||||||
|
funds["BTC_base"] = parseNumber(d[1].slice(0, -4)); break;
|
||||||
|
case "usd inr rate at start":
|
||||||
|
funds["USD_base"] = parseFloat(d[1]); break;
|
||||||
|
case "duration":
|
||||||
|
funds["duration"] = parsePeriod(d[1]); break;
|
||||||
|
case "management fee":
|
||||||
|
funds["fee"] = parseFloat(d[1]); break;
|
||||||
|
case "tapout availability":
|
||||||
|
let x = d[1].toLowerCase().split("after")
|
||||||
|
funds["tapoutInterval"] = x[1].match(/\d+ [a-z]+/gi).map(y => parsePeriod(y))
|
||||||
|
funds["topoutWindow"] = parsePeriod(x[0]); break;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
let floID, details = {};
|
||||||
|
fd.split("|").forEach(d => {
|
||||||
|
d = d.split(': ');
|
||||||
|
switch (d[0].toLowerCase()) {
|
||||||
|
case "investor":
|
||||||
|
floID = d[1]; break;
|
||||||
|
case "end value":
|
||||||
|
details["BTC_net"] = parseNumber(d[1].slice(0, -4)); break;
|
||||||
|
case "date of withdrawal":
|
||||||
|
details["endDate"] = new Date(d[1]); break;
|
||||||
|
case "amount withdrawn":
|
||||||
|
details["amountFinal"] = parseNumber(d[1].match(/\d.+ via/).toString());
|
||||||
|
details["payment_refRef"] = d[1].match(/via .+/).toString().substring(4); break;
|
||||||
|
case "usd inr rate at end":
|
||||||
|
details["USD_net"] = parseFloat(d[1]); break;
|
||||||
|
case "reference":
|
||||||
|
details["refSign"] = d[1]; break;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (floID) {
|
||||||
|
funds.investments[floID] = funds.investments[floID] || {};
|
||||||
|
funds.investments[floID].closed = details;
|
||||||
|
funds.investments[floID].closed.i = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return funds;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
productStr,
|
||||||
|
dateAdder,
|
||||||
|
dateFormat,
|
||||||
|
calcNetValue,
|
||||||
|
parse: parse_details,
|
||||||
|
stringify: {
|
||||||
|
main: stringify_main,
|
||||||
|
continue: stringify_continue,
|
||||||
|
end: stringify_end
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
})();
|
||||||
|
|
||||||
|
function refreshBlockchainData(newOnly = false) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
compactIDB.readData("appendix", "lastTx").then(lastTx => {
|
||||||
|
floBlockchainAPI.readData(floGlobals.adminID, {
|
||||||
|
ignoreOld: lastTx,
|
||||||
|
senders: floExchangeAPI.nodeList.concat(floGlobals.adminID), //sentOnly: true,
|
||||||
|
tx: true,
|
||||||
|
filter: d => d.startsWith(bobsFund.productStr)
|
||||||
|
}).then(result => {
|
||||||
|
compactIDB.readAllData("funds").then(funds => {
|
||||||
|
let writeKeys = new Set();
|
||||||
|
result.data.reverse().forEach(d => {
|
||||||
|
if (/close: /i.test(d.data)) {
|
||||||
|
let ctx = d.data.match(/close: [0-9a-z]{64}/i).toString().split(": ")[1];
|
||||||
|
funds[ctx].push({
|
||||||
|
txid: d.txid,
|
||||||
|
data: d.data
|
||||||
|
})
|
||||||
|
writeKeys.add(ctx);
|
||||||
|
} else if (d.senders.has(floGlobals.adminID)) {
|
||||||
|
if (/continue: /i.test(d.data)) {
|
||||||
|
let ctx = d.data.match(/continue: [0-9a-z]{64}/i).toString().split(": ")[1];
|
||||||
|
funds[ctx].push({
|
||||||
|
txid: d.txid,
|
||||||
|
data: d.data
|
||||||
|
})
|
||||||
|
writeKeys.add(ctx);
|
||||||
|
} else if (/start: /i.test(d.data)) {
|
||||||
|
funds[d.txid] = [{
|
||||||
|
txid: d.txid,
|
||||||
|
data: d.data
|
||||||
|
}]
|
||||||
|
writeKeys.add(d.txid);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
writeKeys = Array.from(writeKeys);
|
||||||
|
Promise.all(writeKeys.map(k => compactIDB.writeData("funds", funds[k], k))).then(results => {
|
||||||
|
compactIDB.writeData('appendix', result.totalTxs, "lastTx");
|
||||||
|
resolve(newOnly ? writeKeys.map(k => funds[k]) : funds)
|
||||||
|
}).catch(error => reject(error))
|
||||||
|
}).catch(error => reject(error))
|
||||||
|
}).catch(error => reject(error))
|
||||||
|
}).catch(error => reject(error))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function getCurrentRates() {
|
||||||
|
let fetchData = api => new Promise((resolve, reject) => {
|
||||||
|
fetch(api).then(response => {
|
||||||
|
if (response.ok)
|
||||||
|
response.json().then(data => resolve(data))
|
||||||
|
else
|
||||||
|
reject(response)
|
||||||
|
}).catch(error => reject(error))
|
||||||
|
})
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
fetchData(`https://bitpay.com/api/rates`).then(result => {
|
||||||
|
let BTC_USD, BTC_INR, USD_INR
|
||||||
|
for (let i of result)
|
||||||
|
i.code == "USD" ? BTC_USD = i.rate : i.code == "INR" ? BTC_INR = i.rate : null;
|
||||||
|
USD_INR = BTC_INR / BTC_USD;
|
||||||
|
resolve({
|
||||||
|
BTC_USD,
|
||||||
|
BTC_INR,
|
||||||
|
USD_INR
|
||||||
|
})
|
||||||
|
}).catch(error => reject(error))
|
||||||
|
})
|
||||||
|
}
|
||||||
259
scripts/compactIDB.js
Normal file
259
scripts/compactIDB.js
Normal file
@ -0,0 +1,259 @@
|
|||||||
|
(function(EXPORTS) { //compactIDB v2.1.0
|
||||||
|
/* Compact IndexedDB operations */
|
||||||
|
'use strict';
|
||||||
|
const compactIDB = EXPORTS;
|
||||||
|
|
||||||
|
var defaultDB;
|
||||||
|
|
||||||
|
const indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
|
||||||
|
const IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.msIDBTransaction;
|
||||||
|
const IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange || window.msIDBKeyRange;
|
||||||
|
|
||||||
|
if (!indexedDB) {
|
||||||
|
console.error("Your browser doesn't support a stable version of IndexedDB.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
compactIDB.setDefaultDB = dbName => defaultDB = dbName;
|
||||||
|
|
||||||
|
Object.defineProperty(compactIDB, 'default', {
|
||||||
|
get: () => defaultDB,
|
||||||
|
set: dbName => defaultDB = dbName
|
||||||
|
});
|
||||||
|
|
||||||
|
function getDBversion(dbName = defaultDB) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
openDB(dbName).then(db => {
|
||||||
|
resolve(db.version)
|
||||||
|
db.close()
|
||||||
|
}).catch(error => reject(error))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function upgradeDB(dbName, createList = null, deleteList = null) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
getDBversion(dbName).then(version => {
|
||||||
|
var idb = indexedDB.open(dbName, version + 1);
|
||||||
|
idb.onerror = (event) => reject("Error in opening IndexedDB");
|
||||||
|
idb.onupgradeneeded = (event) => {
|
||||||
|
let db = event.target.result;
|
||||||
|
if (createList instanceof Object) {
|
||||||
|
if (Array.isArray(createList)) {
|
||||||
|
let tmp = {}
|
||||||
|
createList.forEach(o => tmp[o] = {})
|
||||||
|
createList = tmp
|
||||||
|
}
|
||||||
|
for (let o in createList) {
|
||||||
|
let obs = db.createObjectStore(o, createList[o].options || {});
|
||||||
|
if (createList[o].indexes instanceof Object)
|
||||||
|
for (let i in createList[o].indexes)
|
||||||
|
obs.createIndex(i, i, createList[o].indexes || {});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (Array.isArray(deleteList))
|
||||||
|
deleteList.forEach(o => db.deleteObjectStore(o));
|
||||||
|
resolve('Database upgraded')
|
||||||
|
}
|
||||||
|
idb.onsuccess = (event) => event.target.result.close();
|
||||||
|
}).catch(error => reject(error))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
compactIDB.initDB = function(dbName, objectStores = {}) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
if (!(objectStores instanceof Object))
|
||||||
|
return reject('ObjectStores must be an object or array')
|
||||||
|
defaultDB = defaultDB || dbName;
|
||||||
|
var idb = indexedDB.open(dbName);
|
||||||
|
idb.onerror = (event) => reject("Error in opening IndexedDB");
|
||||||
|
idb.onsuccess = (event) => {
|
||||||
|
var db = event.target.result;
|
||||||
|
let cList = Object.values(db.objectStoreNames);
|
||||||
|
var obs = {},
|
||||||
|
a_obs = {},
|
||||||
|
d_obs = [];
|
||||||
|
if (!Array.isArray(objectStores))
|
||||||
|
var obs = objectStores
|
||||||
|
else
|
||||||
|
objectStores.forEach(o => obs[o] = {})
|
||||||
|
let nList = Object.keys(obs)
|
||||||
|
for (let o of nList)
|
||||||
|
if (!cList.includes(o))
|
||||||
|
a_obs[o] = obs[o]
|
||||||
|
for (let o of cList)
|
||||||
|
if (!nList.includes(o))
|
||||||
|
d_obs.push(o)
|
||||||
|
if (!Object.keys(a_obs).length && !d_obs.length)
|
||||||
|
resolve("Initiated IndexedDB");
|
||||||
|
else
|
||||||
|
upgradeDB(dbName, a_obs, d_obs)
|
||||||
|
.then(result => resolve(result))
|
||||||
|
.catch(error => reject(error))
|
||||||
|
db.close();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const openDB = compactIDB.openDB = function(dbName = defaultDB) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
var idb = indexedDB.open(dbName);
|
||||||
|
idb.onerror = (event) => reject("Error in opening IndexedDB");
|
||||||
|
idb.onupgradeneeded = (event) => {
|
||||||
|
event.target.result.close();
|
||||||
|
deleteDB(dbName).then(_ => null).catch(_ => null).finally(_ => reject("Datebase not found"))
|
||||||
|
}
|
||||||
|
idb.onsuccess = (event) => resolve(event.target.result);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const deleteDB = compactIDB.deleteDB = function(dbName = defaultDB) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
var deleteReq = indexedDB.deleteDatabase(dbName);;
|
||||||
|
deleteReq.onerror = (event) => reject("Error deleting database!");
|
||||||
|
deleteReq.onsuccess = (event) => resolve("Database deleted successfully");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
compactIDB.writeData = function(obsName, data, key = false, dbName = defaultDB) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
openDB(dbName).then(db => {
|
||||||
|
var obs = db.transaction(obsName, "readwrite").objectStore(obsName);
|
||||||
|
let writeReq = (key ? obs.put(data, key) : obs.put(data));
|
||||||
|
writeReq.onsuccess = (evt) => resolve(`Write data Successful`);
|
||||||
|
writeReq.onerror = (evt) => reject(
|
||||||
|
`Write data unsuccessful [${evt.target.error.name}] ${evt.target.error.message}`
|
||||||
|
);
|
||||||
|
db.close();
|
||||||
|
}).catch(error => reject(error));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
compactIDB.addData = function(obsName, data, key = false, dbName = defaultDB) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
openDB(dbName).then(db => {
|
||||||
|
var obs = db.transaction(obsName, "readwrite").objectStore(obsName);
|
||||||
|
let addReq = (key ? obs.add(data, key) : obs.add(data));
|
||||||
|
addReq.onsuccess = (evt) => resolve(`Add data successful`);
|
||||||
|
addReq.onerror = (evt) => reject(
|
||||||
|
`Add data unsuccessful [${evt.target.error.name}] ${evt.target.error.message}`
|
||||||
|
);
|
||||||
|
db.close();
|
||||||
|
}).catch(error => reject(error));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
compactIDB.removeData = function(obsName, key, dbName = defaultDB) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
openDB(dbName).then(db => {
|
||||||
|
var obs = db.transaction(obsName, "readwrite").objectStore(obsName);
|
||||||
|
let delReq = obs.delete(key);
|
||||||
|
delReq.onsuccess = (evt) => resolve(`Removed Data ${key}`);
|
||||||
|
delReq.onerror = (evt) => reject(
|
||||||
|
`Remove data unsuccessful [${evt.target.error.name}] ${evt.target.error.message}`
|
||||||
|
);
|
||||||
|
db.close();
|
||||||
|
}).catch(error => reject(error));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
compactIDB.clearData = function(obsName, dbName = defaultDB) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
openDB(dbName).then(db => {
|
||||||
|
var obs = db.transaction(obsName, "readwrite").objectStore(obsName);
|
||||||
|
let clearReq = obs.clear();
|
||||||
|
clearReq.onsuccess = (evt) => resolve(`Clear data Successful`);
|
||||||
|
clearReq.onerror = (evt) => reject(`Clear data Unsuccessful`);
|
||||||
|
db.close();
|
||||||
|
}).catch(error => reject(error));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
compactIDB.readData = function(obsName, key, dbName = defaultDB) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
openDB(dbName).then(db => {
|
||||||
|
var obs = db.transaction(obsName, "readonly").objectStore(obsName);
|
||||||
|
let getReq = obs.get(key);
|
||||||
|
getReq.onsuccess = (evt) => resolve(evt.target.result);
|
||||||
|
getReq.onerror = (evt) => reject(
|
||||||
|
`Read data unsuccessful [${evt.target.error.name}] ${evt.target.error.message}`
|
||||||
|
);
|
||||||
|
db.close();
|
||||||
|
}).catch(error => reject(error));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
compactIDB.readAllData = function(obsName, dbName = defaultDB) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
openDB(dbName).then(db => {
|
||||||
|
var obs = db.transaction(obsName, "readonly").objectStore(obsName);
|
||||||
|
var tmpResult = {}
|
||||||
|
let curReq = obs.openCursor();
|
||||||
|
curReq.onsuccess = (evt) => {
|
||||||
|
var cursor = evt.target.result;
|
||||||
|
if (cursor) {
|
||||||
|
tmpResult[cursor.primaryKey] = cursor.value;
|
||||||
|
cursor.continue();
|
||||||
|
} else
|
||||||
|
resolve(tmpResult);
|
||||||
|
}
|
||||||
|
curReq.onerror = (evt) => reject(
|
||||||
|
`Read-All data unsuccessful [${evt.target.error.name}] ${evt.target.error.message}`
|
||||||
|
);
|
||||||
|
db.close();
|
||||||
|
}).catch(error => reject(error));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/* compactIDB.searchData = function (obsName, options = {}, dbName = defaultDB) {
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
openDB(dbName).then(db => {
|
||||||
|
var obs = db.transaction(obsName, "readonly").objectStore(obsName);
|
||||||
|
var filteredResult = {}
|
||||||
|
let keyRange;
|
||||||
|
if(options.lowerKey!==null && options.upperKey!==null)
|
||||||
|
keyRange = IDBKeyRange.bound(options.lowerKey, options.upperKey);
|
||||||
|
else if(options.lowerKey!==null)
|
||||||
|
keyRange = IDBKeyRange.lowerBound(options.lowerKey);
|
||||||
|
else if (options.upperKey!==null)
|
||||||
|
keyRange = IDBKeyRange.upperBound(options.upperBound);
|
||||||
|
else if (options.atKey)
|
||||||
|
let curReq = obs.openCursor(keyRange, )
|
||||||
|
}).catch(error => reject(error))
|
||||||
|
})
|
||||||
|
}*/
|
||||||
|
|
||||||
|
compactIDB.searchData = function(obsName, options = {}, dbName = defaultDB) {
|
||||||
|
options.lowerKey = options.atKey || options.lowerKey || 0
|
||||||
|
options.upperKey = options.atKey || options.upperKey || false
|
||||||
|
options.patternEval = options.patternEval || ((k, v) => {
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
options.limit = options.limit || false;
|
||||||
|
options.lastOnly = options.lastOnly || false
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
openDB(dbName).then(db => {
|
||||||
|
var obs = db.transaction(obsName, "readonly").objectStore(obsName);
|
||||||
|
var filteredResult = {}
|
||||||
|
let curReq = obs.openCursor(
|
||||||
|
options.upperKey ? IDBKeyRange.bound(options.lowerKey, options.upperKey) : IDBKeyRange.lowerBound(options.lowerKey),
|
||||||
|
options.lastOnly ? "prev" : "next");
|
||||||
|
curReq.onsuccess = (evt) => {
|
||||||
|
var cursor = evt.target.result;
|
||||||
|
if (cursor) {
|
||||||
|
if (options.patternEval(cursor.primaryKey, cursor.value)) {
|
||||||
|
filteredResult[cursor.primaryKey] = cursor.value;
|
||||||
|
options.lastOnly ? resolve(filteredResult) : cursor.continue();
|
||||||
|
} else
|
||||||
|
cursor.continue();
|
||||||
|
} else
|
||||||
|
resolve(filteredResult);
|
||||||
|
}
|
||||||
|
curReq.onerror = (evt) => reject(`Search unsuccessful [${evt.target.error.name}] ${evt.target.error.message}`);
|
||||||
|
db.close();
|
||||||
|
}).catch(error => reject(error));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
})(window.compactIDB = {});
|
||||||
8
scripts/components.min.js
vendored
Normal file
8
scripts/components.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
580
scripts/floBlockchainAPI.js
Normal file
580
scripts/floBlockchainAPI.js
Normal file
@ -0,0 +1,580 @@
|
|||||||
|
(function (EXPORTS) { //floBlockchainAPI v2.3.3d
|
||||||
|
/* FLO Blockchain Operator to send/receive data from blockchain using API calls*/
|
||||||
|
'use strict';
|
||||||
|
const floBlockchainAPI = EXPORTS;
|
||||||
|
|
||||||
|
const DEFAULT = {
|
||||||
|
blockchain: floGlobals.blockchain,
|
||||||
|
apiURL: {
|
||||||
|
FLO: ['https://flosight.duckdns.org/'],
|
||||||
|
FLO_TEST: ['https://testnet-flosight.duckdns.org', 'https://testnet.flocha.in/']
|
||||||
|
},
|
||||||
|
sendAmt: 0.001,
|
||||||
|
fee: 0.0005,
|
||||||
|
minChangeAmt: 0.0005,
|
||||||
|
receiverID: floGlobals.adminID
|
||||||
|
};
|
||||||
|
|
||||||
|
Object.defineProperties(floBlockchainAPI, {
|
||||||
|
sendAmt: {
|
||||||
|
get: () => DEFAULT.sendAmt,
|
||||||
|
set: amt => !isNaN(amt) ? DEFAULT.sendAmt = amt : null
|
||||||
|
},
|
||||||
|
fee: {
|
||||||
|
get: () => DEFAULT.fee,
|
||||||
|
set: fee => !isNaN(fee) ? DEFAULT.fee = fee : null
|
||||||
|
},
|
||||||
|
defaultReceiver: {
|
||||||
|
get: () => DEFAULT.receiverID,
|
||||||
|
set: floID => DEFAULT.receiverID = floID
|
||||||
|
},
|
||||||
|
blockchain: {
|
||||||
|
get: () => DEFAULT.blockchain
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (floGlobals.sendAmt) floBlockchainAPI.sendAmt = floGlobals.sendAmt;
|
||||||
|
if (floGlobals.fee) floBlockchainAPI.fee = floGlobals.fee;
|
||||||
|
|
||||||
|
Object.defineProperties(floGlobals, {
|
||||||
|
sendAmt: {
|
||||||
|
get: () => DEFAULT.sendAmt,
|
||||||
|
set: amt => !isNaN(amt) ? DEFAULT.sendAmt = amt : null
|
||||||
|
},
|
||||||
|
fee: {
|
||||||
|
get: () => DEFAULT.fee,
|
||||||
|
set: fee => !isNaN(fee) ? DEFAULT.fee = fee : null
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const allServerList = new Set(floGlobals.apiURL && floGlobals.apiURL[DEFAULT.blockchain] ? floGlobals.apiURL[DEFAULT.blockchain] : DEFAULT.apiURL[DEFAULT.blockchain]);
|
||||||
|
|
||||||
|
var serverList = Array.from(allServerList);
|
||||||
|
var curPos = floCrypto.randInt(0, serverList.length - 1);
|
||||||
|
|
||||||
|
function fetch_retry(apicall, rm_flosight) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
let i = serverList.indexOf(rm_flosight)
|
||||||
|
if (i != -1) serverList.splice(i, 1);
|
||||||
|
curPos = floCrypto.randInt(0, serverList.length - 1);
|
||||||
|
fetch_api(apicall, false)
|
||||||
|
.then(result => resolve(result))
|
||||||
|
.catch(error => reject(error));
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function fetch_api(apicall, ic = true) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
if (serverList.length === 0) {
|
||||||
|
if (ic) {
|
||||||
|
serverList = Array.from(allServerList);
|
||||||
|
curPos = floCrypto.randInt(0, serverList.length - 1);
|
||||||
|
fetch_api(apicall, false)
|
||||||
|
.then(result => resolve(result))
|
||||||
|
.catch(error => reject(error));
|
||||||
|
} else
|
||||||
|
reject("No floSight server working");
|
||||||
|
} else {
|
||||||
|
let flosight = serverList[curPos];
|
||||||
|
fetch(flosight + apicall).then(response => {
|
||||||
|
if (response.ok)
|
||||||
|
response.json().then(data => resolve(data));
|
||||||
|
else {
|
||||||
|
fetch_retry(apicall, flosight)
|
||||||
|
.then(result => resolve(result))
|
||||||
|
.catch(error => reject(error));
|
||||||
|
}
|
||||||
|
}).catch(error => {
|
||||||
|
fetch_retry(apicall, flosight)
|
||||||
|
.then(result => resolve(result))
|
||||||
|
.catch(error => reject(error));
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
Object.defineProperties(floBlockchainAPI, {
|
||||||
|
serverList: {
|
||||||
|
get: () => Array.from(serverList)
|
||||||
|
},
|
||||||
|
current_server: {
|
||||||
|
get: () => serverList[curPos]
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
//Promised function to get data from API
|
||||||
|
const promisedAPI = floBlockchainAPI.promisedAPI = floBlockchainAPI.fetch = function (apicall) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
//console.log(apicall);
|
||||||
|
fetch_api(apicall)
|
||||||
|
.then(result => resolve(result))
|
||||||
|
.catch(error => reject(error));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
//Get balance for the given Address
|
||||||
|
const getBalance = floBlockchainAPI.getBalance = function (addr) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
promisedAPI(`api/addr/${addr}/balance`)
|
||||||
|
.then(balance => resolve(parseFloat(balance)))
|
||||||
|
.catch(error => reject(error));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
//Send Tx to blockchain
|
||||||
|
const sendTx = floBlockchainAPI.sendTx = function (senderAddr, receiverAddr, sendAmt, privKey, floData = '', strict_utxo = true) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
if (!floCrypto.validateASCII(floData))
|
||||||
|
return reject("Invalid FLO_Data: only printable ASCII characters are allowed");
|
||||||
|
else if (!floCrypto.validateFloID(senderAddr))
|
||||||
|
return reject(`Invalid address : ${senderAddr}`);
|
||||||
|
else if (!floCrypto.validateFloID(receiverAddr))
|
||||||
|
return reject(`Invalid address : ${receiverAddr}`);
|
||||||
|
else if (privKey.length < 1 || !floCrypto.verifyPrivKey(privKey, senderAddr))
|
||||||
|
return reject("Invalid Private key!");
|
||||||
|
else if (typeof sendAmt !== 'number' || sendAmt <= 0)
|
||||||
|
return reject(`Invalid sendAmt : ${sendAmt}`);
|
||||||
|
|
||||||
|
getBalance(senderAddr).then(balance => {
|
||||||
|
var fee = DEFAULT.fee;
|
||||||
|
if (balance < sendAmt + fee)
|
||||||
|
return reject("Insufficient FLO balance!");
|
||||||
|
//get unconfirmed tx list
|
||||||
|
promisedAPI(`api/addr/${senderAddr}`).then(result => {
|
||||||
|
readTxs(senderAddr, 0, result.unconfirmedTxApperances).then(result => {
|
||||||
|
let unconfirmedSpent = {};
|
||||||
|
for (let tx of result.items)
|
||||||
|
if (tx.confirmations == 0)
|
||||||
|
for (let vin of tx.vin)
|
||||||
|
if (vin.addr === senderAddr) {
|
||||||
|
if (Array.isArray(unconfirmedSpent[vin.txid]))
|
||||||
|
unconfirmedSpent[vin.txid].push(vin.vout);
|
||||||
|
else
|
||||||
|
unconfirmedSpent[vin.txid] = [vin.vout];
|
||||||
|
}
|
||||||
|
//get utxos list
|
||||||
|
promisedAPI(`api/addr/${senderAddr}/utxo`).then(utxos => {
|
||||||
|
//form/construct the transaction data
|
||||||
|
var trx = bitjs.transaction();
|
||||||
|
var utxoAmt = 0.0;
|
||||||
|
for (var i = utxos.length - 1;
|
||||||
|
(i >= 0) && (utxoAmt < sendAmt + fee); i--) {
|
||||||
|
//use only utxos with confirmations (strict_utxo mode)
|
||||||
|
if (utxos[i].confirmations || !strict_utxo) {
|
||||||
|
if (utxos[i].txid in unconfirmedSpent && unconfirmedSpent[utxos[i].txid].includes(utxos[i].vout))
|
||||||
|
continue; //A transaction has already used the utxo, but is unconfirmed.
|
||||||
|
trx.addinput(utxos[i].txid, utxos[i].vout, utxos[i].scriptPubKey);
|
||||||
|
utxoAmt += utxos[i].amount;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if (utxoAmt < sendAmt + fee)
|
||||||
|
reject("Insufficient FLO: Some UTXOs are unconfirmed");
|
||||||
|
else {
|
||||||
|
trx.addoutput(receiverAddr, sendAmt);
|
||||||
|
var change = utxoAmt - sendAmt - fee;
|
||||||
|
if (change > DEFAULT.minChangeAmt)
|
||||||
|
trx.addoutput(senderAddr, change);
|
||||||
|
trx.addflodata(floData.replace(/\n/g, ' '));
|
||||||
|
var signedTxHash = trx.sign(privKey, 1);
|
||||||
|
broadcastTx(signedTxHash)
|
||||||
|
.then(txid => resolve(txid))
|
||||||
|
.catch(error => reject(error))
|
||||||
|
}
|
||||||
|
}).catch(error => reject(error))
|
||||||
|
}).catch(error => reject(error))
|
||||||
|
}).catch(error => reject(error))
|
||||||
|
}).catch(error => reject(error))
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
//Write Data into blockchain
|
||||||
|
floBlockchainAPI.writeData = function (senderAddr, data, privKey, receiverAddr = DEFAULT.receiverID, options = {}) {
|
||||||
|
let strict_utxo = options.strict_utxo === false ? false : true,
|
||||||
|
sendAmt = isNaN(options.sendAmt) ? DEFAULT.sendAmt : options.sendAmt;
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
if (typeof data != "string")
|
||||||
|
data = JSON.stringify(data);
|
||||||
|
sendTx(senderAddr, receiverAddr, sendAmt, privKey, data, strict_utxo)
|
||||||
|
.then(txid => resolve(txid))
|
||||||
|
.catch(error => reject(error));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
//merge all UTXOs of a given floID into a single UTXO
|
||||||
|
floBlockchainAPI.mergeUTXOs = function (floID, privKey, floData = '') {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
if (!floCrypto.validateFloID(floID))
|
||||||
|
return reject(`Invalid floID`);
|
||||||
|
if (!floCrypto.verifyPrivKey(privKey, floID))
|
||||||
|
return reject("Invalid Private Key");
|
||||||
|
if (!floCrypto.validateASCII(floData))
|
||||||
|
return reject("Invalid FLO_Data: only printable ASCII characters are allowed");
|
||||||
|
var trx = bitjs.transaction();
|
||||||
|
var utxoAmt = 0.0;
|
||||||
|
var fee = DEFAULT.fee;
|
||||||
|
promisedAPI(`api/addr/${floID}/utxo`).then(utxos => {
|
||||||
|
for (var i = utxos.length - 1; i >= 0; i--)
|
||||||
|
if (utxos[i].confirmations) {
|
||||||
|
trx.addinput(utxos[i].txid, utxos[i].vout, utxos[i].scriptPubKey);
|
||||||
|
utxoAmt += utxos[i].amount;
|
||||||
|
}
|
||||||
|
trx.addoutput(floID, utxoAmt - fee);
|
||||||
|
trx.addflodata(floData.replace(/\n/g, ' '));
|
||||||
|
var signedTxHash = trx.sign(privKey, 1);
|
||||||
|
broadcastTx(signedTxHash)
|
||||||
|
.then(txid => resolve(txid))
|
||||||
|
.catch(error => reject(error))
|
||||||
|
}).catch(error => reject(error))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/**Write data into blockchain from (and/or) to multiple floID
|
||||||
|
* @param {Array} senderPrivKeys List of sender private-keys
|
||||||
|
* @param {string} data FLO data of the txn
|
||||||
|
* @param {Array} receivers List of receivers
|
||||||
|
* @param {boolean} preserveRatio (optional) preserve ratio or equal contribution
|
||||||
|
* @return {Promise}
|
||||||
|
*/
|
||||||
|
floBlockchainAPI.writeDataMultiple = function (senderPrivKeys, data, receivers = [DEFAULT.receiverID], preserveRatio = true) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
if (!Array.isArray(senderPrivKeys))
|
||||||
|
return reject("Invalid senderPrivKeys: SenderPrivKeys must be Array");
|
||||||
|
if (!preserveRatio) {
|
||||||
|
let tmp = {};
|
||||||
|
let amount = (DEFAULT.sendAmt * receivers.length) / senderPrivKeys.length;
|
||||||
|
senderPrivKeys.forEach(key => tmp[key] = amount);
|
||||||
|
senderPrivKeys = tmp;
|
||||||
|
}
|
||||||
|
if (!Array.isArray(receivers))
|
||||||
|
return reject("Invalid receivers: Receivers must be Array");
|
||||||
|
else {
|
||||||
|
let tmp = {};
|
||||||
|
let amount = DEFAULT.sendAmt;
|
||||||
|
receivers.forEach(floID => tmp[floID] = amount);
|
||||||
|
receivers = tmp
|
||||||
|
}
|
||||||
|
if (typeof data != "string")
|
||||||
|
data = JSON.stringify(data);
|
||||||
|
sendTxMultiple(senderPrivKeys, receivers, data)
|
||||||
|
.then(txid => resolve(txid))
|
||||||
|
.catch(error => reject(error))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/**Send Tx from (and/or) to multiple floID
|
||||||
|
* @param {Array or Object} senderPrivKeys List of sender private-key (optional: with coins to be sent)
|
||||||
|
* @param {Object} receivers List of receivers with respective amount to be sent
|
||||||
|
* @param {string} floData FLO data of the txn
|
||||||
|
* @return {Promise}
|
||||||
|
*/
|
||||||
|
const sendTxMultiple = floBlockchainAPI.sendTxMultiple = function (senderPrivKeys, receivers, floData = '') {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
if (!floCrypto.validateASCII(floData))
|
||||||
|
return reject("Invalid FLO_Data: only printable ASCII characters are allowed");
|
||||||
|
let senders = {},
|
||||||
|
preserveRatio;
|
||||||
|
//check for argument validations
|
||||||
|
try {
|
||||||
|
let invalids = {
|
||||||
|
InvalidSenderPrivKeys: [],
|
||||||
|
InvalidSenderAmountFor: [],
|
||||||
|
InvalidReceiverIDs: [],
|
||||||
|
InvalidReceiveAmountFor: []
|
||||||
|
}
|
||||||
|
let inputVal = 0,
|
||||||
|
outputVal = 0;
|
||||||
|
//Validate sender privatekeys (and send amount if passed)
|
||||||
|
//conversion when only privateKeys are passed (preserveRatio mode)
|
||||||
|
if (Array.isArray(senderPrivKeys)) {
|
||||||
|
senderPrivKeys.forEach(key => {
|
||||||
|
try {
|
||||||
|
if (!key)
|
||||||
|
invalids.InvalidSenderPrivKeys.push(key);
|
||||||
|
else {
|
||||||
|
let floID = floCrypto.getFloID(key);
|
||||||
|
senders[floID] = {
|
||||||
|
wif: key
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
invalids.InvalidSenderPrivKeys.push(key)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
preserveRatio = true;
|
||||||
|
}
|
||||||
|
//conversion when privatekeys are passed with send amount
|
||||||
|
else {
|
||||||
|
for (let key in senderPrivKeys) {
|
||||||
|
try {
|
||||||
|
if (!key)
|
||||||
|
invalids.InvalidSenderPrivKeys.push(key);
|
||||||
|
else {
|
||||||
|
if (typeof senderPrivKeys[key] !== 'number' || senderPrivKeys[key] <= 0)
|
||||||
|
invalids.InvalidSenderAmountFor.push(key);
|
||||||
|
else
|
||||||
|
inputVal += senderPrivKeys[key];
|
||||||
|
let floID = floCrypto.getFloID(key);
|
||||||
|
senders[floID] = {
|
||||||
|
wif: key,
|
||||||
|
coins: senderPrivKeys[key]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
invalids.InvalidSenderPrivKeys.push(key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
preserveRatio = false;
|
||||||
|
}
|
||||||
|
//Validate the receiver IDs and receive amount
|
||||||
|
for (let floID in receivers) {
|
||||||
|
if (!floCrypto.validateFloID(floID))
|
||||||
|
invalids.InvalidReceiverIDs.push(floID);
|
||||||
|
if (typeof receivers[floID] !== 'number' || receivers[floID] <= 0)
|
||||||
|
invalids.InvalidReceiveAmountFor.push(floID);
|
||||||
|
else
|
||||||
|
outputVal += receivers[floID];
|
||||||
|
}
|
||||||
|
//Reject if any invalids are found
|
||||||
|
for (let i in invalids)
|
||||||
|
if (!invalids[i].length)
|
||||||
|
delete invalids[i];
|
||||||
|
if (Object.keys(invalids).length)
|
||||||
|
return reject(invalids);
|
||||||
|
//Reject if given inputVal and outputVal are not equal
|
||||||
|
if (!preserveRatio && inputVal != outputVal)
|
||||||
|
return reject(`Input Amount (${inputVal}) not equal to Output Amount (${outputVal})`);
|
||||||
|
} catch (error) {
|
||||||
|
return reject(error)
|
||||||
|
}
|
||||||
|
//Get balance of senders
|
||||||
|
let promises = [];
|
||||||
|
for (let floID in senders)
|
||||||
|
promises.push(getBalance(floID));
|
||||||
|
Promise.all(promises).then(results => {
|
||||||
|
let totalBalance = 0,
|
||||||
|
totalFee = DEFAULT.fee,
|
||||||
|
balance = {};
|
||||||
|
//Divide fee among sender if not for preserveRatio
|
||||||
|
if (!preserveRatio)
|
||||||
|
var dividedFee = totalFee / Object.keys(senders).length;
|
||||||
|
//Check if balance of each sender is sufficient enough
|
||||||
|
let insufficient = [];
|
||||||
|
for (let floID in senders) {
|
||||||
|
balance[floID] = parseFloat(results.shift());
|
||||||
|
if (isNaN(balance[floID]) || (preserveRatio && balance[floID] <= totalFee) ||
|
||||||
|
(!preserveRatio && balance[floID] < senders[floID].coins + dividedFee))
|
||||||
|
insufficient.push(floID);
|
||||||
|
totalBalance += balance[floID];
|
||||||
|
}
|
||||||
|
if (insufficient.length)
|
||||||
|
return reject({
|
||||||
|
InsufficientBalance: insufficient
|
||||||
|
})
|
||||||
|
//Calculate totalSentAmount and check if totalBalance is sufficient
|
||||||
|
let totalSendAmt = totalFee;
|
||||||
|
for (let floID in receivers)
|
||||||
|
totalSendAmt += receivers[floID];
|
||||||
|
if (totalBalance < totalSendAmt)
|
||||||
|
return reject("Insufficient total Balance");
|
||||||
|
//Get the UTXOs of the senders
|
||||||
|
let promises = [];
|
||||||
|
for (let floID in senders)
|
||||||
|
promises.push(promisedAPI(`api/addr/${floID}/utxo`));
|
||||||
|
Promise.all(promises).then(results => {
|
||||||
|
let wifSeq = [];
|
||||||
|
var trx = bitjs.transaction();
|
||||||
|
for (let floID in senders) {
|
||||||
|
let utxos = results.shift();
|
||||||
|
let sendAmt;
|
||||||
|
if (preserveRatio) {
|
||||||
|
let ratio = (balance[floID] / totalBalance);
|
||||||
|
sendAmt = totalSendAmt * ratio;
|
||||||
|
} else
|
||||||
|
sendAmt = senders[floID].coins + dividedFee;
|
||||||
|
let wif = senders[floID].wif;
|
||||||
|
let utxoAmt = 0.0;
|
||||||
|
for (let i = utxos.length - 1;
|
||||||
|
(i >= 0) && (utxoAmt < sendAmt); i--) {
|
||||||
|
if (utxos[i].confirmations) {
|
||||||
|
trx.addinput(utxos[i].txid, utxos[i].vout, utxos[i].scriptPubKey);
|
||||||
|
wifSeq.push(wif);
|
||||||
|
utxoAmt += utxos[i].amount;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (utxoAmt < sendAmt)
|
||||||
|
return reject("Insufficient balance:" + floID);
|
||||||
|
let change = (utxoAmt - sendAmt);
|
||||||
|
if (change > 0)
|
||||||
|
trx.addoutput(floID, change);
|
||||||
|
}
|
||||||
|
for (let floID in receivers)
|
||||||
|
trx.addoutput(floID, receivers[floID]);
|
||||||
|
trx.addflodata(floData.replace(/\n/g, ' '));
|
||||||
|
for (let i = 0; i < wifSeq.length; i++)
|
||||||
|
trx.signinput(i, wifSeq[i], 1);
|
||||||
|
var signedTxHash = trx.serialize();
|
||||||
|
broadcastTx(signedTxHash)
|
||||||
|
.then(txid => resolve(txid))
|
||||||
|
.catch(error => reject(error))
|
||||||
|
}).catch(error => reject(error))
|
||||||
|
}).catch(error => reject(error))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
//Broadcast signed Tx in blockchain using API
|
||||||
|
const broadcastTx = floBlockchainAPI.broadcastTx = function (signedTxHash) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
if (signedTxHash.length < 1)
|
||||||
|
return reject("Empty Signature");
|
||||||
|
var url = serverList[curPos] + 'api/tx/send';
|
||||||
|
fetch(url, {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: `{"rawtx":"${signedTxHash}"}`
|
||||||
|
}).then(response => {
|
||||||
|
if (response.ok)
|
||||||
|
response.json().then(data => resolve(data.txid.result));
|
||||||
|
else
|
||||||
|
response.text().then(data => resolve(data));
|
||||||
|
}).catch(error => reject(error));
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
floBlockchainAPI.getTx = function (txid) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
promisedAPI(`api/tx/${txid}`)
|
||||||
|
.then(response => resolve(response))
|
||||||
|
.catch(error => reject(error))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
//Read Txs of Address between from and to
|
||||||
|
const readTxs = floBlockchainAPI.readTxs = function (addr, from, to) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
promisedAPI(`api/addrs/${addr}/txs?from=${from}&to=${to}`)
|
||||||
|
.then(response => resolve(response))
|
||||||
|
.catch(error => reject(error))
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
//Read All Txs of Address (newest first)
|
||||||
|
floBlockchainAPI.readAllTxs = function (addr) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
promisedAPI(`api/addrs/${addr}/txs?from=0&to=1`).then(response => {
|
||||||
|
promisedAPI(`api/addrs/${addr}/txs?from=0&to=${response.totalItems}0`)
|
||||||
|
.then(response => resolve(response.items))
|
||||||
|
.catch(error => reject(error));
|
||||||
|
}).catch(error => reject(error))
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/*Read flo Data from txs of given Address
|
||||||
|
options can be used to filter data
|
||||||
|
limit : maximum number of filtered data (default = 1000, negative = no limit)
|
||||||
|
ignoreOld : ignore old txs (default = 0)
|
||||||
|
sentOnly : filters only sent data
|
||||||
|
receivedOnly: filters only received data
|
||||||
|
pattern : filters data that with JSON pattern
|
||||||
|
filter : custom filter funtion for floData (eg . filter: d => {return d[0] == '$'})
|
||||||
|
tx : (boolean) resolve tx data or not (resolves an Array of Object with tx details)
|
||||||
|
sender : flo-id(s) of sender
|
||||||
|
receiver : flo-id(s) of receiver
|
||||||
|
*/
|
||||||
|
floBlockchainAPI.readData = function (addr, options = {}) {
|
||||||
|
options.limit = options.limit || 0;
|
||||||
|
options.ignoreOld = options.ignoreOld || 0;
|
||||||
|
if (typeof options.senders === "string") options.senders = [options.senders];
|
||||||
|
if (typeof options.receivers === "string") options.receivers = [options.receivers];
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
promisedAPI(`api/addrs/${addr}/txs?from=0&to=1`).then(response => {
|
||||||
|
var newItems = response.totalItems - options.ignoreOld;
|
||||||
|
promisedAPI(`api/addrs/${addr}/txs?from=0&to=${newItems * 2}`).then(response => {
|
||||||
|
if (options.limit <= 0)
|
||||||
|
options.limit = response.items.length;
|
||||||
|
var filteredData = [];
|
||||||
|
let numToRead = response.totalItems - options.ignoreOld,
|
||||||
|
unconfirmedCount = 0;
|
||||||
|
for (let i = 0; i < numToRead && filteredData.length < options.limit; i++) {
|
||||||
|
if (!response.items[i].confirmations) { //unconfirmed transactions
|
||||||
|
unconfirmedCount++;
|
||||||
|
if (numToRead < response.items[i].length)
|
||||||
|
numToRead++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (options.pattern) {
|
||||||
|
try {
|
||||||
|
let jsonContent = JSON.parse(response.items[i].floData);
|
||||||
|
if (!Object.keys(jsonContent).includes(options.pattern))
|
||||||
|
continue;
|
||||||
|
} catch (error) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (options.sentOnly) {
|
||||||
|
let flag = false;
|
||||||
|
for (let vin of response.items[i].vin)
|
||||||
|
if (vin.addr === addr) {
|
||||||
|
flag = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (!flag) continue;
|
||||||
|
}
|
||||||
|
if (Array.isArray(options.senders)) {
|
||||||
|
let flag = false;
|
||||||
|
for (let vin of response.items[i].vin)
|
||||||
|
if (options.senders.includes(vin.addr)) {
|
||||||
|
flag = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (!flag) continue;
|
||||||
|
}
|
||||||
|
if (options.receivedOnly) {
|
||||||
|
let flag = false;
|
||||||
|
for (let vout of response.items[i].vout)
|
||||||
|
if (vout.scriptPubKey.addresses[0] === addr) {
|
||||||
|
flag = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (!flag) continue;
|
||||||
|
}
|
||||||
|
if (Array.isArray(options.receivers)) {
|
||||||
|
let flag = false;
|
||||||
|
for (let vout of response.items[i].vout)
|
||||||
|
if (options.receivers.includes(vout.scriptPubKey.addresses[0])) {
|
||||||
|
flag = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (!flag) continue;
|
||||||
|
}
|
||||||
|
if (options.filter && !options.filter(response.items[i].floData))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (options.tx) {
|
||||||
|
let d = {}
|
||||||
|
d.txid = response.items[i].txid;
|
||||||
|
d.time = response.items[i].time;
|
||||||
|
d.blockheight = response.items[i].blockheight;
|
||||||
|
d.senders = new Set(response.items[i].vin.map(v => v.addr));
|
||||||
|
d.receivers = new Set(response.items[i].vout.map(v => v.scriptPubKey.addresses[0]));
|
||||||
|
d.data = response.items[i].floData;
|
||||||
|
filteredData.push(d);
|
||||||
|
} else
|
||||||
|
filteredData.push(response.items[i].floData);
|
||||||
|
}
|
||||||
|
resolve({
|
||||||
|
totalTxs: response.totalItems - unconfirmedCount,
|
||||||
|
data: filteredData
|
||||||
|
});
|
||||||
|
}).catch(error => {
|
||||||
|
reject(error);
|
||||||
|
});
|
||||||
|
}).catch(error => {
|
||||||
|
reject(error);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
})('object' === typeof module ? module.exports : window.floBlockchainAPI = {});
|
||||||
442
scripts/floCrypto.js
Normal file
442
scripts/floCrypto.js
Normal file
@ -0,0 +1,442 @@
|
|||||||
|
(function (EXPORTS) { //floCrypto v2.3.3e
|
||||||
|
/* FLO Crypto Operators */
|
||||||
|
'use strict';
|
||||||
|
const floCrypto = EXPORTS;
|
||||||
|
|
||||||
|
const p = BigInteger("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F", 16);
|
||||||
|
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();
|
||||||
|
// 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
function getUncompressedPublicKey(compressedPublicKey) {
|
||||||
|
// Fetch x from compressedPublicKey
|
||||||
|
let pubKeyBytes = Crypto.util.hexToBytes(compressedPublicKey);
|
||||||
|
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()
|
||||||
|
// Fetch y
|
||||||
|
let y = calculateY(x);
|
||||||
|
let yDecimalValue = y.toString();
|
||||||
|
// verify y value
|
||||||
|
let resultBigInt = y.mod(BigInteger("2"));
|
||||||
|
let check = resultBigInt.toString() % 2;
|
||||||
|
if (prefix_modulus !== check)
|
||||||
|
yDecimalValue = y.negate().mod(p).toString();
|
||||||
|
return {
|
||||||
|
x: xDecimalValue,
|
||||||
|
y: yDecimalValue
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSenderPublicKeyString() {
|
||||||
|
let privateKey = ellipticCurveEncryption.senderRandom();
|
||||||
|
var senderPublicKeyString = ellipticCurveEncryption.senderPublicString(privateKey);
|
||||||
|
return {
|
||||||
|
privateKey: privateKey,
|
||||||
|
senderPublicKeyString: senderPublicKeyString
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function deriveSharedKeySender(receiverPublicKeyHex, senderPrivateKey) {
|
||||||
|
let receiverPublicKeyString = getUncompressedPublicKey(receiverPublicKeyHex);
|
||||||
|
var senderDerivedKey = ellipticCurveEncryption.senderSharedKeyDerivation(
|
||||||
|
receiverPublicKeyString.x, receiverPublicKeyString.y, senderPrivateKey);
|
||||||
|
return senderDerivedKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
function deriveSharedKeyReceiver(senderPublicKeyString, receiverPrivateKey) {
|
||||||
|
return ellipticCurveEncryption.receiverSharedKeyDerivation(
|
||||||
|
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)
|
||||||
|
//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)
|
||||||
|
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(securedMathRandom() * (max - min + 1)) + min;
|
||||||
|
}
|
||||||
|
|
||||||
|
//generate a random String within length (options : alphaNumeric chars only)
|
||||||
|
floCrypto.randString = function (length, alphaNumeric = true) {
|
||||||
|
var result = '';
|
||||||
|
var characters = alphaNumeric ? 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' :
|
||||||
|
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_+-./*?@#&$<>=[]{}():';
|
||||||
|
for (var i = 0; i < length; i++)
|
||||||
|
result += characters.charAt(Math.floor(securedMathRandom() * characters.length));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Encrypt Data using public-key
|
||||||
|
floCrypto.encryptData = function (data, receiverPublicKeyHex) {
|
||||||
|
var senderECKeyData = getSenderPublicKeyString();
|
||||||
|
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.");
|
||||||
|
receiverECKeyData.privateKey = privateKey.privateKeyDecimal;
|
||||||
|
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);
|
||||||
|
var messageHash = Crypto.SHA256(data);
|
||||||
|
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 sigBytes = Crypto.util.hexToBytes(signatureHex);
|
||||||
|
var publicKeyPoint = ecparams.getCurve().decodePointHex(publicKeyHex);
|
||||||
|
var verify = Bitcoin.ECDSA.verify(msgHash, sigBytes, publicKeyPoint);
|
||||||
|
return verify;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Generates a new flo ID and returns private-key, public-key and floID
|
||||||
|
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.defineProperties(floCrypto, {
|
||||||
|
newID: {
|
||||||
|
get: () => generateNewID()
|
||||||
|
},
|
||||||
|
tmpID: {
|
||||||
|
get: () => {
|
||||||
|
let bytes = Crypto.util.randomBytes(20);
|
||||||
|
bytes.unshift(bitjs.pub);
|
||||||
|
var hash = Crypto.SHA256(Crypto.SHA256(bytes, {
|
||||||
|
asBytes: true
|
||||||
|
}), {
|
||||||
|
asBytes: true
|
||||||
|
});
|
||||||
|
var checksum = hash.slice(0, 4);
|
||||||
|
return bitjs.Base58.encode(bytes.concat(checksum));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
//Returns public-key from private-key
|
||||||
|
floCrypto.getPubKeyHex = function (privateKeyHex) {
|
||||||
|
if (!privateKeyHex)
|
||||||
|
return null;
|
||||||
|
var key = new Bitcoin.ECKey(privateKeyHex);
|
||||||
|
if (key.priv == null)
|
||||||
|
return null;
|
||||||
|
key.setCompressed(true);
|
||||||
|
return key.getPubKeyHex();
|
||||||
|
}
|
||||||
|
|
||||||
|
//Returns flo-ID from public-key or private-key
|
||||||
|
floCrypto.getFloID = function (keyHex) {
|
||||||
|
if (!keyHex)
|
||||||
|
return null;
|
||||||
|
try {
|
||||||
|
var key = new Bitcoin.ECKey(keyHex);
|
||||||
|
if (key.priv == null)
|
||||||
|
key.setPub(keyHex);
|
||||||
|
return key.getBitcoinAddress();
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
return false;
|
||||||
|
try {
|
||||||
|
var key = new Bitcoin.ECKey(privateKeyHex);
|
||||||
|
if (key.priv == null)
|
||||||
|
return false;
|
||||||
|
key.setCompressed(true);
|
||||||
|
if (isfloID && pubKey_floID == key.getBitcoinAddress())
|
||||||
|
return true;
|
||||||
|
else if (!isfloID && pubKey_floID == key.getPubKeyHex())
|
||||||
|
return true;
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Check if the given flo-id is valid or not
|
||||||
|
floCrypto.validateFloID = function (floID) {
|
||||||
|
if (!floID)
|
||||||
|
return false;
|
||||||
|
try {
|
||||||
|
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 = true) {
|
||||||
|
let raw = decodeAddress(address);
|
||||||
|
if (!raw)
|
||||||
|
return false;
|
||||||
|
if (typeof raw.version !== 'undefined') { //legacy or segwit
|
||||||
|
if (std == false)
|
||||||
|
return false;
|
||||||
|
else if (std === true || (!Array.isArray(std) && std === raw.version) || (Array.isArray(std) && std.includes(raw.version)))
|
||||||
|
return true;
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
} else if (typeof raw.bech_version !== 'undefined') { //bech32
|
||||||
|
if (bech === false)
|
||||||
|
return false;
|
||||||
|
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
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Check the public-key for the address (any blockchain)
|
||||||
|
floCrypto.verifyPubKey = function (pubKeyHex, address) {
|
||||||
|
let raw = decodeAddress(address),
|
||||||
|
pub_hash = Crypto.util.bytesToHex(ripemd160(Crypto.SHA256(Crypto.util.hexToBytes(pubKeyHex), {
|
||||||
|
asBytes: true
|
||||||
|
})));
|
||||||
|
return raw ? pub_hash === raw.hex : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Convert the given address (any blockchain) to equivalent floID
|
||||||
|
floCrypto.toFloID = function (address, options = null) {
|
||||||
|
if (!address)
|
||||||
|
return;
|
||||||
|
let raw = decodeAddress(address);
|
||||||
|
if (!raw)
|
||||||
|
return;
|
||||||
|
else if (options) {
|
||||||
|
if (typeof raw.version !== 'undefined' && (!options.std || !options.std.includes(raw.version)))
|
||||||
|
return;
|
||||||
|
if (typeof raw.bech_version !== 'undefined' && (!options.bech || !options.bech.includes(raw.bech_version)))
|
||||||
|
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);
|
||||||
|
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
|
||||||
|
});
|
||||||
|
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) {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//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);
|
||||||
|
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)
|
||||||
|
return null;
|
||||||
|
else if (retrieveShamirSecret(sharesArray) === str)
|
||||||
|
return true;
|
||||||
|
else
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 = {});
|
||||||
1599
scripts/floExchangeAPI.js
Normal file
1599
scripts/floExchangeAPI.js
Normal file
File diff suppressed because it is too large
Load Diff
9356
scripts/lib.js
Normal file
9356
scripts/lib.js
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user