updating floBlockchainAPI to latest (v2.1.1)
This commit is contained in:
parent
20fd7aaeb1
commit
c8e27f7315
@ -1,43 +1,49 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
/* FLO Blockchain Operator to send/receive data from blockchain using API calls*/
|
/* FLO Blockchain Operator to send/receive data from blockchain using API calls*/
|
||||||
(function(GLOBAL){
|
(function(GLOBAL) {
|
||||||
const floBlockchainAPI = GLOBAL.floBlockchainAPI = {
|
const floBlockchainAPI = GLOBAL.floBlockchainAPI = {
|
||||||
|
|
||||||
util: {
|
util: {
|
||||||
serverList: floGlobals.apiURL[floGlobals.blockchain].slice(0),
|
serverList: floGlobals.apiURL[floGlobals.blockchain].slice(0),
|
||||||
curPos: floCrypto.randInt(0, floGlobals.apiURL[floGlobals.blockchain].length - 1),
|
curPos: floCrypto.randInt(0, floGlobals.apiURL[floGlobals.blockchain].length - 1),
|
||||||
fetch_retry: function(apicall) {
|
fetch_retry: function(apicall, rm_flosight) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
this.serverList.splice(this.curPos, 1);
|
let i = this.serverList.indexOf(rm_flosight)
|
||||||
|
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));
|
||||||
});
|
})
|
||||||
},
|
},
|
||||||
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 {
|
||||||
fetch(this.serverList[this.curPos] + apicall).then(response => {
|
let flosight = this.serverList[this.curPos];
|
||||||
|
fetch(flosight + apicall).then(response => {
|
||||||
if (response.ok)
|
if (response.ok)
|
||||||
response.json().then(data => resolve(data));
|
response.json().then(data => resolve(data));
|
||||||
else {
|
else {
|
||||||
this.fetch_retry(apicall)
|
this.fetch_retry(apicall, flosight)
|
||||||
.then(result => resolve(result))
|
.then(result => resolve(result))
|
||||||
.catch(error => reject(error));
|
.catch(error => reject(error));
|
||||||
};
|
}
|
||||||
}).catch(error => {
|
}).catch(error => {
|
||||||
this.fetch_retry(apicall)
|
this.fetch_retry(apicall, flosight)
|
||||||
.then(result => resolve(result))
|
.then(result => resolve(result))
|
||||||
.catch(error => reject(error));
|
.catch(error => reject(error));
|
||||||
});
|
})
|
||||||
};
|
}
|
||||||
});
|
})
|
||||||
|
},
|
||||||
|
|
||||||
|
current: function() {
|
||||||
|
return this.serverList[this.curPos];
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
//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) => {
|
||||||
@ -47,7 +53,7 @@
|
|||||||
.catch(error => reject(error));
|
.catch(error => reject(error));
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
//Get balance for the given Address
|
//Get balance for the given Address
|
||||||
getBalance: function(addr) {
|
getBalance: function(addr) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
@ -56,58 +62,80 @@
|
|||||||
.catch(error => reject(error));
|
.catch(error => reject(error));
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
//Write Data into blockchain
|
//Write Data into blockchain
|
||||||
writeData: function(senderAddr, data, privKey, receiverAddr = floGlobals.adminID) {
|
writeData: function(senderAddr, data, privKey, receiverAddr = floGlobals.adminID, strict_utxo = true) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (typeof data != "string")
|
if (typeof data != "string")
|
||||||
data = JSON.stringify(data);
|
data = JSON.stringify(data);
|
||||||
this.sendTx(senderAddr, receiverAddr, floGlobals.sendAmt, privKey, data)
|
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));
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
//Send Tx to blockchain
|
//Send Tx to blockchain
|
||||||
sendTx: function(senderAddr, receiverAddr, sendAmt, privKey, floData = '') {
|
sendTx: function(senderAddr, receiverAddr, sendAmt, privKey, floData = '', strict_utxo = true) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (!floCrypto.validateAddr(senderAddr))
|
if (!floCrypto.validateASCII(floData))
|
||||||
reject(`Invalid address : ${senderAddr}`);
|
return reject("Invalid FLO_Data: only printable ASCII characters are allowed");
|
||||||
|
else if (!floCrypto.validateAddr(senderAddr))
|
||||||
|
return reject(`Invalid address : ${senderAddr}`);
|
||||||
else if (!floCrypto.validateAddr(receiverAddr))
|
else if (!floCrypto.validateAddr(receiverAddr))
|
||||||
reject(`Invalid address : ${receiverAddr}`);
|
return reject(`Invalid address : ${receiverAddr}`);
|
||||||
else if (privKey.length < 1 || !floCrypto.verifyPrivKey(privKey, senderAddr))
|
else if (privKey.length < 1 || !floCrypto.verifyPrivKey(privKey, senderAddr))
|
||||||
reject("Invalid Private key!");
|
return reject("Invalid Private key!");
|
||||||
else if (typeof sendAmt !== 'number' || sendAmt <= 0)
|
else if (typeof sendAmt !== 'number' || sendAmt <= 0)
|
||||||
reject(`Invalid sendAmt : ${sendAmt}`);
|
return reject(`Invalid sendAmt : ${sendAmt}`);
|
||||||
else {
|
|
||||||
var trx = bitjs.transaction();
|
//get unconfirmed tx list
|
||||||
var utxoAmt = 0.0;
|
this.promisedAPI(`api/addr/${senderAddr}`).then(result => {
|
||||||
var fee = floGlobals.fee;
|
this.readTxs(senderAddr, 0, result.unconfirmedTxApperances).then(result => {
|
||||||
this.promisedAPI(`api/addr/${senderAddr}/utxo`).then(utxos => {
|
let unconfirmedSpent = {};
|
||||||
for (var i = utxos.length - 1; (i >= 0) && (utxoAmt < sendAmt + fee); i--) {
|
for (let tx of result.items)
|
||||||
if (utxos[i].confirmations) {
|
if (tx.confirmations == 0)
|
||||||
trx.addinput(utxos[i].txid, utxos[i].vout, utxos[i].scriptPubKey);
|
for (let vin of tx.vin)
|
||||||
utxoAmt += utxos[i].amount;
|
if (vin.addr === senderAddr) {
|
||||||
} else break;
|
if (Array.isArray(unconfirmedSpent[vin.txid]))
|
||||||
};
|
unconfirmedSpent[vin.txid].push(vin.vout);
|
||||||
if (utxoAmt < sendAmt + fee)
|
else
|
||||||
reject("Insufficient balance!");
|
unconfirmedSpent[vin.txid] = [vin.vout];
|
||||||
else {
|
}
|
||||||
trx.addoutput(receiverAddr, sendAmt);
|
//get utxos list
|
||||||
var change = utxoAmt - sendAmt - fee;
|
this.promisedAPI(`api/addr/${senderAddr}/utxo`).then(utxos => {
|
||||||
if (change > 0)
|
//form/construct the transaction data
|
||||||
trx.addoutput(senderAddr, change);
|
var trx = bitjs.transaction();
|
||||||
trx.addflodata(floData.replace(/\n/g, ' '));
|
var utxoAmt = 0.0;
|
||||||
var signedTxHash = trx.sign(privKey, 1);
|
var fee = floGlobals.fee;
|
||||||
this.broadcastTx(signedTxHash)
|
for (var i = utxos.length - 1;
|
||||||
.then(txid => resolve(txid))
|
(i >= 0) && (utxoAmt < sendAmt + fee); i--) {
|
||||||
.catch(error => reject(error));
|
//use only utxos with confirmations (strict_utxo mode)
|
||||||
};
|
if (utxos[i].confirmations || !strict_utxo) {
|
||||||
}).catch(error => reject(error));
|
if (utxos[i].txid in unconfirmedSpent && unconfirmedSpent[utxos[i].txid].includes(utxos[i].vout))
|
||||||
};
|
continue; //A transaction has already used this 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 balance!");
|
||||||
|
else {
|
||||||
|
trx.addoutput(receiverAddr, sendAmt);
|
||||||
|
var change = utxoAmt - sendAmt - fee;
|
||||||
|
if (change > 0)
|
||||||
|
trx.addoutput(senderAddr, change);
|
||||||
|
trx.addflodata(floData.replace(/\n/g, ' '));
|
||||||
|
var signedTxHash = trx.sign(privKey, 1);
|
||||||
|
this.broadcastTx(signedTxHash)
|
||||||
|
.then(txid => resolve(txid))
|
||||||
|
.catch(error => reject(error))
|
||||||
|
}
|
||||||
|
}).catch(error => reject(error))
|
||||||
|
}).catch(error => reject(error))
|
||||||
|
}).catch(error => reject(error))
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
//merge all UTXOs of a given floID into a single UTXO
|
//merge all UTXOs of a given floID into a single UTXO
|
||||||
mergeUTXOs: function(floID, privKey, floData = '') {
|
mergeUTXOs: function(floID, privKey, floData = '') {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
@ -115,7 +143,8 @@
|
|||||||
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))
|
||||||
|
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;
|
||||||
@ -124,17 +153,17 @@
|
|||||||
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);
|
||||||
this.broadcastTx(signedTxHash)
|
this.broadcastTx(signedTxHash)
|
||||||
.then(txid => resolve(txid))
|
.then(txid => resolve(txid))
|
||||||
.catch(error => reject(error));
|
.catch(error => reject(error))
|
||||||
}).catch(error => reject(error));
|
}).catch(error => reject(error))
|
||||||
});
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
/**Write data into blockchain from (and/or) to multiple floID
|
/**Write data into blockchain from (and/or) to multiple floID
|
||||||
* @param {Array} senderPrivKeys List of sender private-keys
|
* @param {Array} senderPrivKeys List of sender private-keys
|
||||||
* @param {string} data FLO data of the txn
|
* @param {string} data FLO data of the txn
|
||||||
@ -151,23 +180,23 @@
|
|||||||
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;
|
||||||
receivers.forEach(floID => tmp[floID] = amount);
|
receivers.forEach(floID => tmp[floID] = amount);
|
||||||
receivers = tmp;
|
receivers = tmp
|
||||||
};
|
}
|
||||||
if (typeof data != "string")
|
if (typeof data != "string")
|
||||||
data = JSON.stringify(data);
|
data = JSON.stringify(data);
|
||||||
this.sendTxMultiple(senderPrivKeys, receivers, data)
|
this.sendTxMultiple(senderPrivKeys, receivers, data)
|
||||||
.then(txid => resolve(txid))
|
.then(txid => resolve(txid))
|
||||||
.catch(error => reject(error));
|
.catch(error => reject(error))
|
||||||
});
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
/**Send Tx from (and/or) to multiple floID
|
/**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 {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 {Object} receivers List of receivers with respective amount to be sent
|
||||||
@ -176,7 +205,8 @@
|
|||||||
*/
|
*/
|
||||||
sendTxMultiple: function(senderPrivKeys, receivers, floData = '') {
|
sendTxMultiple: function(senderPrivKeys, receivers, floData = '') {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
if (!floCrypto.validateASCII(floData))
|
||||||
|
return reject("Invalid FLO_Data: only printable ASCII characters are allowed");
|
||||||
let senders = {},
|
let senders = {},
|
||||||
preserveRatio;
|
preserveRatio;
|
||||||
//check for argument validations
|
//check for argument validations
|
||||||
@ -186,7 +216,7 @@
|
|||||||
InvalidSenderAmountFor: [],
|
InvalidSenderAmountFor: [],
|
||||||
InvalidReceiverIDs: [],
|
InvalidReceiverIDs: [],
|
||||||
InvalidReceiveAmountFor: []
|
InvalidReceiveAmountFor: []
|
||||||
};
|
}
|
||||||
let inputVal = 0,
|
let inputVal = 0,
|
||||||
outputVal = 0;
|
outputVal = 0;
|
||||||
//Validate sender privatekeys (and send amount if passed)
|
//Validate sender privatekeys (and send amount if passed)
|
||||||
@ -200,12 +230,12 @@
|
|||||||
let floID = floCrypto.getFloID(key);
|
let floID = floCrypto.getFloID(key);
|
||||||
senders[floID] = {
|
senders[floID] = {
|
||||||
wif: key
|
wif: key
|
||||||
};
|
}
|
||||||
};
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
invalids.InvalidSenderPrivKeys.push(key);
|
invalids.InvalidSenderPrivKeys.push(key)
|
||||||
};
|
}
|
||||||
});
|
})
|
||||||
preserveRatio = true;
|
preserveRatio = true;
|
||||||
}
|
}
|
||||||
//conversion when privatekeys are passed with send amount
|
//conversion when privatekeys are passed with send amount
|
||||||
@ -223,14 +253,14 @@
|
|||||||
senders[floID] = {
|
senders[floID] = {
|
||||||
wif: key,
|
wif: key,
|
||||||
coins: senderPrivKeys[key]
|
coins: senderPrivKeys[key]
|
||||||
};
|
}
|
||||||
};
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
invalids.InvalidSenderPrivKeys.push(key);
|
invalids.InvalidSenderPrivKeys.push(key)
|
||||||
};
|
}
|
||||||
};
|
}
|
||||||
preserveRatio = false;
|
preserveRatio = false;
|
||||||
};
|
}
|
||||||
//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))
|
||||||
@ -239,7 +269,7 @@
|
|||||||
invalids.InvalidReceiveAmountFor.push(floID);
|
invalids.InvalidReceiveAmountFor.push(floID);
|
||||||
else
|
else
|
||||||
outputVal += receivers[floID];
|
outputVal += receivers[floID];
|
||||||
};
|
}
|
||||||
//Reject if any invalids are found
|
//Reject if any invalids are found
|
||||||
for (let i in invalids)
|
for (let i in invalids)
|
||||||
if (!invalids[i].length)
|
if (!invalids[i].length)
|
||||||
@ -250,7 +280,7 @@
|
|||||||
if (!preserveRatio && inputVal != outputVal)
|
if (!preserveRatio && inputVal != outputVal)
|
||||||
return reject(`Input Amount (${inputVal}) not equal to Output Amount (${outputVal})`);
|
return reject(`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 = [];
|
||||||
@ -267,15 +297,15 @@
|
|||||||
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] <= totalFee)
|
if (isNaN(balance[floID]) || (preserveRatio && balance[floID] <= totalFee) ||
|
||||||
|| (!preserveRatio && balance[floID] < senders[floID].coins + dividedFee))
|
(!preserveRatio && balance[floID] < senders[floID].coins + dividedFee))
|
||||||
insufficient.push(floID);
|
insufficient.push(floID);
|
||||||
totalBalance += balance[floID];
|
totalBalance += balance[floID];
|
||||||
};
|
}
|
||||||
if (insufficient.length)
|
if (insufficient.length)
|
||||||
return reject({
|
return reject({
|
||||||
InsufficientBalance: insufficient
|
InsufficientBalance: insufficient
|
||||||
});
|
})
|
||||||
//Calculate totalSentAmount and check if totalBalance is sufficient
|
//Calculate totalSentAmount and check if totalBalance is sufficient
|
||||||
let totalSendAmt = totalFee;
|
let totalSendAmt = totalFee;
|
||||||
for (floID in receivers)
|
for (floID in receivers)
|
||||||
@ -299,18 +329,20 @@
|
|||||||
sendAmt = senders[floID].coins + dividedFee;
|
sendAmt = senders[floID].coins + dividedFee;
|
||||||
let wif = senders[floID].wif;
|
let wif = senders[floID].wif;
|
||||||
let utxoAmt = 0.0;
|
let utxoAmt = 0.0;
|
||||||
for (let i = utxos.length - 1; (i >= 0) && (utxoAmt < sendAmt); i--)
|
for (let i = utxos.length - 1;
|
||||||
|
(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;
|
||||||
};
|
}
|
||||||
|
}
|
||||||
if (utxoAmt < sendAmt)
|
if (utxoAmt < sendAmt)
|
||||||
return reject("Insufficient balance:" + floID);
|
return reject("Insufficient balance:" + floID);
|
||||||
let change = (utxoAmt - sendAmt);
|
let change = (utxoAmt - sendAmt);
|
||||||
if (change > 0)
|
if (change > 0)
|
||||||
trx.addoutput(floID, change);
|
trx.addoutput(floID, change);
|
||||||
};
|
}
|
||||||
for (floID in receivers)
|
for (floID in receivers)
|
||||||
trx.addoutput(floID, receivers[floID]);
|
trx.addoutput(floID, receivers[floID]);
|
||||||
trx.addflodata(floData.replace(/\n/g, ' '));
|
trx.addflodata(floData.replace(/\n/g, ' '));
|
||||||
@ -319,18 +351,18 @@
|
|||||||
var signedTxHash = trx.serialize();
|
var signedTxHash = trx.serialize();
|
||||||
this.broadcastTx(signedTxHash)
|
this.broadcastTx(signedTxHash)
|
||||||
.then(txid => resolve(txid))
|
.then(txid => resolve(txid))
|
||||||
.catch(error => reject(error));
|
.catch(error => reject(error))
|
||||||
}).catch(error => reject(error));
|
}).catch(error => reject(error))
|
||||||
}).catch(error => reject(error));
|
}).catch(error => reject(error))
|
||||||
});
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
//Broadcast signed Tx in blockchain using API
|
//Broadcast signed Tx in blockchain using API
|
||||||
broadcastTx: function(signedTxHash) {
|
broadcastTx: function(signedTxHash) {
|
||||||
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 {
|
||||||
@ -346,19 +378,27 @@
|
|||||||
reject(request.responseText);
|
reject(request.responseText);
|
||||||
}
|
}
|
||||||
request.send(params);
|
request.send(params);
|
||||||
};
|
}
|
||||||
});
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
|
getTx: function(txid) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
this.promisedAPI(`api/tx/${txid}`)
|
||||||
|
.then(response => resolve(response))
|
||||||
|
.catch(error => reject(error))
|
||||||
|
})
|
||||||
|
},
|
||||||
|
|
||||||
//Read Txs of Address between from and to
|
//Read Txs of Address between from and to
|
||||||
readTxs: function(addr, from, to) {
|
readTxs: function(addr, from, to) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
this.promisedAPI(`api/addrs/${addr}/txs?from=${from}&to=${to}`)
|
this.promisedAPI(`api/addrs/${addr}/txs?from=${from}&to=${to}`)
|
||||||
.then(response => resolve(response))
|
.then(response => resolve(response))
|
||||||
.catch(error => reject(error));
|
.catch(error => reject(error))
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
//Read All Txs of Address (newest first)
|
//Read All Txs of Address (newest first)
|
||||||
readAllTxs: function(addr) {
|
readAllTxs: function(addr) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
@ -366,46 +406,95 @@
|
|||||||
this.promisedAPI(`api/addrs/${addr}/txs?from=0&to=${response.totalItems}0`)
|
this.promisedAPI(`api/addrs/${addr}/txs?from=0&to=${response.totalItems}0`)
|
||||||
.then(response => resolve(response.items))
|
.then(response => resolve(response.items))
|
||||||
.catch(error => reject(error));
|
.catch(error => reject(error));
|
||||||
}).catch(error => reject(error));
|
}).catch(error => reject(error))
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
/*Read flo Data from txs of given Address
|
/*Read flo Data from txs of given Address
|
||||||
options can be used to filter data
|
options can be used to filter data
|
||||||
limit : maximum number of filtered data (default = 1000, negative = no limit)
|
limit : maximum number of filtered data (default = 1000, negative = no limit)
|
||||||
ignoreOld : ignore old txs (default = 0)
|
ignoreOld : ignore old txs (default = 0)
|
||||||
sentOnly : filters only sent data
|
sentOnly : filters only sent data
|
||||||
pattern : filters data that starts with a pattern
|
receivedOnly: filters only received data
|
||||||
contains : filters data that contains a string
|
pattern : filters data that with JSON pattern
|
||||||
filter : custom filter funtion for floData (eg . filter: d => {return d[0] == '$'})
|
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
|
||||||
*/
|
*/
|
||||||
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.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(response => {
|
this.promisedAPI(`api/addrs/${addr}/txs?from=0&to=${newItems*2}`).then(
|
||||||
|
response => {
|
||||||
if (options.limit <= 0)
|
if (options.limit <= 0)
|
||||||
options.limit = response.items.length;
|
options.limit = response.items.length;
|
||||||
var filteredData = [];
|
var filteredData = [];
|
||||||
for (let 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.sentOnly && response.items[i].vin[0].addr !== addr)
|
|
||||||
continue;
|
|
||||||
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) {
|
||||||
continue;
|
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.sender)) {
|
||||||
|
let flag = false;
|
||||||
|
for (let vin of response.items[i].vin)
|
||||||
|
if (options.sender.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.receiver)) {
|
||||||
|
let flag = false;
|
||||||
|
for (let vout of response.items[i].vout)
|
||||||
|
if (options.receiver.includes(vout.scriptPubKey.addresses[0])) {
|
||||||
|
flag = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (!flag) continue;
|
||||||
|
}
|
||||||
if (options.filter && !options.filter(response.items[i].floData))
|
if (options.filter && !options.filter(response.items[i].floData))
|
||||||
continue;
|
continue;
|
||||||
filteredData.push(response.items[i].floData);
|
|
||||||
};
|
if (options.tx) {
|
||||||
|
let d = {}
|
||||||
|
d.txid = response.items[i].txid;
|
||||||
|
d.time = response.items[i].time;
|
||||||
|
d.blockheight = response.items[i].blockheight;
|
||||||
|
d.data = response.items[i].floData;
|
||||||
|
filteredData.push(d);
|
||||||
|
} else
|
||||||
|
filteredData.push(response.items[i].floData);
|
||||||
|
}
|
||||||
resolve({
|
resolve({
|
||||||
totalTxs: response.totalItems,
|
totalTxs: response.totalItems,
|
||||||
data: filteredData
|
data: filteredData
|
||||||
@ -419,4 +508,4 @@
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})(typeof global !== "undefined" ? global : window);
|
})(typeof global !== "undefined" ? global : window);
|
||||||
Loading…
Reference in New Issue
Block a user