7

I'm trying to use the python-bitcoinlib library but its docs aren't entirely clear to me. I'm a programmer but quite new to Python, and not very versed in the Bitcoin protocol.

What's the process, code-wise, with the library, for the following?

  1. Generating a private key
  2. Creating a public key
  3. Creating an address from the public key

I tried various things and am not completely sure what arguments I supply to what functions, and what functions I'm supposed to use in the process.

I'd appreciate a lot, and I think it'd help me the most if I were provided code for achieving each step.

I was able to follow the Bitcoin protocol (specifically, the developer reference and the developer guide on bitcoin.org to do all of those things, and I'm looking specifically how to do it with the library, so I could have my code fully compatible with other functionality of it, since I'd like to use the library for additional purposes.

2 Answers 2

6

Sorry I am not familiar with python-bitcoinlib but if your goal is to just create keys/addresses you can use this library (disclosure: I wrote it) the following way:

Generating a private key

private = PrivateKey.random()

or private = PrivateKey.from_hex('yourhexkeyhere')

or private = PrivateKey.from_wif('yourwifhere')

and then to get the hex representation use private.hex() of private.wif()

Creating a public key

public = private.to_public()
public.hex()
'044cc549c65f4b0b7713d7a763a01221f85fd34602ac8ff0386995349c55de7111d16b5...'
public.hex(compressed=True)
'034cc549c65f4b0b7713d7a763a01221f85fd34602ac8ff0386995349c55de7111'

Creating an address from the public key

public.to_address('P2PKH')
'16B2Ghyu5C1CofKfXF2Zi9JycqaAyaE8cd'
public.to_address('P2WPKH')
'bc1qtqshpjw8kr7jlk27gcvj7pvzt8lp6vn5dc2f24'

If you use this library please use it only as a learning tool and not to send real coins as there might still be some bugs.

3

Some usage examples are now available in the repository.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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