Skip to content
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions keystore/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ pub enum Error {
InvalidKeyFile(String),
/// Account creation failed.
CreationFailed,
/// Account already exists.
AlreadyExists,
/// `ckeys` error
CKey(CKeyError),
/// `CCrypto` error
Expand All @@ -55,6 +57,7 @@ impl fmt::Display for Error {
Error::InvalidMessage => "Invalid message".into(),
Error::InvalidKeyFile(ref reason) => format!("Invalid key file: {}", reason),
Error::CreationFailed => "Account creation failed".into(),
Error::AlreadyExists => "Account already exists".into(),
Error::CKey(ref err) => err.to_string(),
Error::CCrypto(ref err) => err.to_string(),
Error::Custom(ref s) => s.clone(),
Expand Down
14 changes: 9 additions & 5 deletions keystore/src/keystore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ impl KeyStore {

impl SimpleSecretStore for KeyStore {
fn insert_account(&self, secret: Secret, password: &Password) -> Result<Address, Error> {
self.store.insert_account(secret, password)
Copy link
Member

Choose a reason for hiding this comment

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

What does store.insert_account return when the account already exists?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess it's a legacy from Parity, the store is KeyMultiStore. KeyMultiStore can store multiple accounts with different passwords for one address. In that case, the store allows the address to use if one of the passwords is given.

let keypair = KeyPair::from_private(secret.into()).map_err(|_| Error::CreationFailed)?;
match self.has_account(&keypair.address())? {
true => Err(Error::AlreadyExists),
false => self.store.insert_account(secret, password),
}
}

fn accounts(&self) -> Result<Vec<Address>, Error> {
Expand Down Expand Up @@ -345,10 +349,10 @@ impl SimpleSecretStore for KeyMultiStore {
}

fn has_account(&self, account: &Address) -> Result<bool, Error> {
let mut accounts = self.get_accounts(account)?.into_iter();
match accounts.next() {
Some(_) => Ok(true),
None => Ok(false),
match self.get_accounts(account) {
Ok(_) => Ok(true),
Err(Error::InvalidAccount) => Ok(false),
Err(e) => Err(e),
}
}

Expand Down
1 change: 1 addition & 0 deletions rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ authors = ["CodeChain Team <[email protected]>"]
[dependencies]
codechain-core = { path = "../core" }
codechain-key = { path = "../key" }
codechain-keystore = { path = "../keystore" }
codechain-logger = { path = "../util/logger" }
codechain-network = { path = "../network" }
codechain-state = { path = "../state" }
Expand Down
1 change: 1 addition & 0 deletions rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ extern crate codechain_core as ccore;
#[macro_use]
extern crate codechain_logger as clogger;
extern crate codechain_key as ckey;
extern crate codechain_keystore as ckeystore;
extern crate codechain_network as cnetwork;
extern crate codechain_state as cstate;
extern crate codechain_types as ctypes;
Expand Down
43 changes: 39 additions & 4 deletions rpc/src/v1/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use std::fmt;
use ccore::AccountProviderError;
use ccore::Error as CoreError;
use ckey::Error as KeyError;
use ckeystore::Error as KeystoreError;
use cnetwork::control::Error as NetworkControlError;
use cstate::StateError;
use ctypes::parcel::Error as ParcelError;
Expand All @@ -43,6 +44,11 @@ mod codes {
pub const TOO_LOW_FEE: i64 = -32033;
pub const TOO_CHEAP_TO_REPLACE: i64 = -32034;
pub const INVALID_NONCE: i64 = -32035;
pub const KEYSTORE_ERROR: i64 = -32040;
pub const KEY_ERROR: i64 = -32041;
pub const ALREADY_EXISTS: i64 = -32042;
pub const WRONG_PASSWORD: i64 = -32043;
pub const NO_SUCH_ACCOUNT: i64 = -32044;
pub const UNKNOWN_ERROR: i64 = -32099;
}

Expand Down Expand Up @@ -148,10 +154,39 @@ pub fn rlp(error: DecoderError) -> Error {
}

pub fn account_provider(error: AccountProviderError) -> Error {
Error {
code: ErrorCode::ServerError(codes::ACCOUNT_PROVIDER_ERROR),
message: "AccountProvider error".into(),
data: Some(Value::String(format!("{:?}", error))),
match error {
AccountProviderError::KeystoreError(error) => match error {
KeystoreError::InvalidAccount => Error {
code: ErrorCode::ServerError(codes::NO_SUCH_ACCOUNT),
message: "No Such Account".into(),
data: Some(Value::String(format!("{:?}", error))),
},
KeystoreError::InvalidPassword => Error {
code: ErrorCode::ServerError(codes::WRONG_PASSWORD),
message: "Wrong Password".into(),
data: Some(Value::String(format!("{:?}", error))),
},
KeystoreError::AlreadyExists => Error {
code: ErrorCode::ServerError(codes::ALREADY_EXISTS),
message: "Already Exists".into(),
data: Some(Value::String(format!("{:?}", error))),
},
_ => Error {
code: ErrorCode::ServerError(codes::KEYSTORE_ERROR),
message: "Keystore Error".into(),
data: Some(Value::String(format!("{:?}", error))),
},
},
AccountProviderError::KeyError(_) => Error {
code: ErrorCode::ServerError(codes::KEY_ERROR),
message: "Key Error".into(),
data: Some(Value::String(format!("{:?}", error))),
},
_ => Error {
code: ErrorCode::ServerError(codes::ACCOUNT_PROVIDER_ERROR),
message: "AccountProvider Error".into(),
data: Some(Value::String(format!("{:?}", error))),
},
}
}

Expand Down