<p>OK, so first off:</p>

<ol>
<li>Use Python 2.7; there's numerous pieces of code which doesn't work with Python 3.4, namely <code>make_request</code></li>
<li>Check out <a href="https://github.com/simcity4242/pybitcointools" rel="nofollow">this pybitcointools fork</a>, as there's a lot of added functionality (see the <code>README.txt</code> file)</li>
</ol>

<p>The functions you've named work as so:</p>

<ul>
<li><code>mktx(ins, outs)</code> where <code>ins</code> is an array of <code>"hexTxIdString:index"</code> and <code>outs</code> is an array of <code>{"value": 12345, "address": 1base58addressString}</code></li>
<li><code>mksend(ins, outs, txfee, 1changeAddress)</code> where <code>txfee</code> is again in Satoshis

<ul>
<li><code>sign(unsignedtx, index to sign, privkey for utxo)</code> where <code>index</code> is the <code>input</code> index to sign</li>
</ul></li>
</ul>

<p>We'll start by making the ins:</p>

<pre><code>utxos = [(utxo_txid, utxo_index), (txid2, i2)....]
ins = ["%s:%d" % utxo for utxo in utxos]
change_addr = "1thisIsAChangeAddressYouOwn123"
txfee = int(0.45 * len(ins) * 10000) # you may want to check this 10000 value, it could be 100000
rawtx = mksend(ins, txfee, change_addr)
</code></pre>

<p>Right, so let's assume, you have the UTXOs' <code>TxID</code> and corresponding <code>vout</code> for each private key. Make an dict of <code>privkeys ={ 'TxID:vout' : privkey, 'TxID2:vout2': privkey2 }</code> Then finally, <code>signall(rawtx, privkeys)</code> to get your signed Tx. Then to broadcast it, use <code>pushtx(signedTx, source="bci")</code> or <code>pushtx(signedTx, "testnet", source="blockr")</code> for Testnet. Done!</p>

CC BY-SA 3.0
2015-08-22T13:45:01.137Z
What function are you using to easily get the values for utxos ? What's the best way to go about it?
encodex
CC BY-SA 3.0
2015-08-22T13:46:29.963Z
I use that fork I linked; you can filter a list of the history/unspent, then use `get_outputs` to return the string required. Have had this exact same issue
Wizard Of Ozzie
CC BY-SA 3.0
2015-08-22T13:52:54.650Z
In the code block above you have there doesn't show you using mktx but you explained it above.
encodex
CC BY-SA 3.0
2015-08-22T14:23:41.407Z
`address = 164QpJNZPYtHNVZiNmzuG2yEeWok2UfymZ`




So, I should use history(address) rather than unspent(address) ?



It doesn't look like history gives me the unspent_output hash? I'm still really, really confused because it seems like in the pybitcointools example they do it in a completely different way.



I'm trying to imagine how to do it from the very beginning with creating my variables and everything.

`"2e204724677fb2f6cc80ed4cc92f0d068e8ec7774f49bfbf4b075aea76154f2b:0"`

is the outspent output and index, am I right?

Getting my ass kicked with formatting on here for some reason.
encodex
CC BY-SA 3.0
2015-08-22T15:07:25.797Z
Actually, you're right, use unspent: `unspent_1 = [unspent(privtoaddr(x)) for x in privs]`. I believe that'll return an array of "txid:index", which you'll need to filter through for unspents which have len>1, with `[x for y in unspent_1 for x in y]` which returns the flattened list. Re: mktx, mksend calls mktx, it just works out the difference and refunds that to a change address
Wizard Of Ozzie
CC BY-SA 3.0
2015-08-22T15:28:50.003Z
I'll give it a whirl here in a bit. Thanks for all the help.
encodex
CC BY-SA 3.0
2015-08-23T04:56:22.190Z
@encodex cool, I'll edit my answer shortly to show you exactly the process
Wizard Of Ozzie
CC BY-SA 3.0
2015-08-22T13:41:03.360Z
2015-08-24T07:03:58.767Z
2015-08-24T07:03:58.767Z
Wizard Of Ozzie
<p>I'm utilizing the <a href="https://github.com/vbuterin/pybitcointools" rel="nofollow">pybitcointools</a> module in python 3.4</p>

<p>I've went through time and time again in the examples listed on the github page, but it isn't documented very well nor is the code.</p>

<p>My situation is this: I have a list of private keys in a CSV and I want to generate a raw transaction for each address and spend their unspent_outputs to a single address by pushing the signed transaction to <a href="https://blockchain.info/pushtx" rel="nofollow">blockchain.info</a></p>

<p>I'm having a problem when it comes to the these functions: mktx, mksend, sign</p>

<p>If someone could create an example of the process while also documenting each step and explaining what each argument for each function is for and how it's used. This would be an amazing help if someone could give a hand here! I've searched far and low, but I just can't find a good documentation of this process. I'm sure this would help many others as well!</p>

