def from_int(cls, num):
     """
     :param num: A private key in raw integer form.
     :type num: ``int``
     :rtype: :class:`~core.PrivateKey`
     """
     return PrivateKey(ECPrivateKey.from_int(num))
Exemple #2
0
 def from_pem(cls, ledger, pem) -> 'PrivateKey':
     der = pem_to_der(pem.encode())
     try:
         key_int = ECPrivateKey.load(der).native['private_key']
     except ValueError:
         key_int = PrivateKeyInfo.load(der).native['private_key']['private_key']
     private_key = cPrivateKey.from_int(key_int)
     return cls(ledger, private_key, bytes((0,)*32), 0, 0)
def test_create_appointment_receipt(appointment_data):
    # Not much to test here, basically making sure the fields are in the correct order
    # The receipt format is user_signature | start_block
    sk = PrivateKey.from_int(42)
    data = get_random_value_hex(120)
    signature = Cryptographer.sign(data.encode("utf-8"), sk)
    start_block = 200

    receipt = receipts.create_appointment_receipt(signature, start_block)

    assert pyzbase32.encode_bytes(receipt[:-4]).decode() == signature
    assert struct.unpack(">I", receipt[-4:])[0] == start_block
def test_create_appointment_receipt_wrong_inputs():
    sk = PrivateKey.from_int(42)
    data = get_random_value_hex(120)
    signature = Cryptographer.sign(data.encode("utf-8"), sk)
    start_block = 200
    overflow_iu4nt = pow(2, 32)

    no_str = [{}, [], None, 15, 4.5, dict(), object, True]
    no_int = [{}, [], None, "", 4.5, dict(), object]

    for wrong_param in no_str:
        with pytest.raises(InvalidParameter,
                           match="user_signature is invalid"):
            receipts.create_appointment_receipt(wrong_param, start_block)
    for wrong_param in no_int:
        with pytest.raises(InvalidParameter,
                           match="must be a 4-byte unsigned integer"):
            receipts.create_appointment_receipt(signature, wrong_param)

    # Same for overflow u4int
    with pytest.raises(InvalidParameter,
                       match="start_block must be a 4-byte unsigned integer"):
        receipts.create_appointment_receipt(signature, overflow_iu4nt)
Exemple #5
0
 def _signing_key_from_privkey(cls, private_key):
     """ Converts a 32-byte private key into an coincurve.PrivateKey object. """
     return _PrivateKey.from_int(
         PrivateKey._private_key_secret_exponent(private_key))
Exemple #6
0
from requests.exceptions import ConnectionError

import common.receipts as receipts
from common.tools import compute_locator, is_compressed_pk
from common.appointment import Appointment, AppointmentStatus
from common.cryptographer import Cryptographer
from common.exceptions import InvalidParameter, InvalidKey, TowerResponseError

import contrib.client.teos_client as teos_client

from contrib.client.test.conftest import get_random_value_hex, get_config

config = get_config()

# dummy keys for the tests
dummy_user_sk = PrivateKey.from_int(1)
dummy_user_id = Cryptographer.get_compressed_pk(dummy_user_sk.public_key)
dummy_teos_sk = PrivateKey.from_int(2)
dummy_teos_id = Cryptographer.get_compressed_pk(dummy_teos_sk.public_key)
another_sk = PrivateKey.from_int(3)

teos_url = "http://{}:{}".format(config.get("API_CONNECT"),
                                 config.get("API_PORT"))
add_appointment_endpoint = "{}/add_appointment".format(teos_url)
register_endpoint = "{}/register".format(teos_url)
get_appointment_endpoint = "{}/get_appointment".format(teos_url)
get_all_appointments_endpoint = "{}/get_all_appointments".format(teos_url)
get_subscription_info_endpoint = "{}/get_subscription_info".format(teos_url)

dummy_appointment_data = {
    "tx": get_random_value_hex(192),