Ready for Review: Introduce util::key and deprecate util::privkey#183
Ready for Review: Introduce util::key and deprecate util::privkey#183apoelstra merged 5 commits intorust-bitcoin:masterfrom
Conversation
src/util/key.rs
Outdated
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
What do you mean by this? Do you mean that we should eliminate PrivateKey::to_{legacy_,}address?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
@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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
A bit like how an Address is different from a PublicKey, a Wif could be different from a PrivateKey.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
|
Can you please break this up into multiple commits? At least,
|
|
Should we future-proof this wrt Schnorr signatures coming in the (hopefully) near future? |
|
We cannot future proof against a future output version with semantics we don't know yet. |
0879209 to
3a7b7e6
Compare
3a7b7e6 to
84b16a0
Compare
|
So I think when we concluded discussions about this, my personal conclusion was that I'd take |
|
@stevenroose you mean take a network argument for encoding to WIF? What about decoding from WIF? |
|
@apoelstra This is ready for review BTW |
|
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
There was a problem hiding this comment.
how to grammar
not even sure what this is saying
|
What's the latest on the holdup for this one? |
58aff07 to
d6ecba0
Compare
|
Addressed nits from @instagibbs and rebased. |
sgeisler
left a comment
There was a problem hiding this comment.
Looks good except some confusing variable names.
d6ecba0 to
4dfab0e
Compare
|
Like I mentioned, I personally feel that an approach like #221 makes more sense. No hard feelings if this gets merged, though :) |
sgeisler
left a comment
There was a problem hiding this comment.
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.
4dfab0e to
966597c
Compare
|
Rebased and ready for review! |
ariard
left a comment
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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 ?)
There was a problem hiding this comment.
I think you were looking at an outdated hunk, it's been changed :-)
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
By "proposed use" what is mean there ? Being able to export it as WIF compressed pubkey ? Could be clarified :)
966597c to
5fdf9e6
Compare
|
Fixed doc nit from @ariard |
|
@tamasblummer @ariard Please re-ACK, perhaps https://git-scm.com/docs/git-range-diff would make your life easier too |
ariard
left a comment
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
At top on 5fdf9e6, you can still prune bool and io::Result<()> type ascriptions
|
@ariard Let's fix the ascription stuff after merge! |
sgeisler
left a comment
There was a problem hiding this comment.
Renewing previous ACK after rebase.
- 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.
a944c7f
5fdf9e6 to
a944c7f
Compare
|
ACK pending travis |
sgeisler
left a comment
There was a problem hiding this comment.
Renewing previous ACK after rebase.
…itcoin#185) PR rust-bitcoin#180 used `MultiPeerNetworkManager`, this was renamed in rust-bitcoin#183 which was merged before rust-bitcoin#180 but after its CI run.
…-bitcoin#197) * feat(spv): flush headers on shutdown * move fn lower in the impl * refactor: `MultiPeerNetworkManager` -> `PeerNetworkManager` (rust-bitcoin#184) * refactor: `MultiPeerNetworkManager` -> `PeerNetworkManager` * Fix formatting and apply review * feat: Update ffi headers (rust-bitcoin#183) * feat(spv): broadcast transaction (rust-bitcoin#180) * fix: Fix `PeerNetworkManager` cast in `broadcast_transaction` (rust-bitcoin#185) PR rust-bitcoin#180 used `MultiPeerNetworkManager`, this was renamed in rust-bitcoin#183 which was merged before rust-bitcoin#180 but after its CI run. * fix: Use non-blocking `TcpStream` in `dash-spv::network::TcpConnection` (rust-bitcoin#188) * refactor: Improve SPV shutdown handling with `CancellationToken` (rust-bitcoin#187) * refactor: `TcpConnection` -> `Peer` and `ConnectionPool` -> `PeerPool` (rust-bitcoin#190) * fix: Locking issue after rust-bitcoin#190 (rust-bitcoin#191) rust-bitcoin#190 removed the read timeouts of the `Peer::receive_message` which currently leads to a lockup of the peer because the write lock is held while waiting for the message. Needs some more refactoring but this works for now. * fix: More follow-up to rust-bitcoin#190 (rust-bitcoin#193) The sleep timeout branch introduced in rust-bitcoin#191 returns an `Err(NetworkError::Timeout)` which leads to a misbehavior update below in the `msg_result` match and eventually in a peer ban. This shouldn't happen because the `sleep` timing out only means that there is no data available right now. Instead, it now returns `Ok(None)` which will just keep things going. * flush after mn list sync too * move flush to after header sync instead of mnlist * fix --------- Co-authored-by: Kevin Rombach <35775977+xdustinface@users.noreply.github.com>
util::key::PublicKey.
@apoelstra @stevenroose I've introduced a few
.clone()'sthat I would like to remove, and am not entirely happy about the copying that happens inutil::key::PublicKey::serialize. Would love your opinions on how to design this better.