Skip to content

Commit 9200042

Browse files
authored
Transition network key to hex format (#7665)
#7181 Instead of storing the network key as binary data we store it as hex, allowing users to modify it via the file. We can read old-binary forms, however we will migrate binary to hex as it will be the new standard.
1 parent 317dc0f commit 9200042

File tree

1 file changed

+46
-17
lines changed
  • beacon_node/lighthouse_network/src/service

1 file changed

+46
-17
lines changed

beacon_node/lighthouse_network/src/service/utils.rs

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@ pub fn build_transport(
7878
Ok(transport)
7979
}
8080

81-
// Useful helper functions for debugging. Currently not used in the client.
82-
#[allow(dead_code)]
8381
fn keypair_from_hex(hex_bytes: &str) -> Result<Keypair, String> {
8482
let hex_bytes = if let Some(stripped) = hex_bytes.strip_prefix("0x") {
8583
stripped.to_string()
@@ -92,7 +90,6 @@ fn keypair_from_hex(hex_bytes: &str) -> Result<Keypair, String> {
9290
.and_then(keypair_from_bytes)
9391
}
9492

95-
#[allow(dead_code)]
9693
fn keypair_from_bytes(mut bytes: Vec<u8>) -> Result<Keypair, String> {
9794
secp256k1::SecretKey::try_from_bytes(&mut bytes)
9895
.map(|secret| {
@@ -106,21 +103,54 @@ fn keypair_from_bytes(mut bytes: Vec<u8>) -> Result<Keypair, String> {
106103
/// generated and is then saved to disk.
107104
///
108105
/// Currently only secp256k1 keys are allowed, as these are the only keys supported by discv5.
106+
/// Supports both hex format (with or without 0x prefix) and raw bytes format.
109107
pub fn load_private_key(config: &NetworkConfig) -> Keypair {
110108
// check for key from disk
111109
let network_key_f = config.network_dir.join(NETWORK_KEY_FILENAME);
112110
if let Ok(mut network_key_file) = File::open(network_key_f.clone()) {
113-
let mut key_bytes: Vec<u8> = Vec::with_capacity(36);
114-
match network_key_file.read_to_end(&mut key_bytes) {
115-
Err(_) => debug!("Could not read network key file"),
116-
Ok(_) => {
117-
// only accept secp256k1 keys for now
118-
if let Ok(secret_key) = secp256k1::SecretKey::try_from_bytes(&mut key_bytes) {
119-
let kp: secp256k1::Keypair = secret_key.into();
120-
debug!("Loaded network key from disk.");
121-
return kp.into();
122-
} else {
123-
debug!("Network key file is not a valid secp256k1 key");
111+
// Limit read to reasonable hex key size: 32 bytes = 64 hex chars + "0x" prefix + whitespace
112+
let mut buffer = vec![0u8; 70];
113+
match network_key_file.read(&mut buffer) {
114+
Ok(bytes_read) => {
115+
if let Ok(hex_string) = String::from_utf8(buffer[..bytes_read].to_vec()) {
116+
// First try to parse as hex string
117+
let hex_content = hex_string.trim();
118+
if let Ok(keypair) = keypair_from_hex(hex_content) {
119+
debug!("Loaded network key from disk (hex format).");
120+
return keypair;
121+
}
122+
}
123+
}
124+
Err(_) => debug!("Could not read network key file as string, trying binary format"),
125+
}
126+
127+
// If hex parsing failed or file couldn't be read as string, try binary format
128+
if let Ok(mut network_key_file) = File::open(network_key_f.clone()) {
129+
let mut key_bytes: Vec<u8> = Vec::with_capacity(36);
130+
match network_key_file.read_to_end(&mut key_bytes) {
131+
Err(_) => debug!("Could not read network key file"),
132+
Ok(_) => {
133+
// only accept secp256k1 keys for now
134+
if let Ok(secret_key) = secp256k1::SecretKey::try_from_bytes(&mut key_bytes) {
135+
let kp: secp256k1::Keypair = secret_key.clone().into();
136+
debug!(
137+
"Loaded network key from disk (binary format), migrating to hex format."
138+
);
139+
140+
// Migrate binary key to hex format
141+
let hex_key = hex::encode(secret_key.to_bytes());
142+
if let Err(e) = File::create(network_key_f.clone())
143+
.and_then(|mut f| f.write_all(hex_key.as_bytes()))
144+
{
145+
debug!("Failed to migrate key to hex format: {}", e);
146+
} else {
147+
debug!("Successfully migrated key to hex format.");
148+
}
149+
150+
return kp.into();
151+
} else {
152+
debug!("Network key file is not a valid secp256k1 key");
153+
}
124154
}
125155
}
126156
}
@@ -129,9 +159,8 @@ pub fn load_private_key(config: &NetworkConfig) -> Keypair {
129159
// if a key could not be loaded from disk, generate a new one and save it
130160
let local_private_key = secp256k1::Keypair::generate();
131161
let _ = std::fs::create_dir_all(&config.network_dir);
132-
match File::create(network_key_f.clone())
133-
.and_then(|mut f| f.write_all(&local_private_key.secret().to_bytes()))
134-
{
162+
let hex_key = hex::encode(local_private_key.secret().to_bytes());
163+
match File::create(network_key_f.clone()).and_then(|mut f| f.write_all(hex_key.as_bytes())) {
135164
Ok(_) => {
136165
debug!("New network key generated and written to disk");
137166
}

0 commit comments

Comments
 (0)