ct_swap.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. # 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 mmgen.protocol import init_proto
  14. from ..include.common import gr_uc
  15. from .ct_regtest import (
  16. CmdTestRegtest,
  17. rt_data,
  18. dfl_wcls,
  19. rt_pw,
  20. cfg)
  21. sample1 = gr_uc[:24]
  22. sample2 = '00010203040506'
  23. class CmdTestSwap(CmdTestRegtest):
  24. bdb_wallet = True
  25. networks = ('btc',)
  26. tmpdir_nums = [37]
  27. passthru_opts = ('rpc_backend',)
  28. cmd_group_in = (
  29. ('setup', 'regtest (Bob and Alice) mode setup'),
  30. ('subgroup.init_bob', []),
  31. ('subgroup.fund_bob', ['init_bob']),
  32. ('subgroup.data', ['fund_bob']),
  33. ('subgroup.swap', ['fund_bob']),
  34. ('stop', 'stopping regtest daemon'),
  35. )
  36. cmd_subgroups = {
  37. 'init_bob': (
  38. 'creating Bob’s MMGen wallet and tracking wallet',
  39. ('walletgen_bob', 'wallet generation (Bob)'),
  40. ('addrgen_bob', 'address generation (Bob)'),
  41. ('addrimport_bob', 'importing Bob’s addresses'),
  42. ),
  43. 'fund_bob': (
  44. 'funding Bob’s wallet',
  45. ('fund_bob1', 'funding Bob’s wallet (bech32)'),
  46. ('fund_bob2', 'funding Bob’s wallet (native Segwit)'),
  47. ('bob_bal', 'displaying Bob’s balance'),
  48. ),
  49. 'data': (
  50. 'OP_RETURN data operations',
  51. ('data_tx1_create', 'Creating a transaction with OP_RETURN data (hex-encoded UTF-8)'),
  52. ('data_tx1_sign', 'Signing the transaction'),
  53. ('data_tx1_send', 'Sending the transaction'),
  54. ('data_tx1_chk', 'Checking the sent transaction'),
  55. ('generate3', 'Generate 3 blocks'),
  56. ('data_tx2_do', 'Creating and sending a transaction with OP_RETURN data (binary)'),
  57. ('data_tx2_chk', 'Checking the sent transaction'),
  58. ('generate3', 'Generate 3 blocks'),
  59. ('bob_listaddrs', 'Display Bob’s addresses'),
  60. ),
  61. 'swap': (
  62. 'Swap operations',
  63. ('bob_swaptxcreate1', 'Create a swap transaction'),
  64. ),
  65. }
  66. def __init__(self, trunner, cfgs, spawn):
  67. super().__init__(trunner, cfgs, spawn)
  68. globals_dict = globals()
  69. for k in rt_data:
  70. globals_dict[k] = rt_data[k]['btc']
  71. self.protos = [init_proto(cfg, k, network='regtest', need_amt=True) for k in ('btc', 'ltc', 'bch')]
  72. @property
  73. def sid(self):
  74. return self._user_sid('bob')
  75. def _addrgen_bob(self, proto_idx, mmtypes, subseed_idx=None):
  76. return self.addrgen('bob', subseed_idx=subseed_idx, mmtypes=mmtypes, proto=self.protos[proto_idx])
  77. def _addrimport_bob(self, proto_idx):
  78. return self.addrimport('bob', mmtypes=['S', 'B'], proto=self.protos[proto_idx])
  79. def _fund_bob(self, proto_idx, addrtype_code, amt):
  80. return self.fund_wallet('bob', addrtype_code, amt, proto=self.protos[proto_idx])
  81. def _bob_bal(self, proto_idx, bal, skip_check=False):
  82. return self.user_bal('bob', bal, proto=self.protos[proto_idx], skip_check=skip_check)
  83. def addrgen_bob(self):
  84. return self._addrgen_bob(0, ['S', 'B'])
  85. def addrimport_bob(self):
  86. return self._addrimport_bob(0)
  87. def fund_bob1(self):
  88. return self._fund_bob(0, 'B', '500')
  89. def fund_bob2(self):
  90. return self._fund_bob(0, 'S', '500')
  91. def bob_bal(self):
  92. return self._bob_bal(0, '1000')
  93. def data_tx1_create(self):
  94. return self._data_tx_create('1', 'B:2', 'B:3', 'data', sample1)
  95. def _data_tx_create(self, src, dest, chg, pfx, sample):
  96. t = self.spawn(
  97. 'mmgen-txcreate',
  98. ['-d', self.tmpdir, '-B', '--bob', f'{self.sid}:{dest},1', f'{self.sid}:{chg}', f'{pfx}:{sample}'])
  99. return self.txcreate_ui_common(t, menu=[], inputs='1', interactive_fee='3s')
  100. def data_tx1_sign(self):
  101. return self._data_tx_sign()
  102. def _data_tx_sign(self):
  103. fn = self.get_file_with_ext('rawtx')
  104. t = self.spawn('mmgen-txsign', ['-d', self.tmpdir, '--bob', fn])
  105. t.view_tx('v')
  106. t.passphrase(dfl_wcls.desc, rt_pw)
  107. t.do_comment(None)
  108. t.expect('(Y/n): ', 'y')
  109. t.written_to_file('Signed transaction')
  110. return t
  111. def data_tx1_send(self):
  112. return self._data_tx_send()
  113. def _data_tx_send(self):
  114. fn = self.get_file_with_ext('sigtx')
  115. t = self.spawn('mmgen-txsend', ['-q', '-d', self.tmpdir, '--bob', fn])
  116. t.expect('view: ', 'n')
  117. t.expect('(y/N): ', '\n')
  118. t.expect('to confirm: ', 'YES\n')
  119. t.written_to_file('Sent transaction')
  120. return t
  121. def data_tx1_chk(self):
  122. return self._data_tx_chk(sample1.encode().hex())
  123. def data_tx2_do(self):
  124. return self._data_tx_do('2', 'B:4', 'B:5', 'hexdata', sample2, 'v')
  125. def data_tx2_chk(self):
  126. return self._data_tx_chk(sample2)
  127. def _data_tx_do(self, src, dest, chg, pfx, sample, view):
  128. t = self.user_txdo(
  129. user = 'bob',
  130. fee = '30s',
  131. outputs_cl = [f'{self.sid}:{dest},1', f'{self.sid}:{chg}', f'{pfx}:{sample}'],
  132. outputs_list = src,
  133. add_comment = 'Transaction with OP_RETURN data',
  134. return_early = True)
  135. t.view_tx(view)
  136. if view == 'v':
  137. t.expect(sample)
  138. t.expect('amount:')
  139. t.passphrase(dfl_wcls.desc, rt_pw)
  140. t.written_to_file('Signed transaction')
  141. self._do_confirm_send(t)
  142. t.expect('Transaction sent')
  143. return t
  144. def _data_tx_chk(self, sample):
  145. mp = self._get_mempool(do_msg=True)
  146. assert len(mp) == 1
  147. self.write_to_tmpfile('data_tx1_id', mp[0]+'\n')
  148. tx_hex = self._do_cli(['getrawtransaction', mp[0]])
  149. tx = self._do_cli(['decoderawtransaction', tx_hex], decode_json=True)
  150. v0 = tx['vout'][0]
  151. assert v0['scriptPubKey']['hex'] == f'6a{(len(sample) // 2):02x}{sample}'
  152. assert v0['scriptPubKey']['type'] == 'nulldata'
  153. assert v0['value'] == "0.00000000"
  154. return 'ok'
  155. def generate3(self):
  156. return self.generate(3)
  157. def bob_listaddrs(self):
  158. t = self.spawn('mmgen-tool', ['--bob', 'listaddresses'])
  159. return t
  160. def bob_swaptxcreate1(self):
  161. t = self.spawn(
  162. 'mmgen-swaptxcreate',
  163. ['-d', self.tmpdir, '-B', '--bob', 'BTC', '1.234', f'{self.sid}:S:3', 'LTC'])
  164. return t