diff --git a/Cargo.lock b/Cargo.lock index 35a79fbd5a..6abcab2f25 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1428,6 +1428,7 @@ dependencies = [ "futures", "hex", "hmac-sha512", + "libp2p-identity 0.2.8", "libsecp256k1", "pallet-cf-account-roles", "pallet-cf-environment", @@ -1707,7 +1708,7 @@ checksum = "b9b68e3193982cd54187d71afdb2a271ad4cf8af157858e9cb911b91321de143" dependencies = [ "core2", "multibase", - "multihash", + "multihash 0.17.0", "serde", "unsigned-varint", ] @@ -5648,7 +5649,7 @@ dependencies = [ "libp2p-core", "libp2p-dns", "libp2p-identify", - "libp2p-identity", + "libp2p-identity 0.1.3", "libp2p-kad", "libp2p-mdns", "libp2p-metrics", @@ -5673,7 +5674,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "510daa05efbc25184458db837f6f9a5143888f1caa742426d92e1833ddd38a50" dependencies = [ "libp2p-core", - "libp2p-identity", + "libp2p-identity 0.1.3", "libp2p-swarm", "void", ] @@ -5685,7 +5686,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4caa33f1d26ed664c4fe2cca81a08c8e07d4c1c04f2f4ac7655c2dd85467fda0" dependencies = [ "libp2p-core", - "libp2p-identity", + "libp2p-identity 0.1.3", "libp2p-swarm", "void", ] @@ -5701,10 +5702,10 @@ dependencies = [ "futures", "futures-timer", "instant", - "libp2p-identity", + "libp2p-identity 0.1.3", "log", "multiaddr", - "multihash", + "multihash 0.17.0", "multistream-select", "once_cell", "parking_lot 0.12.1", @@ -5743,7 +5744,7 @@ dependencies = [ "futures", "futures-timer", "libp2p-core", - "libp2p-identity", + "libp2p-identity 0.1.3", "libp2p-swarm", "log", "lru", @@ -5764,7 +5765,7 @@ dependencies = [ "ed25519-dalek 2.0.0", "log", "multiaddr", - "multihash", + "multihash 0.17.0", "quick-protobuf", "rand 0.8.5", "sha2 0.10.8", @@ -5772,6 +5773,23 @@ dependencies = [ "zeroize", ] +[[package]] +name = "libp2p-identity" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "999ec70441b2fb35355076726a6bc466c932e9bdc66f6a11c6c0aa17c7ab9be0" +dependencies = [ + "bs58 0.5.0", + "ed25519-dalek 2.0.0", + "hkdf", + "multihash 0.19.1", + "quick-protobuf", + "sha2 0.10.8", + "thiserror", + "tracing", + "zeroize", +] + [[package]] name = "libp2p-kad" version = "0.43.3" @@ -5787,7 +5805,7 @@ dependencies = [ "futures-timer", "instant", "libp2p-core", - "libp2p-identity", + "libp2p-identity 0.1.3", "libp2p-swarm", "log", "quick-protobuf", @@ -5810,7 +5828,7 @@ dependencies = [ "futures", "if-watch", "libp2p-core", - "libp2p-identity", + "libp2p-identity 0.1.3", "libp2p-swarm", "log", "rand 0.8.5", @@ -5845,7 +5863,7 @@ dependencies = [ "curve25519-dalek 3.2.0", "futures", "libp2p-core", - "libp2p-identity", + "libp2p-identity 0.1.3", "log", "once_cell", "quick-protobuf", @@ -5886,7 +5904,7 @@ dependencies = [ "futures-timer", "if-watch", "libp2p-core", - "libp2p-identity", + "libp2p-identity 0.1.3", "libp2p-tls", "log", "parking_lot 0.12.1", @@ -5907,7 +5925,7 @@ dependencies = [ "futures", "instant", "libp2p-core", - "libp2p-identity", + "libp2p-identity 0.1.3", "libp2p-swarm", "rand 0.8.5", "smallvec", @@ -5925,7 +5943,7 @@ dependencies = [ "futures-timer", "instant", "libp2p-core", - "libp2p-identity", + "libp2p-identity 0.1.3", "libp2p-swarm-derive", "log", "rand 0.8.5", @@ -5970,7 +5988,7 @@ dependencies = [ "futures", "futures-rustls", "libp2p-core", - "libp2p-identity", + "libp2p-identity 0.1.3", "rcgen 0.10.0", "ring 0.16.20", "rustls 0.20.9", @@ -6008,10 +6026,10 @@ dependencies = [ "hex", "if-watch", "libp2p-core", - "libp2p-identity", + "libp2p-identity 0.1.3", "libp2p-noise", "log", - "multihash", + "multihash 0.17.0", "quick-protobuf", "quick-protobuf-codec", "rand 0.8.5", @@ -6554,7 +6572,7 @@ dependencies = [ "data-encoding", "log", "multibase", - "multihash", + "multihash 0.17.0", "percent-encoding", "serde", "static_assertions", @@ -6590,6 +6608,16 @@ dependencies = [ "unsigned-varint", ] +[[package]] +name = "multihash" +version = "0.19.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "076d548d76a0e2a0d4ab471d0b1c36c577786dfc4471242035d97a12a735c492" +dependencies = [ + "core2", + "unsigned-varint", +] + [[package]] name = "multihash-derive" version = "0.8.1" @@ -9370,7 +9398,7 @@ dependencies = [ "clap 4.4.7", "fdlimit", "futures", - "libp2p-identity", + "libp2p-identity 0.1.3", "log", "names", "parity-scale-codec", @@ -9459,7 +9487,7 @@ dependencies = [ "async-trait", "futures", "futures-timer", - "libp2p-identity", + "libp2p-identity 0.1.3", "log", "mockall", "parking_lot 0.12.1", @@ -9719,7 +9747,7 @@ dependencies = [ "async-channel 1.9.0", "cid", "futures", - "libp2p-identity", + "libp2p-identity 0.1.3", "log", "prost", "prost-build", @@ -9739,7 +9767,7 @@ dependencies = [ "async-trait", "bitflags 1.3.2", "futures", - "libp2p-identity", + "libp2p-identity 0.1.3", "parity-scale-codec", "prost-build", "sc-consensus", @@ -9774,7 +9802,7 @@ dependencies = [ "array-bytes 6.1.0", "async-channel 1.9.0", "futures", - "libp2p-identity", + "libp2p-identity 0.1.3", "log", "parity-scale-codec", "prost", diff --git a/api/bin/chainflip-cli/src/main.rs b/api/bin/chainflip-cli/src/main.rs index fcd21b90d4..b38d806059 100644 --- a/api/bin/chainflip-cli/src/main.rs +++ b/api/bin/chainflip-cli/src/main.rs @@ -308,6 +308,7 @@ fn generate_keys(json: bool, path: Option, seed_phrase: Option) #[derive(Serialize)] struct Keys { node_key: KeyPair, + peer_id: String, seed_phrase: String, ethereum_key: KeyPair, #[serde(with = "hex")] @@ -319,6 +320,7 @@ fn generate_keys(json: bool, path: Option, seed_phrase: Option) 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{}", @@ -346,8 +348,12 @@ fn generate_keys(json: bool, path: Option, seed_phrase: Option) 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, diff --git a/api/lib/Cargo.toml b/api/lib/Cargo.toml index 8ca862ec29..d1ad1bccc4 100644 --- a/api/lib/Cargo.toml +++ b/api/lib/Cargo.toml @@ -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/" } diff --git a/api/lib/src/lib.rs b/api/lib/src/lib.rs index 3190cc910b..a62d28ee50 100644 --- a/api/lib/src/lib.rs +++ b/api/lib/src/lib.rs @@ -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). @@ -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(), + ); } } }