Skip to content

Commit

Permalink
REF: net & tls dependency inject
Browse files Browse the repository at this point in the history
  • Loading branch information
Overtorment committed Jan 8, 2021
1 parent f9a827e commit 99ebcc6
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 18 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,6 @@ typings/
# dotenv environment variables file
.env

#editors
.idea/

17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,24 @@ Electrum Protocol Client for React Native

## usage

Relies on `react-native-tcp` so it should be already installed and linked in RN project. `net` should be provided from outside, this library wont do `require('net')`.
For RN it should be in `shim.js`:
Relies on `react-native-tcp` so it should be already installed and linked in RN project. `net` & `tls` dependencies should be
injected via constructor, this library won't do `require('net')`.
For RN you can place it in `shim.js`:

```javascript
global.net = require('react-native-tcp');
```

For nodejs it should be provided before usage:
For nodejs simply

```javascript
global.net = require('net');
const net = require('net');
```

and then

```javascript
const client = new ElectrumClient(net, false, 50001, 'electrum1.bluewallet.io', 'tcp');
const ver = await client.initElectrum({ client: 'bluewallet', version: '1.4' });
const balance = await client.blockchainScripthash_getBalance('716decbe1660861c3d93906cb1d98ee68b154fd4d23aed9783859c1271b52a9c');
```
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
const Client = require('./lib/client');

class ElectrumClient extends Client {
constructor(port, host, protocol, options) {
super(port, host, protocol, options);
constructor(net, tls, port, host, protocol, options) {
super(net, tls, port, host, protocol, options);
this.timeLastCall = 0;
}

Expand Down
22 changes: 11 additions & 11 deletions lib/client.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
'use strict';
/**
* expecting NET & TLS to be injected from outside:
* for RN it should be in shim.js:
* NET & TLS dependencies should be injected via constructor
* for RN it can be something like this in shim.js:
* global.net = require('react-native-tcp');
* global.tls = require('react-native-tcp/tls');
*
* for nodejs tests it should be provided before tests:
* global.net = require('net');
* global.tls = require('tls');
* for nodejs tests it should be regular node's net * tls:
* const net = require('net');
* const tls = require('tls');
* */
let net = global.net;
let tls = global.tls;
const TIMEOUT = 5000;

const TlsSocketWrapper = require('./TlsSocketWrapper.js');
const EventEmitter = require('events').EventEmitter;
const util = require('./util');

class Client {
constructor(port, host, protocol, options) {
constructor(net, tls, port, host, protocol, options) {
this.net = net;
this.tls = tls;
this.id = 0;
this.port = port;
this.host = host;
Expand All @@ -37,14 +37,14 @@ class Client {
options = options || this._options;
switch (protocol) {
case 'tcp':
this.conn = new net.Socket();
this.conn = new this.net.Socket();
break;
case 'tls':
case 'ssl':
if (!tls) {
if (!this.tls) {
throw new Error('tls package could not be loaded');
}
this.conn = new TlsSocketWrapper(tls);
this.conn = new TlsSocketWrapper(this.tls);
break;
default:
throw new Error('unknown protocol');
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "electrum-client",
"version": "1.3.0",
"version": "2.0.0",
"description": "Electrum protocol client for React Native & Node.js",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 99ebcc6

Please sign in to comment.