Browse Source

Added net & tls as params throughout helpers/index.js as needed.

Updated connectResponse timeout in connectToPeer method in helpers/index.
Updated initSocket & getSocket in init_socket.js to work with and utilize the passed net & tls params.
get-transaction-merkle
Corey Phillips 4 years ago
parent
commit
8cc26a41c6
No known key found for this signature in database GPG Key ID: 80C0975F55D3A07B
  1. 19
      helpers/index.js
  2. 12
      lib/init_socket.js

19
helpers/index.js

@ -78,7 +78,7 @@ const pingServer = ({ id = Math.random() } = {}) => {
//peers = A list of peers acquired from default electrum servers using the getPeers method.
//customPeers = A list of peers added by the user to connect to by default in lieu of the default peer list.
const start = ({ id = Math.random(), network = "", peers = [], customPeers = []} = {}) => {
const start = ({ id = Math.random(), network = "", peers = [], customPeers = [], net, tls} = {}) => {
const method = "connectToPeer";
return new Promise(async (resolve) => {
try {
@ -99,11 +99,12 @@ const start = ({ id = Math.random(), network = "", peers = [], customPeers = []}
//Attempt to connect to specified peer
let connectionResponse = { error: true, data: "" };
if (customPeersLength > 0) {
const { host = "", port = "", protocol = "ssl" } = customPeers[0];
connectionResponse = await connectToPeer({ host, port, protocol, network });
const { host = "", protocol = "ssl" } = customPeers[0];
const port = customPeers[0][protocol];
connectionResponse = await connectToPeer({ host, port, protocol, network, net, tls });
} else {
//Attempt to connect to random peer if none specified
connectionResponse = await connectToRandomPeer(network, peers);
connectionResponse = await connectToRandomPeer(network, peers, 'ssl', net, tls);
}
resolve({
...connectionResponse,
@ -119,7 +120,7 @@ const start = ({ id = Math.random(), network = "", peers = [], customPeers = []}
});
};
const connectToPeer = ({ port = 50002, host = "", protocol = "ssl", network = "bitcoin" } = {}) => {
const connectToPeer = ({ port = 50002, host = "", protocol = "ssl", network = "bitcoin", net, tls } = {}) => {
return new Promise(async (resolve) => {
try {
clients.network = network;
@ -134,8 +135,8 @@ const connectToPeer = ({ port = 50002, host = "", protocol = "ssl", network = "b
}
}
if (needToConnect) {
clients.mainClient[network] = new ElectrumClient(port, host, protocol);
connectionResponse = await promiseTimeout(1000, clients.mainClient[network].connect());
clients.mainClient[network] = new ElectrumClient(port, host, protocol, net, tls);
connectionResponse = await promiseTimeout(_getTimeout(), clients.mainClient[network].connect());
if (connectionResponse.error) {
return resolve(connectionResponse);
}
@ -166,7 +167,7 @@ const connectToPeer = ({ port = 50002, host = "", protocol = "ssl", network = "b
});
};
const connectToRandomPeer = async (network, peers = [], protocol = "ssl") => {
const connectToRandomPeer = async (network, peers = [], protocol = "ssl", net, tls) => {
//Peers can be found in peers.json.
//Additional Peers can be located here in servers.json & servers_testnet.json for reference: https://github.com/spesmilo/electrum/tree/master/electrum
let hasPeers = false;
@ -202,7 +203,7 @@ const connectToRandomPeer = async (network, peers = [], protocol = "ssl") => {
host = peer.host;
protocol = peer.protocol;
}
const connectionResponse = await connectToPeer({ port, host, protocol, network });
const connectionResponse = await connectToPeer({ port, host, protocol, network, net, tls });
if (connectionResponse.error === false && connectionResponse.data) {
return {
error: connectionResponse.error,

12
lib/init_socket.js

@ -1,23 +1,21 @@
'use strict'
const net = require('net');
const tls = require('tls');
const TlsSocketWrapper = require('./TlsSocketWrapper.js');
const TIMEOUT = 5000
const getSocket = (protocol, options) => {
const getSocket = (self, protocol, options) => {
switch(protocol){
case 'tcp':
return new net.Socket();
return new self.net.Socket();
case 'tls':
case 'ssl':
if (!tls) throw new Error('tls package could not be loaded');
return new TlsSocketWrapper({ tls, verbose: false });
if (!self.tls) throw new Error('tls package could not be loaded');
return new TlsSocketWrapper({ tls: self.tls, verbose: false });
}
throw new Error('unknown protocol')
}
const initSocket = (self, protocol, options) => {
const conn = getSocket(protocol, options);
const conn = getSocket(self, protocol, options);
conn.setTimeout(TIMEOUT)
conn.setEncoding('utf8')
conn.setKeepAlive(true, 0)

Loading…
Cancel
Save