main_txbump.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #!/usr/bin/env python
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2018 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. opts_data = lambda: {
  24. 'desc': 'Increase the fee on a replaceable (RBF) {g.proj_name} transaction, creating a new transaction, and optionally sign and send the new transaction'.format(g=g),
  25. 'usage': '[opts] <{g.proj_name} TX file> [seed source] ...'.format(g=g),
  26. 'sets': ( ('yes', True, 'quiet', True), ),
  27. 'options': """
  28. -h, --help Print this help message
  29. --, --longhelp Print help message for long options (common options)
  30. -b, --brain-params=l,p Use seed length 'l' and hash preset 'p' for
  31. brainwallet input
  32. -c, --comment-file= f Source the transaction's comment from file 'f'
  33. -d, --outdir= d Specify an alternate directory 'd' for output
  34. -e, --echo-passphrase Print passphrase to screen when typing it
  35. -f, --tx-fee= f Transaction fee, as a decimal {cu} amount or in
  36. satoshis per byte (an integer followed by 's')
  37. -H, --hidden-incog-input-params=f,o Read hidden incognito data from file
  38. 'f' at offset 'o' (comma-separated)
  39. -i, --in-fmt= f Input is from wallet format 'f' (see FMT CODES below)
  40. -l, --seed-len= l Specify wallet seed length of 'l' bits. This option
  41. is required only for brainwallet and incognito inputs
  42. with non-standard (< {g.seed_len}-bit) seed lengths.
  43. -k, --keys-from-file=f Provide additional keys for non-{pnm} addresses
  44. -K, --key-generator= m Use method 'm' for public key generation
  45. Options: {kgs}
  46. (default: {kg})
  47. -M, --mmgen-keys-from-file=f Provide keys for {pnm} addresses in a key-
  48. address file (output of '{pnl}-keygen'). Permits
  49. online signing without an {pnm} seed source. The
  50. key-address file is also used to verify {pnm}-to-{cu}
  51. mappings, so the user should record its checksum.
  52. -o, --output-to-reduce=o Deduct the fee from output 'o' (an integer, or 'c'
  53. for the transaction's change output, if present)
  54. -O, --old-incog-fmt Specify old-format incognito input
  55. -p, --hash-preset= p Use the scrypt hash parameters defined by preset 'p'
  56. for password hashing (default: '{g.hash_preset}')
  57. -P, --passwd-file= f Get {pnm} wallet or {dn} passphrase from file 'f'
  58. -q, --quiet Suppress warnings; overwrite files without prompting
  59. -s, --send Sign and send the transaction (the default if seed
  60. data is provided)
  61. -v, --verbose Produce more verbose output
  62. -y, --yes Answer 'yes' to prompts, suppress non-essential output
  63. -z, --show-hash-presets Show information on available hash presets
  64. """.format(g=g,pnm=g.proj_name,pnl=g.proj_name.lower(),dn=g.proto.daemon_name,
  65. kgs=' '.join(['{}:{}'.format(n,k) for n,k in enumerate(g.key_generators,1)]),
  66. kg=g.key_generator,
  67. cu=g.coin
  68. ),
  69. 'notes': '\n' + help_notes('fee') + help_notes('txsign')
  70. }
  71. cmd_args = opts.init(opts_data)
  72. rpc_init()
  73. tx_file = cmd_args.pop(0)
  74. check_infile(tx_file)
  75. from mmgen.tx import *
  76. from mmgen.txsign import *
  77. seed_files = get_seed_files(opt,cmd_args) if (cmd_args or opt.send) else None
  78. kal = get_keyaddrlist(opt)
  79. kl = get_keylist(opt)
  80. tx = MMGenBumpTX(filename=tx_file,send=(seed_files or kl or kal))
  81. do_license_msg()
  82. silent = opt.yes and opt.tx_fee != None and opt.output_to_reduce != None
  83. if not silent:
  84. msg(green('ORIGINAL TRANSACTION'))
  85. msg(tx.format_view(terse=True))
  86. tx.set_min_fee()
  87. if not [o.amt for o in tx.outputs if o.amt >= tx.min_fee]:
  88. die(1,'Transaction cannot be bumped.' +
  89. '\nAll outputs have less than the minimum fee ({} {})'.format(tx.min_fee,g.coin))
  90. msg('Creating new transaction')
  91. op_idx = tx.choose_output()
  92. if not silent:
  93. msg('Minimum fee for new transaction: {} {}'.format(tx.min_fee,g.coin))
  94. fee = tx.get_usr_fee_interactive(tx_fee=opt.tx_fee,desc='User-selected')
  95. tx.update_output_amt(op_idx,tx.sum_inputs()-tx.sum_outputs(exclude=op_idx)-fee)
  96. d = tx.get_fee()
  97. assert d == fee and d <= g.proto.max_tx_fee
  98. if not opt.yes:
  99. tx.add_comment() # edits an existing comment
  100. tx.create_raw() # creates tx.hex, tx.txid
  101. tx.add_timestamp()
  102. tx.add_blockcount()
  103. qmsg('Fee successfully increased')
  104. if not silent:
  105. msg(green('\nREPLACEMENT TRANSACTION:'))
  106. msg_r(tx.format_view(terse=True))
  107. if seed_files or kl or kal:
  108. txsign(tx,seed_files,kl,kal)
  109. tx.write_to_file(ask_write=False)
  110. tx.send(exit_on_fail=True)
  111. tx.write_to_file(ask_write=False)
  112. else:
  113. tx.write_to_file(ask_write=not opt.yes,ask_write_default_yes=False,ask_overwrite=not opt.yes)