main_txbump.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2019 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 mmgen.common import *
  23. from mmgen.seed import SeedSource
  24. opts_data = {
  25. 'sets': [('yes', True, 'quiet', True)],
  26. 'text': {
  27. 'desc': """
  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. """.format(g=g),
  32. 'usage': '[opts] <{g.proj_name} TX file> [seed source] ...'.format(g=g),
  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}).
  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 (< {g.seed_len}-bit) seed lengths.
  50. -k, --keys-from-file=f Provide additional keys for non-{pnm} addresses
  51. -K, --key-generator= m Use method 'm' for public key generation
  52. Options: {kgs}
  53. (default: {kg})
  54. -M, --mmgen-keys-from-file=f Provide keys for {pnm} addresses in a key-
  55. address file (output of '{pnl}-keygen'). Permits
  56. online signing without an {pnm} seed source. The
  57. key-address file is also used to verify {pnm}-to-{cu}
  58. mappings, so the user should record its checksum.
  59. -o, --output-to-reduce=o Deduct the fee from output 'o' (an integer, or 'c'
  60. for the transaction's change output, if present)
  61. -O, --old-incog-fmt Specify old-format incognito input
  62. -p, --hash-preset= p Use the scrypt hash parameters defined by preset 'p'
  63. for password hashing (default: '{g.hash_preset}')
  64. -P, --passwd-file= f Get {pnm} wallet or {dn} passphrase from file 'f'
  65. -q, --quiet Suppress warnings; overwrite files without prompting
  66. -s, --send Sign and send the transaction (the default if seed
  67. data is provided)
  68. -v, --verbose Produce more verbose output
  69. -y, --yes Answer 'yes' to prompts, suppress non-essential output
  70. -z, --show-hash-presets Show information on available hash presets
  71. """,
  72. 'notes': """
  73. {}{}
  74. Seed source files must have the canonical extensions listed in the 'FileExt'
  75. column below:
  76. {f}
  77. """
  78. },
  79. 'code': {
  80. 'options': lambda s: s.format(
  81. g=g,pnm=g.proj_name,pnl=g.proj_name.lower(),dn=g.proto.daemon_name,
  82. fu=help_notes('rel_fee_desc'),fl=help_notes('fee_spec_letters'),
  83. kgs=' '.join(['{}:{}'.format(n,k) for n,k in enumerate(g.key_generators,1)]),
  84. kg=g.key_generator,
  85. cu=g.coin),
  86. 'notes': lambda s: s.format(
  87. help_notes('fee'),
  88. help_notes('txsign'),
  89. f='\n '.join(SeedSource.format_fmt_codes().splitlines()))
  90. }
  91. }
  92. cmd_args = opts.init(opts_data)
  93. rpc_init()
  94. tx_file = cmd_args.pop(0)
  95. check_infile(tx_file)
  96. from mmgen.tx import *
  97. from mmgen.txsign import *
  98. seed_files = get_seed_files(opt,cmd_args) if (cmd_args or opt.send) else None
  99. kal = get_keyaddrlist(opt)
  100. kl = get_keylist(opt)
  101. tx = MMGenBumpTX(filename=tx_file,send=(seed_files or kl or kal))
  102. do_license_msg()
  103. silent = opt.yes and opt.tx_fee != None and opt.output_to_reduce != None
  104. if not silent:
  105. msg(green('ORIGINAL TRANSACTION'))
  106. msg(tx.format_view(terse=True))
  107. tx.set_min_fee()
  108. tx.check_bumpable()
  109. msg('Creating new transaction')
  110. op_idx = tx.choose_output()
  111. if not silent:
  112. msg('Minimum fee for new transaction: {} {}'.format(tx.min_fee.hl(),g.coin))
  113. fee = tx.get_usr_fee_interactive(tx_fee=opt.tx_fee,desc='User-selected')
  114. tx.update_fee(op_idx,fee)
  115. d = tx.get_fee_from_tx()
  116. assert d == fee and d <= g.proto.max_tx_fee
  117. if g.proto.base_proto == 'Bitcoin':
  118. tx.outputs.sort_bip69() # output amts have changed, so re-sort
  119. if not opt.yes:
  120. tx.add_comment() # edits an existing comment
  121. tx.create_raw() # creates tx.hex, tx.txid
  122. tx.add_timestamp()
  123. tx.add_blockcount()
  124. qmsg('Fee successfully increased')
  125. if not silent:
  126. msg(green('\nREPLACEMENT TRANSACTION:'))
  127. msg_r(tx.format_view(terse=True))
  128. if seed_files or kl or kal:
  129. if txsign(tx,seed_files,kl,kal):
  130. tx.write_to_file(ask_write=False)
  131. tx.send(exit_on_fail=True)
  132. tx.write_to_file(ask_write=False)
  133. else:
  134. die(2,'Transaction could not be signed')
  135. else:
  136. tx.write_to_file(ask_write=not opt.yes,ask_write_default_yes=False,ask_overwrite=not opt.yes)