Skip to content

Commit

Permalink
Add sr25519 keys to the enums
Browse files Browse the repository at this point in the history
  • Loading branch information
i1i1 committed May 23, 2022
1 parent 0bd7eaa commit 76ea3c8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
39 changes: 39 additions & 0 deletions core/src/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ pub enum Keypair {
/// An ECDSA keypair.
#[cfg(feature = "ecdsa")]
Ecdsa(ecdsa::Keypair),
/// An Sr25519 keypair.
#[cfg(feature = "ecdsa")]
Sr25519(sr25519::Keypair),
}

impl Keypair {
Expand Down Expand Up @@ -129,6 +132,8 @@ impl Keypair {
Secp256k1(ref pair) => pair.secret().sign(msg),
#[cfg(feature = "ecdsa")]
Ecdsa(ref pair) => Ok(pair.secret().sign(msg)),
#[cfg(feature = "sr25519")]
Sr25519(ref pair) => Ok(pair.sign(msg)),
}
}

Expand All @@ -143,6 +148,8 @@ impl Keypair {
Secp256k1(pair) => PublicKey::Secp256k1(pair.public().clone()),
#[cfg(feature = "ecdsa")]
Ecdsa(pair) => PublicKey::Ecdsa(pair.public().clone()),
#[cfg(feature = "sr25519")]
Sr25519(pair) => PublicKey::Sr25519(pair.public()),
}
}

Expand Down Expand Up @@ -173,6 +180,11 @@ impl Keypair {
"Encoding ECDSA key into Protobuf is unsupported",
))
}
#[cfg(feature = "sr25519")]
Self::Sr25519(data) => keys_proto::PrivateKey {
r#type: keys_proto::KeyType::Sr25519.into(),
data: data.encode().into(),
},
};

Ok(pk.encode_to_vec())
Expand All @@ -194,6 +206,14 @@ impl Keypair {
keys_proto::KeyType::Ed25519 => {
ed25519::Keypair::decode(&mut private_key.data).map(Keypair::Ed25519)
}
#[cfg(feature = "sr25519")]
keys_proto::KeyType::Sr25519 => {
sr25519::Keypair::decode(&mut private_key.data).map(Keypair::Sr25519)
}
#[cfg(not(feature = "sr25519"))]
keys_proto::KeyType::Sr25519 => Err(DecodingError::new(
"Decoding Sr25519 key from Protobuf is unsupported.",
)),
keys_proto::KeyType::Rsa => Err(DecodingError::new(
"Decoding RSA key from Protobuf is unsupported.",
)),
Expand Down Expand Up @@ -228,6 +248,9 @@ pub enum PublicKey {
/// A public ECDSA key.
#[cfg(feature = "ecdsa")]
Ecdsa(ecdsa::PublicKey),
/// A public Sr25519 key.
#[cfg(feature = "sr25519")]
Sr25519(sr25519::PublicKey),
}

impl PublicKey {
Expand All @@ -246,6 +269,8 @@ impl PublicKey {
Secp256k1(pk) => pk.verify(msg, sig),
#[cfg(feature = "ecdsa")]
Ecdsa(pk) => pk.verify(msg, sig),
#[cfg(feature = "sr25519")]
Sr25519(pk) => pk.verify(msg, sig),
}
}

Expand Down Expand Up @@ -302,6 +327,11 @@ impl From<&PublicKey> for keys_proto::PublicKey {
r#type: keys_proto::KeyType::Ecdsa as i32,
data: key.encode_der(),
},
#[cfg(feature = "sr25519")]
PublicKey::Sr25519(key) => keys_proto::PublicKey {
r#type: keys_proto::KeyType::Sr25519 as i32,
data: key.encode().to_vec(),
},
}
}
}
Expand All @@ -326,6 +356,15 @@ impl TryFrom<keys_proto::PublicKey> for PublicKey {
log::debug!("support for RSA was disabled at compile-time");
Err(DecodingError::new("Unsupported"))
}
#[cfg(feature = "sr25519")]
keys_proto::KeyType::Sr25519 => {
sr25519::PublicKey::decode(&pubkey.data).map(PublicKey::Sr25519)
}
#[cfg(not(feature = "sr25519"))]
keys_proto::KeyType::Sr25519 => {
log::debug!("support for Sr25519 was disabled at compile-time");
Err(DecodingError::new("Unsupported"))
}
#[cfg(feature = "secp256k1")]
keys_proto::KeyType::Secp256k1 => {
secp256k1::PublicKey::decode(&pubkey.data).map(PublicKey::Secp256k1)
Expand Down
1 change: 1 addition & 0 deletions core/src/keys.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ enum KeyType {
Ed25519 = 1;
Secp256k1 = 2;
ECDSA = 3;
Sr25519 = 4;
}

message PublicKey {
Expand Down

0 comments on commit 76ea3c8

Please sign in to comment.