{"id":326,"date":"2013-07-29T17:47:22","date_gmt":"2013-07-30T01:47:22","guid":{"rendered":"http:\/\/blog.richardkiss.com\/?p=326"},"modified":"2021-02-05T17:30:13","modified_gmt":"2021-02-06T01:30:13","slug":"command-line-bitcoin-transactions","status":"publish","type":"post","link":"https:\/\/blog.richardkiss.com\/?p=326","title":{"rendered":"Command-line Bitcoin Transactions"},"content":{"rendered":"<p><strong>The &#8220;spend&#8221; tool is obsolete, which makes this post not-so-useful. Look at <a href=\"https:\/\/github.com\/richardkiss\/pycoin\/blob\/master\/COMMAND-LINE-TOOLS.md\" title=\"https:\/\/github.com\/richardkiss\/pycoin\/blob\/master\/COMMAND-LINE-TOOLS.md\" target=\"_blank\" rel=\"noopener\">the &#8220;tx&#8221; tool mentioned in this post<\/a> instead.<\/strong><\/p>\n<p>Creating a signed transaction is the holy grail of Bitcoin. Without this functionality, a Bitcoin client can&#8217;t really be called a client. Being able to sign a transaction though&#8230; wow! The whole Bitcoin world is at your fingertips!<\/p>\n<p>OK, that may be a bit of an overstatement&#8230; but it&#8217;s still pretty neat stuff.<\/p>\n<p>My <a href=\"https:\/\/github.com\/richardkiss\/pycoin\">pycoin<\/a> project features a Python command-line script &#8220;spend&#8221; that will let you generate a standard transaction that reassigns coins from one set of addresses to another set. It&#8217;s obviously not nearly as easy as using a GUI app to spend your bitcoin. But it is very simple-to-follow sample code that you can use as a template should you want to create your own Bitcoin transactions programmatically.<\/p>\n<p>To create a transaction, you need the coin sources. These are the TxOut portions of the transactions that assigned bitcoin to an address. The &#8220;spend&#8221; script takes bitcoin addresses as input, and queries the blockchain.info web site to get the list of unclosed spendable transactions. This script &#8220;spends&#8221; all your bitcoin on the addresses you give. No problem though: you can send coins you want to keep (the &#8220;change&#8221;) to an address you control.<\/p>\n<p>Here&#8217;s an example blockchain.info query:<\/p>\n<p><a href=\"http:\/\/blockchain.info\/unspent?active=12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX\">http:\/\/blockchain.info\/unspent?active=12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX<\/a><\/p>\n<p>(If Satoshi ever gets around to spending all these coins, this URL will throw a 500.)<\/p>\n<p>Of course, it&#8217;s not enough to have the sources for the coins; you need the private key. Although pycoin deals with this private key in its native &#8220;secret exponent&#8221; numerical form, the spend script expects WIF format. So you need to create a file with the WIF. How you get the WIF depends on how you generated the address. In Electrum, right-click on the address and choose &#8220;Private Key&#8221;, and the WIF will be displayed.<\/p>\n<p>For maximal security, you might think about creating this WIF file on a RAM disk. On Mac OS X,<\/p>\n<p><code>diskutil erasevolume HFS+ RAMDISK `hdiutil attach -nomount ram:\/\/2048`<br \/>\n<\/code><br \/>\nand it will mount in \/Volumes\/RAMDISK.<\/p>\n<p>Create a &#8220;wifs&#8221; text file, and add the WIF elements, one per line. You can add extraneous WIF with no worries; just make sure you have a WIF entry for every source address so the &#8220;spend&#8221; script has the information it needs to sign the outgoing transactions.<\/p>\n<p><code>cat >> \/Volumes\/RAMDISK\/wifs<br \/>\n(paste WIF)<br \/>\n<\/code><\/p>\n<p>Since it&#8217;s on a RAM disk, you don&#8217;t have to worry about someone scraping your unused drive sectors later. Unless that RAM gets paged to disk. Oh no, a security rabbit hole!! <strong>HELP<\/strong><\/p>\n<p>You also need to specify where you want the coins to go. That&#8217;s a simple list of (bitcoin address, coin value) pairs.<\/p>\n<p>Those three elements &#8212; coin source, private keys, coin destination &#8212; are all you need to create a transaction.<\/p>\n<p><code>$ spend -s 1BHeaS4vn9NsA6Fi4KFYREGzYmdhdZuJhH -f \/Volumes\/RAMDISK\/wifs -d 15jpiqB2AHb2iwi83KJNxzohouxd1PEDyu\/0.04476<br \/>\ntransaction fee: 0 BTC<br \/>\nwarning: transaction fee lower than (casually calculated) expected value of 0.00010000 BTC, transaction might not propogate<br \/>\ncopy the following hex to http:\/\/blockchain.info\/pushtx to put the transaction on the network:<\/p>\n<p>0100000001068ded23b986c6fc01106596ccbabfc157c850569359ea082ab2e813f277142201\\<br \/>\n0000008b483045022100ef5b8504a227f967a7a7c2af55e79f9886e4f9182027745fcdb7fcc4\\<br \/>\nb8afdf830220421704dd397cee26e1634e458b1678d80c37cafdf13a0f41a6dd98bff350d9f9\\<br \/>\n014104430ba52cd1a88b7049295ceee2e6576af7211e8a9b01b70e572ff70f4718699a64b4c8\\<br \/>\nea0dd3ddb3c7dab0ee53dcc4e70c6913ee0cf904853da94422b5c1b289ffffffff01604c4400\\<br \/>\n000000001976a91433f9cf2774a90c53a3e55e5e8bbb5cb3d1090a9988ac00000000<br \/>\n<\/code><\/p>\n<p>The spend script also displays how much bitoin is unaccounted for in outputs. This amount becomes a transaction fee for the block.<\/p>\n<p>The script generates the transaction and displays it as hex on the screen. It does not send it to the network. You can paste the hex onto blockchain.info to get the transaction on the network and into a block.<\/p>\n<p>Note that if you generate what seems like should be the exact same transaction again, you will (hopefully) get a different hex output. That&#8217;s because part of the process of signing a transaction uses a randomly generated value K. If the value for K is known, observers can work backwards to figure out the secret key from the signature generated. In fact, if you use the same value for K to sign two different transactions, the secret key can be recovered. So it&#8217;s important that this K value be generated securely, using a cryptographically-safe random number generator.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The &#8220;spend&#8221; tool is obsolete, which makes this post not-so-useful. Look at the &#8220;tx&#8221; tool mentioned in this post instead. Creating a signed transaction is the holy grail of Bitcoin. Without this functionality, a Bitcoin client can&#8217;t really be called a client. Being able to sign a transaction though&#8230; wow! The whole Bitcoin world is &hellip; <a href=\"https:\/\/blog.richardkiss.com\/?p=326\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Command-line Bitcoin Transactions<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-326","post","type-post","status-publish","format-standard","hentry","category-computers"],"_links":{"self":[{"href":"https:\/\/blog.richardkiss.com\/index.php?rest_route=\/wp\/v2\/posts\/326","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.richardkiss.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.richardkiss.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.richardkiss.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.richardkiss.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=326"}],"version-history":[{"count":42,"href":"https:\/\/blog.richardkiss.com\/index.php?rest_route=\/wp\/v2\/posts\/326\/revisions"}],"predecessor-version":[{"id":599,"href":"https:\/\/blog.richardkiss.com\/index.php?rest_route=\/wp\/v2\/posts\/326\/revisions\/599"}],"wp:attachment":[{"href":"https:\/\/blog.richardkiss.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=326"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.richardkiss.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=326"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.richardkiss.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=326"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}