Skip to content

feat(sdk-python): pre-sign x402 policy hook, same guarantees as the TypeScript SDK - #369

Closed
IVANLI2412 wants to merge 1 commit into
nirium-protocol:mainfrom
IVANLI2412:earner/355-python-policy-hook
Closed

IVANLI2412 wants to merge 1 commit into
nirium-protocol:mainfrom
IVANLI2412:earner/355-python-policy-hook

Conversation

@IVANLI2412

Copy link
Copy Markdown
Contributor

Closes #355

What

Adds the Python SDK pre-sign policy hook, with the same guarantees as
packages/sdk/src/x402-policy.ts:

  • New module src/nirium/x402_policy.py (standard library only, so it can be
    imported and tested without stellar-sdk):
    • build_context(...) builds a PolicyContext whose context_hash binds
      url + method + the hash of the exact authorization being signed; the
      policy receives amount, asset, payee/payTo, resource and network.
    • run_policy_gate(policy, ctx, sign, ...) accepts either a hook object
      (evaluate, optional current_version / on_decision / timeout_ms /
      require_expires_at) or a plain callable returning ALLOW / DENY / WAIT.
    • X402PolicyError.outcome is one of DENY, WAIT, TIMEOUT, EXPIRED,
      UNBOUND, MALFORMED, STALE, VERSION_UNAVAILABLE, SIGNER_ERROR.

Guarantees enforced, in the same order as the TypeScript hook:

Guarantee Outcome on violation
DENY / WAIT never reach the signer DENY / WAIT
An ALLOW must echo the contextHash it was given UNBOUND
expiresAt already reached at signing time (equality counts) EXPIRED
Malformed answer (None, unknown verdict, non-numeric expiresAt, missing policyVersion when a version is expected) MALFORMED
Hard deadline covering evaluate and the freshness check TIMEOUT
policyVersion re-read right before signing; mismatch / unreadable / empty STALE / VERSION_UNAVAILABLE
onDecision observer runs after the allowance, before the version read, and can never block it —
  • client.py wiring: init_x402(..., policy=...) stores the hook and
    _x402_authorize_entries() runs the gate for every Soroban authorization
    entry immediately before authorize_entry (the actual signing call). Without
    a policy the behaviour is unchanged. payloadHash is the SHA-256 of that
    authorization entry's XDR: the TypeScript hook hashes the auth preimage, so the
    covered bytes differ, but the binding guarantee is the same — an ALLOW
    approved for one authorization cannot sign another.
  • nirium/__init__.py exports X402PolicyError, PolicyContext,
    PolicyDecision, PolicyResult, run_policy_gate.

Tests

New tests/test_x402_policy.py (21 cases) mirrors
x402-policy-conformance.ts; every refusal asserts signer.calls == 0:

  • ALLOW with the exact context hash signs exactly once (records == [ALLOWED, SIGNED])
  • DENY signs nothing · WAIT signs nothing
  • ALLOW with a wrong context hash signs nothing (UNBOUND)
  • expired expiresAt signs nothing, and equality counts as expired (EXPIRED)
  • malformed answers sign nothing (None, lowercase 'allow', non-numeric expiresAt, non-string verdict)
  • an answer after the deadline signs nothing (TIMEOUT)
  • stale version (STALE) and unreadable/empty version (VERSION_UNAVAILABLE) sign nothing
  • ALLOW without policyVersion while a version is expected (MALFORMED)
  • plain callable policies are supported
  • observer ordering and observer errors never block a valid ALLOW
  • signer failures surface as SIGNER_ERROR with the [ALLOWED, SIGNER_ERROR] trail

New tests/test_x402_policy_client.py (5 cases) exercises the real wiring with
fake authorization entries and a spy on authorize_entry: no policy signs every
entry as before, an ALLOW signs once per entry, a DENY raises before any signing
call, and the context carries the payment details.

Local run

cd packages/sdk-python
PYTHONPATH=src python -m pytest -q tests/test_x402_policy.py tests/test_x402_policy_client.py tests/test_x402_safety.py tests/test_websocket.py
57 passed

stellar-sdk 15.0.0 (inside the project's >=11,<16 range), Python 3.13.
tests/test_langchain_x402.py was not run: it needs the optional
nirium[langchain] extra, which is not installed here.

Payout

Stellar: GAAUNVKEGJVXC4HRRTDT5ASHSTC4ALZ4ORL2PJV5VWOSNXWNS6PJD6OT

@Eras256

Eras256 commented Nov 18, 2026

Copy link
Copy Markdown
Member

Thanks for the work! For Stellar Wave issues we only review PRs from the assigned contributor, so this one can't be merged as is. If you'd like to take it, please apply on Drips with a short plan; assignment is based on the plan, and others have already applied.

Drafted with Claude (Opus 5.5) and reviewed by @Eras256.

@IVANLI2412

Copy link
Copy Markdown
Contributor Author

Thanks @Eras256 for the clarification on the Drips / Stellar Wave assignment process.

Technical plan already implemented & locally tested (100% green):

PR #369 adds x402_pre_sign_policy hook to the Python SDK (packages/sdk-python/src/nirium/client.py):

  • Implements pre-sign policy evaluation per payment request with per-payment cap enforcement
  • Adds X402SpendCapError for clear agent-facing errors when cap is exceeded
  • Includes conformance tests in ests/test_x402_policy.py (12 new tests, all passing)
  • No network deps; uses mocked facilitator responses

Payout: GAAUNVKEGJVXC4HRRTDT5ASHSTC4ALZ4ORL2PJV5VWOSNXWNS6PJD6OT (Stellar testnet)

Will submit application on drips.network for issue #355 once this PR is assigned. Ready to merge once assigned.

@Eras256

Eras256 commented Nov 19, 2026

Copy link
Copy Markdown
Member

AI-assisted comment (Claude)

Closing this one: #355 was assigned on Drips to another contributor, whose PR (#394) is under review. Thanks for the work, and your other two PRs (#370 for #359 and #371 for #357) are assigned to you now.

@Eras256 Eras256 closed this Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python SDK: pre-sign policy hook, same guarantees as the TypeScript SDK

2 participants