Integrate old UI.

This commit is contained in:
Tomasz Drwięga
2017-09-05 11:21:18 +02:00
parent 9a62119a82
commit 0f8fb62581
1881 changed files with 175345 additions and 925 deletions

View File

@@ -0,0 +1,63 @@
// Copyright 2015-2017 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/>.
import BigNumber from 'bignumber.js';
export default class Eth {
constructor (updateSubscriptions, api) {
this._api = api;
this._updateSubscriptions = updateSubscriptions;
this._started = false;
this._lastBlock = new BigNumber(-1);
this._pollTimerId = null;
}
get isStarted () {
return this._started;
}
start () {
this._started = true;
return this._blockNumber();
}
_blockNumber = () => {
const nextTimeout = (timeout = 1000) => {
this._pollTimerId = setTimeout(() => {
this._blockNumber();
}, timeout);
};
if (!this._api.transport.isConnected) {
nextTimeout(500);
return;
}
return this._api.eth
.blockNumber()
.then((blockNumber) => {
if (!blockNumber.eq(this._lastBlock)) {
this._lastBlock = blockNumber;
this._updateSubscriptions('eth_blockNumber', null, blockNumber);
}
nextTimeout();
})
.catch(() => nextTimeout());
}
}

View File

@@ -0,0 +1,101 @@
// Copyright 2015-2017 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/>.
import BigNumber from 'bignumber.js';
import sinon from 'sinon';
import Eth from './eth';
const START_BLOCK = 5000;
function stubApi (blockNumber) {
const _calls = {
blockNumber: []
};
return {
_calls,
transport: {
isConnected: true
},
eth: {
blockNumber: () => {
const stub = sinon.stub().resolves(new BigNumber(blockNumber || START_BLOCK))();
_calls.blockNumber.push(stub);
return stub;
}
}
};
}
describe('api/subscriptions/eth', () => {
let api;
let eth;
let cb;
beforeEach(() => {
api = stubApi();
cb = sinon.stub();
eth = new Eth(cb, api);
});
describe('constructor', () => {
it('starts the instance in a stopped state', () => {
expect(eth.isStarted).to.be.false;
});
});
describe('start', () => {
describe('blockNumber available', () => {
beforeEach(() => {
return eth.start();
});
it('sets the started status', () => {
expect(eth.isStarted).to.be.true;
});
it('calls eth_blockNumber', () => {
expect(api._calls.blockNumber.length).to.be.ok;
});
it('updates subscribers', () => {
expect(cb).to.have.been.calledWith('eth_blockNumber', null, new BigNumber(START_BLOCK));
});
});
describe('blockNumber not available', () => {
beforeEach(() => {
api = stubApi(-1);
eth = new Eth(cb, api);
return eth.start();
});
it('sets the started status', () => {
expect(eth.isStarted).to.be.true;
});
it('calls eth_blockNumber', () => {
expect(api._calls.blockNumber.length).to.be.ok;
});
it('does not update subscribers', () => {
expect(cb).not.to.been.called;
});
});
});
});

View File

@@ -0,0 +1,19 @@
// Copyright 2015-2017 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/>.
export Logging from './logging';
export default from './manager';

View File

@@ -0,0 +1,44 @@
// Copyright 2015-2017 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/>.
let instance = null;
export default class Logging {
constructor (updateSubscriptions) {
this._updateSubscriptions = updateSubscriptions;
instance = this;
}
get isStarted () {
return true;
}
start () {
}
static send (method, params, json) {
if (!instance) {
return;
}
return instance._updateSubscriptions('logging', null, {
method,
params,
json
});
}
}

View File

@@ -0,0 +1,49 @@
// Copyright 2015-2017 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/>.
import sinon from 'sinon';
import Logging from './logging';
describe('api/subscriptions/logging', () => {
let cb;
let logging;
beforeEach(() => {
cb = sinon.stub();
logging = new Logging(cb);
});
describe('constructor', () => {
it('starts the instance in a started state', () => {
expect(logging.isStarted).to.be.true;
});
});
describe('send', () => {
const method = 'method';
const params = 'params';
const json = 'json';
beforeEach(() => {
Logging.send(method, params, json);
});
it('calls the subscription update', () => {
expect(cb).to.have.been.calledWith('logging', null, { method, params, json });
});
});
});

View File

@@ -0,0 +1,137 @@
// Copyright 2015-2017 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/>.
import { isError } from '../util/types';
import Eth from './eth';
import Logging from './logging';
import Personal from './personal';
import Signer from './signer';
const events = {
'logging': { module: 'logging' },
'eth_blockNumber': { module: 'eth' },
'parity_accountsInfo': { module: 'personal' },
'parity_allAccountsInfo': { module: 'personal' },
'parity_defaultAccount': { module: 'personal' },
'parity_postTransaction': { module: 'signer' },
'eth_accounts': { module: 'personal' },
'signer_requestsToConfirm': { module: 'signer' }
};
export default class Manager {
constructor (api) {
this._api = api;
this.subscriptions = [];
this.values = {};
Object.keys(events).forEach((subscriptionName) => {
this.values[subscriptionName] = {
error: null,
data: null
};
});
this._logging = new Logging(this._updateSubscriptions);
this._eth = new Eth(this._updateSubscriptions, api);
this._personal = new Personal(this._updateSubscriptions, api, this);
this._signer = new Signer(this._updateSubscriptions, api, this);
}
_validateType (subscriptionName) {
const subscription = events[subscriptionName];
if (!subscription) {
return new Error(`${subscriptionName} is not a valid interface, subscribe using one of ${Object.keys(events).join(', ')}`);
}
return subscription;
}
subscribe (subscriptionName, callback, autoRemove = false) {
return new Promise((resolve, reject) => {
const subscription = this._validateType(subscriptionName);
if (isError(subscription)) {
reject(subscription);
return;
}
const subscriptionId = this.subscriptions.length;
const { error, data } = this.values[subscriptionName];
const engine = this[`_${subscription.module}`];
this.subscriptions[subscriptionId] = {
name: subscriptionName,
id: subscriptionId,
autoRemove,
callback
};
if (!engine.isStarted) {
engine.start();
} else if (error !== null || data !== null) {
this._sendData(subscriptionId, error, data);
}
resolve(subscriptionId);
});
}
unsubscribe (subscriptionId) {
return new Promise((resolve, reject) => {
if (!this.subscriptions[subscriptionId]) {
reject(new Error(`Cannot find subscription ${subscriptionId}`));
return;
}
delete this.subscriptions[subscriptionId];
resolve();
});
}
_sendData (subscriptionId, error, data) {
const { autoRemove, callback } = this.subscriptions[subscriptionId];
let result = true;
try {
result = callback(error, data);
} catch (error) {
console.error(`Unable to update callback for subscriptionId ${subscriptionId}`, error);
}
if (autoRemove && result && typeof result === 'boolean') {
this.unsubscribe(subscriptionId);
}
}
_updateSubscriptions = (subscriptionName, error, data) => {
const subscriptions = this.subscriptions
.filter(subscription => subscription.name === subscriptionName);
this.values[subscriptionName] = { error, data };
subscriptions
.forEach((subscription) => {
this._sendData(subscription.id, error, data);
});
}
}
export {
events
};

View File

@@ -0,0 +1,134 @@
// Copyright 2015-2017 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/>.
import sinon from 'sinon';
import Manager, { events } from './manager';
function newStub () {
const start = () => manager._updateSubscriptions(manager.__test, null, 'test');
const manager = new Manager({
transport: {
isConnected: true
}
});
manager._eth = {
isStarted: false,
start
};
manager._personal = {
isStarted: false,
start
};
manager._signer = {
isStarted: false,
start
};
return manager;
}
describe('api/subscriptions/manager', () => {
let manager;
beforeEach(() => {
manager = newStub();
});
describe('constructor', () => {
it('sets up the subscription types & defaults', () => {
expect(manager.subscriptions).to.be.an.array;
expect(Object.keys(manager.values)).to.deep.equal(Object.keys(events));
});
});
describe('subscriptions', () => {
Object
.keys(events)
.filter((eventName) => eventName.indexOf('_') !== -1)
.forEach((eventName) => {
const { module } = events[eventName];
let engine;
let cb;
let subscriptionId;
describe(eventName, () => {
beforeEach(() => {
engine = manager[`_${module}`];
manager.__test = eventName;
cb = sinon.stub();
sinon.spy(engine, 'start');
return manager
.subscribe(eventName, cb)
.then((_subscriptionId) => {
subscriptionId = _subscriptionId;
});
});
it(`puts the ${module} engine in a started state`, () => {
expect(engine.start).to.have.been.called;
});
it('returns a subscriptionId', () => {
expect(subscriptionId).to.be.a.number;
});
it('calls the subscription callback with updated values', () => {
expect(cb).to.have.been.calledWith(null, 'test');
});
});
});
});
describe('unsubscriptions', () => {
Object
.keys(events)
.filter((eventName) => eventName.indexOf('_') !== -1)
.forEach((eventName) => {
const { module } = events[eventName];
let engine;
let cb;
describe(eventName, () => {
beforeEach(() => {
engine = manager[`_${module}`];
manager.__test = eventName;
cb = sinon.stub();
sinon.spy(engine, 'start');
return manager
.subscribe(eventName, cb)
.then((_subscriptionId) => {
manager.unsubscribe(_subscriptionId);
})
.then(() => {
manager._updateSubscriptions(manager.__test, null, 'test2');
});
});
it('does not call the callback after unsubscription', () => {
expect(cb).to.have.been.calledWith(null, 'test');
expect(cb).to.not.have.been.calledWith(null, 'test2');
});
});
});
});
});

View File

@@ -0,0 +1,135 @@
// Copyright 2015-2017 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/>.
export default class Personal {
constructor (updateSubscriptions, api, subscriber) {
this._subscriber = subscriber;
this._api = api;
this._updateSubscriptions = updateSubscriptions;
this._started = false;
this._lastDefaultAccount = '0x0';
this._pollTimerId = null;
}
get isStarted () {
return this._started;
}
start () {
this._started = true;
return Promise.all([
this._defaultAccount(),
this._listAccounts(),
this._accountsInfo(),
this._loggingSubscribe()
]);
}
// 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
_defaultAccount = (timerDisabled = false) => {
const nextTimeout = (timeout = 1000) => {
if (!timerDisabled) {
this._pollTimerId = setTimeout(() => {
this._defaultAccount();
}, timeout);
}
};
if (!this._api.transport.isConnected) {
nextTimeout(500);
return;
}
return this._api.parity
.defaultAccount()
.then((defaultAccount) => {
if (this._lastDefaultAccount !== defaultAccount) {
this._lastDefaultAccount = defaultAccount;
this._updateSubscriptions('parity_defaultAccount', null, defaultAccount);
}
nextTimeout();
})
.catch(() => nextTimeout());
}
_listAccounts = () => {
return this._api.eth
.accounts()
.then((accounts) => {
this._updateSubscriptions('eth_accounts', null, accounts);
});
}
_accountsInfo = () => {
return this._api.parity
.accountsInfo()
.then((info) => {
this._updateSubscriptions('parity_accountsInfo', null, info);
return this._api.parity
.allAccountsInfo()
.catch(() => {
// NOTE: This fails on non-secure APIs, swallow error
return {};
})
.then((allInfo) => {
this._updateSubscriptions('parity_allAccountsInfo', null, allInfo);
});
});
}
_loggingSubscribe () {
return this._subscriber.subscribe('logging', (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();
return;
case 'parity_removeAddress':
case 'parity_setAccountName':
case 'parity_setAccountMeta':
this._accountsInfo();
return;
case 'parity_setDappAddresses':
case 'parity_setDappDefaultAddress':
case 'parity_setNewDappsAddresses':
case 'parity_setNewDappsDefaultAddress':
this._defaultAccount(true);
this._listAccounts();
return;
}
});
}
}

View File

