REF: update rn-electrum-client to support latest react-native-tcp-socket; delete obsolete code

This commit is contained in:
Marcos Rodriguez Vélez 2024-02-05 15:00:34 -04:00 committed by GitHub
parent 975edd19ed
commit 30cde4281f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 29 additions and 187 deletions

View File

@ -10,6 +10,9 @@ const ElectrumClient = require('electrum-client');
const reverse = require('buffer-reverse');
const BigNumber = require('bignumber.js');
const net = require('net');
const tls = require('tls');
const Realm = require('realm');
const ELECTRUM_HOST = 'electrum_host';
@ -121,7 +124,7 @@ async function connectMain() {
try {
console.log('begin connection:', JSON.stringify(usingPeer));
mainClient = new ElectrumClient(global.net, global.tls, usingPeer.ssl || usingPeer.tcp, usingPeer.host, usingPeer.ssl ? 'tls' : 'tcp');
mainClient = new ElectrumClient(net, tls, usingPeer.ssl || usingPeer.tcp, usingPeer.host, usingPeer.ssl ? 'tls' : 'tcp');
mainClient.onError = function (e) {
console.log('electrum mainClient.onError():', e.message);
@ -934,7 +937,7 @@ module.exports.calculateBlockTime = function (height) {
* @returns {Promise<boolean>} Whether provided host:port is a valid electrum server
*/
module.exports.testConnection = async function (host, tcpPort, sslPort) {
const client = new ElectrumClient(global.net, global.tls, sslPort || tcpPort, host, sslPort ? 'tls' : 'tcp');
const client = new ElectrumClient(net, tls, sslPort || tcpPort, host, sslPort ? 'tls' : 'tcp');
client.onError = () => {}; // mute
let timeoutId = false;

View File

@ -1,110 +0,0 @@
/**
* @fileOverview adapter for ReactNative TCP module
* This module mimics the nodejs net api and is intended to work in RN environment.
* @see https://github.com/Rapsssito/react-native-tcp-socket
*/
import TcpSocket from 'react-native-tcp-socket';
/**
* Constructor function. Resulting object has to act as it was a real socket (basically
* conform to nodejs/net api)
*
* @constructor
*/
function Socket() {
this._socket = false; // reference to socket thats gona be created later
// defaults:
this._noDelay = true;
this._listeners = {};
// functions not supported by RN module, yet:
this.setTimeout = () => {};
this.setEncoding = () => {};
this.setKeepAlive = () => {};
// proxying call to real socket object:
this.setNoDelay = noDelay => {
if (this._socket) this._socket.setNoDelay(noDelay);
this._noDelay = noDelay;
};
this.connect = (port, host, callback) => {
this._socket = TcpSocket.createConnection(
{
port,
host,
tls: false,
},
callback,
);
this._socket.on('data', data => {
this._passOnEvent('data', data);
});
this._socket.on('error', data => {
this._passOnEvent('error', data);
});
this._socket.on('close', data => {
this._passOnEvent('close', data);
});
this._socket.on('connect', data => {
this._passOnEvent('connect', data);
this._socket.setNoDelay(this._noDelay);
});
this._socket.on('connection', data => {
this._passOnEvent('connection', data);
});
};
this._passOnEvent = (event, data) => {
this._listeners[event] = this._listeners[event] || [];
for (const savedListener of this._listeners[event]) {
savedListener(data);
}
};
this.on = (event, listener) => {
this._listeners[event] = this._listeners[event] || [];
this._listeners[event].push(listener);
};
this.removeListener = (event, listener) => {
this._listeners[event] = this._listeners[event] || [];
const newListeners = [];
let found = false;
for (const savedListener of this._listeners[event]) {
if (savedListener === listener) {
// found our listener
found = true;
// we just skip it
} else {
// other listeners should go back to original array
newListeners.push(savedListener);
}
}
if (found) {
this._listeners[event] = newListeners;
} else {
// something went wrong, lets just cleanup all listeners
this._listeners[event] = [];
}
};
this.end = () => {
this._socket.end();
};
this.destroy = () => {
this._socket.destroy();
};
this.write = data => {
this._socket.write(data);
};
}
module.exports.Socket = Socket;

View File

@ -1,46 +0,0 @@
/**
* @fileOverview adapter for ReactNative TCP module
* This module mimics the nodejs tls api and is intended to work in RN environment.
* @see https://github.com/Rapsssito/react-native-tcp-socket
*/
import TcpSocket from 'react-native-tcp-socket';
/**
* Constructor function. Mimicking nodejs/tls api
*
* @constructor
*/
function connect(config, callback) {
const client = TcpSocket.createConnection(
{
port: config.port,
host: config.host,
tls: true,
tlsCheckValidity: config.rejectUnauthorized,
},
callback,
);
// defaults:
this._noDelay = true;
// functions not supported by RN module, yet:
client.setTimeout = () => {};
client.setEncoding = () => {};
client.setKeepAlive = () => {};
// we will save `noDelay` and proxy it to socket object when its actually created and connected:
const realSetNoDelay = client.setNoDelay; // reference to real setter
client.setNoDelay = noDelay => {
this._noDelay = noDelay;
};
client.on('connect', () => {
realSetNoDelay.apply(client, [this._noDelay]);
});
return client;
}
module.exports.connect = connect;

View File

@ -349,7 +349,7 @@ PODS:
- React-Core
- react-native-secure-key-store (2.0.10):
- React-Core
- react-native-tcp-socket (5.6.2):
- react-native-tcp-socket (6.0.6):
- CocoaAsyncSocket
- React-Core
- react-native-webview (13.7.0):
@ -822,7 +822,7 @@ SPEC CHECKSUMS:
react-native-randombytes: 421f1c7d48c0af8dbcd471b0324393ebf8fe7846
react-native-safe-area-context: b97eb6f9e3b7f437806c2ce5983f479f8eb5de4b
react-native-secure-key-store: 910e6df6bc33cb790aba6ee24bc7818df1fe5898
react-native-tcp-socket: c1b7297619616b4c9caae6889bcb0aba78086989
react-native-tcp-socket: e724380c910c2e704816ec817ed28f1342246ff7
react-native-webview: 4e7d637b43eddec107016d316ae75f7063a3075c
react-native-widget-center: 12dfba20a4fa995850b52cf0afecf734397f4b9c
React-NativeModulesApple: c3e696ff867e4bc212266cbdf7e862e48a0166fd

28
package-lock.json generated
View File

@ -45,7 +45,7 @@
"detox": "20.17.0",
"ecpair": "2.0.1",
"ecurve": "1.0.6",
"electrum-client": "https://github.com/BlueWallet/rn-electrum-client#76c0ea35e1a50c47f3a7f818d529ebd100161496",
"electrum-client": "github:BlueWallet/rn-electrum-client#1bfe3cc",
"electrum-mnemonic": "2.0.0",
"events": "3.3.0",
"frisbee": "3.1.0",
@ -95,7 +95,7 @@
"react-native-secure-key-store": "https://github.com/BlueWallet/react-native-secure-key-store#2076b48",
"react-native-share": "10.0.2",
"react-native-svg": "13.14.0",
"react-native-tcp-socket": "5.6.2",
"react-native-tcp-socket": "6.0.6",
"react-native-vector-icons": "10.0.3",
"react-native-watch-connectivity": "1.1.0",
"react-native-webview": "13.7.0",
@ -10054,12 +10054,11 @@
"integrity": "sha512-osHqhtjojpCsACVnuD11xO5g9xaCyw7Qqn/C2KParkMv42i8jrJJgx3g7mkHfpxwhy9MnOJr8+pKOdZ7qzgizg=="
},
"node_modules/electrum-client": {
"version": "2.0.0",
"resolved": "git+ssh://git@github.com/BlueWallet/rn-electrum-client.git#76c0ea35e1a50c47f3a7f818d529ebd100161496",
"integrity": "sha512-w9LHCQYUlCddBRGrDmgo1EUNp+zmzcyQSKLFOeO1XPITiAAFQDBZLwORVbBPywhMXf4PUk1dOphhHzJBJYG0vA==",
"version": "3.1.0",
"resolved": "git+ssh://git@github.com/BlueWallet/rn-electrum-client.git#1bfe3cc4249d5440b816baac942b0cfa921eebf9",
"license": "MIT",
"engines": {
"node": ">=6"
"node": ">=18"
}
},
"node_modules/electrum-mnemonic": {
@ -19953,9 +19952,9 @@
}
},
"node_modules/react-native-tcp-socket": {
"version": "5.6.2",
"resolved": "https://registry.npmjs.org/react-native-tcp-socket/-/react-native-tcp-socket-5.6.2.tgz",
"integrity": "sha512-doijFOAJd9p8KmduhfbZaPfqRVd3CZuTLAimJx0yxIqFWy/EDPGHeFVrOEOqRZ3lWBVDcssiCIQJhV0baKu5Pg==",
"version": "6.0.6",
"resolved": "https://registry.npmjs.org/react-native-tcp-socket/-/react-native-tcp-socket-6.0.6.tgz",
"integrity": "sha512-FIFSP+3S5OnJRjl7ddD7G9NfVbVEiQ4WROEmOBei1cKE0pDz2R/Kcm4XkFlnMd5sdoG6Wbv830Yd/ZsmOjkEKw==",
"dependencies": {
"buffer": "^5.4.3",
"eventemitter3": "^4.0.7"
@ -30194,9 +30193,8 @@
"integrity": "sha512-osHqhtjojpCsACVnuD11xO5g9xaCyw7Qqn/C2KParkMv42i8jrJJgx3g7mkHfpxwhy9MnOJr8+pKOdZ7qzgizg=="
},
"electrum-client": {
"version": "git+ssh://git@github.com/BlueWallet/rn-electrum-client.git#76c0ea35e1a50c47f3a7f818d529ebd100161496",
"integrity": "sha512-w9LHCQYUlCddBRGrDmgo1EUNp+zmzcyQSKLFOeO1XPITiAAFQDBZLwORVbBPywhMXf4PUk1dOphhHzJBJYG0vA==",
"from": "electrum-client@https://github.com/BlueWallet/rn-electrum-client#76c0ea35e1a50c47f3a7f818d529ebd100161496"
"version": "git+ssh://git@github.com/BlueWallet/rn-electrum-client.git#1bfe3cc4249d5440b816baac942b0cfa921eebf9",
"from": "electrum-client@github:BlueWallet/rn-electrum-client#1bfe3cc"
},
"electrum-mnemonic": {
"version": "2.0.0",
@ -37643,9 +37641,9 @@
}
},
"react-native-tcp-socket": {
"version": "5.6.2",
"resolved": "https://registry.npmjs.org/react-native-tcp-socket/-/react-native-tcp-socket-5.6.2.tgz",
"integrity": "sha512-doijFOAJd9p8KmduhfbZaPfqRVd3CZuTLAimJx0yxIqFWy/EDPGHeFVrOEOqRZ3lWBVDcssiCIQJhV0baKu5Pg==",
"version": "6.0.6",
"resolved": "https://registry.npmjs.org/react-native-tcp-socket/-/react-native-tcp-socket-6.0.6.tgz",
"integrity": "sha512-FIFSP+3S5OnJRjl7ddD7G9NfVbVEiQ4WROEmOBei1cKE0pDz2R/Kcm4XkFlnMd5sdoG6Wbv830Yd/ZsmOjkEKw==",
"requires": {
"buffer": "^5.4.3",
"eventemitter3": "^4.0.7"

View File

@ -11,11 +11,11 @@
"@babel/runtime": "^7.20.0",
"@jest/reporters": "^27.5.1",
"@react-native/eslint-config": "^0.72.2",
"@react-native/metro-config": "^0.73.0",
"@tsconfig/react-native": "^3.0.2",
"@types/bs58check": "^2.1.0",
"@types/create-hash": "^1.2.2",
"@types/jest": "^29.4.0",
"@react-native/metro-config": "^0.73.0",
"@types/react": "^18.2.16",
"@types/react-native": "^0.72.0",
"@types/react-test-renderer": "^18.0.0",
@ -129,7 +129,7 @@
"detox": "20.17.0",
"ecpair": "2.0.1",
"ecurve": "1.0.6",
"electrum-client": "https://github.com/BlueWallet/rn-electrum-client#76c0ea35e1a50c47f3a7f818d529ebd100161496",
"electrum-client": "github:BlueWallet/rn-electrum-client#1bfe3cc",
"electrum-mnemonic": "2.0.0",
"events": "3.3.0",
"frisbee": "3.1.0",
@ -179,7 +179,7 @@
"react-native-secure-key-store": "https://github.com/BlueWallet/react-native-secure-key-store#2076b48",
"react-native-share": "10.0.2",
"react-native-svg": "13.14.0",
"react-native-tcp-socket": "5.6.2",
"react-native-tcp-socket": "6.0.6",
"react-native-vector-icons": "10.0.3",
"react-native-watch-connectivity": "1.1.0",
"react-native-webview": "13.7.0",
@ -196,6 +196,8 @@
},
"react-native": {
"crypto": "react-native-crypto",
"net": "react-native-tcp-socket",
"tls": "react-native-tcp-socket",
"path": "path-browserify",
"_stream_transform": "readable-stream/transform",
"_stream_readable": "readable-stream/readable",

View File

@ -15,14 +15,6 @@ if (typeof process === 'undefined') {
process.browser = false;
// global.net = require('react-native-tcp');
// global.tls = require('react-native-tcp/tls');
//
// since new TCP/TLS module for React Native has different api from what is expected from nodejs/net & nodejs/tls
// (or from old module) we wrap this module in adapter:
global.net = require('./blue_modules/net');
global.tls = require('./blue_modules/tls');
// global.location = global.location || { port: 80 }
const isDev = typeof __DEV__ === 'boolean' && __DEV__;
process.env.NODE_ENV = isDev ? 'development' : 'production';

View File

@ -2,6 +2,9 @@ import * as bitcoin from 'bitcoinjs-lib';
import assert from 'assert';
import ElectrumClient from 'electrum-client';
const net = require('net');
const tls = require('tls');
jest.setTimeout(150 * 1000);
const hardcodedPeers = [
@ -16,7 +19,7 @@ const hardcodedPeers = [
describe('ElectrumClient', () => {
it('can connect and query', async () => {
for (const peer of hardcodedPeers) {
const mainClient = new ElectrumClient(global.net, global.tls, peer.ssl || peer.tcp, peer.host, peer.ssl ? 'tls' : 'tcp');
const mainClient = new ElectrumClient(net, tls, peer.ssl || peer.tcp, peer.host, peer.ssl ? 'tls' : 'tcp');
try {
await mainClient.connect();