From 37b8a3400db3bd1b9fd35ab24f6b1311448b09a5 Mon Sep 17 00:00:00 2001 From: Cesar <142530682+cr-fuel@users.noreply.github.com> Date: Wed, 4 Oct 2023 14:20:32 -0300 Subject: [PATCH 1/2] Improve keygen Make the keygen a crate that can be implemented and extended from other places (like a forc-plugin). Upgraded its dependencies to avoid depending on protoc for building --- CHANGELOG.md | 1 + Cargo.lock | 41 ++++++++++++++++++++++---- bin/keygen/Cargo.toml | 2 +- bin/keygen/src/keygen.rs | 62 ++++++++++++++++------------------------ bin/keygen/src/lib.rs | 6 ++++ bin/keygen/src/main.rs | 19 ++++++++++-- 6 files changed, 84 insertions(+), 47 deletions(-) create mode 100644 bin/keygen/src/lib.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index b0073e4cb0b..72709e4b312 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ Description of the upcoming release here. ### Changed +- [#1397](https://github.com/FuelLabs/fuel-core/pull/1397): Improved keygen. Created a crate to be included from forc plugins and upgraded internal library to drop requirement of protoc to build - [#1349](https://github.com/FuelLabs/fuel-core/pull/1349): Updated peer-to-peer transactions API to support multiple blocks in a single request, and updated block synchronization to request multiple blocks based on the configured range of headers. - [#1380](https://github.com/FuelLabs/fuel-core/pull/1380): Add preliminary, hard-coded config values for heartbeat peer reputation, removing `todo`. - [#1377](https://github.com/FuelLabs/fuel-core/pull/1377): Remove `DiscoveryEvent` and use `KademliaEvent` directly in `DiscoveryBehavior`. diff --git a/Cargo.lock b/Cargo.lock index 412ed42a608..c45b818dc8a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2957,7 +2957,7 @@ dependencies = [ "anyhow", "clap 4.4.5", "fuel-core-types", - "libp2p-core 0.38.0", + "libp2p-identity 0.2.4", "serde_json", ] @@ -4373,7 +4373,7 @@ dependencies = [ "futures", "futures-timer", "instant", - "libp2p-identity", + "libp2p-identity 0.1.3", "log", "multiaddr 0.17.1", "multihash 0.17.0", @@ -4473,6 +4473,25 @@ dependencies = [ "zeroize", ] +[[package]] +name = "libp2p-identity" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f37304f29c82ede408db06aaba60cd2f783a111f46414d3fc4beedac19e0c67b" +dependencies = [ + "asn1_der", + "bs58 0.5.0", + "hkdf", + "libsecp256k1", + "log", + "multihash 0.19.1", + "quick-protobuf", + "rand 0.8.5", + "sha2 0.10.8", + "thiserror", + "zeroize", +] + [[package]] name = "libp2p-kad" version = "0.42.0" @@ -4586,7 +4605,7 @@ dependencies = [ "curve25519-dalek 3.2.0", "futures", "libp2p-core 0.39.2", - "libp2p-identity", + "libp2p-identity 0.1.3", "log", "once_cell", "quick-protobuf", @@ -4610,7 +4629,7 @@ dependencies = [ "futures-timer", "if-watch", "libp2p-core 0.39.2", - "libp2p-identity", + "libp2p-identity 0.1.3", "libp2p-tls", "log", "parking_lot 0.12.1", @@ -4697,7 +4716,7 @@ dependencies = [ "futures", "futures-rustls", "libp2p-core 0.39.2", - "libp2p-identity", + "libp2p-identity 0.1.3", "rcgen 0.10.0", "ring", "rustls 0.20.9", @@ -4721,7 +4740,7 @@ dependencies = [ "hex", "if-watch", "libp2p-core 0.39.2", - "libp2p-identity", + "libp2p-identity 0.1.3", "libp2p-noise 0.42.2", "log", "multihash 0.17.0", @@ -5140,6 +5159,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" diff --git a/bin/keygen/Cargo.toml b/bin/keygen/Cargo.toml index 87abf698b89..b74b70fb958 100644 --- a/bin/keygen/Cargo.toml +++ b/bin/keygen/Cargo.toml @@ -13,5 +13,5 @@ description = "Command line utilities for fuel-core key management" anyhow = { workspace = true } clap = { workspace = true, features = ["derive", "env"] } fuel-core-types = { workspace = true, features = ["serde", "random"] } -libp2p-core = { version = "0.38", features = ["secp256k1"] } +libp2p-identity = { version = "0.2.4", features = ["secp256k1", "peerid"] } serde_json = { workspace = true, features = ["raw_value"] } diff --git a/bin/keygen/src/keygen.rs b/bin/keygen/src/keygen.rs index caa59937920..24a6b5c1165 100644 --- a/bin/keygen/src/keygen.rs +++ b/bin/keygen/src/keygen.rs @@ -1,7 +1,8 @@ -use clap::{ - Parser, - ValueEnum, +use crate::{ + BLOCK_PRODUCTION, + P2P, }; +use clap::ValueEnum; use fuel_core_types::{ fuel_crypto::{ rand::{ @@ -12,11 +13,9 @@ use fuel_core_types::{ }, fuel_tx::Input, }; -use libp2p_core::{ - identity::{ - secp256k1, - Keypair, - }, +use libp2p_identity::{ + secp256k1, + Keypair, PeerId, }; use serde_json::json; @@ -25,46 +24,31 @@ use std::{ str::FromStr, }; -/// Key management utilities for configuring fuel-core -#[derive(Debug, Parser)] -pub(crate) enum Command { - New(NewKey), - Parse(ParseSecret), -} - -impl Command { - pub(crate) fn exec(&self) -> anyhow::Result<()> { - match self { - Command::New(cmd) => cmd.exec(), - Command::Parse(cmd) => cmd.exec(), - } - } -} - /// Generate a random new secret & public key in the format expected by fuel-core #[derive(Debug, clap::Args)] #[clap(author, version, about)] -pub(crate) struct NewKey { +pub struct NewKey { #[clap(long = "pretty", short = 'p')] pretty: bool, #[clap( long = "key-type", short = 'k', value_enum, - default_value = "block-production" + default_value = BLOCK_PRODUCTION, )] key_type: KeyType, } #[derive(Clone, Debug, Default, ValueEnum)] -pub(crate) enum KeyType { +#[clap(rename_all = "snake_case")] +pub enum KeyType { #[default] BlockProduction, Peering, } impl NewKey { - fn exec(&self) -> anyhow::Result<()> { + pub fn exec(&self) -> anyhow::Result<()> { let mut rng = StdRng::from_entropy(); let secret = SecretKey::random(&mut rng); let public_key = secret.public_key(); @@ -76,19 +60,20 @@ impl NewKey { json!({ "secret": secret_str, "address": address, - "type": "block_production" + "type": BLOCK_PRODUCTION, }) } KeyType::Peering => { let mut bytes = *secret.deref(); - let p2p_secret = secp256k1::SecretKey::from_bytes(&mut bytes) + let p2p_secret = secp256k1::SecretKey::try_from_bytes(&mut bytes) .expect("Should be a valid private key"); - let libp2p_keypair = Keypair::Secp256k1(p2p_secret.into()); + let p2p_keypair = secp256k1::Keypair::from(p2p_secret); + let libp2p_keypair = Keypair::from(p2p_keypair); let peer_id = PeerId::from_public_key(&libp2p_keypair.public()); json!({ "secret": secret_str, "peer_id": peer_id.to_string(), - "type": "p2p" + "type": P2P }) } }; @@ -99,7 +84,7 @@ impl NewKey { /// Parse a secret key to view the associated public key #[derive(Debug, clap::Args)] #[clap(author, version, about)] -pub(crate) struct ParseSecret { +pub struct ParseSecret { secret: String, #[clap(long = "pretty", short = 'p')] pretty: bool, @@ -107,13 +92,13 @@ pub(crate) struct ParseSecret { long = "key-type", short = 'k', value_enum, - default_value = "block-production" + default_value = BLOCK_PRODUCTION, )] key_type: KeyType, } impl ParseSecret { - fn exec(&self) -> anyhow::Result<()> { + pub fn exec(&self) -> anyhow::Result<()> { let secret = SecretKey::from_str(&self.secret)?; match self.key_type { KeyType::BlockProduction => { @@ -126,13 +111,14 @@ impl ParseSecret { } KeyType::Peering => { let mut bytes = *secret.deref(); - let p2p_secret = secp256k1::SecretKey::from_bytes(&mut bytes) + let p2p_secret = secp256k1::SecretKey::try_from_bytes(&mut bytes) .expect("Should be a valid private key"); - let libp2p_keypair = Keypair::Secp256k1(p2p_secret.into()); + let p2p_keypair = secp256k1::Keypair::from(p2p_secret); + let libp2p_keypair = Keypair::from(p2p_keypair); let peer_id = PeerId::from_public_key(&libp2p_keypair.public()); let output = json!({ "peer_id": peer_id.to_string(), - "type": "p2p" + "type": P2P }); print_value(output, self.pretty) } diff --git a/bin/keygen/src/lib.rs b/bin/keygen/src/lib.rs new file mode 100644 index 00000000000..d4fc0e28d30 --- /dev/null +++ b/bin/keygen/src/lib.rs @@ -0,0 +1,6 @@ +//! Keygen crate + +pub const BLOCK_PRODUCTION: &str = "block_production"; +pub const P2P: &str = "p2p"; + +pub mod keygen; diff --git a/bin/keygen/src/main.rs b/bin/keygen/src/main.rs index 62fbb198ba6..c05cef36f68 100644 --- a/bin/keygen/src/main.rs +++ b/bin/keygen/src/main.rs @@ -1,10 +1,25 @@ //! A simple keygen cli utility tool for configuring fuel-core use clap::Parser; +use fuel_core_keygen::keygen; -pub mod keygen; +/// Key management utilities for configuring fuel-core +#[derive(Debug, Parser)] +pub(crate) enum Command { + New(keygen::NewKey), + Parse(keygen::ParseSecret), +} + +impl Command { + pub(crate) fn exec(&self) -> anyhow::Result<()> { + match self { + Command::New(cmd) => cmd.exec(), + Command::Parse(cmd) => cmd.exec(), + } + } +} fn main() -> anyhow::Result<()> { - let cmd = keygen::Command::parse(); + let cmd = Command::parse(); cmd.exec() } From da99190b23cef2b7ea1f2b0d218b94ddcd8fa3ba Mon Sep 17 00:00:00 2001 From: Cesar <142530682+cr-fuel@users.noreply.github.com> Date: Wed, 4 Oct 2023 17:13:36 -0300 Subject: [PATCH 2/2] Use - instead of _ --- bin/keygen/src/keygen.rs | 1 - bin/keygen/src/lib.rs | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/bin/keygen/src/keygen.rs b/bin/keygen/src/keygen.rs index 24a6b5c1165..eec28b78608 100644 --- a/bin/keygen/src/keygen.rs +++ b/bin/keygen/src/keygen.rs @@ -40,7 +40,6 @@ pub struct NewKey { } #[derive(Clone, Debug, Default, ValueEnum)] -#[clap(rename_all = "snake_case")] pub enum KeyType { #[default] BlockProduction, diff --git a/bin/keygen/src/lib.rs b/bin/keygen/src/lib.rs index d4fc0e28d30..8dfe2700546 100644 --- a/bin/keygen/src/lib.rs +++ b/bin/keygen/src/lib.rs @@ -1,6 +1,6 @@ //! Keygen crate -pub const BLOCK_PRODUCTION: &str = "block_production"; +pub const BLOCK_PRODUCTION: &str = "block-production"; pub const P2P: &str = "p2p"; pub mod keygen;