main_txsign.py 5.6 KB

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