automount.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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.automount: autosigning with automount tests for the cmdtest.py test suite
  12. """
  13. import time
  14. from .autosign import CmdTestAutosignThreaded
  15. from .regtest import CmdTestRegtest, rt_pw
  16. from ..include.common import gr_uc, create_addrpairs
  17. class CmdTestAutosignAutomount(CmdTestAutosignThreaded, CmdTestRegtest):
  18. 'automounted transacting operations via regtest mode'
  19. networks = ('btc', 'bch', 'ltc')
  20. tmpdir_nums = [49]
  21. bdb_wallet = True
  22. keylist_passwd = 'abc'
  23. rt_data = {
  24. 'rtFundAmt': {'btc':'500', 'bch':'500', 'ltc':'5500'},
  25. }
  26. bal1_chk = {
  27. 'btc': '502.46',
  28. 'bch': '502.46',
  29. 'ltc': '5502.46'}
  30. bal2_chk = {
  31. 'btc': '493.56992828',
  32. 'bch': '501.22524576',
  33. 'ltc': '5493.56992828'}
  34. cmd_group = (
  35. ('setup', 'regtest mode setup'),
  36. ('walletgen_alice', 'wallet generation (Alice)'),
  37. ('addrgen_alice', 'address generation (Alice)'),
  38. ('addrimport_alice', 'importing Alice’s addresses'),
  39. ('addrimport_alice_non_mmgen', 'importing Alice’s non-MMGen addresses'),
  40. ('fund_alice', 'funding Alice’s wallet'),
  41. ('fund_alice_non_mmgen1', 'funding Alice’s wallet (non-MMGen addr #1)'),
  42. ('fund_alice_non_mmgen2', 'funding Alice’s wallet (non-MMGen addr #2)'),
  43. ('generate', 'mining a block'),
  44. ('alice_bal1', 'checking Alice’s balance'),
  45. ('alice_txcreate1', 'creating a transaction'),
  46. ('alice_txcreate_bad_have_unsigned', 'creating the transaction again (error)'),
  47. ('alice_run_autosign_setup', 'running ‘autosign setup’ (with default wallet)'),
  48. ('wait_loop_start', 'starting autosign wait loop'),
  49. ('alice_txstatus1', 'getting transaction status (unsigned)'),
  50. ('alice_txstatus2', 'getting transaction status (unsent)'),
  51. ('alice_txcreate_bad_have_unsent', 'creating the transaction again (error)'),
  52. ('alice_txsend1', 'sending a transaction, editing comment'),
  53. ('alice_txstatus3', 'getting transaction status (in mempool)'),
  54. ('alice_txsend_bad_no_unsent', 'sending the transaction again (error)'),
  55. ('generate', 'mining a block'),
  56. ('alice_txstatus4', 'getting transaction status (one confirmation)'),
  57. ('alice_txcreate2', 'creating a transaction'),
  58. ('alice_txsend_abort1', 'aborting the transaction (raw only)'),
  59. ('alice_txsend_abort2', 'aborting the transaction again (error)'),
  60. ('alice_txcreate3', 'creating a transaction'),
  61. ('alice_txsend_abort3', 'aborting the transaction (user exit)'),
  62. ('alice_txsend_abort4', 'aborting the transaction (raw + signed)'),
  63. ('alice_txsend_abort5', 'aborting the transaction again (error)'),
  64. ('generate', 'mining a block'),
  65. ('alice_txcreate4', 'creating a transaction'),
  66. ('alice_txbump1', 'bumping the unsigned transaction (error)'),
  67. ('alice_txbump2', 'bumping the unsent transaction (error)'),
  68. ('alice_txsend2_dump_hex', 'dumping the transaction to hex'),
  69. ('alice_txsend2_cli', 'sending the transaction via cli'),
  70. ('alice_txsend2_mark_sent', 'marking the transaction sent'),
  71. ('alice_txbump3', 'bumping the transaction'),
  72. ('alice_txsend3', 'sending the bumped transaction'),
  73. ('alice_txbump4', 'bumping the transaction (new outputs, fee too low)'),
  74. ('alice_txbump_abort1', 'aborting the transaction'),
  75. ('alice_txbump5', 'bumping the transaction (new outputs)'),
  76. ('alice_txsend5', 'sending the bumped transaction'),
  77. ('alice_txstatus5', 'getting transaction status (in mempool)'),
  78. ('generate', 'mining a block'),
  79. ('alice_bal2', 'checking Alice’s balance'),
  80. ('wait_loop_kill', 'stopping autosign wait loop'),
  81. ('stop', 'stopping regtest daemon'),
  82. ('txview', 'viewing transactions'),
  83. )
  84. def __init__(self, cfg, trunner, cfgs, spawn):
  85. self.coins = [cfg.coin.lower()]
  86. CmdTestAutosignThreaded.__init__(self, cfg, trunner, cfgs, spawn)
  87. CmdTestRegtest.__init__(self, cfg, trunner, cfgs, spawn)
  88. if trunner is None:
  89. return
  90. self.opts.append('--alice')
  91. self.non_mmgen_addrs = create_addrpairs(self.proto, 'C', 2)
  92. def addrimport_alice_non_mmgen(self):
  93. self.write_to_tmpfile(
  94. 'non_mmgen_addrs',
  95. '\n'.join(e.addr for e in self.non_mmgen_addrs))
  96. return self.spawn(
  97. 'mmgen-addrimport',
  98. ['--alice', '--quiet', '--addrlist', f'{self.tmpdir}/non_mmgen_addrs'])
  99. def fund_alice_non_mmgen1(self):
  100. return self.fund_wallet('alice', '1.23', addr=self.non_mmgen_addrs[0].addr)
  101. def fund_alice_non_mmgen2(self):
  102. return self.fund_wallet('alice', '1.23', addr=self.non_mmgen_addrs[1].addr)
  103. def alice_bal1(self):
  104. return self._user_bal_cli('alice', chk=self.bal1_chk[self.coin])
  105. def alice_txcreate1(self):
  106. return self._user_txcreate(
  107. 'alice',
  108. inputs = '1-3',
  109. tweaks = ['confirm_non_mmgen'],
  110. chg_addr = 'C:5',
  111. data_arg = 'data:'+gr_uc[:24])
  112. def alice_txcreate2(self):
  113. return self._user_txcreate('alice', chg_addr='L:5')
  114. alice_txcreate3 = alice_txcreate2
  115. def alice_txcreate4(self):
  116. return self._user_txcreate('alice', chg_addr='L:4', need_rbf=True)
  117. def _alice_txsend_abort(self, err=False, send_resp='y', expect=None, shred_expect=[]):
  118. self.insert_device_online()
  119. t = self.spawn(
  120. 'mmgen-txsend',
  121. ['--quiet', '--abort'],
  122. no_passthru_opts = ['coin'],
  123. exit_val = 2 if err else 1 if send_resp == 'n' else None)
  124. if err:
  125. t.expect(expect)
  126. else:
  127. t.expect('(y/N): ', send_resp)
  128. if expect:
  129. t.expect(expect)
  130. for pat in shred_expect:
  131. t.expect(pat, regex=True)
  132. t.read()
  133. self.remove_device_online()
  134. return t
  135. def alice_txsend_abort1(self):
  136. return self._alice_txsend_abort(shred_expect=['Shredding .*arawtx'])
  137. def alice_txsend_abort2(self):
  138. return self._alice_txsend_abort(err=True, expect='No unsent transactions')
  139. def alice_txsend_abort3(self):
  140. return self._alice_txsend_abort(send_resp='n', expect='Exiting at user request')
  141. def alice_txsend_abort4(self):
  142. self._wait_signed('transaction')
  143. return self._alice_txsend_abort(shred_expect=[r'Shredding .*arawtx', r'Shredding .*asigtx'])
  144. alice_txsend_abort5 = alice_txsend_abort2
  145. def alice_txcreate_bad_have_unsigned(self):
  146. return self._user_txcreate('alice', chg_addr='C:5', exit_val=2, expect_str='already present')
  147. def alice_txcreate_bad_have_unsent(self):
  148. return self._user_txcreate('alice', chg_addr='C:5', exit_val=2, expect_str='unsent transaction')
  149. def alice_run_autosign_setup(self):
  150. from mmgen.crypto import Crypto
  151. from mmgen.cfg import Config
  152. new_cfg = Config({'_clone': self.cfg, 'usr_randchars': 0, 'hash_preset': '1'})
  153. enc_data = Crypto(new_cfg).mmgen_encrypt(
  154. '\n'.join(e.wif for e in self.non_mmgen_addrs).encode(), passwd=self.keylist_passwd)
  155. self.write_to_tmpfile('non_mmgen_keys.mmenc', enc_data, binary=True)
  156. return self.run_setup(
  157. mn_type = 'default',
  158. use_dfl_wallet = True,
  159. wallet_passwd = rt_pw,
  160. add_opts = [f'--keys-from-file={self.tmpdir}/non_mmgen_keys.mmenc'],
  161. keylist_passwd = self.keylist_passwd)
  162. def alice_txsend1(self):
  163. return self._user_txsend('alice', comment='This one’s worth a comment', no_wait=True)
  164. def alice_txsend2_dump_hex(self):
  165. return self._user_txsend('alice', need_rbf=True, dump_hex=True)
  166. def alice_txsend2_cli(self):
  167. if not self.proto.cap('rbf'):
  168. return 'skip'
  169. return self._user_dump_hex_send_cli('alice')
  170. def alice_txsend2_mark_sent(self):
  171. return self._user_txsend('alice', need_rbf=True, mark_sent=True)
  172. def alice_txsend3(self):
  173. return self._user_txsend('alice', need_rbf=True)
  174. def alice_txsend5(self):
  175. return self._user_txsend('alice', need_rbf=True)
  176. def _alice_txstatus(self, expect, exit_val=None, need_rbf=False):
  177. if need_rbf and not self.proto.cap('rbf'):
  178. return 'skip'
  179. self.insert_device_online()
  180. t = self.spawn(
  181. 'mmgen-txsend',
  182. ['--alice', '--autosign', '--status', '--verbose'],
  183. no_passthru_opts = ['coin'],
  184. exit_val = exit_val)
  185. t.expect(expect)
  186. if not exit_val:
  187. t.expect('view: ', 'n')
  188. t.read()
  189. self.remove_device_online()
  190. return t
  191. def alice_txstatus1(self):
  192. return self._alice_txstatus('unsigned', 1)
  193. def alice_txstatus2(self):
  194. self._wait_signed('transaction')
  195. return self._alice_txstatus('unsent', 1)
  196. def alice_txstatus3(self):
  197. return self._alice_txstatus('in mempool', 0)
  198. def alice_txstatus4(self):
  199. return self._alice_txstatus('1 confirmation', 0)
  200. def alice_txstatus5(self):
  201. return self._alice_txstatus('in mempool', need_rbf=True)
  202. def alice_txsend_bad_no_unsent(self):
  203. self.insert_device_online()
  204. t = self.spawn('mmgen-txsend', ['--quiet', '--autosign'], exit_val=2, no_passthru_opts=['coin'])
  205. t.expect('No unsent transactions')
  206. t.read()
  207. self.remove_device_online()
  208. return t
  209. def _alice_txbump(self, fee_opt=None, output_args=[], bad_tx_expect=None, low_fee_fix=None):
  210. if not self.proto.cap('rbf'):
  211. return 'skip'
  212. self.insert_device_online()
  213. t = self.spawn(
  214. 'mmgen-txbump',
  215. ['--alice', '--autosign']
  216. + ([fee_opt] if fee_opt else [])
  217. + output_args,
  218. exit_val = 1 if bad_tx_expect else None)
  219. if bad_tx_expect:
  220. time.sleep(0.5)
  221. t.expect('Only sent transactions')
  222. t.expect(bad_tx_expect)
  223. else:
  224. if not output_args:
  225. t.expect(r'to deduct the fee from .* change output\): ', '\n', regex=True)
  226. t.expect(r'(Y/n): ', 'y') # output OK?
  227. if low_fee_fix or not fee_opt:
  228. if low_fee_fix:
  229. t.expect('Please choose a higher fee')
  230. t.expect('transaction fee: ', (low_fee_fix or '200s') + '\n')
  231. if output_args:
  232. t.expect(r'(Y/n): ', 'y')
  233. t.expect(r'(Y/n): ', 'y') # fee OK?
  234. t.expect(r'(y/N): ', '\n') # add comment?
  235. t.expect(r'(y/N): ', 'y') # save?
  236. t.read()
  237. self.remove_device_online()
  238. return t
  239. def alice_txbump1(self):
  240. return self._alice_txbump(bad_tx_expect='unsigned transaction')
  241. def alice_txbump2(self):
  242. self._wait_signed('transaction')
  243. return self._alice_txbump(bad_tx_expect='unsent transaction')
  244. def alice_txbump3(self):
  245. return self._alice_txbump()
  246. def alice_txbump4(self):
  247. sid = self._user_sid('alice')
  248. return self._alice_txbump(
  249. fee_opt = '--fee=3s',
  250. output_args = [f'{self.burn_addr},7.654321', f'{sid}:C:1'],
  251. low_fee_fix = '300s')
  252. def alice_txbump_abort1(self):
  253. if not self.proto.cap('rbf'):
  254. return 'skip'
  255. return self._alice_txsend_abort(shred_expect=['Shredding .*arawtx'])
  256. def alice_txbump5(self):
  257. sid = self._user_sid('alice')
  258. return self._alice_txbump(
  259. fee_opt = '--fee=400s',
  260. output_args = ['data:message for posterity', f'{self.burn_addr},7.654321', f'{sid}:C:1'])
  261. def alice_bal2(self):
  262. return self.user_bal('alice', self.bal2_chk[self.coin])