examples/basic_usage.py
| 1 | """Basic usage: create wallet, add credentials, reload and read. |
| 2 | |
| 3 | Run: python examples/basic_usage.py |
| 4 | """ |
| 5 | |
| 6 | from __future__ import annotations |
| 7 | |
| 8 | import tempfile |
| 9 | |
| 10 | from quantumshield import AgentIdentity |
| 11 | |
| 12 | from pqc_agent_wallet import Wallet |
| 13 | |
| 14 | |
| 15 | def main() -> None: |
| 16 | owner = AgentIdentity.create("demo-agent") |
| 17 | with tempfile.NamedTemporaryFile(suffix=".wallet", delete=False) as tmp: |
| 18 | path = tmp.name |
| 19 | |
| 20 | # Create and populate |
| 21 | w = Wallet.create_with_passphrase(path, "hunter2-demo", owner) |
| 22 | w.put("openai_api_key", "sk-demo-openai", service="openai", tags=["prod"]) |
| 23 | w.put( |
| 24 | "anthropic_api_key", |
| 25 | "sk-demo-anthropic", |
| 26 | service="anthropic", |
| 27 | tags=["prod"], |
| 28 | ) |
| 29 | w.put( |
| 30 | "postgres_password", |
| 31 | "demo-db-password", |
| 32 | service="postgres", |
| 33 | scheme="password", |
| 34 | ) |
| 35 | print(f"Stored {len(w.list_names())} credentials: {w.list_names()}") |
| 36 | w.save() |
| 37 | w.lock() |
| 38 | print(f"[OK] Wallet saved and locked at {path}") |
| 39 | |
| 40 | # Reload & unlock |
| 41 | w2 = Wallet.load(path, owner) |
| 42 | w2.unlock_with_passphrase("hunter2-demo") |
| 43 | print("[OK] Wallet reloaded and unlocked.") |
| 44 | print(f"openai_api_key = {w2.get('openai_api_key')}") |
| 45 | print(f"postgres_password = {w2.get('postgres_password')}") |
| 46 | |
| 47 | # Audit log |
| 48 | print("\nAudit log (last 5):") |
| 49 | for e in w2.audit.entries(limit=5): |
| 50 | print( |
| 51 | f" [{e.timestamp}] {e.operation} {e.credential_name} success={e.success}" |
| 52 | ) |
| 53 | |
| 54 | |
| 55 | if __name__ == "__main__": |
| 56 | main() |
| 57 | |