<commit_before>from binascii import unhexlify

from cryptography.exceptions import InvalidSignature

from torba.testcase import AsyncioTestCase
from torba.client.constants import CENT, NULL_HASH32

from lbrynet.wallet.ledger import MainNetLedger
from lbrynet.wallet.transaction import Transaction, Input, Output

from lbrynet.schema.claim import Claim


def get_output(amount=CENT, pubkey_hash=NULL_HASH32):
    return Transaction() \
        .add_outputs([Output.pay_pubkey_hash(amount, pubkey_hash)]) \
        .outputs[0]


def get_input():
    return Input.spend(get_output())


def get_tx():
    return Transaction().add_inputs([get_input()])


class TestSigningAndValidatingClaim(AsyncioTestCase):

    @staticmethod
    def get_channel():
        channel_txo = Output.pay_claim_name_pubkey_hash(CENT, '@foo', Claim(), b'abc')
        channel_txo.generate_channel_private_key()
        get_tx().add_outputs([channel_txo])
        return channel_txo

    @staticmethod
    def get_stream():
        stream_txo = Output.pay_claim_name_pubkey_hash(CENT, 'foo', Claim(), b'abc')
        get_tx().add_outputs([stream_txo])
        return stream_txo

    def test_successful_create_sign_and_validate(self):
        channel = self.get_channel()
        stream = self.get_stream()
        stream.sign(channel)
        self.assertTrue(stream.is_signed_by(channel))

    def test_fail_to_validate_on_wrong_channel(self):
        stream = self.get_stream()
        stream.sign(self.get_channel())
        self.assertFalse(stream.is_signed_by(self.get_channel()))

    def test_fail_to_validate_altered_claim(self):
        channel = self.get_channel()
        stream = self.get_stream()
        stream.sign(channel)
        self.assertTrue(stream.is_signed_by(channel))
        stream.claim.stream.title = 'hello'
        self.assertFalse(stream.is_signed_by(channel))

    def test_valid_private_key_for_cert(self):
        channel = self.get_channel()
        self.assertTrue(channel.is_channel_private_key(channel.private_key))

    def test_fail_to_load_wrong_private_key_for_cert(self):
        channel = self.get_channel()
        self.assertFalse(channel.is_channel_private_key(self.get_channel().private_key))


class TestValidatingOldSignatures(AsyncioTestCase):

    def test_signed_claim_made_by_ytsync(self):
        stream_tx = Transaction(unhexlify(
            b'0100000001eb2a756e15bde95db3d2ae4a6e9b2796a699087890644607b5b04a5f15b67062010000006a4'
            b'7304402206444b920bd318a07d9b982e30eb66245fdaaa6c9866e1f6e5900161d9b0ffd70022036464714'
            b'4f1830898a2042aa0d6cef95a243799cc6e36630a58d411e2f9111f00121029b15f9a00a7c3f21b10bd4b'
            b'98ab23a9e895bd9160e21f71317862bf55fbbc89effffffff0240420f0000000000fd1503b52268657265'
            b'2d6172652d352d726561736f6e732d692d6e657874636c6f75642d746c674dd302080110011aee0408011'
            b'2a604080410011a2b4865726520617265203520526561736f6e73204920e29da4efb88f204e657874636c'<commit_msg>Moves get_channel,get_stream out of Class into their own functions<commit_after>from binascii import unhexlify

from cryptography.exceptions import InvalidSignature

from torba.testcase import AsyncioTestCase
from torba.client.constants import CENT, NULL_HASH32

from lbrynet.wallet.ledger import MainNetLedger
from lbrynet.wallet.transaction import Transaction, Input, Output

from lbrynet.schema.claim import Claim


def get_output(amount=CENT, pubkey_hash=NULL_HASH32):
    return Transaction() \
        .add_outputs([Output.pay_pubkey_hash(amount, pubkey_hash)]) \
        .outputs[0]


def get_input():
    return Input.spend(get_output())


def get_tx():
    return Transaction().add_inputs([get_input()])


def get_channel(claim_name='@foo'):
    channel_txo = Output.pay_claim_name_pubkey_hash(CENT, claim_name, Claim(), b'abc')
    channel_txo.generate_channel_private_key()
    get_tx().add_outputs([channel_txo])
    return channel_txo


def get_stream(claim_name='foo'):
    stream_txo = Output.pay_claim_name_pubkey_hash(CENT, claim_name, Claim(), b'abc')
    get_tx().add_outputs([stream_txo])
    return stream_txo


class TestSigningAndValidatingClaim(AsyncioTestCase):

    def test_successful_create_sign_and_validate(self):
        channel = get_channel()
        stream = get_stream()
        stream.sign(channel)
        self.assertTrue(stream.is_signed_by(channel))

    def test_fail_to_validate_on_wrong_channel(self):
        stream = get_stream()
        stream.sign(get_channel())
        self.assertFalse(stream.is_signed_by(get_channel()))

    def test_fail_to_validate_altered_claim(self):
        channel = get_channel()
        stream = get_stream()
        stream.sign(channel)
        self.assertTrue(stream.is_signed_by(channel))
        stream.claim.stream.title = 'hello'
        self.assertFalse(stream.is_signed_by(channel))

    def test_valid_private_key_for_cert(self):
        channel = get_channel()
        self.assertTrue(channel.is_channel_private_key(channel.private_key))

    def test_fail_to_load_wrong_private_key_for_cert(self):
        channel = get_channel()
        self.assertFalse(channel.is_channel_private_key(get_channel().private_key))


class TestValidatingOldSignatures(AsyncioTestCase):

    def test_signed_claim_made_by_ytsync(self):
        stream_tx = Transaction(unhexlify(
            b'0100000001eb2a756e15bde95db3d2ae4a6e9b2796a699087890644607b5b04a5f15b67062010000006a4'
            b'7304402206444b920bd318a07d9b982e30eb66245fdaaa6c9866e1f6e5900161d9b0ffd70022036464714'
            b'4f1830898a2042aa0d6cef95a243799cc6e36630a58d411e2f9111f00121029b15f9a00a7c3f21b10bd4b'
            b'98ab23a9e895bd9160e21f71317862bf55fbbc89effffffff0240420f0000000000fd1503b52268657265'
            b'2d6172652d352d726561736f6e732d692d6e657874636c6f75642d746c674dd302080110011aee0408011'
            b'<KEY>'
{'fraction_non_alphanumeric': 0.08055413032324268, 'fraction_numerical': 0.11954848640328374, 'mean_word_length': 4.5961722488038275, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 6, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3420465', 'n_tokens_mistral': 2452, 'n_tokens_neox': 2057, 'n_words': 243}