ADD: isTorCapable

Removes the need to manual code adjustments on platforms that aren't compatible with react-native-tor
This commit is contained in:
Marcos Rodriguez Vélez 2021-07-18 12:34:03 -04:00
parent b50032db21
commit 308e9e1545
10 changed files with 39 additions and 14 deletions

View File

@ -5,11 +5,12 @@ import { LegacyWallet, SegwitBech32Wallet, SegwitP2SHWallet } from '../class';
import DefaultPreference from 'react-native-default-preference'; import DefaultPreference from 'react-native-default-preference';
import RNWidgetCenter from 'react-native-widget-center'; import RNWidgetCenter from 'react-native-widget-center';
import loc from '../loc'; import loc from '../loc';
import { isTorCapable } from './environment';
const bitcoin = require('bitcoinjs-lib'); const bitcoin = require('bitcoinjs-lib');
const ElectrumClient = require('electrum-client'); const ElectrumClient = require('electrum-client');
const reverse = require('buffer-reverse'); const reverse = require('buffer-reverse');
const BigNumber = require('bignumber.js'); const BigNumber = require('bignumber.js');
const torrific = require('../blue_modules/torrific'); const torrific = require('./torrific');
const Realm = require('realm'); const Realm = require('realm');
const ELECTRUM_HOST = 'electrum_host'; const ELECTRUM_HOST = 'electrum_host';
@ -102,12 +103,13 @@ async function connectMain() {
try { try {
console.log('begin connection:', JSON.stringify(usingPeer)); console.log('begin connection:', JSON.stringify(usingPeer));
mainClient = new ElectrumClient( mainClient = new ElectrumClient(
usingPeer.host.endsWith('.onion') ? torrific : global.net, usingPeer.host.endsWith('.onion') && isTorCapable ? torrific : global.net,
global.tls, global.tls,
usingPeer.ssl || usingPeer.tcp, usingPeer.ssl || usingPeer.tcp,
usingPeer.host, usingPeer.host,
usingPeer.ssl ? 'tls' : 'tcp', usingPeer.ssl ? 'tls' : 'tcp',
); );
mainClient.onError = function (e) { mainClient.onError = function (e) {
console.log('electrum mainClient.onError():', e.message); console.log('electrum mainClient.onError():', e.message);
if (mainConnected) { if (mainConnected) {
@ -806,7 +808,7 @@ module.exports.calculateBlockTime = function (height) {
*/ */
module.exports.testConnection = async function (host, tcpPort, sslPort) { module.exports.testConnection = async function (host, tcpPort, sslPort) {
const client = new ElectrumClient( const client = new ElectrumClient(
host.endsWith('.onion') ? torrific : global.net, host.endsWith('.onion') && isTorCapable ? torrific : global.net,
global.tls, global.tls,
sslPort || tcpPort, sslPort || tcpPort,
host, host,
@ -818,7 +820,7 @@ module.exports.testConnection = async function (host, tcpPort, sslPort) {
try { try {
const rez = await Promise.race([ const rez = await Promise.race([
new Promise(resolve => { new Promise(resolve => {
timeoutId = setTimeout(() => resolve('timeout'), host.endsWith('.onion') ? 21000 : 5000); timeoutId = setTimeout(() => resolve('timeout'), host.endsWith('.onion') && isTorCapable ? 21000 : 5000);
}), }),
client.connect(), client.connect(),
]); ]);

View File

@ -1,8 +1,20 @@
import { Platform } from 'react-native';
import { getSystemName, isTablet, getDeviceType } from 'react-native-device-info'; import { getSystemName, isTablet, getDeviceType } from 'react-native-device-info';
const isMacCatalina = getSystemName() === 'Mac OS X'; const isMacCatalina = getSystemName() === 'Mac OS X';
const isDesktop = getDeviceType() === 'Desktop';
const isTorCapable = () => {
let capable = true;
if (Platform.OS === 'android' && Platform.Version < 26) {
capable = false;
} else if (isDesktop) {
capable = false;
}
return capable;
};
module.exports.isMacCatalina = isMacCatalina; module.exports.isMacCatalina = isMacCatalina;
module.exports.isDesktop = getDeviceType() === 'Desktop'; module.exports.isDesktop = isDesktop;
module.exports.isHandset = getDeviceType() === 'Handset'; module.exports.isHandset = getDeviceType() === 'Handset';
module.exports.isTablet = isTablet; module.exports.isTablet = isTablet;
module.exports.isTorCapable = isTorCapable();

View File

@ -2,6 +2,7 @@ import { LegacyWallet } from './legacy-wallet';
import Frisbee from 'frisbee'; import Frisbee from 'frisbee';
import bolt11 from 'bolt11'; import bolt11 from 'bolt11';
import { BitcoinUnit, Chain } from '../../models/bitcoinUnits'; import { BitcoinUnit, Chain } from '../../models/bitcoinUnits';
import { isTorCapable } from '../../blue_modules/environment';
const torrific = require('../../blue_modules/torrific'); const torrific = require('../../blue_modules/torrific');
export class LightningCustodianWallet extends LegacyWallet { export class LightningCustodianWallet extends LegacyWallet {
@ -72,7 +73,7 @@ export class LightningCustodianWallet extends LegacyWallet {
baseURI: this.baseURI, baseURI: this.baseURI,
}); });
if (this.baseURI?.indexOf('.onion') !== -1) { if (isTorCapable && this.baseURI?.indexOf('.onion') !== -1) {
this._api = new torrific.Torsbee({ this._api = new torrific.Torsbee({
baseURI: this.baseURI, baseURI: this.baseURI,
}); });

View File

@ -16,6 +16,7 @@ import { BlueCurrentTheme } from '../../components/themes';
import Notifications from '../../blue_modules/notifications'; import Notifications from '../../blue_modules/notifications';
import { BlueStorageContext } from '../../blue_modules/storage-context'; import { BlueStorageContext } from '../../blue_modules/storage-context';
import { Psbt } from 'bitcoinjs-lib'; import { Psbt } from 'bitcoinjs-lib';
import { isTorCapable } from '../../blue_modules/environment';
const currency = require('../../blue_modules/currency'); const currency = require('../../blue_modules/currency');
const BlueElectrum = require('../../blue_modules/BlueElectrum'); const BlueElectrum = require('../../blue_modules/BlueElectrum');
const Bignumber = require('bignumber.js'); const Bignumber = require('bignumber.js');
@ -70,7 +71,7 @@ export default class Confirm extends Component {
const wallet = new PayjoinTransaction(this.state.psbt, txHex => this.broadcast(txHex), this.state.fromWallet); const wallet = new PayjoinTransaction(this.state.psbt, txHex => this.broadcast(txHex), this.state.fromWallet);
const paymentScript = this.getPaymentScript(); const paymentScript = this.getPaymentScript();
let payjoinClient; let payjoinClient;
if (this.state.payjoinUrl.includes('.onion')) { if (isTorCapable && this.state.payjoinUrl.includes('.onion')) {
console.warn('trying TOR....'); console.warn('trying TOR....');
const payjoinUrl = this.state.payjoinUrl; const payjoinUrl = this.state.payjoinUrl;
// working through TOR - crafting custom requester that will handle TOR http request // working through TOR - crafting custom requester that will handle TOR http request

View File

@ -5,7 +5,7 @@ import Notifications from '../../blue_modules/notifications';
import navigationStyle from '../../components/navigationStyle'; import navigationStyle from '../../components/navigationStyle';
import { SafeBlueArea, BlueListItem } from '../../BlueComponents'; import { SafeBlueArea, BlueListItem } from '../../BlueComponents';
import loc from '../../loc'; import loc from '../../loc';
import { isDesktop } from '../../blue_modules/environment'; import { isTorCapable } from '../../blue_modules/environment';
const NetworkSettings = () => { const NetworkSettings = () => {
const { navigate } = useNavigation(); const { navigate } = useNavigation();
@ -35,7 +35,7 @@ const NetworkSettings = () => {
chevron chevron
/> />
)} )}
{!isDesktop && <BlueListItem title={loc.settings.tor_settings} onPress={navigateToTorSettings} testID="TorSettings" chevron />} {isTorCapable && <BlueListItem title={loc.settings.tor_settings} onPress={navigateToTorSettings} testID="TorSettings" chevron />}
</ScrollView> </ScrollView>
</SafeBlueArea> </SafeBlueArea>
); );

View File

@ -33,7 +33,7 @@ import {
BlueDismissKeyboardInputAccessory, BlueDismissKeyboardInputAccessory,
} from '../../BlueComponents'; } from '../../BlueComponents';
import { BlueCurrentTheme } from '../../components/themes'; import { BlueCurrentTheme } from '../../components/themes';
import { isDesktop } from '../../blue_modules/environment'; import { isTorCapable } from '../../blue_modules/environment';
import ReactNativeHapticFeedback from 'react-native-haptic-feedback'; import ReactNativeHapticFeedback from 'react-native-haptic-feedback';
const BlueElectrum = require('../../blue_modules/BlueElectrum'); const BlueElectrum = require('../../blue_modules/BlueElectrum');
@ -286,7 +286,7 @@ export default class ElectrumSettings extends Component {
<TextInput <TextInput
placeholder={ placeholder={
loc.formatString(loc.settings.electrum_host, { example: '111.222.333.111' }) + loc.formatString(loc.settings.electrum_host, { example: '111.222.333.111' }) +
(!isDesktop ? ' (' + loc.settings.tor_supported + ')' : '') (isTorCapable ? ' (' + loc.settings.tor_supported + ')' : '')
} }
value={this.state.host} value={this.state.host}
onChangeText={text => this.setState({ host: text.trim() })} onChangeText={text => this.setState({ host: text.trim() })}

View File

@ -12,7 +12,7 @@ import { LightningCustodianWallet } from '../../class/wallets/lightning-custodia
import loc from '../../loc'; import loc from '../../loc';
import { BlueCurrentTheme } from '../../components/themes'; import { BlueCurrentTheme } from '../../components/themes';
import DeeplinkSchemaMatch from '../../class/deeplink-schema-match'; import DeeplinkSchemaMatch from '../../class/deeplink-schema-match';
import { isDesktop } from '../../blue_modules/environment'; import { isTorCapable } from '../../blue_modules/environment';
const styles = StyleSheet.create({ const styles = StyleSheet.create({
uri: { uri: {
@ -136,7 +136,7 @@ const LightningSettings = () => {
value={URI} value={URI}
placeholder={ placeholder={
loc.formatString(loc.settings.electrum_host, { example: '111.222.333.111' }) + loc.formatString(loc.settings.electrum_host, { example: '111.222.333.111' }) +
(!isDesktop ? ' (' + loc.settings.tor_supported + ')' : '') (isTorCapable ? ' (' + loc.settings.tor_supported + ')' : '')
} }
onChangeText={setLndhubURI} onChangeText={setLndhubURI}
numberOfLines={1} numberOfLines={1}

View File

@ -14,6 +14,9 @@ const styles = StyleSheet.create({
}, },
}); });
/*
TorSettings is not displayed in Settings menu if isTorCapable is false. No need to provide code protection.
*/
const TorSettings = () => { const TorSettings = () => {
const [isLoading, setIsLoading] = useState(false); const [isLoading, setIsLoading] = useState(false);
const [daemonStatus, setDaemonStatus] = useState(''); const [daemonStatus, setDaemonStatus] = useState('');

View File

@ -1,6 +1,11 @@
echo "Applying patch for package.json" echo "Applying patch for package.json"
sed -i '' '/react-native-tor/d' ./package.json sed -i '' '/react-native-tor/d' ./package.json
rm -fr node_modules
echo "Re-installing node_modules" echo "Re-installing node_modules"
npm i npm i
echo "Applying patch for react-native-xcode.sh"
sed -i '' 's/--platform "$BUNDLE_PLATFORM"/--platform "ios"/g' ./node_modules/react-native/scripts/react-native-xcode.sh
echo "Deleting torrific.js content"
echo > blue_modules/torrific.js
echo "" echo ""
echo "react-native-tor is not currently compatible with Mac Catalyst. You will need to remove all references from torrific.js. After this, you should be able to compile BlueWallet using Mac Catalyst on XCode. If you are running macOS Catalina, you will need to remove the iOS 14 Widgets from the project targets." echo "NOTE: react-native-tor is not currently compatible with Mac Catalyst. If you are running macOS Catalina, you will need to remove the iOS 14 Widgets from the project targets."

View File

@ -24,6 +24,7 @@ jest.mock('react-native-device-info', () => {
return { return {
getUniqueId: jest.fn().mockReturnValue('uniqueId'), getUniqueId: jest.fn().mockReturnValue('uniqueId'),
getSystemName: jest.fn(), getSystemName: jest.fn(),
getDeviceType: jest.fn().mockReturnValue(false),
hasGmsSync: jest.fn().mockReturnValue(true), hasGmsSync: jest.fn().mockReturnValue(true),
hasHmsSync: jest.fn().mockReturnValue(false), hasHmsSync: jest.fn().mockReturnValue(false),
}; };