REF: net & tls dependency inject
This commit is contained in:
parent
f9a827e724
commit
99ebcc649d
3
.gitignore
vendored
3
.gitignore
vendored
@ -57,3 +57,6 @@ typings/
|
||||
# dotenv environment variables file
|
||||
.env
|
||||
|
||||
#editors
|
||||
.idea/
|
||||
|
||||
|
||||
17
README.md
17
README.md
@ -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
index.js
4
index.js
@ -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;
|
||||
}
|
||||
|
||||
|
||||
@ -1,16 +1,14 @@
|
||||
'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');
|
||||
@ -18,7 +16,9 @@ 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;
|
||||
@ -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');
|
||||
|
||||
@ -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": {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user