|
|
@ -1,164 +1,449 @@ |
|
|
|
# See the file "LICENSE" for information about the copyright |
|
|
|
# Copyright (c) 2016, Neil Booth |
|
|
|
# |
|
|
|
# All rights reserved. |
|
|
|
# |
|
|
|
# See the file "LICENCE" for information about the copyright |
|
|
|
# and warranty status of this software. |
|
|
|
|
|
|
|
'''Classes for local RPC server and remote client TCP/SSL servers.''' |
|
|
|
|
|
|
|
|
|
|
|
import asyncio |
|
|
|
import codecs |
|
|
|
import json |
|
|
|
import struct |
|
|
|
import traceback |
|
|
|
from functools import partial |
|
|
|
|
|
|
|
from server.daemon import DaemonError |
|
|
|
from lib.hash import sha256, double_sha256, hash_to_str, hex_str_to_hash |
|
|
|
from lib.util import LoggedClass |
|
|
|
from server.version import VERSION |
|
|
|
|
|
|
|
|
|
|
|
class Error(Exception): |
|
|
|
BAD_REQUEST = 1 |
|
|
|
INTERNAL_ERROR = 2 |
|
|
|
class RPCError(Exception): |
|
|
|
'''RPC handlers raise this error.''' |
|
|
|
|
|
|
|
|
|
|
|
def json_notification(method, params): |
|
|
|
'''Create a json notification.''' |
|
|
|
return {'id': None, 'method': method, 'params': params} |
|
|
|
|
|
|
|
|
|
|
|
class JSONRPC(asyncio.Protocol, LoggedClass): |
|
|
|
'''Base class that manages a JSONRPC connection.''' |
|
|
|
SESSIONS = set() |
|
|
|
|
|
|
|
def __init__(self, controller): |
|
|
|
def __init__(self): |
|
|
|
super().__init__() |
|
|
|
self.controller = controller |
|
|
|
self.parts = [] |
|
|
|
self.send_count = 0 |
|
|
|
self.send_size = 0 |
|
|
|
self.error_count = 0 |
|
|
|
|
|
|
|
def connection_made(self, transport): |
|
|
|
'''Handle an incoming client connection.''' |
|
|
|
self.transport = transport |
|
|
|
peername = transport.get_extra_info('peername') |
|
|
|
self.logger.info('connection from {}'.format(peername)) |
|
|
|
self.controller.add_session(self) |
|
|
|
self.peername = transport.get_extra_info('peername') |
|
|
|
self.logger.info('connection from {}'.format(self.peername)) |
|
|
|
self.SESSIONS.add(self) |
|
|
|
|
|
|
|
def connection_lost(self, exc): |
|
|
|
self.logger.info('disconnected') |
|
|
|
self.controller.remove_session(self) |
|
|
|
'''Handle client disconnection.''' |
|
|
|
self.logger.info('{} disconnected. ' |
|
|
|
'Sent {:,d} bytes in {:,d} messages {:,d} errors' |
|
|
|
.format(self.peername, self.send_size, |
|
|
|
self.send_count, self.error_count)) |
|
|
|
self.SESSIONS.remove(self) |
|
|
|
|
|
|
|
def data_received(self, data): |
|
|
|
'''Handle incoming data (synchronously). |
|
|
|
|
|
|
|
Requests end in newline characters. Pass complete requests to |
|
|
|
decode_message for handling. |
|
|
|
''' |
|
|
|
while True: |
|
|
|
npos = data.find(ord('\n')) |
|
|
|
if npos == -1: |
|
|
|
self.parts.append(data) |
|
|
|
break |
|
|
|
tail, data = data[:npos], data[npos + 1:] |
|
|
|
parts = self.parts |
|
|
|
self.parts = [] |
|
|
|
parts, self.parts = self.parts, [] |
|
|
|
parts.append(tail) |
|
|
|
self.decode_message(b''.join(parts)) |
|
|
|
|
|
|
|
if data: |
|
|
|
self.parts.append(data) |
|
|
|
|
|
|
|
def decode_message(self, message): |
|
|
|
'''Message is a binary message.''' |
|
|
|
'''Decode a binary message and queue it for asynchronous handling.''' |
|
|
|
try: |
|
|
|
message = json.loads(message.decode()) |
|
|
|
except Exception as e: |
|
|
|
self.logger.info('caught exception decoding message'.format(e)) |
|
|
|
return |
|
|
|
|
|
|
|
job = self.request_handler(message) |
|
|
|
self.controller.add_job(job) |
|
|
|
self.logger.info('error decoding JSON message'.format(e)) |
|
|
|
else: |
|
|
|
self.ADD_JOB(self.request_handler(message)) |
|
|
|
|
|
|
|
async def request_handler(self, request): |
|
|
|
'''Called asynchronously.''' |
|
|
|
error = result = None |
|
|
|
try: |
|
|
|
result = await self.json_handler(request) |
|
|
|
except Error as e: |
|
|
|
error = {'code': e.args[0], 'message': e.args[1]} |
|
|
|
except asyncio.CancelledError: |
|
|
|
raise |
|
|
|
except Exception as e: |
|
|
|
# This should be considered a bug and fixed |
|
|
|
traceback.print_exc() |
|
|
|
error = {'code': Error.INTERNAL_ERROR, 'message': str(e)} |
|
|
|
|
|
|
|
handler = self.rpc_handler(request.get('method'), |
|
|
|
request.get('params', [])) |
|
|
|
result = await handler() |
|
|
|
except RPCError as e: |
|
|
|
self.error_count += 1 |
|
|
|
error = {'code': 1, 'message': e.args[0]} |
|
|
|
payload = {'id': request.get('id'), 'error': error, 'result': result} |
|
|
|
try: |
|
|
|
data = json.dumps(payload) + '\n' |
|
|
|
except TypeError: |
|
|
|
msg = 'cannot JSON encode response to request {}'.format(request) |
|
|
|
self.logger.error(msg) |
|
|
|
error = {'code': Error.INTERNAL_ERROR, 'message': msg} |
|
|
|
payload = {'id': request.get('id'), 'error': error, 'result': None} |
|
|
|
data = json.dumps(payload) + '\n' |
|
|
|
self.transport.write(data.encode()) |
|
|
|
|
|
|
|
async def json_handler(self, request): |
|
|
|
method = request.get('method') |
|
|
|
self.json_send(payload) |
|
|
|
|
|
|
|
def json_send(self, payload): |
|
|
|
data = (json.dumps(payload) + '\n').encode() |
|
|
|
self.transport.write(data) |
|
|
|
self.send_count += 1 |
|
|
|
self.send_size += len(data) |
|
|
|
|
|
|
|
def rpc_handler(self, method, params): |
|
|
|
handler = None |
|
|
|
if isinstance(method, str): |
|
|
|
handler_name = 'handle_{}'.format(method.replace('.', '_')) |
|
|
|
handler = getattr(self, handler_name, None) |
|
|
|
handler = self.handlers.get(method) |
|
|
|
if not handler: |
|
|
|
self.logger.info('unknown method: {}'.format(method)) |
|
|
|
raise Error(Error.BAD_REQUEST, 'unknown method: {}'.format(method)) |
|
|
|
params = request.get('params', []) |
|
|
|
raise RPCError('unknown method: {}'.format(method)) |
|
|
|
|
|
|
|
if not isinstance(params, list): |
|
|
|
raise Error(Error.BAD_REQUEST, 'params should be an array') |
|
|
|
return await handler(params) |
|
|
|
raise RPCError('params should be an array') |
|
|
|
|
|
|
|
return partial(handler, self, params) |
|
|
|
|
|
|
|
@classmethod |
|
|
|
def tx_hash_from_param(cls, param): |
|
|
|
'''Raise an RPCError if the parameter is not a valid transaction |
|
|
|
hash.''' |
|
|
|
if isinstance(param, str) and len(param) == 64: |
|
|
|
try: |
|
|
|
bytes.fromhex(param) |
|
|
|
return param |
|
|
|
except ValueError: |
|
|
|
pass |
|
|
|
raise RPCError('parameter should be a transaction hash: {}' |
|
|
|
.format(param)) |
|
|
|
|
|
|
|
@classmethod |
|
|
|
def hash168_from_param(cls, param): |
|
|
|
if isinstance(param, str): |
|
|
|
try: |
|
|
|
return cls.COIN.address_to_hash168(param) |
|
|
|
except: |
|
|
|
pass |
|
|
|
raise RPCError('parameter should be a valid address: {}'.format(param)) |
|
|
|
|
|
|
|
@classmethod |
|
|
|
def non_negative_integer_from_param(cls, param): |
|
|
|
try: |
|
|
|
param = int(param) |
|
|
|
except ValueError: |
|
|
|
pass |
|
|
|
else: |
|
|
|
if param >= 0: |
|
|
|
return param |
|
|
|
|
|
|
|
raise RPCError('param should be a non-negative integer: {}' |
|
|
|
.format(param)) |
|
|
|
|
|
|
|
@classmethod |
|
|
|
def extract_hash168(cls, params): |
|
|
|
if len(params) == 1: |
|
|
|
return cls.hash168_from_param(params[0]) |
|
|
|
raise RPCError('params should contain a single address: {}' |
|
|
|
.format(params)) |
|
|
|
|
|
|
|
@classmethod |
|
|
|
def extract_non_negative_integer(cls, params): |
|
|
|
if len(params) == 1: |
|
|
|
return cls.non_negative_integer_from_param(params[0]) |
|
|
|
raise RPCError('params should contain a non-negative integer: {}' |
|
|
|
.format(params)) |
|
|
|
|
|
|
|
@classmethod |
|
|
|
def require_empty_params(cls, params): |
|
|
|
if params: |
|
|
|
raise RPCError('params should be empty: {}'.format(params)) |
|
|
|
|
|
|
|
@classmethod |
|
|
|
def init(cls, block_processor, daemon, coin, add_job): |
|
|
|
cls.BLOCK_PROCESSOR = block_processor |
|
|
|
cls.DAEMON = daemon |
|
|
|
cls.COIN = coin |
|
|
|
cls.ADD_JOB = add_job |
|
|
|
|
|
|
|
@classmethod |
|
|
|
def height(cls): |
|
|
|
'''Return the current height.''' |
|
|
|
return cls.BLOCK_PROCESSOR.height |
|
|
|
|
|
|
|
@classmethod |
|
|
|
def electrum_header(cls, height=None): |
|
|
|
'''Return the binary header at the given height.''' |
|
|
|
if not 0 <= height <= cls.height(): |
|
|
|
raise RPCError('height {:,d} out of range'.format(height)) |
|
|
|
header = cls.BLOCK_PROCESSOR.read_headers(height, 1) |
|
|
|
return cls.COIN.electrum_header(header, height) |
|
|
|
|
|
|
|
@classmethod |
|
|
|
def current_electrum_header(cls): |
|
|
|
'''Used as response to a headers subscription request.''' |
|
|
|
return cls.electrum_header(cls.height()) |
|
|
|
|
|
|
|
|
|
|
|
class ElectrumX(JSONRPC): |
|
|
|
'''A TCP server that handles incoming Electrum connections.''' |
|
|
|
|
|
|
|
def __init__(self, controller, daemon, env): |
|
|
|
super().__init__(controller) |
|
|
|
self.daemon = daemon |
|
|
|
def __init__(self, env): |
|
|
|
super().__init__() |
|
|
|
self.env = env |
|
|
|
self.addresses = set() |
|
|
|
self.hash168s = set() |
|
|
|
self.subscribe_headers = False |
|
|
|
self.subscribe_height = False |
|
|
|
self.notified_height = None |
|
|
|
rpcs = [ |
|
|
|
('blockchain', |
|
|
|
'address.get_balance address.get_history address.get_mempool ' |
|
|
|
'address.get_proof address.listunspent address.subscribe ' |
|
|
|
'block.get_header block.get_chunk estimatefee headers.subscribe ' |
|
|
|
'numblocks.subscribe relayfee transaction.broadcast ' |
|
|
|
'transaction.get transaction.get_merkle utxo.get_address'), |
|
|
|
('server', |
|
|
|
'banner donation_address peers.subscribe version'), |
|
|
|
] |
|
|
|
self.handlers = {'.'.join([prefix, suffix]): |
|
|
|
getattr(self.__class__, suffix.replace('.', '_')) |
|
|
|
for prefix, suffixes in rpcs |
|
|
|
for suffix in suffixes.split()} |
|
|
|
|
|
|
|
@classmethod |
|
|
|
def watched_address_count(cls): |
|
|
|
return sum(len(session.hash168s) for session in self.SESSIONS |
|
|
|
if isinstance(session, cls)) |
|
|
|
|
|
|
|
@classmethod |
|
|
|
def notify(cls, height, touched): |
|
|
|
'''Notify electrum clients about height changes and touched |
|
|
|
addresses.''' |
|
|
|
headers_payload = json_notification( |
|
|
|
'blockchain.headers.subscribe', |
|
|
|
(cls.electrum_header(height), ), |
|
|
|
) |
|
|
|
height_payload = json_notification( |
|
|
|
'blockchain.numblocks.subscribe', |
|
|
|
(height, ), |
|
|
|
) |
|
|
|
hash168_to_address = cls.COIN.hash168_to_address |
|
|
|
|
|
|
|
for session in cls.SESSIONS: |
|
|
|
if height != session.notified_height: |
|
|
|
session.notified_height = height |
|
|
|
if session.subscribe_headers: |
|
|
|
session.json_send(headers_payload) |
|
|
|
if session.subscribe_height: |
|
|
|
session.json_send(height_payload) |
|
|
|
|
|
|
|
for hash168 in session.hash168s.intersection(touched): |
|
|
|
address = hash168_to_address(hash168) |
|
|
|
status = cls.address_status(hash168) |
|
|
|
payload = json_notification('blockchain.address.subscribe', |
|
|
|
(address, status)) |
|
|
|
session.json_send(payload) |
|
|
|
|
|
|
|
@classmethod |
|
|
|
def address_status(cls, hash168): |
|
|
|
'''Returns status as 32 bytes.''' |
|
|
|
history = cls.BLOCK_PROCESSOR.get_history(hash168) |
|
|
|
status = ''.join('{}:{:d}:'.format(hash_to_str(tx_hash), height) |
|
|
|
for tx_hash, height in history) |
|
|
|
if status: |
|
|
|
return sha256(status.encode()).hex() |
|
|
|
return None |
|
|
|
|
|
|
|
@classmethod |
|
|
|
async def tx_merkle(cls, tx_hash, height): |
|
|
|
'''tx_hash is a hex string.''' |
|
|
|
block_hash = await cls.DAEMON.send_single('getblockhash', (height,)) |
|
|
|
block = await cls.DAEMON.send_single('getblock', (block_hash, True)) |
|
|
|
tx_hashes = block['tx'] |
|
|
|
# This will throw if the tx_hash is bad |
|
|
|
pos = tx_hashes.index(tx_hash) |
|
|
|
|
|
|
|
idx = pos |
|
|
|
hashes = [hex_str_to_hash(txh) for txh in tx_hashes] |
|
|
|
merkle_branch = [] |
|
|
|
while len(hashes) > 1: |
|
|
|
if len(hashes) & 1: |
|
|
|
hashes.append(hashes[-1]) |
|
|
|
idx = idx - 1 if (idx & 1) else idx + 1 |
|
|
|
merkle_branch.append(hash_to_str(hashes[idx])) |
|
|
|
idx //= 2 |
|
|
|
hashes = [double_sha256(hashes[n] + hashes[n + 1]) |
|
|
|
for n in range(0, len(hashes), 2)] |
|
|
|
|
|
|
|
return {"block_height": height, "merkle": merkle_branch, "pos": pos} |
|
|
|
|
|
|
|
@classmethod |
|
|
|
def irc_peers(cls): |
|
|
|
'''Returns a dictionary of IRC nick to (ip, host, ports) tuples, one |
|
|
|
per peer.''' |
|
|
|
return {} |
|
|
|
|
|
|
|
@classmethod |
|
|
|
def height(cls): |
|
|
|
return cls.BLOCK_PROCESSOR.height |
|
|
|
|
|
|
|
@classmethod |
|
|
|
def get_history(cls, hash168): |
|
|
|
history = cls.BLOCK_PROCESSOR.get_history(hash168, limit=None) |
|
|
|
return [ |
|
|
|
{'tx_hash': hash_to_str(tx_hash), 'height': height} |
|
|
|
for tx_hash, height in history |
|
|
|
] |
|
|
|
|
|
|
|
@classmethod |
|
|
|
def get_chunk(cls, index): |
|
|
|
'''Return header chunk as hex. Index is a non-negative integer.''' |
|
|
|
chunk_size = cls.COIN.CHUNK_SIZE |
|
|
|
next_height = cls.height() + 1 |
|
|
|
start_height = min(index * chunk_size, next_height) |
|
|
|
count = min(next_height - start_height, chunk_size) |
|
|
|
return cls.BLOCK_PROCESSOR.read_headers(start_height, count).hex() |
|
|
|
|
|
|
|
@classmethod |
|
|
|
def get_balance(cls, hash168): |
|
|
|
confirmed = cls.BLOCK_PROCESSOR.get_balance(hash168) |
|
|
|
unconfirmed = -1 # FIXME |
|
|
|
return {'confirmed': confirmed, 'unconfirmed': unconfirmed} |
|
|
|
|
|
|
|
@classmethod |
|
|
|
def list_unspent(cls, hash168): |
|
|
|
utxos = cls.BLOCK_PROCESSOR.get_utxos_sorted(hash168) |
|
|
|
return tuple({'tx_hash': hash_to_str(utxo.tx_hash), |
|
|
|
'tx_pos': utxo.tx_pos, 'height': utxo.height, |
|
|
|
'value': utxo.value} |
|
|
|
for utxo in utxos) |
|
|
|
|
|
|
|
# --- blockchain commands |
|
|
|
|
|
|
|
async def address_get_balance(self, params): |
|
|
|
hash168 = self.extract_hash168(params) |
|
|
|
return self.get_balance(hash168) |
|
|
|
|
|
|
|
async def address_get_history(self, params): |
|
|
|
hash168 = self.extract_hash168(params) |
|
|
|
return self.get_history(hash168) |
|
|
|
|
|
|
|
async def address_get_mempool(self, params): |
|
|
|
hash168 = self.extract_hash168(params) |
|
|
|
raise RPCError('get_mempool is not yet implemented') |
|
|
|
|
|
|
|
async def address_get_proof(self, params): |
|
|
|
hash168 = self.extract_hash168(params) |
|
|
|
raise RPCError('get_proof is not yet implemented') |
|
|
|
|
|
|
|
async def address_listunspent(self, params): |
|
|
|
hash168 = self.extract_hash168(params) |
|
|
|
return self.list_unspent(hash168) |
|
|
|
|
|
|
|
async def address_subscribe(self, params): |
|
|
|
hash168 = self.extract_hash168(params) |
|
|
|
self.hash168s.add(hash168) |
|
|
|
return self.address_status(hash168) |
|
|
|
|
|
|
|
async def block_get_chunk(self, params): |
|
|
|
index = self.extract_non_negative_integer(params) |
|
|
|
return self.get_chunk(index) |
|
|
|
|
|
|
|
async def block_get_header(self, params): |
|
|
|
height = self.extract_non_negative_integer(params) |
|
|
|
return self.electrum_header(height) |
|
|
|
|
|
|
|
async def estimatefee(self, params): |
|
|
|
return await self.DAEMON.estimatefee(params) |
|
|
|
|
|
|
|
async def headers_subscribe(self, params): |
|
|
|
self.require_empty_params(params) |
|
|
|
self.subscribe_headers = True |
|
|
|
return self.current_electrum_header() |
|
|
|
|
|
|
|
def params_to_hash168(self, params): |
|
|
|
if len(params) != 1: |
|
|
|
raise Error(Error.BAD_REQUEST, |
|
|
|
'params should contain a single address') |
|
|
|
address = params[0] |
|
|
|
try: |
|
|
|
return self.env.coin.address_to_hash168(address) |
|
|
|
except: |
|
|
|
raise Error(Error.BAD_REQUEST, |
|
|
|
'invalid address: {}'.format(address)) |
|
|
|
|
|
|
|
async def handle_blockchain_address_get_history(self, params): |
|
|
|
hash168 = self.params_to_hash168(params) |
|
|
|
return self.controller.get_history(hash168) |
|
|
|
|
|
|
|
async def handle_blockchain_address_subscribe(self, params): |
|
|
|
hash168 = self.params_to_hash168(params) |
|
|
|
status = self.controller.address_status(hash168) |
|
|
|
return status.hex() if status else None |
|
|
|
async def numblocks_subscribe(self, params): |
|
|
|
self.require_empty_params(params) |
|
|
|
self.subscribe_height = True |
|
|
|
return self.height() |
|
|
|
|
|
|
|
async def handle_blockchain_estimatefee(self, params): |
|
|
|
result = await self.daemon.send_single('estimatefee', params) |
|
|
|
return result |
|
|
|
async def relayfee(self, params): |
|
|
|
'''The minimum fee a low-priority tx must pay in order to be accepted |
|
|
|
to the daemon's memory pool.''' |
|
|
|
self.require_empty_params(params) |
|
|
|
return await self.DAEMON.relayfee() |
|
|
|
|
|
|
|
async def handle_blockchain_headers_subscribe(self, params): |
|
|
|
self.subscribe_headers = True |
|
|
|
return self.controller.get_current_header() |
|
|
|
async def transaction_broadcast(self, params): |
|
|
|
'''Pass through the parameters to the daemon. |
|
|
|
|
|
|
|
async def handle_blockchain_relayfee(self, params): |
|
|
|
'''The minimum fee a low-priority tx must pay in order to be accepted |
|
|
|
to this daemon's memory pool. |
|
|
|
An ugly API: current Electrum clients only pass the raw |
|
|
|
transaction in hex and expect error messages to be returned in |
|
|
|
the result field. And the server shouldn't be doing the client's |
|
|
|
user interface job here. |
|
|
|
''' |
|
|
|
net_info = await self.daemon.send_single('getnetworkinfo') |
|
|
|
return net_info['relayfee'] |
|
|
|
|
|
|
|
async def handle_blockchain_transaction_get(self, params): |
|
|
|
if len(params) != 1: |
|
|
|
raise Error(Error.BAD_REQUEST, |
|
|
|
'params should contain a transaction hash') |
|
|
|
tx_hash = params[0] |
|
|
|
return await self.daemon.send_single('getrawtransaction', (tx_hash, 0)) |
|
|
|
|
|
|
|
async def handle_blockchain_transaction_get_merkle(self, params): |
|
|
|
if len(params) != 2: |
|
|
|
raise Error(Error.BAD_REQUEST, |
|
|
|
'params should contain a transaction hash and height') |
|
|
|
tx_hash, height = params |
|
|
|
return await self.controller.get_merkle(tx_hash, height) |
|
|
|
|
|
|
|
async def handle_server_banner(self, params): |
|
|
|
try: |
|
|
|
tx_hash = await self.DAEMON.sendrawtransaction(params) |
|
|
|
self.logger.info('sent tx: {}'.format(tx_hash)) |
|
|
|
return tx_hash |
|
|
|
except DaemonError as e: |
|
|
|
errors = e.args[0] |
|
|
|
error = errors[0] |
|
|
|
message = error['message'] |
|
|
|
self.logger.info('sendrawtransaction: {}'.format(message)) |
|
|
|
if 'non-mandatory-script-verify-flag' in message: |
|
|
|
return ( |
|
|
|
'Your client produced a transaction that is not accepted ' |
|
|
|
'by the network any more. Please upgrade to Electrum ' |
|
|
|
'2.5.1 or newer.' |
|
|
|
) |
|
|
|
|
|
|
|
return ( |
|
|
|
'The transaction was rejected by network rules. ({})\n[{}]' |
|
|
|
.format(message, params[0]) |
|
|
|
) |
|
|
|
|
|
|
|
async def transaction_get(self, params): |
|
|
|
'''Return the serialized raw transaction.''' |
|
|
|
# For some reason Electrum passes a height. Don't require it |
|
|
|
# in anticipation it might be dropped in the future. |
|
|
|
if 1 <= len(params) <= 2: |
|
|
|
tx_hash = self.tx_hash_from_param(params[0]) |
|
|
|
return await self.DAEMON.getrawtransaction(tx_hash) |
|
|
|
|
|
|
|
raise RPCError('params wrong length: {}'.format(params)) |
|
|
|
|
|
|
|
async def transaction_get_merkle(self, params): |
|
|
|
if len(params) == 2: |
|
|
|
tx_hash = self.tx_hash_from_param(params[0]) |
|
|
|
height = self.non_negative_integer_from_param(params[1]) |
|
|
|
return await self.tx_merkle(tx_hash, height) |
|
|
|
|
|
|
|
raise RPCError('params should contain a transaction hash and height') |
|
|
|
|
|
|
|
async def utxo_get_address(self, params): |
|
|
|
if len(params) == 2: |
|
|
|
tx_hash = self.tx_hash_from_param(params[0]) |
|
|
|
index = self.non_negative_integer_from_param(params[1]) |
|
|
|
tx_hash = hex_str_to_hash(tx_hash) |
|
|
|
hash168 = self.BLOCK_PROCESSOR.get_utxo_hash168(tx_hash, index) |
|
|
|
if hash168: |
|
|
|
return self.COIN.hash168_to_address(hash168) |
|
|
|
return None |
|
|
|
|
|
|
|
raise RPCError('params should contain a transaction hash and index') |
|
|
|
|
|
|
|
# --- server commands |
|
|
|
|
|
|
|
async def banner(self, params): |
|
|
|
'''Return the server banner.''' |
|
|
|
self.require_empty_params(params) |
|
|
|
banner = 'Welcome to Electrum!' |
|
|
|
if self.env.banner_file: |
|
|
|
try: |
|
|
@ -169,23 +454,25 @@ class ElectrumX(JSONRPC): |
|
|
|
.format(self.env.banner_file, e)) |
|
|
|
return banner |
|
|
|
|
|
|
|
async def handle_server_donation_address(self, params): |
|
|
|
async def donation_address(self, params): |
|
|
|
'''Return the donation address as a string. |
|
|
|
|
|
|
|
If none is specified return the empty string. |
|
|
|
''' |
|
|
|
self.require_empty_params(params) |
|
|
|
return self.env.donation_address |
|
|
|
|
|
|
|
async def handle_server_peers_subscribe(self, params): |
|
|
|
async def peers_subscribe(self, params): |
|
|
|
'''Returns the peer (ip, host, ports) tuples. |
|
|
|
|
|
|
|
Despite the name electrum-server does not treat this as a |
|
|
|
subscription. |
|
|
|
''' |
|
|
|
peers = self.controller.get_peers() |
|
|
|
self.require_empty_params(params) |
|
|
|
peers = ElectrumX.irc_peers() |
|
|
|
return tuple(peers.values()) |
|
|
|
|
|
|
|
async def handle_server_version(self, params): |
|
|
|
async def version(self, params): |
|
|
|
'''Return the server version as a string.''' |
|
|
|
return VERSION |
|
|
|
|
|
|
@ -193,24 +480,28 @@ class ElectrumX(JSONRPC): |
|
|
|
class LocalRPC(JSONRPC): |
|
|
|
'''A local TCP RPC server for querying status.''' |
|
|
|
|
|
|
|
async def handle_getinfo(self, params): |
|
|
|
def __init__(self): |
|
|
|
super().__init__() |
|
|
|
cmds = 'getinfo sessions numsessions peers numpeers'.split() |
|
|
|
self.handlers = {cmd: getattr(self.__class__, cmd) for cmd in cmds} |
|
|
|
|
|
|
|
async def getinfo(self, params): |
|
|
|
return { |
|
|
|
'blocks': self.controller.height(), |
|
|
|
'peers': len(self.controller.get_peers()), |
|
|
|
'sessions': len(self.controller.sessions), |
|
|
|
'watched': sum(len(s.addresses) for s in self.controller.sessions |
|
|
|
if isinstance(s, ElectrumX)), |
|
|
|
'blocks': self.height(), |
|
|
|
'peers': len(ElectrumX.irc_peers()), |
|
|
|
'sessions': len(self.SESSIONS), |
|
|
|
'watched': ElectrumX.watched_address_count(), |
|
|
|
'cached': 0, |
|
|
|
} |
|
|
|
|
|
|
|
async def handle_sessions(self, params): |
|
|
|
async def sessions(self, params): |
|
|
|
return [] |
|
|
|
|
|
|
|
async def handle_numsessions(self, params): |
|
|
|
return len(self.controller.sessions) |
|
|
|
async def numsessions(self, params): |
|
|
|
return len(self.SESSIONS) |
|
|
|
|
|
|
|
async def handle_peers(self, params): |
|
|
|
return tuple(self.controller.get_peers().keys()) |
|
|
|
async def peers(self, params): |
|
|
|
return tuple(ElectrumX.irc_peers().keys()) |
|
|
|
|
|
|
|
async def handle_numpeers(self, params): |
|
|
|
return len(self.controller.get_peers()) |
|
|
|
async def numpeers(self, params): |
|
|
|
return len(ElectrumX.irc_peers()) |
|
|
|