forc_crypto/keys/
parse_secret.rs1use super::KeyType;
2use anyhow::Result;
3use fuel_core_types::{fuel_crypto::SecretKey, fuel_tx::Input};
4use libp2p_identity::{secp256k1, Keypair, PeerId};
5use serde_json::json;
6use std::{ops::Deref, str::FromStr};
7
8forc_util::cli_examples! {
9 crate::Command {
10 [ Parses the secret of a block production => "forc crypto parse-secret \"f5204427d0ab9a311266c96a377f7c329cb8a41b9088225b6fcf40eefb423e28\"" ]
11 [ Parses the secret of a peering => "forc crypto parse-secret -k peering \"f5204427d0ab9a311266c96a377f7c329cb8a41b9088225b6fcf40eefb423e28\"" ]
12 }
13}
14
15#[derive(Debug, clap::Args)]
17#[clap(
18 version,
19 after_help = help(),
20)]
21pub struct Arg {
22 secret: String,
24 #[clap(
26 long = "key-type",
27 short = 'k',
28 value_enum,
29 default_value = <&'static str>::from(KeyType::BlockProduction),
30 )]
31 key_type: KeyType,
32}
33
34pub fn handler(arg: Arg) -> Result<serde_json::Value> {
35 let secret = SecretKey::from_str(&arg.secret)?;
36 let output = match arg.key_type {
37 KeyType::BlockProduction => {
38 let address = Input::owner(&secret.public_key());
39 let output = json!({
40 "address": address.to_string(),
41 "type": <KeyType as std::convert::Into<&'static str>>::into(KeyType::BlockProduction),
42 });
43 output
44 }
45 KeyType::Peering => {
46 let mut bytes = *secret.deref();
47 let p2p_secret = secp256k1::SecretKey::try_from_bytes(&mut bytes)
48 .expect("Should be a valid private key");
49 let p2p_keypair = secp256k1::Keypair::from(p2p_secret);
50 let libp2p_keypair = Keypair::from(p2p_keypair);
51 let peer_id = PeerId::from_public_key(&libp2p_keypair.public());
52 let output = json!({
53 "peer_id": peer_id.to_string(),
54 "type": <KeyType as std::convert::Into<&'static str>>::into(KeyType::Peering),
55 });
56 output
57 }
58 };
59 Ok(output)
60}