Adding tor API support
This commit is contained in:
parent
7a99b45b42
commit
7757ac3239
@ -1,4 +1,4 @@
|
||||
(function (EXPORTS) { //btcOperator v1.2.7
|
||||
(function (EXPORTS) { //btcOperator v1.2.8
|
||||
/* BTC Crypto and API Operator */
|
||||
const btcOperator = EXPORTS;
|
||||
const SATOSHI_IN_BTC = 1e8;
|
||||
@ -22,6 +22,7 @@
|
||||
try {
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
mode: 'no-cors',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
@ -56,7 +57,7 @@
|
||||
},
|
||||
async broadcast({ rawTxHex, url }) {
|
||||
try {
|
||||
const result = await post(`${url || this.url}txs/push`, { tx: rawTxHex })
|
||||
const result = await post(`${url || this.url}pushtx`, { tx: rawTxHex })
|
||||
return result.hash
|
||||
} catch (e) {
|
||||
throw e
|
||||
@ -78,16 +79,19 @@
|
||||
latestBlock() {
|
||||
return fetch_api(`blocks/tip/height`, { url: this.url })
|
||||
},
|
||||
// tx({ txid, url }) {
|
||||
// return fetch_api(`tx/${txid}`, { url: url || this.url })
|
||||
// .then(result => formatTx(result))
|
||||
// },
|
||||
// txHex({ txid, url }) {
|
||||
// return fetch_api(`tx/${txid}/hex`, { url: url || this.url, asText: true })
|
||||
// },
|
||||
// txs({ addr, before, after, url }) {
|
||||
// return fetch_api(`address/${addr}/txs${before ? `?before=${before}` : ''}${after ? `?after=${after}` : ''}`, { url: url || this.url })
|
||||
// },
|
||||
tx({ txid, url }) {
|
||||
return fetch_api(`tx/${txid}`, { url: url || this.url })
|
||||
.then(result => formatTx(result))
|
||||
},
|
||||
txHex({ txid, url }) {
|
||||
return fetch_api(`tx/${txid}/hex`, { url: url || this.url, asText: true })
|
||||
},
|
||||
txs({ addr, url, ...args }) {
|
||||
let queryParams = Object.entries(args).map(([key, value]) => `${key}=${value}`).join('&')
|
||||
if (queryParams)
|
||||
queryParams = '?' + queryParams
|
||||
return fetch_api(`address/${addr}/txs${queryParams}`, { url: url || this.url })
|
||||
},
|
||||
async block({ id, url }) {
|
||||
// if id is hex string then it is block hash
|
||||
try {
|
||||
@ -114,17 +118,20 @@
|
||||
latestBlock() {
|
||||
return fetch_api(`blocks/tip/height`, { url: this.url })
|
||||
},
|
||||
// tx({ txid }) {
|
||||
// return fetch_api(`tx/${txid}`, { url: this.url })
|
||||
// .then(result => formatTx(result))
|
||||
tx({ txid }) {
|
||||
return fetch_api(`tx/${txid}`, { url: this.url })
|
||||
.then(result => formatTx(result))
|
||||
|
||||
// },
|
||||
// txHex({ txid }) {
|
||||
// return fetch_api(`tx/${txid}/hex`, { url: this.url, asText: true })
|
||||
// },
|
||||
// txs({ addr, before, after }) {
|
||||
// return fetch_api(`address/${addr}/txs${before ? `?before=${before}` : ''}${after ? `?after=${after}` : ''}`, { url: this.url })
|
||||
// },
|
||||
},
|
||||
txHex({ txid }) {
|
||||
return fetch_api(`tx/${txid}/hex`, { url: this.url, asText: true })
|
||||
},
|
||||
txs({ addr, ...args }) {
|
||||
let queryParams = Object.entries(args).map(([key, value]) => `${key}=${value}`).join('&')
|
||||
if (queryParams)
|
||||
queryParams = '?' + queryParams
|
||||
return fetch_api(`address/${addr}/txs${queryParams}`, { url: this.url })
|
||||
},
|
||||
async block({ id }) {
|
||||
// if id is hex string then it is block hash
|
||||
try {
|
||||
@ -159,8 +166,11 @@
|
||||
txHex({ txid }) {
|
||||
return fetch_api(`rawtx/${txid}?format=hex`, { url: this.url, asText: true })
|
||||
},
|
||||
txs({ addr, before, after }) {
|
||||
return fetch_api(`rawaddr/${addr}${before ? `?before=${before}` : ''}${after ? `?after=${after}` : ''}`, { url: this.url })
|
||||
txs({ addr, ...args }) {
|
||||
let queryParams = Object.entries(args).map(([key, value]) => `${key}=${value}`).join('&')
|
||||
if (queryParams)
|
||||
queryParams = '?' + queryParams
|
||||
return fetch_api(`rawaddr/${addr}${queryParams}`, { url: this.url })
|
||||
.then(result => result.txs)
|
||||
},
|
||||
latestBlock() {
|
||||
@ -196,6 +206,43 @@
|
||||
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
url: 'https://coinb.in/api/?uid=1&key=12345678901234567890123456789012&setmodule=bitcoin&request=sendrawtransaction',
|
||||
name: 'Coinb.in',
|
||||
broadcast({ rawTxHex }) {
|
||||
return new Promise((resolve, reject) => {
|
||||
fetch(this.url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded'
|
||||
},
|
||||
body: "rawtx=" + rawTxHex
|
||||
}).then(response => {
|
||||
console.log(response)
|
||||
response.text().then(resultText => {
|
||||
let r = resultText.match(/<result>.*<\/result>/);
|
||||
if (!r)
|
||||
reject(resultText);
|
||||
else {
|
||||
r = r.pop().replace('<result>', '').replace('</result>', '');
|
||||
if (r == '1') {
|
||||
let txid = resultText.match(/<txid>.*<\/txid>/).pop().replace('<txid>', '').replace('</txid>', '');
|
||||
resolve(txid);
|
||||
} else if (r == '0') {
|
||||
let error
|
||||
if (resultText.includes('<message>')) {
|
||||
error = resultText.match(/<message>.*<\/message>/).pop().replace('<message>', '').replace('</message>', '');
|
||||
} else {
|
||||
error = resultText.match(/<response>.*<\/response>/).pop().replace('<response>', '').replace('</response>', '');
|
||||
}
|
||||
reject(decodeURIComponent(error.replace(/\+/g, " ")));
|
||||
} else reject(resultText);
|
||||
}
|
||||
}).catch(error => reject(error))
|
||||
}).catch(error => reject(error))
|
||||
});
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
@ -309,10 +356,6 @@
|
||||
console.error(error)
|
||||
APIs[index].coolDownTime = new Date().getTime() + 1000 * 60 * 10; // 10 minutes
|
||||
return multiApi(fnName, { index: index + 1, ...args });
|
||||
if (error.code && [301, 429, 404].includes(error.code)) {
|
||||
} else {
|
||||
throw error.message || error;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
1
btcOperator.min.js
vendored
Normal file
1
btcOperator.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
1
compactIDB.min.js
vendored
Normal file
1
compactIDB.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
@ -1,4 +1,4 @@
|
||||
(function (EXPORTS) { //floBlockchainAPI v3.0.1b
|
||||
(function (EXPORTS) { //floBlockchainAPI v3.1.1
|
||||
/* FLO Blockchain Operator to send/receive data from blockchain using API calls via FLO Blockbook*/
|
||||
'use strict';
|
||||
const floBlockchainAPI = EXPORTS;
|
||||
@ -17,6 +17,22 @@
|
||||
|
||||
const SATOSHI_IN_BTC = 1e8;
|
||||
const isUndefined = val => typeof val === 'undefined';
|
||||
const checkIfTor = floBlockchainAPI.checkIfTor = () => {
|
||||
return fetch('https://check.torproject.org/api/ip', {
|
||||
mode: 'no-cors'
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(result => result.IsTor)
|
||||
.catch(error => false)
|
||||
}
|
||||
let isTor = false;
|
||||
checkIfTor().then(result => {
|
||||
isTor = result
|
||||
if (isTor) {
|
||||
DEFAULT.apiURL.FLO.push('http://vl7ni6byqx7rbub5hypxtod5dbfeuhoj5r5exuyl44pspqh2gasjj4qd.onion:9166/')
|
||||
DEFAULT.apiURL.FLO_TEST.push('http://omwkzk6bd6zuragdqsrhdyzgxzre7yx4vzrou4vzftintzc2dmagp6qd.onion:15017/')
|
||||
}
|
||||
});
|
||||
|
||||
const util = floBlockchainAPI.util = {};
|
||||
|
||||
@ -1041,4 +1057,4 @@
|
||||
})
|
||||
}
|
||||
|
||||
})('object' === typeof module ? module.exports : window.floBlockchainAPI = {});
|
||||
})('object' === typeof module ? module.exports : window.floBlockchainAPI = {});
|
||||
1
floBlockchainAPI.min.js
vendored
Normal file
1
floBlockchainAPI.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
1
floCloudAPI.min.js
vendored
Normal file
1
floCloudAPI.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
1
floCrypto.min.js
vendored
Normal file
1
floCrypto.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
1
floDapps.min.js
vendored
Normal file
1
floDapps.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
@ -1,16 +1,33 @@
|
||||
(function (EXPORTS) { //floTokenAPI v1.0.4a
|
||||
(function (EXPORTS) { //floTokenAPI v1.1.0a
|
||||
/* Token Operator to send/receive tokens via blockchain using API calls*/
|
||||
'use strict';
|
||||
const tokenAPI = EXPORTS;
|
||||
|
||||
const DEFAULT = {
|
||||
apiURL: floGlobals.tokenURL || "https://ranchimallflo.duckdns.org/",
|
||||
apiURL: [floGlobals.tokenURL || "https://ranchimallflo.ranchimall.net/"],
|
||||
currency: floGlobals.currency || "rupee"
|
||||
}
|
||||
|
||||
|
||||
const checkIfTor = tokenAPI.checkIfTor = () => {
|
||||
return fetch('https://check.torproject.org/api/ip', {
|
||||
mode: 'no-cors'
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(result => result.IsTor)
|
||||
.catch(error => false)
|
||||
}
|
||||
let isTor = false;
|
||||
checkIfTor().then(result => {
|
||||
isTor = result
|
||||
if (isTor) {
|
||||
DEFAULT.apiURL.push('http://omwkzk6bd6zuragdqsrhdyzgxzre7yx4vzrou4vzftintzc2dmagp6qd.onion:5017/')
|
||||
}
|
||||
});
|
||||
|
||||
Object.defineProperties(tokenAPI, {
|
||||
URL: {
|
||||
get: () => DEFAULT.apiURL
|
||||
get: () => DEFAULT.apiURL[0],
|
||||
},
|
||||
currency: {
|
||||
get: () => DEFAULT.currency,
|
||||
@ -27,29 +44,38 @@
|
||||
}
|
||||
});
|
||||
|
||||
const fetch_api = tokenAPI.fetch = function (apicall) {
|
||||
const fetch_api = tokenAPI.fetch = function (apicall, apiURLs = DEFAULT.apiURL) {
|
||||
return new Promise((resolve, reject) => {
|
||||
console.debug(DEFAULT.apiURL + apicall);
|
||||
fetch(DEFAULT.apiURL + apicall).then(response => {
|
||||
if (apiURLs.length === 0) {
|
||||
reject("No API URLs available");
|
||||
return;
|
||||
}
|
||||
const currentURL = apiURLs[0];
|
||||
console.debug(currentURL + apicall);
|
||||
fetch(currentURL + apicall).then(response => {
|
||||
if (response.ok)
|
||||
response.json().then(data => resolve(data));
|
||||
else
|
||||
reject(response)
|
||||
}).catch(error => reject(error))
|
||||
})
|
||||
reject(response);
|
||||
}).catch(error => {
|
||||
console.error(`Failed to fetch from ${currentURL}: ${error}`);
|
||||
// Try the next API URL recursively
|
||||
fetch_api(apicall, apiURLs.slice(1)).then(resolve).catch(reject);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
const getBalance = tokenAPI.getBalance = function (floID, token = DEFAULT.currency) {
|
||||
return new Promise((resolve, reject) => {
|
||||
fetch_api(`api/v1.0/getFloAddressBalance?token=${token}&floAddress=${floID}`)
|
||||
.then(result => resolve(result.balance || 0))
|
||||
fetch_api(`api/v2/floAddressInfo/${floID}`)
|
||||
.then(result => resolve(result.floAddressBalances[token]?.balance || 0))
|
||||
.catch(error => reject(error))
|
||||
})
|
||||
}
|
||||
|
||||
tokenAPI.getTx = function (txID) {
|
||||
return new Promise((resolve, reject) => {
|
||||
fetch_api(`api/v1.0/getTransactionDetails/${txID}`).then(res => {
|
||||
fetch_api(`api/v2/transactionDetails/${txID}`).then(res => {
|
||||
if (res.result === "error")
|
||||
reject(res.description);
|
||||
else if (!res.parsedFloData)
|
||||
@ -143,7 +169,7 @@
|
||||
|
||||
tokenAPI.getAllTxs = function (floID, token = DEFAULT.currency) {
|
||||
return new Promise((resolve, reject) => {
|
||||
fetch_api(`api/v1.0/getFloAddressTransactions?token=${token}&floAddress=${floID}`)
|
||||
fetch_api(`api/v2/floAddressTransactions/${floID}${token ? `?token=${token}` : ''}`)
|
||||
.then(result => resolve(result))
|
||||
.catch(error => reject(error))
|
||||
})
|
||||
|
||||
1
floTokenAPI.min.js
vendored
Normal file
1
floTokenAPI.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
57
lib.min.js
vendored
Normal file
57
lib.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user