2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-12-27 16:23:41 +01:00
|
|
|
// 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 Store {
|
|
|
|
@observable accounts = [];
|
|
|
|
@observable modalOpen = false;
|
|
|
|
@observable whitelist = [];
|
2017-02-22 10:43:02 +01:00
|
|
|
@observable whitelistDefault = null;
|
2016-12-27 16:23:41 +01:00
|
|
|
|
|
|
|
constructor (api) {
|
|
|
|
this._api = api;
|
|
|
|
|
2017-07-10 17:03:16 +02:00
|
|
|
this.load();
|
2016-12-27 16:23:41 +01:00
|
|
|
}
|
|
|
|
|
2017-07-10 17:03:16 +02:00
|
|
|
save = () => {
|
|
|
|
const checkedAccounts = this.accounts.filter((account) => account.checked);
|
|
|
|
const defaultAddress = (this.accounts.find((account) => account.default) || {}).address;
|
|
|
|
const addresses = checkedAccounts.length === this.accounts.length
|
|
|
|
? null
|
|
|
|
: checkedAccounts.map((account) => account.address);
|
|
|
|
|
|
|
|
this.updateWhitelist(addresses, defaultAddress);
|
2016-12-27 16:23:41 +01:00
|
|
|
}
|
|
|
|
|
2017-07-10 17:03:16 +02:00
|
|
|
// FIXME: Hardware accounts are not showing up here
|
|
|
|
@action setAccounts = (accounts) => {
|
2016-12-27 16:23:41 +01:00
|
|
|
transaction(() => {
|
|
|
|
this.accounts = Object
|
2017-07-10 17:03:16 +02:00
|
|
|
.keys(accounts)
|
|
|
|
.filter((address) => {
|
|
|
|
const account = accounts[address];
|
|
|
|
|
|
|
|
if (account.uuid) {
|
|
|
|
return true;
|
|
|
|
} else if (account.meta.hardware) {
|
|
|
|
account.hardware = true;
|
|
|
|
return true;
|
|
|
|
} else if (account.meta.external) {
|
|
|
|
account.external = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
})
|
|
|
|
.map((address, index) => {
|
|
|
|
const account = accounts[address];
|
|
|
|
|
2016-12-27 16:23:41 +01:00
|
|
|
return {
|
2017-07-10 17:03:16 +02:00
|
|
|
address,
|
2016-12-27 16:23:41 +01:00
|
|
|
checked: this.whitelist
|
2017-07-10 17:03:16 +02:00
|
|
|
? this.whitelist.includes(address)
|
2016-12-27 16:23:41 +01:00
|
|
|
: true,
|
2017-02-22 10:43:02 +01:00
|
|
|
default: this.whitelistDefault
|
2017-07-10 17:03:16 +02:00
|
|
|
? this.whitelistDefault === address
|
2017-01-31 17:04:41 +01:00
|
|
|
: index === 0,
|
2016-12-27 16:23:41 +01:00
|
|
|
description: account.meta.description,
|
|
|
|
name: account.name
|
|
|
|
};
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@action selectAccount = (address) => {
|
2017-01-31 17:04:41 +01:00
|
|
|
transaction(() => {
|
2017-02-22 10:43:02 +01:00
|
|
|
const isSingleAccount = this.accounts.filter((account) => account.checked).length === 1;
|
|
|
|
|
2017-01-31 17:04:41 +01:00
|
|
|
this.accounts = this.accounts.map((account) => {
|
2017-02-22 10:43:02 +01:00
|
|
|
if (account.address === address && (!isSingleAccount || !account.checked)) {
|
2017-01-31 17:04:41 +01:00
|
|
|
account.checked = !account.checked;
|
|
|
|
account.default = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return account;
|
|
|
|
});
|
|
|
|
|
|
|
|
this.setDefaultAccount((
|
|
|
|
this.accounts.find((account) => account.default) ||
|
|
|
|
this.accounts.find((account) => account.checked) ||
|
|
|
|
{}
|
|
|
|
).address);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@action setDefaultAccount = (address) => {
|
2016-12-27 16:23:41 +01:00
|
|
|
this.accounts = this.accounts.map((account) => {
|
|
|
|
if (account.address === address) {
|
2017-01-31 17:04:41 +01:00
|
|
|
account.checked = true;
|
|
|
|
account.default = true;
|
|
|
|
} else if (account.default) {
|
|
|
|
account.default = false;
|
2016-12-27 16:23:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return account;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-02-22 10:43:02 +01:00
|
|
|
@action setWhitelist = (whitelist, whitelistDefault) => {
|
|
|
|
transaction(() => {
|
|
|
|
this.whitelist = whitelist;
|
|
|
|
this.whitelistDefault = whitelistDefault;
|
|
|
|
});
|
2016-12-27 16:23:41 +01:00
|
|
|
}
|
|
|
|
|
2017-07-10 17:03:16 +02:00
|
|
|
load () {
|
2017-02-22 10:43:02 +01:00
|
|
|
return Promise
|
|
|
|
.all([
|
2017-07-10 17:03:16 +02:00
|
|
|
this._api.parity.allAccountsInfo(),
|
2017-02-22 10:43:02 +01:00
|
|
|
this._api.parity.getNewDappsAddresses(),
|
|
|
|
this._api.parity.getNewDappsDefaultAddress()
|
|
|
|
])
|
2017-07-10 17:03:16 +02:00
|
|
|
.then(([accounts, whitelist, whitelistDefault]) => {
|
2017-02-22 10:43:02 +01:00
|
|
|
this.setWhitelist(whitelist, whitelistDefault);
|
2017-07-10 17:03:16 +02:00
|
|
|
this.setAccounts(accounts);
|
2016-12-27 16:23:41 +01:00
|
|
|
})
|
|
|
|
.catch((error) => {
|
2017-07-10 17:03:16 +02:00
|
|
|
console.warn('load', error);
|
2016-12-27 16:23:41 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-02-22 10:43:02 +01:00
|
|
|
updateWhitelist (whitelist, whitelistDefault = null) {
|
|
|
|
return Promise
|
|
|
|
.all([
|
|
|
|
this._api.parity.setNewDappsAddresses(whitelist),
|
|
|
|
this._api.parity.setNewDappsDefaultAddress(whitelistDefault)
|
|
|
|
])
|
2016-12-27 16:23:41 +01:00
|
|
|
.then(() => {
|
2017-02-22 10:43:02 +01:00
|
|
|
this.setWhitelist(whitelist, whitelistDefault);
|
2016-12-27 16:23:41 +01:00
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.warn('updateWhitelist', error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|