Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(identity): follow naming conventions for conversion methods #3775

Merged
merged 47 commits into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
7d5e606
feat: make mdns::Event clone-able
drHuangMHT Mar 13, 2023
05ce438
Merge branch 'libp2p:master' into master
Mar 13, 2023
a72e5e2
docs: filling CHANGELOG for PR 3606
drHuangMHT Mar 13, 2023
09bef20
Merge branch 'master' of https://github.com/drHuangMHT/rust-libp2p
drHuangMHT Mar 13, 2023
926c7ca
Update protocols/mdns/CHANGELOG.md
thomaseizinger Mar 13, 2023
49d26c4
Update protocols/mdns/CHANGELOG.md
thomaseizinger Mar 13, 2023
4a35406
Merge branch 'master' into master
mergify[bot] Mar 13, 2023
a8ff1eb
feat: change 'mdns::Event' to hold 'SmallVec'
drHuangMHT Mar 16, 2023
4cec5fa
test: adapting test to 'mdns::Event' change
drHuangMHT Mar 16, 2023
a876f2f
Merge branch 'master' of https://github.com/drHuangMHT/rust-libp2p
drHuangMHT Mar 16, 2023
18811d2
feat(mdns): change 'mdns::Event' to hold 'Vec'
drHuangMHT Mar 16, 2023
abdce7e
doc: add entry for #3621
drHuangMHT Mar 16, 2023
d5c246c
Update protocols/mdns/CHANGELOG.md
Mar 16, 2023
ed128a7
Merge branch 'master' of https://github.com/drHuangMHT/rust-libp2p
drHuangMHT Mar 26, 2023
0d06f4c
Merge branch 'master' of https://github.com/drHuangMHT/rust-libp2p
drHuangMHT Mar 26, 2023
9d45cf3
Remove conflict marker to resolve conflicts
drHuangMHT Mar 26, 2023
752002b
Merge branch 'master' of https://github.com/drHuangMHT/rust-libp2p
drHuangMHT Apr 11, 2023
74d01be
rename methods in 'libp2p-identity::ecdsa'
drHuangMHT Apr 12, 2023
d219b0e
introduce 'KeyType' in 'libp2p-identity'
drHuangMHT Apr 12, 2023
8ca95ee
introduce 'OtherVariantError' in 'libp2p-identity::error'
drHuangMHT Apr 12, 2023
9b81b81
rename methods in 'libp2p-identity::ed25519'
drHuangMHT Apr 12, 2023
7a1f645
rename methods in 'libp2p-identity::keypair'
drHuangMHT Apr 12, 2023
25e1139
rename methods in 'libp2p-identity::rsa'
drHuangMHT Apr 12, 2023
d15d95b
rename methods in 'libp2p-identity::secp256k1'
drHuangMHT Apr 12, 2023
19ff427
formatting
drHuangMHT Apr 12, 2023
ba4511d
change call to deprecated methods to recommended ones
drHuangMHT Apr 12, 2023
8e9c227
clippy fix
drHuangMHT Apr 12, 2023
9ed6436
formatting
drHuangMHT Apr 12, 2023
cbbd01b
Undo mdns changes
thomaseizinger Apr 12, 2023
aa72364
delegate deprecated method to recommended one
drHuangMHT Apr 12, 2023
e6a3144
documentation improvement on 'libp2p-identity::ecdsa'
drHuangMHT Apr 12, 2023
4c7caf8
rename methods in 'libp2p-identity::ed25519'
drHuangMHT Apr 12, 2023
aa1577a
change visibility of 'OtherVariantError::new' to crate level
drHuangMHT Apr 12, 2023
6d9de35
documentation improvement on 'OtherVariantError'
drHuangMHT Apr 12, 2023
fdc0d25
documentation improvement and correction on 'libp2p_identity::ed25519'
drHuangMHT Apr 12, 2023
e736918
Merge branch 'rename-identity' of https://github.com/drHuangMHT/rust-…
drHuangMHT Apr 12, 2023
a46f661
patch version bump(to 0.1.2) for 'libp2p-identity'
drHuangMHT Apr 12, 2023
4479ff9
Update identity/CHANGELOG.md
thomaseizinger Apr 13, 2023
947284c
lockfile update
drHuangMHT Apr 14, 2023
9ae4e41
export 'OtherVariantError'
drHuangMHT Apr 14, 2023
85e9f5f
Merge branch 'libp2p:master' into rename-identity
Apr 14, 2023
38f4b38
dependency version bump for 'gossipsub'
drHuangMHT Apr 14, 2023
4fe27a0
dependency version bump for 'identify'
drHuangMHT Apr 14, 2023
b426f0d
dependency version bump for 'noise'
drHuangMHT Apr 14, 2023
f19ce60
dependency version bump for 'plaintext'
drHuangMHT Apr 14, 2023
8394c6a
dependency version bump for 'tls'
drHuangMHT Apr 14, 2023
c3d3038
Merge branch 'rename-identity' of https://github.com/drHuangMHT/rust-…
drHuangMHT Apr 14, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions core/src/signed_envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl SignedEnvelope {
use quick_protobuf::MessageWrite;

let envelope = proto::Envelope {
public_key: self.key.to_protobuf_encoding(),
public_key: self.key.encode_protobuf(),
payload_type: self.payload_type,
payload: self.payload,
signature: self.signature,
Expand All @@ -101,7 +101,7 @@ impl SignedEnvelope {
proto::Envelope::from_reader(&mut reader, bytes).map_err(DecodeError::from)?;

Ok(Self {
key: PublicKey::from_protobuf_encoding(&envelope.public_key)?,
key: PublicKey::try_decode_protobuf(&envelope.public_key)?,
payload_type: envelope.payload_type.to_vec(),
payload: envelope.payload.to_vec(),
signature: envelope.signature.to_vec(),
Expand Down
27 changes: 26 additions & 1 deletion identity/src/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,22 @@ impl SecretKey {
}

/// Decode a secret key from a byte buffer.
#[deprecated(
since = "0.2.0",
note = "This method name does not follow Rust naming conventions, use `SecretKey::try_from_bytes` instead"
)]
pub fn from_bytes(buf: &[u8]) -> Result<Self, DecodingError> {
SigningKey::from_bytes(buf)
.map_err(|err| DecodingError::failed_to_parse("ecdsa p256 secret key", err))
.map(SecretKey)
drHuangMHT marked this conversation as resolved.
Show resolved Hide resolved
}

/// Try to parse a secret key from a byte buffer.
pub fn try_from_bytes(buf: impl AsRef<[u8]>) -> Result<SecretKey, DecodingError> {
SigningKey::from_bytes(buf.as_ref())
.map_err(|err| DecodingError::failed_to_parse("ecdsa p256 secret key", err))
.map(SecretKey)
}
}

impl fmt::Debug for SecretKey {
Expand All @@ -136,7 +147,16 @@ impl PublicKey {
}

/// Decode a public key from a byte buffer without compression.
#[deprecated(
since = "0.2.0",
note = "This method name does not follow Rust naming conventions, use `PublicKey::try_from_bytes` instead."
)]
pub fn from_bytes(k: &[u8]) -> Result<PublicKey, DecodingError> {
Self::try_from_bytes(k)
}

/// Decode a public key from a byte buffer without compression.
pub fn try_from_bytes(k: &[u8]) -> Result<PublicKey, DecodingError> {
let enc_pt = EncodedPoint::from_bytes(k)
.map_err(|e| DecodingError::failed_to_parse("ecdsa p256 encoded point", e))?;

Expand All @@ -158,10 +178,15 @@ impl PublicKey {

/// Decode a public key into a DER encoded byte buffer as defined by SEC1 standard.
pub fn decode_der(k: &[u8]) -> Result<PublicKey, DecodingError> {
drHuangMHT marked this conversation as resolved.
Show resolved Hide resolved
Self::try_decode_der(k)
}

/// Decode a public key into a DER encoded byte buffer as defined by SEC1 standard.
pub fn try_decode_der(k: &[u8]) -> Result<PublicKey, DecodingError> {
let buf = Self::del_asn1_header(k).ok_or_else(|| {
DecodingError::failed_to_parse::<Void, _>("ASN.1-encoded ecdsa p256 public key", None)
})?;
Self::from_bytes(buf)
Self::try_from_bytes(buf)
}

// ecPublicKey (ANSI X9.62 public key type) OID: 1.2.840.10045.2.1
Expand Down
55 changes: 50 additions & 5 deletions identity/src/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,19 @@ impl Keypair {
/// produced by [`Keypair::encode`], zeroing the input on success.
///
/// Note that this binary format is the same as `ed25519_dalek`'s and `ed25519_zebra`'s.
#[deprecated(
since = "0.2.0",
note = "This method name does not follow Rust naming conventions, use `Keypair::try_from_bytes` instead."
)]
pub fn decode(kp: &mut [u8]) -> Result<Keypair, DecodingError> {
Self::try_from_bytes(kp)
}

/// Decode a keypair from the [binary format](https://datatracker.ietf.org/doc/html/rfc8032#section-5.1.5)
/// produced by [`Keypair::encode`], zeroing the input on success.
drHuangMHT marked this conversation as resolved.
Show resolved Hide resolved
///
/// Note that this binary format is the same as `ed25519_dalek`'s and `ed25519_zebra`'s.
pub fn try_from_bytes(kp: &mut [u8]) -> Result<Keypair, DecodingError> {
ed25519::Keypair::from_bytes(kp)
.map(|k| {
kp.zeroize();
Expand All @@ -70,7 +82,7 @@ impl Keypair {

/// Get the secret key of this keypair.
pub fn secret(&self) -> SecretKey {
SecretKey::from_bytes(&mut self.0.secret.to_bytes())
SecretKey::try_from_bytes(&mut self.0.secret.to_bytes())
.expect("ed25519::SecretKey::from_bytes(to_bytes(k)) != k")
}
}
Expand All @@ -86,7 +98,7 @@ impl fmt::Debug for Keypair {
impl Clone for Keypair {
fn clone(&self) -> Keypair {
let mut sk_bytes = self.0.secret.to_bytes();
let secret = SecretKey::from_bytes(&mut sk_bytes)
let secret = SecretKey::try_from_bytes(&mut sk_bytes)
.expect("ed25519::SecretKey::from_bytes(to_bytes(k)) != k")
.0;

Expand Down Expand Up @@ -164,12 +176,32 @@ impl PublicKey {

/// Encode the public key into a byte array in compressed form, i.e.
/// where one coordinate is represented by a single bit.
#[deprecated(
since = "0.2.0",
note = "This method name does not follow Rust naming conventions, use `PublicKey::to_bytes` instead."
)]
pub fn encode(&self) -> [u8; 32] {
self.to_bytes()
}

/// Encode the public key into a byte array in compressed form, i.e.
/// where one coordinate is represented by a single bit.
pub fn to_bytes(&self) -> [u8; 32] {
self.0.to_bytes()
}

/// Decode a public key from a byte array as produced by `encode`.
///
#[deprecated(
since = "0.2.0",
note = "This method name does not follow Rust naming conventions, use `PublicKey::try_from_bytes` instead."
)]
pub fn decode(k: &[u8]) -> Result<PublicKey, DecodingError> {
Self::try_from_bytes(k)
}

/// Decode a public key from a byte array as produced by `encode`.
drHuangMHT marked this conversation as resolved.
Show resolved Hide resolved
pub fn try_from_bytes(k: &[u8]) -> Result<PublicKey, DecodingError> {
ed25519::PublicKey::from_bytes(k)
.map_err(|e| DecodingError::failed_to_parse("Ed25519 public key", e))
.map(PublicKey)
Expand All @@ -189,7 +221,8 @@ impl AsRef<[u8]> for SecretKey {
impl Clone for SecretKey {
fn clone(&self) -> SecretKey {
let mut sk_bytes = self.0.to_bytes();
Self::from_bytes(&mut sk_bytes).expect("ed25519::SecretKey::from_bytes(to_bytes(k)) != k")
Self::try_from_bytes(&mut sk_bytes)
.expect("ed25519::SecretKey::from_bytes(to_bytes(k)) != k")
}
}

Expand All @@ -214,7 +247,19 @@ impl SecretKey {
/// Create an Ed25519 secret key from a byte slice, zeroing the input on success.
/// If the bytes do not constitute a valid Ed25519 secret key, an error is
/// returned.
#[deprecated(
since = "0.2.0",
note = "This method name does not follow Rust naming conventions, use `SecretKey::try_from_bytes` instead."
)]
#[allow(unused_mut)]
pub fn from_bytes(mut sk_bytes: impl AsMut<[u8]>) -> Result<SecretKey, DecodingError> {
Self::try_from_bytes(sk_bytes)
}

/// Create an Ed25519 secret key from a byte slice, zeroing the input on success.
/// If the bytes do not constitute a valid Ed25519 secret key, an error is
/// returned.
pub fn try_from_bytes(mut sk_bytes: impl AsMut<[u8]>) -> Result<SecretKey, DecodingError> {
let sk_bytes = sk_bytes.as_mut();
let secret = ed25519::SecretKey::from_bytes(&*sk_bytes)
.map_err(|e| DecodingError::failed_to_parse("Ed25519 secret key", e))?;
Expand All @@ -237,7 +282,7 @@ mod tests {
fn prop() -> bool {
let kp1 = Keypair::generate();
let mut kp1_enc = kp1.encode();
let kp2 = Keypair::decode(&mut kp1_enc).unwrap();
let kp2 = Keypair::try_from_bytes(&mut kp1_enc).unwrap();
eq_keypairs(&kp1, &kp2) && kp1_enc.iter().all(|b| *b == 0)
}
QuickCheck::new().tests(10).quickcheck(prop as fn() -> _);
Expand All @@ -248,7 +293,7 @@ mod tests {
fn prop() -> bool {
let kp1 = Keypair::generate();
let mut sk = kp1.0.secret.to_bytes();
let kp2 = Keypair::from(SecretKey::from_bytes(&mut sk).unwrap());
let kp2 = Keypair::from(SecretKey::try_from_bytes(&mut sk).unwrap());
eq_keypairs(&kp1, &kp2) && sk == [0u8; 32]
}
QuickCheck::new().tests(10).quickcheck(prop as fn() -> _);
Expand Down
25 changes: 25 additions & 0 deletions identity/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
use std::error::Error;
use std::fmt;

use crate::KeyType;

/// An error during decoding of key material.
#[derive(Debug)]
pub struct DecodingError {
Expand Down Expand Up @@ -156,3 +158,26 @@ impl Error for SigningError {
self.source.as_ref().map(|s| &**s as &dyn Error)
}
}

/// Error produced when failing to convert `libp2p_identity::Keypair` to a more concrete keypair.
drHuangMHT marked this conversation as resolved.
Show resolved Hide resolved
#[derive(Debug)]
pub struct OtherVariantError {
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved
actual: KeyType,
}

impl OtherVariantError {
pub fn new(actual: KeyType) -> OtherVariantError {
drHuangMHT marked this conversation as resolved.
Show resolved Hide resolved
OtherVariantError { actual }
}
}

impl fmt::Display for OtherVariantError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&format!(
"Cannot convert to the given type, the actual key type inside is {}",
self.actual
))
}
}

impl Error for OtherVariantError {}
Loading