Skip to content

Commit 3cdb2f1

Browse files
committed
fix: add BB headers on REST calls
1 parent 1d6822c commit 3cdb2f1

File tree

7 files changed

+101
-82
lines changed

7 files changed

+101
-82
lines changed

actions/wallet-utxo.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
const _ = require('lodash')
44
const BigNumber = require('bignumber.js')
5-
const axios = require('axios')
6-
//const axiosRetry = require('axios-retry')
75

86
const configWallet = require('../config/wallet')
97
const configExternal = require('../config/wallet-external')
108

9+
const axios = require('axios'); configExternal.blockbookHeaders.set(axios, configExternal.blockbookHeaders)
10+
//const axiosRetry = require('axios-retry')
11+
1112
const utilsWallet = require('../utils')
1213

1314
module.exports = {

app-worker/worker-account.js

Lines changed: 51 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Distributed under AGPLv3 license: see /LICENSE for terms. Copyright 2019-2021 Dominic Morris.
22

33
const BigNumber = require('bignumber.js')
4-
const axios = require('axios')
4+
//const axios = require('axios')
55
const _ = require('lodash')
66
const CircularBuffer = require("circular-buffer")
77

@@ -533,7 +533,7 @@ async function getAddressBalance_Account(symbol, address) {
533533

534534
function getETHAddressBalance_api(symbol, address) {
535535

536-
if (configWallet.ETH_USEWEB3_ACCOUNT_BALANCES) {
536+
//if (configWallet.ETH_USEWEB3_ACCOUNT_BALANCES) {
537537
//utilsWallet.debug(`*** getETHAddressBalance_api (using web3) (ACCOUNT) ${symbol} (${address})...`)
538538

539539
return new Promise((resolve, reject) => {
@@ -553,34 +553,34 @@ function getETHAddressBalance_api(symbol, address) {
553553
reject(err)
554554
})
555555
})
556-
}
557-
else {
558-
//utilsWallet.debug(`*** getETHAddressBalance_api (using api) (ACCOUNT) ${symbol} (${address})...`)
559-
560-
return new Promise((resolve, reject) => {
561-
//axiosRetry(axios, configWallet.AXIOS_RETRY_3PBP)
562-
axios.get(configExternal.walletExternal_config[symbol].api.balance(address) + `&noCache=${new Date().getTime()}`)
563-
.then(res => {
564-
if (res && res.status === 200 && res.data && res.data.message === "OK") {
565-
var balWei = res.data.result
566-
utilsWallet.log(`*** getETHAddressBalance_api (using api) ${symbol} (${address}), balWei=`, balWei)
567-
resolve(balWei.toString())
568-
} else {
569-
const err = `### getETHAddressBalance_api (using api) ${symbol} (${address}) UNEXPECTED DATA; balance undefined ###`
570-
utilsWallet.warn(err)
571-
reject(err)
572-
}
573-
})
574-
.catch((err) => {
575-
utilsWallet.warn(`### getETHAddressBalance_api (using api) ${symbol} (${address}) FAIL - err=`, err)
576-
reject(err)
577-
})
578-
})
579-
}
556+
//}
557+
// else {
558+
// //utilsWallet.debug(`*** getETHAddressBalance_api (using api) (ACCOUNT) ${symbol} (${address})...`)
559+
560+
// return new Promise((resolve, reject) => {
561+
// //axiosRetry(axios, configWallet.AXIOS_RETRY_3PBP)
562+
// axios.get(configExternal.walletExternal_config[symbol].api.balance(address) + `&noCache=${new Date().getTime()}`)
563+
// .then(res => {
564+
// if (res && res.status === 200 && res.data && res.data.message === "OK") {
565+
// var balWei = res.data.result
566+
// utilsWallet.log(`*** getETHAddressBalance_api (using api) ${symbol} (${address}), balWei=`, balWei)
567+
// resolve(balWei.toString())
568+
// } else {
569+
// const err = `### getETHAddressBalance_api (using api) ${symbol} (${address}) UNEXPECTED DATA; balance undefined ###`
570+
// utilsWallet.warn(err)
571+
// reject(err)
572+
// }
573+
// })
574+
// .catch((err) => {
575+
// utilsWallet.warn(`### getETHAddressBalance_api (using api) ${symbol} (${address}) FAIL - err=`, err)
576+
// reject(err)
577+
// })
578+
// })
579+
// }
580580
}
581581

582582
function getERC20AddressBalance_api(symbol, address) {
583-
if (configWallet.ETH_ERC20_USEWEB3_TOKEN_BALANCES) {
583+
//if (configWallet.ETH_ERC20_USEWEB3_TOKEN_BALANCES) {
584584
//utilsWallet.debug(`*** getERC20AddressBalance_api ${symbol} (${address}) web3...`)
585585

586586
return new Promise((resolve, reject) => {
@@ -617,34 +617,32 @@ function getERC20AddressBalance_api(symbol, address) {
617617
}
618618
});
619619
})
620-
}
621-
else {
622-
//utilsWallet.debug(`*** getERC20AddressBalance_api (using api) (ACCOUNT) ${symbol} (${address})...`)
623-
624-
return new Promise((resolve, reject) => {
625-
//axiosRetry(axios, configWallet.AXIOS_RETRY_3PBP)
626-
axios.get(configExternal.walletExternal_config[symbol].api.balance(address) + `&noCache=${new Date().getTime()}`)
627-
.then(res => {
628-
if (res && res.status === 200 && res.data && res.data.message === "OK") {
629-
var balWei = res.data.result
630-
631-
resolve(balWei.toString())
632-
} else {
633-
const err = `### getERC20AddressBalance_api ${symbol} (${address}) UNEXPECTED DATA; balance undefined ###`
634-
utilsWallet.warn(err)
635-
reject(err)
636-
}
637-
})
638-
.catch((err) => {
639-
utilsWallet.warn(`### getERC20AddressBalance_api ${symbol} (${address}) FAIL - err=`, err)
640-
reject(err)
641-
})
642-
})
643-
}
620+
// }
621+
// else {
622+
// //utilsWallet.debug(`*** getERC20AddressBalance_api (using api) (ACCOUNT) ${symbol} (${address})...`)
623+
624+
// return new Promise((resolve, reject) => {
625+
// //axiosRetry(axios, configWallet.AXIOS_RETRY_3PBP)
626+
// axios.get(configExternal.walletExternal_config[symbol].api.balance(address) + `&noCache=${new Date().getTime()}`)
627+
// .then(res => {
628+
// if (res && res.status === 200 && res.data && res.data.message === "OK") {
629+
// var balWei = res.data.result
630+
631+
// resolve(balWei.toString())
632+
// } else {
633+
// const err = `### getERC20AddressBalance_api ${symbol} (${address}) UNEXPECTED DATA; balance undefined ###`
634+
// utilsWallet.warn(err)
635+
// reject(err)
636+
// }
637+
// })
638+
// .catch((err) => {
639+
// utilsWallet.warn(`### getERC20AddressBalance_api ${symbol} (${address}) FAIL - err=`, err)
640+
// reject(err)
641+
// })
642+
// })
643+
// }
644644
}
645645

