Handle utxo.get_address

This commit is contained in:
Neil Booth 2016-11-03 10:46:09 +09:00
parent a5b73560f3
commit be2475f617
4 changed files with 32 additions and 6 deletions

View File

@ -77,6 +77,11 @@ class Coin(object):
raise CoinError('invalid address: {}'.format(addr)) raise CoinError('invalid address: {}'.format(addr))
return result return result
@classmethod
def hash168_to_address(cls, hash168):
'''Return an address given a 21-byte hash.'''
return Base58.encode_check(hash168)
@classmethod @classmethod
def P2PKH_address_from_hash160(cls, hash_bytes): def P2PKH_address_from_hash160(cls, hash_bytes):
'''Return a P2PKH address given a public key.''' '''Return a P2PKH address given a public key.'''

View File

@ -762,3 +762,12 @@ class BlockProcessor(LoggedClass):
'''Returns all the UTXOs for an address sorted by height and '''Returns all the UTXOs for an address sorted by height and
position in the block.''' position in the block.'''
return sorted(self.get_utxos(hash168, limit=None)) return sorted(self.get_utxos(hash168, limit=None))
def get_utxo_hash168(self, tx_hash, index):
'''Returns the hash168 for a UTXO.'''
hash168 = None
if 0 <= index <= 65535:
hash168 = self.utxo_cache(tx_hash, struct.pack('<H', index))
if hash168 == NO_CACHE_ENTRY:
hash168 = None
return hash168

View File

@ -17,11 +17,10 @@ import ssl
import traceback import traceback
from functools import partial from functools import partial
from server.daemon import Daemon, DaemonError from server.daemon import Daemon
from server.block_processor import BlockProcessor from server.block_processor import BlockProcessor
from server.protocol import ElectrumX, LocalRPC, RPCError, JSONRPC from server.protocol import ElectrumX, LocalRPC, JSONRPC
from lib.hash import (sha256, double_sha256, hash_to_str, from lib.hash import sha256, double_sha256, hash_to_str, hex_str_to_hash
Base58, hex_str_to_hash)
from lib.util import LoggedClass from lib.util import LoggedClass

View File

@ -16,6 +16,7 @@ import traceback
from functools import partial from functools import partial
from server.daemon import DaemonError from server.daemon import DaemonError
from lib.hash import hex_str_to_hash
from lib.util import LoggedClass from lib.util import LoggedClass
from server.version import VERSION from server.version import VERSION
@ -201,6 +202,8 @@ class JSONRPC(asyncio.Protocol, LoggedClass):
'blockchain.numblocks.subscribe', 'blockchain.numblocks.subscribe',
(height, ), (height, ),
) )
hash168_to_address = cls.COIN.hash168_to_address
for session in cls.SESSIONS: for session in cls.SESSIONS:
if height != session.notified_height: if height != session.notified_height:
session.notified_height = height session.notified_height = height
@ -210,8 +213,9 @@ class JSONRPC(asyncio.Protocol, LoggedClass):
session.json_send(height_payload) session.json_send(height_payload)
for hash168 in session.hash168s.intersection(touched): for hash168 in session.hash168s.intersection(touched):
address = hash168_to_address(hash168)
payload = json_notification('blockchain.address.subscribe', payload = json_notification('blockchain.address.subscribe',
(Base58.encode_check(hash168), )) (address, ))
session.json_send(payload) session.json_send(payload)
@ -347,7 +351,16 @@ class ElectrumX(JSONRPC):
raise RPCError('params should contain a transaction hash and height') raise RPCError('params should contain a transaction hash and height')
async def utxo_get_address(self, params): async def utxo_get_address(self, params):
pass # TODO 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 # --- server commands