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`
This commit is contained in:
Bastian Köcher
2020-09-11 14:05:43 +02:00
committed by GitHub
parent 232a30fdb4
commit 3abcd72f8f
2 changed files with 55 additions and 33 deletions
+19 -3
View File
@@ -625,10 +625,26 @@ impl NodeKeyConfig {
Ok(Keypair::Ed25519(k.into())),
Ed25519(Secret::File(f)) =>
get_secret(f,
|mut b| ed25519::SecretKey::from_bytes(&mut b),
get_secret(
f,
|mut b| {
match String::from_utf8(b.to_vec())
.ok()
.and_then(|s|{
if s.len() == 64 {
hex::decode(&s).ok()
} else {
None
}}
)
{
Some(s) => ed25519::SecretKey::from_bytes(s),
_ => ed25519::SecretKey::from_bytes(&mut b),
}
},
ed25519::SecretKey::generate,
|b| b.as_ref().to_vec())
|b| b.as_ref().to_vec()
)
.map(ed25519::Keypair::from)
.map(Keypair::Ed25519),
}