convenience methods on ledger

This commit is contained in:
Lex Berezhny 2020-05-06 10:50:00 -04:00
parent 2a0c653c37
commit d476f08d13

View file

@ -6,6 +6,7 @@ from lbry.conf import Config
from lbry.schema.url import URL from lbry.schema.url import URL
from .header import Headers, UnvalidatedHeaders from .header import Headers, UnvalidatedHeaders
from .checkpoints import HASHES from .checkpoints import HASHES
from .dewies import lbc_to_dewies
class Ledger: class Ledger:
@ -60,13 +61,11 @@ class Ledger:
except: except:
raise Exception(f"'{address}' is not a valid address") raise Exception(f"'{address}' is not a valid address")
@classmethod @staticmethod
def valid_channel_name_or_error(cls, name): def valid_channel_name_or_error(name: str):
try: try:
if not name: if not name:
raise Exception( raise Exception("Channel name cannot be blank.")
"Channel name cannot be blank."
)
parsed = URL.parse(name) parsed = URL.parse(name)
if not parsed.has_channel: if not parsed.has_channel:
raise Exception("Channel names must start with '@' symbol.") raise Exception("Channel names must start with '@' symbol.")
@ -75,6 +74,53 @@ class Ledger:
except (TypeError, ValueError): except (TypeError, ValueError):
raise Exception("Invalid channel name.") raise Exception("Invalid channel name.")
@staticmethod
def valid_stream_name_or_error(name: str):
try:
if not name:
raise Exception('Stream name cannot be blank.')
parsed = URL.parse(name)
if parsed.has_channel:
raise Exception(
"Stream names cannot start with '@' symbol. This is reserved for channels claims."
)
if not parsed.has_stream or parsed.stream.name != name:
raise Exception('Stream name has invalid characters.')
except (TypeError, ValueError):
raise Exception("Invalid stream name.")
@staticmethod
def valid_collection_name_or_error(name: str):
try:
if not name:
raise Exception('Collection name cannot be blank.')
parsed = URL.parse(name)
if parsed.has_channel:
raise Exception(
"Collection names cannot start with '@' symbol. This is reserved for channels claims."
)
if not parsed.has_stream or parsed.stream.name != name:
raise Exception('Collection name has invalid characters.')
except (TypeError, ValueError):
raise Exception("Invalid collection name.")
@staticmethod
def get_dewies_or_error(argument: str, lbc: str, positive_value=False):
try:
dewies = lbc_to_dewies(lbc)
if positive_value and dewies <= 0:
raise ValueError(f"'{argument}' value must be greater than 0.0")
return dewies
except ValueError as e:
raise ValueError(f"Invalid value for '{argument}': {e.args[0]}")
def get_fee_address(self, kwargs: dict, claim_address: str) -> str:
if 'fee_address' in kwargs:
self.valid_address_or_error(kwargs['fee_address'])
return kwargs['fee_address']
if 'fee_currency' in kwargs or 'fee_amount' in kwargs:
return claim_address
@classmethod @classmethod
def public_key_to_address(cls, public_key): def public_key_to_address(cls, public_key):
return cls.hash160_to_address(hash160(public_key)) return cls.hash160_to_address(hash160(public_key))