main_txbump.py 7.6 KB

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