rpc.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. class responses:
  21. def get_balance(m, length):
  22. return [
  23. {'denom': 'foocoin', 'amount': 321321321321},
  24. {'denom': 'rune', 'amount': 987654321321},
  25. {'denom': 'barcoin', 'amount': 123123123123}]
  26. def get_account_info(m, length):
  27. return {
  28. 'value': {
  29. 'address': m[1],
  30. 'pub_key': 'PubKeySecp256k1{0000}',
  31. 'account_number': '1234',
  32. 'sequence': '333444'}}
  33. def get_tx_info(m, length):
  34. txid = environ['wsgi.input'].read(length).decode().removeprefix('hash=0x').upper()
  35. return {
  36. 'hash': txid,
  37. 'height': '21298600',
  38. 'index': 2,
  39. 'tx_result': {
  40. 'gas_used': '173222',
  41. 'events': [],
  42. 'codespace': ''
  43. },
  44. 'tx': 'MHgwMGZvb2Jhcg=='}
  45. def check_tx(m, length):
  46. return {
  47. 'code': 0,
  48. 'data': '',
  49. 'log': '',
  50. 'info': '',
  51. 'gas_wanted': '-1',
  52. 'gas_used': '53774',
  53. 'events': [],
  54. 'codespace': ''}
  55. def broadcast_tx_sync(m, length):
  56. txhex = environ['wsgi.input'].read(length).decode().removeprefix('tx=0x').upper()
  57. res = {'code': 0, 'codespace': '', 'data': '', 'log': ''}
  58. if txhex.startswith('0A540A52'):
  59. res.update({'hash': '14463C716CF08A814868DB779156BCD85A1DF8EE49E924900A74482E9DEE132D'})
  60. elif txhex.startswith('0AC1010A'):
  61. res.update({'hash': '17F9411E48542C0DCA4D40A0DD4A1795DE6D5791A873A27CBBDC1031FE8D1BC5'})
  62. return res
  63. pat_info = (
  64. ('get_balance', 'GET', r'/bank/balances/(\S+)'),
  65. ('get_account_info', 'GET', r'/auth/accounts/(\S+)'),
  66. ('get_tx_info', 'POST', r'/tx$'),
  67. ('check_tx', 'POST', r'/check_tx$'),
  68. ('broadcast_tx_sync', 'POST', r'/broadcast_tx_sync$'))
  69. req_str = request_uri(environ)
  70. for name, method_chk, pat in pat_info:
  71. if m := re.search(pat, req_str):
  72. assert method == method_chk
  73. length = int(environ.get('CONTENT_LENGTH', '0')) if method == 'POST' else None
  74. res = getattr(responses, name)(m, length)
  75. return json.dumps({'result': res}).encode()
  76. raise ValueError(f'‘{req_str}’: malformed query path')