Advertisement
Guest User

monero

a guest
Feb 8th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.92 KB | None | 0 0
  1. import json
  2. from json import JSONDecodeError
  3.  
  4. import requests
  5. from requests.auth import HTTPDigestAuth
  6.  
  7. url_base = "http://{}:{}/{}"
  8. json_string = "json_rpc"
  9.  
  10. ERROR_MSG = -1
  11.  
  12.  
  13. class BColors:
  14.     HEADER = '\033[95m'
  15.     OKBLUE = '\033[94m'
  16.     OKGREEN = '\033[92m'
  17.     WARNING = '\033[93m'
  18.     FAIL = '\033[91m'
  19.     ENDC = '\033[0m'
  20.     BOLD = '\033[1m'
  21.     UNDERLINE = '\033[4m'
  22.  
  23.  
  24. class MoneroHandler():
  25.  
  26.     def __init__(self, ip, port, mainnet=True, user="test", password="123456"):
  27.         self.ip = ip
  28.         self.url = url_base.format(ip, port, json_string)
  29.         self.port = port
  30.         self.mainnet = mainnet
  31.         self.user = user
  32.         self.password = password
  33.  
  34.     def get_total_balance(self):
  35.         payload = {
  36.             "jsonrpc": "2.0",
  37.             "id": "0",
  38.             "method": "get_balance",
  39.             "params": {
  40.                 "account_index": 0,
  41.             }
  42.         }
  43.         result = self.make_request(payload)
  44.         # TODO add something like match to include the real error msg in the error msg
  45.         if result == ERROR_MSG:
  46.             return ERROR_MSG
  47.         return result["result"]["unlocked_balance"]
  48.  
  49.     def get_account_balance(self, address):
  50.         index = self.get_account_index(address)
  51.         print("INDEX: " + str(index))
  52.         payload = {
  53.             "jsonrpc": "2.0",
  54.             "id": "0",
  55.             "method": "get_balance",
  56.             "params": {
  57.                 "account_index": 0,
  58.                 "address_indices": [index[0], index[1]]
  59.             }
  60.         }
  61.         result = self.make_request(payload)
  62.         if result == ERROR_MSG:
  63.             return result
  64.         """ EXAMPLE OUTPUT
  65.        {
  66.            "id": "0",
  67.            "jsonrpc": "2.0",
  68.            "result": {
  69.                "balance": 157443303037455077,
  70.                "multisig_import_needed": false,
  71.                "per_subaddress": [{
  72.                    "address": "55LTR8KniP4LQGJSPtbYDacR7dz8RBFnsfAKMaMuwUNYX6aQbBcovzDPyrQF9KXF9tVU6Xk3K8no1BywnJX6GvZX8yJsXvt",
  73.                    "address_index": 0,
  74.                    "balance": 157360317826255077,
  75.                    "label": "Primary account",
  76.                    "num_unspent_outputs": 5281,
  77.                    "unlocked_balance": 157360317826255077
  78.                }, {
  79.                    "address": "7BnERTpvL5MbCLtj5n9No7J5oE5hHiB3tVCK5cjSvCsYWD2WRJLFuWeKTLiXo5QJqt2ZwUaLy2Vh1Ad51K7FNgqcHgjW85o",
  80.                    "address_index": 1,
  81.                    "balance": 59985211200000,
  82.                    "label": "",
  83.                    "num_unspent_outputs": 1,
  84.                    "unlocked_balance": 59985211200000
  85.                }],
  86.                "unlocked_balance": 157443303037455077
  87.            }
  88.        }
  89.        """
  90.         return result["result"]["balance"]
  91.  
  92.     def get_account_index(self, address):
  93.         payload = {
  94.             "jsonrpc": "2.0",
  95.             "id": "0",
  96.             "method": "get_address_index",
  97.             "params": {
  98.                 "address": "{}".format(address)
  99.             }
  100.         }
  101.         result = self.make_request(payload)
  102.         # TODO add something like match to include the real error msg in the error msg
  103.         if result == ERROR_MSG:
  104.             return ERROR_MSG
  105.         """ EXAMPLE RESULT
  106.        {
  107.            "id": "0",
  108.            "jsonrpc": "2.0",
  109.            "result": {
  110.                "index": {
  111.                    "major": 0,
  112.                    "minor": 1
  113.                }
  114.            }
  115.        }
  116.        """
  117.         # return as a tuple
  118.         try:
  119.             return result["result"]["index"]["major"], result["result"]["index"]["minor"]
  120.         except KeyError:
  121.             print(str(result))
  122.             return ERROR_MSG
  123.  
  124.     def get_account_balance_min(self, address, min_value):
  125.         pass
  126.  
  127.     def get_new_sub_address(self):
  128.         payload = {
  129.             "jsonrpc": "2.0",
  130.             "id": "0",
  131.             "method": "create_address",
  132.             "params": {
  133.                 "account_index": 0,
  134.             }
  135.         }
  136.         result = self.make_request(payload)
  137.         if result == ERROR_MSG:
  138.             return ERROR_MSG
  139.         """ EXAMPLE RESULT
  140.        {
  141.            "id": "0",
  142.            "jsonrpc": "2.0",
  143.            "result": {
  144.                "address": "7BG5jr9QS5sGMdpbBrZEwVLZjSKJGJBsXdZLt8wiXyhhLjy7x2LZxsrAnHTgD8oG46ZtLjUGic2pWc96GFkGNPQQDA3Dt7Q",
  145.                "address_index": 5
  146.            }
  147.        }
  148.        """
  149.         return result["result"]["address"]
  150.  
  151.     def make_request(self, payload):
  152.         try:
  153.             headers = {'content-type': 'application/json'}
  154.             data = json.dumps(payload)
  155.             response = requests.post(self.url, data=data, headers=headers,
  156.                                      auth=HTTPDigestAuth(self.user, self.password)).json()
  157.             return response
  158.         except (IOError, JSONDecodeError) as e:
  159.             print(BColors.FAIL + str(self.url) + BColors.ENDC)
  160.             print(BColors.FAIL + str(payload) + BColors.ENDC)
  161.             print(BColors.FAIL + str(e) + BColors.ENDC)
  162.             print(BColors.FAIL + "Fehler bei Verbindung mit Server" + BColors.ENDC)
  163.             return ERROR_MSG
  164.  
  165.     def get_donation_address(self):
  166.         payload = {
  167.             "jsonrpc": "2.0",
  168.             "id": "0",
  169.             "method": "get_address",
  170.             "params": {
  171.                 "account_index": 0,
  172.                 "address_index": [
  173.                     0,
  174.                 ]
  175.             }
  176.         }
  177.         result = self.make_request(payload)
  178.         # TODO add something like match to include the real error msg in the error msg
  179.         if result == ERROR_MSG:
  180.             return ERROR_MSG
  181.  
  182.  
  183. """
  184.    a string representation of  the atomi xmr value
  185.    so something like 1 to 0.0000000001
  186. """
  187.  
  188.  
  189. def atmomic_to_normal_string(atomic_value):
  190.     tmp = atomic_value / (10 ** 12)
  191.     return '{0:.12f}'.format(tmp)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement