<commit_before>import os
import shutil
import unittest
import tempfile
import jsonschema
from picopayments import api
from picopayments_client import auth
from picopayments import lib
from picopayments import srv
from picopayments import err
from micropayment_core import util
from micropayment_core import keys
from micropayment_core.scripts import compile_deposit_script


CP_URL = os.environ.get("COUNTERPARTY_URL", "http://127.0.0.1:14000/api/")


DEPOSIT_RESULT_SCHEMA = {
    "type": "object",
    "properties": {
        "deposit_script": {"type": "string"},
        "next_revoke_secret_hash": {"type": "string"},
        "signature": {"type": "string"},
        "pubkey": {"type": "string"},
    },
    "required": [
        "deposit_script", "next_revoke_secret_hash", "signature", "pubkey"
    ],
    "additionalProperties": False
}


class TestMpcHubDeposit(unittest.TestCase):

    def setUp(self):
        self.tempdir = tempfile.mkdtemp(prefix="picopayments_test_")
        self.basedir = os.path.join(self.tempdir, "basedir")
        shutil.copytree("tests/fixtures", self.basedir)
        srv.main([
            "--testnet",
            "--basedir={0}".format(self.basedir),
            "--cp_url={0}".format(CP_URL)
        ], serve=False)

    def tearDown(self):
        shutil.rmtree(self.tempdir)

    @unittest.skip("FIXME setup mock counterpartylib")
    def test_standard_usage_xcp(self):

        asset = "XCP"
        client_key = lib.create_key(asset, netcode="XTN")
        client_pubkey = client_key["pubkey"]

        h2c_spend_secret = util.b2h(os.urandom(32))
        h2c_spend_secret_hash = util.hash160hex(h2c_spend_secret)

        params = {
            "asset": asset,
            "spend_secret_hash": h2c_spend_secret_hash
        }
        params = auth.sign_json(params, keys.wif_to_privkey(client_key["wif"]))
        result = api.mph_request(**params)

        handle = result["handle"]
        hub_pubkey = result["pubkey"]
        c2h_spend_secret_hash = result["spend_secret_hash"]

        c2h_deposit_script = compile_deposit_script(
            client_pubkey, hub_pubkey, c2h_spend_secret_hash, 1337
        )

        next_revoke_secret_hash = util.hash160hex(util.b2h(os.urandom(32)))
        params = {
            "handle": handle,
            "deposit_script": c2h_deposit_script,
            "next_revoke_secret_hash": next_revoke_secret_hash
        }
        params = auth.sign_json(params, keys.wif_to_privkey(client_key["wif"]))
        result = api.mph_deposit(**params)

        self.assertIsNotNone(result)
        jsonschema.validate(result, DEPOSIT_RESULT_SCHEMA)

    @unittest.skip("FIXME setup mock counterpartylib")
    def test_validate_deposit_not_already_given(self):

        def func():

            asset = "XCP"
            client_key = lib.create_key(asset, netcode="XTN")
            client_pubkey = client_key["pubkey"]

            h2c_spend_secret = util.b2h(os.urandom(32))
            h2c_spend_secret_hash = util.hash160hex(
                h2c_spend_secret
            )

            params = {
                "asset": asset,
                "spend_secret_hash": h2c_spend_secret_hash
            }
            privkey = keys.wif_to_privkey(client_key["wif"])
            params = auth.sign_json(params, privkey)
            result = api.mph_request(**params)

            handle = result["handle"]
            hub_pubkey = result["pubkey"]
            c2h_spend_secret_hash = result["spend_secret_hash"]

            c2h_deposit_script = compile_deposit_script(
                client_pubkey, hub_pubkey, c2h_spend_secret_hash, 1337
            )

            # submit deposit
            next_revoke_secret_hash = util.hash160hex(util.b2h(os.urandom(32)))

            params = {
                "handle": handle,
                "deposit_script": c2h_deposit_script,
                "next_revoke_secret_hash": next_revoke_secret_hash
            }
            privkey = keys.wif_to_privkey(client_key["wif"])
            params = auth.sign_json(params, privkey)
            result = api.mph_deposit(**params)
            self.assertIsNotNone(result)

            # resubmit deposit
            next_revoke_secret_hash = util.hash160hex(util.b2h(os.urandom(32)))
            params = {
                "handle": handle,
                "deposit_script": c2h_deposit_script,
                "next_revoke_secret_hash": next_revoke_secret_hash
            }
            privkey = keys.wif_to_privkey(client_key["wif"])
            params = auth.sign_json(params, privkey)
            result = api.mph_deposit(**params)

        self.assertRaises(err.DepositAlreadyGiven, func)

    @unittest.skip("FIXME setup mock counterpartylib")
    def test_validate_handle_exists(self):

        def func():

            asset = "XCP"
            client_key = lib.create_key(asset, netcode="XTN")
            next_revoke_secret_hash = util.hash160hex(util.b2h(os.urandom(32)))
            c2h_deposit_script = util.b2h(os.urandom(32)),

            params = {
                "handle": "deadbeef",
                "deposit_script": c2h_deposit_script,
                "next_revoke_secret_hash": next_revoke_secret_hash
            }
            privkey = keys.wif_to_privkey(client_key["wif"])
            params = auth.sign_json(params, privkey)
            api.mph_deposit(**params)

        self.assertRaises(err.HandleNotFound, func)


