Skip to content

Commit 02e07b2

Browse files
committed
Don't hardcode the network id for the account CLI
1 parent a3fd922 commit 02e07b2

File tree

5 files changed

+31
-2
lines changed

5 files changed

+31
-2
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.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ clap = { version = "2", features = ["yaml"] }
1010
codechain-core = { path = "core" }
1111
codechain-discovery = { path = "discovery" }
1212
codechain-finally = { path = "util/finally" }
13+
codechain-json = { path = "json" }
1314
codechain-logger = { path = "util/logger" }
1415
codechain-key = { path = "key" }
1516
codechain-keystore = { path = "keystore" }

codechain/codechain.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,11 @@ subcommands:
200200
value_name: PATH
201201
help: Specify the path for JSON key files to be found
202202
takes_value: true
203+
- chain:
204+
short: c
205+
long: chain
206+
help: Set the blockchain type out of solo, solo_authority, tendermint, cuckoo, blake_pow, husky or a path to chain scheme file.
207+
takes_value: true
203208
subcommands:
204209
- create:
205210
about: create account

codechain/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ extern crate serde_json;
3030
extern crate app_dirs;
3131
extern crate codechain_core as ccore;
3232
extern crate codechain_discovery as cdiscovery;
33+
extern crate codechain_json as cjson;
3334
extern crate codechain_key as ckey;
3435
extern crate codechain_keystore as ckeystore;
3536
#[macro_use]

codechain/subcommand/account_command.rs

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

1717
use std::fs;
18+
use std::io::Read;
1819
use std::str::FromStr;
1920

2021
use rpassword;
2122

2223
use ccore::AccountProvider;
24+
use cjson::scheme::Scheme;
2325
use ckey::{NetworkId, Password, PlatformAddress, Private};
2426
use ckeystore::accounts_dir::RootDiskDirectory;
2527
use ckeystore::KeyStore;
@@ -41,8 +43,8 @@ pub fn run_account_command(matches: ArgMatches) -> Result<(), String> {
4143
let dir = RootDiskDirectory::create(keys_path).expect("Cannot read key path directory");
4244
let keystore = KeyStore::open(Box::new(dir)).unwrap();
4345
let ap = AccountProvider::new(keystore);
44-
// FIXME: Don't hardcode network_id.
45-
let network_id: NetworkId = "tc".into();
46+
let chain = matches.value_of("chain").unwrap_or("solo");
47+
let network_id: NetworkId = read_network_id(chain).expect("Cannot read a network id");
4648

4749
match matches.subcommand() {
4850
("create", _) => create(&ap, network_id),
@@ -163,3 +165,22 @@ fn read_password_and_confirm() -> Option<Password> {
163165
None
164166
}
165167
}
168+
169+
fn read_network_id(chain: &str) -> Result<NetworkId, String> {
170+
let mut contents = String::new();
171+
let reader: &[u8] = match chain {
172+
"solo" => include_bytes!("../../core/res/solo.json"),
173+
"soloAuthority" => include_bytes!("../../core/res/solo_authority.json"),
174+
"tendermint" => include_bytes!("../../core/res/tendermint.json"),
175+
"cuckoo" => include_bytes!("../../core/res/cuckoo.json"),
176+
"blake_pow" => include_bytes!("../../core/res/blake_pow.json"),
177+
"husky" => include_bytes!("../../core/res/husky.json"),
178+
filename => {
179+
fs::File::open(filename)
180+
.and_then(|mut file| file.read_to_string(&mut contents))
181+
.map_err(|e| format!("Failed to load the scheme file at {}: {}", filename, e))?;
182+
contents.as_ref()
183+
}
184+
};
185+
Ok(Scheme::load(reader).map_err(|e| format!("Failed to load the scheme file: {}", e))?.params.network_id)
186+
}

0 commit comments

Comments
 (0)