646-
647-
648646
// export function getERC20AddressBalance_web3(symbol, address) {
649647
// utilsWallet.log(`*** getERC20AddressBalance_web3 (ACCOUNT) ${symbol} (${address})...`)
650648
// return new Promise((resolve, reject) => {

app-worker/worker-blockbook.js

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
// Distributed under AGPLv3 license: see /LICENSE for terms. Copyright 2019-2021 Dominic Morris.
22

33
const _ = require('lodash')
4-
const axios = require('axios')
5-
const isoWs = require('isomorphic-ws')
6-
const BigNumber = require('bignumber.js')
7-
const CircularBuffer = require("circular-buffer")
84

95
const configWS = require('../config/websockets')
106
const configExternal = require('../config/wallet-external')
117
const configWallet = require('../config/wallet')
128

9+
const axios = require('axios'); configExternal.blockbookHeaders.set(axios, configExternal.blockbookHeaders)
10+
11+
const isoWs = require('isomorphic-ws')
12+
const BigNumber = require('bignumber.js')
13+
const CircularBuffer = require("circular-buffer")
14+
1315
const walletUtxo = require('../actions/wallet-utxo')
1416
const actionsWallet = require('../actions')
1517

@@ -52,7 +54,7 @@ module.exports = {
5254
// BB v3
5355
function getAddressFull_Blockbook_v3(wallet, asset, address, utxo_mempool_spentTxIds, allDispatchActions) {
5456
const symbol = asset.symbol
55-
utilsWallet.log(`getAddressFull_Blockbook_v3 ${symbol}...`)
57+
//utilsWallet.log(`getAddressFull_Blockbook_v3 ${symbol}...`)
5658

5759
return new Promise((resolve, reject) => {
5860
isosocket_send_Blockbook(symbol, 'getAccountInfo', {
@@ -412,15 +414,15 @@ function isosocket_Setup_Blockbook(networkConnected, networkStatusChanged, loade
412414

413415
self.blockbookIsoSockets[x] = new isoWs(configWS.blockbook_ws_config[x].url + "/websocket", configWallet.WALLET_ENV === "BROWSER" ? undefined : {
414416
headers: {
415-
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36",
416-
"Connection": "Upgrade",
417-
"Upgrade": "websocket",
418-
"Sec-WebSocket-Extensions": "permessage-deflate; client_max_window_bits",
419-
"Sec-WebSocket-Version": "13",
420-
"Accept-Encoding": "gzip, deflate, br",
421-
"Accept-Language": "en-US,en;q=0.9,id;q=0.8",
422-
"Cache-Control": "no-cache",
423-
"Pragma": "no-cache",
417+
"User-Agent": configExternal.blockbookHeaders["User-Agent"], //"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36",
418+
"Connection": configExternal.blockbookHeaders["Connection"], //"Upgrade",
419+
"Upgrade": configExternal.blockbookHeaders["Upgrade"], //"websocket",
420+
"Sec-WebSocket-Extensions": configExternal.blockbookHeaders["Sec-WebSocket-Extensions"], //"permessage-deflate; client_max_window_bits",
421+
"Sec-WebSocket-Version": configExternal.blockbookHeaders["Sec-WebSocket-Version"], //"13",
422+
"Accept-Encoding": configExternal.blockbookHeaders["Accept-Encoding"], //"gzip, deflate, br",
423+
"Accept-Language": configExternal.blockbookHeaders["Accept-Language"], //"en-US,en;q=0.9,id;q=0.8",
424+
"Cache-Control": configExternal.blockbookHeaders["Cache-Control"], //"no-cache",
425+
"Pragma": configExternal.blockbookHeaders["Pragma"], //"no-cache",
424426
"Host": ws_url.hostname,
425427
"Origin": ws_url.origin.replace('wss', 'https'),
426428
}

app-worker/worker.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const workerAccount = require('./worker-account')
1616
const workerUtxo = require('./worker-insight')
1717
const configWS = require('../config/websockets')
1818
const configWallet = require('../config/wallet')
19+
const configExternal = require('../config/wallet-external')
1920
const walletExternal = require('../actions/wallet-external')
2021
const walletP2shBtc = require('../actions/wallet-btc-p2sh')
2122
const utilsWallet = require('../utils')
@@ -714,15 +715,15 @@ self.get_BlockbookSocketIo = function(asset) {
714715
transportOptions: {
715716
websocket: {
716717
extraHeaders: {
717-
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36",
718-
"Connection": "Upgrade",
719-
"Upgrade": "websocket",
720-
"Sec-WebSocket-Extensions": "permessage-deflate; client_max_window_bits",
721-
"Sec-WebSocket-Version": "13",
722-
"Accept-Encoding": "gzip, deflate, br",
723-
"Accept-Language": "en-US,en;q=0.9,id;q=0.8",
724-
"Cache-Control": "no-cache",
725-
"Pragma": "no-cache",
718+
"User-Agent": configExternal.blockbookHeaders["User-Agent"], //"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36",
719+
"Connection": configExternal.blockbookHeaders["Connection"], //"Upgrade",
720+
"Upgrade": configExternal.blockbookHeaders["Upgrade"], //"websocket",
721+
"Sec-WebSocket-Extensions": configExternal.blockbookHeaders["Sec-WebSocket-Extensions"], //"permessage-deflate; client_max_window_bits",
722+
"Sec-WebSocket-Version": configExternal.blockbookHeaders["Sec-WebSocket-Version"], //"13",
723+
"Accept-Encoding": configExternal.blockbookHeaders["Accept-Encoding"], //"gzip, deflate, br",
724+
"Accept-Language": configExternal.blockbookHeaders["Accept-Language"], //"en-US,en;q=0.9,id;q=0.8",
725+
"Cache-Control": configExternal.blockbookHeaders["Cache-Control"], //"no-cache",
726+
"Pragma": configExternal.blockbookHeaders["Pragma"], //"no-cache",
726727
"Host": ws_url.hostname,
727728
"Origin": ws_url.origin.replace('wss', 'https'),
728729
}

config/wallet-external.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,4 +531,21 @@ module.exports = {
531531
,erc20_ropstenAddrExplorer: (tokenAddr, holderAddr) => erc20_ropstenAddrExplorer(tokenAddr, holderAddr)
532532
,ethTestHttpProvider
533533
,ethHttpProvider
534+
535+
,blockbookHeaders: {
536+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36"
537+
,"Connection": "Upgrade"
538+
,"Upgrade": "websocket"
539+
,"Sec-WebSocket-Extensions": "permessage-deflate; client_max_window_bits"
540+
,"Sec-WebSocket-Version": "13"
541+
,"Accept-Encoding": "gzip, deflate, br"
542+
,"Accept-Language": "en-US,en;q=0.9,id;q=0.8"
543+
,"Cache-Control": "no-cache"
544+
,"Pragma": "no-cache"
545+
, set(axios, headers) {
546+
axios.defaults.headers.common['User-Agent'] = headers["User-Agent"]
547+
axios.defaults.headers.common['Cache-Control'] = headers["Cache-Control"]
548+
axios.defaults.headers.common['Pragma'] = headers["Pragma"]
549+
}
550+
}
534551
}

config/wallet.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const WALLET_INCLUDE_DYNAMIC_STM_ASSETS = false
4747
const WALLET_INCLUDE_AIRCARBON_TEST = false
4848
const WALLET_INCLUDE_AYONDO_TEST = false
4949

50-
const WALLET_INCLUDE_ETH_TEST = false // always include eth_test - so it can be available in prod for testnets2@scoop.tech
50+
const WALLET_INCLUDE_ETH_TEST = true // always include eth_test - so it can be available in prod for testnets2@scoop.tech
5151
// WALLET_INCLUDE_AIRCARBON_TEST ||
5252
// WALLET_INCLUDE_SINGDAX_TEST ||
5353
// WALLET_INCLUDE_AYONDO_TEST ||
@@ -1102,8 +1102,8 @@ module.exports = {
11021102
, ETH_SENDMAX_PADDING_WEI: 50 // help ETH transactions by reducing this amount of Wei (intermittent Geth issues with full sends)
11031103
, ETH_COALESCE_DUST_TO_ZERO: true // hide dust values: modifies balances at API and at calculation layers
11041104
, ETH_DUST_WEI: 200 // if less than this, we coalesce the Wei balance to zero
1105-
, ETH_USEWEB3_ACCOUNT_BALANCES: true // use web3 and eth.getBalance to get ethereum balances; otherwise use 3PBP (etherscan or blockscout)
1106-
, ETH_ERC20_USEWEB3_TOKEN_BALANCES: true // use web3 and make contract call to get erc20 token balances; otherwise use 3PBP (etherscan or blockscout)
1105+
//, ETH_USEWEB3_ACCOUNT_BALANCES: true // use web3 and eth.getBalance to get ethereum balances; otherwise use 3PBP (etherscan or blockscout)
1106+
//, ETH_ERC20_USEWEB3_TOKEN_BALANCES: true // use web3 and make contract call to get erc20 token balances; otherwise use 3PBP (etherscan or blockscout)
11071107
, ETH_ERC20_TX_FALLBACK_WEI_GASLIMIT: 120000 // static gasLimit for ERC20 token transfers, if not specified on the asset's config
11081108

11091109
// privkey regexs

svr-wallet/sw-asset.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ module.exports = {
3636

3737
const feeData = await opsWallet.getAssetFeeData(asset)
3838

39-
return Promise.resolve({ ok: { feeData } })
39+
return Promise.resolve({ symbol, ok: { feeData } })
4040
},
4141

4242
// use exchange service to convert from one asset to another

0 commit comments

Comments
 (0)