7

Lets say I have a bitcoin address that has 10 BTC sent to it. I also have the private key for this address

Using Python (2 or 3) is there a way to create a signed transaction (which I believe is a long string of hex digits) that can then be sent to an external service (bockchain.info or whatever) for network propogation.

It seems all the python libraries for making transactions require a connection to the bitcoind rpc interface. In my application, there will be no locally ran bitcoind.

I'm thinking the code I'm looking for is like this:

address = '1ALis8zeW1XduXf98ZjoL4EKLen5mVA1q4'
private = '5KiUZd5as1TKsiwnt1KiPgiECtXiuF9BS1MxrAgedNrXcScm4d5'

from some_btc_library import make_transaction

tx_hex = make_transaction(
    inputs=[[address, private]],
    to='123rn4tNGhf1ZehQHLohYn8WRQYhjeGSCw',
    amount=3,
    miner_fee=0.0001
)

send_to_exteral_service(tx_hex)
print "transaction complete"
11

In case you have not seen it, there is a good (IMHO) article which describes how to generate a transaction "manually" using "raw" python (no bitcoin libs or RPC): "Bitcoins the hard way: Using the raw Bitcoin protocol"

8

Vitalik Buterin has recently shared Pybitcointools, a nice and simple-to-use Python library for handcrafting Bitcoin transactions. The example shows the basic workflow, which is pretty much what you have described in your example (just broken down to few more steps).

If you need more help, this nice article explains bitcoin transactions technically and shows examples using the Pybitcointools library.

1

I think that PyCoin would serve your needs.

This test case shows how to construct a transaction.

This test case shows how to use Wallet Import Format with PyCoin.

I'd also like to note that this is not a very good idea. Suppose that your input is actually 20 BTC instead of 10. What will happen is that Bitcoin will interpret the extra 10 BTC as a fee to be collected by the miner.

  • 1
    Note that in addition to the destination address and the private key, the other piece of data you need is the utxo (id and output number from a previous transaction) which is to be spent. Unless you happen to know what is, you'll need a Bitcoin client (or access to a copy of the block chain) to find an appropriate utxo given the private key. – Nate Eldredge Sep 17 '14 at 6:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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