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

[Alternative] Allow deserializing from owned types #218

Closed
wants to merge 4 commits into from

Conversation

elichai
Copy link
Member

@elichai elichai commented May 15, 2020

This is an alternative to #217
I removed the macro because only SecretKey used it, trying to remerge all of the implementations into a macro has some problems because some of them use specific functions(from_der() / serialize() etc.) we could add it back it a complicated way (or implement a private serialize function for all the types that use whatever is needed underneath and use that in the macro)

I used the implementation for PublicKey to replace the one in SecretKey and Signature (implemented a similar one, just to realize PublicKey has it's own one that does the same)
And added tests that check that (they should fail before this change and pass after)

Anyone that actually uses it please test that there's no difference in the actual input/output in the serializer you're using (it shouldn't, but worth testing against multiple common serializers)

@sgeisler
Copy link
Contributor

It's definitely less hacky than my #217, but introduces quite some code duplication. I'd like to propose to unify the visitors as implemented in sgeisler@6356ba4.

@elichai
Copy link
Member Author

elichai commented May 19, 2020

I'd like to propose to unify the visitors as implemented in sgeisler@6356ba4.

Thanks! cherry-picked it modulo some formatting and small changes for readability :)

@thomaseizinger
Copy link
Contributor

@elichai Can you rebase this so it is mergable again?

@apoelstra Any chance we can get this merged? Not being able to deserialize from owned types is rather inconvenient in some situations.

Copy link
Member

@tcharding tcharding left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aside from question, looks good to me.

serde_impl!(SecretKey, constants::SECRET_KEY_SIZE);

#[cfg(feature = "serde")]
impl ::serde::Serialize for SecretKey {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this is encouraging user to serialize their secrets. Do we really want to be doing that?

Copy link
Contributor

@thomaseizinger thomaseizinger Jan 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As an alternative, we could provide a module so users have to explicitly opt-in to this by saying #[serde(with = "secp256k1::serde::secret_key")]. This has the downside that we need to provide a module for Option and Vec as well if we want it to work out of the box with those containers.

However, I am not sure if this makes things actually better? Some situations require secret keys to be serialized (a backup functionality for example). Additionally, SecretKey also implements Debug and Display which are basically the same.

I am not convinced that we should be introducing a policy on this abstraction level. Whether or not serializing a secret key is dangerous depends completely on the application.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(At least as long as we don't have HD derivation natively in this library), serializing secret keys is essential. Keys need to be stored.

In one case, the caller has the secret key in serialized form already and just passes it to the library. Then letting the user serialize keys won't hurt either.

In the other case, the caller relies on the library to generate (or tweak) keys. Then it's essential to serialize them for storage.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, concept ACK having serialization. You need to store secret key material somehow.

This PR needs a rebase though.

@sgeisler
Copy link
Contributor

This PR requires some heavier lifting by now. Back then we chose to remove the serde_impl[_from_slice] macros since each was only used once, needleslly obscuring the code. But now they are also used for schnorr keys, so I think we should leave them in and rework this PR so it changes the macros instead.

@elichai
Copy link
Member Author

elichai commented Jan 11, 2021

Yeah after schnorr and other changes this PR doesn't have a clean path for a rebase and requires some re-thinking and balance between simplicity and readability and code duplication(adding back macros).

If anyone wants to take this feel free, otherwise I'll try to find some time for it this weekend / next week

@thomaseizinger
Copy link
Contributor

thomaseizinger commented Jan 12, 2021

If anyone wants to take this feel free

@elichai I had a go at this and opened a PR here: #270. Let me know what you think in terms of simplicity, code duplication etc.

@elichai
Copy link
Member Author

elichai commented Jan 12, 2021

Looks good

@elichai elichai closed this Jan 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants