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 */
|
/* BTC Crypto and API Operator */
|
||||||
const btcOperator = EXPORTS;
|
const btcOperator = EXPORTS;
|
||||||
const SATOSHI_IN_BTC = 1e8;
|
const SATOSHI_IN_BTC = 1e8;
|
||||||
@ -22,6 +22,7 @@
|
|||||||
try {
|
try {
|
||||||
const response = await fetch(url, {
|
const response = await fetch(url, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
|
mode: 'no-cors',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
},
|
},
|
||||||
@ -56,7 +57,7 @@
|
|||||||
},
|
},
|
||||||
async broadcast({ rawTxHex, url }) {
|
async broadcast({ rawTxHex, url }) {
|
||||||
try {
|
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
|
return result.hash
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
throw e
|
throw e
|
||||||
@ -78,16 +79,19 @@
|
|||||||
latestBlock() {
|
latestBlock() {
|
||||||
return fetch_api(`blocks/tip/height`, { url: this.url })
|
return fetch_api(`blocks/tip/height`, { url: this.url })
|
||||||
},
|
},
|
||||||
// tx({ txid, url }) {
|
tx({ txid, url }) {
|
||||||
// return fetch_api(`tx/${txid}`, { url: url || this.url })
|
return fetch_api(`tx/${txid}`, { url: url || this.url })
|
||||||
// .then(result => formatTx(result))
|
.then(result => formatTx(result))
|
||||||
// },
|
},
|
||||||
// txHex({ txid, url }) {
|
txHex({ txid, url }) {
|
||||||
// return fetch_api(`tx/${txid}/hex`, { url: url || this.url, asText: true })
|
return fetch_api(`tx/${txid}/hex`, { url: url || this.url, asText: true })
|
||||||
// },
|
},
|
||||||
// txs({ addr, before, after, url }) {
|
txs({ addr, url, ...args }) {
|
||||||
// return fetch_api(`address/${addr}/txs${before ? `?before=${before}` : ''}${after ? `?after=${after}` : ''}`, { url: url || this.url })
|
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 }) {
|
async block({ id, url }) {
|
||||||
// if id is hex string then it is block hash
|
// if id is hex string then it is block hash
|
||||||
try {
|
try {
|
||||||
@ -114,17 +118,20 @@
|
|||||||
latestBlock() {
|
latestBlock() {
|
||||||
return fetch_api(`blocks/tip/height`, { url: this.url })
|
return fetch_api(`blocks/tip/height`, { url: this.url })
|
||||||
},
|
},
|
||||||
// tx({ txid }) {
|
tx({ txid }) {
|
||||||
// return fetch_api(`tx/${txid}`, { url: this.url })
|
return fetch_api(`tx/${txid}`, { url: this.url })
|
||||||
// .then(result => formatTx(result))
|
.then(result => formatTx(result))
|
||||||
|
|
||||||
// },
|
},
|
||||||
// txHex({ txid }) {
|
txHex({ txid }) {
|
||||||
// return fetch_api(`tx/${txid}/hex`, { url: this.url, asText: true })
|
return fetch_api(`tx/${txid}/hex`, { url: this.url, asText: true })
|
||||||
// },
|
},
|
||||||
// txs({ addr, before, after }) {
|
txs({ addr, ...args }) {
|
||||||
// return fetch_api(`address/${addr}/txs${before ? `?before=${before}` : ''}${after ? `?after=${after}` : ''}`, { url: this.url })
|
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 }) {
|
async block({ id }) {
|
||||||
// if id is hex string then it is block hash
|
// if id is hex string then it is block hash
|
||||||
try {
|
try {
|
||||||
@ -159,8 +166,11 @@
|
|||||||
txHex({ txid }) {
|
txHex({ txid }) {
|
||||||
return fetch_api(`rawtx/${txid}?format=hex`, { url: this.url, asText: true })
|
return fetch_api(`rawtx/${txid}?format=hex`, { url: this.url, asText: true })
|
||||||
},
|
},
|
||||||
txs({ addr, before, after }) {
|
txs({ addr, ...args }) {
|
||||||
return fetch_api(`rawaddr/${addr}${before ? `?before=${before}` : ''}${after ? `?after=${after}` : ''}`, { url: this.url })
|
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)
|
.then(result => result.txs)
|
||||||
},
|
},
|
||||||
latestBlock() {
|
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)
|
console.error(error)
|
||||||
APIs[index].coolDownTime = new Date().getTime() + 1000 * 60 * 10; // 10 minutes
|
APIs[index].coolDownTime = new Date().getTime() + 1000 * 60 * 10; // 10 minutes
|
||||||
return multiApi(fnName, { index: index + 1, ...args });
|
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*/
|
/* 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;
|
||||||
@ -17,6 +17,22 @@
|
|||||||
|
|
||||||
const SATOSHI_IN_BTC = 1e8;
|
const SATOSHI_IN_BTC = 1e8;
|
||||||
const isUndefined = val => typeof val === 'undefined';
|
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 = {};
|
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*/
|
/* Token Operator to send/receive tokens via blockchain using API calls*/
|
||||||
'use strict';
|
'use strict';
|
||||||
const tokenAPI = EXPORTS;
|
const tokenAPI = EXPORTS;
|
||||||
|
|
||||||
const DEFAULT = {
|
const DEFAULT = {
|
||||||
apiURL: floGlobals.tokenURL || "https://ranchimallflo.duckdns.org/",
|
apiURL: [floGlobals.tokenURL || "https://ranchimallflo.ranchimall.net/"],
|
||||||
currency: floGlobals.currency || "rupee"
|
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, {
|
Object.defineProperties(tokenAPI, {
|
||||||
URL: {
|
URL: {
|
||||||
get: () => DEFAULT.apiURL
|
get: () => DEFAULT.apiURL[0],
|
||||||
},
|
},
|
||||||
currency: {
|
currency: {
|
||||||
get: () => DEFAULT.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) => {
|
return new Promise((resolve, reject) => {
|
||||||
console.debug(DEFAULT.apiURL + apicall);
|
if (apiURLs.length === 0) {
|
||||||
fetch(DEFAULT.apiURL + apicall).then(response => {
|
reject("No API URLs available");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const currentURL = apiURLs[0];
|
||||||
|
console.debug(currentURL + apicall);
|
||||||
|
fetch(currentURL + apicall).then(response => {
|
||||||
if (response.ok)
|
if (response.ok)
|
||||||
response.json().then(data => resolve(data));
|
response.json().then(data => resolve(data));
|
||||||
else
|
else
|
||||||
reject(response)
|
reject(response);
|
||||||
}).catch(error => reject(error))
|
}).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) {
|
const getBalance = tokenAPI.getBalance = function (floID, token = DEFAULT.currency) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
fetch_api(`api/v1.0/getFloAddressBalance?token=${token}&floAddress=${floID}`)
|
fetch_api(`api/v2/floAddressInfo/${floID}`)
|
||||||
.then(result => resolve(result.balance || 0))
|
.then(result => resolve(result.floAddressBalances[token]?.balance || 0))
|
||||||
.catch(error => reject(error))
|
.catch(error => reject(error))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
tokenAPI.getTx = function (txID) {
|
tokenAPI.getTx = function (txID) {
|
||||||
return new Promise((resolve, reject) => {
|
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")
|
if (res.result === "error")
|
||||||
reject(res.description);
|
reject(res.description);
|
||||||
else if (!res.parsedFloData)
|
else if (!res.parsedFloData)
|
||||||
@ -143,7 +169,7 @@
|
|||||||
|
|
||||||
tokenAPI.getAllTxs = function (floID, token = DEFAULT.currency) {
|
tokenAPI.getAllTxs = function (floID, token = DEFAULT.currency) {
|
||||||
return new Promise((resolve, reject) => {
|
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))
|
.then(result => resolve(result))
|
||||||
.catch(error => reject(error))
|
.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