2

I'm looking for a simple, easy to use Bitcoin library for Python.

What I want to do is verify I can enter a hex private key that I want, and get the expected pubkey in compressed format, and the 1Btc... address.

I've looked at cryptotools on github which looked promising, and easy to use, but it would not accept a hex value that wasn't a multiple of 2. I know that sounds odd, but I want to be able to enter a hex value like c12 and have it work as expected (unless I have to enter 0c12 and that will work, with the leading zero discarded). I expect than when I enter 0cfa721d that this will be interpreted as big endian.

bitcoinlib looks promising, but I find it confusing and I've tried for a couple of hours but unsure of how to get what I want.

  1. Input private key in hex
  2. Get back pub key in hash160 format, compressed
  3. Get the 1Btc... address as well.

I wonder if this is a strict mathematical relationship. For instance could there be an arbitrary or infinite number of pub keys from the private key? Is there a way to ensure I get the pubkey I want from the private key? I want to test known private keys with the pubkey and BTC address generated that corresponds to known private keys and addresses, so I can try and figure out how whoever generated these addresses from choices of private keys I can try to replicate and do the same thing myself.

Thank you for any help or suggestions.

  • Hi, author of cryptotools here. The reason it wont accept odd digits hex is that it will store the private key inteternally as Python bytes object and the bytes.fromhex method requires full bytes. I'll have a look at it. As a workaround you can simply pad with a zero if it has odd amount of digits.0x0cfa721d is exactly the same as 0xcfa721d. The other functionality is there, just do myprivatekey.to_public().to_address('P2PKH', compressed=True) – Mike D Nov 18 '18 at 16:51
1

For this very reduced case, you can take a look at https://github.com/circulosmeos/bitcoin-in-tiny-pieces

It has each operation coded for ease of understanding, isolated in a different file, but they're easy to join or to use directly from command line: for example:

$ echo 0x01 | ./bitcoin-public-from-private.py | ./bitcoin-address-from-public-key.py

And for the question:

For instance could there be an arbitrary or infinite number of pub keys from the private key?

In principle, from a private key only one public key is derived: but this can be coded in three different Bitcoin addresses. Normally only one or two of these formats are used.

This is in the most simple case, not considering more complex cases introduced later in Bitcoin (like HD keys, etc)...

  • Hey thank you! I forked your repository at github.com/jamesyoungdigital/bitcoin-in-tiny-pieces.git to add a new little tool that checks brainwallet address balances. I wasn't having much success with the shell script. It now depends on my fork of pybitcointools.git, which is really easy to use and depends on almost nothing. Hope you might like the little additions! :) – James Young Nov 19 '18 at 13:47
  • Glad it has been useful! As for the shell script just open an issue and I'll take a look: it'd work flawlessly! – circulosmeos Nov 19 '18 at 17:25
  • Star given! I'd like to give a few more but they won't let me, haha. :-) With the shell script, I try like this: ./bitcoin-test-address-balance.sh satoshi but the output is always: ... 2 .... n and as far as I can tell, never ends. The problem seems to be in the address balance python script, lines 62 to 64, when an exception is caught. The URL should use the {} for interpolation. Please see my pull request. Thanks! :-) – James Young Nov 19 '18 at 17:35
  • Hi James, I think that the exception is correctly caught: the thing is that blockchain.info sometimes discard the petition, probably due to high traffic: in that case the script sleeps 1 to n minutes between retries: as it is done for running in batches, this is not considered a problem :-o but thanks for your pull! – circulosmeos Nov 20 '18 at 20:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.