Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- from json import JSONDecodeError
- import requests
- from requests.auth import HTTPDigestAuth
- url_base = "http://{}:{}/{}"
- json_string = "json_rpc"
- ERROR_MSG = -1
- class BColors:
- HEADER = '\033[95m'
- OKBLUE = '\033[94m'
- OKGREEN = '\033[92m'
- WARNING = '\033[93m'
- FAIL = '\033[91m'
- ENDC = '\033[0m'
- BOLD = '\033[1m'
- UNDERLINE = '\033[4m'
- class MoneroHandler():
- def __init__(self, ip, port, mainnet=True, user="test", password="123456"):
- self.ip = ip
- self.url = url_base.format(ip, port, json_string)
- self.port = port
- self.mainnet = mainnet
- self.user = user
- self.password = password
- def get_total_balance(self):
- payload = {
- "jsonrpc": "2.0",
- "id": "0",
- "method": "get_balance",
- "params": {
- "account_index": 0,
- }
- }
- result = self.make_request(payload)
- # TODO add something like match to include the real error msg in the error msg
- if result == ERROR_MSG:
- return ERROR_MSG
- return result["result"]["unlocked_balance"]
- def get_account_balance(self, address):
- index = self.get_account_index(address)
- print("INDEX: " + str(index))
- payload = {
- "jsonrpc": "2.0",
- "id": "0",
- "method": "get_balance",
- "params": {
- "account_index": 0,
- "address_indices": [index[0], index[1]]
- }
- }
- result = self.make_request(payload)
- if result == ERROR_MSG:
- return result
- """ EXAMPLE OUTPUT
- {
- "id": "0",
- "jsonrpc": "2.0",
- "result": {
- "balance": 157443303037455077,
- "multisig_import_needed": false,
- "per_subaddress": [{
- "address": "55LTR8KniP4LQGJSPtbYDacR7dz8RBFnsfAKMaMuwUNYX6aQbBcovzDPyrQF9KXF9tVU6Xk3K8no1BywnJX6GvZX8yJsXvt",
- "address_index": 0,
- "balance": 157360317826255077,
- "label": "Primary account",
- "num_unspent_outputs": 5281,
- "unlocked_balance": 157360317826255077
- }, {
- "address": "7BnERTpvL5MbCLtj5n9No7J5oE5hHiB3tVCK5cjSvCsYWD2WRJLFuWeKTLiXo5QJqt2ZwUaLy2Vh1Ad51K7FNgqcHgjW85o",
- "address_index": 1,
- "balance": 59985211200000,
- "label": "",
- "num_unspent_outputs": 1,
- "unlocked_balance": 59985211200000
- }],
- "unlocked_balance": 157443303037455077
- }
- }
- """
- return result["result"]["balance"]
- def get_account_index(self, address):
- payload = {
- "jsonrpc": "2.0",
- "id": "0",
- "method": "get_address_index",
- "params": {
- "address": "{}".format(address)
- }
- }
- result = self.make_request(payload)
- # TODO add something like match to include the real error msg in the error msg
- if result == ERROR_MSG:
- return ERROR_MSG
- """ EXAMPLE RESULT
- {
- "id": "0",
- "jsonrpc": "2.0",
- "result": {
- "index": {
- "major": 0,
- "minor": 1
- }
- }
- }
- """
- # return as a tuple
- try:
- return result["result"]["index"]["major"], result["result"]["index"]["minor"]
- except KeyError:
- print(str(result))
- return ERROR_MSG
- def get_account_balance_min(self, address, min_value):
- pass
- def get_new_sub_address(self):
- payload = {
- "jsonrpc": "2.0",
- "id": "0",
- "method": "create_address",
- "params": {
- "account_index": 0,
- }
- }
- result = self.make_request(payload)
- if result == ERROR_MSG:
- return ERROR_MSG
- """ EXAMPLE RESULT
- {
- "id": "0",
- "jsonrpc": "2.0",
- "result": {
- "address": "7BG5jr9QS5sGMdpbBrZEwVLZjSKJGJBsXdZLt8wiXyhhLjy7x2LZxsrAnHTgD8oG46ZtLjUGic2pWc96GFkGNPQQDA3Dt7Q",
- "address_index": 5
- }
- }
- """
- return result["result"]["address"]
- def make_request(self, payload):
- try:
- headers = {'content-type': 'application/json'}
- data = json.dumps(payload)
- response = requests.post(self.url, data=data, headers=headers,
- auth=HTTPDigestAuth(self.user, self.password)).json()
- return response
- except (IOError, JSONDecodeError) as e:
- print(BColors.FAIL + str(self.url) + BColors.ENDC)
- print(BColors.FAIL + str(payload) + BColors.ENDC)
- print(BColors.FAIL + str(e) + BColors.ENDC)
- print(BColors.FAIL + "Fehler bei Verbindung mit Server" + BColors.ENDC)
- return ERROR_MSG
- def get_donation_address(self):
- payload = {
- "jsonrpc": "2.0",
- "id": "0",
- "method": "get_address",
- "params": {
- "account_index": 0,
- "address_index": [
- 0,
- ]
- }
- }
- result = self.make_request(payload)
- # TODO add something like match to include the real error msg in the error msg
- if result == ERROR_MSG:
- return ERROR_MSG
- """
- a string representation of the atomi xmr value
- so something like 1 to 0.0000000001
- """
- def atmomic_to_normal_string(atomic_value):
- tmp = atomic_value / (10 ** 12)
- return '{0:.12f}'.format(tmp)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement