Skip to content

Commit 63847e2

Browse files
committed
Implement the errors of account RPC methods
1 parent cd3e3cf commit 63847e2

File tree

6 files changed

+50
-5
lines changed

6 files changed

+50
-5
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

keystore/src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ pub enum Error {
3737
InvalidKeyFile(String),
3838
/// Account creation failed.
3939
CreationFailed,
40+
/// Account already exists.
41+
AlreadyExists,
4042
/// `ckeys` error
4143
CKey(CKeyError),
4244
/// `CCrypto` error
@@ -55,6 +57,7 @@ impl fmt::Display for Error {
5557
Error::InvalidMessage => "Invalid message".into(),
5658
Error::InvalidKeyFile(ref reason) => format!("Invalid key file: {}", reason),
5759
Error::CreationFailed => "Account creation failed".into(),
60+
Error::AlreadyExists => "Account already exists".into(),
5861
Error::CKey(ref err) => err.to_string(),
5962
Error::CCrypto(ref err) => err.to_string(),
6063
Error::Custom(ref s) => s.clone(),

keystore/src/keystore.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ impl KeyStore {
6464

6565
impl SimpleSecretStore for KeyStore {
6666
fn insert_account(&self, secret: Secret, password: &Password) -> Result<Address, Error> {
67-
self.store.insert_account(secret, password)
67+
let keypair = KeyPair::from_private(secret.into()).map_err(|_| Error::CreationFailed)?;
68+
match self.has_account(&keypair.address())? {
69+
true => Err(Error::AlreadyExists),
70+
false => self.store.insert_account(secret, password),
71+
}
6872
}
6973

7074
fn accounts(&self) -> Result<Vec<Address>, Error> {

rpc/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ authors = ["CodeChain Team <[email protected]>"]
88
[dependencies]
99
codechain-core = { path = "../core" }
1010
codechain-key = { path = "../key" }
11+
codechain-keystore = { path = "../keystore" }
1112
codechain-logger = { path = "../util/logger" }
1213
codechain-network = { path = "../network" }
1314
codechain-state = { path = "../state" }

rpc/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ extern crate codechain_core as ccore;
1818
#[macro_use]
1919
extern crate codechain_logger as clogger;
2020
extern crate codechain_key as ckey;
21+
extern crate codechain_keystore as ckeystore;
2122
extern crate codechain_network as cnetwork;
2223
extern crate codechain_state as cstate;
2324
extern crate codechain_types as ctypes;

rpc/src/v1/errors.rs

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use std::fmt;
1818

1919
use ccore::AccountProviderError;
2020
use ccore::Error as CoreError;
21+
use ckeystore::Error as KeystoreError;
2122
use cnetwork::control::Error as NetworkControlError;
2223
use cstate::StateError;
2324
use kvdb::Error as KVDBError;
@@ -35,6 +36,11 @@ mod codes {
3536
pub const NETWORK_DISABLED: i64 = -32014;
3637
pub const NETWORK_CANNOT_DISCONNECT_NOT_CONNECTED_ERROR: i64 = -32015;
3738
pub const ACCOUNT_PROVIDER_ERROR: i64 = -32016;
39+
pub const KEYSTORE_ERROR: i64 = -32040;
40+
pub const KEY_ERROR: i64 = -32041;
41+
pub const ALREADY_EXISTS: i64 = -32042;
42+
pub const WRONG_PASSWORD: i64 = -32043;
43+
pub const NO_SUCH_ACCOUNT: i64 = -32044;
3844
}
3945

4046
pub fn core<T: Into<CoreError>>(error: T) -> Error {
@@ -97,10 +103,39 @@ pub fn rlp(error: DecoderError) -> Error {
97103
}
98104

99105
pub fn account_provider(error: AccountProviderError) -> Error {
100-
Error {
101-
code: ErrorCode::ServerError(codes::ACCOUNT_PROVIDER_ERROR),
102-
message: "AccountProvider error".into(),
103-
data: Some(Value::String(format!("{:?}", error))),
106+
match error {
107+
AccountProviderError::KeystoreError(error) => match error {
108+
KeystoreError::InvalidAccount => Error {
109+
code: ErrorCode::ServerError(codes::NO_SUCH_ACCOUNT),
110+
message: "No Such Account".into(),
111+
data: Some(Value::String(format!("{:?}", error))),
112+
},
113+
KeystoreError::InvalidPassword => Error {
114+
code: ErrorCode::ServerError(codes::WRONG_PASSWORD),
115+
message: "Wrong Password".into(),
116+
data: Some(Value::String(format!("{:?}", error))),
117+
},
118+
KeystoreError::AlreadyExists => Error {
119+
code: ErrorCode::ServerError(codes::ALREADY_EXISTS),
120+
message: "Already Exists".into(),
121+
data: Some(Value::String(format!("{:?}", error))),
122+
},
123+
_ => Error {
124+
code: ErrorCode::ServerError(codes::KEYSTORE_ERROR),
125+
message: "Keystore Error".into(),
126+
data: Some(Value::String(format!("{:?}", error))),
127+
},
128+
},
129+
AccountProviderError::KeyError(_) => Error {
130+
code: ErrorCode::ServerError(codes::KEY_ERROR),
131+
message: "Key Error".into(),
132+
data: Some(Value::String(format!("{:?}", error))),
133+
},
134+
_ => Error {
135+
code: ErrorCode::ServerError(codes::ACCOUNT_PROVIDER_ERROR),
136+
message: "AccountProvider Error".into(),
137+
data: Some(Value::String(format!("{:?}", error))),
138+
},
104139
}
105140
}
106141

0 commit comments

Comments
 (0)