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.
Bitcoin Stack Exchange is a question and answer site for Bitcoin users, developers, and enthusiasts. It only takes a minute to sign up.
Sign up to join this communityIs 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.
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:
You have a local bitcoind or btcd installed which would let you query for transactions, at the expense of downloading the entire blockchain.
You use an electrum or some other SPV server to query for transactions, at the expense of connecting to these nodes.
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.
For Ubuntu, Python 2.7 (same setup as me) I'd highly recommend either:
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.
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):
DECODED_ADDR=$(cat private.key | sx addr | sx decode-addr) # hash160
PREVOUT_SCRIPT=$(sx rawscript dup hash160 [ $DECODED_ADDR ] equalverify checksig) # hash160 + script padding
SIGNATURE=$(cat private.key | sx sign-input txfile.tx 0 $PREVOUT_SCRIPT) # 72 digit der signature
SIGNATURE_AND_PUBKEY_SCRIPT=$(sx rawscript [ $SIGNATURE ] [ $(cat private.key | sx pubkey) ]) # 278 digit script/sig/pubkey
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
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