1515// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616
1717use std:: fs;
18+ use std:: io:: Read ;
1819use std:: str:: FromStr ;
1920
2021use rpassword;
2122
2223use ccore:: AccountProvider ;
24+ use cjson:: scheme:: Scheme ;
2325use ckey:: { NetworkId , Password , PlatformAddress , Private } ;
2426use ckeystore:: accounts_dir:: RootDiskDirectory ;
2527use 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