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

identity: make Keypair and Publickey opaque #3860

Closed
thomaseizinger opened this issue May 1, 2023 · 2 comments · Fixed by #3866
Closed

identity: make Keypair and Publickey opaque #3860

thomaseizinger opened this issue May 1, 2023 · 2 comments · Fixed by #3866

Comments

@thomaseizinger
Copy link
Contributor

Description

As already announced in some deprecation warnings, we want to encapsulate the KeyPair and PublicKey types to not expose enums anymore but just structs. This allows us to contain the cfg statements in our code and not bother our users with it.

Are you planning to do it yourself in a pull request?

Maybe, unless someone beats me to it.

@folex
Copy link
Contributor

folex commented Jun 22, 2023

Hi!

That breaks some of our code: it was converting from libp2p:_identity::Keypair to our own KeyPair structure that defines stricter and simpler serialization.

But now, we're not able to match on the key type. Instead, we have to use try_into 3 times, which internally uses match 3 times instead of a single time. And the code looks... harder to read.

Instead of

match keypair {
  Ed25519(kp) => KeyPair::Ed25519(decode(kp)),
  Rsa(kp) => KeyPair::RSA(transmute(kp)),
  Secp256k1(kp) => KeyPair::Secp256k1(kp.into()),
}

We now have to do

impl From<libp2p_identity::Keypair> for KeyPair {
    fn from(key: libp2p_identity::Keypair) -> Self {
        key.try_into_ed25519().map(|kp| {
            KeyPair::Ed25519(decode(kp))
        }).or_else(
            key.try_into_rsa().map(|kp| KeyPair::RSA(transmute))
        ).or_else(key.try_into_secp256k1().map(|kp| {
            KeyPair::Secp256k1(kp.into())
        })).unwrap()
    }
}

This is less effective, because of all the matching and error handling underneath, and readability is far far worse.

Is there any chance you could at least expose key_type so we can know the key type, and match on that?

P.S. same holds for PublicKey

@thomaseizinger
Copy link
Contributor Author

See #3649.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants