main_txsign.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2019 The MMGen Project <mmgen@tuta.io>
  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 transaction generated by 'mmgen-txcreate'
  20. """
  21. from mmgen.common import *
  22. # -w, --use-wallet-dat (keys from running coin daemon) removed: use walletdump rpc instead
  23. opts_data = {
  24. 'sets': [('yes', True, 'quiet', True)],
  25. 'text': {
  26. 'desc': 'Sign cryptocoin transactions generated by {pnl}-txcreate'.format(pnl=g.proj_name.lower()),
  27. 'usage': '[opts] <transaction file>... [seed source]...',
  28. 'options': """
  29. -h, --help Print this help message
  30. --, --longhelp Print help message for long options (common options)
  31. -b, --brain-params=l,p Use seed length 'l' and hash preset 'p' for
  32. brainwallet input
  33. -d, --outdir= d Specify an alternate directory 'd' for output
  34. -D, --tx-id Display transaction ID and exit
  35. -e, --echo-passphrase Print passphrase to screen when typing it
  36. -E, --use-internal-keccak-module Force use of the internal keccak module
  37. -i, --in-fmt= f Input is from wallet format 'f' (see FMT CODES below)
  38. -H, --hidden-incog-input-params=f,o Read hidden incognito data from file
  39. 'f' at offset 'o' (comma-separated)
  40. -O, --old-incog-fmt Specify old-format incognito input
  41. -l, --seed-len= l Specify wallet seed length of 'l' bits. This option
  42. is required only for brainwallet and incognito inputs
  43. with non-standard (< {g.seed_len}-bit) seed lengths.
  44. -p, --hash-preset=p Use the scrypt hash parameters defined by preset 'p'
  45. for password hashing (default: '{g.hash_preset}')
  46. -z, --show-hash-presets Show information on available hash presets
  47. -k, --keys-from-file=f Provide additional keys for non-{pnm} addresses
  48. -K, --key-generator=m Use method 'm' for public key generation
  49. Options: {kgs} (default: {kg})
  50. -M, --mmgen-keys-from-file=f Provide keys for {pnm} addresses in a key-
  51. address file (output of '{pnl}-keygen'). Permits
  52. online signing without an {pnm} seed source. The
  53. key-address file is also used to verify {pnm}-to-{cu}
  54. mappings, so the user should record its checksum.
  55. -P, --passwd-file= f Get {pnm} wallet or {dn} passphrase from file 'f'
  56. -q, --quiet Suppress warnings; overwrite files without prompting
  57. -I, --info Display information about the transaction and exit
  58. -t, --terse-info Like '--info', but produce more concise output
  59. -v, --verbose Produce more verbose output
  60. -V, --vsize-adj= f Adjust transaction's estimated vsize by factor 'f'
  61. -y, --yes Answer 'yes' to prompts, suppress non-essential output
  62. """,
  63. 'notes': '\n{}'
  64. },
  65. 'code': {
  66. 'options': lambda s: s.format(
  67. g=g,pnm=g.proj_name,pnl=g.proj_name.lower(),dn=g.proto.daemon_name,
  68. kgs=' '.join(['{}:{}'.format(n,k) for n,k in enumerate(g.key_generators,1)]),
  69. kg=g.key_generator,
  70. cu=g.coin),
  71. 'notes': lambda s: s.format(
  72. help_notes('txsign'))
  73. }
  74. }
  75. infiles = opts.init(opts_data,add_opts=['b16'])
  76. if not infiles: opts.usage()
  77. for i in infiles: check_infile(i)
  78. if g.proto.sign_mode == 'daemon':
  79. rpc_init()
  80. if not opt.info and not opt.terse_info:
  81. do_license_msg(immed=True)
  82. from mmgen.txsign import *
  83. tx_files = get_tx_files(opt,infiles)
  84. seed_files = get_seed_files(opt,infiles)
  85. kal = get_keyaddrlist(opt)
  86. kl = get_keylist(opt)
  87. if kl and kal: kl.remove_dup_keys(kal)
  88. tx_num_str,bad_tx_count = '',0
  89. for tx_num,tx_file in enumerate(tx_files,1):
  90. if len(tx_files) > 1:
  91. msg('\nTransaction #{} of {}:'.format(tx_num,len(tx_files)))
  92. tx_num_str = ' #{}'.format(tx_num)
  93. tx = MMGenTX(tx_file)
  94. if tx.marked_signed():
  95. msg('Transaction is already signed!'); continue
  96. vmsg("Successfully opened transaction file '{}'".format(tx_file))
  97. if opt.tx_id:
  98. msg(tx.txid); continue
  99. if opt.info or opt.terse_info:
  100. tx.view(pause=False,terse=opt.terse_info); continue
  101. if not opt.yes:
  102. tx.view_with_prompt('View data for transaction{}?'.format(tx_num_str))
  103. if txsign(tx,seed_files,kl,kal,tx_num_str):
  104. if not opt.yes:
  105. tx.add_comment() # edits an existing comment
  106. tx.write_to_file(ask_write=not opt.yes,ask_write_default_yes=True,add_desc=tx_num_str)
  107. else:
  108. ymsg('Transaction could not be signed')
  109. bad_tx_count += 1
  110. if bad_tx_count:
  111. ydie(2,'{} transaction{} could not be signed'.format(bad_tx_count,suf(bad_tx_count)))