import time
import json
from typing import Optional, Union
try:
import requests
except ImportError:
requests = None
from .dcap_qvl import (
PyQuoteCollateralV3 as QuoteCollateralV3,
PyVerifiedReport as VerifiedReport,
PyQuote as Quote,
py_verify as verify,
parse_quote,
get_collateral_for_fmspc,
)
PCS_URL = "https://api.trustedservices.intel.com"
def _make_pcs_request(url: str) -> bytes:
if requests is None:
raise ImportError(
"requests library is required for collateral fetching. Install with: pip install requests")
try:
response = requests.get(url, timeout=30, verify=False)
response.raise_for_status()
return response.content
except requests.RequestException as e:
raise RuntimeError(f"Failed to fetch from {url}: {e}")
async def get_collateral(pccs_url: str, raw_quote: bytes) -> QuoteCollateralV3:
if not isinstance(raw_quote, (bytes, bytearray)):
raise TypeError("raw_quote must be bytes")
quote = Quote.parse(raw_quote)
fmspc = quote.fmspc()
is_sgx = quote.is_sgx()
ca = quote.ca()
return await get_collateral_for_fmspc(pccs_url, fmspc, ca, is_sgx)
async def get_collateral_from_pcs(raw_quote: bytes) -> QuoteCollateralV3:
return await get_collateral(PCS_URL, raw_quote)
async def get_collateral_and_verify(raw_quote: bytes, pccs_url: Optional[str] = None) -> VerifiedReport:
url = pccs_url.strip() if pccs_url else PCS_URL
if not url:
url = PCS_URL
collateral = await get_collateral(url, raw_quote)
now_secs = int(time.time())
print("Collateral:", collateral.to_json())
return verify(raw_quote, collateral, now_secs)
__all__ = [
"QuoteCollateralV3",
"VerifiedReport",
"Quote",
"verify",
"get_collateral",
"get_collateral_from_pcs",
"get_collateral_and_verify",
"get_collateral_for_fmspc",
]
__version__ = "0.3.2"