ct_swap.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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', ['init_bob']),
  25. ('stop', 'stopping regtest daemon'),
  26. )
  27. cmd_subgroups = {
  28. 'init_bob': (
  29. 'creating Bob’s MMGen wallet and tracking wallet',
  30. ('walletgen_bob', 'wallet generation (Bob)'),
  31. ('addrgen_bob', 'address generation (Bob)'),
  32. ('addrimport_bob', 'importing Bob’s addresses'),
  33. ),
  34. 'fund_bob': (
  35. 'funding Bob’s wallet',
  36. ('fund_bob', 'funding Bob’s wallet'),
  37. ('bob_bal1', 'Bob’s balance'),
  38. ),
  39. 'data': (
  40. 'OP_RETURN data operations',
  41. ('data_tx1_create', 'Creating a transaction with OP_RETURN data (hex-encoded ascii)'),
  42. ('data_tx1_sign', 'Signing the transaction'),
  43. ('data_tx1_send', 'Sending the transaction'),
  44. ('data_tx1_chk', 'Checking the sent transaction'),
  45. ('generate3', 'Generate 3 blocks'),
  46. ('data_tx2_do', 'Creating and sending a transaction with OP_RETURN data (binary)'),
  47. ('data_tx2_chk', 'Checking the sent transaction'),
  48. ('generate3', 'Generate 3 blocks'),
  49. ),
  50. }
  51. def __init__(self, trunner, cfgs, spawn):
  52. super().__init__(trunner, cfgs, spawn)
  53. gldict = globals()
  54. for k in rt_data:
  55. gldict[k] = rt_data[k]['btc']
  56. @property
  57. def sid(self):
  58. return self._user_sid('bob')
  59. def addrgen_bob(self):
  60. return self.addrgen('bob', mmtypes=['S', 'B'])
  61. def addrimport_bob(self):
  62. return self.addrimport('bob', mmtypes=['S', 'B'])
  63. def fund_bob(self):
  64. return self.fund_wallet('bob', 'B', '500')
  65. def data_tx1_create(self):
  66. return self._data_tx_create('1', 'B:2', 'B:3', 'data', sample1)
  67. def _data_tx_create(self, src, dest, chg, pfx, sample):
  68. t = self.spawn(
  69. 'mmgen-txcreate',
  70. ['-d', self.tmpdir, '-B', '--bob', f'{self.sid}:{dest},1', f'{self.sid}:{chg}', f'{pfx}:{sample}'])
  71. return self.txcreate_ui_common(t, menu=[], inputs='1', interactive_fee='3s')
  72. def data_tx1_sign(self):
  73. return self._data_tx_sign()
  74. def _data_tx_sign(self):
  75. fn = self.get_file_with_ext('rawtx')
  76. t = self.spawn('mmgen-txsign', ['-d', self.tmpdir, '--bob', fn])
  77. t.view_tx('v')
  78. t.passphrase(dfl_wcls.desc, rt_pw)
  79. t.do_comment(None)
  80. t.expect('(Y/n): ', 'y')
  81. t.written_to_file('Signed transaction')
  82. return t
  83. def data_tx1_send(self):
  84. return self._data_tx_send()
  85. def _data_tx_send(self):
  86. fn = self.get_file_with_ext('sigtx')
  87. t = self.spawn('mmgen-txsend', ['-q', '-d', self.tmpdir, '--bob', fn])
  88. t.expect('view: ', 'n')
  89. t.expect('(y/N): ', '\n')
  90. t.expect('to confirm: ', 'YES\n')
  91. t.written_to_file('Sent transaction')
  92. return t
  93. def data_tx1_chk(self):
  94. return self._data_tx_chk(sample1.encode().hex())
  95. def data_tx2_do(self):
  96. return self._data_tx_do('2', 'B:4', 'B:5', 'hexdata', sample2, 'v')
  97. def data_tx2_chk(self):
  98. return self._data_tx_chk(sample2)
  99. def _data_tx_do(self, src, dest, chg, pfx, sample, view):
  100. t = self.user_txdo(
  101. user = 'bob',
  102. fee = '30s',
  103. outputs_cl = [f'{self.sid}:{dest},1', f'{self.sid}:{chg}', f'{pfx}:{sample}'],
  104. outputs_list = src,
  105. add_comment = 'Transaction with OP_RETURN data',
  106. return_early = True)
  107. t.view_tx(view)
  108. if view == 'v':
  109. t.expect(sample)
  110. t.expect('amount:')
  111. t.passphrase(dfl_wcls.desc, rt_pw)
  112. t.written_to_file('Signed transaction')
  113. self._do_confirm_send(t)
  114. t.expect('Transaction sent')
  115. return t
  116. def _data_tx_chk(self, sample):
  117. mp = self._get_mempool(do_msg=True)
  118. assert len(mp) == 1
  119. self.write_to_tmpfile('data_tx1_id', mp[0]+'\n')
  120. tx_hex = self._do_cli(['getrawtransaction', mp[0]])
  121. tx = self._do_cli(['decoderawtransaction', tx_hex], decode_json=True)
  122. v0 = tx['vout'][0]
  123. assert v0['scriptPubKey']['hex'] == f'6a{(len(sample) // 2):02x}{sample}'
  124. assert v0['scriptPubKey']['type'] == 'nulldata'
  125. assert v0['value'] == "0.00000000"
  126. return 'ok'
  127. def generate3(self):
  128. return self.generate(3)