mmgen-txsign 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. -h, --help Print this help message
  35. -d, --outdir d Specify an alternate directory 'd' for output
  36. -e, --echo-passphrase Print passphrase to screen when typing it
  37. -i, --info Just show info about the transaction and exit
  38. -q, --quiet Suppress warnings; overwrite files without asking
  39. """
  40. }
  41. short_opts = "hd:eiq"
  42. long_opts = "help","outdir=","echo_passphrase","info","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) == 1:
  50. infile = cmd_args[0]
  51. check_infile(infile)
  52. else: usage(help_data)
  53. # Begin execution
  54. c = connect_to_bitcoind()
  55. tx_data = get_lines_from_file(infile,"transaction data")
  56. timestamp,tx_hex,sig_data,inputs_data = parse_tx_data(tx_data)
  57. if 'info' in opts:
  58. view_tx_data(c,inputs_data,tx_hex,timestamp)
  59. sys.exit(0)
  60. if not 'quiet' in opts and not 'info' in opts: do_license_msg()
  61. msg("Successfully opened transaction file '%s'" % infile)
  62. if user_confirm("View transaction data? ",default_yes=False):
  63. view_tx_data(c,inputs_data,tx_hex,timestamp)
  64. prompt = "Enter bitcoind passphrase: "
  65. if 'echo_passphrase' in opts:
  66. password = my_raw_input(prompt)
  67. else:
  68. password = my_getpass(prompt)
  69. wallet_enc = True
  70. from bitcoinrpc import exceptions
  71. try:
  72. c.walletpassphrase(password, 9999)
  73. except exceptions.WalletWrongEncState:
  74. msg("Wallet is unencrypted")
  75. wallet_enc = False
  76. except exceptions.WalletPassphraseIncorrect:
  77. msg("Passphrase incorrect")
  78. sys.exit(3)
  79. except exceptions.WalletAlreadyUnlocked:
  80. msg("WARNING: Wallet already unlocked!")
  81. # signrawtransaction <hex string> [{"txid":txid,"vout":n,"scriptPubKey":hex,"redeemScript":hex},...] [<privatekey1>,...] [sighashtype="ALL"]
  82. try:
  83. sig_tx = c.signrawtransaction(tx_hex,sig_data)
  84. except:
  85. msg("Failed to sign transaction")
  86. if wallet_enc: c.walletlock()
  87. sys.exit(3)
  88. finally:
  89. if wallet_enc: c.walletlock()
  90. if not sig_tx['complete']:
  91. msg("signrawtransaction() returned failure")
  92. sys.exit(3)
  93. prompt = "Save signed transaction?"
  94. if user_confirm(prompt,default_yes=True):
  95. print_signed_tx_to_file(tx_hex,sig_tx['hex'],opts)