mmgen-txsend 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/usr/bin/env python
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C) 2013-2014 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-txsend: Broadcast a Bitcoin transaction to the network
  20. """
  21. import sys
  22. from mmgen.Opts import *
  23. from mmgen.license import *
  24. import mmgen.config as g
  25. from mmgen.tx import *
  26. from mmgen.util import msg,check_infile,get_lines_from_file,confirm_or_exit
  27. prog_name = sys.argv[0].split("/")[-1]
  28. help_data = {
  29. 'prog_name': prog_name,
  30. 'desc': "Send a Bitcoin transaction signed by mmgen-txsign",
  31. 'usage': "[opts] <signed transaction file>",
  32. 'options': """
  33. -h, --help Print this help message
  34. -d, --outdir d Specify an alternate directory 'd' for output
  35. -q, --quiet Suppress warnings; overwrite files without prompting
  36. """
  37. }
  38. short_opts = "hd:q"
  39. long_opts = "help","outdir=","quiet"
  40. opts,cmd_args = process_opts(sys.argv,help_data,short_opts,long_opts)
  41. if 'quiet' in opts: g.quiet = True
  42. check_opts(opts,long_opts)
  43. if len(cmd_args) == 1:
  44. infile = cmd_args[0]
  45. check_infile(infile)
  46. else: usage(help_data)
  47. # Begin execution
  48. try:
  49. metadata,tx_sig = get_lines_from_file(infile,"signed transaction")
  50. except:
  51. msg("Invalid signed transaction file")
  52. sys.exit(3)
  53. metadata = metadata.split()
  54. if len(metadata) != 3:
  55. msg("Invalid file header")
  56. sys.exit(3)
  57. from binascii import unhexlify
  58. try: unhexlify(tx_sig)
  59. except:
  60. msg("Invalid signed transaction data")
  61. sys.exit(3)
  62. do_license_msg()
  63. qmsg("Signed transaction file '%s' is valid" % infile)
  64. warn = "Once this transaction is sent, there's no taking it back!"
  65. what = "broadcast this transaction to the network"
  66. expect = "yes, i really want to do this".upper()
  67. confirm_or_exit(warn, what, expect)
  68. msg("Sending transaction")
  69. c = connect_to_bitcoind()
  70. try:
  71. tx = c.sendrawtransaction(tx_sig)
  72. except:
  73. msg("Unable to send transaction")
  74. sys.exit(3)
  75. msg("Transaction sent: %s" % tx)
  76. print_sent_tx_to_file(tx,metadata,opts)