main_split.py 5.3 KB

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