chainside / btcpy

A Python3 SegWit-compliant library which provides tools to handle Bitcoin data structures in a simple fashion.
https://www.chainside.net
GNU Lesser General Public License v3.0
271 stars 74 forks source link

Leading zeros in ExtendedPrivateKey break PrivateKey #14

Closed l0rb closed 7 years ago

l0rb commented 7 years ago

When creating a PrivateKey from the output of serialize_key() on ExtendedPrivateKey the leading zeros that this data has break the pub() function on the PrivateKey. Code example below:

rootkey = 'tprv8....'

# this throws an error
PrivateKey(
    ExtendedPrivateKey.decode(rootkey).serialize_key()
).pub()

# this one works
PrivateKey(
    ExtendedPrivateKey.decode(rootkey).key.serialize()
).pub()
SimoneBronzini commented 7 years ago

Looking into it right now

SimoneBronzini commented 7 years ago

The serialize_key method is not intended to create regular keys from extended keys. It is defined in the ExtendedKey abstract class so that child classes have to implement a method which serialises them in the format expected by extended serialisation. serialize_key() should probably be made private. What you are trying to achieve is supposed to be done as follows:

ExtendedPrivateKey.decode(rootkey).pub()  # this returns the corresponding ExtendedPublicKey
ExtendedPrivateKey.decode(rootkey).pub().key  # this returns the corresponding PublicKey
l0rb commented 7 years ago

I see. Tanks for the prompt response and clarification!

SimoneBronzini commented 7 years ago

Made serialize_key() and serialized_public() private in 2729f24.