def test_key_to_certificate():
    """
    Using known good cert files from casperlabs/key-generator in `cert_files` directory to
    test conversion from key to certificate.
    """
    current_path = Path(__file__).resolve().parent
    cert_path = current_path / "cert_files"
    node_cert_pem = cert_path / "node.certificate.pem"
    node_key_pem = cert_path / "node.key.pem"
    node_id = cert_path / "node-id"

    # Read in and generate key
    node_key_data = read_binary_file(node_key_pem)
    private_key_obj = serialization.load_pem_private_key(
        node_key_data, None, default_backend())
    public_key_obj = private_key_obj.public_key()
    node_address_data = read_file(node_id).strip()
    node_address_calc = node_public_address(public_key_obj)
    assert node_address_data == node_address_calc

    # Read cert
    node_cert_data = read_binary_file(node_cert_pem)
    cert = x509.load_pem_x509_certificate(node_cert_data, default_backend())
    print(cert)

    py_cert_pem, key_pem = crypto.generate_node_certificates(
        private_key_obj, private_key_obj.public_key())
    py_cert = x509.load_pem_x509_certificate(py_cert_pem, default_backend())
    print(py_cert)
Пример #2
0
    def from_public_key_path(
            public_key_pem_path: Union[str, Path]) -> "KeyHolder":
        """
        Creates SECP256K1Key object from public key file in pem format.

        Note: Functionality requiring Private Key will not be possible.  Use only if no private key pem is available.
        """
        public_key_pem = read_binary_file(public_key_pem_path)
        return SECP256K1Key(public_key_pem=public_key_pem)
Пример #3
0
 def __init__(self, host, port, serviceStub, node_id, certificate_file):
     self.address = f"{host}:{port}"
     self.serviceStub = serviceStub
     self.node_id = node_id or extract_common_name(certificate_file)
     self.certificate_file = certificate_file
     file_contents = read_binary_file(self.certificate_file)
     self.credentials = grpc.ssl_channel_credentials(file_contents)
     self.secure_channel_options = ((
         ("grpc.ssl_target_name_override", self.node_id),
         ("grpc.default_authority", self.node_id),
     ) if self.node_id else None)
Пример #4
0
 def to_protobuf(self) -> consensus.Deploy.Code:
     """ Encode contract into consensus.Deploy.Code """
     if self.wasm_file_path:
         wasm_contract = consensus.Deploy.Code.WasmContract(
             wasm=read_binary_file(self.wasm_file_path)
         )
         return consensus.Deploy.Code(
             args=self.contract_args, wasm_contract=wasm_contract
         )
     if self.contract_hash:
         stored_contract = consensus.Deploy.Code.StoredContract(
             contract_hash=self.contract_hash, entry_point=self.entry_point
         )
         return consensus.Deploy.Code(
             args=self.contract_args, stored_contract=stored_contract
         )
     if self.contract_name:
         stored_contract = consensus.Deploy.Code.StoredContract(
             name=self.contract_name, entry_point=self.entry_point
         )
         return consensus.Deploy.Code(
             args=self.contract_args, stored_contract=stored_contract
         )
     elif self.package_hash:
         svc = consensus.Deploy.Code.StoredVersionedContract(
             package_hash=self.package_hash,
             entry_point=self.entry_point,
             version=self.version,
         )
         return consensus.Deploy.Code(
             args=self.contract_args, stored_versioned_contract=svc
         )
     elif self.package_name:
         svc = consensus.Deploy.Code.StoredVersionedContract(
             name=self.package_name,
             entry_point=self.entry_point,
             version=self.version,
         )
         return consensus.Deploy.Code(
             args=self.contract_args, stored_versioned_contract=svc
         )
     elif self.transfer_args:
         transfer_contract = consensus.Deploy.Code.TransferContract()
         return consensus.Deploy.Code(
             args=self.transfer_args, transfer_contract=transfer_contract
         )
     # If we fall through, may error or provide defaults
     return self._only_args_encode()
Пример #5
0
 def from_private_key_path(
         private_key_pem_path: Union[str, Path]) -> "KeyHolder":
     """ Creates SECP256K1Key object from private key file in pem format """
     private_key_pem = read_binary_file(private_key_pem_path)
     return SECP256K1Key(private_key_pem=private_key_pem)
Пример #6
0
 def from_public_key_path(
         public_key_pem_path: Union[str, Path]) -> "KeyHolder":
     """ Returns a ED25519Key object loaded from a private_key_pem file"""
     public_key_pem = read_binary_file(public_key_pem_path)
     return ED25519Key(public_key_pem=public_key_pem)