import json
import os.path

from tools import paths


def get_parameters(folder: str) -> dict:
    """Takes a protocol folder ('proto_alpha', 'proto_005_PsBabyM1'...) and
    retrieve json test parameters for that protocol. Assertion failure
    if parameters can't be found."""

    params_file = (
        f'{paths.TEZOS_HOME}src/{folder}/parameters/' 'test-parameters.json'
    )
    assert os.path.isfile(params_file), (
        f'{params_file}'
        ' cannot be found; please first run'
        ' `make` in TEZOS_HOME.'
    )
    with open(params_file) as params:
        return json.load(params)


# This is the secret key used to activate a protocol from genesis in sandbox
# mode. The corresponding public key is hard-coded in the tezos node.
GENESIS_SK = "<KEY>"
GENESIS_PK = "<KEY>"


IDENTITIES = {
    'bootstrap1': {
        'identity': "tz1KqTpEZ7Yob7QbPE4Hy4Wo8fHG8LhKxZSx",
        'public': "<KEY>",
        'secret': (
            "unencrypted:"
            "<KEY>"
        ),
    },
    'bootstrap2': {
        'identity': "tz1gjaF81ZRRvdzjobyfVNsAeSC6PScjfQwN",
        'public': "<KEY>",
        'secret': (
            "unencrypted:"
            "edsk39qAm1fiMjgmPkw1EgQYkMzkJezLNewd7PLNHTkr6w9XA2zdfo"
        ),
    },
    'bootstrap3': {
        'identity': "<KEY>",
        'public': "<KEY>",
        'secret': (
            "unencrypted:"
            "edsk4ArLQgBTLWG5FJmnGnT689VKoqhXwmDPBuGx3z4cvwU9MmrPZZ"
        ),
    },
    'bootstrap4': {
        'identity': "<KEY>",
        'public': "<KEY>",
        'secret': (
            "unencrypted:"
            "edsk2uqQB9AY4FvioK2YMdfmyMrer5R8mGFyuaLLFfSRo8EoyNdht3"
        ),
    },
    'bootstrap5': {
        'identity': "tz1ddb9NMYHZi5UzPdzTZMYQQZoMub195zgv",
        'public': "<KEY>",
        'secret': (
            "unencrypted:"
            "<KEY>"
        ),
    },
    'activator': {'secret': "unencrypted:" + GENESIS_SK},
}


IDENTITIES_SHORT = {'activator': {'secret': "unencrypted:" + GENESIS_SK}}

PROTO_GENESIS = 'ProtoGenesisGenesisGenesisGenesisGenesisGenesk612im'

PROTO_DEMO_NOOPS = 'ProtoDemoNoopsDemoNoopsDemoNoopsDemoNoopsDemo6XBoYp'
PROTO_DEMO_COUNTER = 'ProtoDemoCounterDemoCounterDemoCounterDemoCou4LSpdT'

ALPHA = "ProtoALphaALphaALphaALphaALphaALphaALphaALphaDdp3zK"
ALPHA_DAEMON = "alpha"  # tezos-baker-alpha
ALPHA_FOLDER = "proto_alpha"
ALPHA_PARAMETERS = get_parameters(ALPHA_FOLDER)

BABYLON = "PsBabyM1eUXZseaJdmXFApDSBqj8YBfwELoxZHHW77EMcAbbwAS"
BABYLON_DAEMON = "005-PsBabyM1"
BABYLON_FOLDER = "proto_005_PsBabyM1"

CARTHAGE = "PsCARTHAGazKbHtnKfLzQg3kms52kSRpgnDY982a9oYsSXRLQEb"
CARTHAGE_FOLDER = "proto_006_PsCARTHA"

GRANADA = "PtGRANADsDU8R9daYKAgWnQYAJ64omN1o3KMGVCykShA97vQbvV"
GRANADA_DAEMON = "010-PtGRANAD"
GRANADA_FOLDER = "proto_010_PtGRANAD"
GRANADA_PARAMETERS = get_parameters(GRANADA_FOLDER)

HANGZHOU = "PtHangzHogokSuiMHemCuowEavgYTP8J5qQ9fQS793MHYFpCY3r"
HANGZHOU_DAEMON = "011-PtHangzH"
HANGZHOU_FOLDER = "proto_011_PtHangzH"
HANGZHOU_PARAMETERS = get_parameters(HANGZHOU_FOLDER)

