Skip to content

Replace manual x402 Solana implementation with official x402 Python SDK #3

@notorious-d-e-v

Description

@notorious-d-e-v

Problem

blockrun_llm/x402.py manually reimplements the x402 client-side payment logic for Solana (ATA derivation, transaction building, v0 signing, blockhash fetching). This has led to two bugs that break all Solana payments:

  1. Wrong Associated Token Program IDATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJe1bRS instead of ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL (causes recipient_mismatch)
  2. Missing v0 version prefix when signingbytes(message) instead of b'\x80' + bytes(message) (causes SignatureFailure)

Both are fixed in #2, but the root cause is maintaining a parallel implementation of logic that already exists in the official SDK.

Proposed Solution

Replace the manual Solana x402 implementation with the official x402 Python SDK (v2.3.0+):

pip install "x402[svm]"

The official SDK's ExactSvmScheme already handles:

  • Correct ATA derivation (including Token-2022 support)
  • v0 message signing with 0x80 prefix
  • Token program detection from on-chain mint owner
  • Decimals fetched from mint data (not hardcoded)
  • Memo instruction with random nonce for uniqueness
  • Compute budget instructions

Example integration

from x402 import x402ClientSync
from x402.mechanisms.svm.exact import ExactSvmScheme
from x402.mechanisms.svm.signers import KeypairSigner

signer = KeypairSigner.from_base58(private_key)
client = x402ClientSync()
client.register("solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp", ExactSvmScheme(signer=signer))

# Create payment payload from 402 response
payload = client.create_payment_payload(payment_required)

This would replace ~200 lines of manual transaction construction in x402.py and eliminate an entire class of bugs by staying in sync with the facilitator's expectations.

Benefits

  • Correctness: maintained alongside the x402 facilitator, so client and server always agree on transaction format
  • Future-proof: new features (Token-2022, new instructions) are handled upstream
  • Less code to maintain: removes _get_ata(), _get_latest_blockhash(), create_solana_payment_payload(), and all the Solana constants

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions