2017-02-01 16:18:11 +01:00
|
|
|
// 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 { action, observable, transaction } from 'mobx';
|
|
|
|
|
|
|
|
export default class AccountStore {
|
|
|
|
@observable accounts = [];
|
|
|
|
@observable defaultAccount = null;
|
|
|
|
@observable isLoading = false;
|
|
|
|
|
|
|
|
constructor (api) {
|
|
|
|
this._api = api;
|
|
|
|
|
2017-09-10 18:03:35 +02:00
|
|
|
this.subscribeDefaultAccount()
|
2017-02-14 22:47:34 +01:00
|
|
|
.then(() => this.loadAccounts());
|
2017-02-01 16:18:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@action setAccounts = (accounts) => {
|
|
|
|
this.accounts = accounts;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action setDefaultAccount = (defaultAccount) => {
|
2017-02-14 22:47:34 +01:00
|
|
|
transaction(() => {
|
|
|
|
this.accounts = this.accounts.map((account) => {
|
2017-02-24 14:37:56 +01:00
|
|
|
account.checked = account.address === defaultAccount;
|
2017-02-14 22:47:34 +01:00
|
|
|
|
|
|
|
return account;
|
|
|
|
});
|
|
|
|
|
|
|
|
this.defaultAccount = defaultAccount;
|
|
|
|
});
|
2017-02-01 16:18:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@action setLoading = (isLoading) => {
|
|
|
|
this.isLoading = isLoading;
|
|
|
|
}
|
|
|
|
|
2017-02-22 10:43:02 +01:00
|
|
|
makeDefaultAccount = (defaultAddress) => {
|
|
|
|
this.setDefaultAccount(defaultAddress);
|
2017-02-14 22:47:34 +01:00
|
|
|
|
2017-02-01 16:18:11 +01:00
|
|
|
return this._api.parity
|
2017-02-22 10:43:02 +01:00
|
|
|
.setNewDappsDefaultAddress(defaultAddress)
|
2017-02-01 16:18:11 +01:00
|
|
|
.catch((error) => {
|
|
|
|
console.warn('makeDefaultAccount', error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
loadAccounts () {
|
|
|
|
this.setLoading(true);
|
|
|
|
|
|
|
|
return Promise
|
|
|
|
.all([
|
2017-02-20 16:33:12 +01:00
|
|
|
this._api.parity.getNewDappsAddresses(),
|
2017-02-01 16:18:11 +01:00
|
|
|
this._api.parity.allAccountsInfo()
|
|
|
|
])
|
2017-03-10 15:46:13 +01:00
|
|
|
.then(([whitelist, allAccounts]) => {
|
2017-02-01 16:18:11 +01:00
|
|
|
transaction(() => {
|
2017-03-10 15:46:13 +01:00
|
|
|
const accounts = Object
|
|
|
|
.keys(allAccounts)
|
|
|
|
.filter((address) => {
|
|
|
|
const account = allAccounts[address];
|
2017-03-31 23:36:24 +02:00
|
|
|
const isAccount = account.uuid;
|
|
|
|
const isExternal = account.meta && (account.meta.external || account.meta.hardware);
|
2017-03-10 15:46:13 +01:00
|
|
|
const isWhitelisted = !whitelist || whitelist.includes(address);
|
|
|
|
|
2017-03-31 23:36:24 +02:00
|
|
|
return (isAccount || isExternal) && isWhitelisted;
|
2017-03-10 15:46:13 +01:00
|
|
|
})
|
|
|
|
.map((address) => {
|
|
|
|
return {
|
|
|
|
...allAccounts[address],
|
|
|
|
checked: address === this.defaultAccount,
|
|
|
|
address
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2017-02-01 16:18:11 +01:00
|
|
|
this.setLoading(false);
|
2017-03-10 15:46:13 +01:00
|
|
|
this.setAccounts(accounts);
|
2017-02-01 16:18:11 +01:00
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
this.setLoading(false);
|
|
|
|
console.warn('loadAccounts', error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
subscribeDefaultAccount () {
|
2017-02-16 17:42:19 +01:00
|
|
|
const promiseDefaultAccount = this._api.subscribe('parity_defaultAccount', (error, defaultAccount) => {
|
2017-02-01 16:18:11 +01:00
|
|
|
if (!error) {
|
|
|
|
this.setDefaultAccount(defaultAccount);
|
|
|
|
}
|
|
|
|
});
|
2017-02-16 17:42:19 +01:00
|
|
|
|
|
|
|
const promiseEthAccounts = this._api.subscribe('eth_accounts', (error) => {
|
|
|
|
if (!error) {
|
|
|
|
this.loadAccounts();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-03-10 15:46:13 +01:00
|
|
|
const promiseAccountsInfo = this._api
|
|
|
|
.subscribe('parity_allAccountsInfo', (error, accountsInfo) => {
|
|
|
|
if (!error) {
|
|
|
|
this.loadAccounts();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return Promise.all([ promiseDefaultAccount, promiseEthAccounts, promiseAccountsInfo ]);
|
2017-02-01 16:18:11 +01:00
|
|
|
}
|
|
|
|
}
|