Skip to content

Commit d357547

Browse files
joojiskseo
authored andcommitted
Add import-raw subcommand to account command
1 parent e83b7f9 commit d357547

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

codechain/account_command.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616

1717
use std::fs;
18+
use std::str::FromStr;
1819

1920
use rpassword;
2021

2122
use ccore::AccountProvider;
23+
use ckey::Private;
2224
use ckeystore::accounts_dir::RootDiskDirectory;
2325
use ckeystore::KeyStore;
2426
use clap::ArgMatches;
@@ -65,6 +67,30 @@ pub fn run_account_command(matches: ArgMatches) -> Result<(), String> {
6567
}
6668
Ok(())
6769
}
70+
("import-raw", Some(matches)) => {
71+
let key = {
72+
let val = matches.value_of("RAW_KEY").expect("RAW_KEY arg is required and its index is 1");
73+
if val.starts_with("0x") {
74+
&val[2..]
75+
} else {
76+
&val[..]
77+
}
78+
};
79+
match Private::from_str(key) {
80+
Ok(private) => {
81+
if let Some(password) = read_password_and_confirm() {
82+
match ap.insert_account(private, password.as_ref()) {
83+
Ok(address) => println!("{:?}", address),
84+
Err(e) => return Err(format!("{:?}", e)),
85+
}
86+
} else {
87+
return Err("The password does not match".to_string())
88+
}
89+
}
90+
Err(e) => return Err(format!("{:?}", e)),
91+
}
92+
Ok(())
93+
}
6894
("list", _) => {
6995
let addresses = ap.get_list().expect("Cannot get account list");
7096
for address in addresses {

codechain/codechain.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,13 @@ subcommands:
197197
help: The path of the JSON key file.
198198
required: true
199199
index: 1
200+
- import-raw:
201+
about: import a raw private key
202+
args:
203+
- RAW_KEY:
204+
help: A raw private key.
205+
required: true
206+
index: 1
200207
- list:
201208
about: list managed accounts
202209
- lock:

0 commit comments

Comments
 (0)