floBlockchainAPI v3.0.0: BlockBook API

- Converting API calls to FLO blockbook API
- Changes and fixes required for the same
This commit is contained in:
sairajzero 2023-07-04 00:20:24 +05:30
parent 49d5c2411c
commit d15af05764

View File

@ -1,13 +1,13 @@
(function (EXPORTS) { //floBlockchainAPI v2.5.6b (function (EXPORTS) { //floBlockchainAPI v3.0.0
/* FLO Blockchain Operator to send/receive data from blockchain using API calls*/ /* FLO Blockchain Operator to send/receive data from blockchain using API calls via FLO Blockbook*/
'use strict'; 'use strict';
const floBlockchainAPI = EXPORTS; const floBlockchainAPI = EXPORTS;
const DEFAULT = { const DEFAULT = {
blockchain: floGlobals.blockchain, blockchain: floGlobals.blockchain,
apiURL: { apiURL: {
FLO: ['https://flosight.ranchimall.net/'], FLO: ['https://blockbook.ranchimall.net/'],
FLO_TEST: ['https://flosight-testnet.ranchimall.net/'] FLO_TEST: []
}, },
sendAmt: 0.0003, sendAmt: 0.0003,
fee: 0.0002, fee: 0.0002,
@ -61,9 +61,9 @@
var serverList = Array.from(allServerList); var serverList = Array.from(allServerList);
var curPos = floCrypto.randInt(0, serverList.length - 1); var curPos = floCrypto.randInt(0, serverList.length - 1);
function fetch_retry(apicall, rm_flosight) { function fetch_retry(apicall, rm_node) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let i = serverList.indexOf(rm_flosight) let i = serverList.indexOf(rm_node)
if (i != -1) serverList.splice(i, 1); if (i != -1) serverList.splice(i, 1);
curPos = floCrypto.randInt(0, serverList.length - 1); curPos = floCrypto.randInt(0, serverList.length - 1);
fetch_api(apicall, false) fetch_api(apicall, false)
@ -82,19 +82,19 @@
.then(result => resolve(result)) .then(result => resolve(result))
.catch(error => reject(error)); .catch(error => reject(error));
} else } else
reject("No floSight server working"); reject("No FLO blockbook server working");
} else { } else {
let flosight = serverList[curPos]; let serverURL = serverList[curPos];
fetch(flosight + apicall).then(response => { fetch(serverURL + apicall).then(response => {
if (response.ok) if (response.ok)
response.json().then(data => resolve(data)); response.json().then(data => resolve(data));
else { else {
fetch_retry(apicall, flosight) fetch_retry(apicall, serverURL)
.then(result => resolve(result)) .then(result => resolve(result))
.catch(error => reject(error)); .catch(error => reject(error));
} }
}).catch(error => { }).catch(error => {
fetch_retry(apicall, flosight) fetch_retry(apicall, serverURL)
.then(result => resolve(result)) .then(result => resolve(result))
.catch(error => reject(error)); .catch(error => reject(error));
}) })
@ -124,43 +124,27 @@
} }
//Get balance for the given Address //Get balance for the given Address
const getBalance = floBlockchainAPI.getBalance = function (addr, after = null) { const getBalance = floBlockchainAPI.getBalance = function (addr) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let api = `api/addr/${addr}/balance`, query_params = {}; let api = `api/address/${addr}`;
if (after) { promisedAPI(api, { details: "basic" })
if (typeof after === 'string' && /^[0-9a-z]{64}$/i.test(after)) .then(result => resolve(result["balance"]))
query_params.after = after; .catch(error => reject(error))
else return reject("Invalid 'after' parameter");
}
promisedAPI(api, query_params).then(result => {
if (typeof result === 'object' && result.lastItem) {
getBalance(addr, result.lastItem)
.then(r => resolve(util.toFixed(r + result.data)))
.catch(error => reject(error))
} else resolve(result);
}).catch(error => reject(error))
}); });
} }
const getUTXOs = address => new Promise((resolve, reject) => { function getScriptPubKey(address) {
promisedAPI(`api/addr/${address}/utxo`) var tx = bitjs.transaction();
.then(utxo => resolve(utxo)) tx.addoutput(address, 0);
.catch(error => reject(error)) let outputBuffer = tx.outputs.pop().script;
}) return Crypto.util.bytesToHex(outputBuffer)
}
const getUnconfirmedSpent = address => new Promise((resolve, reject) => { const getUTXOs = address => new Promise((resolve, reject) => {
readTxs(address, { mempool: "only" }).then(result => { promisedAPI(`api/utxo/${address}`, { confirmed: true }).then(utxos => {
let unconfirmedSpent = {}; let scriptPubKey = getScriptPubKey(address);
for (let tx of result.items) utxos.forEach(u => u.scriptPubKey = scriptPubKey);
if (tx.confirmations == 0) resolve(utxos);
for (let vin of tx.vin)
if (vin.addr === address) {
if (Array.isArray(unconfirmedSpent[vin.txid]))
unconfirmedSpent[vin.txid].push(vin.vout);
else
unconfirmedSpent[vin.txid] = [vin.vout];
}
resolve(unconfirmedSpent);
}).catch(error => reject(error)) }).catch(error => reject(error))
}) })
@ -180,32 +164,28 @@
var fee = DEFAULT.fee; var fee = DEFAULT.fee;
if (balance < sendAmt + fee) if (balance < sendAmt + fee)
return reject("Insufficient FLO balance!"); return reject("Insufficient FLO balance!");
getUnconfirmedSpent(senderAddr).then(unconfirmedSpent => { getUTXOs(senderAddr).then(utxos => {
getUTXOs(senderAddr).then(utxos => { //form/construct the transaction data
//form/construct the transaction data var trx = bitjs.transaction();
var trx = bitjs.transaction(); var utxoAmt = 0.0;
var utxoAmt = 0.0; for (var i = utxos.length - 1;
for (var i = utxos.length - 1; (i >= 0) && (utxoAmt < sendAmt + fee); i--) {
(i >= 0) && (utxoAmt < sendAmt + fee); i--) { //use only utxos with confirmations (strict_utxo mode)
//use only utxos with confirmations (strict_utxo mode) if (utxos[i].confirmations || !strict_utxo) {
if (utxos[i].confirmations || !strict_utxo) { trx.addinput(utxos[i].txid, utxos[i].vout, utxos[i].scriptPubKey);
if (utxos[i].txid in unconfirmedSpent && unconfirmedSpent[utxos[i].txid].includes(utxos[i].vout)) utxoAmt += utxos[i].amount;
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 {
if (utxoAmt < sendAmt + fee) trx.addoutput(receiverAddr, sendAmt);
reject("Insufficient FLO: Some UTXOs are unconfirmed"); var change = utxoAmt - sendAmt - fee;
else { if (change > DEFAULT.minChangeAmt)
trx.addoutput(receiverAddr, sendAmt); trx.addoutput(senderAddr, change);
var change = utxoAmt - sendAmt - fee; trx.addflodata(floData.replace(/\n/g, ' '));
if (change > DEFAULT.minChangeAmt) resolve(trx);
trx.addoutput(senderAddr, change); }
trx.addflodata(floData.replace(/\n/g, ' '));
resolve(trx);
}
}).catch(error => reject(error))
}).catch(error => reject(error)) }).catch(error => reject(error))
}).catch(error => reject(error)) }).catch(error => reject(error))
}) })
@ -293,34 +273,30 @@
if (balance < totalAmt + fee) if (balance < totalAmt + fee)
return reject("Insufficient FLO balance!"); return reject("Insufficient FLO balance!");
//get unconfirmed tx list //get unconfirmed tx list
getUnconfirmedSpent(floID).then(unconfirmedSpent => { getUTXOs(floID).then(utxos => {
getUTXOs(floID).then(utxos => { var trx = bitjs.transaction();
var trx = bitjs.transaction(); var utxoAmt = 0.0;
var utxoAmt = 0.0; for (let i = utxos.length - 1; (i >= 0) && (utxoAmt < totalAmt + fee); i--) {
for (let i = utxos.length - 1; (i >= 0) && (utxoAmt < totalAmt + fee); i--) { //use only utxos with confirmations (strict_utxo mode)
//use only utxos with confirmations (strict_utxo mode) if (utxos[i].confirmations || !strict_utxo) {
if (utxos[i].confirmations || !strict_utxo) { trx.addinput(utxos[i].txid, utxos[i].vout, utxos[i].scriptPubKey);
if (utxos[i].txid in unconfirmedSpent && unconfirmedSpent[utxos[i].txid].includes(utxos[i].vout)) utxoAmt += utxos[i].amount;
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 < totalAmt + fee)
}; reject("Insufficient FLO: Some UTXOs are unconfirmed");
} else {
if (utxoAmt < totalAmt + fee) for (let i = 0; i < count; i++)
reject("Insufficient FLO: Some UTXOs are unconfirmed"); trx.addoutput(floID, splitAmt);
else { var change = utxoAmt - totalAmt - fee;
for (let i = 0; i < count; i++) if (change > DEFAULT.minChangeAmt)
trx.addoutput(floID, splitAmt); trx.addoutput(floID, change);
var change = utxoAmt - totalAmt - fee; trx.addflodata(floData.replace(/\n/g, ' '));
if (change > DEFAULT.minChangeAmt) var signedTxHash = trx.sign(privKey, 1);
trx.addoutput(floID, change); broadcastTx(signedTxHash)
trx.addflodata(floData.replace(/\n/g, ' ')); .then(txid => resolve(txid))
var signedTxHash = trx.sign(privKey, 1); .catch(error => reject(error))
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)) }).catch(error => reject(error))
}) })
@ -551,33 +527,29 @@
var fee = DEFAULT.fee; var fee = DEFAULT.fee;
if (balance < sendAmt + fee) if (balance < sendAmt + fee)
return reject("Insufficient FLO balance!"); return reject("Insufficient FLO balance!");
getUnconfirmedSpent(senderAddr).then(unconfirmedSpent => { getUTXOs(senderAddr).then(utxos => {
getUTXOs(senderAddr).then(utxos => { //form/construct the transaction data
//form/construct the transaction data var trx = bitjs.transaction();
var trx = bitjs.transaction(); var utxoAmt = 0.0;
var utxoAmt = 0.0; for (var i = utxos.length - 1;
for (var i = utxos.length - 1; (i >= 0) && (utxoAmt < sendAmt + fee); i--) {
(i >= 0) && (utxoAmt < sendAmt + fee); i--) { //use only utxos with confirmations (strict_utxo mode)
//use only utxos with confirmations (strict_utxo mode) if (utxos[i].confirmations || !strict_utxo) {
if (utxos[i].confirmations || !strict_utxo) { trx.addinput(utxos[i].txid, utxos[i].vout, redeemScript); //for multisig, script=redeemScript
if (utxos[i].txid in unconfirmedSpent && unconfirmedSpent[utxos[i].txid].includes(utxos[i].vout)) utxoAmt += utxos[i].amount;
continue; //A transaction has already used the utxo, but is unconfirmed. };
trx.addinput(utxos[i].txid, utxos[i].vout, redeemScript); //for multisig, script=redeemScript }
utxoAmt += utxos[i].amount; if (utxoAmt < sendAmt + fee)
}; reject("Insufficient FLO: Some UTXOs are unconfirmed");
} else {
if (utxoAmt < sendAmt + fee) for (let i in receivers)
reject("Insufficient FLO: Some UTXOs are unconfirmed"); trx.addoutput(receivers[i], amounts[i]);
else { var change = utxoAmt - sendAmt - fee;
for (let i in receivers) if (change > DEFAULT.minChangeAmt)
trx.addoutput(receivers[i], amounts[i]); trx.addoutput(senderAddr, change);
var change = utxoAmt - sendAmt - fee; trx.addflodata(floData.replace(/\n/g, ' '));
if (change > DEFAULT.minChangeAmt) resolve(trx);
trx.addoutput(senderAddr, change); }
trx.addflodata(floData.replace(/\n/g, ' '));
resolve(trx);
}
}).catch(error => reject(error))
}).catch(error => reject(error)) }).catch(error => reject(error))
}).catch(error => reject(error)) }).catch(error => reject(error))
}); });
@ -773,20 +745,11 @@
const broadcastTx = floBlockchainAPI.broadcastTx = function (signedTxHash) { const broadcastTx = floBlockchainAPI.broadcastTx = function (signedTxHash) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (signedTxHash.length < 1) if (signedTxHash.length < 1)
return reject("Empty Signature"); return reject("Empty Transaction Data");
var url = serverList[curPos] + 'api/tx/send';
fetch(url, { promisedAPI('/api/sendtx/' + signedTxHash)
method: "POST", .then(response => resolve(response["result"]))
headers: { .catch(error => reject(error))
'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));
}) })
} }
@ -825,27 +788,27 @@
}) })
} }
//Read Txs of Address between from and to //Read Txs of Address
const readTxs = floBlockchainAPI.readTxs = function (addr, options = {}) { const readTxs = floBlockchainAPI.readTxs = function (addr, options = {}) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let api = `api/addrs/${addr}/txs`; let api = `api/addrs/${addr}/txs`;
//API options //API options
let query_params = {}; let query_params = {};
if (!isUndefined(options.after) || !isUndefined(options.before)) { //page options
if (!isUndefined(options.after)) if (!isUndefined(options.page) && Number.isInteger(options.page))
query_params.after = options.after; query_params.page = options.page;
if (!isUndefined(options.before)) if (!isUndefined(options.pageSize) && Number.isInteger(options.pageSize))
query_params.before = options.before; query_params.pageSize = options.pageSize;
} else { //only confirmed tx
if (!isUndefined(options.from)) if (options.confirmed) //Default is false in server, so only add confirmed filter if confirmed has a true value
query_params.from = options.from; query_params.confirmed = true;
if (!isUndefined(options.to)) //from block number
query_params.to = options.to; if (!isUndefined(options.fromBlock) && Number.isInteger(options.fromBlock))
} query_params.from = options.fromBlock;
if (!isUndefined(options.latest)) //to block number
query_params.latest = options.latest; if (!isUndefined(options.toBlock) && Number.isInteger(options.toBlock))
if (!isUndefined(options.mempool)) query_params.to = options.toBlock;
query_params.mempool = options.mempool;
promisedAPI(api, query_params) promisedAPI(api, query_params)
.then(response => resolve(response)) .then(response => resolve(response))
.catch(error => reject(error)) .catch(error => reject(error))