<p>I am using <a href="https://github.com/1200wd/bitcoinlib" rel="nofollow noreferrer">python's bitcoinlib</a> 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.</p>
<p>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.</p>
<p>However, bitcoinlib's wallet.send and wallet.create_transaction functions don't let you set the change address(es).</p>
<p>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</p>
<pre><code>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)
</code></pre>
<p>But I get a</p>
<blockquote>
<p>bitcoinlib.wallets.WalletError: Not enough unspent transaction outputs found</p>
</blockquote>
<p>at the second transaction_create.</p>
<p>How can I set the change address of the transaction?</p>
<p>Also if this is the wrong approach what would be the right one?</p>

CC BY-SA 4.0
2023-05-25T14:23:55.997Z
Can you link what "python bitcoinlib" are you using? I took a look at https://github.com/petertodd/python-bitcoinlib and I cannot find `send` nor `create_transaction` related to the wallet.
sr_gi
CC BY-SA 4.0
2023-05-25T15:28:05.250Z
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
CC BY-SA 4.0
2023-05-25T15:44:03.303Z
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`: https://github.com/1200wd/bitcoinlib/blob/a61f490f5ac4c043af01b502d0df43b2c49af7a9/bitcoinlib/wallets.py#L3706-L3710
sr_gi
<python><change><python-bitcoinlib>
How to set change address with python bitcoinlib
Colm Evans
# How to set change address with python bitcoinlib

(Asked by: username_0 on 2023-05-24)

I am using [python's bitcoinlib](https://github.com/1200wd/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?

**Tags:** python, change, python-bitcoinlib

> **Comments:**
> **username_1**: Can you link what "python bitcoinlib" are you using? I took a look at https://github.com/petertodd/python-bitcoinlib and I cannot find `send` nor `create_transaction` related to the wallet.  
> **username_0**: 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.  
> **username_1**: 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`: https://github.com/1200wd/bitcoinlib/blob/a61f490f5ac4c043af01b502d0df43b2c49af7a9/bitcoinlib/wallets.py#L3706-L3710  

---

## 0 answers

