P2PKH address

Generate address

 1import hashlib
 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
11# Create the (in)famous correct brainwallet secret key.
12h = hashlib.sha256(b'correct horse battery staple').digest()
13seckey = CBitcoinSecret.from_secret_bytes(h)
15# Create a redeemScript used to unlock bitcoins
16redeem_script = CScript([OP_DUP, OP_HASH160, Hash160(seckey.pub), OP_EQUALVERIFY, OP_CHECKSIG])
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
 3txid = lx("c36a4408a242402cb1584e640a5cbe883513a78aae0cf8ea02986cc76845e9e0")
 4vout = 0
 6# Specify the amount send to your P2WSH address.
 7amount = int(1 * COIN)
 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
13# Create the txin structure, which includes the outpoint. The scriptSig
14# defaults to being empty.
15txin = CMutableTxIn(COutPoint(txid, vout))
17# Specify a destination address and create the txout.
18destination = CBitcoinAddress('bcrt1q05cfmjm79ujnnpe2r8wr5kv3kcrtsq3jec3n0l').to_scriptPubKey()
19txout = CMutableTxOut(amount_less_fee, destination)
21# Create the unsigned transaction.
22tx = CMutableTransaction([txin], [txout])
24# Calculate the signature hash for that transaction.
25sighash = SignatureHash(redeem_script, tx, 0, SIGHASH_ALL)
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])
31# Set the scriptSig of our transaction input appropriately.
32txin.scriptSig = CScript([sig, seckey.pub])
34# Done! Print the transaction
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.