mmgen-txcreate 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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: Send BTC from specified outputs 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': "Send BTC from specified outputs to specified addresses",
  33. 'usage': "[opts] <recipient 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. -q, --quiet Suppress warnings; overwrite files without asking
  39. """
  40. }
  41. short_opts = "hd:eq"
  42. long_opts = "help","outdir=","echo_passphrase","quiet"
  43. opts,cmd_args = process_opts(sys.argv,help_data,short_opts,long_opts)
  44. # Exits on invalid input
  45. check_opts(opts, ('outdir',))
  46. if debug:
  47. print "Processed options: %s" % repr(opts)
  48. print "Cmd args: %s" % repr(cmd_args)
  49. if len(cmd_args) == 4:
  50. rcpt_addr,send_amt,tx_fee,change_addr = cmd_args
  51. check_address(change_addr)
  52. elif len(cmd_args) == 3:
  53. rcpt_addr,send_amt,tx_fee = cmd_args
  54. change_addr = ""
  55. else: usage(help_data)
  56. check_address(rcpt_addr)
  57. send_amt = check_btc_amt(send_amt)
  58. tx_fee = check_btc_amt(tx_fee)
  59. # Begin execution
  60. c = connect_to_bitcoind()
  61. if not 'quiet' in opts: do_license_msg()
  62. unspent = sort_and_view(c.listunspent())
  63. total = remove_exponent(sum([i.amount for i in unspent]))
  64. msg("Total unspent: %s BTC" % total)
  65. msg("Amount to spend: %s BTC" % send_amt)
  66. msg("%s unspent outputs total" % len(unspent))
  67. sel_unspent = select_outputs(unspent,"Choose the outputs to spend: ")
  68. total_in = remove_exponent(sum([o.amount for o in sel_unspent]))
  69. change = remove_exponent(total_in - (send_amt + tx_fee))
  70. if change < 0:
  71. msg(txmsg['not_enough_btc'] % change)
  72. sys.exit(2)
  73. elif change > 0 and not change_addr:
  74. msg(txmsg['throwaway_change'] % (change, total_in-tx_fee))
  75. sys.exit(2)
  76. tx_in = [{"txid":i.txid, "vout":i.vout} for i in sel_unspent]
  77. tx_out = {rcpt_addr:float(send_amt), change_addr:float(change)}
  78. tx_hex = c.createrawtransaction(tx_in,tx_out)
  79. msg("Transaction successfully created\n")
  80. prompt = "View decoded transaction?"
  81. if user_confirm(prompt,default_yes=False):
  82. view_tx_data(c,[i.__dict__ for i in sel_unspent],tx_hex)
  83. prompt = "Save transaction?"
  84. if user_confirm(prompt,default_yes=True):
  85. print_tx_to_file(tx_hex,sel_unspent,send_amt,opts)