mmgen-txsend 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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-txsend: Broadcast a Bitcoin transaction to the network
  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 msg,check_infile,get_lines_from_file,confirm_or_exit
  28. prog_name = sys.argv[0].split("/")[-1]
  29. help_data = {
  30. 'prog_name': prog_name,
  31. 'desc': "Sign a Bitcoin transaction generated by mmgen-txcreate",
  32. 'usage': "[opts] <transaction file>",
  33. 'options': """
  34. -h, --help Print this help message
  35. -q, --quiet Suppress warnings; overwrite files without asking
  36. """
  37. }
  38. short_opts = "hq"
  39. long_opts = "help","quiet"
  40. opts,cmd_args = process_opts(sys.argv,help_data,short_opts,long_opts)
  41. if len(cmd_args) == 1:
  42. infile = cmd_args[0]
  43. check_infile(infile)
  44. else: usage(help_data)
  45. # Begin execution
  46. c = connect_to_bitcoind()
  47. tx_sig = get_lines_from_file(infile,"signed transaction")[0]
  48. from binascii import unhexlify
  49. try: unhexlify(tx_sig)
  50. except:
  51. msg("Invalid transaction data")
  52. sys.exit(3)
  53. msg("Successfully opened signed transaction file '%s'" % infile)
  54. confirm_or_exit("", "broadcast this transaction to the network")
  55. msg("Sending transaction")
  56. try:
  57. tx = c.sendrawtransaction(tx_sig)
  58. except:
  59. msg("Unable to send transaction")
  60. sys.exit(3)
  61. msg("Transaction sent: %s" % tx)
  62. print_sent_tx_to_file(tx)