mmgen-txcreate 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #!/usr/bin/env python
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C) 2013 by philemon <mmgen-py@yandex.com>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. mmgen-txcreate: Create a BTC transaction, sending to specified addresses
  20. """
  21. import sys
  22. #from hashlib import sha256
  23. from mmgen.Opts import *
  24. from mmgen.license import *
  25. from mmgen.config import *
  26. from mmgen.tx import *
  27. from mmgen.utils import check_opts, msg, user_confirm
  28. from decimal import Decimal
  29. prog_name = sys.argv[0].split("/")[-1]
  30. help_data = {
  31. 'prog_name': prog_name,
  32. 'desc': "Create a BTC transaction, sending to specified addresses",
  33. 'usage': "[opts] <address:amount>[,...] <transaction fee> <change address>",
  34. 'options': """
  35. -h, --help Print this help message
  36. -d, --outdir d Specify an alternate directory 'd' for output
  37. -e, --echo-passphrase Print passphrase to screen when typing it
  38. -i, --info Display unspent outputs and exit
  39. -q, --quiet Suppress warnings; overwrite files without asking
  40. Outputs to spend are chosen by the user via a menu.
  41. Ages of transactions are approximate based on an estimated block discovery
  42. time of %s minutes.
  43. """ % mins_per_block
  44. }
  45. short_opts = "hd:eiq"
  46. long_opts = "help","outdir=","echo_passphrase","info","quiet"
  47. opts,cmd_args = process_opts(sys.argv,help_data,short_opts,long_opts)
  48. # Exits on invalid input
  49. check_opts(opts, ('outdir',))
  50. if debug:
  51. print "Processed options: %s" % repr(opts)
  52. print "Cmd args: %s" % repr(cmd_args)
  53. if len(cmd_args) == 3:
  54. rcpt_arg,tx_fee,change_addr = cmd_args
  55. check_address(change_addr)
  56. elif len(cmd_args) == 2:
  57. rcpt_arg,tx_fee = cmd_args
  58. change_addr = ""
  59. elif len(cmd_args) == 0 and 'info' in opts:
  60. pass
  61. else: usage(help_data)
  62. if not 'info' in opts:
  63. tx_out = make_tx_out(rcpt_arg)
  64. for i in tx_out.keys(): check_address(i)
  65. for i in tx_out.values(): check_btc_amt(i)
  66. tx_fee = check_btc_amt(tx_fee)
  67. # Begin execution
  68. c = connect_to_bitcoind()
  69. if not 'quiet' in opts and not 'info' in opts: do_license_msg()
  70. # Begin test
  71. # import mmgen.rpc
  72. # us = eval(get_data_from_file("listunspent.json"))
  73. # End test
  74. us = c.listunspent()
  75. # write_to_file("listunspent.json",repr(us))
  76. # sys.exit()
  77. unspent = sort_and_view(us)
  78. total = trim_exponent(sum([i.amount for i in unspent]))
  79. msg("Total unspent: %s BTC" % total)
  80. if 'info' in opts: sys.exit(0)
  81. send_amt = sum(tx_out.values())
  82. msg("Total amount to spend: %s BTC\n%s unspent outputs total" %
  83. (send_amt, len(unspent)))
  84. while True:
  85. sel_nums = select_outputs(unspent,"Choose the outputs to spend: ")
  86. msg("Selected outputs: %s" % " ".join(str(i) for i in sel_nums))
  87. sel_unspent = [unspent[i-1] for i in sel_nums]
  88. lbls = set([verify_mmgen_label(
  89. i.account,return_str=True,check_label_len=True)
  90. for i in sel_unspent])
  91. lbls.discard("")
  92. if lbls and len(lbls) < len(sel_unspent):
  93. msg(txmsg['mixed_inputs'] % ", ".join(sorted(lbls)))
  94. if not user_confirm("Accept?"):
  95. continue
  96. total_in = trim_exponent(sum([o.amount for o in sel_unspent]))
  97. change = trim_exponent(total_in - (send_amt + tx_fee))
  98. if change >= 0:
  99. prompt = "Transaction produces %s BTC in change. OK?" % change
  100. if user_confirm(prompt,default_yes=True):
  101. break
  102. else:
  103. msg(txmsg['not_enough_btc'] % change)
  104. if change > 0 and not change_addr:
  105. msg(txmsg['throwaway_change'] % (change, total_in-tx_fee))
  106. sys.exit(2)
  107. tx_in = [{"txid":i.txid, "vout":i.vout} for i in sel_unspent]
  108. for i in tx_out.keys(): tx_out[i] = float(tx_out[i])
  109. if change: tx_out[change_addr] = float(change)
  110. tx_hex = c.createrawtransaction(tx_in,tx_out)
  111. prompt = "Transaction successfully created\nView decoded transaction?"
  112. if user_confirm(prompt,default_yes=False):
  113. view_tx_data(c,[i.__dict__ for i in sel_unspent],tx_hex)
  114. prompt = "Save transaction?"
  115. if user_confirm(prompt,default_yes=True):
  116. print_tx_to_file(tx_hex,sel_unspent,send_amt,opts)