Skip to content

Commit

Permalink
ADD: support for Electrum Personal Server (SSL connection)
Browse files Browse the repository at this point in the history
  • Loading branch information
Overtorment committed Mar 26, 2020
1 parent c0f3d99 commit 6b188d2
Show file tree
Hide file tree
Showing 13 changed files with 194 additions and 87 deletions.
31 changes: 14 additions & 17 deletions BlueElectrum.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ let reverse = require('buffer-reverse');
let BigNumber = require('bignumber.js');

const storageKey = 'ELECTRUM_PEERS';
const defaultPeer = { host: 'electrum1.bluewallet.io', tcp: '50001' };
const defaultPeer = { host: 'electrum1.bluewallet.io', ssl: '443' };
const hardcodedPeers = [
// { host: 'noveltybobble.coinjoined.com', tcp: '50001' }, // down
// { host: 'electrum.be', tcp: '50001' },
Expand All @@ -16,27 +16,27 @@ const hardcodedPeers = [
// { host: 'Bitkoins.nl', tcp: '50001' }, // down
// { host: 'fullnode.coinkite.com', tcp: '50001' },
// { host: 'preperfect.eleCTruMioUS.com', tcp: '50001' }, // down
{ host: 'electrum1.bluewallet.io', tcp: '50001' },
{ host: 'electrum1.bluewallet.io', tcp: '50001' }, // 2x weight
{ host: 'electrum2.bluewallet.io', tcp: '50001' },
{ host: 'electrum3.bluewallet.io', tcp: '50001' },
{ host: 'electrum3.bluewallet.io', tcp: '50001' }, // 2x weight
{ host: 'electrum1.bluewallet.io', ssl: '443' },
{ host: 'electrum1.bluewallet.io', ssl: '443' }, // 2x weight
{ host: 'electrum2.bluewallet.io', ssl: '443' },
{ host: 'electrum3.bluewallet.io', ssl: '443' },
{ host: 'electrum3.bluewallet.io', ssl: '443' }, // 2x weight
];

let mainClient = false;
let mainClient: ElectrumClient = false;
let mainConnected = false;
let wasConnectedAtLeastOnce = false;

async function connectMain() {
let usingPeer = await getRandomHardcodedPeer();
let savedPeer = await getSavedPeer();
if (savedPeer && savedPeer.host && savedPeer.tcp) {
if (savedPeer && savedPeer.host && (savedPeer.tcp || savedPeer.ssl)) {
usingPeer = savedPeer;
}

try {
console.log('begin connection:', JSON.stringify(usingPeer));
mainClient = new ElectrumClient(usingPeer.tcp, usingPeer.host, 'tcp');
mainClient = new ElectrumClient(usingPeer.ssl || usingPeer.tcp, usingPeer.host, usingPeer.ssl ? 'tls' : 'tcp');
mainClient.onError = function(e) {
console.log('ElectrumClient error: ' + e);
mainConnected = false;
Expand Down Expand Up @@ -78,7 +78,8 @@ async function getRandomHardcodedPeer() {
async function getSavedPeer() {
let host = await AsyncStorage.getItem(AppStorage.ELECTRUM_HOST);
let port = await AsyncStorage.getItem(AppStorage.ELECTRUM_TCP_PORT);
return { host, tcp: port };
let sslPort = await AsyncStorage.getItem(AppStorage.ELECTRUM_SSL_PORT);
return { host, tcp: port, ssl: sslPort };
}

/**
Expand Down Expand Up @@ -391,17 +392,15 @@ module.exports.broadcastV2 = async function(hex) {
*
* @param host
* @param tcpPort
* @param sslPort
* @returns {Promise<boolean>} Whether provided host:port is a valid electrum server
*/
module.exports.testConnection = async function(host, tcpPort) {
let client = new ElectrumClient(tcpPort, host, 'tcp');
module.exports.testConnection = async function(host, tcpPort, sslPort) {
let client = new ElectrumClient(sslPort || tcpPort, host, sslPort ? 'tls' : 'tcp');
try {
await client.connect();
await client.server_version('2.7.11', '1.4');
await client.server_ping();

client.keepAlive = () => {}; // dirty hack to make it stop reconnecting
client.reconnect = () => {}; // dirty hack to make it stop reconnecting
client.close();
return true;
} catch (_) {
Expand All @@ -410,8 +409,6 @@ module.exports.testConnection = async function(host, tcpPort) {
};

module.exports.forceDisconnect = () => {
mainClient.keepAlive = () => {}; // dirty hack to make it stop reconnecting
mainClient.reconnect = () => {}; // dirty hack to make it stop reconnecting
mainClient.close();
};

Expand Down
1 change: 1 addition & 0 deletions class/app-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export class AppStorage {
static LNDHUB = 'lndhub';
static ELECTRUM_HOST = 'electrum_host';
static ELECTRUM_TCP_PORT = 'electrum_tcp_port';
static ELECTRUM_SSL_PORT = 'electrum_ssl_port';
static PREFERRED_CURRENCY = 'preferredCurrency';
static ADVANCED_MODE_ENABLED = 'advancedmodeenabled';
static DELETE_WALLET_AFTER_UNINSTALL = 'deleteWalletAfterUninstall';
Expand Down
101 changes: 77 additions & 24 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
"crypto-js": "3.1.9-1",
"dayjs": "1.8.20",
"ecurve": "1.0.6",
"electrum-client": "git+https://github.com/BlueWallet/rn-electrum-client.git#d194ff69195ccc86f72088ea3712179b4be9cbb4",
"electrum-client": "git+https://github.com/BlueWallet/rn-electrum-client.git#aa73cd8bb3f2d6aefd7c054cb08d5bf5151d1cbb",
"eslint-config-prettier": "6.10.0",
"eslint-config-standard": "12.0.0",
"eslint-config-standard-react": "7.0.2",
Expand Down Expand Up @@ -122,7 +122,7 @@
"react-native-snap-carousel": "3.8.4",
"react-native-sortable-list": "0.0.23",
"react-native-svg": "9.13.6",
"react-native-tcp": "git+https://github.com/aprock/react-native-tcp.git",
"react-native-tcp": "git+https://github.com/BlueWallet/react-native-tcp.git",
"react-native-tooltip": "git+https://github.com/marcosrdz/react-native-tooltip.git",
"react-native-vector-icons": "6.6.0",
"react-native-watch-connectivity": "0.4.2",
Expand Down
36 changes: 34 additions & 2 deletions screen/settings/electrumSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ export default class ElectrumSettings extends Component {
async componentDidMount() {
let host = await AsyncStorage.getItem(AppStorage.ELECTRUM_HOST);
let port = await AsyncStorage.getItem(AppStorage.ELECTRUM_TCP_PORT);
let sslPort = await AsyncStorage.getItem(AppStorage.ELECTRUM_SSL_PORT);

this.setState({
isLoading: false,
host,
port,
sslPort,
});

await this.setState({
Expand All @@ -41,16 +43,19 @@ export default class ElectrumSettings extends Component {
this.setState({ isLoading: true }, async () => {
this.state.host = this.state.host ? this.state.host : '';
this.state.port = this.state.port ? this.state.port : '';
this.state.sslPort = this.state.sslPort ? this.state.sslPort : '';
try {
if (!this.state.host && !this.state.port) {
if (!this.state.host && !this.state.port && !this.state.sslPort) {
await AsyncStorage.setItem(AppStorage.ELECTRUM_HOST, '');
await AsyncStorage.setItem(AppStorage.ELECTRUM_TCP_PORT, '');
await AsyncStorage.setItem(AppStorage.ELECTRUM_SSL_PORT, '');
alert('Your changes have been saved successfully. Restart may be required for changes to take effect.');
} else if (!(await BlueElectrum.testConnection(this.state.host, this.state.port))) {
} else if (!(await BlueElectrum.testConnection(this.state.host, this.state.port, this.state.sslPort))) {
alert("Can't connect to provided Electrum server");
} else {
await AsyncStorage.setItem(AppStorage.ELECTRUM_HOST, this.state.host);
await AsyncStorage.setItem(AppStorage.ELECTRUM_TCP_PORT, this.state.port);
await AsyncStorage.setItem(AppStorage.ELECTRUM_SSL_PORT, this.state.sslPort);
alert('Your changes have been saved successfully. Restart may be required for changes to take effect.');
}
} catch (_) {}
Expand Down Expand Up @@ -99,6 +104,7 @@ export default class ElectrumSettings extends Component {
borderBottomWidth: 0.5,
backgroundColor: '#f5f5f5',
minHeight: 44,
width: 200,
height: 44,
alignItems: 'center',
borderRadius: 4,
Expand All @@ -114,6 +120,32 @@ export default class ElectrumSettings extends Component {
underlineColorAndroid="transparent"
/>
</View>
<BlueSpacing20 />
<View
style={{
flexDirection: 'row',
borderColor: '#d2d2d2',
borderBottomColor: '#d2d2d2',
borderWidth: 1.0,
borderBottomWidth: 0.5,
backgroundColor: '#f5f5f5',
minHeight: 44,
height: 44,
width: 200,
alignItems: 'center',
borderRadius: 4,
}}
>
<TextInput
placeholder={'SSL port, usually 50002'}
value={this.state.sslPort}
onChangeText={text => this.setState({ sslPort: text })}
numberOfLines={1}
style={{ flex: 1, marginHorizontal: 8, minHeight: 36, height: 36 }}
editable={!this.state.isLoading}
underlineColorAndroid="transparent"
/>
</View>

<BlueSpacing20 />
{this.state.isLoading ? <BlueLoading /> : <BlueButton onPress={this.save} title={loc.settings.save} />}
Expand Down
4 changes: 3 additions & 1 deletion shim.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* global __DEV__, localStorage */
global.net = require('react-native-tcp');
if (typeof __dirname === 'undefined') global.__dirname = '/';
if (typeof __filename === 'undefined') global.__filename = '';
if (typeof process === 'undefined') {
Expand All @@ -16,6 +15,9 @@ if (typeof process === 'undefined') {
process.browser = false;
if (typeof Buffer === 'undefined') global.Buffer = require('buffer').Buffer;

global.net = require('react-native-tcp');
global.tls = require('react-native-tcp/tls');

// global.location = global.location || { port: 80 }
const isDev = typeof __DEV__ === 'boolean' && __DEV__;
process.env['NODE_ENV'] = isDev ? 'development' : 'production';
Expand Down
Loading

0 comments on commit 6b188d2

Please sign in to comment.