main_txbump.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2023 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-txbump: Increase the fee on a replaceable (replace-by-fee) MMGen
  20. transaction, and optionally sign and send it
  21. """
  22. import mmgen.opts as opts
  23. from .cfg import gc,Config
  24. from .util import msg,msg_r,die,async_run
  25. from .color import green
  26. from .wallet import Wallet
  27. opts_data = {
  28. 'sets': [('yes', True, 'quiet', True)],
  29. 'text': {
  30. 'desc': f"""
  31. Increase the fee on a replaceable (RBF) {gc.proj_name} transaction,
  32. creating a new transaction, and optionally sign and send the
  33. new transaction
  34. """,
  35. 'usage': f'[opts] <{gc.proj_name} TX file> [seed source] ...',
  36. 'options': """
  37. -h, --help Print this help message
  38. --, --longhelp Print help message for long options (common options)
  39. -b, --brain-params=l,p Use seed length 'l' and hash preset 'p' for
  40. brainwallet input
  41. -c, --comment-file= f Source the transaction's comment from file 'f'
  42. -d, --outdir= d Specify an alternate directory 'd' for output
  43. -e, --echo-passphrase Print passphrase to screen when typing it
  44. -f, --fee= f Transaction fee, as a decimal {cu} amount or as
  45. {fu} (an integer followed by {fl!r}).
  46. See FEE SPECIFICATION below.
  47. -H, --hidden-incog-input-params=f,o Read hidden incognito data from file
  48. 'f' at offset 'o' (comma-separated)
  49. -i, --in-fmt= f Input is from wallet format 'f' (see FMT CODES below)
  50. -l, --seed-len= l Specify wallet seed length of 'l' bits. This option
  51. is required only for brainwallet and incognito inputs
  52. with non-standard (< {dsl}-bit) seed lengths.
  53. -k, --keys-from-file=f Provide additional keys for non-{pnm} addresses
  54. -K, --keygen-backend=n Use backend 'n' for public key generation. Options
  55. for {coin_id}: {kgs}
  56. -M, --mmgen-keys-from-file=f Provide keys for {pnm} addresses in a key-
  57. address file (output of '{pnl}-keygen'). Permits
  58. online signing without an {pnm} seed source. The
  59. key-address file is also used to verify {pnm}-to-{cu}
  60. mappings, so the user should record its checksum.
  61. -o, --output-to-reduce=o Deduct the fee from output 'o' (an integer, or 'c'
  62. for the transaction's change output, if present)
  63. -O, --old-incog-fmt Specify old-format incognito input
  64. -p, --hash-preset= p Use the scrypt hash parameters defined by preset 'p'
  65. for password hashing (default: '{gc.dfl_hash_preset}')
  66. -P, --passwd-file= f Get {pnm} wallet passphrase from file 'f'
  67. -q, --quiet Suppress warnings; overwrite files without prompting
  68. -s, --send Sign and send the transaction (the default if seed
  69. data is provided)
  70. -v, --verbose Produce more verbose output
  71. -y, --yes Answer 'yes' to prompts, suppress non-essential output
  72. -z, --show-hash-presets Show information on available hash presets
  73. """,
  74. 'notes': """
  75. {}{}
  76. Seed source files must have the canonical extensions listed in the 'FileExt'
  77. column below:
  78. FMT CODES:
  79. {f}
  80. """
  81. },
  82. 'code': {
  83. 'options': lambda cfg,help_notes,proto,s: s.format(
  84. cfg=cfg,
  85. gc=gc,
  86. pnm=gc.proj_name,
  87. pnl=gc.proj_name.lower(),
  88. fu=help_notes('rel_fee_desc'),
  89. fl=help_notes('fee_spec_letters'),
  90. kgs=help_notes('keygen_backends'),
  91. coin_id=help_notes('coin_id'),
  92. dsl=help_notes('dfl_seed_len'),
  93. cu=proto.coin),
  94. 'notes': lambda help_notes,s: s.format(
  95. help_notes('fee'),
  96. help_notes('txsign'),
  97. f=help_notes('fmt_codes')),
  98. }
  99. }
  100. cfg = Config(opts_data=opts_data)
  101. tx_file = cfg._args.pop(0)
  102. from .fileutil import check_infile
  103. check_infile(tx_file)
  104. from .tx import CompletedTX,BumpTX,UnsignedTX,OnlineSignedTX
  105. from .tx.sign import txsign,get_seed_files,get_keyaddrlist,get_keylist
  106. seed_files = get_seed_files(cfg,cfg._args) if (cfg._args or cfg.send) else None
  107. from .ui import do_license_msg
  108. do_license_msg(cfg)
  109. silent = cfg.yes and cfg.fee != None and cfg.output_to_reduce != None
  110. async def main():
  111. orig_tx = await CompletedTX(cfg=cfg,filename=tx_file)
  112. if not silent:
  113. msg(green('ORIGINAL TRANSACTION'))
  114. msg(orig_tx.info.format(terse=True))
  115. kal = get_keyaddrlist(cfg,orig_tx.proto)
  116. kl = get_keylist(cfg,orig_tx.proto)
  117. sign_and_send = bool(seed_files or kl or kal)
  118. from .tw.ctl import TwCtl
  119. tx = await BumpTX(
  120. cfg = cfg,
  121. data = orig_tx.__dict__,
  122. send = sign_and_send,
  123. twctl = await TwCtl(cfg,orig_tx.proto) if orig_tx.proto.tokensym else None )
  124. from .rpc import rpc_init
  125. tx.rpc = await rpc_init(cfg,tx.proto)
  126. msg('Creating replacement transaction')
  127. tx.check_sufficient_funds_for_bump()
  128. output_idx = tx.choose_output()
  129. if not silent:
  130. msg(f'Minimum fee for new transaction: {tx.min_fee.hl()} {tx.proto.coin}')
  131. tx.usr_fee = tx.get_usr_fee_interactive(fee=cfg.fee,desc='User-selected')
  132. tx.bump_fee(output_idx,tx.usr_fee)
  133. assert tx.fee <= tx.proto.max_tx_fee
  134. if not cfg.yes:
  135. tx.add_comment() # edits an existing comment
  136. await tx.create_serialized(bump=True)
  137. tx.add_timestamp()
  138. tx.add_blockcount()
  139. cfg._util.qmsg('Fee successfully increased')
  140. if not silent:
  141. msg(green('\nREPLACEMENT TRANSACTION:'))
  142. msg_r(tx.info.format(terse=True))
  143. if sign_and_send:
  144. tx2 = UnsignedTX(cfg=cfg,data=tx.__dict__)
  145. tx3 = await txsign(cfg,tx2,seed_files,kl,kal)
  146. if tx3:
  147. tx4 = await OnlineSignedTX(cfg=cfg,data=tx3.__dict__)
  148. tx4.file.write(ask_write=False)
  149. await tx4.send(exit_on_fail=True)
  150. tx4.file.write(ask_write=False)
  151. else:
  152. die(2,'Transaction could not be signed')
  153. else:
  154. tx.file.write(ask_write=not cfg.yes,ask_write_default_yes=False,ask_overwrite=not cfg.yes)
  155. async_run(main())