ct_swap.py 5.9 KB

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