if __name__ == "__main__":
    unittest.main()<commit_msg>fixed mpc_deposit test<commit_after>import tempfile
import pytest

# this is require near the top to do setup of the test suite
# from counterpartylib.test import conftest

from counterpartylib.test.util_test import CURR_DIR as CPLIB_TESTDIR
from counterpartylib.test.fixtures.params import DP
from micropayment_core.keys import address_from_wif
from picopayments import lib
from picopayments import api
from picopayments_client import auth
import os
from picopayments import err
from micropayment_core import util
from micropayment_core import keys
from micropayment_core.scripts import compile_deposit_script


FIXTURE_SQL_FILE = CPLIB_TESTDIR + '/fixtures/scenarios/unittest_fixture.sql'
FIXTURE_DB = tempfile.gettempdir() + '/fixtures.unittest_fixture.db'
ASSET = "XCP"
FUNDING_WIF = DP["addresses"][0][2]  # XTC: 91950000000, BTC: 199909140
FUNDING_ADDRESS = address_from_wif(FUNDING_WIF)
DEPOSIT_RESULT_SCHEMA = {  # FIXME check deposit result schema
    "type": "object",
    "properties": {
        "deposit_script": {"type": "string"},
        "next_revoke_secret_hash": {"type": "string"},
        "signature": {"type": "string"},
        "pubkey": {"type": "string"},
    },
    "required": [
        "deposit_script", "next_revoke_secret_hash", "signature", "pubkey"
    ],
    "additionalProperties": False
}


@pytest.mark.usefixtures("picopayments_server")
def test_validate_handle_exists():

    try:

        asset = "XCP"
        client_key = lib.create_key(asset, netcode="XTN")
        next_revoke_secret_hash = util.hash160hex(util.b2h(os.urandom(32)))
        c2h_deposit_script = util.b2h(os.urandom(32)),

        params = {
            "handle": "deadbeef",
            "deposit_script": c2h_deposit_script,
            "next_revoke_secret_hash": next_revoke_secret_hash
        }
        privkey = keys.wif_to_privkey(client_key["wif"])
        params = auth.sign_json(params, privkey)
        api.mph_deposit(**params)
        assert False
    except err.HandleNotFound:
        assert True


@pytest.mark.usefixtures("picopayments_server")
def test_validate_deposit_not_already_given():

    try:

        asset = "XCP"
        client_key = lib.create_key(asset, netcode="XTN")
        client_pubkey = client_key["pubkey"]

        h2c_spend_secret = util.b2h(os.urandom(32))
        h2c_spend_secret_hash = util.hash160hex(
            h2c_spend_secret
        )

        params = {
            "asset": asset,
            "spend_secret_hash": h2c_spend_secret_hash
        }
        privkey = keys.wif_to_privkey(client_key["wif"])
        params = auth.sign_json(params, privkey)
        result = api.mph_request(**params)

        handle = result["handle"]
        hub_pubkey = result["pubkey"]
        c2h_spend_secret_hash = result["spend_secret_hash"]

        c2h_deposit_script = compile_deposit_script(
            client_pubkey, hub_pubkey, c2h_spend_secret_hash, 1337
        )

        # submit deposit
        next_revoke_secret_hash = util.hash160hex(util.b2h(os.urandom(32)))

        params = {
            "handle": handle,
            "deposit_script": c2h_deposit_script,
            "next_revoke_secret_hash": next_revoke_secret_hash
        }
        privkey = keys.wif_to_privkey(client_key["wif"])
        params = auth.sign_json(params, privkey)
        result = api.mph_deposit(**params)
        assert result is not None

        # resubmit deposit
        next_revoke_secret_hash = util.hash160hex(util.b2h(os.urandom(32)))
        params = {
            "handle": handle,
            "deposit_script": c2h_deposit_script,
            "next_revoke_secret_hash": next_revoke_secret_hash
        }
        privkey = keys.wif_to_privkey(client_key["wif"])
        params = auth.sign_json(params, privkey)
        result = api.mph_deposit(**params)

        assert False
    except err.DepositAlreadyGiven:
        assert True
{'fraction_non_alphanumeric': 0.10344458707744972, 'fraction_numerical': 0.015297389816003423, 'mean_word_length': 2.66771282856022, 'pattern_counts': {'":': 52, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 1, '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': '1438931', 'n_tokens_mistral': 3221, 'n_tokens_neox': 3022, 'n_words': 491}