8000 Ready for Review: Introduce util::key and deprecate util::privkey by dongcarl · Pull Request #183 · rust-bitcoin/rust-bitcoin · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Ready for Review: Introduce util::key and deprecate util::privkey #183

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

Merged
merged 5 commits into from
Feb 11, 2019

Conversation

dongcarl
Copy link
Member
  • Move util::privkey::PrivKey to util::key::PrivateKey.
  • Clean up util::key::PrivateKey.
  • Add new util::key::PublicKey struct.
  • Modify util::address::Address to appropriately wrap new
    util::key::PublicKey.

@apoelstra @stevenroose I've introduced a few .clone()'s that I would like to remove, and am not entirely happy about the copying that happens in util::key::PublicKey::serialize. Would love your opinions on how to design this better.

src/util/key.rs Outdated
#[inline]
pub fn is_compressed(&self) -> bool {
self.compressed
Address::p2pkh(self.public_key(secp), self.network)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Given the amount of ways you can go from a key to an address, I think it might be better to just have the explicit Address constructors and no getters here at all.

Copy link
Member Author

Choose a reason for hiding this comment

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

What do you mean by this? Do you mean that we should eliminate PrivateKey::to_{legacy_,}address?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah. I mean there is p2pk, p2pkh, p2wpkh. Which one is "address" and which one is "legacy address"? Every time there is a new way to make an address from a key, this will get more confusing. The Address constructors are explicit and very clear.

If you really care about the getters on the key, I'd make them more explicit. Like to_p2pkh_address, ... But I think the constructors are sufficient.

Copy link
Member Author

Choose a reason for hiding this comment

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

@stevenroose Agreed.

Copy link
Member Author

Choose a reason for hiding this comment

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

@stevenroose Actually... I've thought about it a little... It's a little un-ergonomic that we would have the use the constructor like so: Address::p2pk(sk.public_key(&secp), sk.network).

Perhaps we should do PrivateKey::to_p2pkh_address(&secp) etc., just for ergonomics sake.

Copy link
Member

Choose a reason for hiding this comment

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

@stevenroose correct

Copy link
Collaborator

Choose a reason for hiding this comment

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

I feel like that's kind of a pity. I mean WIF is just an "import format", right? I feel like it would be perfectly acceptable to have wif just as a util/wif.rs thing for compatibility sake if it's actually being annoying wrt other things.

Copy link
Collaborator

Choose a reason for hiding this comment

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

A bit like how an Address is different from a PublicKey, a Wif could be different from a PrivateKey.

Copy link
Member

Choose a reason for hiding this comment

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

We could do that. It would mean that you couldn't serialize a PrivateKey without converting to a WIf first, and you couldn't do that without knowing your network (which you'd need to obtain from somewhere)...and you'd then be unable to obtain an address from a PrivateKey without doing the same thing ... so the PrivateKey type becomes very unergonomic.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah it becomes basically useless because of secp256k1::key::SecretKey. But that's not necessarily a problem. Address::p2pkh(secp_priv.public_key(), network) doesn't seem wrong though (considering context-less priv-pub conversion).
And you really only need WIF strings when you actually want WIF strings, no? bitcoin::utils::wif::to_wif(my_pk, network) also not.

Question: will the secp256k1 SecretKey and PublicKey look different for Schnorr?

@apoelstra
Copy link
Member

Can you please break this up into multiple commits? At least,

  • One that renames Privkey to PrivateKey
  • One that adds the public key field to Address
  • One that moves stuff into the key module
  • One that does everything else, if there's anything left

@stevenroose
Copy link
Collaborator

Should we future-proof this wrt Schnorr signatures coming in the (hopefully) near future?

@apoelstra
Copy link
Member

We cannot future proof against a future output version with semantics we don't know yet.

@dongcarl dongcarl force-pushed the 2018-9-pubkey-wrapper branch 2 times, most recently from 0879209 to 3a7b7e6 Compare November 16, 2018 04:33
@dongcarl dongcarl changed the title WIP: Introduce util::key and deprecate util::privkey Ready for Review: Introduce util::key and deprecate util::privkey Nov 18, 2018
@dongcarl dongcarl force-pushed the 2018-9-pubkey-wrapper branch from 3a7b7e6 to 84b16a0 Compare December 3, 2018 04:07
@stevenroose
Copy link
Collaborator

So I think when we concluded discussions about this, my personal conclusion was that I'd take network out of the private key type. And just take a network argument for the WIF method.

@dongcarl
Copy link
Member Author
dongcarl commented Dec 3, 2018

@stevenroose you mean take a network argument for encoding to WIF? What about decoding from WIF?

@dongcarl
Copy link
Member Author
dongcarl commented Dec 3, 2018

@apoelstra This is ready for review BTW

< 8000 /form>

@stevenroose
Copy link
Collaborator

Decoding could just return a privkey and a network. I don't think the network has any relevance when decoding a WIF. Perhaps as a protection measure, but that's up to the user using the WIF stuff IMO.

src/util/key.rs Outdated
@@ -32,13 +71,13 @@ pub struct PrivateKey {
/// The network on which this key should be used
pub network: Network,
/// The actual ECDSA key
pub key: SecretKey
pub key: secp256k1::SecretKey,
}

impl PrivateKey {
/// Computes the public key as supposed to be used with this secret
Copy link
Contributor

Choose a reason for hiding this comment

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

how to grammar

not even sure what this is saying

@stevenroose
Copy link
Collaborator

What's the latest on the holdup for this one?

@dongcarl
Copy link
Member Author

Addressed nits from @instagibbs and rebased.

Copy link
Contributor
@sgeisler sgeisler left a comment

Choose a reason for hiding this comment

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

Looks good except some confusing variable names.

@dongcarl dongcarl force-pushed the 2018-9-pubkey-wrapper branch from d6ecba0 to 4dfab0e Compare January 22, 2019 03:51
@stevenroose
Copy link
Collaborator

Like I mentioned, I personally feel that an approach like #221 makes more sense. No hard feelings if this gets merged, though :)

sgeisler
sgeisler previously approved these changes Jan 22, 2019
Copy link
Contributor
@sgeisler sgeisler left a comment

Choose a reason for hiding this comment

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

It's probably not the best solution we could come up with but I feel it's good enough to move forward and eventually improve incrementally on it.

@dongcarl
Copy link
Member Author

Rebased and ready for review!

Copy link
Contributor
@ariard ariard left a comment

Choose a reason for hiding this comment

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

Good to me, imo documentation could be clearer, but can go forward.

src/util/key.rs Outdated

/// Deserialize a public key from a slice
pub fn from_slice(data: &[u8]) -> Result<PublicKey, encode::Error> {
let key: secp256k1::PublicKey = secp256k1::PublicKey::from_slice(data)
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems you don't need type ascription there (1.22, 1.29.2, stable)
Same for the io::Result and bool below (or are you doing it for code readability ?)

Copy link
Member Author

Choose a reason for hiding this comment

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

I think you were looking at an outdated hunk, it's been changed :-)

Copy link
Contributor
@ariard ariard Feb 7, 2019

Choose a reason for hiding this comment

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

At top on 5fdf9e6, you can still prune bool and io::Result<()> type ascriptions

src/util/key.rs Outdated
//!
//! A private key represents the secret data associated with its proposed use
//! Structures and methods representing the public/secret data associated with
//! its proposed use
Copy link
Contributor
@ariard ariard Feb 2, 2019

Choose a reason for hiding this comment

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

By "proposed use" what is mean there ? Being able to export it as WIF compressed pubkey ? Could be clarified :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed!

tamasblummer
tamasblummer previously approved these changes Feb 2, 2019
@dongcarl dongcarl force-pushed the 2018-9-pubkey-wrapper branch from 966597c to 5fdf9e6 Compare February 4, 2019 15:28
@dongcarl
Copy link
Member Author
dongcarl commented Feb 4, 2019

Fixed doc nit from @ariard

@dongcarl
Copy link
Member Author
dongcarl commented Feb 5, 2019

@tamasblummer @ariard Please re-ACK, perhaps https://git-scm.com/docs/git-range-diff would make your life easier too

ariard
ariard previously approved these changes Feb 7, 2019
Copy link
Contributor
@ariard ariard left a comment

Choose a reason for hiding this comment

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

Good to me, just type ascription nit

src/util/key.rs Outdated

/// Deserialize a public key from a slice
pub fn from_slice(data: &[u8]) -> Result<PublicKey, encode::Error> {
let key: secp256k1::PublicKey = secp256k1::PublicKey::from_slice(data)
Copy link
Contributor
@ariard ariard Feb 7, 2019

Choose a reason for hiding this comment

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

At top on 5fdf9e6, you can still prune bool and io::Result<()> type ascriptions

@dongcarl
Copy link
Member Author
dongcarl commented Feb 7, 2019

@ariard Let's fix the ascription stuff after merge!

sgeisler
sgeisler previously approved these changes Feb 7, 2019
Copy link
Contributor
@sgeisler sgeisler left a comment

Choose a reason for hiding this comment

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

Renewing previous ACK after rebase.

tamasblummer
tamasblummer previously approved these changes Feb 9, 2019
- Rename privkey::PrivKey to privkey::PrivateKey
- Remove unnecessary methods for privkey::PrivateKey
- Modify tests to work with above changes
- Move util::privkey to util::key
- Add PublicKey struct to util::key
- Implement de/serialization methods for util::key::PublicKey
- Switch util::address::Payload::Pubkey variant to wrap
  util::key::PublicKey
- Switch util::address::Address::p*k* constructors to use
  util::key::PublicKey
- Fix tests for aforementioned switch
- Add convenience methods for util::key::PublicKey to
  util::key::PrivateKey conversion
- Switch BIP143 tests to use util::key::PublicKey
This change also moves the secp256k1::Error wrapper from util::Error to
consensus::encode::Error, since we do not use it anywhere else. We can
add it back to util::Error once we have instances of secp256k1::Error
that are not related to consensus::encode.
@dongcarl dongcarl dismissed stale reviews from tamasblummer, sgeisler, and ariard via a944c7f February 11, 2019 20:47
@dongcarl dongcarl force-pushed the 2018-9-pubkey-wrapper branch from 5fdf9e6 to a944c7f Compare February 11, 2019 20:47
@apoelstra
Copy link
Member

ACK pending travis

Copy link
Contributor
@sgeisler sgeisler left a comment

Choose a reason for hiding this comment

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

Renewing previous ACK after rebase.

@apoelstra apoelstra merged commit 8ae4aee into rust-bitcoin:master Feb 11, 2019
@dongcarl dongcarl mentioned this pull request Feb 11, 2019
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.

7 participants
0