rpc.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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.httpd.thornode.rpc: Thornode RPC HTTP server
  12. """
  13. import re, json
  14. from wsgiref.util import request_uri
  15. from . import ThornodeServer
  16. class ThornodeRPCServer(ThornodeServer):
  17. port = 18800
  18. name = 'thornode RPC server'
  19. def make_response_body(self, method, environ):
  20. req_str = request_uri(environ)
  21. if re.search(r'/bank/balances/(\S+)', req_str):
  22. res = [
  23. {'denom': 'foocoin', 'amount': 321321321321},
  24. {'denom': 'rune', 'amount': 987654321321},
  25. {'denom': 'barcoin', 'amount': 123123123123}]
  26. elif m := re.search(r'/auth/accounts/(\S+)', req_str):
  27. res = {
  28. 'value': {
  29. 'address': m[1],
  30. 'pub_key': 'PubKeySecp256k1{0000}',
  31. 'account_number': '1234',
  32. 'sequence': '333444'}}
  33. elif m := re.search(r'/tx$', req_str):
  34. assert method == 'POST'
  35. txid = environ['wsgi.input'].read(71).decode().removeprefix('hash=0x').upper()
  36. res = {
  37. 'hash': txid,
  38. 'height': '21298600',
  39. 'index': 2,
  40. 'tx_result': {
  41. 'gas_used': '173222',
  42. 'events': [],
  43. 'codespace': ''
  44. },
  45. 'tx': 'MHgwMGZvb2Jhcg=='}
  46. elif m := re.search(r'/check_tx$', req_str):
  47. assert method == 'POST'
  48. res = {
  49. 'code': 0,
  50. 'data': '',
  51. 'log': '',
  52. 'info': '',
  53. 'gas_wanted': '-1',
  54. 'gas_used': '53774',
  55. 'events': [],
  56. 'codespace': ''}
  57. elif m := re.search(r'/broadcast_tx_sync$', req_str):
  58. assert method == 'POST'
  59. txhex = environ['wsgi.input'].read(24).decode().removeprefix('tx=0x').upper()
  60. res = {'code': 0, 'codespace': '', 'data': '', 'log': ''}
  61. if txhex.startswith('0A540A52'):
  62. res.update({'hash': '14463C716CF08A814868DB779156BCD85A1DF8EE49E924900A74482E9DEE132D'})
  63. else:
  64. raise ValueError(f'‘{req_str}’: malformed query path')
  65. return json.dumps({'result': res}).encode()