mmgen-txsign 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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-txsign: Sign a Bitcoin transaction generated by mmgen-txcreate
  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, check_infile, get_lines_from_file, my_getpass, my_raw_input
  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. <<<<<<< HEAD
  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 Just show info about the transaction and exit
  39. -q, --quiet Suppress warnings; overwrite files without asking
  40. =======
  41. -h, --help Print this help message
  42. -d, --outdir d Specify an alternate directory 'd' for output
  43. -e, --echo-passphrase Print passphrase to screen when typing it
  44. -i, --info Display information about the transaction and exit
  45. -q, --quiet Suppress warnings; overwrite files without asking
  46. >>>>>>> my
  47. """
  48. }
  49. short_opts = "hd:eiq"
  50. long_opts = "help","outdir=","echo_passphrase","info","quiet"
  51. opts,cmd_args = process_opts(sys.argv,help_data,short_opts,long_opts)
  52. # Exits on invalid input
  53. check_opts(opts, ('outdir',))
  54. if debug:
  55. print "Processed options: %s" % repr(opts)
  56. print "Cmd args: %s" % repr(cmd_args)
  57. if len(cmd_args) == 1:
  58. infile = cmd_args[0]
  59. check_infile(infile)
  60. else: usage(help_data)
  61. # Begin execution
  62. c = connect_to_bitcoind()
  63. tx_data = get_lines_from_file(infile,"transaction data")
  64. <<<<<<< HEAD
  65. timestamp,tx_hex,sig_data,inputs_data = parse_tx_data(tx_data)
  66. if 'info' in opts:
  67. view_tx_data(c,inputs_data,tx_hex,timestamp)
  68. =======
  69. metadata,tx_hex,sig_data,inputs_data = parse_tx_data(tx_data)
  70. if 'info' in opts:
  71. view_tx_data(c,inputs_data,tx_hex,metadata)
  72. >>>>>>> my
  73. sys.exit(0)
  74. if not 'quiet' in opts and not 'info' in opts: do_license_msg()
  75. msg("Successfully opened transaction file '%s'" % infile)
  76. if user_confirm("View transaction data? ",default_yes=False):
  77. <<<<<<< HEAD
  78. view_tx_data(c,inputs_data,tx_hex,timestamp)
  79. prompt = "Enter bitcoind passphrase: "
  80. =======
  81. view_tx_data(c,inputs_data,tx_hex,metadata)
  82. prompt = "Enter passphrase for bitcoind wallet: "
  83. >>>>>>> my
  84. if 'echo_passphrase' in opts:
  85. password = my_raw_input(prompt)
  86. else:
  87. password = my_getpass(prompt)
  88. wallet_enc = True
  89. from bitcoinrpc import exceptions
  90. try:
  91. c.walletpassphrase(password, 9999)
  92. except exceptions.WalletWrongEncState:
  93. msg("Wallet is unencrypted")
  94. wallet_enc = False
  95. except exceptions.WalletPassphraseIncorrect:
  96. msg("Passphrase incorrect")
  97. sys.exit(3)
  98. except exceptions.WalletAlreadyUnlocked:
  99. msg("WARNING: Wallet already unlocked!")
  100. <<<<<<< HEAD
  101. # signrawtransaction <hex string> [{"txid":txid,"vout":n,"scriptPubKey":hex,"redeemScript":hex},...] [<privatekey1>,...] [sighashtype="ALL"]
  102. try:
  103. sig_tx = c.signrawtransaction(tx_hex,sig_data)
  104. except:
  105. msg("Failed to sign transaction")
  106. if wallet_enc: c.walletlock()
  107. sys.exit(3)
  108. finally:
  109. if wallet_enc: c.walletlock()
  110. if not sig_tx['complete']:
  111. msg("signrawtransaction() returned failure")
  112. sys.exit(3)
  113. prompt = "Save signed transaction?"
  114. if user_confirm(prompt,default_yes=True):
  115. print_signed_tx_to_file(tx_hex,sig_tx['hex'],opts)
  116. =======
  117. else:
  118. msg("Passphrase OK")
  119. try:
  120. sig_tx = c.signrawtransaction(tx_hex,sig_data)
  121. # sig_tx = {'hex':"deadbeef0123456789ab",'complete':True}
  122. except:
  123. msg("Failed to sign transaction")
  124. if wallet_enc:
  125. c.walletlock()
  126. msg("Locking wallet")
  127. sys.exit(3)
  128. if wallet_enc:
  129. c.walletlock()
  130. msg("Locking wallet")
  131. if sig_tx['complete']:
  132. msg("Signing completed")
  133. else:
  134. msg("signrawtransaction() returned failure")
  135. sys.exit(3)
  136. prompt = "Save signed transaction?"
  137. if user_confirm(prompt,default_yes=True):
  138. print_signed_tx_to_file(tx_hex,sig_tx['hex'],metadata,opts)
  139. >>>>>>> my