I am using python's bitcoinlib to try and set up payments where a client sends funds to a server and then sends requests signed with their key as proof that the request came from them. The server keeps track of how much funds are remaining. Additionally, a client will be expected to connect to many nodes. I would like to have only one key-address pair to keep track of in the client.

Unless I am mistaken, the easiest way to do this would be to send the change back to the original address in the payment transaction.

However, bitcoinlib's wallet.send and wallet.create_transaction functions don't let you set the change address(es).

Additionally, I tried creating a transaction using transaction_create, taking the change output and creating a new address with that change value being sent to the original address

t = w1.transaction_create([(k2.address, 100)], account_id=0)
change = list(filter(lambda x: x.address != k2.address, t.outputs))[0]
new_t = w1.transaction_create([(k2.address, 100), (k1.address, change.value)], account_id=0)

But I get a

bitcoinlib.wallets.WalletError: Not enough unspent transaction outputs found

at the second transaction_create.

How can I set the change address of the transaction?

Also if this is the wrong approach what would be the right one?

  • 1
    Can you link what "python bitcoinlib" are you using? I took a look at github.com/petertodd/python-bitcoinlib and I cannot find send nor create_transaction related to the wallet.
    – sr_gi
    Commented May 25, 2023 at 14:23
  • Sorry about that, I didn't realize there was another package with such a similar name. I edited the question to add the link. Thank you for your for bringing this to my attention.
    – Colm Evans
    Commented May 25, 2023 at 15:28
  • For what I can see the library does manage the change outputs for you. They are created to the change addresses of the wallet identified by account_id: github.com/1200wd/bitcoinlib/blob/…
    – sr_gi
    Commented May 25, 2023 at 15:44


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.