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

JSON-RPC personal service (follows #582) #583

Merged
merged 6 commits into from
Mar 4, 2016
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions rpc/src/v1/impls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ macro_rules! take_weak {
mod web3;
mod eth;
mod net;
mod personal;

pub use self::web3::Web3Client;
pub use self::eth::{EthClient, EthFilterClient};
pub use self::net::NetClient;
pub use self::personal::PersonalClient;
81 changes: 81 additions & 0 deletions rpc/src/v1/impls/personal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright 2015, 2016 Ethcore (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/>.

//! Account management (personal) rpc implementation
use std::sync::{Arc, Weak};
use jsonrpc_core::*;
use v1::traits::Personal;
use util::keys::store::*;
use util::{Bytes, Address};
use std::sync::RwLock;

/// Account management (personal) rpc implementation.
pub struct PersonalClient {
secret_store: Weak<SecretStore>,
unlocked_account: Arc<RwLock<Option<Address>>>,
unlocked_secret: Arc<RwLock<Option<Bytes>>>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't secret always H256?

}

impl PersonalClient {
/// Creates new PersonalClient
pub fn new(store: &Arc<SecretStore>) -> Self {
PersonalClient {
secret_store: Arc::downgrade(store),
unlocked_account: Arc::new(RwLock::new(None)),
unlocked_secret: Arc::new(RwLock::new(None)),
}
}
}

impl Personal for PersonalClient {
fn accounts(&self, _: Params) -> Result<Value, Error> {
let store = take_weak!(self.secret_store);
match store.accounts() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor issue. this function could be less lenient, and return error if there are any input params ;)

Ok(account_list) => {
Ok(Value::Array(account_list.iter()
.map(|&(account, _)| Value::String(format!("{:?}", account)))
.collect::<Vec<Value>>())
)
}
Err(_) => Err(Error::internal_error())
}
}

fn new_account(&self, _: Params) -> Result<Value, Error> {
Err(Error::internal_error())
}

fn unlock_account(&self, params: Params) -> Result<Value, Error> {
from_params::<(Address, String, u64)>(params).and_then(
|(account, account_pass, _)|{
let store = take_weak!(self.secret_store);
let secret_id = match store.account(&account) {
None => { return Ok(Value::Bool(false)); }
Some(id) => id
};
match store.get(&secret_id, &account_pass) {
Ok(secret) => {
*self.unlocked_account.write().unwrap() = Some(account);
*self.unlocked_secret.write().unwrap() = Some(secret);
Ok(Value::Bool(true))
},
Err(_) => {
Ok(Value::Bool(false))
}
}
})
}
}
4 changes: 2 additions & 2 deletions rpc/src/v1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

//! Ethcore rpc v1.
//!
//!
//! Compliant with ethereum rpc.

pub mod traits;
Expand All @@ -25,5 +25,5 @@ mod types;
mod tests;
mod helpers;

pub use self::traits::{Web3, Eth, EthFilter, Net};
pub use self::traits::{Web3, Eth, EthFilter, Personal, Net};
pub use self::impls::*;
2 changes: 2 additions & 0 deletions rpc/src/v1/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ macro_rules! rpc_unimplemented {
pub mod web3;
pub mod eth;
pub mod net;
pub mod personal;

pub use self::web3::Web3;
pub use self::eth::{Eth, EthFilter};
pub use self::net::Net;
pub use self::personal::Personal;
41 changes: 41 additions & 0 deletions rpc/src/v1/traits/personal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2015, 2016 Ethcore (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/>.

//! Personal rpc interface.
use std::sync::Arc;
use jsonrpc_core::*;

/// Personal rpc interface.
pub trait Personal: Sized + Send + Sync + 'static {

/// Lists all stored accounts
fn accounts(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }

/// Creates new account (it becomes new current unlocked account)
fn new_account(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }

/// Unlocks specified account for use (can only be one unlocked account at one moment)
fn unlock_account(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }

/// Should be used to convert object to io delegate.
fn to_delegate(self) -> IoDelegate<Self> {
let mut delegate = IoDelegate::new(Arc::new(self));
delegate.add_method("personal_listAccounts", Personal::accounts);
delegate.add_method("personal_newAccount", Personal::new_account);
delegate.add_method("personal_unlockAccount", Personal::unlock_account);
delegate
}
}