1 line
8.2 KiB
JavaScript
1 line
8.2 KiB
JavaScript
(function(e){"use strict";function t(e,t){return new Promise((n,i)=>{let a=o.indexOf(t);-1!=a&&o.splice(a,1),l=floCrypto.randInt(0,o.length-1),r(e,!1).then(e=>n(e)).catch(e=>i(e))})}function r(e,n=!0){return new Promise((i,s)=>{if(0===o.length)n?(o=Array.from(a),l=floCrypto.randInt(0,o.length-1),r(e,!1).then(e=>i(e)).catch(e=>s(e))):s("No floSight server working");else{let r=o[l];fetch(r+e).then(n=>{n.ok?n.json().then(e=>i(e)):t(e,r).then(e=>i(e)).catch(e=>s(e))}).catch(n=>{t(e,r).then(e=>i(e)).catch(e=>s(e))})}})}const n=e,i={blockchain:floGlobals.blockchain,apiURL:{FLO:["https://flosight.duckdns.org/"],FLO_TEST:["https://testnet-flosight.duckdns.org","https://testnet.flocha.in/"]},sendAmt:.001,fee:5e-4,receiverID:floGlobals.adminID};Object.defineProperties(n,{sendAmt:{get:()=>i.sendAmt,set:e=>isNaN(e)?null:i.sendAmt=e},fee:{get:()=>i.fee,set:e=>isNaN(e)?null:i.fee=e},defaultReceiver:{get:()=>i.receiverID,set:e=>i.receiverID=e},blockchain:{get:()=>i.blockchain}}),floGlobals.sendAmt&&(n.sendAmt=floGlobals.sendAmt),floGlobals.fee&&(n.fee=floGlobals.fee),Object.defineProperties(floGlobals,{sendAmt:{get:()=>i.sendAmt,set:e=>isNaN(e)?null:i.sendAmt=e},fee:{get:()=>i.fee,set:e=>isNaN(e)?null:i.fee=e}});const a=new Set(floGlobals.apiURL&&floGlobals.apiURL[i.blockchain]?floGlobals.apiURL[i.blockchain]:i.apiURL[i.blockchain]);var o=Array.from(a),l=floCrypto.randInt(0,o.length-1);Object.defineProperties(n,{serverList:{get:()=>Array.from(o)},current_server:{get:()=>o[l]}});const s=n.promisedAPI=n.fetch=function(e){return new Promise((t,n)=>{r(e).then(e=>t(e)).catch(e=>n(e))})},c=n.getBalance=function(e){return new Promise((t,r)=>{s(`api/addr/${e}/balance`).then(e=>t(parseFloat(e))).catch(e=>r(e))})},f=n.sendTx=function(e,t,r,n,a="",o=!0){return new Promise((l,f)=>floCrypto.validateASCII(a)?floCrypto.validateFloID(e)?floCrypto.validateFloID(t)?n.length<1||!floCrypto.verifyPrivKey(n,e)?f("Invalid Private key!"):"number"!=typeof r||r<=0?f(`Invalid sendAmt : ${r}`):void c(e).then(c=>{var d=i.fee;if(c<r+d)return f("Insufficient FLO balance!");s(`api/addr/${e}`).then(i=>{h(e,0,i.unconfirmedTxApperances).then(i=>{let c={};for(let t of i.items)if(0==t.confirmations)for(let r of t.vin)r.addr===e&&(Array.isArray(c[r.txid])?c[r.txid].push(r.vout):c[r.txid]=[r.vout]);s(`api/addr/${e}/utxo`).then(i=>{for(var s=bitjs.transaction(),h=0,p=i.length-1;p>=0&&h<r+d;p--)if(i[p].confirmations||!o){if(i[p].txid in c&&c[i[p].txid].includes(i[p].vout))continue;s.addinput(i[p].txid,i[p].vout,i[p].scriptPubKey),h+=i[p].amount}if(h<r+d)f("Insufficient FLO: Some UTXOs are unconfirmed");else{s.addoutput(t,r);var m=h-r-d;m>0&&s.addoutput(e,m),s.addflodata(a.replace(/\n/g," "));var v=s.sign(n,1);u(v).then(e=>l(e)).catch(e=>f(e))}}).catch(e=>f(e))}).catch(e=>f(e))}).catch(e=>f(e))}).catch(e=>f(e)):f(`Invalid address : ${t}`):f(`Invalid address : ${e}`):f("Invalid FLO_Data: only printable ASCII characters are allowed"))};n.writeData=function(e,t,r,n=i.receiverID,a={}){let o=!1!==a.strict_utxo,l=isNaN(a.sendAmt)?i.sendAmt:a.sendAmt;return new Promise((i,a)=>{"string"!=typeof t&&(t=JSON.stringify(t)),f(e,n,l,r,t,o).then(e=>i(e)).catch(e=>a(e))})},n.mergeUTXOs=function(e,t,r=""){return new Promise((n,a)=>{if(!floCrypto.validateFloID(e))return a("Invalid floID");if(!floCrypto.verifyPrivKey(t,e))return a("Invalid Private Key");if(!floCrypto.validateASCII(r))return a("Invalid FLO_Data: only printable ASCII characters are allowed");var o=bitjs.transaction(),l=0,c=i.fee;s(`api/addr/${e}/utxo`).then(i=>{for(var s=i.length-1;s>=0;s--)i[s].confirmations&&(o.addinput(i[s].txid,i[s].vout,i[s].scriptPubKey),l+=i[s].amount);o.addoutput(e,l-c),o.addflodata(r.replace(/\n/g," "));var f=o.sign(t,1);u(f).then(e=>n(e)).catch(e=>a(e))}).catch(e=>a(e))})},n.writeDataMultiple=function(e,t,r=[i.receiverID],n=!0){return new Promise((a,o)=>{if(!Array.isArray(e))return o("Invalid senderPrivKeys: SenderPrivKeys must be Array");if(!n){let t={},n=i.sendAmt*r.length/e.length;e.forEach(e=>t[e]=n),e=t}if(!Array.isArray(r))return o("Invalid receivers: Receivers must be Array");{let e={},t=i.sendAmt;r.forEach(r=>e[r]=t),r=e}"string"!=typeof t&&(t=JSON.stringify(t)),d(e,r,t).then(e=>a(e)).catch(e=>o(e))})};const d=n.sendTxMultiple=function(e,t,r=""){return new Promise((n,a)=>{if(!floCrypto.validateASCII(r))return a("Invalid FLO_Data: only printable ASCII characters are allowed");let o,l={};try{let r={InvalidSenderPrivKeys:[],InvalidSenderAmountFor:[],InvalidReceiverIDs:[],InvalidReceiveAmountFor:[]},n=0,i=0;if(Array.isArray(e))e.forEach(e=>{try{if(e){let t=floCrypto.getFloID(e);l[t]={wif:e}}else r.InvalidSenderPrivKeys.push(e)}catch(t){r.InvalidSenderPrivKeys.push(e)}}),o=!0;else{for(let t in e)try{if(t){"number"!=typeof e[t]||e[t]<=0?r.InvalidSenderAmountFor.push(t):n+=e[t];let i=floCrypto.getFloID(t);l[i]={wif:t,coins:e[t]}}else r.InvalidSenderPrivKeys.push(t)}catch(e){r.InvalidSenderPrivKeys.push(t)}o=!1}for(let e in t)floCrypto.validateFloID(e)||r.InvalidReceiverIDs.push(e),"number"!=typeof t[e]||t[e]<=0?r.InvalidReceiveAmountFor.push(e):i+=t[e];for(let e in r)r[e].length||delete r[e];if(Object.keys(r).length)return a(r);if(!o&&n!=i)return a(`Input Amount (${n}) not equal to Output Amount (${i})`)}catch(e){return a(e)}let f=[];for(let e in l)f.push(c(e));Promise.all(f).then(e=>{let c=0,f=i.fee,d={};if(!o)var h=f/Object.keys(l).length;let p=[];for(let t in l)d[t]=parseFloat(e.shift()),(isNaN(d[t])||o&&d[t]<=f||!o&&d[t]<l[t].coins+h)&&p.push(t),c+=d[t];if(p.length)return a({InsufficientBalance:p});let m=f;for(let e in t)m+=t[e];if(c<m)return a("Insufficient total Balance");let v=[];for(let e in l)v.push(s(`api/addr/${e}/utxo`));Promise.all(v).then(e=>{let i=[];var s=bitjs.transaction();for(let t in l){let r,n=e.shift();if(o){let e=d[t]/c;r=m*e}else r=l[t].coins+h;let f=l[t].wif,u=0;for(let e=n.length-1;e>=0&&u<r;e--)n[e].confirmations&&(s.addinput(n[e].txid,n[e].vout,n[e].scriptPubKey),i.push(f),u+=n[e].amount);if(u<r)return a("Insufficient balance:"+t);let p=u-r;p>0&&s.addoutput(t,p)}for(let e in t)s.addoutput(e,t[e]);s.addflodata(r.replace(/\n/g," "));for(let e=0;e<i.length;e++)s.signinput(e,i[e],1);var f=s.serialize();u(f).then(e=>n(e)).catch(e=>a(e))}).catch(e=>a(e))}).catch(e=>a(e))})},u=n.broadcastTx=function(e){return new Promise((t,r)=>{if(e.length<1)return r("Empty Signature");var n=o[l]+"api/tx/send";fetch(n,{method:"POST",headers:{"Content-Type":"application/json"},body:`{"rawtx":"${e}"}`}).then(e=>{e.ok?e.json().then(e=>t(e.txid.result)):e.text().then(e=>t(e))}).catch(e=>r(e))})};n.getTx=function(e){return new Promise((t,r)=>{s(`api/tx/${e}`).then(e=>t(e)).catch(e=>r(e))})};const h=n.readTxs=function(e,t,r){return new Promise((n,i)=>{s(`api/addrs/${e}/txs?from=${t}&to=${r}`).then(e=>n(e)).catch(e=>i(e))})};n.readAllTxs=function(e){return new Promise((t,r)=>{s(`api/addrs/${e}/txs?from=0&to=1`).then(n=>{s(`api/addrs/${e}/txs?from=0&to=${n.totalItems}0`).then(e=>t(e.items)).catch(e=>r(e))}).catch(e=>r(e))})},n.readData=function(e,t={}){return t.limit=t.limit||0,t.ignoreOld=t.ignoreOld||0,"string"==typeof t.sender&&(t.sender=[t.sender]),"string"==typeof t.receiver&&(t.receiver=[t.receiver]),new Promise((r,n)=>{s(`api/addrs/${e}/txs?from=0&to=1`).then(i=>{var a=i.totalItems-t.ignoreOld;s(`api/addrs/${e}/txs?from=0&to=${2*a}`).then(n=>{t.limit<=0&&(t.limit=n.items.length);var i=[];let a=n.totalItems-t.ignoreOld,o=0;for(let r=0;r<a&&i.length<t.limit;r++)if(n.items[r].confirmations){if(t.pattern)try{let e=JSON.parse(n.items[r].floData);if(!Object.keys(e).includes(t.pattern))continue}catch(e){continue}if(t.sentOnly){let t=!1;for(let i of n.items[r].vin)if(i.addr===e){t=!0;break}if(!t)continue}if(Array.isArray(t.sender)){let e=!1;for(let i of n.items[r].vin)if(t.sender.includes(i.addr)){e=!0;break}if(!e)continue}if(t.receivedOnly){let t=!1;for(let i of n.items[r].vout)if(i.scriptPubKey.addresses[0]===e){t=!0;break}if(!t)continue}if(Array.isArray(t.receiver)){let e=!1;for(let i of n.items[r].vout)if(t.receiver.includes(i.scriptPubKey.addresses[0])){e=!0;break}if(!e)continue}if(!t.filter||t.filter(n.items[r].floData))if(t.tx){let e={};e.txid=n.items[r].txid,e.time=n.items[r].time,e.blockheight=n.items[r].blockheight,e.data=n.items[r].floData,i.push(e)}else i.push(n.items[r].floData)}else o++,a<n.items[r].length&&a++;r({totalTxs:n.totalItems-o,data:i})}).catch(e=>{n(e)})}).catch(e=>{n(e)})})}})("object"==typeof module?module.exports:window.floBlockchainAPI={}); |