main_txbump.py 6.2 KB

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