2019-01-07 11:33:07 +01:00
|
|
|
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of Parity Ethereum.
|
2017-02-20 16:13:21 +01:00
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is free software: you can redistribute it and/or modify
|
2017-02-20 16:13:21 +01:00
|
|
|
// 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.
|
|
|
|
|
2019-01-07 11:33:07 +01:00
|
|
|
// Parity Ethereum is distributed in the hope that it will be useful,
|
2017-02-20 16:13:21 +01:00
|
|
|
// 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
|
2019-01-07 11:33:07 +01:00
|
|
|
// along with Parity Ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2017-02-20 16:13:21 +01:00
|
|
|
|
2017-04-03 11:13:51 +02:00
|
|
|
extern crate byteorder;
|
2019-01-04 14:05:46 +01:00
|
|
|
extern crate common_types;
|
2018-02-09 09:32:06 +01:00
|
|
|
extern crate ethabi;
|
|
|
|
extern crate ethcore;
|
2019-01-17 16:43:08 +01:00
|
|
|
extern crate ethcore_call_contract as call_contract;
|
2018-04-10 12:13:49 +02:00
|
|
|
extern crate ethcore_sync as sync;
|
2018-02-09 09:32:06 +01:00
|
|
|
extern crate ethereum_types;
|
|
|
|
extern crate ethkey;
|
2017-02-20 16:13:21 +01:00
|
|
|
extern crate hyper;
|
2018-02-09 09:32:06 +01:00
|
|
|
extern crate keccak_hash as hash;
|
|
|
|
extern crate kvdb;
|
2019-01-04 14:05:46 +01:00
|
|
|
extern crate parity_bytes as bytes;
|
|
|
|
extern crate parity_crypto as crypto;
|
|
|
|
extern crate parity_runtime;
|
2017-02-20 16:13:21 +01:00
|
|
|
extern crate parking_lot;
|
2019-11-11 21:57:38 +01:00
|
|
|
extern crate percent_encoding;
|
2017-07-06 11:26:14 +02:00
|
|
|
extern crate rustc_hex;
|
2017-04-03 11:13:51 +02:00
|
|
|
extern crate serde;
|
|
|
|
extern crate serde_json;
|
2017-10-02 15:27:31 +02:00
|
|
|
extern crate tiny_keccak;
|
2018-04-10 19:25:27 +02:00
|
|
|
extern crate tokio;
|
2018-02-09 09:32:06 +01:00
|
|
|
extern crate tokio_io;
|
|
|
|
extern crate tokio_service;
|
2017-02-20 16:13:21 +01:00
|
|
|
extern crate url;
|
|
|
|
|
2018-02-09 09:32:06 +01:00
|
|
|
#[macro_use]
|
|
|
|
extern crate ethabi_derive;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate ethabi_contract;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate futures;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate serde_derive;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate lazy_static;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
2017-02-20 16:13:21 +01:00
|
|
|
|
2019-01-08 15:07:20 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
extern crate env_logger;
|
2018-04-13 21:14:53 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
extern crate kvdb_rocksdb;
|
|
|
|
|
2019-02-07 14:34:24 +01:00
|
|
|
#[cfg(feature = "accounts")]
|
|
|
|
extern crate ethcore_accounts as accounts;
|
|
|
|
|
2020-08-05 06:08:03 +02:00
|
|
|
mod helpers;
|
2017-03-13 12:54:56 +01:00
|
|
|
mod key_server_cluster;
|
2017-02-20 16:13:21 +01:00
|
|
|
mod types;
|
|
|
|
|
|
|
|
mod acl_storage;
|
|
|
|
mod key_server;
|
2017-07-19 10:35:17 +02:00
|
|
|
mod key_server_set;
|
2020-08-05 06:08:03 +02:00
|
|
|
mod key_storage;
|
2017-11-14 12:26:31 +01:00
|
|
|
mod listener;
|
2020-08-05 06:08:03 +02:00
|
|
|
mod node_key_pair;
|
|
|
|
mod serialization;
|
|
|
|
mod traits;
|
2017-12-20 14:02:21 +01:00
|
|
|
mod trusted_client;
|
2017-02-20 16:13:21 +01:00
|
|
|
|
2020-08-05 06:08:03 +02:00
|
|
|
use ethcore::{client::Client, miner::Miner};
|
2018-04-13 21:14:53 +02:00
|
|
|
use kvdb::KeyValueDB;
|
2018-11-25 18:36:43 +01:00
|
|
|
use parity_runtime::Executor;
|
2020-08-05 06:08:03 +02:00
|
|
|
use std::sync::Arc;
|
|
|
|
use sync::SyncProvider;
|
2017-04-03 17:46:51 +02:00
|
|
|
|
2019-02-07 14:34:24 +01:00
|
|
|
#[cfg(feature = "accounts")]
|
|
|
|
pub use self::node_key_pair::KeyStoreNodeKeyPair;
|
2020-08-05 06:08:03 +02:00
|
|
|
pub use self::node_key_pair::PlainNodeKeyPair;
|
|
|
|
pub use traits::{KeyServer, NodeKeyPair};
|
|
|
|
pub use types::{
|
|
|
|
ClusterConfiguration, ContractAddress, EncryptedDocumentKey, Error, NodeAddress, Public,
|
|
|
|
RequestSignature, ServerKeyId, ServiceConfiguration,
|
|
|
|
};
|
2017-02-20 16:13:21 +01:00
|
|
|
|
|
|
|
/// Start new key server instance
|
2020-08-05 06:08:03 +02:00
|
|
|
pub fn start(
|
|
|
|
client: Arc<Client>,
|
2020-07-29 10:36:15 +02:00
|
|
|
sync: Arc<dyn SyncProvider>,
|
2020-08-05 06:08:03 +02:00
|
|
|
miner: Arc<Miner>,
|
2020-07-29 10:36:15 +02:00
|
|
|
self_key_pair: Arc<dyn NodeKeyPair>,
|
2020-08-05 06:08:03 +02:00
|
|
|
mut config: ServiceConfiguration,
|
2020-07-29 10:36:15 +02:00
|
|
|
db: Arc<dyn KeyValueDB>,
|
2020-08-05 06:08:03 +02:00
|
|
|
executor: Executor,
|
2020-07-29 10:36:15 +02:00
|
|
|
) -> Result<Box<dyn KeyServer>, Error> {
|
2020-08-05 06:08:03 +02:00
|
|
|
let trusted_client =
|
|
|
|
trusted_client::TrustedClient::new(self_key_pair.clone(), client.clone(), sync, miner);
|
2020-07-29 10:36:15 +02:00
|
|
|
let acl_storage: Arc<dyn acl_storage::AclStorage> =
|
|
|
|
match config.acl_check_contract_address.take() {
|
|
|
|
Some(acl_check_contract_address) => acl_storage::OnChainAclStorage::new(
|
|
|
|
trusted_client.clone(),
|
|
|
|
acl_check_contract_address,
|
|
|
|
)?,
|
|
|
|
None => Arc::new(acl_storage::DummyAclStorage::default()),
|
|
|
|
};
|
2020-08-05 06:08:03 +02:00
|
|
|
|
|
|
|
let key_server_set = key_server_set::OnChainKeyServerSet::new(
|
|
|
|
trusted_client.clone(),
|
|
|
|
config.cluster_config.key_server_set_contract_address.take(),
|
|
|
|
self_key_pair.clone(),
|
|
|
|
config.cluster_config.auto_migrate_enabled,
|
|
|
|
config.cluster_config.nodes.clone(),
|
|
|
|
)?;
|
|
|
|
let key_storage = Arc::new(key_storage::PersistentKeyStorage::new(db)?);
|
|
|
|
let key_server = Arc::new(key_server::KeyServerImpl::new(
|
|
|
|
&config.cluster_config,
|
|
|
|
key_server_set.clone(),
|
|
|
|
self_key_pair.clone(),
|
|
|
|
acl_storage.clone(),
|
|
|
|
key_storage.clone(),
|
|
|
|
executor.clone(),
|
|
|
|
)?);
|
|
|
|
let cluster = key_server.cluster();
|
2020-07-29 10:36:15 +02:00
|
|
|
let key_server: Arc<dyn KeyServer> = key_server;
|
2020-08-05 06:08:03 +02:00
|
|
|
|
|
|
|
// prepare HTTP listener
|
|
|
|
let http_listener = match config.listener_address {
|
|
|
|
Some(listener_address) => Some(listener::http_listener::KeyServerHttpListener::start(
|
|
|
|
listener_address,
|
|
|
|
Arc::downgrade(&key_server),
|
|
|
|
executor,
|
|
|
|
)?),
|
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
|
|
|
|
// prepare service contract listeners
|
|
|
|
let create_service_contract = |address, name, api_mask| {
|
|
|
|
Arc::new(listener::service_contract::OnChainServiceContract::new(
|
|
|
|
api_mask,
|
|
|
|
trusted_client.clone(),
|
|
|
|
name,
|
|
|
|
address,
|
|
|
|
self_key_pair.clone(),
|
|
|
|
))
|
|
|
|
};
|
|
|
|
|
2020-07-29 10:36:15 +02:00
|
|
|
let mut contracts: Vec<Arc<dyn listener::service_contract::ServiceContract>> = Vec::new();
|
2020-08-05 06:08:03 +02:00
|
|
|
config
|
|
|
|
.service_contract_address
|
|
|
|
.map(|address| {
|
|
|
|
create_service_contract(
|
|
|
|
address,
|
|
|
|
listener::service_contract::SERVICE_CONTRACT_REGISTRY_NAME.to_owned(),
|
|
|
|
listener::ApiMask::all(),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.map(|l| contracts.push(l));
|
|
|
|
config
|
|
|
|
.service_contract_srv_gen_address
|
|
|
|
.map(|address| {
|
|
|
|
create_service_contract(
|
|
|
|
address,
|
|
|
|
listener::service_contract::SRV_KEY_GEN_SERVICE_CONTRACT_REGISTRY_NAME.to_owned(),
|
|
|
|
listener::ApiMask {
|
|
|
|
server_key_generation_requests: true,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.map(|l| contracts.push(l));
|
|
|
|
config
|
|
|
|
.service_contract_srv_retr_address
|
|
|
|
.map(|address| {
|
|
|
|
create_service_contract(
|
|
|
|
address,
|
|
|
|
listener::service_contract::SRV_KEY_RETR_SERVICE_CONTRACT_REGISTRY_NAME.to_owned(),
|
|
|
|
listener::ApiMask {
|
|
|
|
server_key_retrieval_requests: true,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.map(|l| contracts.push(l));
|
|
|
|
config
|
|
|
|
.service_contract_doc_store_address
|
|
|
|
.map(|address| {
|
|
|
|
create_service_contract(
|
|
|
|
address,
|
|
|
|
listener::service_contract::DOC_KEY_STORE_SERVICE_CONTRACT_REGISTRY_NAME.to_owned(),
|
|
|
|
listener::ApiMask {
|
|
|
|
document_key_store_requests: true,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.map(|l| contracts.push(l));
|
|
|
|
config
|
|
|
|
.service_contract_doc_sretr_address
|
|
|
|
.map(|address| {
|
|
|
|
create_service_contract(
|
|
|
|
address,
|
|
|
|
listener::service_contract::DOC_KEY_SRETR_SERVICE_CONTRACT_REGISTRY_NAME.to_owned(),
|
|
|
|
listener::ApiMask {
|
|
|
|
document_key_shadow_retrieval_requests: true,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.map(|l| contracts.push(l));
|
|
|
|
|
2020-07-29 10:36:15 +02:00
|
|
|
let contract: Option<Arc<dyn listener::service_contract::ServiceContract>> = match contracts
|
|
|
|
.len()
|
|
|
|
{
|
2020-08-05 06:08:03 +02:00
|
|
|
0 => None,
|
|
|
|
1 => Some(contracts.pop().expect("contract.len() is 1; qed")),
|
|
|
|
_ => Some(Arc::new(
|
|
|
|
listener::service_contract_aggregate::OnChainServiceContractAggregate::new(contracts),
|
|
|
|
)),
|
|
|
|
};
|
|
|
|
|
|
|
|
let contract_listener = match contract {
|
|
|
|
Some(contract) => Some({
|
|
|
|
let listener = listener::service_contract_listener::ServiceContractListener::new(
|
|
|
|
listener::service_contract_listener::ServiceContractListenerParams {
|
|
|
|
contract: contract,
|
|
|
|
self_key_pair: self_key_pair.clone(),
|
|
|
|
key_server_set: key_server_set,
|
|
|
|
acl_storage: acl_storage,
|
|
|
|
cluster: cluster,
|
|
|
|
key_storage: key_storage,
|
|
|
|
},
|
|
|
|
)?;
|
|
|
|
client.add_notify(listener.clone());
|
|
|
|
listener
|
|
|
|
}),
|
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(Box::new(listener::Listener::new(
|
|
|
|
key_server,
|
|
|
|
http_listener,
|
|
|
|
contract_listener,
|
|
|
|
)))
|
2017-02-20 16:13:21 +01:00
|
|
|
}
|