<commit_before>import pytest
from os import urandom
from decimal import Decimal
import time
from pypeerassets.kutil import Kutil
from pypeerassets.provider import Explorer
from pypeerassets.networks import net_query
from pypeerassets.transactions import tx_output, find_parent_outputs, p2pkh_script
from btcpy.structs.transaction import PeercoinMutableTx, PeercoinTx, Locktime
from btcpy.structs.sig import P2pkhSolver


def test_key_generation():
    '''test privkey/pubkey generation.'''

    mykey = Kutil(network="ppc")

    assert isinstance(mykey.privkey, str)
    assert isinstance(mykey.pubkey, str)


def test_key_generation_from_seed():
    '''check if key generation is what is expected from seed.'''

    seed = "Hello PeerAssets."
    mykey = Kutil(from_string=seed, network="tppc")

    assert mykey.privkey == '<KEY>'
    assert mykey.pubkey == '037cf9e7664b5d10ce209cf9e2c7f68baa06f1950114f25677531b959edd7e670c'


def test_address_generation():
    '''test if addresses are properly made'''

    privkey = bytearray(urandom(32))

    assert Kutil(network="ppc", privkey=privkey).address.startswith("P")

    assert isinstance(Kutil(network='ppc').address, str)
    assert len(Kutil(network='ppc').address) == 34


def test_mainnet_wif_import():
    '''test importing WIF privkey'''

    mykey = Kutil(network='ppc', from_wif="U624wXL6iT7XZ9qeHsrtPGEiU78V1YxDfwq75Mymd61Ch56w47KE")

    assert mykey.address == 'PAprodbYvZqf4vjhef49aThB9rSZRxXsM6'
    assert mykey.pubkey == '023aaca6c4f022543f4a2920f66544a6ce89746f7fce4da35d63b5886fdac06634'
    assert mykey.privkey == '<KEY>'


def test_wif_export():
    '''test Kutil WIF export'''

    mykey = Kutil(network='ppc',
                  privkey=bytearray.fromhex('<KEY>')
                 )

    assert isinstance(mykey.wif, str)
    assert mykey.wif == '<KEY>'


def test_sign_transaction():

    network_params = net_query('tppc')
    provider = Explorer(network='tppc')
    key = Kutil(network='tppc',
                privkey=bytearray.fromhex('9e321f5379c2d1c4327c12227e1226a7c2e08342d88431dcbb0063e1e715a36c')
                )
    dest_address = 'mwn75Gavp6Y1tJxca53HeCj5zzERqWagr6'

    unspent = provider.select_inputs(key.address, 0.63)  # 0.69
    output = tx_output(network='tppc',
                       value=Decimal(0.1),
                       n=0, script=p2pkh_script(network='tppc',
                                                address=dest_address)
                       )

    unsigned = PeercoinMutableTx(version=1,
                                 timestamp=int(time.time()),
                                 ins=unspent['utxos'],
                                 outs=[output],
                                 network=network_params.btcpy_constants,
                                 locktime=Locktime(0)
                                 )

    parent_outputs = [find_parent_outputs(provider, i) for i in unsigned.ins]
    solver = P2pkhSolver(key._private_key)

    signed = unsigned.spend(parent_outputs,
                            [solver for i in parent_outputs])

    assert isinstance(signed, PeercoinTx)
    assert signed.hash() == '2f5b3217823371e01fd4adc81f984b78f23481a8297a32d49f78946546dcecf0'<commit_msg>it's impossible to assert hash, due to changing timestamp<commit_after>import pytest
from os import urandom
from decimal import Decimal
import time
from pypeerassets.kutil import Kutil
from pypeerassets.provider import Explorer
from pypeerassets.networks import net_query
from pypeerassets.transactions import tx_output, find_parent_outputs, p2pkh_script
from btcpy.structs.transaction import PeercoinMutableTx, PeercoinTx, Locktime
from btcpy.structs.sig import P2pkhSolver


def test_key_generation():
    '''test privkey/pubkey generation.'''

    mykey = Kutil(network="ppc")

    assert isinstance(mykey.privkey, str)
    assert isinstance(mykey.pubkey, str)


def test_key_generation_from_seed():
    '''check if key generation is what is expected from seed.'''

    seed = "Hello PeerAssets."
    mykey = Kutil(from_string=seed, network="tppc")

    assert mykey.privkey == '<KEY>'
    assert mykey.pubkey == '037cf9e7664b5d10ce209cf9e2c7f68baa06f1950114f25677531b959edd7e670c'


def test_address_generation():
    '''test if addresses are properly made'''

    privkey = bytearray(urandom(32))

    assert Kutil(network="ppc", privkey=privkey).address.startswith("P")

    assert isinstance(Kutil(network='ppc').address, str)
    assert len(Kutil(network='ppc').address) == 34


def test_mainnet_wif_import():
    '''test importing WIF privkey'''

    mykey = Kutil(network='ppc', from_wif="U624wXL6iT7XZ9qeHsrtPGEiU78V1YxDfwq75Mymd61Ch56w47KE")

    assert mykey.address == '<KEY>'
    assert mykey.pubkey == '023aaca6c4f022543f4a2920f66544a6ce89746f7fce4da35d63b5886fdac06634'
    assert mykey.privkey == '<KEY>'


def test_wif_export():
    '''test Kutil WIF export'''

    mykey = Kutil(network='ppc',
                  privkey=bytearray.fromhex('<KEY>')
                 )

    assert isinstance(mykey.wif, str)
    assert mykey.wif == '<KEY>'


def test_sign_transaction():

    network_params = net_query('tppc')
    provider = Explorer(network='tppc')
    key = Kutil(network='tppc',
                privkey=bytearray.fromhex('9e321f5379c2d1c4327c12227e1226a7c2e08342d88431dcbb0063e1e715a36c')
                )
    dest_address = 'mwn75Gavp6Y1tJxca53HeCj5zzERqWagr6'

    unspent = provider.select_inputs(key.address, 0.63)  # 0.69
    output = tx_output(network='tppc',
                       value=Decimal(0.1),
                       n=0, script=p2pkh_script(network='tppc',
                                                address=dest_address)
                       )

    unsigned = PeercoinMutableTx(version=1,
                                 timestamp=int(time.time()),
                                 ins=unspent['utxos'],
                                 outs=[output],
                                 network=network_params.btcpy_constants,
                                 locktime=Locktime(0)
                                 )

    parent_outputs = [find_parent_outputs(provider, i) for i in unsigned.ins]
    solver = P2pkhSolver(key._private_key)

    signed = unsigned.spend(parent_outputs,
                            [solver for i in parent_outputs])

    assert isinstance(signed, PeercoinTx)
{'fraction_non_alphanumeric': 0.0966267339218159, 'fraction_numerical': 0.06257881462799496, 'mean_word_length': 2.8454545454545452, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '613172', 'n_tokens_mistral': 2336, 'n_tokens_neox': 2071, 'n_words': 362}