P2PKH address

Generate address

 1import hashlib
 2
 3from bitcoin import SelectParams
 4from bitcoin.core import b2x, lx, COIN, COutPoint, CMutableTxOut, CMutableTxIn, CMutableTransaction, Hash160
 5from bitcoin.core.script import CScript, OP_DUP, OP_HASH160, OP_EQUALVERIFY, OP_CHECKSIG, SignatureHash, SIGHASH_ALL
 6from bitcoin.core.scripteval import VerifyScript, SCRIPT_VERIFY_P2SH
 7from bitcoin.wallet import CBitcoinAddress, CBitcoinSecret, P2PKHBitcoinAddress
 8
 9SelectParams('regtest')
10
11# Create the (in)famous correct brainwallet secret key.
12h = hashlib.sha256(b'correct horse battery staple').digest()
13seckey = CBitcoinSecret.from_secret_bytes(h)
14
15# Create a redeemScript used to unlock bitcoins
16redeem_script = CScript([OP_DUP, OP_HASH160, Hash160(seckey.pub), OP_EQUALVERIFY, OP_CHECKSIG])
17
18public_key = seckey.pub
19address = P2PKHBitcoinAddress.from_pubkey(public_key)
20print('Address:', str(address))
21# outputs: Address: mrdwvWkma2D6n9mGsbtkazedQQuoksnqJV

Spend from address

Assuming the previously generated address has received funds, we can spend them. In order to spend them, we'll need information about the transaction id (txid) and a vector of an output (vout). You can get both from an explorer or by querying your running Bitcoin node by running listunspent along with some filters:

bitcoin-cli listunspent 1 9999999 "[\"address\"]"

Note that you must have an address in the watchlist in order to get any output. To add an address to a watchlist run importaddress:

bitcoin-cli importaddress <address> "<label>" false false

 1# we are continuing the code from above
 2
 3txid = lx("c36a4408a242402cb1584e640a5cbe883513a78aae0cf8ea02986cc76845e9e0")
 4vout = 0
 5
 6# Specify the amount send to your P2WSH address.
 7amount = int(1 * COIN)
 8
 9# Calculate an amount for the upcoming new UTXO. Set a high fee (5%) to bypass bitcoind minfee
10# setting on regtest.
11amount_less_fee = amount * 0.99
12
13# Create the txin structure, which includes the outpoint. The scriptSig
14# defaults to being empty.
15txin = CMutableTxIn(COutPoint(txid, vout))
16
17# Specify a destination address and create the txout.
18destination = CBitcoinAddress('bcrt1q05cfmjm79ujnnpe2r8wr5kv3kcrtsq3jec3n0l').to_scriptPubKey()
19txout = CMutableTxOut(amount_less_fee, destination)
20
21# Create the unsigned transaction.
22tx = CMutableTransaction([txin], [txout])
23
24# Calculate the signature hash for that transaction.
25sighash = SignatureHash(redeem_script, tx, 0, SIGHASH_ALL)
26
27# Now sign it. We have to append the type of signature we want to the end, in
28# this case the usual SIGHASH_ALL.
29sig = seckey.sign(sighash) + bytes([SIGHASH_ALL])
30
31# Set the scriptSig of our transaction input appropriately.
32txin.scriptSig = CScript([sig, seckey.pub])
33
34# Done! Print the transaction
35print(b2x(tx.serialize()))
36# outputs: 0100000001e0e94568c76c9802eaf80cae8aa7133588be5c0a644e58b12c4042a208446ac3000000006b4830450221009abd104d04ecf518c288979ce387defe2ac1d25cb2cf4b3cd97f54e182ef0c3c022055a558c31bcb79237bb75a6659d07eee7875046693818d6f6f0e6064e733c72401210378d430274f8c5ec1321338151e9f27f4c676a008bdf8638d07c0b6be9ab35c71ffffffff01c09ee605000000001600147d309dcb7e2f2539872a19dc3a5991b606b8023200000000

Now that we have our signed and encoded transaction, we can broadcast it using sendrawtransaction:

bitcoin-cli sendrawtransaction <transaction>

If the transaction is broadcasted successfully a transaction id will be returned. In this case it was cd6bac96d5f43afaa3663647ed7e7301925ac7760d0d22f1fb7f540a7c080dd7.