mmgen-txsign 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 mmgen.Opts import *
  23. from mmgen.license import *
  24. import mmgen.config as g
  25. from mmgen.tx import *
  26. from mmgen.utils import msg
  27. help_data = {
  28. 'prog_name': sys.argv[0].split("/")[-1],
  29. 'desc': "Sign a Bitcoin transaction generated by mmgen-txcreate",
  30. 'usage': "[opts] <transaction file> [mmgen wallet/seed/words/brain file]...",
  31. 'options': """
  32. -h, --help Print this help message
  33. -d, --outdir d Specify an alternate directory 'd' for output
  34. -e, --echo-passphrase Print passphrase to screen when typing it
  35. -i, --info Display information about the transaction and exit
  36. -I, --tx_id Display transaction ID and exit
  37. -k, --keys-from-file k Provide additional key data from file 'k'
  38. -P, --passwd-file f Get passphrase from file 'f'
  39. -q, --quiet Suppress warnings; overwrite files without
  40. prompting
  41. -b, --from-brain l,p Generate keys from a user-created password,
  42. i.e. a "brainwallet", using seed length 'l' and
  43. hash preset 'p' (comma-separated)
  44. -m, --from-mnemonic Generate keys from an electrum-like mnemonic
  45. -s, --from-seed Generate keys from a seed in .{} format
  46. -w, --use-wallet-dat Use the keys in the bitcoind wallet.dat file too
  47. Transactions with either mmgen or non-mmgen input addresses may be signed.
  48. For non-mmgen inputs, the bitcoind wallet.dat is used as the key source.
  49. For mmgen inputs, key data is generated from your seed as with the
  50. mmgen-addrgen and mmgen-keygen utilities.
  51. Data for the --from-<what> options will be taken from a file if a second
  52. file is specified on the command line. Otherwise, the user will be
  53. prompted to enter the data.
  54. In cases of transactions with mixed mmgen and non-mmgen inputs, non-mmgen
  55. keys must be supplied in a separate file (WIF format, one key per line)
  56. using the '--keys-from-file' option. Alternatively, one may import the
  57. required mmgen keys into the bitcoind wallet.dat and use the
  58. '--force-wallet-dat' option.
  59. Seed data supplied in files must have the following extensions:
  60. wallet: '.{}'
  61. seed: '.{}'
  62. mnemonic: '.{}'
  63. brainwallet: '.{}'
  64. """.format(g.seed_ext,g.wallet_ext,g.seed_ext,g.mn_ext,g.brain_ext)
  65. }
  66. short_opts = "hd:eiIk:P:qb:msw"
  67. long_opts = "help","outdir=","echo_passphrase","info","tx_id",\
  68. "keys_from_file=","passwd_file=","quiet","from_brain=",\
  69. "from_mnemonic","from_seed","use_wallet_dat"
  70. opts,infiles = process_opts(sys.argv,help_data,short_opts,long_opts)
  71. check_opts(opts,long_opts)
  72. if not infiles: usage(help_data)
  73. for i in infiles: check_infile(i)
  74. # Begin execution
  75. c = connect_to_bitcoind()
  76. tx_file = infiles.pop(0)
  77. m = "" if 'tx_id' in opts else "transaction data"
  78. tx_data = get_lines_from_file(tx_file,m)
  79. metadata,tx_hex,sig_data,inputs_data = parse_tx_data(tx_data,tx_file)
  80. if 'tx_id' in opts:
  81. msg(metadata[0])
  82. sys.exit(0)
  83. if 'info' in opts:
  84. view_tx_data(c,inputs_data,tx_hex,metadata)
  85. sys.exit(0)
  86. if not 'quiet' in opts: do_license_msg(immed=True)
  87. msg("Successfully opened transaction file '%s'" % tx_file)
  88. prompt = "View transaction data? (y)es, (N)o, (v)iew in pager"
  89. reply = prompt_and_get_char(prompt,"YyNnVv",enter_ok=True)
  90. if reply and reply in "YyVv":
  91. p = True if reply in "Vv" else False
  92. view_tx_data(c,inputs_data,tx_hex,metadata,pager=p)
  93. # Are inputs mmgen addresses?
  94. mmgen_addrs = [i for i in inputs_data if verify_mmgen_label(i['account'])]
  95. other_addrs = [i for i in inputs_data if not verify_mmgen_label(i['account'])]
  96. keys = get_lines_from_file(opts['keys_from_file'],"key data") \
  97. if 'keys_from_file' in opts else []
  98. if mmgen_addrs:
  99. if other_addrs and not keys and not 'use_wallet_dat' in opts:
  100. missing_keys_errormsg(other_addrs)
  101. sys.exit(2)
  102. keys += get_keys_for_mmgen_addrs(mmgen_addrs,infiles,opts)
  103. if 'use_wallet_dat' in opts:
  104. sig_tx = sign_tx_with_bitcoind_wallet(c,tx_hex,sig_data,keys,opts)
  105. else:
  106. sig_tx = sign_transaction(c,tx_hex,sig_data,keys)
  107. elif other_addrs:
  108. if 'use_wallet_dat' in opts:
  109. sig_tx = sign_tx_with_bitcoind_wallet(c,tx_hex,sig_data,keys,opts)
  110. else:
  111. if keys:
  112. sig_tx = sign_transaction(c,tx_hex,sig_data,keys)
  113. else:
  114. missing_keys_errormsg(other_addrs)
  115. sys.exit(2)
  116. if sig_tx['complete']:
  117. msg("Signing completed")
  118. prompt = "Save signed transaction?"
  119. if user_confirm(prompt,default_yes=True):
  120. print_signed_tx_to_file(tx_hex,sig_tx['hex'],metadata,opts)
  121. else:
  122. msg("Some keys were missing. Transaction could not be signed.")
  123. sys.exit(3)