mmgen-txsend 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. import mmgen.config as g
  23. from mmgen.Opts import *
  24. from mmgen.license import *
  25. from mmgen.tx import *
  26. from mmgen.util import msg,check_infile,get_lines_from_file,confirm_or_exit
  27. help_data = {
  28. 'prog_name': g.prog_name,
  29. 'desc': "Send a Bitcoin transaction signed by {}-txsign".format(g.proj_name.lower()),
  30. 'usage': "[opts] <signed transaction file>",
  31. 'options': """
  32. -h, --help Print this help message
  33. -d, --outdir= d Specify an alternate directory 'd' for output
  34. -q, --quiet Suppress warnings; overwrite files without prompting
  35. """
  36. }
  37. opts,cmd_args = parse_opts(sys.argv,help_data)
  38. if 'quiet' in opts: g.quiet = True
  39. if len(cmd_args) == 1:
  40. infile = cmd_args[0]; check_infile(infile)
  41. else: usage(help_data)
  42. # Begin execution
  43. do_license_msg()
  44. tx_data = get_lines_from_file(infile,"signed transaction data")
  45. metadata,tx_hex,inputs_data,b2m_map = parse_tx_data(tx_data,infile)
  46. qmsg("Signed transaction file '%s' is valid" % infile)
  47. c = connect_to_bitcoind()
  48. prompt = "View transaction data? (y)es, (N)o, (v)iew in pager"
  49. reply = prompt_and_get_char(prompt,"YyNnVv",enter_ok=True)
  50. if reply and reply in "YyVv":
  51. p = True if reply in "Vv" else False
  52. view_tx_data(c,inputs_data,tx_hex,b2m_map,metadata,pager=p)
  53. warn = "Once this transaction is sent, there's no taking it back!"
  54. what = "broadcast this transaction to the network"
  55. expect = "YES, I REALLY WANT TO DO THIS"
  56. if g.quiet: warn,expect = "","YES"
  57. confirm_or_exit(warn, what, expect)
  58. msg("Sending transaction")
  59. try:
  60. tx_id = c.sendrawtransaction(tx_hex)
  61. except:
  62. msg("Unable to send transaction")
  63. sys.exit(3)
  64. msg("Transaction sent: %s" % tx_id)
  65. of = "tx_{}[{}].out".format(*metadata[:2])
  66. write_to_file(of, tx_id+"\n",opts,"transaction ID",True,True)