Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 447a64a

Browse files
authored
Support hex encoded secret key for --node-key (#7052)
* Support hex encoded secret key for `--node-key` Adds support for reading a hex encoded secret key when being passed as file via `--node-key`. * Make the key loading uniform * Switch to `hex::decode`
1 parent b4ee65d commit 447a64a

File tree

2 files changed

+55
-33
lines changed

2 files changed

+55
-33
lines changed

client/cli/src/params/node_key_params.rs

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
// You should have received a copy of the GNU General Public License
1717
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1818

19-
use sc_network::config::NodeKeyConfig;
19+
use sc_network::{config::identity::ed25519, config::NodeKeyConfig};
2020
use sp_core::H256;
2121
use std::{path::PathBuf, str::FromStr};
2222
use structopt::StructOpt;
@@ -83,7 +83,7 @@ pub struct NodeKeyParams {
8383
/// as follows:
8484
///
8585
/// `ed25519`:
86-
/// The file must contain an unencoded 32 byte Ed25519 secret key.
86+
/// The file must contain an unencoded 32 byte or hex encoded Ed25519 secret key.
8787
///
8888
/// If the file does not exist, it is created with a newly generated secret key of
8989
/// the chosen type.
@@ -100,12 +100,11 @@ impl NodeKeyParams {
100100
let secret = if let Some(node_key) = self.node_key.as_ref() {
101101
parse_ed25519_secret(node_key)?
102102
} else {
103-
let path = self
104-
.node_key_file
105-
.clone()
106-
.unwrap_or_else(|| net_config_dir.join(NODE_KEY_ED25519_FILE));
107-
108-
sc_network::config::Secret::File(path)
103+
sc_network::config::Secret::File(
104+
self.node_key_file
105+
.clone()
106+
.unwrap_or_else(|| net_config_dir.join(NODE_KEY_ED25519_FILE))
107+
)
109108
};
110109

111110
NodeKeyConfig::Ed25519(secret)
@@ -124,7 +123,7 @@ fn parse_ed25519_secret(hex: &str) -> error::Result<sc_network::config::Ed25519S
124123
H256::from_str(&hex)
125124
.map_err(invalid_node_key)
126125
.and_then(|bytes| {
127-
sc_network::config::identity::ed25519::SecretKey::from_bytes(bytes)
126+
ed25519::SecretKey::from_bytes(bytes)
128127
.map(sc_network::config::Secret::Input)
129128
.map_err(invalid_node_key)
130129
})
@@ -133,7 +132,8 @@ fn parse_ed25519_secret(hex: &str) -> error::Result<sc_network::config::Ed25519S
133132
#[cfg(test)]
134133
mod tests {
135134
use super::*;
136-
use sc_network::config::identity::ed25519;
135+
use sc_network::config::identity::{ed25519, Keypair};
136+
use std::fs;
137137

138138
#[test]
139139
fn test_node_key_config_input() {
@@ -164,28 +164,34 @@ mod tests {
164164

165165
#[test]
166166
fn test_node_key_config_file() {
167-
fn secret_file(net_config_dir: &PathBuf) -> error::Result<()> {
168-
NodeKeyType::variants().iter().try_for_each(|t| {
169-
let node_key_type = NodeKeyType::from_str(t).unwrap();
170-
let tmp = tempfile::Builder::new().prefix("alice").tempdir()?;
171-
let file = tmp.path().join(format!("{}_mysecret", t)).to_path_buf();
172-
let params = NodeKeyParams {
173-
node_key_type,
174-
node_key: None,
175-
node_key_file: Some(file.clone()),
176-
};
177-
params.node_key(net_config_dir).and_then(|c| match c {
178-
NodeKeyConfig::Ed25519(sc_network::config::Secret::File(ref f))
179-
if node_key_type == NodeKeyType::Ed25519 && f == &file =>
180-
{
181-
Ok(())
182-
}
183-
_ => Err(error::Error::Input("Unexpected node key config".into())),
184-
})
185-
})
167+
fn check_key(file: PathBuf, key: &ed25519::SecretKey) {
168+
let params = NodeKeyParams {
169+
node_key_type: NodeKeyType::Ed25519,
170+
node_key: None,
171+
node_key_file: Some(file),
172+
};
173+
174+
let node_key = params.node_key(&PathBuf::from("not-used"))
175+
.expect("Creates node key config")
176+
.into_keypair()
177+
.expect("Creates node key pair");
178+
179+
match node_key {
180+
Keypair::Ed25519(ref pair)
181+
if pair.secret().as_ref() == key.as_ref() => {}
182+
_ => panic!("Invalid key"),
183+
}
186184
}
187185

188-
assert!(secret_file(&PathBuf::from_str("x").unwrap()).is_ok());
186+
let tmp = tempfile::Builder::new().prefix("alice").tempdir().expect("Creates tempfile");
187+
let file = tmp.path().join("mysecret").to_path_buf();
188+
let key = ed25519::SecretKey::generate();
189+
190+
fs::write(&file, hex::encode(key.as_ref())).expect("Writes secret key");
191+
check_key(file.clone(), &key);
192+
193+
fs::write(&file, &key).expect("Writes secret key");
194+
check_key(file.clone(), &key);
189195
}
190196

191197
#[test]

client/network/src/config.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -625,10 +625,26 @@ impl NodeKeyConfig {
625625
Ok(Keypair::Ed25519(k.into())),
626626

627627
Ed25519(Secret::File(f)) =>
628-
get_secret(f,
629-
|mut b| ed25519::SecretKey::from_bytes(&mut b),
628+
get_secret(
629+
f,
630+
|mut b| {
631+
match String::from_utf8(b.to_vec())
632+
.ok()
633+
.and_then(|s|{
634+
if s.len() == 64 {
635+
hex::decode(&s).ok()
636+
} else {
637+
None
638+
}}
639+
)
640+
{
641+
Some(s) => ed25519::SecretKey::from_bytes(s),
642+
_ => ed25519::SecretKey::from_bytes(&mut b),
643+
}
644+
},
630645
ed25519::SecretKey::generate,
631-
|b| b.as_ref().to_vec())
646+
|b| b.as_ref().to_vec()
647+
)
632648
.map(ed25519::Keypair::from)
633649
.map(Keypair::Ed25519),
634650
}

0 commit comments

Comments
 (0)