Browse Source

Handle utxo.get_address

master
Neil Booth 8 years ago
parent
commit
be2475f617
  1. 5
      lib/coins.py
  2. 9
      server/block_processor.py
  3. 7
      server/controller.py
  4. 17
      server/protocol.py

5
lib/coins.py

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

9
server/block_processor.py

@ -762,3 +762,12 @@ class BlockProcessor(LoggedClass):
'''Returns all the UTXOs for an address sorted by height and
position in the block.'''
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

7
server/controller.py

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

17
server/protocol.py

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

Loading…
Cancel
Save