import secrets
import requests
from eth_account.messages import encode_defunct
from eth_account import Account
IO_API_KEY = "your-api-key"
BASE_URL = "https://api.intelligence.io.net/v1"
PRIVATE_URL = "https://api.intelligence.io.net/v1/private"
def get_attestation_models():
"""Get models that support attestation."""
response = requests.get(
f"{BASE_URL}/models",
headers={"Authorization": f"Bearer {IO_API_KEY}"}
)
models = response.json()["data"]
return [m for m in models if m.get("supports_attestation")]
def get_attestation(model_id: str, nonce: str):
"""Get attestation report for a model."""
response = requests.post(
f"{PRIVATE_URL}/attestation",
headers={
"Authorization": f"Bearer {IO_API_KEY}",
"Content-Type": "application/json"
},
json={"model_id": model_id, "nonce": nonce}
)
response.raise_for_status()
return response.json()
def confidential_completion(model: str, messages: list):
"""Run confidential inference and return response with headers."""
response = requests.post(
f"{PRIVATE_URL}/completions",
headers={
"Authorization": f"Bearer {IO_API_KEY}",
"Content-Type": "application/json"
},
json={"model": model, "messages": messages}
)
response.raise_for_status()
return {
"body": response.json(),
"signature_headers": {
"text": response.headers.get("text"),
"signature": response.headers.get("signature"),
"signing_address": response.headers.get("signing_address"),
"signing_algo": response.headers.get("signing_algo"),
"image_digest": response.headers.get("image_digest"),
}
}
def verify_signature(text: str, signature: str, expected_address: str):
"""Verify response signature."""
message = encode_defunct(text=text)
recovered = Account.recover_message(message, signature=signature)
return recovered.lower() == expected_address.lower()
# Main flow
if __name__ == "__main__":
# 1. Find an attestation-enabled model
models = get_attestation_models()
model = models[0]
print(f"Using model: {model['name']}")
# 2. Get attestation with fresh nonce
nonce = secrets.token_hex(16)
attestation = get_attestation(model["model_id"], nonce)
# Verify nonce freshness (nonce is padded to 64 hex chars)
assert attestation["nonce"].startswith(nonce), "Nonce mismatch!"
signing_address = attestation["signing_address"]
print(f"Attestation verified, signing address: {signing_address}")
# 3. Run confidential inference
result = confidential_completion(
model=model["name"],
messages=[{"role": "user", "content": "Hello, explain TEE in one sentence."}]
)
# 4. Verify response signature
headers = result["signature_headers"]
assert headers["signing_address"].lower() == signing_address.lower(), \
"Signing address mismatch!"
assert verify_signature(
headers["text"],
headers["signature"],
signing_address
), "Signature verification failed!"
print("Response verified!")
print(result["body"]["choices"][0]["message"]["content"])