diff options
Diffstat (limited to 'utils')
-rwxr-xr-x | utils/python-rpc/console | 3 | ||||
-rwxr-xr-x | utils/python-rpc/console.py | 115 | ||||
-rw-r--r-- | utils/python-rpc/framework/daemon.py | 120 | ||||
-rw-r--r-- | utils/python-rpc/framework/wallet.py | 114 |
4 files changed, 303 insertions, 49 deletions
diff --git a/utils/python-rpc/console b/utils/python-rpc/console new file mode 100755 index 000000000..a4ec6a9ad --- /dev/null +++ b/utils/python-rpc/console @@ -0,0 +1,3 @@ +#!/bin/sh + +env python -i $(dirname $0)/console.py "$@" diff --git a/utils/python-rpc/console.py b/utils/python-rpc/console.py index ab0d9f27f..57a04528b 100755 --- a/utils/python-rpc/console.py +++ b/utils/python-rpc/console.py @@ -4,46 +4,85 @@ from __future__ import print_function import sys import subprocess import socket -from framework import rpc -from framework import wallet -from framework import daemon +import urlparse +import framework.rpc +import framework.daemon +import framework.wallet -USAGE = 'usage: python -i console.py <port>' -try: - port = int(sys.argv[1]) -except: - print(USAGE) - sys.exit(1) - -# check for open port -s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) -s.settimeout(1) -if s.connect_ex(('127.0.0.1', port)) != 0: - print('No wallet or daemon RPC on port ' + str(port)) - sys.exit(1) -s.close() - -# both wallet and daemon have a get_version JSON RPC -rpc = rpc.JSONRPC('{protocol}://{host}:{port}'.format(protocol='http', host='127.0.0.1', port=port)) -get_version = { - 'method': 'get_version', - 'jsonrpc': '2.0', - 'id': '0' -} -try: - res = rpc.send_json_rpc_request(get_version) -except Exception, e: - print('Failed to call version RPC: ' + str(e)) - sys.exit(1) +USAGE = 'usage: python -i console.py [[[scheme]<host>:]<port> [[[scheme]<host>:]<port>...]]' +daemons = [] +wallets = [] +rpcs = [] +for n in range(1, len(sys.argv)): + scheme='http' + host='127.0.0.1' + port=None + try: + try: + port = int(sys.argv[n]) + except: + t = urlparse.urlparse(sys.argv[n], allow_fragments = False) + scheme = t.scheme or scheme + host = t.hostname or host + port = t.port or port + if scheme != 'http' and scheme != 'https': + raise Exception(USAGE) + if port <= 0 or port > 65535: + raise Exception(USAGE) + except Exception, e: + print('Error: ' + str(e)) + raise Exception(USAGE) + + # check for open port + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.settimeout(1) + if s.connect_ex((host, port)) != 0: + raise Exception('No wallet or daemon RPC on port ' + str(port)) + s.close() + + # both wallet and daemon have a get_version JSON RPC + rpc = framework.rpc.JSONRPC('{protocol}://{host}:{port}'.format(protocol=scheme, host=host, port=port)) + get_version = { + 'method': 'get_version', + 'jsonrpc': '2.0', + 'id': '0' + } + try: + res = rpc.send_json_rpc_request(get_version) + except Exception, e: + raise Exception('Failed to call version RPC: ' + str(e)) -if 'version' not in res: - print('Server is not a monero process') - sys.exit(1) + if 'version' not in res: + raise Exception('Server is not a Monero process') -if 'status' in res: - rpc = daemon.Daemon(port=port) + if 'status' in res: + daemons.append(framework.daemon.Daemon(port=port)) + rpcs.append(daemons[-1]) + else: + wallets.append(framework.wallet.Wallet(port=port)) + rpcs.append(wallets[-1]) + +# add tab completion if we can: https://stackoverflow.com/questions/246725 +try: + import readline +except: + pass else: - rpc = wallet.Wallet(port=port) + import rlcompleter + readline.parse_and_bind('tab: complete') + +if len(daemons) == 1: + daemon = daemons[0] +if len(wallets) == 1: + wallet = wallets[0] -print('Connected to %s RPC on port %u' % ('daemon' if 'status' in res else 'wallet', port)) -print('The \'rpc\' object may now be used to use the API') +didx = 0 +widx = 0 +for rpc in rpcs: + if type(rpc) == framework.daemon.Daemon: + var = "daemon" if len(daemons) == 1 else "daemons[" + str(didx) + "]" + didx += 1 + else: + var = "wallet" if len(wallets) == 1 else "wallets[" + str(widx) + "]" + widx += 1 + print('Variable \'%s\' connected to %s RPC on %s:%u' % (var, 'daemon' if type(rpc) == framework.daemon.Daemon else 'wallet', rpc.host ,rpc.port)) diff --git a/utils/python-rpc/framework/daemon.py b/utils/python-rpc/framework/daemon.py index f60fe62db..f4d5e90f0 100644 --- a/utils/python-rpc/framework/daemon.py +++ b/utils/python-rpc/framework/daemon.py @@ -33,14 +33,17 @@ from .rpc import JSONRPC class Daemon(object): def __init__(self, protocol='http', host='127.0.0.1', port=0, idx=0): + self.host = host + self.port = port self.rpc = JSONRPC('{protocol}://{host}:{port}'.format(protocol=protocol, host=host, port=port if port else 18180+idx)) - def getblocktemplate(self, address): + def getblocktemplate(self, address, prev_block = ""): getblocktemplate = { 'method': 'getblocktemplate', 'params': { 'wallet_address': address, - 'reserve_size' : 1 + 'reserve_size' : 1, + 'prev_block' : prev_block, }, 'jsonrpc': '2.0', 'id': '0' @@ -143,13 +146,15 @@ class Daemon(object): } return self.rpc.send_json_rpc_request(hard_fork_info) - def generateblocks(self, address, blocks=1): + def generateblocks(self, address, blocks=1, prev_block = "", starting_nonce = 0): generateblocks = { 'method': 'generateblocks', 'params': { 'amount_of_blocks' : blocks, 'reserve_size' : 20, - 'wallet_address': address + 'wallet_address': address, + 'prev_block': prev_block, + 'starting_nonce': starting_nonce, }, 'jsonrpc': '2.0', 'id': '0' @@ -217,3 +222,110 @@ class Daemon(object): 'id': '0' } return self.rpc.send_json_rpc_request(get_version) + + def get_bans(self): + get_bans = { + 'method': 'get_bans', + 'params': { + }, + 'jsonrpc': '2.0', + 'id': '0' + } + return self.rpc.send_json_rpc_request(get_bans) + + def set_bans(self, bans = []): + set_bans = { + 'method': 'set_bans', + 'params': { + 'bans': bans + }, + 'jsonrpc': '2.0', + 'id': '0' + } + return self.rpc.send_json_rpc_request(set_bans) + + def get_transactions(self, txs_hashes = [], decode_as_json = False, prune = False, split = False): + get_transactions = { + 'txs_hashes': txs_hashes, + 'decode_as_json': decode_as_json, + 'prune': prune, + 'split': split, + } + return self.rpc.send_request('/get_transactions', get_transactions) + + def get_outs(self, outputs = [], get_txid = False): + get_outs = { + 'outputs': outputs, + 'get_txid': get_txid, + } + return self.rpc.send_request('/get_outs', get_outs) + + def get_coinbase_tx_sum(self, height, count): + get_coinbase_tx_sum = { + 'method': 'get_coinbase_tx_sum', + 'params': { + 'height': height, + 'count': count, + }, + 'jsonrpc': '2.0', + 'id': '0' + } + return self.rpc.send_json_rpc_request(get_coinbase_tx_sum) + + def get_output_distribution(self, amounts = [], from_height = 0, to_height = 0, cumulative = False, binary = False, compress = False): + get_output_distribution = { + 'method': 'get_output_distribution', + 'params': { + 'amounts': amounts, + 'from_height': from_height, + 'to_height': to_height, + 'cumulative': cumulative, + 'binary': binary, + 'compress': compress, + }, + 'jsonrpc': '2.0', + 'id': '0' + } + return self.rpc.send_json_rpc_request(get_output_distribution) + + def get_output_histogram(self, amounts = [], min_count = 0, max_count = 0, unlocked = False, recent_cutoff = 0): + get_output_histogram = { + 'method': 'get_output_histogram', + 'params': { + 'amounts': amounts, + 'min_count': min_count, + 'max_count': max_count, + 'unlocked': unlocked, + 'recent_cutoff': recent_cutoff, + }, + 'jsonrpc': '2.0', + 'id': '0' + } + return self.rpc.send_json_rpc_request(get_output_histogram) + + def set_log_level(self, level): + set_log_level = { + 'level': level, + } + return self.rpc.send_request('/set_log_level', set_log_level) + + def set_log_categories(self, categories = ''): + set_log_categories = { + 'categories': categories, + } + return self.rpc.send_request('/set_log_categories', set_log_categories) + + def get_alt_blocks_hashes(self): + get_alt_blocks_hashes = { + } + return self.rpc.send_request('/get_alt_blocks_hashes', get_alt_blocks_hashes) + + def get_alternate_chains(self): + get_alternate_chains = { + 'method': 'get_alternate_chains', + 'params': { + }, + 'jsonrpc': '2.0', + 'id': '0' + } + return self.rpc.send_json_rpc_request(get_alternate_chains) diff --git a/utils/python-rpc/framework/wallet.py b/utils/python-rpc/framework/wallet.py index ea683b8c5..a80aaefec 100644 --- a/utils/python-rpc/framework/wallet.py +++ b/utils/python-rpc/framework/wallet.py @@ -33,6 +33,8 @@ from .rpc import JSONRPC class Wallet(object): def __init__(self, protocol='http', host='127.0.0.1', port=0, idx=0): + self.host = host + self.port = port self.rpc = JSONRPC('{protocol}://{host}:{port}'.format(protocol=protocol, host=host, port=port if port else 18090+idx)) def make_uniform_destinations(self, address, transfer_amount, transfer_number_of_destinations=1): @@ -89,6 +91,18 @@ class Wallet(object): } return self.rpc.send_json_rpc_request(transfer) + def get_transfer_by_txid(self, txid, account_index = 0): + get_transfer_by_txid = { + 'method': 'get_transfer_by_txid', + 'params': { + 'txid': txid, + 'account_index': account_index, + }, + 'jsonrpc': '2.0', + 'id': '0' + } + return self.rpc.send_json_rpc_request(get_transfer_by_txid) + def get_bulk_payments(self, payment_ids = [], min_block_height = 0): get_bulk_payments = { 'method': 'get_bulk_payments', @@ -101,24 +115,25 @@ class Wallet(object): } return self.rpc.send_json_rpc_request(get_bulk_payments) - def describe_transfer(self, unsigned_txset): + def describe_transfer(self, unsigned_txset = '', multisig_txset = ''): describe_transfer = { 'method': 'describe_transfer', 'params': { 'unsigned_txset': unsigned_txset, + 'multisig_txset': multisig_txset, }, 'jsonrpc': '2.0', 'id': '0' } return self.rpc.send_json_rpc_request(describe_transfer) - def create_wallet(self, index=''): + def create_wallet(self, filename='', password = '', language = 'English'): create_wallet = { 'method': 'create_wallet', 'params': { - 'filename': 'testWallet' + index, - 'password' : '', - 'language' : 'English' + 'filename': filename, + 'password': password, + 'language': language }, 'jsonrpc': '2.0', 'id': '0' @@ -146,11 +161,23 @@ class Wallet(object): } return self.rpc.send_json_rpc_request(sweep_dust) - def sweep_all(self, address): + def sweep_all(self, address = '', account_index = 0, subaddr_indices = [], priority = 0, ring_size = 0, outputs = 1, unlock_time = 0, payment_id = '', get_tx_keys = False, below_amount = 0, do_not_relay = False, get_tx_hex = False, get_tx_metadata = False): sweep_all = { 'method': 'sweep_all', 'params' : { - 'address' : '' + 'address' : address, + 'account_index' : account_index, + 'subaddr_indices' : subaddr_indices, + 'priority' : priority, + 'ring_size' : ring_size, + 'outputs' : outputs, + 'unlock_time' : unlock_time, + 'payment_id' : payment_id, + 'get_tx_keys' : get_tx_keys, + 'below_amount' : below_amount, + 'do_not_relay' : do_not_relay, + 'get_tx_hex' : get_tx_hex, + 'get_tx_metadata' : get_tx_metadata, }, 'jsonrpc': '2.0', 'id': '0' @@ -591,6 +618,79 @@ class Wallet(object): } return self.rpc.send_json_rpc_request(verify) + def get_height(self): + get_height = { + 'method': 'get_height', + 'jsonrpc': '2.0', + 'id': '0' + } + return self.rpc.send_json_rpc_request(get_height) + + def relay_tx(self, hex_): + relay_tx = { + 'method': 'relay_tx', + 'params': { + 'hex': hex_, + }, + 'jsonrpc': '2.0', + 'id': '0' + } + return self.rpc.send_json_rpc_request(relay_tx) + + def get_languages(self): + get_languages = { + 'method': 'get_languages', + 'params': { + }, + 'jsonrpc': '2.0', + 'id': '0' + } + return self.rpc.send_json_rpc_request(get_languages) + + def export_outputs(self): + export_outputs = { + 'method': 'export_outputs', + 'params': { + }, + 'jsonrpc': '2.0', + 'id': '0' + } + return self.rpc.send_json_rpc_request(export_outputs) + + def import_outputs(self, outputs_data_hex): + import_outputs = { + 'method': 'import_outputs', + 'params': { + 'outputs_data_hex': outputs_data_hex + }, + 'jsonrpc': '2.0', + 'id': '0' + } + return self.rpc.send_json_rpc_request(import_outputs) + + def export_key_images(self, all_ = False): + export_key_images = { + 'method': 'export_key_images', + 'params': { + 'all': all_ + }, + 'jsonrpc': '2.0', + 'id': '0' + } + return self.rpc.send_json_rpc_request(export_key_images) + + def import_key_images(self, signed_key_images, offset = 0): + import_key_images = { + 'method': 'import_key_images', + 'params': { + 'offset': offset, + 'signed_key_images': signed_key_images, + }, + 'jsonrpc': '2.0', + 'id': '0' + } + return self.rpc.send_json_rpc_request(import_key_images) + def get_version(self): get_version = { 'method': 'get_version', |