nativescript-electrumx-client
Version:
An ElectrumX Client wrapper for NativeScript
316 lines • 13.8 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var EventEmitter = require("events");
var UnexpectedResponseError = (function (_super) {
__extends(UnexpectedResponseError, _super);
function UnexpectedResponseError(rawResponse) {
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
var _this = _super.apply(this, args) || this;
_this.name = 'UnexpectedResponseError';
_this.message = args[0];
_this.rawResponse = rawResponse;
return _this;
}
return UnexpectedResponseError;
}(Error));
exports.UnexpectedResponseError = UnexpectedResponseError;
var TCPClientError = (function (_super) {
__extends(TCPClientError, _super);
function TCPClientError(rawResponse) {
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
var _this = _super.apply(this, args) || this;
_this.name = 'TCPClientError';
_this.message = args[0];
return _this;
}
return TCPClientError;
}(Error));
exports.TCPClientError = TCPClientError;
function makeRequest(method, params, id) {
if (typeof id === 'undefined')
id = 0;
return JSON.stringify({
jsonrpc: "2.0",
method: method,
params: params,
id: id,
});
}
exports.makeRequest = makeRequest;
function createPromiseResult(resolve, reject) {
return function (err, result) {
if (err)
reject(err);
else
resolve(result);
};
}
exports.createPromiseResult = createPromiseResult;
var TcpClient = (function () {
function TcpClient(host, port, options) {
if (typeof options === 'undefined')
options = {};
this.host = host;
this.port = port;
this.status = 0;
this.atomicInteger = 0;
this.callbackQueue = {};
this.subscribe = new EventEmitter();
var self = this;
var listener = new cz.honzamrazek.simplenetworking.TcpClientListener({
onData: function (data) {
var dataArr = data.split('\n');
dataArr.pop();
dataArr.forEach(function (_raw) {
try {
var data_1 = JSON.parse(_raw);
if (data_1.id !== undefined)
self.handleResponse(data_1);
else
self.subscribe.emit(data_1.method, data_1.params);
}
catch (err) {
self.subscribe.emit('error', new UnexpectedResponseError(_raw, 'Invalid JSON'));
}
self.subscribe.emit('data', _raw);
});
},
onError: function (id, message) {
console.log(message);
self.subscribe.emit('error', new TCPClientError("TCPClient encountered an issue with (#" + id + "):" + message));
},
onFinished: function (id) {
self.subscribe.emit('finished', id);
}
});
this.client = new cz.honzamrazek.simplenetworking.TcpClient(listener);
}
TcpClient.prototype.connect = function () {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
var callback;
return __generator(this, function (_a) {
console.log('MAKING CONNECTION', {
h: this.host,
p: this.port
});
return [2, new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
var id;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (this.status)
return [2, resolve(true)];
console.log('doing it!', {
h: this.host,
p: this.port
});
this.status = 1;
return [4, this.client.start(this.host, this.port)];
case 1:
id = _a.sent();
setTimeout(function () {
resolve(id);
}, 2000);
return [2];
}
});
}); })];
});
});
};
TcpClient.prototype.close = function () {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!this.status)
return [2, true];
this.status = 0;
Object.keys(this.callbackQueue).forEach(function (key) {
_this.callbackQueue[key](new Error('close connect'));
delete _this.callbackQueue[key];
});
return [4, this.client.stop()];
case 1:
_a.sent();
return [2, Promise.resolve()];
}
});
});
};
TcpClient.prototype.send = function (content) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2, this.client.send(content)];
});
});
};
TcpClient.prototype.handleResponse = function (data) {
var callback = this.callbackQueue[data.id];
if (!callback)
return;
delete this.callbackQueue[data.id];
if (data.error)
callback(data.error);
else
callback(null, data.result);
};
return TcpClient;
}());
exports.TcpClient = TcpClient;
var ElectrumxClient = (function (_super) {
__extends(ElectrumxClient, _super);
function ElectrumxClient(host, port, options) {
return _super.call(this, host, port, options) || this;
}
ElectrumxClient.prototype._request = function (method, params) {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
if (!this.status)
return [2, Promise.reject(new Error('ESOCKET'))];
return [2, new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
var id, content;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
id = ++this.atomicInteger;
content = makeRequest(method, params, id);
this.callbackQueue[id] = createPromiseResult(resolve, reject);
return [4, this.send(content + '\n')];
case 1:
_a.sent();
return [2];
}
});
}); })];
});
});
};
ElectrumxClient.prototype.close = function () {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
var list;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4, _super.prototype.close.call(this)];
case 1:
_a.sent();
list = [
'blockchain.headers.subscribe',
'blockchain.address.subscribe'
];
list.forEach(function (eventName) { return _this.subscribe.removeAllListeners(eventName); });
return [2, Promise.resolve(true)];
}
});
});
};
ElectrumxClient.prototype.server_version = function (client_name, protocol_version) {
return this._request('server.version', [client_name, protocol_version]);
};
ElectrumxClient.prototype.server_banner = function () {
return this._request('server.banner', []);
};
ElectrumxClient.prototype.server_ping = function () {
return this._request('server.ping', []);
};
ElectrumxClient.prototype.server_addPeer = function (features) {
return this._request('server.add_peer', [features]);
};
ElectrumxClient.prototype.serverDonation_address = function () {
return this._request('server.donation_address', []);
};
ElectrumxClient.prototype.serverPeers_subscribe = function () {
return this._request('server.peers.subscribe', []);
};
ElectrumxClient.prototype.blockchainScripthash_getBalance = function (scripthash) {
return this._request('blockchain.scripthash.get_balance', [scripthash]);
};
ElectrumxClient.prototype.blockchainScripthash_getHistory = function (scripthash) {
return this._request('blockchain.scripthash.get_history', [scripthash]);
};
ElectrumxClient.prototype.blockchainScripthash_history = function (scripthash, start_height) {
return this._request('blockchain.scripthash.history', [scripthash, start_height]);
};
ElectrumxClient.prototype.blockchainScripthash_utxos = function (scripthash, start_height) {
return this._request('blockchain.scripthash.utxos', [scripthash, start_height]);
};
ElectrumxClient.prototype.blockchainScripthash_getMempool = function (scripthash) {
return this._request('blockchain.scripthash.get_mempool', [scripthash]);
};
ElectrumxClient.prototype.blockchainScripthash_listunspent = function (scripthash) {
return this._request('blockchain.scripthash.listunspent', [scripthash]);
};
ElectrumxClient.prototype.blockchainScripthash_subscribe = function (scripthash) {
return this._request('blockchain.scripthash.subscribe', [scripthash]);
};
ElectrumxClient.prototype.blockchainBlock_header = function (height, cp_height) {
return this._request('blockchain.block.header', [height, cp_height]);
};
ElectrumxClient.prototype.blockchainBlock_headers = function (start_height, count) {
return this._request('blockchain.block.headers', [start_height, count]);
};
ElectrumxClient.prototype.blockchainEstimatefee = function (numBlocks) {
return this._request('blockchain.estimatefee', [numBlocks]);
};
ElectrumxClient.prototype.blockchainHeaders_subscribe = function () {
return this._request('blockchain.headers.subscribe', []);
};
ElectrumxClient.prototype.blockchain_relayfee = function () {
return this._request('blockchain.relayfee', []);
};
ElectrumxClient.prototype.blockchainTransaction_broadcast = function (rawtx) {
return this._request('blockchain.transaction.broadcast', [rawtx]);
};
ElectrumxClient.prototype.blockchainTransaction_get = function (txHash, verbose) {
var params = [txHash];
if (typeof verbose !== 'undefined')
verbose = !!(verbose === true);
return this._request('blockchain.transaction.get', [txHash, verbose]);
};
ElectrumxClient.prototype.blockchainTransaction_getMerkle = function (txHash, height) {
return this._request('blockchain.transaction.get_merkle', [txHash, height]);
};
ElectrumxClient.prototype.mempool_getFeeHistogram = function () {
return this._request('mempool.get_fee_histogram', []);
};
ElectrumxClient.prototype.blockchainBlock_getHeader = function (height) {
return this._request('blockchain.block.get_header', [height]);
};
ElectrumxClient.prototype.blockchainBlock_getChunk = function (index) {
return this._request('blockchain.block.get_chunk', [index]);
};
ElectrumxClient.prototype.blockchainAddress_getBalance = function (address) {
return this._request('blockchain.address.get_balance', [address]);
};
ElectrumxClient.prototype.blockchainAddress_getHistory = function (address) {
return this._request('blockchain.address.get_history', [address]);
};
ElectrumxClient.prototype.blockchainAddress_getMempool = function (address) {
return this._request('blockchain.address.get_mempool', [address]);
};
ElectrumxClient.prototype.blockchainAddress_listunspent = function (address) {
return this._request('blockchain.address.listunspent', [address]);
};
ElectrumxClient.prototype.blockchainAddress_subscribe = function (address) {
return this._request('blockchain.address.subscribe', [address]);
};
ElectrumxClient.prototype.blockchainUtxo_getAddress = function (tx_hash, index) {
return this._request('blockchain.utxo.get_address', [tx_hash, index]);
};
ElectrumxClient.prototype.blockchainNumblocks_subscribe = function () {
return this._request('blockchain.numblocks.subscribe', []);
};
return ElectrumxClient;
}(TcpClient));
exports.ElectrumxClient = ElectrumxClient;
//# sourceMappingURL=electrumx-client.android.js.map