Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

RPC for dapps port and signer port #2819

Merged
merged 1 commit into from
Oct 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion parity/dapps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub fn new(configuration: Configuration, deps: Dependencies) -> Result<Option<We
return Ok(None);
}

let signer_port = deps.apis.signer_port.clone();
let signer_port = deps.apis.signer_service.port();
let url = format!("{}:{}", configuration.interface, configuration.port);
let addr = try!(url.parse().map_err(|_| format!("Invalid Webapps listen host/port given: {}", url)));

Expand Down
22 changes: 17 additions & 5 deletions parity/rpc_apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ impl FromStr for ApiSet {
}

pub struct Dependencies {
pub signer_port: Option<u16>,
pub signer_service: Arc<SignerService>,
pub client: Arc<Client>,
pub sync: Arc<SyncProvider>,
Expand All @@ -105,6 +104,7 @@ pub struct Dependencies {
pub settings: Arc<NetworkSettings>,
pub net_service: Arc<ManageNetwork>,
pub geth_compatibility: bool,
pub dapps_port: Option<u16>,
}

fn to_modules(apis: &[Api]) -> BTreeMap<String, String> {
Expand Down Expand Up @@ -172,21 +172,33 @@ pub fn setup_rpc<T: Extendable>(server: T, deps: Arc<Dependencies>, apis: ApiSet
let filter_client = EthFilterClient::new(&deps.client, &deps.miner);
server.add_delegate(filter_client.to_delegate());

if deps.signer_port.is_some() {
if deps.signer_service.is_enabled() {
server.add_delegate(EthSigningQueueClient::new(&deps.signer_service, &deps.client, &deps.miner, &deps.secret_store).to_delegate());
} else {
server.add_delegate(EthSigningUnsafeClient::new(&deps.client, &deps.secret_store, &deps.miner).to_delegate());
}
},
Api::Personal => {
server.add_delegate(PersonalClient::new(&deps.secret_store, &deps.client, &deps.miner, deps.signer_port, deps.geth_compatibility).to_delegate());
server.add_delegate(PersonalClient::new(&deps.secret_store, &deps.client, &deps.miner, deps.geth_compatibility).to_delegate());
},
Api::Signer => {
server.add_delegate(SignerClient::new(&deps.secret_store, &deps.client, &deps.miner, &deps.signer_service).to_delegate());
},
Api::Ethcore => {
let signer = deps.signer_port.map(|_| deps.signer_service.clone());
server.add_delegate(EthcoreClient::new(&deps.client, &deps.miner, &deps.sync, &deps.net_service, deps.logger.clone(), deps.settings.clone(), signer).to_delegate())
let signer = match deps.signer_service.is_enabled() {
true => Some(deps.signer_service.clone()),
false => None,
};
server.add_delegate(EthcoreClient::new(
&deps.client,
&deps.miner,
&deps.sync,
&deps.net_service,
deps.logger.clone(),
deps.settings.clone(),
signer,
deps.dapps_port,
).to_delegate())
},
Api::EthcoreSet => {
server.add_delegate(EthcoreSetClient::new(&deps.client, &deps.miner, &deps.net_service).to_delegate())
Expand Down
7 changes: 5 additions & 2 deletions parity/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,9 @@ pub fn execute(cmd: RunCmd) -> Result<(), String> {
// set up dependencies for rpc servers
let signer_path = cmd.signer_conf.signer_path.clone();
let deps_for_rpc_apis = Arc::new(rpc_apis::Dependencies {
signer_port: cmd.signer_port,
signer_service: Arc::new(rpc_apis::SignerService::new(move || {
signer::generate_new_token(signer_path.clone()).map_err(|e| format!("{:?}", e))
})),
}, cmd.signer_port)),
client: client.clone(),
sync: sync_provider.clone(),
net: manage_network.clone(),
Expand All @@ -260,6 +259,10 @@ pub fn execute(cmd: RunCmd) -> Result<(), String> {
settings: Arc::new(cmd.net_settings.clone()),
net_service: manage_network.clone(),
geth_compatibility: cmd.geth_compatibility,
dapps_port: match cmd.dapps_conf.enabled {
true => Some(cmd.dapps_conf.port),
false => None,
},
});

let dependencies = rpc::Dependencies {
Expand Down
9 changes: 9 additions & 0 deletions rpc/src/v1/helpers/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ mod codes {
pub const PASSWORD_INVALID: i64 = -32021;
pub const ACCOUNT_ERROR: i64 = -32023;
pub const SIGNER_DISABLED: i64 = -32030;
pub const DAPPS_DISABLED: i64 = -32031;
pub const REQUEST_REJECTED: i64 = -32040;
pub const REQUEST_REJECTED_LIMIT: i64 = -32041;
pub const REQUEST_NOT_FOUND: i64 = -32042;
Expand Down Expand Up @@ -167,6 +168,14 @@ pub fn signer_disabled() -> Error {
}
}

pub fn dapps_disabled() -> Error {
Error {
code: ErrorCode::ServerError(codes::DAPPS_DISABLED),
message: "Dapps Server is disabled. This API is not available.".into(),
data: None
}
}

pub fn encryption_error<T: fmt::Debug>(error: T) -> Error {
Error {
code: ErrorCode::ServerError(codes::ENCRYPTION_ERROR),
Expand Down
18 changes: 15 additions & 3 deletions rpc/src/v1/helpers/signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@ use v1::helpers::signing_queue::{ConfirmationsQueue};
pub struct SignerService {
queue: Arc<ConfirmationsQueue>,
generate_new_token: Box<Fn() -> Result<String, String> + Send + Sync + 'static>,
port: Option<u16>,
}

impl SignerService {

/// Creates new Signer Service given function to generate new tokens.
pub fn new<F>(new_token: F) -> Self
pub fn new<F>(new_token: F, port: Option<u16>) -> Self
where F: Fn() -> Result<String, String> + Send + Sync + 'static {
SignerService {
queue: Arc::new(ConfirmationsQueue::default()),
generate_new_token: Box::new(new_token),
port: port,
}
}

Expand All @@ -45,10 +47,20 @@ impl SignerService {
self.queue.clone()
}

/// Returns signer port (if signer enabled) or `None` otherwise
pub fn port(&self) -> Option<u16> {
self.port
}

/// Returns true if Signer is enabled.
pub fn is_enabled(&self) -> bool {
self.port.is_some()
}

#[cfg(test)]
/// Creates new Signer Service for tests.
pub fn new_test() -> Self {
SignerService::new(|| Ok("new_token".into()))
pub fn new_test(port: Option<u16>) -> Self {
SignerService::new(|| Ok("new_token".into()), port)
}
}

Expand Down
28 changes: 24 additions & 4 deletions rpc/src/v1/impls/ethcore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ pub struct EthcoreClient<C, M, S: ?Sized, F=FetchClient> where
logger: Arc<RotatingLogger>,
settings: Arc<NetworkSettings>,
signer: Option<Arc<SignerService>>,
fetch: Mutex<F>
fetch: Mutex<F>,
dapps_port: Option<u16>,
}

impl<C, M, S: ?Sized> EthcoreClient<C, M, S> where
Expand All @@ -67,9 +68,10 @@ impl<C, M, S: ?Sized> EthcoreClient<C, M, S> where
net: &Arc<ManageNetwork>,
logger: Arc<RotatingLogger>,
settings: Arc<NetworkSettings>,
signer: Option<Arc<SignerService>>
signer: Option<Arc<SignerService>>,
dapps_port: Option<u16>,
) -> Self {
Self::with_fetch(client, miner, sync, net, logger, settings, signer)
Self::with_fetch(client, miner, sync, net, logger, settings, signer, dapps_port)
}
}

Expand All @@ -87,7 +89,8 @@ impl<C, M, S: ?Sized, F> EthcoreClient<C, M, S, F> where
net: &Arc<ManageNetwork>,
logger: Arc<RotatingLogger>,
settings: Arc<NetworkSettings>,
signer: Option<Arc<SignerService>>
signer: Option<Arc<SignerService>>,
dapps_port: Option<u16>,
) -> Self {
EthcoreClient {
client: Arc::downgrade(client),
Expand All @@ -98,6 +101,7 @@ impl<C, M, S: ?Sized, F> EthcoreClient<C, M, S, F> where
settings: settings,
signer: signer,
fetch: Mutex::new(F::default()),
dapps_port: dapps_port,
}
}

Expand Down Expand Up @@ -314,4 +318,20 @@ impl<C, M, S: ?Sized, F> Ethcore for EthcoreClient<C, M, S, F> where
}
}
}

fn signer_port(&self) -> Result<u16, Error> {
try!(self.active());

self.signer
.clone()
.and_then(|signer| signer.port())
.ok_or_else(|| errors::signer_disabled())
}

fn dapps_port(&self) -> Result<u16, Error> {
try!(self.active());

self.dapps_port
.ok_or_else(|| errors::dapps_disabled())
}
}
13 changes: 1 addition & 12 deletions rpc/src/v1/impls/personal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,16 @@ pub struct PersonalClient<C, M> where C: MiningBlockChainClient, M: MinerService
accounts: Weak<AccountProvider>,
client: Weak<C>,
miner: Weak<M>,
signer_port: Option<u16>,
allow_perm_unlock: bool,
}

impl<C, M> PersonalClient<C, M> where C: MiningBlockChainClient, M: MinerService {
/// Creates new PersonalClient
pub fn new(store: &Arc<AccountProvider>, client: &Arc<C>, miner: &Arc<M>, signer_port: Option<u16>, allow_perm_unlock: bool) -> Self {
pub fn new(store: &Arc<AccountProvider>, client: &Arc<C>, miner: &Arc<M>, allow_perm_unlock: bool) -> Self {
PersonalClient {
accounts: Arc::downgrade(store),
client: Arc::downgrade(client),
miner: Arc::downgrade(miner),
signer_port: signer_port,
allow_perm_unlock: allow_perm_unlock,
}
}
Expand All @@ -59,15 +57,6 @@ impl<C, M> PersonalClient<C, M> where C: MiningBlockChainClient, M: MinerService

impl<C: 'static, M: 'static> Personal for PersonalClient<C, M> where C: MiningBlockChainClient, M: MinerService {

fn signer_enabled(&self, params: Params) -> Result<Value, Error> {
try!(self.active());
try!(expect_no_params(params));

Ok(self.signer_port
.map(|v| to_value(&v))
.unwrap_or_else(|| to_value(&false)))
}

fn accounts(&self, params: Params) -> Result<Value, Error> {
try!(self.active());
try!(expect_no_params(params));
Expand Down
8 changes: 3 additions & 5 deletions rpc/src/v1/tests/mocked/eth_signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct EthSigningTester {

impl Default for EthSigningTester {
fn default() -> Self {
let signer = Arc::new(SignerService::new_test());
let signer = Arc::new(SignerService::new_test(None));
let client = Arc::new(TestBlockChainClient::default());
let miner = Arc::new(TestMinerService::default());
let accounts = Arc::new(AccountProvider::transient_provider());
Expand Down Expand Up @@ -272,10 +272,8 @@ fn should_dispatch_transaction_if_account_is_unlock() {
fn should_decrypt_message_if_account_is_unlocked() {
// given
let tester = eth_signing();
let sync = ethcore::sync_provider();
let net = ethcore::network_service();
let ethcore_client = ethcore::ethcore_client(&tester.client, &tester.miner, &sync, &net);
tester.io.add_delegate(ethcore_client.to_delegate());
let ethcore = ethcore::Dependencies::new();
tester.io.add_delegate(ethcore.client(None).to_delegate());
let (address, public) = tester.accounts.new_account_and_public("test").unwrap();
tester.accounts.unlock_account_permanently(address, "test".into()).unwrap();

Expand Down
Loading