ct_swap.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #!/usr/bin/env python3
  2. #
  3. # MMGen Wallet, a terminal-based cryptocurrency wallet
  4. # Copyright (C)2013-2024 The MMGen Project <mmgen@tuta.io>
  5. # Licensed under the GNU General Public License, Version 3:
  6. # https://www.gnu.org/licenses
  7. # Public project repositories:
  8. # https://github.com/mmgen/mmgen-wallet
  9. # https://gitlab.com/mmgen/mmgen-wallet
  10. """
  11. test.cmdtest_d.ct_swap: asset swap tests for the cmdtest.py test suite
  12. """
  13. from .ct_regtest import CmdTestRegtest, rt_data, dfl_wcls, rt_pw
  14. sample1 = '=:ETH.ETH:0x86d526d6624AbC0178cF7296cD538Ecc080A95F1:0/1/0'
  15. sample2 = '00010203040506'
  16. class CmdTestSwap(CmdTestRegtest):
  17. bdb_wallet = True
  18. networks = ('btc',)
  19. tmpdir_nums = [37]
  20. cmd_group_in = (
  21. ('setup', 'regtest (Bob and Alice) mode setup'),
  22. ('subgroup.init_bob', []),
  23. ('subgroup.fund_bob', ['init_bob']),
  24. ('subgroup.data', ['fund_bob']),
  25. ('subgroup.swap', ['fund_bob']),
  26. ('stop', 'stopping regtest daemon'),
  27. )
  28. cmd_subgroups = {
  29. 'init_bob': (
  30. 'creating Bob’s MMGen wallet and tracking wallet',
  31. ('walletgen_bob', 'wallet generation (Bob)'),
  32. ('addrgen_bob', 'address generation (Bob)'),
  33. ('addrimport_bob', 'importing Bob’s addresses'),
  34. ),
  35. 'fund_bob': (
  36. 'funding Bob’s wallet',
  37. ('fund_bob1', 'funding Bob’s wallet (bech32)'),
  38. ('fund_bob2', 'funding Bob’s wallet (native Segwit)'),
  39. ('bob_bal', 'displaying Bob’s balance'),
  40. ),
  41. 'data': (
  42. 'OP_RETURN data operations',
  43. ('data_tx1_create', 'Creating a transaction with OP_RETURN data (hex-encoded ascii)'),
  44. ('data_tx1_sign', 'Signing the transaction'),
  45. ('data_tx1_send', 'Sending the transaction'),
  46. ('data_tx1_chk', 'Checking the sent transaction'),
  47. ('generate3', 'Generate 3 blocks'),
  48. ('data_tx2_do', 'Creating and sending a transaction with OP_RETURN data (binary)'),
  49. ('data_tx2_chk', 'Checking the sent transaction'),
  50. ('generate3', 'Generate 3 blocks'),
  51. ('bob_listaddrs', 'Display Bob’s addresses'),
  52. ),
  53. 'swap': (
  54. 'Swap operations',
  55. ('bob_swaptxcreate1', 'Create a swap transaction'),
  56. ),
  57. }
  58. def __init__(self, trunner, cfgs, spawn):
  59. super().__init__(trunner, cfgs, spawn)
  60. gldict = globals()
  61. for k in rt_data:
  62. gldict[k] = rt_data[k]['btc']
  63. @property
  64. def sid(self):
  65. return self._user_sid('bob')
  66. def addrgen_bob(self):
  67. return self.addrgen('bob', mmtypes=['S', 'B'])
  68. def addrimport_bob(self):
  69. return self.addrimport('bob', mmtypes=['S', 'B'])
  70. def fund_bob1(self):
  71. return self.fund_wallet('bob', 'B', '500')
  72. def fund_bob2(self):
  73. return self.fund_wallet('bob', 'S', '500')
  74. def bob_bal(self):
  75. return self.user_bal('bob', '1000')
  76. def data_tx1_create(self):
  77. return self._data_tx_create('1', 'B:2', 'B:3', 'data', sample1)
  78. def _data_tx_create(self, src, dest, chg, pfx, sample):
  79. t = self.spawn(
  80. 'mmgen-txcreate',
  81. ['-d', self.tmpdir, '-B', '--bob', f'{self.sid}:{dest},1', f'{self.sid}:{chg}', f'{pfx}:{sample}'])
  82. return self.txcreate_ui_common(t, menu=[], inputs='1', interactive_fee='3s')
  83. def data_tx1_sign(self):
  84. return self._data_tx_sign()
  85. def _data_tx_sign(self):
  86. fn = self.get_file_with_ext('rawtx')
  87. t = self.spawn('mmgen-txsign', ['-d', self.tmpdir, '--bob', fn])
  88. t.view_tx('v')
  89. t.passphrase(dfl_wcls.desc, rt_pw)
  90. t.do_comment(None)
  91. t.expect('(Y/n): ', 'y')
  92. t.written_to_file('Signed transaction')
  93. return t
  94. def data_tx1_send(self):
  95. return self._data_tx_send()
  96. def _data_tx_send(self):
  97. fn = self.get_file_with_ext('sigtx')
  98. t = self.spawn('mmgen-txsend', ['-q', '-d', self.tmpdir, '--bob', fn])
  99. t.expect('view: ', 'n')
  100. t.expect('(y/N): ', '\n')
  101. t.expect('to confirm: ', 'YES\n')
  102. t.written_to_file('Sent transaction')
  103. return t
  104. def data_tx1_chk(self):
  105. return self._data_tx_chk(sample1.encode().hex())
  106. def data_tx2_do(self):
  107. return self._data_tx_do('2', 'B:4', 'B:5', 'hexdata', sample2, 'v')
  108. def data_tx2_chk(self):
  109. return self._data_tx_chk(sample2)
  110. def _data_tx_do(self, src, dest, chg, pfx, sample, view):
  111. t = self.user_txdo(
  112. user = 'bob',
  113. fee = '30s',
  114. outputs_cl = [f'{self.sid}:{dest},1', f'{self.sid}:{chg}', f'{pfx}:{sample}'],
  115. outputs_list = src,
  116. add_comment = 'Transaction with OP_RETURN data',
  117. return_early = True)
  118. t.view_tx(view)
  119. if view == 'v':
  120. t.expect(sample)
  121. t.expect('amount:')
  122. t.passphrase(dfl_wcls.desc, rt_pw)
  123. t.written_to_file('Signed transaction')
  124. self._do_confirm_send(t)
  125. t.expect('Transaction sent')
  126. return t
  127. def _data_tx_chk(self, sample):
  128. mp = self._get_mempool(do_msg=True)
  129. assert len(mp) == 1
  130. self.write_to_tmpfile('data_tx1_id', mp[0]+'\n')
  131. tx_hex = self._do_cli(['getrawtransaction', mp[0]])
  132. tx = self._do_cli(['decoderawtransaction', tx_hex], decode_json=True)
  133. v0 = tx['vout'][0]
  134. assert v0['scriptPubKey']['hex'] == f'6a{(len(sample) // 2):02x}{sample}'
  135. assert v0['scriptPubKey']['type'] == 'nulldata'
  136. assert v0['value'] == "0.00000000"
  137. return 'ok'
  138. def generate3(self):
  139. return self.generate(3)
  140. def bob_listaddrs(self):
  141. t = self.spawn('mmgen-tool', ['--bob', 'listaddresses'])
  142. return t
  143. def bob_swaptxcreate1(self):
  144. t = self.spawn(
  145. 'mmgen-swaptxcreate',
  146. ['-d', self.tmpdir, '-B', '--bob', 'BTC', '1.234', f'{self.sid}:S:3', 'LTC'])
  147. return t