Skip to content

Commit 3c6067a

Browse files
committed
Implement the errors of account RPC methods
1 parent ae11889 commit 3c6067a

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
@@ -19,6 +19,7 @@ use std::fmt;
1919
use ccore::AccountProviderError;
2020
use ccore::Error as CoreError;
2121
use ckey::Error as KeyError;
22+
use ckeystore::Error as KeystoreError;
2223
use cnetwork::control::Error as NetworkControlError;
2324
use cstate::StateError;
2425
use ctypes::parcel::Error as ParcelError;
@@ -43,6 +44,11 @@ mod codes {
4344
pub const TOO_LOW_FEE: i64 = -32033;
4445
pub const TOO_CHEAP_TO_REPLACE: i64 = -32034;
4546
pub const INVALID_NONCE: i64 = -32035;
47+
pub const KEYSTORE_ERROR: i64 = -32040;
48+
pub const KEY_ERROR: i64 = -32041;
49+
pub const ALREADY_EXISTS: i64 = -32042;
50+
pub const WRONG_PASSWORD: i64 = -32043;
51+
pub const NO_SUCH_ACCOUNT: i64 = -32044;
4652
pub const UNKNOWN_ERROR: i64 = -32099;
4753
}
4854

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

150156
pub fn account_provider(error: AccountProviderError) -> Error {
151-
Error {
152-
code: ErrorCode::ServerError(codes::ACCOUNT_PROVIDER_ERROR),
153-
message: "AccountProvider error".into(),
154-
data: Some(Value::String(format!("{:?}", error))),
157+
match error {
158+
AccountProviderError::KeystoreError(error) => match error {
159+
KeystoreError::InvalidAccount => Error {
160+
code: ErrorCode::ServerError(codes::NO_SUCH_ACCOUNT),
161+
message: "No Such Account".into(),
162+
data: Some(Value::String(format!("{:?}", error))),
163+
},
164+
KeystoreError::InvalidPassword => Error {
165+
code: ErrorCode::ServerError(codes::WRONG_PASSWORD),
166+
message: "Wrong Password".into(),
167+
data: Some(Value::String(format!("{:?}", error))),
168+
},
169+
KeystoreError::AlreadyExists => Error {
170+
code: ErrorCode::ServerError(codes::ALREADY_EXISTS),
171+
message: "Already Exists".into(),
172+
data: Some(Value::String(format!("{:?}", error))),
173+
},
174+
_ => Error {
175+
code: ErrorCode::ServerError(codes::KEYSTORE_ERROR),
176+
message: "Keystore Error".into(),
177+
data: Some(Value::String(format!("{:?}", error))),
178+
},
179+
},
180+
AccountProviderError::KeyError(_) => Error {
181+
code: ErrorCode::ServerError(codes::KEY_ERROR),
182+
message: "Key Error".into(),
183+
data: Some(Value::String(format!("{:?}", error))),
184+
},
185+
_ => Error {
186+
code: ErrorCode::ServerError(codes::ACCOUNT_PROVIDER_ERROR),
187+
message: "AccountProvider Error".into(),
188+
data: Some(Value::String(format!("{:?}", error))),
189+
},
155190
}
156191
}
157192

0 commit comments

Comments
 (0)