I'm trying to use the python package python-bitcoinlib
to send a timelock transaction on testnet. The github has two examples to create an address, and then use that address to send the bitcoin to another address.
The problem is, when I send bitcoin to the generated address using my own private key, I never receive the bitcoin in my wallet. I'm able to broadcast the transaction to the second address, but I would like to have access to the bitcoin in case I change my mind before the timelock arrives. Here is the code:
from bitcoinutils.setup import setup
from bitcoinutils.transactions import Transaction, TxInput, TxOutput, Sequence
from bitcoinutils.keys import P2pkhAddress, P2shAddress, PrivateKey
from bitcoinutils.script import Script
from bitcoinutils.constants import TYPE_RELATIVE_TIMELOCK
def main():
# always remember to setup the network
setup('testnet')
#
# This script creates a P2SH address containing a CHECKSEQUENCEVERIFY plus
# a P2PKH locking funds with a key as well as for 20 blocks
#
# set values
relative_blocks = 20
seq = Sequence(TYPE_RELATIVE_TIMELOCK, relative_blocks)
# secret key corresponding to the pubkey needed for the P2SH (P2PKH) transaction
p2pkh_sk = PrivateKey('cRvyLwCPLU88jsyj94L7iJjQX5C2f8koG4G2gevN4BeSGcEvfKe9')
# get the address (from the public key)
p2pkh_addr = p2pkh_sk.get_public_key().get_address()
# create the redeem script
redeem_script = Script([seq.for_script(), 'OP_CHECKSEQUENCEVERIFY', 'OP_DROP', 'OP_DUP', 'OP_HASH160', p2pkh_addr.to_hash160(), 'OP_EQUALVERIFY', 'OP_CHECKSIG'])
# create a P2SH address from a redeem script
addr = P2shAddress.from_script(redeem_script)
print(addr.to_string())
if __name__ == "__main__":
main()
When I run this code with my own private key, I get an address like:
2N3M2ZnDsw4FMDYTyMmtk6k12pBzbyKJvBR
From what I understand, that address is created from my private key, so shouldn't the funds I send to it go straight back to my wallet?