简体   繁体   中英

How to create bitcoin address with python

Am creating an application where i would like to create a bitcoin address for a registered user. I want to create the address so it can be used to later receive bitcoin. How can i do this using the python programming language.

You can do this by starting a full node on your computer and connecting to it with python-bitcoinrpc. After the node is fully configured and rpc is up and running you can get a new address on python with AuthServiceProxy(" http://%s:%s@127.0.0.1:8332 "%(RPC_USER, RPC_PASSWORD).getnewaddress() which returns the address as a string.

from Crypto.PublicKey import RSA
import Crypto.Random

 def generate_keys(self):
        # those keys are in binary format.
        # Crypto.Random is the function that generates the random key.
        private_key=RSA.generate(1024, Crypto.Random.new().read)
        public_key=private_key.publickey()
        # DER is a binary format for data structures described by ASN.
        # from hex we decode it to ascii characters
        return binascii.hexlify(private_key.exportKey(format="DER")).decode('ascii'), binascii.hexlify(public_key.exportKey(format="DER")).decode('ascii')

I've found the bitmerchant package to be useful for this.

https://github.com/sbuss/bitmerchant

You can easily create new wallets as well as child wallets along with public and private keys. The examples on the github page are quite easy to follow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM