Repository navigation
feat(sdk-python): pre-sign x402 policy hook, same guarantees as the TypeScript SDK - #369
IVANLI2412 wants to merge 1 commit into
Conversation
|
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. |
|
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):
Payout: GAAUNVKEGJVXC4HRRTDT5ASHSTC4ALZ4ORL2PJV5VWOSNXWNS6PJD6OT (Stellar testnet) Will submit application on drips.network for issue #355 once this PR is assigned. Ready to merge once assigned. |
Closes #355
What
Adds the Python SDK pre-sign policy hook, with the same guarantees as
packages/sdk/src/x402-policy.ts:src/nirium/x402_policy.py(standard library only, so it can beimported and tested without
stellar-sdk):build_context(...)builds aPolicyContextwhosecontext_hashbindsurl+method+ the hash of the exact authorization being signed; thepolicy receives
amount,asset,payee/payTo,resourceandnetwork.run_policy_gate(policy, ctx, sign, ...)accepts either a hook object(
evaluate, optionalcurrent_version/on_decision/timeout_ms/require_expires_at) or a plain callable returningALLOW/DENY/WAIT.X402PolicyError.outcomeis one ofDENY,WAIT,TIMEOUT,EXPIRED,UNBOUND,MALFORMED,STALE,VERSION_UNAVAILABLE,SIGNER_ERROR.Guarantees enforced, in the same order as the TypeScript hook:
DENY/WAITnever reach the signerDENY/WAITALLOWmust echo thecontextHashit was givenUNBOUNDexpiresAtalready reached at signing time (equality counts)EXPIREDNone, unknown verdict, non-numericexpiresAt, missingpolicyVersionwhen a version is expected)MALFORMEDevaluateand the freshness checkTIMEOUTpolicyVersionre-read right before signing; mismatch / unreadable / emptySTALE/VERSION_UNAVAILABLEonDecisionobserver runs after the allowance, before the version read, and can never block itclient.pywiring:init_x402(..., policy=...)stores the hook and_x402_authorize_entries()runs the gate for every Soroban authorizationentry immediately before
authorize_entry(the actual signing call). Withouta policy the behaviour is unchanged.
payloadHashis the SHA-256 of thatauthorization entry's XDR: the TypeScript hook hashes the auth preimage, so the
covered bytes differ, but the binding guarantee is the same — an
ALLOWapproved for one authorization cannot sign another.
nirium/__init__.pyexportsX402PolicyError,PolicyContext,PolicyDecision,PolicyResult,run_policy_gate.Tests
New
tests/test_x402_policy.py(21 cases) mirrorsx402-policy-conformance.ts; every refusal assertssigner.calls == 0:records == [ALLOWED, SIGNED])UNBOUND)expiresAtsigns nothing, and equality counts as expired (EXPIRED)None, lowercase'allow', non-numericexpiresAt, non-string verdict)TIMEOUT)STALE) and unreadable/empty version (VERSION_UNAVAILABLE) sign nothingpolicyVersionwhile a version is expected (MALFORMED)SIGNER_ERRORwith the[ALLOWED, SIGNER_ERROR]trailNew
tests/test_x402_policy_client.py(5 cases) exercises the real wiring withfake authorization entries and a spy on
authorize_entry: no policy signs everyentry as before, an ALLOW signs once per entry, a DENY raises before any signing
call, and the context carries the payment details.
Local run
stellar-sdk 15.0.0(inside the project's>=11,<16range), Python 3.13.tests/test_langchain_x402.pywas not run: it needs the optionalnirium[langchain]extra, which is not installed here.Payout
Stellar:
GAAUNVKEGJVXC4HRRTDT5ASHSTC4ALZ4ORL2PJV5VWOSNXWNS6PJD6OT