@parity/api
Version:
The Parity Promise-based API library for interfacing with Ethereum over RPC
109 lines (108 loc) • 4.09 kB
JavaScript
"use strict";
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
var outTransaction = require('../format/output').outTransaction;
var Signer = /** @class */ (function () {
function Signer(updateSubscriptions, api, subscriber) {
var _this = this;
this._subscriber = subscriber;
this._api = api;
this._updateSubscriptions = updateSubscriptions;
this._started = false;
this._listRequests = this._listRequests.bind(this);
this._api.provider.on('close', function () {
if (_this.isStarted) {
_this.start();
}
});
}
Object.defineProperty(Signer.prototype, "isStarted", {
get: function () {
return this._started;
},
enumerable: true,
configurable: true
});
Signer.prototype.start = function () {
var _this = this;
this._started = true;
if (this._api.isPubSub) {
var subscription = this._api.pubsub
.subscribeAndGetResult(function (callback) { return _this._api.pubsub.signer.pendingRequests(callback); }, function (requests) {
_this.updateSubscriptions(requests);
return requests;
});
return Promise.all([
this._listRequests(false),
subscription
]);
}
return Promise.all([
this._listRequests(true),
this._loggingSubscribe()
]);
};
Signer.prototype.updateSubscriptions = function (requests) {
return this._updateSubscriptions('signer_requestsToConfirm', null, requests);
};
Signer.prototype._listRequests = function (doTimeout) {
var _this = this;
var nextTimeout = function (timeout, forceTimeout) {
if (timeout === void 0) { timeout = 1000; }
if (forceTimeout === void 0) { forceTimeout = doTimeout; }
if (forceTimeout) {
setTimeout(function () {
_this._listRequests(doTimeout);
}, timeout);
}
};
if (!this._api.isConnected) {
nextTimeout(500, true);
return;
}
return this._api.signer
.requestsToConfirm()
.then(function (requests) {
_this.updateSubscriptions(requests);
nextTimeout();
})
.catch(function () { return nextTimeout(); });
};
Signer.prototype._postTransaction = function (data) {
var request = {
transaction: outTransaction(data.params[0]),
requestId: data.json.result.result
};
this._updateSubscriptions('parity_postTransaction', null, request);
};
Signer.prototype._loggingSubscribe = function () {
var _this = this;
return this._subscriber.subscribe('logging', function (error, data) {
if (error || !data) {
return;
}
switch (data.method) {
case 'eth_sendTransaction':
case 'eth_sendRawTransaction':
_this._listRequests(false);
break;
case 'parity_postTransaction':
_this._postTransaction(data);
_this._listRequests(false);
break;
}
});
};
return Signer;
}());
module.exports = Signer;