-
Notifications
You must be signed in to change notification settings - Fork 622
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
Inconsistency: PrivateKey
tracks network, but PublicKey
doesn't
#1008
Comments
It does seem that way, but there's nothing we can do. There are standard encodings for both and the encodings are inconsistent in the amount of data they contain. |
We could drop |
"Key" != "Encoding of a Key" |
I initially agreed with @Kixunil's suggestion. But now the more I think about it the |
Should One way or another the documentation should explain the meaning and role of these types, and why are they different from the underlying types, in part for educational purposes, in part to settle this issue. |
No, but you need to be able to obtain a key from the encoding of a key, and the encoding does not include the network. Similarly if we drop the network from the private key, then we'll be unable to encode it. |
When I think about it: most if not all Bitcoin applications that I ever used picks one network that it will handle very, very early. Mobile ones are typically whole separate app for testnet/mainnet. No app ever supports multiple networks at once, because Bitcoin node supports only one at the time, and it would just be confusing and error prone. Storing the network data on every single private key, even though it will be the same value 100% of the time, just because WIP has it encoded seems really backwards. You can still encode network + key: impl PrivateKey {
pub fn to_wif(&self, network: Network) -> String {
WIFKey { key: self, network }.to_string()
}
pub from_wif(wif_str: &str) -> (Network, Self) { ... }
} Additional type gives opportunity to document what WIF is, give https://docs.rs/bitcoin reader some links and so on. |
Yeah, that's what I meant by "in tuple when deserializing from WIF/accept as argumment when serializing into WIF" WIF key sounds kinda good but I was thinking maybe also deriving a key using BIP32 could carry the network because xprivs do. |
Ok, I think I'm convinced. Can we keep around |
I almost agree to having a public POD type, I just wonder if |
I'd be fine adding convenience methods for both equality checking and for changing the network field, on an otherwise-POD type. There's a temptation to try to go down the "express the network with the type system" but I'm not optimistic that that'd be fruitful. |
I think network in the type system would just encourage people to hardcode mainnet which is pretty bad. There's already a quite popular project which I won't name that only supports mainnet and it worries me quite a bit. (How) do they test it?! Also forgot to mention I'd like to see a method to derive address directly so people won't mess up networks. |
FWIW I routinely test code on mainnet, at least when fees are cheap. It keeps me focused on writing bug-free code ;) |
I assume it's not your only testing strategy and/or you don't expose less-tested code to unaware users. |
Correct :P. At Blockstream we have real regtest-based testing infrastructure that actually does use the |
|
I believe he meant |
Ah, I see. Imo, 99% not worth the trouble. This bound would infect all outter-types in a code that wants to handle any network. |
I'm not so worried about it affecting outer types (the network is not encoded in any data on the p2p layer or blockchain), but I do think it'd have a high ergonomic cost for not a ton of benefit. Usually you want to support different networks and switch at runtime (even if "at runtime" just means "at startup"). |
Exactly. |
Just to recap current consensus (tell me if I'm wrong):
Question: What about This is only really used in the context of WIF encoding and deciding whether to make a derived PublicKey also be |
@junderw I believe the idea was to use tuples, not newtype for
If we do that we can remove |
I guess it does make sense to keep passing the compressed bool around through all the different conversions (ie. bip32 doesn't allow uncompressed pubkeys, so being able to only output PrivateKeys with compressed as true makes sense) I was just trying to figure out the use case for it, and only saw WIF encoding and PublicKey creation. |
I'm aware of CompressedPublicKey. I was talking about PrivateKey. If I create a private key from a bip32 extended key and then try to use that to create an uncompressed PublicKey, something is wrong... so having the ability to output a PrivateKey with the compressed == true already inside of it has value for that use case, is what I'm saying. |
Well, we probably also should have |
We have had a lot of discussion re Can we close this issue? |
Yeah, let's close this. We have rough agreement that we want some sort of |
This is surprising and I wonder if it helps with anything. Supposedly the idea is to ensure that WIF-imported key would create correct address but this doesn't work since
PublicKey
doesn't containNetwork
. Is there any reason to do it like that?It seems either both should contain
Network
or none.The text was updated successfully, but these errors were encountered: