0

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?

1 Answer 1

3

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?

Unless you told your wallet to expect transactions sent to that address, there is no way the wallet can guess that that address's hash is in fact the hash of a script it happens to have the private key to. Even if it did know, there is generally no way for the wallet to be able to spend funds sent to such scripts.

The receiver wallet, and it alone, is responsible for deciding what addresses it accepts funds on. You taking one of the wallets' keys, and constructing an arbitrary script with that key, hashing it, and putting that in an address, is you trying to do the wallets' job.

This is akin to you refunding a friend for a dinner by going to their place at night, digging a hole in their yard, burying the funds there, and then acting all surprised when they claim they didn't receive your money ("How do you mean? The money was on your property! It was yours!").

Having the keys to spend funds isn't sufficient for a receiver to accept funds. In fact, it may not even be necessary. If the private key is stored in a hardware wallet, software may well be configured to accept payments on addresses involving public keys it knows the hardware wallet has the private keys to. But it still needs to be configured to do so, just like in your example.

0

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.