main_split.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. # TODO: check that balances of output addrs are zero?
  19. """
  20. mmgen-split: Split funds after a replayable chain fork using a timelocked transaction
  21. UNMAINTAINED
  22. """
  23. from .cfg import Config,gc
  24. from .util import gmsg,die
  25. opts_data = {
  26. 'text': {
  27. 'desc': f"""
  28. Split funds in an {gc.proj_name} wallet after a chain fork using a
  29. timelocked transaction
  30. """,
  31. 'usage':'[opts] [output addr1] [output addr2]',
  32. 'options': """
  33. -h, --help Print this help message
  34. --, --longhelp Print help message for long options (common options)
  35. -f, --tx-fees= f The transaction fees for each chain (comma-separated)
  36. -c, --other-coin= c The coin symbol of the other chain (default: {oc})
  37. -B, --no-blank Don't blank screen before displaying unspent outputs
  38. -d, --outdir= d Specify an alternate directory 'd' for output
  39. -m, --minconf= n Minimum number of confirmations required to spend
  40. outputs (default: 1)
  41. -q, --quiet Suppress warnings; overwrite files without prompting
  42. -r, --rbf Make transaction BIP 125 replaceable (replace-by-fee)
  43. -v, --verbose Produce more verbose output
  44. -y, --yes Answer 'yes' to prompts, suppress non-essential output
  45. -R, --rpc-host2= h Host the other coin daemon is running on (default: none)
  46. -l, --locktime= t Lock time (block height or unix seconds)
  47. (default: {bh})
  48. """,
  49. 'notes': f"""\n
  50. This command creates two transactions: one (with the timelock) to be broadcast
  51. on the long chain and one on the short chain after a replayable chain fork.
  52. Only {gc.proj_name} addresses may be spent to.
  53. The command must be run on the longest chain. The user is reponsible for
  54. ensuring that the current chain is the longest. The other chain is specified
  55. on the command line, or it defaults to the most recent replayable fork of the
  56. current chain.
  57. For the split to have a reasonable chance of succeeding, the long chain should
  58. be well ahead of the short one (by more than 20 blocks or so) and transactions
  59. should have a good chance of confirming quickly on both chains. For this
  60. larger than normal fees may be required. Fees may be specified on the command
  61. line, or network fee estimation may be used.
  62. If the split fails (i.e. the long-chain TX is broadcast and confirmed on the
  63. short chain), no funds are lost. A new split attempt can be made with the
  64. long-chain transaction's output as an input for the new split transaction.
  65. This process can be repeated as necessary until the split succeeds.
  66. IMPORTANT: Timelock replay protection offers NO PROTECTION against reorg
  67. attacks on the majority chain or reorg attacks on the minority chain if the
  68. minority chain is ahead of the timelock. If the reorg'd minority chain is
  69. behind the timelock, protection is contingent on getting the non-timelocked
  70. transaction reconfirmed before the timelock expires. Use at your own risk.
  71. """
  72. },
  73. 'code': {
  74. 'options': lambda proto,s: s.format(
  75. oc=proto.forks[-1][2].upper(),
  76. bh='current block height'),
  77. }
  78. }
  79. cfg = Config( opts_data=opts_data, need_amt=False )
  80. proto = cfg._proto
  81. die(1,'This command is disabled')
  82. # the following code is broken:
  83. cfg.other_coin = cfg.other_coin.upper() if cfg.other_coin else proto.forks[-1][2].upper()
  84. if cfg.other_coin.lower() not in [e[2] for e in proto.forks if e[3] is True]:
  85. die(1,f'{cfg.other_coin!r}: not a replayable fork of {proto.coin} chain')
  86. if len(cfg._args) != 2:
  87. die(1,f'This command requires exactly two {gc.proj_name} addresses as arguments')
  88. from .addr import MMGenID
  89. try:
  90. mmids = [MMGenID(proto,a) for a in cfg._args]
  91. except:
  92. die(1,'Command line arguments must be valid MMGen IDs')
  93. if mmids[0] == mmids[1]:
  94. die(2,f'Both transactions have the same output! ({mmids[0]})')
  95. from .tx import MMGenSplitTX
  96. from .protocol import init_proto
  97. if cfg.tx_fees:
  98. for idx,g_coin in ((1,cfg.other_coin),(0,proto.coin)):
  99. proto = init_proto( cfg, g_coin )
  100. cfg.fee = cfg.tx_fees.split(',')[idx]
  101. # opts.opt_is_tx_fee('foo',cfg.fee,'transaction fee') # raises exception on error
  102. tx1 = MMGenSplitTX(proto)
  103. cfg.no_blank = True
  104. async def main():
  105. gmsg(f'Creating timelocked transaction for long chain ({proto.coin})')
  106. locktime = int(cfg.locktime)
  107. if not locktime:
  108. from .rpc import rpc_init
  109. rpc = rpc_init(proto)
  110. locktime = rpc.call('getblockcount')
  111. tx1.create(mmids[0],locktime)
  112. tx1.format()
  113. tx1.create_fn()
  114. gmsg(f'\nCreating transaction for short chain ({cfg.other_coin})')
  115. proto2 = init_proto( cfg, cfg.other_coin )
  116. tx2 = MMGenSplitTX(proto2)
  117. tx2.inputs = tx1.inputs
  118. tx2.inputs.convert_coin()
  119. tx2.create_split(mmids[1])
  120. for tx,desc in ((tx1,'Long chain (timelocked)'),(tx2,'Short chain')):
  121. tx.desc = desc + ' transaction'
  122. tx.file.write(ask_write=False,ask_overwrite=not cfg.yes,ask_write_default_yes=False)