<p>For Ubuntu, Python 2.7 (same setup as me) I'd highly recommend either:</p>

<ol>
<li><a href="http://www.github.com/vbuterin/pybitcointools/" rel="nofollow">pybitcointools</a> (install using <em>pip install pybitcointools</em> for version 1.1.15 or <em>pip install bitcoin</em> for version 1.1.25 (recommended)</li>
<li><a href="http://sx.dyne.org" rel="nofollow">sx</a>/libbitcoin. <a href="https://bitcoin.stackexchange.com/questions/32219/ubuntu-14-lts-with-sx-tools-installation-issues/32411#32411">Might need to install using mastercoin's <em>install.sh</em> script</a>, depending on whether dependencies work (I had issues using Tasty Tahr Ubuntu 14.04).</li>
</ol>

<p>Both are CLIs, and both work similarly allowing piping of variables, etc. However <em>pybitcointools</em> seems to support the <em>address history</em> function (API call to Blockchain.info/Blockr.io) and the <a href="https://github.com/vbuterin/pybitcointools/blob/master/README.txt" rel="nofollow">support documentation for <em>pybitcointools</em></a> seems to be more self-explanatory than the brief <em>sx</em> tutorial.</p>

CC BY-SA 3.0
2015-01-25T12:52:20.197Z
+1 Do you happen to have an example of how to perform a transaction knowing only the WIF and a destination address?
Sebi
CC BY-SA 3.0
2015-01-25T13:07:37.057Z
From the manual page it is not very clear to me how the destination address is specified.
Sebi
CC BY-SA 3.0
2015-01-26T12:24:09.870Z
Sure. I'll do it once I'm off iOS. I'll need to play around and I'll post it here in ~12hrs
Wizard Of Ozzie
CC BY-SA 3.0
2015-01-27T08:58:24.570Z
@Sebi just to clarify, you want to create a raw Txn and send BTC from your address (ie private key) to another address, right?
Wizard Of Ozzie
CC BY-SA 3.0
2015-01-27T09:02:04.093Z
yes that is what I want
Sebi
CC BY-SA 3.0
2015-01-27T10:01:41.810Z
OK, I'll edit accordingly. SX tools is how I have gone about it in the past. Read https://bitcoin.stackexchange.com/questions/32219/ubuntu-14-lts-with-sx-tools-installation-issues/32411#32411 if you're having dependency issues as I'll put procedures for both SX and pybitcointools
Wizard Of Ozzie
CC BY-SA 3.0
2015-02-04T06:26:07.923Z
See http://bitcoin.stackexchange.com/a/35889/9382
Wizard Of Ozzie
CC BY-SA 3.0
2015-01-25T08:59:39.980Z
2015-01-27T10:02:14.667Z
2015-01-27T10:02:14.667Z
Wizard Of Ozzie
<p>Using SX tools.</p>

<p>At bash command line, create a WIF private key file called <code>private.key</code> for 1MBngSqZbMydscpzSoehjP8kznMaHAzh9y with this data inside:  <code>5HvofFG7K1e2aeWESm5pbCzRHtCSiZNbfLYXBvxyA57DhKHV4U3</code>. FYI, the private key (as discussed <a href="https://bitcoin.stackexchange.com/questions/32628/redeeming-a-raw-transaction-step-by-step-example-required/32695#32695">here</a>) is a brainwallet of <em>mrbubbymrbubbymrbubby!</em> (which is cool, because 1MB ≈ brainwallet phrase)</p>

<p>OK, now we run these aliases/commands in the exact order given (node comments explaining each step):</p>

<ol>
<li><code>DECODED_ADDR=$(cat private.key | sx addr | sx decode-addr) # hash160</code></li>
<li><code>PREVOUT_SCRIPT=$(sx rawscript dup hash160 [ $DECODED_ADDR ] equalverify checksig)  # hash160 + script padding</code></li>
<li><code>SIGNATURE=$(cat private.key | sx sign-input txfile.tx 0 $PREVOUT_SCRIPT)   # 72 digit der signature</code></li>
<li><code>SIGNATURE_AND_PUBKEY_SCRIPT=$(sx rawscript [ $SIGNATURE ] [ $(cat private.key | sx pubkey) ])  # 278 digit script/sig/pubkey</code></li>
<li><code>sx set-input txfile.tx 0 $SIGNATURE_AND_PUBKEY_SCRIPT &gt; txfile.tx.signed    # 192 digit signed txn</code></li>
</ol>

<p>In this instance, the aliases give the following:</p>

<ul>
<li><code>DECODED_ADDR=dd6cce9f255a8cc17bda8ba0373df8e861cb866e</code></li>
<li><code>PREVOUT_SCRIPT=76a914dd6cce9f255a8cc17bda8ba0373df8e861cb866e88ac</code></li>
<li><code>SIGNATURE=3045022100da43201760bda697222002f56266bf65023fef2094519e13077f777baed553b102205ce35d05eabda58cd50a67977a65706347cc25ef43153e309ff210a134722e9e01</code></li>
<li><code>SIGNATURE_AND_PUBKEY_SCRIPT=483045022100da43201760bda697222002f56266bf65023fef2094519e13077f777baed553b102205ce35d05eabda58cd50a67977a65706347cc25ef43153e309ff210a134722e9e0141042daa93315eebbe2cb9b5c3505df4c6fb6caca8b756786098567550d4820c09db988fe9997d049d687292f815ccd6e7fb5c1b1a91137999818d17c73d0f80aef9</code></li>
</ul>

<p><strong>FINAL SIGNED TXN:</strong></p>

<p>Here's the final raw Txn which sends BTC to 14zWNsgUMmHhYx4suzc2tZD6HieGbkQi5s.</p>

<p><code>0100000001be66e10da854e7aea9338c1f91cd489768d1d6d7189f586d7a3613f2a24d5396000000008b483045022100da43201760bda697222002f56266bf65023fef2094519e13077f777baed553b102205ce35d05eabda58cd50a67977a65706347cc25ef43153e309ff210a134722e9e0141042daa93315eebbe2cb9b5c3505df4c6fb6caca8b756786098567550d4820c09db988fe9997d049d687292f815ccd6e7fb5c1b1a91137999818d17c73d0f80aef9ffffffff0123ce0100000000001976a9142bc89c2702e0e618db7d59eb5ce2f0f147b4075488ac00000000</code></p>

<p>File Contents:</p>

<p>private.key = <code>5HvofFG7K1e2aeWESm5pbCzRHtCSiZNbfLYXBvxyA57DhKHV4U3</code>
txfile.tx = <code>0100000001be66e10da854e7aea9338c1f91cd489768d1d6d7189f586d7a3613f2a24d53960000000000ffffffff0123ce0100000000001976a9142bc89c2702e0e618db7d59eb5ce2f0f147b4075488ac00000000</code>
rawscript.sigpubkey.tx = <code>473044022054f60e8ae19411541597167362d12fc132e081a546c766bfd69c16d9d58e268a022048055c7fd8bf78e48543e8756bb3b26336df35b812a184119ba0e9d525bbb8aa0141042daa93315eebbe2cb9b5c3505df4c6fb6caca8b756786098567550d4820c09db988fe9997d049d687292f815ccd6e7fb5c1b1a91137999818d17c73d0f80aef9</code>
signed.tx = <code>0100000001be66e10da854e7aea9338c1f91cd489768d1d6d7189f586d7a3613f2a24d5396000000000b00000c0000000000000000ffffffff0123ce0100000000001976a9142bc89c2702e0e618db7d59eb5ce2f0f147b4075488ac00000000</code></p>

CC BY-SA 3.0
2015-02-04T05:44:48.150Z
2015-02-04T10:06:08.620Z
2015-02-04T10:06:08.620Z
Wizard Of Ozzie
<p>here is an example of transaction on <code>testnet</code>(<a href="https://github.com/suhailvs/bitcoin-code/tree/testnet" rel="nofollow noreferrer">https://github.com/suhailvs/bitcoin-code/tree/testnet</a>), you need <code>private key</code>,<code>destination address</code> and <code>previous transacion hash</code> which has output address which relate to the given privatekey::</p>

<pre><code>import hashlib
import txnUtils
from keyUtils import keyToAddr, addrToScriptPubkey

# From --&gt; one input
privateKey = hashlib.sha256('abcdefghijklmnop').hexdigest() # mpSyb71528U8dQjuTCeDCcJqH8dQTyY13c
from_address= keyToAddr(privateKey,testnet=True)
txn_hash = "3c24ca820100153fb43434191d10464dd2dcd13f0c9aa07d15f7330e8bcd0596"

# To --&gt; android testnet wallet
to_address = 'miD4PnSDWC2M725hvFBoBhNn8fbowoHnnS'

# Sign the Transction
signed_txn = txnUtils.makeSignedTransaction(
    privateKey, txn_hash, 0, addrToScriptPubkey(from_address), # input: has balance 0.01btc
    [[900000,addrToScriptPubkey(to_address)]] # outputs: 0.009btc 
)

print 'SIGNED TXN', signed_txn

# Broadcast this transaction(ie: signed_txn) at:
# https://testnet.blockexplorer.com/tx/send
</code></pre>

CC BY-SA 3.0
2017-09-14T02:04:46.863Z
2017-09-14T02:04:46.863Z
suhailvs
<p>In order to create a transaction, you need the blockchain, or at least the transactions in the blockchain that belong to a particular address (technical term would be unspent transaction outputs or UTXOs for a particular script). There are three ways this can happen:</p>

<ol>
<li><p>You have a local bitcoind or btcd installed which would let you query for transactions, at the expense of downloading the entire blockchain.</p></li>
<li><p>You use an electrum or some other SPV server to query for transactions, at the expense of connecting to these nodes.</p></li>
<li><p>You use a centralized service such as blockchain.info to gather all transactions at the expense of having to trust the centralized service to be up all the time and to not be corrupt.</p></li>
</ol>

<p>Any way you slice it, you cannot get the transaction data from a python library that doesn't connect to some other service. Hence tools like pybitcointools or pycoin will not get you what you need. sx has some tools to look up utxos using method #3.</p>

<p>You can install Armory or bitcoind and import the private key and use method #1 above or use something like <a href="http://www.pi-wallet.com" rel="nofollow">pi-wallet</a> and use method #2.</p>

CC BY-SA 3.0
2015-01-28T16:32:38.967Z
2015-01-28T16:32:38.967Z
Jimmy Song
<p>Is there a quick way to do transactions knowing only the private key(secret exponent) and the destination address, preferably from a command line utility such as pycoin?</p>

<p>I am using Debian/Ubuntu and python 2.7.</p>

CC BY-SA 3.0
2015-01-25T04:24:49.180Z
You'll need to clarify what OS and Python version you're using. Assuming Win/Posix, Python 2.7 pybitcointools has an excellent CLI. Although I do agree that Pycoin is a great tool for simplicity. More complicated usage cases under posix could use SX/libbitcoin
Wizard Of Ozzie
CC BY-SA 3.0
2015-01-25T08:46:03.077Z
I am currently using Ubuntu with python 2.7.6.
Sebi
<transactions><wallet><bitcoind><private-key><python>
pycoin command line transaction
Sebi
# pycoin command line transaction

(Asked by: username_0 on 2015-01-24)

Is there a quick way to do transactions knowing only the private key(secret exponent) and the destination address, preferably from a command line utility such as pycoin?

I am using Debian/Ubuntu and python 2.7.

**Tags:** transactions, wallet, bitcoind, private-key, python

> **Comments:**
> **username_1**: You'll need to clarify what OS and Python version you're using. Assuming Win/Posix, Python 2.7 pybitcointools has an excellent CLI. Although I do agree that Pycoin is a great tool for simplicity. More complicated usage cases under posix could use SX/libbitcoin  
> **username_0**: I am currently using Ubuntu with python 2.7.6.  

---

## 4 answers

### Answer by username_3 on 2015-01-28. Score: 3 | Accepted answer

In order to create a transaction, you need the blockchain, or at least the transactions in the blockchain that belong to a particular address (technical term would be unspent transaction outputs or UTXOs for a particular script). There are three ways this can happen:

1. You have a local bitcoind or btcd installed which would let you query for transactions, at the expense of downloading the entire blockchain.
2. You use an electrum or some other SPV server to query for transactions, at the expense of connecting to these nodes.
3. You use a centralized service such as blockchain.info to gather all transactions at the expense of having to trust the centralized service to be up all the time and to not be corrupt.

Any way you slice it, you cannot get the transaction data from a python library that doesn't connect to some other service. Hence tools like pybitcointools or pycoin will not get you what you need. sx has some tools to look up utxos using method #3.

You can install Armory or bitcoind and import the private key and use method #1 above or use something like [pi-wallet](http://www.pi-wallet.com) and use method #2.


---

### Answer by username_1 on 2015-01-25. Score: 2

For Ubuntu, Python 2.7 (same setup as me) I'd highly recommend either:

1. [pybitcointools](http://www.github.com/vbuterin/pybitcointools/) (install using *pip install pybitcointools* for version 1.1.15 or *pip install bitcoin* for version 1.1.25 (recommended)
2. [sx](http://sx.dyne.org)/libbitcoin. [Might need to install using mastercoin's *install.sh* script](https://bitcoin.stackexchange.com/questions/32219/ubuntu-14-lts-with-sx-tools-installation-issues/32411#32411), depending on whether dependencies work (I had issues using Tasty Tahr Ubuntu 14.04).

Both are CLIs, and both work similarly allowing piping of variables, etc. However *pybitcointools* seems to support the *address history* function (API call to Blockchain.info/Blockr.io) and the [support documentation for *pybitcointools*](https://github.com/vbuterin/pybitcointools/blob/master/README.txt) seems to be more self-explanatory than the brief *sx* tutorial.

> **Comments:**
> **username_0**: +1 Do you happen to have an example of how to perform a transaction knowing only the WIF and a destination address?  
> **username_0**: From the manual page it is not very clear to me how the destination address is specified.  
> **username_1**: Sure. I'll do it once I'm off iOS. I'll need to play around and I'll post it here in ~12hrs  
> **username_1**: @username_0 just to clarify, you want to create a raw Txn and send BTC from your address (ie private key) to another address, right?  
> **username_0**: yes that is what I want  
> **username_1**: OK, I'll edit accordingly. SX tools is how I have gone about it in the past. Read https://bitcoin.stackexchange.com/questions/32219/ubuntu-14-lts-with-sx-tools-installation-issues/32411#32411 if you're having dependency issues as I'll put procedures for both SX and pybitcointools  
> **username_1**: See http://bitcoin.stackexchange.com/a/35889/9382  

---

### Answer by username_1 on 2015-02-04. Score: 2

Using SX tools.

At bash command line, create a WIF private key file called `private.key` for 1MBngSqZbMydscpzSoehjP8kznMaHAzh9y with this data inside: `5HvofFG7K1e2aeWESm5pbCzRHtCSiZNbfLYXBvxyA57DhKHV4U3`. FYI, the private key (as discussed [here](https://bitcoin.stackexchange.com/questions/32628/redeeming-a-raw-transaction-step-by-step-example-required/32695#32695)) is a brainwallet of *mrbubbymrbubbymrbubby!* (which is cool, because 1MB ≈ brainwallet phrase)

OK, now we run these aliases/commands in the exact order given (node comments explaining each step):

1. `DECODED_ADDR=$(cat private.key | sx addr | sx decode-addr) # hash160`
2. `PREVOUT_SCRIPT=$(sx rawscript dup hash160 [ $DECODED_ADDR ] equalverify checksig) # hash160 + script padding`
3. `SIGNATURE=$(cat private.key | sx sign-input txfile.tx 0 $PREVOUT_SCRIPT) # 72 digit der signature`
4. `SIGNATURE_AND_PUBKEY_SCRIPT=$(sx rawscript [ $SIGNATURE ] [ $(cat private.key | sx pubkey) ]) # 278 digit script/sig/pubkey`
5. `sx set-input txfile.tx 0 $SIGNATURE_AND_PUBKEY_SCRIPT > txfile.tx.signed # 192 digit signed txn`

In this instance, the aliases give the following:

* `DECODED_ADDR=dd6cce9f255a8cc17bda8ba0373df8e861cb866e`
* `PREVOUT_SCRIPT=76a914dd6cce9f255a8cc17bda8ba0373df8e861cb866e88ac`
* `SIGNATURE=3045022100da43201760bda697222002f56266bf65023fef2094519e13077f777baed553b102205ce35d05eabda58cd50a67977a65706347cc25ef43153e309ff210a134722e9e01`
* `SIGNATURE_AND_PUBKEY_SCRIPT=483045022100da43201760bda697222002f56266bf65023fef2094519e13077f777baed553b102205ce35d05eabda58cd50a67977a65706347cc25ef43153e309ff210a134722e9e0141042daa93315eebbe2cb9b5c3505df4c6fb6caca8b756786098567550d4820c09db988fe9997d049d687292f815ccd6e7fb5c1b1a91137999818d17c73d0f80aef9`

**FINAL SIGNED TXN:**

Here's the final raw Txn which sends BTC to 14zWNsgUMmHhYx4suzc2tZD6HieGbkQi5s.

`0100000001be66e10da854e7aea9338c1f91cd489768d1d6d7189f586d7a3613f2a24d5396000000008b483045022100da43201760bda697222002f56266bf65023fef2094519e13077f777baed553b102205ce35d05eabda58cd50a67977a65706347cc25ef43153e309ff210a134722e9e0141042daa93315eebbe2cb9b5c3505df4c6fb6caca8b756786098567550d4820c09db988fe9997d049d687292f815ccd6e7fb5c1b1a91137999818d17c73d0f80aef9ffffffff0123ce0100000000001976a9142bc89c2702e0e618db7d59eb5ce2f0f147b4075488ac00000000`

File Contents:

private.key = `5HvofFG7K1e2aeWESm5pbCzRHtCSiZNbfLYXBvxyA57DhKHV4U3`
txfile.tx = `0100000001be66e10da854e7aea9338c1f91cd489768d1d6d7189f586d7a3613f2a24d53960000000000ffffffff0123ce0100000000001976a9142bc89c2702e0e618db7d59eb5ce2f0f147b4075488ac00000000`
rawscript.sigpubkey.tx = `473044022054f60e8ae19411541597167362d12fc132e081a546c766bfd69c16d9d58e268a022048055c7fd8bf78e48543e8756bb3b26336df35b812a184119ba0e9d525bbb8aa0141042daa93315eebbe2cb9b5c3505df4c6fb6caca8b756786098567550d4820c09db988fe9997d049d687292f815ccd6e7fb5c1b1a91137999818d17c73d0f80aef9`
signed.tx = `0100000001be66e10da854e7aea9338c1f91cd489768d1d6d7189f586d7a3613f2a24d5396000000000b00000c0000000000000000ffffffff0123ce0100000000001976a9142bc89c2702e0e618db7d59eb5ce2f0f147b4075488ac00000000`


---

### Answer by username_2 on 2017-09-14. Score: 0

here is an example of transaction on `testnet`(<https://github.com/username_2/bitcoin-code/tree/testnet>), you need `private key`,`destination address` and `previous transacion hash` which has output address which relate to the given privatekey::

```
import hashlib
import txnUtils
from keyUtils import keyToAddr, addrToScriptPubkey

# From --> one input
privateKey = hashlib.sha256('abcdefghijklmnop').hexdigest() # mpSyb71528U8dQjuTCeDCcJqH8dQTyY13c
from_address= keyToAddr(privateKey,testnet=True)
txn_hash = "3c24ca820100153fb43434191d10464dd2dcd13f0c9aa07d15f7330e8bcd0596"

# To --> android testnet wallet
to_address = 'miD4PnSDWC2M725hvFBoBhNn8fbowoHnnS'

# Sign the Transction
signed_txn = txnUtils.makeSignedTransaction(
    privateKey, txn_hash, 0, addrToScriptPubkey(from_address), # input: has balance 0.01btc
    [[900000,addrToScriptPubkey(to_address)]] # outputs: 0.009btc 
)

print 'SIGNED TXN', signed_txn

# Broadcast this transaction(ie: signed_txn) at:
# https://testnet.blockexplorer.com/tx/send

```


---

