-
Notifications
You must be signed in to change notification settings - Fork 13
Updated changes for TEE x402 #165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f636256
Adding changes from previous PR + experimental og-test-v2-x402 module
kylexqian 5be123d
Update models
kylexqian 8c944e8
Merge remote-tracking branch 'origin' into kyle/tee-changes-2
kylexqian 56b53fe
Fix Google model
kylexqian 0988aa6
Add tee signature and tee timestamp to streaming / non-streaming chat…
kylexqian bf973a4
Update Makefile
kylexqian 51bf895
Update Makefile
kylexqian 6d82c4f
Update src/opengradient/cli.py
kylexqian 25c4c21
Update src/opengradient/defaults.py
kylexqian 2012f2d
Update src/opengradient/client/llm.py
kylexqian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,4 +7,4 @@ requests>=2.32.3 | |
| langchain>=0.3.7 | ||
| openai>=1.58.1 | ||
| pydantic>=2.9.2 | ||
| og-test-v2-x402==0.0.9 | ||
| og-test-v2-x402==0.0.11 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,10 @@ | |
| import threading | ||
| from queue import Queue | ||
| from typing import AsyncGenerator, Dict, List, Optional, Union | ||
| import ssl | ||
| import socket | ||
| import tempfile | ||
| from urllib.parse import urlparse | ||
|
|
||
| import httpx | ||
| from eth_account.account import LocalAccount | ||
|
|
@@ -36,6 +40,53 @@ | |
| ) | ||
|
|
||
|
|
||
| def _fetch_tls_cert_as_ssl_context(server_url: str) -> Optional[ssl.SSLContext]: | ||
| """ | ||
| Connect to a server, retrieve its TLS certificate (TOFU), | ||
| and return an ssl.SSLContext that trusts ONLY that certificate. | ||
|
|
||
| Hostname verification is disabled because the TEE server's cert | ||
| is typically issued for a hostname but we may connect via IP address. | ||
| The pinned certificate itself provides the trust anchor. | ||
|
|
||
| Returns None if the server is not HTTPS or unreachable. | ||
| """ | ||
| parsed = urlparse(server_url) | ||
| if parsed.scheme != "https": | ||
| return None | ||
|
|
||
| hostname = parsed.hostname | ||
| port = parsed.port or 443 | ||
|
|
||
| # Connect without verification to retrieve the server's certificate | ||
| fetch_ctx = ssl.create_default_context() | ||
| fetch_ctx.check_hostname = False | ||
| fetch_ctx.verify_mode = ssl.CERT_NONE | ||
|
|
||
kylexqian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| try: | ||
| with socket.create_connection((hostname, port), timeout=10) as sock: | ||
| with fetch_ctx.wrap_socket(sock, server_hostname=hostname) as ssock: | ||
| der_cert = ssock.getpeercert(binary_form=True) | ||
| pem_cert = ssl.DER_cert_to_PEM_cert(der_cert) | ||
| except Exception: | ||
| return None | ||
|
|
||
| # Write PEM to a temp file so we can load it into the SSLContext | ||
| cert_file = tempfile.NamedTemporaryFile( | ||
| prefix="og_tee_tls_", suffix=".pem", delete=False, mode="w" | ||
| ) | ||
| cert_file.write(pem_cert) | ||
| cert_file.flush() | ||
|
Comment on lines
+75
to
+79
|
||
| cert_file.close() | ||
|
|
||
| # Build an SSLContext that trusts ONLY this cert, with hostname check disabled | ||
| ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) | ||
| ctx.load_verify_locations(cert_file.name) | ||
| ctx.check_hostname = False # Cert is for a hostname, but we connect via IP | ||
| ctx.verify_mode = ssl.CERT_REQUIRED # Still verify the cert itself | ||
| return ctx | ||
|
|
||
|
|
||
| class LLM: | ||
| """ | ||
| LLM inference namespace. | ||
|
|
@@ -64,6 +115,13 @@ def __init__(self, wallet_account: LocalAccount, og_llm_server_url: str, og_llm_ | |
| self._og_llm_server_url = og_llm_server_url | ||
| self._og_llm_streaming_server_url = og_llm_streaming_server_url | ||
|
|
||
| self._tls_verify: Union[ssl.SSLContext, bool] = ( | ||
| _fetch_tls_cert_as_ssl_context(self._og_llm_server_url) or True | ||
| ) | ||
| self._streaming_tls_verify: Union[ssl.SSLContext, bool] = ( | ||
| _fetch_tls_cert_as_ssl_context(self._og_llm_streaming_server_url) or True | ||
| ) | ||
|
|
||
| signer = EthAccountSignerv2(self._wallet_account) | ||
| self._x402_client = x402Clientv2() | ||
| register_exact_evm_clientv2(self._x402_client, signer, networks=[BASE_TESTNET_NETWORK]) | ||
|
|
@@ -92,10 +150,10 @@ def _run_coroutine(self, coroutine): | |
|
|
||
| async def _initialize_http_clients(self) -> None: | ||
| if self._request_client is None: | ||
| self._request_client_ctx = x402HttpxClientv2(self._x402_client) | ||
| self._request_client_ctx = x402HttpxClientv2(self._x402_client, verify=self._tls_verify) | ||
| self._request_client = await self._request_client_ctx.__aenter__() | ||
| if self._stream_client is None: | ||
| self._stream_client_ctx = x402HttpxClientv2(self._x402_client) | ||
| self._stream_client_ctx = x402HttpxClientv2(self._x402_client, verify=self._streaming_tls_verify) | ||
| self._stream_client = await self._stream_client_ctx.__aenter__() | ||
|
|
||
| async def _close_http_clients(self) -> None: | ||
|
|
@@ -223,6 +281,8 @@ async def make_request_v2(): | |
| return TextGenerationOutput( | ||
| transaction_hash="external", | ||
| completion_output=result.get("completion"), | ||
| tee_signature=result.get("tee_signature"), | ||
| tee_timestamp=result.get("tee_timestamp"), | ||
| ) | ||
|
|
||
| except Exception as e: | ||
|
|
@@ -348,10 +408,20 @@ async def make_request_v2(): | |
| if not choices: | ||
| raise OpenGradientError(f"Invalid response: 'choices' missing or empty in {result}") | ||
|
|
||
| message = choices[0].get("message", {}) | ||
| content = message.get("content") | ||
| if isinstance(content, list): | ||
| message["content"] = " ".join( | ||
| block.get("text", "") for block in content | ||
| if isinstance(block, dict) and block.get("type") == "text" | ||
| ).strip() | ||
|
|
||
| return TextGenerationOutput( | ||
| transaction_hash="external", | ||
| finish_reason=choices[0].get("finish_reason"), | ||
| chat_output=choices[0].get("message"), | ||
| chat_output=message, | ||
| tee_signature=result.get("tee_signature"), | ||
| tee_timestamp=result.get("tee_timestamp"), | ||
| ) | ||
|
|
||
| except Exception as e: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.