rune.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #!/usr/bin/env python3
  2. import os
  3. from collections import namedtuple
  4. from decimal import Decimal
  5. from mmgen.cfg import Config
  6. from mmgen.util import pp_fmt, ymsg
  7. from mmgen.proto.btc.common import hash160
  8. from mmgen.proto.cosmos.tx.protobuf import RawTx
  9. from mmgen.proto.rune.tx.protobuf import (
  10. RuneTx,
  11. build_tx,
  12. build_swap_tx,
  13. tx_info,
  14. send_tx_parms,
  15. deposit_tx_parms,
  16. swap_tx_parms)
  17. from ..include.common import vmsg, silence, end_silence
  18. test_cfg = Config({'coin': 'rune', 'test_suite': True})
  19. _pv = namedtuple('parse_vector', ['fn', 'txid', 'parms', 'null_fee'], defaults=[None])
  20. parse_vectors = [
  21. _pv(
  22. 'mainnet-tx-msgsend1.binpb',
  23. '36f91982c1911fe1aa66b44eed60e29175e5b8ae3301feef9158b7617779b00e',
  24. send_tx_parms(
  25. 'thor1t60f02r8jvzjrhtnjgfj4ne6rs5wjnejwmj7fh',
  26. 'thor166n4w5039meulfa3p6ydg60ve6ueac7tlt0jws',
  27. '12613.15290000',
  28. 8000000,
  29. 45060,
  30. 302033,
  31. pubkey = '02f9cbb8409443ccf043f26d8f91c2550d2578ecc49bb3ad89d4e21a7882bf1e23',
  32. signature = 'd44b2e0c7546c5fae24a2c829757f49cce1bb29553f7e1a2f87c1ac2f1c46e22' # r
  33. '509d765fc605d85e8967639864622ebc7c39a1a93fc20cf0fe5d703c4aa3636d')), # s
  34. _pv(
  35. 'mainnet-tx-msgdeposit1.binpb',
  36. '1089bbd54746bbc6a40e264d3ce8085561978739094c9c5aac59c569b4c28ba9',
  37. deposit_tx_parms(
  38. 'THOR', 'RUNE', 'RUNE',
  39. 'thor1lukwlve7hayy66qrdkp4k7sh0emjqwergy7tl3',
  40. '299.23861844',
  41. 600000000,
  42. 125632,
  43. 348388,
  44. decimals = 8,
  45. memo = '=:LTC~LTC:thor1lukwlve7hayy66qrdkp4k7sh0emjqwergy7tl3:605926421/0/1',
  46. pubkey = '03da157f891abfe7822efb91f59667aa6cc6c3768a7e280caeb9ae243c969eb3e7',
  47. signature = '869399bcc2ccb9c9c286bdf214439ad132221cb8206547ceb012e06efbc3ff3e' # r
  48. '0ecc1ba4106702fb5b60cd7a8b94193ea71af5e6e860d978c49a6a63d97e4ded')), # s
  49. _pv(
  50. 'mainnet-tx-msgdeposit2.binpb',
  51. '44f45b91e97558e63a11758ac3c186196b9b46f6331f32eff1256888ea879b62',
  52. deposit_tx_parms(
  53. 'ETH', 'USDT-0XDAC17F958D2EE523A2206206994597C13D831EC7', 'USDT',
  54. 'thor1xxncvuptvmgcl5ep7rry3xehtw97jsg9uyv6rn',
  55. '500.00000000',
  56. 50000000,
  57. 88176,
  58. 104625,
  59. decimals = None,
  60. synth = False,
  61. trade = True,
  62. memo = '=:AVAX~AVAX:thor1xxncvuptvmgcl5ep7rry3xehtw97jsg9uyv6rn:2113883178',
  63. pubkey = '02a6e97e3f20809511500d8895117d4344badda9e6af4216d41b10a105d1070254',
  64. signature = 'b0673ab89781199d35b94051f26db30996f055abd71804d67fe4bdf33934bdb9' # r
  65. '30d830675d398c6d042328ef681bf1a23e856ed0ab33d948ea149489400db953'), # s
  66. null_fee = True),
  67. _pv(
  68. 'mainnet-tx-msgdeposit3.binpb',
  69. '02d2fb2f2e5ac00ad4a31c37ffc43b72963f93598f8b3c8f4d3932c2e950b459',
  70. deposit_tx_parms(
  71. 'ETH', 'USDC-0XA0B86991C6218B36C1D19D4A2E9EB0CE3606EB48', 'USDC',
  72. 'thor1lukwlve7hayy66qrdkp4k7sh0emjqwergy7tl3',
  73. '2425.75973697',
  74. 600000000,
  75. 125632,
  76. 375988,
  77. decimals = 8,
  78. synth = None,
  79. trade = True,
  80. memo = '=:THOR.RUNE:thor1lukwlve7hayy66qrdkp4k7sh0emjqwergy7tl3:127025748855/0/1',
  81. pubkey = '03da157f891abfe7822efb91f59667aa6cc6c3768a7e280caeb9ae243c969eb3e7',
  82. signature = 'be8324f6a1535b971d63715532e2e42ee3c35c05b81ed9b6bccd9a1765688eca'
  83. '4ca00a66119b07c5168c6c78e22299becc82d5f311ee79ae9183776b0dff3269'))
  84. ]
  85. _bv = namedtuple('build_vector', ['txid', 'txid2', 'parms', 'null_fee'], defaults=[None])
  86. build_vectors = [
  87. _bv(
  88. '3939612d0ddc55fd4d1c6ef118d1b2085a6655cb57d55ac9efd658467e039e0c',
  89. 'e783ced14909a9e2a21c99b6b3b66fb38ba3bdf985876bb8d6cea02813d603a9',
  90. send_tx_parms(
  91. 'thor1tx3nm6xfynq3re5ehtm6530z0pah9qjeu0r9nd',
  92. 'thor1j5u6vlr8kzt76fe7896hsmurkhgn68j0z4qa6w',
  93. '123.456789',
  94. 8000000,
  95. 12345,
  96. 37,
  97. wifkey = 'L5nWojqqMLq7wh3CfhxUNYQ38acABD6sUao9dfb8i5B5wSefCJXe')),
  98. _bv(
  99. '0d41e0ee40cd18a991cd8f0ef0e60e4c5bea898c53d54e00b6dddc0c9ce7edb7',
  100. '444e026fe5d0988da602dc22f0ff6172c080f1d2e2d66012d86f4afd314b78d6',
  101. deposit_tx_parms(
  102. 'THOR', 'RUNE', 'RUNE',
  103. 'thor18ug6p4zs5dsy0m3u69gf5md5ssdg8hqkk8aya4',
  104. '123.456789',
  105. 8000000,
  106. 12345,
  107. 37,
  108. decimals = 8,
  109. memo = '=:MEMO',
  110. wifkey = 'Ky9bSjPUD35uUaY3JReXiESivnfxV6rLMsW1wTFyvVZwYXpX95vF'))
  111. ]
  112. swap_build_vectors = [
  113. _bv(
  114. '0d41e0ee40cd18a991cd8f0ef0e60e4c5bea898c53d54e00b6dddc0c9ce7edb7',
  115. '444e026fe5d0988da602dc22f0ff6172c080f1d2e2d66012d86f4afd314b78d6',
  116. swap_tx_parms(
  117. 'thor18ug6p4zs5dsy0m3u69gf5md5ssdg8hqkk8aya4',
  118. '123.456789',
  119. 8000000,
  120. 12345,
  121. 37,
  122. memo = '=:MEMO',
  123. wifkey = 'Ky9bSjPUD35uUaY3JReXiESivnfxV6rLMsW1wTFyvVZwYXpX95vF'))
  124. ]
  125. def test_tx(src, cfg, vec):
  126. proto = cfg._proto
  127. parms = vec.parms._replace(amt=Decimal(vec.parms.amt))
  128. if parms.pubkey:
  129. parms = parms._replace(
  130. pubkey = bytes.fromhex(parms.pubkey),
  131. signature = bytes.fromhex(parms.signature))
  132. assert src in ('parse', 'build', 'swapbuild')
  133. match src:
  134. case 'parse':
  135. tx_in = open(os.path.join('test/ref/thorchain', vec.fn), 'br').read()
  136. tx = RuneTx.loads(tx_in)
  137. if not parms.from_addr:
  138. ymsg(f'Warning: missing test vector data for {vec.fn}')
  139. assert bytes(tx) == tx_in
  140. case 'build':
  141. tx = build_tx(cfg, proto, parms, null_fee=vec.null_fee)
  142. case 'swapbuild':
  143. tx = build_swap_tx(cfg, proto, parms)
  144. vmsg(pp_fmt(tx))
  145. msg_type = 'MsgSend' if tx.body.messages[0].id == '/types.MsgSend' else 'MsgDeposit'
  146. vmsg('\n TX info:\n ' + '\n '.join(tx_info(tx, proto)) + '\n')
  147. tx.verify_sig(proto, parms.account_number)
  148. pubkey = tx.authInfo.signerInfos[0].publicKey.key.data
  149. vec_txid2 = getattr(vec, 'txid2', None)
  150. assert hash160(pubkey) == getattr(
  151. tx.body.messages[0].body,
  152. 'fromAddress' if msg_type == 'MsgSend' else 'signer')
  153. if tx.txid not in (vec.txid, vec_txid2):
  154. raise ValueError(f'{tx.txid} not in ({vec.txid}, {vec_txid2})')
  155. if tx.txid == vec_txid2:
  156. ymsg('\nWarning: non-standard TxID produced')
  157. if src == 'parse' and parms.from_addr:
  158. built_tx = build_tx(cfg, proto, parms, null_fee=vec.null_fee)
  159. addr_from_pubkey = proto.encode_addr_bech32x(hash160(pubkey))
  160. assert addr_from_pubkey == parms.from_addr
  161. assert bytes(built_tx) == tx_in
  162. raw_tx = RawTx(bytes(tx.body), bytes(tx.authInfo), tx.signatures)
  163. assert bytes(raw_tx) == bytes(RawTx.loads(tx_in))
  164. assert bytes(raw_tx) == bytes(tx.raw)
  165. assert tx_in == bytes(tx)
  166. class unit_tests:
  167. def txparse(self, name, ut, desc='transaction parsing and signature verification'):
  168. for vec in parse_vectors:
  169. test_tx('parse', test_cfg, vec)
  170. return True
  171. def txbuild(self, name, ut, desc='transaction building and signing (MsgSend, MsgDeposit)'):
  172. for vec in build_vectors:
  173. test_tx('build', test_cfg, vec)
  174. return True
  175. def swaptxbuild(self, name, ut, desc='transaction building and signing (Swap TX)'):
  176. for vec in swap_build_vectors:
  177. test_tx('swapbuild', test_cfg, vec)
  178. return True
  179. def rpc(self, name, ut, desc='remote RPC operations'):
  180. import sys, asyncio
  181. from mmgen.rpc import rpc_init
  182. from ..cmdtest_d.httpd.thornode.rpc import ThornodeRPCServer
  183. silence()
  184. regtest_cfg = Config({'coin': 'rune', 'regtest': True, 'test_suite': True})
  185. end_silence()
  186. thornode_server = ThornodeRPCServer(test_cfg)
  187. thornode_server.start()
  188. addr = 'thor1lukwlve7hayy66qrdkp4k7sh0emjqwergy7tl3'
  189. txhash = 'abcdef01' * 8
  190. txbytes = open('test/ref/thorchain/mainnet-tx-msgsend1.binpb', 'rb').read()
  191. async def main():
  192. rpc = await rpc_init(regtest_cfg)
  193. res = rpc.get_account_info(addr)
  194. assert res['address'] == addr
  195. assert res['account_number']
  196. assert res['sequence']
  197. res = rpc.get_tx_info(txhash)
  198. assert res['hash'] == txhash.upper()
  199. res = rpc.tx_op(txbytes.hex(), op='check_tx')
  200. assert res['code'] == 0
  201. asyncio.run(main())
  202. return True