From 0a066dc99c0ddca8870e927541f480cf8a395497 Mon Sep 17 00:00:00 2001 From: Matthew Raymer Date: Wed, 5 Mar 2025 13:49:54 +0000 Subject: [PATCH] feat: add claim fetching functionality - Add fetch_claim method to match TypeScript version - Implement JWT authentication for claim fetching - Update main flow to fetch claim after registration - Add error handling and logging - Match TypeScript API structure This adds the ability to fetch claim details after successful DID registration, completing the full registration and verification flow. --- test-scripts/new_flow.py | 63 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/test-scripts/new_flow.py b/test-scripts/new_flow.py index 8fbe1d4..480564c 100644 --- a/test-scripts/new_flow.py +++ b/test-scripts/new_flow.py @@ -349,6 +349,55 @@ async def register( print(f"\nAPI Request Error: {str(e)}") return {"error": f"Error submitting claim: {str(e)}"} +async def fetch_claim( + claim_id: str, + active_did: str, + private_key_hex: str, + api_server: str = API_SERVER +) -> Dict[str, Any]: + """ + Fetch claim details from the endorser service. + + Args: + claim_id (str): The ID of the claim to fetch + active_did (str): The DID making the request + private_key_hex (str): Private key for signing auth JWT + api_server (str, optional): API endpoint. Defaults to API_SERVER. + + Returns: + Dict[str, Any]: The claim details + + Raises: + requests.RequestException: For API communication errors + """ + # Create authentication JWT + auth_payload = { + "intent": "claim.read", + "claimId": claim_id + } + + # Sign with the active DID (not endorser DID) + auth_token = await create_endorser_jwt( + active_did, + private_key_hex, + auth_payload + ) + + try: + response = requests.get( + f"{api_server}/api/claim/byHandle/{claim_id}", + headers={ + 'Authorization': f'Bearer {auth_token}', + 'Content-Type': 'application/json' + } + ) + return response.json() + + except requests.RequestException as e: + print("\nError fetching claim:") + print(f"Error: {str(e)}") + raise + # Main execution async def main(): """ @@ -398,5 +447,19 @@ async def main(): result = await register(active_did, private_key_hex) print("Registration result:", result) + # Step 3: If registration successful, fetch claim details + if result.get("success") and result.get("claimId"): + print("\nFetching claim details...") + try: + claim_details = await fetch_claim( + result["claimId"], + active_did, + private_key_hex + ) + print("\nClaim Details:") + print(json.dumps(claim_details, indent=2)) + except Exception as e: + print(f"Error fetching claim: {str(e)}") + if __name__ == "__main__": asyncio.run(main())