Skip to content

Commit

Permalink
feat: CLI generate keys outputs peer id (#4241)
Browse files Browse the repository at this point in the history
  • Loading branch information
j4m1ef0rd authored Nov 14, 2023
1 parent e4fbd0f commit 845cbd5
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 37 deletions.
74 changes: 51 additions & 23 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion api/bin/chainflip-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ fn generate_keys(json: bool, path: Option<PathBuf>, seed_phrase: Option<String>)
#[derive(Serialize)]
struct Keys {
node_key: KeyPair,
peer_id: String,
seed_phrase: String,
ethereum_key: KeyPair,
#[serde(with = "hex")]
Expand All @@ -319,6 +320,7 @@ fn generate_keys(json: bool, path: Option<PathBuf>, seed_phrase: Option<String>)
impl std::fmt::Display for Keys {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "🔑 Node Public Key: 0x{}", hex::encode(&self.node_key.public_key))?;
writeln!(f, "👤 Node Peer ID: {}", self.peer_id)?;
writeln!(
f,
"🔑 Ethereum Public Key: 0x{}",
Expand Down Expand Up @@ -346,8 +348,12 @@ fn generate_keys(json: bool, path: Option<PathBuf>, seed_phrase: Option<String>)
api::generate_ethereum_key(Some(&seed_phrase))
.context("Error while generating Ethereum key.")?;
assert_eq!(seed_phrase, seed_phrase_eth);
let (node_key, peer_id) =
api::generate_node_key().context("Error while generating node key.")?;

Ok(Keys {
node_key: api::generate_node_key(),
node_key,
peer_id: peer_id.to_string(),
seed_phrase,
ethereum_key,
ethereum_address,
Expand Down
1 change: 1 addition & 0 deletions api/lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ tiny-bip39 = "1.0.0"
tokio = "1.28"
tracing = "0.1"
zeroize = "1.5.4"
libp2p-identity = { version = "0.2", features = ["ed25519", "peerid"] }

# Local
chainflip-engine = { path = "../../engine/" }
Expand Down
31 changes: 18 additions & 13 deletions api/lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,17 +381,22 @@ impl Serialize for KeyPair {
}

/// Generate a new random node key.
///
/// This key is used for secure communication between Validators.
pub fn generate_node_key() -> KeyPair {
pub fn generate_node_key() -> Result<(KeyPair, libp2p_identity::PeerId)> {
use rand_v7::SeedableRng;

let mut rng = rand_v7::rngs::StdRng::from_entropy();
let keypair = ed25519_dalek::Keypair::generate(&mut rng);
let libp2p_keypair = libp2p_identity::Keypair::ed25519_from_bytes(keypair.secret.to_bytes())?;

KeyPair {
secret_key: keypair.secret.as_bytes().to_vec(),
public_key: keypair.public.to_bytes().to_vec(),
}
Ok((
KeyPair {
secret_key: keypair.secret.as_bytes().to_vec(),
public_key: keypair.public.to_bytes().to_vec(),
},
libp2p_keypair.public().to_peer_id(),
))
}

/// Generate a signing key (aka. account key).
Expand Down Expand Up @@ -530,14 +535,14 @@ mod tests {
.unwrap(),
);
assert_eq!(
clean_foreign_chain_address(
ForeignChain::Polkadot,
"126PaS7kDWTdtiojd556gD4ZPcxj7KbjrMJj7xZ5i6XKfARF"
)
.unwrap_err()
.to_string(),
anyhow!("Address is neither valid ss58: 'Invalid checksum' nor hex: 'Invalid character 'P' at position 3'").to_string(),
);
clean_foreign_chain_address(
ForeignChain::Polkadot,
"126PaS7kDWTdtiojd556gD4ZPcxj7KbjrMJj7xZ5i6XKfARF"
)
.unwrap_err()
.to_string(),
anyhow!("Address is neither valid ss58: 'Invalid checksum' nor hex: 'Invalid character 'P' at position 3'").to_string(),
);
}
}
}

0 comments on commit 845cbd5

Please sign in to comment.