Skip to content

Commit 99c7078

Browse files
izolyomiIstván Zólyomirubdosmxinden
authored
core/identity: Document using external fixed keys for network identity (#2165)
Co-authored-by: István Zólyomi <[email protected]> Co-authored-by: Ruben De Smet <[email protected]> Co-authored-by: Max Inden <[email protected]>
1 parent 50b0957 commit 99c7078

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

core/src/identity.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@
1919
// DEALINGS IN THE SOFTWARE.
2020

2121
//! A node's network identity keys.
22+
//!
23+
//! Such identity keys can be randomly generated on every startup,
24+
//! but using already existing, fixed keys is usually required.
25+
//! Though libp2p uses other crates (e.g. `ed25519_dalek`) internally,
26+
//! such details are not exposed as part of libp2p's public interface
27+
//! to keep them easily upgradable or replaceable (e.g. to `ed25519_zebra`).
28+
//! Consequently, keys of external ed25519 or secp256k1 crates cannot be
29+
//! directly converted into libp2p network identities.
30+
//! Instead, loading fixed keys must use the standard, thus more portable
31+
//! binary representation of the specific key type
32+
//! (e.g. [ed25519 binary format](https://datatracker.ietf.org/doc/html/rfc8032#section-5.1.5)).
33+
//! All key types have functions to enable conversion to/from their binary representations.
2234
2335
pub mod ed25519;
2436
#[cfg(not(target_arch = "wasm32"))]

core/src/identity/ed25519.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use core::fmt;
3131
pub struct Keypair(ed25519::Keypair);
3232

3333
impl Keypair {
34-
/// Generate a new Ed25519 keypair.
34+
/// Generate a new random Ed25519 keypair.
3535
pub fn generate() -> Keypair {
3636
Keypair::from(SecretKey::generate())
3737
}
@@ -43,8 +43,10 @@ impl Keypair {
4343
self.0.to_bytes()
4444
}
4545

46-
/// Decode a keypair from the format produced by `encode`,
47-
/// zeroing the input on success.
46+
/// Decode a keypair from the [binary format](https://datatracker.ietf.org/doc/html/rfc8032#section-5.1.5)
47+
/// produced by [`Keypair::encode`], zeroing the input on success.
48+
///
49+
/// Note that this binary format is the same as `ed25519_dalek`'s and `ed25519_zebra`'s.
4850
pub fn decode(kp: &mut [u8]) -> Result<Keypair, DecodingError> {
4951
ed25519::Keypair::from_bytes(kp)
5052
.map(|k| { kp.zeroize(); Keypair(k) })

core/src/identity/secp256k1.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,16 @@ impl fmt::Debug for SecretKey {
8383
}
8484

8585
impl SecretKey {
86-
/// Generate a new Secp256k1 secret key.
86+
/// Generate a new random Secp256k1 secret key.
8787
pub fn generate() -> SecretKey {
8888
SecretKey(libsecp256k1::SecretKey::random(&mut rand::thread_rng()))
8989
}
9090

9191
/// Create a secret key from a byte slice, zeroing the slice on success.
9292
/// If the bytes do not constitute a valid Secp256k1 secret key, an
9393
/// error is returned.
94+
///
95+
/// Note that the expected binary format is the same as `libsecp256k1`'s.
9496
pub fn from_bytes(mut sk: impl AsMut<[u8]>) -> Result<SecretKey, DecodingError> {
9597
let sk_bytes = sk.as_mut();
9698
let secret = libsecp256k1::SecretKey::parse_slice(&*sk_bytes)

0 commit comments

Comments
 (0)