2

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.

2
  • 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 Jan 25, 2015 at 4:24
  • I am currently using Ubuntu with python 2.7.6.
    – Sebi
    Jan 25, 2015 at 8:46

4 Answers 4

3

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 and use method #2.

2

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

  1. pybitcointools (install using pip install pybitcointools for version 1.1.15 or pip install bitcoin for version 1.1.25 (recommended)
  2. sx/libbitcoin. Might need to install using mastercoin's install.sh script, 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 seems to be more self-explanatory than the brief sx tutorial.

7
  • +1 Do you happen to have an example of how to perform a transaction knowing only the WIF and a destination address?
    – Sebi
    Jan 25, 2015 at 12:52
  • From the manual page it is not very clear to me how the destination address is specified.
    – Sebi
    Jan 25, 2015 at 13:07
  • Sure. I'll do it once I'm off iOS. I'll need to play around and I'll post it here in ~12hrs Jan 26, 2015 at 12:24
  • @Sebi just to clarify, you want to create a raw Txn and send BTC from your address (ie private key) to another address, right? Jan 27, 2015 at 8:58
  • 1
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) 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

0

here is an example of transaction on testnet(https://github.com/suhailvs/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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.