diff --git a/lbrynet/blob/blob_file.py b/lbrynet/blob/blob_file.py index 5f9173969..728962971 100644 --- a/lbrynet/blob/blob_file.py +++ b/lbrynet/blob/blob_file.py @@ -3,8 +3,8 @@ import logging from twisted.internet import defer, threads from twisted.web.client import FileBodyProducer from twisted.python.failure import Failure +from lbrynet.cryptoutils import get_lbry_hash_obj from lbrynet.p2p.Error import DownloadCanceledError, InvalidDataError, InvalidBlobHashError -from lbrynet.utils import is_valid_blobhash from lbrynet.blob.writer import HashBlobWriter from lbrynet.blob.reader import HashBlobReader @@ -12,6 +12,24 @@ log = logging.getLogger(__name__) MAX_BLOB_SIZE = 2 * 2 ** 20 +# digest_size is in bytes, and blob hashes are hex encoded +blobhash_length = get_lbry_hash_obj().digest_size * 2 + + +def is_valid_hashcharacter(char): + return char in "0123456789abcdef" + + +def is_valid_blobhash(blobhash): + """Checks whether the blobhash is the correct length and contains only + valid characters (0-9, a-f) + + @param blobhash: string, the blobhash to check + + @return: True/False + """ + return len(blobhash) == blobhash_length and all(is_valid_hashcharacter(l) for l in blobhash) + class BlobFile: """ diff --git a/lbrynet/extras/reflector/server/server.py b/lbrynet/extras/reflector/server/server.py index c1d5f9ac1..f51869e38 100644 --- a/lbrynet/extras/reflector/server/server.py +++ b/lbrynet/extras/reflector/server/server.py @@ -3,7 +3,7 @@ import json from twisted.python import failure from twisted.internet import error, defer from twisted.internet.protocol import Protocol, ServerFactory -from lbrynet.utils import is_valid_blobhash +from lbrynet.blob.blob_file import is_valid_blobhash from lbrynet.p2p.Error import DownloadCanceledError, InvalidBlobHashError from lbrynet.p2p.StreamDescriptor import BlobStreamDescriptorReader from lbrynet.p2p.StreamDescriptor import save_sd_info diff --git a/lbrynet/p2p/client/StandaloneBlobDownloader.py b/lbrynet/p2p/client/StandaloneBlobDownloader.py index 6715f10b3..c9434c98c 100644 --- a/lbrynet/p2p/client/StandaloneBlobDownloader.py +++ b/lbrynet/p2p/client/StandaloneBlobDownloader.py @@ -4,7 +4,8 @@ from lbrynet.p2p.client.BlobRequester import BlobRequester from lbrynet.p2p.client.ConnectionManager import ConnectionManager from lbrynet.p2p.client.DownloadManager import DownloadManager from lbrynet.p2p.Error import InvalidBlobHashError, DownloadSDTimeout -from lbrynet.utils import is_valid_blobhash, safe_start_looping_call, safe_stop_looping_call +from lbrynet.blob.blob_file import is_valid_blobhash +from lbrynet.utils import safe_start_looping_call, safe_stop_looping_call from twisted.python.failure import Failure from twisted.internet import defer from twisted.internet.task import LoopingCall diff --git a/lbrynet/utils.py b/lbrynet/utils.py index 52a33e581..114291102 100644 --- a/lbrynet/utils.py +++ b/lbrynet/utils.py @@ -16,9 +16,6 @@ from lbrynet.cryptoutils import get_lbry_hash_obj log = logging.getLogger(__name__) -# digest_size is in bytes, and blob hashes are hex encoded -blobhash_length = get_lbry_hash_obj().digest_size * 2 - # defining these time functions here allows for easier overriding in testing def now(): @@ -70,21 +67,6 @@ def generate_id(num=None): return h.digest() -def is_valid_hashcharacter(char): - return char in "0123456789abcdef" - - -def is_valid_blobhash(blobhash): - """Checks whether the blobhash is the correct length and contains only - valid characters (0-9, a-f) - - @param blobhash: string, the blobhash to check - - @return: True/False - """ - return len(blobhash) == blobhash_length and all(is_valid_hashcharacter(l) for l in blobhash) - - def version_is_greater_than(a, b): """Returns True if version a is more recent than version b""" return pkg_resources.parse_version(a) > pkg_resources.parse_version(b)