ct_swap.py 5.3 KB

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