main_txsign.py 6.0 KB

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