Advertisement
Guest User

Untitled

a guest
Dec 20th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import os
  3. import re
  4. import sys
  5. import time
  6. import codecs
  7. import requests
  8. import json
  9. import platform
  10.  
  11. #chain=sys.argv[0]
  12. #orcl_id=sys.argv[1]
  13. chain='CFEK'
  14. orcl_id="155ea1645056c7e369b2bd1fb50c81608b8caac4d3bb1bf6f532f435f6d90d4d"
  15.  
  16. # define function that fetchs rpc creds from .conf
  17. def def_credentials(chain):
  18. operating_system = platform.system()
  19. if operating_system == 'Darwin':
  20. ac_dir = os.environ['HOME'] + '/Library/Application Support/Komodo'
  21. elif operating_system == 'Linux':
  22. ac_dir = os.environ['HOME'] + '/.komodo'
  23. elif operating_system == 'Win64':
  24. ac_dir = "dont have windows machine now to test"
  25. # define config file path
  26. if chain == 'KMD':
  27. coin_config_file = str(ac_dir + '/komodo.conf')
  28. else:
  29. coin_config_file = str(ac_dir + '/' + chain + '/' + chain + '.conf')
  30. #define rpc creds
  31. with open(coin_config_file, 'r') as f:
  32. for line in f:
  33. l = line.rstrip()
  34. if re.search('rpcuser', l):
  35. rpcuser = l.replace('rpcuser=', '')
  36. elif re.search('rpcpassword', l):
  37. rpcpassword = l.replace('rpcpassword=', '')
  38. elif re.search('rpcport', l):
  39. rpcport = l.replace('rpcport=', '')
  40. return('http://' + rpcuser + ':' + rpcpassword + '@127.0.0.1:' + rpcport)
  41.  
  42. # define function that posts json data
  43. def post_rpc(url, payload, auth=None):
  44. try:
  45. r = requests.post(url, data=json.dumps(payload), auth=auth)
  46. return(json.loads(r.text))
  47. except Exception as e:
  48. raise Exception("Couldn't connect to " + url + ": ", e)
  49.  
  50. def oraclesinfo_rpc(chain, oracletxid):
  51. oraclesinfo_payload = {
  52. "jsonrpc": "1.0",
  53. "id": "python",
  54. "method": "oraclesinfo",
  55. "params": [oracletxid]}
  56. oraclesinfo_result = post_rpc(def_credentials(chain), oraclesinfo_payload)
  57. return(oraclesinfo_result['result'])
  58.  
  59. def oraclesdata_rpc(chain, oracletxid, hexstr):
  60. oraclesdata_payload = {
  61. "jsonrpc": "1.0",
  62. "id": "python",
  63. "method": "oraclesdata",
  64. "params": [
  65. oracletxid,
  66. hexstr]}
  67. oraclesdata_result = post_rpc(def_credentials(chain), oraclesdata_payload)
  68. return(oraclesdata_result['result'])
  69.  
  70. def sendrawtx_rpc(chain, rawtx):
  71. sendrawtx_payload = {
  72. "jsonrpc": "1.0",
  73. "id": "python",
  74. "method": "sendrawtransaction",
  75. "params": [rawtx]}
  76. rpcurl = def_credentials(chain)
  77. return(post_rpc(def_credentials(chain), sendrawtx_payload))
  78.  
  79. def write2oracle(chain, orcl_id, MSG):
  80. print("MSG: " + str(MSG))
  81. rawhex = codecs.encode(MSG).hex()
  82.  
  83. #get length in bytes of hex in decimal
  84. bytelen = int(len(rawhex) / int(2))
  85. hexlen = format(bytelen, 'x')
  86.  
  87. #get length in big endian hex
  88. if bytelen < 16:
  89. bigend = "000" + str(hexlen)
  90. elif bytelen < 256:
  91. bigend = "00" + str(hexlen)
  92. elif bytelen < 4096:
  93. bigend = "0" + str(hexlen)
  94. elif bytelen < 65536:
  95. bigend = str(hexlen)
  96. else:
  97. print("message too large, must be less than 65536 characters")
  98.  
  99. #convert big endian length to little endian, append rawhex to little endian length
  100. lilend = bigend[2] + bigend[3] + bigend[0] + bigend[1]
  101. fullhex = lilend + rawhex
  102.  
  103. oraclesdata_result = oraclesdata_rpc(chain, orcl_id, fullhex)
  104. print(chain)
  105. print(orcl_id)
  106. print(fullhex)
  107. result = oraclesdata_result['result']
  108.  
  109. if result == 'error':
  110. print('ERROR:' + oraclesdata_result['error'] + ', try using oraclesregister if you have not already')
  111. else:
  112. rawtx = oraclesdata_result['hex']
  113. sendrawtx_result = sendrawtx_rpc(chain, rawtx)
  114. return result
  115.  
  116.  
  117. while True:
  118. rawdata = 'put the data you want to write to the oracle here'
  119. result = write2oracle(chain, orcl_id, rawdata)
  120. print('=========== Data written to Oracle ===============')
  121. print('sleeping')
  122. time.sleep(300)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement