UNPKG

@parity/api

Version:

The Parity Promise-based API library for interfacing with Ethereum over RPC

154 lines (153 loc) 6.06 kB
"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 Personal = /** @class */ (function () { function Personal(updateSubscriptions, api, subscriber) { var _this = this; this._subscriber = subscriber; this._api = api; this._updateSubscriptions = updateSubscriptions; this._started = false; this._lastDefaultAccount = '0x0'; this._pollTimerId = null; this._accountsInfo = this._accountsInfo.bind(this); this._defaultAccount = this._defaultAccount.bind(this); this._listAccounts = this._listAccounts.bind(this); this._api.provider.on('close', function () { if (_this.isStarted) { _this.start(); } }); } Object.defineProperty(Personal.prototype, "isStarted", { get: function () { return this._started; }, enumerable: true, configurable: true }); Personal.prototype.start = function () { var _this = this; this._started = true; var defaultAccount = null; if (this._api.isPubSub) { defaultAccount = this._api.pubsub .subscribeAndGetResult(function (callback) { return _this._api.pubsub.parity.defaultAccount(callback); }, function (defaultAccount) { _this.updateDefaultAccount(defaultAccount); return defaultAccount; }); } else { defaultAccount = this._defaultAccount(); } return Promise.all([ defaultAccount, this._listAccounts(), this._accountsInfo(), this._loggingSubscribe() ]); }; Personal.prototype.updateDefaultAccount = function (defaultAccount) { if (this._lastDefaultAccount !== defaultAccount) { this._lastDefaultAccount = defaultAccount; this._updateSubscriptions('parity_defaultAccount', null, defaultAccount); } }; // FIXME: Because of the different API instances, the "wait for valid changes" approach // doesn't work. Since the defaultAccount is critical to operation, we poll in exactly // same way we do in ../eth (ala eth_blockNumber) and update. This should be moved // to pub-sub as it becomes available Personal.prototype._defaultAccount = function (timerDisabled) { var _this = this; if (timerDisabled === void 0) { timerDisabled = false; } var nextTimeout = function (timeout) { if (timeout === void 0) { timeout = 3000; } if (!timerDisabled) { _this._pollTimerId = setTimeout(function () { _this._defaultAccount(); }, timeout); } }; if (!this._api.isConnected) { nextTimeout(500); return; } return this._api.parity .defaultAccount() .then(function (defaultAccount) { _this.updateDefaultAccount(defaultAccount); nextTimeout(); }) .catch(function () { return nextTimeout(); }); }; Personal.prototype._listAccounts = function () { var _this = this; return this._api.eth .accounts() .then(function (accounts) { _this._updateSubscriptions('eth_accounts', null, accounts); }); }; Personal.prototype._accountsInfo = function () { var _this = this; return this._api.parity .accountsInfo() .then(function (info) { _this._updateSubscriptions('parity_accountsInfo', null, info); return _this._api.parity .allAccountsInfo() .catch(function () { // NOTE: This fails on non-secure APIs, swallow error return {}; }) .then(function (allInfo) { _this._updateSubscriptions('parity_allAccountsInfo', null, allInfo); }); }); }; Personal.prototype._loggingSubscribe = function () { var _this = this; return this._subscriber.subscribe('logging', function (error, data) { if (error || !data) { return; } switch (data.method) { case 'parity_closeVault': case 'parity_openVault': case 'parity_killAccount': case 'parity_importGethAccounts': case 'parity_newAccountFromPhrase': case 'parity_newAccountFromWallet': case 'personal_newAccount': _this._defaultAccount(true); _this._listAccounts(); _this._accountsInfo(); break; case 'parity_removeAddress': case 'parity_setAccountName': case 'parity_setAccountMeta': _this._accountsInfo(); break; case 'parity_setDappAddresses': case 'parity_setDappDefaultAddress': case 'parity_setNewDappsAddresses': case 'parity_setNewDappsDefaultAddress': _this._defaultAccount(true); _this._listAccounts(); break; } }); }; return Personal; }()); module.exports = Personal;