lbry-sdk/lbry/blockchain/hash.py

57 lines
1.1 KiB
Python
Raw Normal View History

2018-05-25 08:03:25 +02:00
from binascii import hexlify, unhexlify
2020-05-01 15:28:51 +02:00
from lbry.constants import NULL_HASH32
2018-05-25 08:03:25 +02:00
class TXRef:
__slots__ = '_id', '_hash'
def __init__(self):
self._id = None
self._hash = None
@property
def id(self):
return self._id
@property
def hash(self):
return self._hash
2018-10-11 03:29:29 +02:00
@property
def height(self):
return -1
@property
def is_null(self):
return self.hash == NULL_HASH32
class TXRefImmutable(TXRef):
2018-10-11 03:39:36 +02:00
__slots__ = ('_height',)
def __init__(self):
super().__init__()
self._height = -1
@classmethod
2018-10-11 03:29:29 +02:00
def from_hash(cls, tx_hash: bytes, height: int) -> 'TXRefImmutable':
ref = cls()
ref._hash = tx_hash
ref._id = hexlify(tx_hash[::-1]).decode()
2018-10-11 03:29:29 +02:00
ref._height = height
return ref
@classmethod
2018-10-11 03:29:29 +02:00
def from_id(cls, tx_id: str, height: int) -> 'TXRefImmutable':
ref = cls()
ref._id = tx_id
ref._hash = unhexlify(tx_id)[::-1]
2018-10-11 03:29:29 +02:00
ref._height = height
return ref
2018-10-11 03:29:29 +02:00
@property
def height(self):
return self._height