Advertisement
zefie

Nicehash API via MicroPython

Nov 1st, 2023 (edited)
961
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.58 KB | None | 0 0
  1. # usage example
  2. # obj = nicehash_request("/main/api/v2/accounting/accounts2","fiat=USD")
  3.  
  4. def nicehash_request(url, query=""):
  5.     start = utime.ticks_ms()
  6.     localtime = str('{0:f}'.format(get_time(False,True) * 1000)).split('.')[0] # epoch in ms
  7.     nonce = str(uuid.uuid4())
  8.     reqid = str(uuid.uuid4())
  9.     message = bytearray(secrets['nicehash_key'],"utf-8") # api key
  10.     message += b'\x00'
  11.     message += bytearray(localtime,"utf-8")
  12.     message += b'\x00'
  13.     message += bytearray(nonce,"utf-8")
  14.     message += b'\x00'
  15.     message += b'\x00'
  16.     message += bytearray(secrets['nicehash_org'],"utf-8") # api organization
  17.     message += b'\x00'
  18.     message += b'\x00'
  19.     message += bytearray("GET","utf-8")
  20.     message += b'\x00'
  21.     message += bytearray(url,"utf-8")
  22.     if query != "":
  23.         message += b'\x00'
  24.         message += bytearray(query,"utf-8")
  25.    
  26.     hmacs = hmac.HMAC(bytearray(secrets['nicehash_secret'],"utf-8"), message, hashlib.sha256) # api secret
  27.     hashed_string = hmacs.hexdigest()
  28.     auth = secrets['nicehash_key']+":"+hashed_string; # api key
  29.     headers = {
  30.         "X-Time": localtime,
  31.         "X-Nonce": nonce,
  32.         "X-Organization-ID": secrets['nicehash_org'], # api organization
  33.         "X-Request-ID": reqid,
  34.         "X-Auth": auth
  35.     }
  36.     response = urequests.get("https://api2.nicehash.com"+url+"?"+query, headers=headers)
  37.     obj = json.loads(response.text)
  38.     response.close()
  39.     delta = utime.ticks_diff(utime.ticks_ms(), start)
  40.     debug("Received response from Nicehash API (took "+str(delta)+"ms)")
  41.     return obj
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement