fix(keypair): deserialize libp2p keypair from secret key (#116)

* fix: Ed25519 keypair

* Update keypair/src/key_pair.rs

Co-authored-by: folex <0xdxdy@gmail.com>

---------

Co-authored-by: folex <0xdxdy@gmail.com>
This commit is contained in:
Nick 2023-07-04 19:47:26 +03:00 committed by GitHub
parent c22eab38c1
commit ee330a715a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 1 deletions

View File

@ -201,6 +201,7 @@ pub struct Signature(pub Vec<u8>);
#[cfg(test)]
mod tests {
use super::*;
use crate::KeyPair;
use quickcheck::*;
fn eq_keypairs(kp1: &Keypair, kp2: &Keypair) -> bool {
@ -218,6 +219,17 @@ mod tests {
QuickCheck::new().tests(10).quickcheck(prop as fn() -> _);
}
#[test]
fn ed25519_keypair_convert() {
fn prop() -> bool {
let kp1 = KeyPair::generate_ed25519();
let libp2p_kp: libp2p_identity::Keypair = kp1.clone().into();
let kp2: KeyPair = libp2p_kp.into();
kp1.public() == kp2.public() && kp1.secret().unwrap() == kp2.secret().unwrap()
}
QuickCheck::new().tests(10).quickcheck(prop as fn() -> _);
}
#[test]
fn ed25519_keypair_from_secret() {
fn prop() -> bool {

View File

@ -283,7 +283,9 @@ impl From<KeyPair> for libp2p_identity::Keypair {
fn convert_keypair(key: KeyPair) -> eyre::Result<libp2p_identity::Keypair> {
match key {
KeyPair::Ed25519(kp) => {
let kp = Keypair::ed25519_from_bytes(kp.encode().to_vec().as_mut_slice())?;
// for some reason, libp2p takes SecretKey's 32 bytes here instead of Keypair's 64 bytes
let secret_bytes = kp.secret().0.to_bytes();
let kp = libp2p_identity::Keypair::ed25519_from_bytes(secret_bytes)?;
Ok(kp)
}
#[cfg(not(target_arch = "wasm32"))]