TEZOS_CRT = """
Certificate:
    Data:
        Version: 3 (0x2)
        Serial Number:
            f0:c1:da:d7:8e:cf:49:44:b7:69:a8:1b:89:d4:36:a5
        Signature Algorithm: sha256WithRSAEncryption
        Issuer: CN=Easy-RSA CA
        Validity
            Not Before: Apr 14 09:21:27 2021 GMT
            Not After : Jul 18 09:21:27 2023 GMT
        Subject: CN=localhost
        Subject Public Key Info:
            Public Key Algorithm: rsaEncryption
                RSA Public-Key: (2048 bit)
                Modulus:
                    00:bc:08:18:ef:f6:fd:70:f1:88:02:e7:09:a1:54:
                    ae:68:9f:6a:58:96:84:a8:a0:54:ab:8f:6d:45:7c:
                    fd00:c2b6:b24b:be67:2827:688d:e6a1:6a3b:5d:3b:04:b6:4b:df:c8:
                    7a:a4:4c:49:a7:53:16:d9:df:6c:83:49:7b:fd:d2:
                    34:70:af:db:d4:66:c7:ae:e0:97:b9:82:c6:c6:b9:
                    47:af:f7:39:46:2d:a3:d3:7d:b0:6f:ab:58:d8:c3:
                    fc00:db20:35b:7399::5:74:7b:e6:f8:2d:f9:47:
                    21:43:af:14:e3:b5:75:56:fc:7d:d1:98:d0:6d:07:
                    aa:6e:6c:2e:2e:74:a5:24:05:6b:2f:4a:bc:a8:e8:
                    83:2a:ae:e0:f2:12:78:9b:d2:27:02:ad:a1:af:5e:
                    7a:cb:66:40:bc:94:7f:f4:cc:ab:54:e5:d9:a2:11:
                    08:6a:33:97:aa:5c:46:5d:ad:b5:ae:ca:a6:21:74:
                    89:29:01:e6:a1:df:f1:a8:58:be:10:63:67:a9:9f:
                    fa:8a:f9:dd:a5:2e:bf:00:eb:a6:2b:49:ff:95:d5:
                    a5:0f:f4:15:19:2f:72:7c:52:3c:53:81:56:cb:c3:
                    b3:e0:ea:eb:b9:18:72:cc:91:1e:0a:79:0f:b5:7c:
                    7a:f3:6f:cb:b0:a5:32:3e:36:41:b1:74:05:28:2f:
                    a2:d3
                Exponent: 65537 (0x10001)
        X509v3 extensions:
            X509v3 Basic Constraints:
                CA:FALSE
            X509v3 Subject Key Identifier:
                CB:AA:52:32:A6:25:9D:15:26:55:85:97:82:D6:EA:8B:AD:73:46:FD
            X509v3 Authority Key Identifier:
                keyid:E1:2F:3E:C9:77:09:2E:6D:6A:26:F1:7F:45:3B:30:3F:4E:D8:77:9B
                DirName:/CN=Easy-RSA CA
                serial:4D:04:D1:4A:10:52:34:02:D4:9B:45:C4:C1:BD:FD:F3:2C:EA:77:C9

            X509v3 Extended Key Usage:
                TLS Web Server Authentication
            X509v3 Key Usage:
                Digital Signature, Key Encipherment
            X509v3 Subject Alternative Name:
                DNS:localhost
    Signature Algorithm: sha256WithRSAEncryption
         61:b6:bb:32:4d:44:ee:bb:5f:5b:2d:49:9a:c5:92:ac:11:a0:
         e4:4f:d4:f4:ab:65:c0:65:92:96:55:00:7f:c3:06:a3:f6:48:
         ad:3d:67:cc:c3:71:15:de:bc:1a:f3:e2:d0:f6:e7:50:be:79:
         bc:5b:44:86:5c:06:a1:7e:bb:b1:00:ba:9a:b1:35:5a:ea:6f:
         2d:9d:73:6d:cd:9b:ec:c2:84:ea:30:b7:e7:03:88:b2:9a:2d:
         fd00:a516:7c1b:17cd:6d81:2137:bd2a:2c5b:2f:4b:88:2a:77:05:78:63:78:90:
         1b:1c:c9:10:f4:a0:22:87:cb:45:4c:44:f8:6d:75:3c:f7:11:
         a0:2e:4a:8c:01:5f:ee:5e:15:3b:f5:16:f0:de:0a:1a:99:69:
         1d:1b:b1:c6:07:95:ce:50:7f:5f:af:59:8a:aa:a8:fb:00:43:
         00:a2:a8:84:36:09:f0:68:95:d9:9a:1b:fe:22:e5:0d:1b:63:
         7f:aa:cf:80:1c:a7:07:81:b2:69:f8:13:a5:70:4a:ce:08:53:
         b7:7b:94:e8:bb:b6:65:28:8a:89:85:74:45:b9:65:1b:11:a9:
         8d:7f:5b:81:66:7d:da:7a:ac:28:ba:db:2f:01:1a:bf:d1:a5:
         01:2c:53:c0:8e:09:40:2f:22:36:a0:95:d9:be:96:b9:52:8c:
         38:17:55:1f
-----BEGIN CERTIFICATE-----
MIIDbjCCAlagAwIBAgIRAPDB2teOz0lEt2moG4nUNqUwDQYJKoZIhvcNAQELBQAw
FjEUMBIGA1UEAwwLRWFzeS1SU0EgQ0EwHhcNMjEwNDE0MDkyMTI3WhcNMjMwNzE4
MDkyMTI3WjAUMRIwEAYDVQQDDAlsb2NhbGhvc3QwggEiMA0GCSqGSIb3DQE<KEY>
-----END CERTIFICATE-----
"""


TEZOS_KEY = """
-----<KEY>
"""

# Required to be install for TLS related tests to succeed
TEZOS_CERT = """
-----BEGIN CER<KEY>CD<KEY>
-----END CERTIFICATE-----
"""

"""
Default node parameters.

A high-number of connections helps triggering the maintenance process
 more often, which speeds up some tests. A synchronisation threshold of 0
 ensures all nodes are bootstrapped when they start, which can avoid
 some spurious deadlocks (e.g. a node not broadcasting its head).
"""
NODE_PARAMS = ['--connections', '100', '--synchronisation-threshold', '0']
