main_split.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #!/usr/bin/env python3
  2. #
  3. # MMGen Wallet, a terminal-based cryptocurrency wallet
  4. # Copyright (C)2013-2025 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 (global) 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, --no-rbf Make transaction non-replaceable (non-replace-by-fee
  43. according to BIP 125)
  44. -v, --verbose Produce more verbose output
  45. -y, --yes Answer 'yes' to prompts, suppress non-essential output
  46. -R, --rpc-host2= h Host the other coin daemon is running on (default: none)
  47. -l, --locktime= t Lock time (block height or unix seconds)
  48. (default: {bh})
  49. """,
  50. 'notes': f"""\n
  51. This command creates two transactions: one (with the timelock) to be broadcast
  52. on the long chain and one on the short chain after a replayable chain fork.
  53. Only {gc.proj_name} addresses may be spent to.
  54. The command must be run on the longest chain. The user is reponsible for
  55. ensuring that the current chain is the longest. The other chain is specified
  56. on the command line, or it defaults to the most recent replayable fork of the
  57. current chain.
  58. For the split to have a reasonable chance of succeeding, the long chain should
  59. be well ahead of the short one (by more than 20 blocks or so) and transactions
  60. should have a good chance of confirming quickly on both chains. For this
  61. larger than normal fees may be required. Fees may be specified on the command
  62. line, or network fee estimation may be used.
  63. If the split fails (i.e. the long-chain TX is broadcast and confirmed on the
  64. short chain), no funds are lost. A new split attempt can be made with the
  65. long-chain transaction's output as an input for the new split transaction.
  66. This process can be repeated as necessary until the split succeeds.
  67. IMPORTANT: Timelock replay protection offers NO PROTECTION against reorg
  68. attacks on the majority chain or reorg attacks on the minority chain if the
  69. minority chain is ahead of the timelock. If the reorg'd minority chain is
  70. behind the timelock, protection is contingent on getting the non-timelocked
  71. transaction reconfirmed before the timelock expires. Use at your own risk.
  72. """
  73. },
  74. 'code': {
  75. 'options': lambda proto, s: s.format(
  76. oc = proto.forks[-1][2].upper(),
  77. bh = 'current block height'),
  78. }
  79. }
  80. cfg = Config(opts_data=opts_data, need_amt=False)
  81. proto = cfg._proto
  82. die(1, 'This command is disabled')
  83. # the following code is broken:
  84. cfg.other_coin = cfg.other_coin.upper() if cfg.other_coin else proto.forks[-1][2].upper()
  85. if cfg.other_coin.lower() not in [e[2] for e in proto.forks if e[3] is True]:
  86. die(1, f'{cfg.other_coin!r}: not a replayable fork of {proto.coin} chain')
  87. if len(cfg._args) != 2:
  88. die(1, f'This command requires exactly two {gc.proj_name} addresses as arguments')
  89. from .addr import MMGenID
  90. try:
  91. mmids = [MMGenID(proto, a) for a in cfg._args]
  92. except:
  93. die(1, 'Command line arguments must be valid MMGen IDs')
  94. if mmids[0] == mmids[1]:
  95. die(2, f'Both transactions have the same output! ({mmids[0]})')
  96. from .tx import MMGenSplitTX
  97. from .protocol import init_proto
  98. if cfg.tx_fees:
  99. for idx, g_coin in ((1, cfg.other_coin), (0, proto.coin)):
  100. proto = init_proto(cfg, g_coin)
  101. cfg.fee = cfg.tx_fees.split(',')[idx]
  102. # opts.opt_is_tx_fee('foo', cfg.fee, 'transaction fee') # raises exception on error
  103. tx1 = MMGenSplitTX(proto)
  104. cfg.no_blank = True
  105. async def main():
  106. gmsg(f'Creating timelocked transaction for long chain ({proto.coin})')
  107. locktime = int(cfg.locktime)
  108. if not locktime:
  109. from .rpc import rpc_init
  110. rpc = rpc_init(proto)
  111. locktime = rpc.call('getblockcount')
  112. tx1.create(mmids[0], locktime)
  113. tx1.format()
  114. tx1.create_fn()
  115. gmsg(f'\nCreating transaction for short chain ({cfg.other_coin})')
  116. proto2 = init_proto(cfg, cfg.other_coin)
  117. tx2 = MMGenSplitTX(proto2)
  118. tx2.inputs = tx1.inputs
  119. tx2.inputs.convert_coin()
  120. tx2.create_split(mmids[1])
  121. for tx, desc in ((tx1, 'Long chain (timelocked)'), (tx2, 'Short chain')):
  122. tx.desc = desc + ' transaction'
  123. tx.file.write(ask_write=False, ask_overwrite=not cfg.yes, ask_write_default_yes=False)