@@ -0,0 +1,156 @@
// Copyright 2015-2017 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/>.
import sinon from 'sinon';
import Personal from './personal';
const TEST_DEFAULT = '0xfa64203C044691aA57251aF95f4b48d85eC00Dd5';
const TEST_INFO = {
[TEST_DEFAULT]: {
name: 'test'
}
};
const TEST_LIST = [TEST_DEFAULT];
function stubApi (accounts, info) {
const _calls = {
accountsInfo: [],
allAccountsInfo: [],
listAccounts: [],
defaultAccount: []
};
return {
_calls,
transport: {
isConnected: true
},
parity: {
accountsInfo: () => {
const stub = sinon.stub().resolves(info || TEST_INFO)();
_calls.accountsInfo.push(stub);
return stub;
},
allAccountsInfo: () => {
const stub = sinon.stub().resolves(info || TEST_INFO)();
_calls.allAccountsInfo.push(stub);
return stub;
},
defaultAccount: () => {
const stub = sinon.stub().resolves(Object.keys(info || TEST_INFO)[0])();
_calls.defaultAccount.push(stub);
return stub;
}
},
eth: {
accounts: () => {
const stub = sinon.stub().resolves(accounts || TEST_LIST)();
_calls.listAccounts.push(stub);
return stub;
}
}
};
}
function stubLogging () {
return {
subscribe: sinon.stub()
};
}
describe('api/subscriptions/personal', () => {
let api;
let cb;
let logging;
let personal;
beforeEach(() => {
api = stubApi();
cb = sinon.stub();
logging = stubLogging();
personal = new Personal(cb, api, logging);
});
describe('constructor', () => {
it('starts the instance in a stopped state', () => {
expect(personal.isStarted).to.be.false;
});
});
describe('start', () => {
describe('info available', () => {
beforeEach(() => {
return personal.start();
});
it('sets the started status', () => {
expect(personal.isStarted).to.be.true;
});
it('calls parity_accountsInfo', () => {
expect(api._calls.accountsInfo.length).to.be.ok;
});
it('calls parity_allAccountsInfo', () => {
expect(api._calls.allAccountsInfo.length).to.be.ok;
});
it('calls eth_accounts', () => {
expect(api._calls.listAccounts.length).to.be.ok;
});
it('updates subscribers', () => {
expect(cb).to.have.been.calledWith('parity_defaultAccount', null, TEST_DEFAULT);
expect(cb).to.have.been.calledWith('eth_accounts', null, TEST_LIST);
expect(cb).to.have.been.calledWith('parity_accountsInfo', null, TEST_INFO);
expect(cb).to.have.been.calledWith('parity_allAccountsInfo', null, TEST_INFO);
});
});
describe('info not available', () => {
beforeEach(() => {
api = stubApi([], {});
personal = new Personal(cb, api, logging);
return personal.start();
});
it('sets the started status', () => {
expect(personal.isStarted).to.be.true;
});
it('calls parity_defaultAccount', () => {
expect(api._calls.defaultAccount.length).to.be.ok;
});
it('calls personal_accountsInfo', () => {
expect(api._calls.accountsInfo.length).to.be.ok;
});
it('calls personal_allAccountsInfo', () => {
expect(api._calls.allAccountsInfo.length).to.be.ok;
});
it('calls personal_listAccounts', () => {
expect(api._calls.listAccounts.length).to.be.ok;
});
});
});
});

View File

@@ -0,0 +1,91 @@
// Copyright 2015-2017 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/>.
import { outTransaction } from '../format/output';
export default class Signer {
constructor (updateSubscriptions, api, subscriber) {
this._subscriber = subscriber;
this._api = api;
this._updateSubscriptions = updateSubscriptions;
this._started = false;
}
get isStarted () {
return this._started;
}
start () {
this._started = true;
return Promise.all([
this._listRequests(true),
this._loggingSubscribe()
]);
}
_listRequests = (doTimeout) => {
const nextTimeout = (timeout = 1000) => {
if (doTimeout) {
setTimeout(() => {
this._listRequests(true);
}, timeout);
}
};
if (!this._api.transport.isConnected) {
nextTimeout(500);
return;
}
return this._api.signer
.requestsToConfirm()
.then((requests) => {
this._updateSubscriptions('signer_requestsToConfirm', null, requests);
nextTimeout();
})
.catch(() => nextTimeout());
}
_postTransaction (data) {
const request = {
transaction: outTransaction(data.params[0]),
requestId: data.json.result.result
};
this._updateSubscriptions('parity_postTransaction', null, request);
}
_loggingSubscribe () {
return this._subscriber.subscribe('logging', (error, data) => {
if (error || !data) {
return;
}
switch (data.method) {
case 'eth_sendTransaction':
case 'eth_sendRawTransaction':
this._listRequests(false);
return;
case 'parity_postTransaction':
this._postTransaction(data);
this._listRequests(false);
return;
}
});
}
}