CC BY-SA 3.0
2015-08-22T06:49:09.527Z
Give me an hour and I'll write up an answer. You're right, the code is severely lacking in documentation. Check out [this fork](https://github.com/simcity4242/pybitcointools) for some better code comments until i reply but note it's not tested for 3.4 only 2.7,
Wizard Of Ozzie
<transactions><development><private-key><script><python>
Constructing, signing, and pushing a Bitcoin transaction by using pybitcointools with Python
encodex
# Constructing, signing, and pushing a Bitcoin transaction by using pybitcointools with Python

(Asked by: username_0 on 2015-08-21)

I'm utilizing the [pybitcointools](https://github.com/vbuterin/pybitcointools) module in python 3.4

I've went through time and time again in the examples listed on the github page, but it isn't documented very well nor is the code.

My situation is this: I have a list of private keys in a CSV and I want to generate a raw transaction for each address and spend their unspent\_outputs to a single address by pushing the signed transaction to [blockchain.info](https://blockchain.info/pushtx)

I'm having a problem when it comes to the these functions: mktx, mksend, sign

If someone could create an example of the process while also documenting each step and explaining what each argument for each function is for and how it's used. This would be an amazing help if someone could give a hand here! I've searched far and low, but I just can't find a good documentation of this process. I'm sure this would help many others as well!

**Tags:** transactions, development, private-key, script, python

> **Comments:**
> **username_1**: Give me an hour and I'll write up an answer. You're right, the code is severely lacking in documentation. Check out [this fork](https://github.com/simcity4242/pybitcointools) for some better code comments until i reply but note it's not tested for 3.4 only 2.7,  

---

## 1 answers

### Answer by username_1 on 2015-08-22. Score: 3 | Accepted answer

OK, so first off:

1. Use Python 2.7; there's numerous pieces of code which doesn't work with Python 3.4, namely `make_request`
2. Check out [this pybitcointools fork](https://github.com/simcity4242/pybitcointools), as there's a lot of added functionality (see the `README.txt` file)

The functions you've named work as so:

* `mktx(ins, outs)` where `ins` is an array of `"hexTxIdString:index"` and `outs` is an array of `{"value": 12345, "address": 1base58addressString}`
* `mksend(ins, outs, txfee, 1changeAddress)` where `txfee` is again in Satoshis
  + `sign(unsignedtx, index to sign, privkey for utxo)` where `index` is the `input` index to sign

We'll start by making the ins:

```
utxos = [(utxo_txid, utxo_index), (txid2, i2)....]
ins = ["%s:%d" % utxo for utxo in utxos]
change_addr = "1thisIsAChangeAddressYouOwn123"
txfee = int(0.45 * len(ins) * 10000) # you may want to check this 10000 value, it could be 100000
rawtx = mksend(ins, txfee, change_addr)

```

Right, so let's assume, you have the UTXOs' `TxID` and corresponding `vout` for each private key. Make an dict of `privkeys ={ 'TxID:vout' : privkey, 'TxID2:vout2': privkey2 }` Then finally, `signall(rawtx, privkeys)` to get your signed Tx. Then to broadcast it, use `pushtx(signedTx, source="bci")` or `pushtx(signedTx, "testnet", source="blockr")` for Testnet. Done!

> **Comments:**
> **username_0**: What function are you using to easily get the values for utxos ? What's the best way to go about it?  
> **username_1**: I use that fork I linked; you can filter a list of the history/unspent, then use `get_outputs` to return the string required. Have had this exact same issue  
> **username_0**: In the code block above you have there doesn't show you using mktx but you explained it above.  
> **username_0**: `address = 164QpJNZPYtHNVZiNmzuG2yEeWok2UfymZ`     So, I should use history(address) rather than unspent(address) ?    It doesn't look like history gives me the unspent_output hash? I'm still really, really confused because it seems like in the pybitcointools example they do it in a completely different way.    I'm trying to imagine how to do it from the very beginning with creating my variables and everything.  `"2e204724677fb2f6cc80ed4cc92f0d068e8ec7774f49bfbf4b075aea76154f2b:0"`  is the outspent output and index, am I right?  Getting my ass kicked with formatting on here for some reason.  
> **username_1**: Actually, you're right, use unspent: `unspent_1 = [unspent(privtoaddr(x)) for x in privs]`. I believe that'll return an array of "txid:index", which you'll need to filter through for unspents which have len>1, with `[x for y in unspent_1 for x in y]` which returns the flattened list. Re: mktx, mksend calls mktx, it just works out the difference and refunds that to a change address  
> **username_0**: I'll give it a whirl here in a bit. Thanks for all the help.  
> **username_1**: @username_0 cool, I'll edit my answer shortly to show you exactly the process  

---

