@berrywallet/core
Version:
Berrywallet main Core for work with common cryptocurrencies like Bitcoin, Ethereum, Dash, Litecoin
113 lines (112 loc) • 4.67 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
}
Object.defineProperty(exports, "__esModule", { value: true });
const lodash_1 = require("lodash");
const url_1 = require("url");
const _1 = require("../../../");
const tracker_client_1 = require("./tracker-client");
const socket_io_client_1 = __importDefault(require("socket.io-client"));
class InsightTrackerProvider extends tracker_client_1.TrackerClient {
constructor(networkClient) {
super(networkClient);
this.connected = false;
this.enableReconnect = true;
this.onHandleBlock = (blockHash) => __awaiter(this, void 0, void 0, function* () {
const block = yield this.networkClient.getBlock(blockHash);
this.fireNewBlock(block);
});
this.onHandleTransaction = (tx) => __awaiter(this, void 0, void 0, function* () {
const { callback, addrs } = this.addrTxEvents;
if (callback && addrs.length) {
const transactionAddrs = lodash_1.reduce(tx.vout, (list, vout) => [...list, ...Object.keys(vout)], []);
const addr = lodash_1.find(transactionAddrs, (addr) => this.isAddrTrack(addr));
if (addr) {
const responseTx = yield this.networkClient.getTx(tx.txid);
callback(responseTx);
}
}
});
const wsUrl = url_1.parse(this.networkClient.getWSUrl());
this.debug = _1.Debug.create('SOCKET:' + wsUrl.host);
setTimeout(() => this.createSocketConnection(), 1);
}
destruct() {
this.enableReconnect = false;
this.__removeSocketConnection();
super.destruct();
}
createSocketConnection() {
this.socket = socket_io_client_1.default.connect(this.networkClient.getWSUrl(), {
timeout: 1000,
autoConnect: false,
rejectUnauthorized: true,
transports: ['websocket'],
});
this.socket.on('connect', () => {
this.debug('Socket connected!');
setTimeout(() => {
(!this.connected && this.socket) && this.fireConnect();
}, 500);
});
this.socket.on('block', this.onHandleBlock);
this.socket.on('tx', this.onHandleTransaction);
this.socket.on('error', (error) => {
this.debug(error);
this.debug('Socket connection error');
this.fireConnectionError(error);
this.reconnectSocket();
});
this.socket.on('connect_timeout', () => {
this.debug('Socket connection timeout');
this.reconnectSocket();
});
this.socket.on('disconnect', () => {
this.reconnectSocket();
});
this.socket.open();
}
reconnectSocket() {
if (!this.enableReconnect) {
return;
}
this.__removeSocketConnection();
this.debug('Start reconnecting...');
setTimeout(() => this.createSocketConnection(), 2000);
}
fireConnect() {
if (!this.socket) {
throw new Error('No socket connection for ' + this.networkClient.getWSUrl());
}
this.socket.emit('subscribe', 'inv');
this.connected = true;
return super.fireConnect();
}
fireNewBlock(block) {
lodash_1.forEach(block.txids, (txid) => __awaiter(this, void 0, void 0, function* () {
if (this.listenerCount('tx.' + txid) === 0)
return;
const tx = yield this.networkClient.getTx(txid);
tx && this.fireTxidConfirmation(tx);
}));
return super.fireNewBlock(block);
}
__removeSocketConnection() {
this.connected = false;
if (typeof this.socket !== 'undefined') {
this.socket.removeAllListeners();
this.socket.close();
delete this.socket;
}
}
}
exports.InsightTrackerProvider = InsightTrackerProvider;