The anp.proof module provides tools for generating and verifying digital signatures for JSON documents following the W3C Data Integrity specification. It is designed for long-term verifiable claims like Verifiable Credentials (VCs), Agent Identity Claims, and DID documentation, as opposed to short-lived session tokens (JWT/JWS).
Key Features:
- W3C Standard Compliance: Fully follows the W3C Data Integrity spec.
- Signature Suites: Supports
EcdsaSecp256k1Signature2019 (for secp256k1 keys) and Ed25519Signature2020 (for Ed25519 keys). - Deterministic: Uses RFC 8785 (JSON Canonicalization Scheme) to ensure signatures are consistent.
- Security: Supports
domain binding and challenge strings to prevent replay attacks. - Offline Verification: Any third party with the public key can verify the proof without contacting a central server.
from cryptography.hazmat.primitives.asymmetric import ec
from anp.proof import generate_w3c_proof, verify_w3c_proof
# 1. Generate key pair
private_key = ec.generate_private_key(ec.SECP256K1())
public_key = private_key.public_key()
# 2. Generate Proof for a document
document = {
"@context": ["https://www.w3.org/ns/did/v1"],
"id": "did:wba:example.com:alice",
"name": "Agent Alice",
"capabilities": ["search", "booking"]
}
signed_doc = generate_w3c_proof(
document=document,
private_key=private_key,
verification_method="did:wba:example.com:alice#key-1",
proof_purpose="assertionMethod"
)
# 3. Verify Proof
is_valid = verify_w3c_proof(signed_doc, public_key)
print(f"Verification result: {is_valid}") # Output: True