-
Notifications
You must be signed in to change notification settings - Fork 976
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::Keypair
ergonomics improvements
#3649
Comments
What is Generally, the path forward from here is to add whatever |
Working on #3681 to solve this. Discussion welcome. |
in LH I don't think we do anything by value, but I don't think it's an unrealistic scenario to have
Thanks! looking forward to this |
Okay thanks! Please open a new issue once we have something actionable. |
What's the point of closing this @thomaseizinger. I raised a couple of problems. Access by value is just one of them, access by reference is already happening in our repo and there is someone working on improving this |
It would be more productive to address the proposed solutions and weight in which sounds more reasonable in each case |
I closed it because you said you don't have a usecase? What are you accessing the individual keypairs for that you can't do via the composed one? |
Just re-read your comment and I missed the "by value". My overall point still stands though: You should be using the Keypair and its functions and not the individual variants. Can you point me to code that is currently accessing the individual variants? |
Yeah I thought we did for accessing individual keys by value, but we don't. We do need to access individual keys by reference. This is since Ethereum node records have defined obtaining a peer id for two of the keys. Before the deprecation this would be a simple match over a reference. This now requires a clone and it's an operation that occurs at least every time a peer connects. This is less performant now beside the simple lack of convenience we had before. Happy to implement the required methods. As I said in the issue, we can either do an AsRef impl or add the as_keyname following the same convention of the new into_keyname methods. Just need you people to pick what you prefer |
Some keys can't be accessed by reference because of internal details, like RSA keys and Ed25519 keys. And currently top-level |
Are you talking about this code here? Funnily enough, it is code like this why I want to make the #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
Keypair::Rsa(_) => Err("rsa keypairs not supported"), This is very hard to get right and I don't expect people to. Which is why I want to hide hit :) In this case, the In regards to the actual usecase, I want to say two things:
To make things more ergonomic, I'd suggest that we add our own enum KeyType {
Ed25519,
Rsa,
Secp256k1,
Ecdsa
} That allows you to correctly call the corresponding Unrelated feedback: Instead of taking the first 32-bytes of the "encoding", you are probably better of calling |
Once #3775 lands, we have a |
@divagant-martian Would you be willing to contribute a PR that exposes a |
Hi @thomaseizinger due to life changes since the past month and at least for one more month I won't have time to contribute to libp2p, sorry |
No worries at all, thanks for letting me know and hopefully all is right :) |
@thomaseizinger, hello :) Could you please elaborate on how do you envision the exposition? impl Keypair {
fn key_type(&self) -> KeyType {
match self.inner {
Ed25519(_) => KeyType::Ed25519,
...
}
}
} Something like that? |
Yep that looks good! |
Resolves #3649. Pull-Request: #4107. Co-Authored-By: Nick Pavlov <[email protected]> Co-Authored-By: Thomas Eizinger <[email protected]>
a patch with this would be indeed welcomed! |
Motivation
After #3350 a couple of things that were easily achievable with a match statement are convoluted/potentially less performant.
Simple examples:
Accessing the kind of key by value when supporting more than one kind requires clone
Getting the value of the key while supporting more than one key type
Now this requires a clone since
into_secp256k1
consumes the valueAccessing the kind of key by reference
Now this requires a clone since
into_secp256k1
consumes the valueSupporting a new key kind
Before: Enable the feature and compiler would tell of a missing match branch.
Now: Enable feature and dig in the code where the
into_keykind()
are, to add a new oneProposed sol
Assuming the code base is going ahead with this deprecation,
for 1: Implement
TryFrom
/TryInto
for the necessary types. Alternative, add the matchingtry_into_secp256k1
and others. In these, the error type should give back the original key to be used.for 2: Implement
AsRef
for the necessary types. Alternative, add the matchingas_secp245k1()
and othersfor 3. Can't see a good solution right now
Are you planning to do it yourself in a pull request?
Maybe.
The text was updated successfully, but these errors were encountered: