floBlockchainAPI_v2.1.1a

- formatting the code
This commit is contained in:
sairajzero 2021-09-17 02:46:48 +05:30
parent 5c21a01b68
commit 57ef8d14df

View File

@ -7290,7 +7290,7 @@ Bitcoin.Util = {
} }
} }
</script> </script>
<script id="floBlockchainAPI" version="2.1.1"> <script id="floBlockchainAPI" version="2.1.1a">
/* FLO Blockchain Operator to send/receive data from blockchain using API calls*/ /* FLO Blockchain Operator to send/receive data from blockchain using API calls*/
const floBlockchainAPI = { const floBlockchainAPI = {
@ -7301,7 +7301,7 @@ Bitcoin.Util = {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let i = this.serverList.indexOf(rm_flosight) let i = this.serverList.indexOf(rm_flosight)
if (i != -1) this.serverList.splice(i, 1); if (i != -1) this.serverList.splice(i, 1);
this.curPos = floCrypto.randInt(0, this.serverList.length - 1) this.curPos = floCrypto.randInt(0, this.serverList.length - 1);
this.fetch_api(apicall) this.fetch_api(apicall)
.then(result => resolve(result)) .then(result => resolve(result))
.catch(error => reject(error)); .catch(error => reject(error));
@ -7310,7 +7310,7 @@ Bitcoin.Util = {
fetch_api: function(apicall) { fetch_api: function(apicall) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (this.serverList.length === 0) if (this.serverList.length === 0)
reject("No floSight server working") reject("No floSight server working");
else { else {
let flosight = this.serverList[this.curPos]; let flosight = this.serverList[this.curPos];
fetch(flosight + apicall).then(response => { fetch(flosight + apicall).then(response => {
@ -7338,7 +7338,7 @@ Bitcoin.Util = {
//Promised function to get data from API //Promised function to get data from API
promisedAPI: function(apicall) { promisedAPI: function(apicall) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
console.log(apicall) //console.log(apicall);
this.util.fetch_api(apicall) this.util.fetch_api(apicall)
.then(result => resolve(result)) .then(result => resolve(result))
.catch(error => reject(error)); .catch(error => reject(error));
@ -7361,7 +7361,7 @@ Bitcoin.Util = {
data = JSON.stringify(data); data = JSON.stringify(data);
this.sendTx(senderAddr, receiverAddr, floGlobals.sendAmt, privKey, data, strict_utxo) this.sendTx(senderAddr, receiverAddr, floGlobals.sendAmt, privKey, data, strict_utxo)
.then(txid => resolve(txid)) .then(txid => resolve(txid))
.catch(error => reject(error)) .catch(error => reject(error));
}); });
}, },
@ -7433,20 +7433,18 @@ Bitcoin.Util = {
if (!floCrypto.validateAddr(floID)) if (!floCrypto.validateAddr(floID))
return reject(`Invalid floID`); return reject(`Invalid floID`);
if (!floCrypto.verifyPrivKey(privKey, floID)) if (!floCrypto.verifyPrivKey(privKey, floID))
return reject("Invalid Private Key") return reject("Invalid Private Key");
if (!floCrypto.validateASCII(floData)) if (!floCrypto.validateASCII(floData))
return reject("Invalid FLO_Data: only printable ASCII characters are allowed"); return reject("Invalid FLO_Data: only printable ASCII characters are allowed");
var trx = bitjs.transaction(); var trx = bitjs.transaction();
var utxoAmt = 0.0; var utxoAmt = 0.0;
var fee = floGlobals.fee; var fee = floGlobals.fee;
this.promisedAPI(`api/addr/${floID}/utxo`).then(utxos => { this.promisedAPI(`api/addr/${floID}/utxo`).then(utxos => {
for (var i = utxos.length - 1; i >= 0; i--) { for (var i = utxos.length - 1; i >= 0; i--)
if (utxos[i].confirmations) { if (utxos[i].confirmations) {
trx.addinput(utxos[i].txid, utxos[i].vout, utxos[i].scriptPubKey) trx.addinput(utxos[i].txid, utxos[i].vout, utxos[i].scriptPubKey);
utxoAmt += utxos[i].amount; utxoAmt += utxos[i].amount;
} }
}
trx.addoutput(floID, utxoAmt - fee); trx.addoutput(floID, utxoAmt - fee);
trx.addflodata(floData.replace(/\n/g, ' ')); trx.addflodata(floData.replace(/\n/g, ' '));
var signedTxHash = trx.sign(privKey, 1); var signedTxHash = trx.sign(privKey, 1);
@ -7467,15 +7465,15 @@ Bitcoin.Util = {
writeDataMultiple: function(senderPrivKeys, data, receivers = [floGlobals.adminID], preserveRatio = true) { writeDataMultiple: function(senderPrivKeys, data, receivers = [floGlobals.adminID], preserveRatio = true) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (!Array.isArray(senderPrivKeys)) if (!Array.isArray(senderPrivKeys))
return reject("Invalid senderPrivKeys: SenderPrivKeys must be Array") return reject("Invalid senderPrivKeys: SenderPrivKeys must be Array");
if (!preserveRatio) { if (!preserveRatio) {
let tmp = {}; let tmp = {};
let amount = (floGlobals.sendAmt * receivers.length) / senderPrivKeys.length; let amount = (floGlobals.sendAmt * receivers.length) / senderPrivKeys.length;
senderPrivKeys.forEach(key => tmp[key] = amount); senderPrivKeys.forEach(key => tmp[key] = amount);
senderPrivKeys = tmp senderPrivKeys = tmp;
} }
if (!Array.isArray(receivers)) if (!Array.isArray(receivers))
return reject("Invalid receivers: Receivers must be Array") return reject("Invalid receivers: Receivers must be Array");
else { else {
let tmp = {}; let tmp = {};
let amount = floGlobals.sendAmt; let amount = floGlobals.sendAmt;
@ -7538,9 +7536,8 @@ Bitcoin.Util = {
if (!key) if (!key)
invalids.InvalidSenderPrivKeys.push(key); invalids.InvalidSenderPrivKeys.push(key);
else { else {
if (typeof senderPrivKeys[key] !== 'number' || senderPrivKeys[ if (typeof senderPrivKeys[key] !== 'number' || senderPrivKeys[key] <= 0)
key] <= 0) invalids.InvalidSenderAmountFor.push(key);
invalids.InvalidSenderAmountFor.push(key)
else else
inputVal += senderPrivKeys[key]; inputVal += senderPrivKeys[key];
let floID = floCrypto.getFloID(key); let floID = floCrypto.getFloID(key);
@ -7558,9 +7555,9 @@ Bitcoin.Util = {
//Validate the receiver IDs and receive amount //Validate the receiver IDs and receive amount
for (let floID in receivers) { for (let floID in receivers) {
if (!floCrypto.validateAddr(floID)) if (!floCrypto.validateAddr(floID))
invalids.InvalidReceiverIDs.push(floID) invalids.InvalidReceiverIDs.push(floID);
if (typeof receivers[floID] !== 'number' || receivers[floID] <= 0) if (typeof receivers[floID] !== 'number' || receivers[floID] <= 0)
invalids.InvalidReceiveAmountFor.push(floID) invalids.InvalidReceiveAmountFor.push(floID);
else else
outputVal += receivers[floID]; outputVal += receivers[floID];
} }
@ -7572,15 +7569,14 @@ Bitcoin.Util = {
return reject(invalids); return reject(invalids);
//Reject if given inputVal and outputVal are not equal //Reject if given inputVal and outputVal are not equal
if (!preserveRatio && inputVal != outputVal) if (!preserveRatio && inputVal != outputVal)
return reject( return reject(`Input Amount (${inputVal}) not equal to Output Amount (${outputVal})`);
`Input Amount (${inputVal}) not equal to Output Amount (${outputVal})`)
} catch (error) { } catch (error) {
return reject(error) return reject(error)
} }
//Get balance of senders //Get balance of senders
let promises = [] let promises = [];
for (let floID in senders) for (let floID in senders)
promises.push(this.getBalance(floID)) promises.push(this.getBalance(floID));
Promise.all(promises).then(results => { Promise.all(promises).then(results => {
let totalBalance = 0, let totalBalance = 0,
totalFee = floGlobals.fee, totalFee = floGlobals.fee,
@ -7592,10 +7588,9 @@ Bitcoin.Util = {
let insufficient = []; let insufficient = [];
for (let floID in senders) { for (let floID in senders) {
balance[floID] = parseFloat(results.shift()); balance[floID] = parseFloat(results.shift());
if (isNaN(balance[floID]) || (preserveRatio && balance[floID] <= if (isNaN(balance[floID]) || (preserveRatio && balance[floID] <= totalFee) ||
totalFee) || (!preserveRatio && balance[floID] < senders[floID] (!preserveRatio && balance[floID] < senders[floID].coins + dividedFee))
.coins + dividedFee)) insufficient.push(floID);
insufficient.push(floID)
totalBalance += balance[floID]; totalBalance += balance[floID];
} }
if (insufficient.length) if (insufficient.length)
@ -7607,11 +7602,11 @@ Bitcoin.Util = {
for (floID in receivers) for (floID in receivers)
totalSendAmt += receivers[floID]; totalSendAmt += receivers[floID];
if (totalBalance < totalSendAmt) if (totalBalance < totalSendAmt)
return reject("Insufficient total Balance") return reject("Insufficient total Balance");
//Get the UTXOs of the senders //Get the UTXOs of the senders
let promises = [] let promises = [];
for (floID in senders) for (floID in senders)
promises.push(this.promisedAPI(`api/addr/${floID}/utxo`)) promises.push(this.promisedAPI(`api/addr/${floID}/utxo`));
Promise.all(promises).then(results => { Promise.all(promises).then(results => {
let wifSeq = []; let wifSeq = [];
var trx = bitjs.transaction(); var trx = bitjs.transaction();
@ -7628,7 +7623,7 @@ Bitcoin.Util = {
for (let i = utxos.length - 1; for (let i = utxos.length - 1;
(i >= 0) && (utxoAmt < sendAmt); i--) { (i >= 0) && (utxoAmt < sendAmt); i--) {
if (utxos[i].confirmations) { if (utxos[i].confirmations) {
trx.addinput(utxos[i].txid, utxos[i].vout, utxos[i].scriptPubKey) trx.addinput(utxos[i].txid, utxos[i].vout, utxos[i].scriptPubKey);
wifSeq.push(wif); wifSeq.push(wif);
utxoAmt += utxos[i].amount; utxoAmt += utxos[i].amount;
} }
@ -7658,7 +7653,7 @@ Bitcoin.Util = {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
var request = new XMLHttpRequest(); var request = new XMLHttpRequest();
var url = this.util.serverList[this.util.curPos] + 'api/tx/send'; var url = this.util.serverList[this.util.curPos] + 'api/tx/send';
console.log(url) console.log(url);
if (signedTxHash.length < 1) if (signedTxHash.length < 1)
reject("Empty Signature"); reject("Empty Signature");
else { else {
@ -7719,23 +7714,21 @@ Bitcoin.Util = {
receiver : flo-id(s) of receiver receiver : flo-id(s) of receiver
*/ */
readData: function(addr, options = {}) { readData: function(addr, options = {}) {
options.limit = options.limit || 0 options.limit = options.limit || 0;
options.ignoreOld = options.ignoreOld || 0 options.ignoreOld = options.ignoreOld || 0;
if (typeof options.sender === "string") options.sender = [options.sender]; if (typeof options.sender === "string") options.sender = [options.sender];
if (typeof options.receiver === "string") options.receiver = [options.receiver]; if (typeof options.receiver === "string") options.receiver = [options.receiver];
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.promisedAPI(`api/addrs/${addr}/txs?from=0&to=1`).then(response => { this.promisedAPI(`api/addrs/${addr}/txs?from=0&to=1`).then(response => {
var newItems = response.totalItems - options.ignoreOld; var newItems = response.totalItems - options.ignoreOld;
this.promisedAPI(`api/addrs/${addr}/txs?from=0&to=${newItems*2}`).then( this.promisedAPI(`api/addrs/${addr}/txs?from=0&to=${newItems*2}`).then(response => {
response => {
if (options.limit <= 0) if (options.limit <= 0)
options.limit = response.items.length; options.limit = response.items.length;
var filteredData = []; var filteredData = [];
for (i = 0; i < (response.totalItems - options.ignoreOld) && for (let i = 0; i < (response.totalItems - options.ignoreOld) && filteredData.length < options.limit; i++) {
filteredData.length < options.limit; i++) {
if (options.pattern) { if (options.pattern) {
try { try {
let jsonContent = JSON.parse(response.items[i].floData) let jsonContent = JSON.parse(response.items[i].floData);
if (!Object.keys(jsonContent).includes(options.pattern)) if (!Object.keys(jsonContent).includes(options.pattern))
continue; continue;
} catch (error) { } catch (error) {
@ -7789,7 +7782,7 @@ Bitcoin.Util = {
d.data = response.items[i].floData; d.data = response.items[i].floData;
filteredData.push(d); filteredData.push(d);
} else } else
filteredData.push(response.items[i].floData) filteredData.push(response.items[i].floData);
} }
resolve({ resolve({
totalTxs: response.totalItems, totalTxs: response.totalItems,