Skip to content

feat(tvm): add TON mechanism for exact payment scheme#1583

Draft
ohld wants to merge 13 commits intox402-foundation:mainfrom
ohld:feat/mechanism-tvm-exact
Draft

feat(tvm): add TON mechanism for exact payment scheme#1583
ohld wants to merge 13 commits intox402-foundation:mainfrom
ohld:feat/mechanism-tvm-exact

Conversation

@ohld
Copy link
Copy Markdown

@ohld ohld commented Mar 12, 2026

Summary

Adds full TVM (TON) mechanism support to x402 — both Python and TypeScript SDKs — so anyone using pip install x402[tvm] or @x402/tvm can pay and accept TON payments out of the box, matching the existing EVM/SVM pattern.

What's included

Python SDK (python/x402/mechanisms/tvm/)

  • ExactTvmClientScheme — builds gasless USDT payments on TON using TONAPI relay
  • ExactTvmServerScheme — price parsing with 6-decimal USDT default
  • ExactTvmFacilitatorScheme — verify (Ed25519 + 5 field checks) + settle via TONAPI gasless/send
  • ClientTvmSigner / FacilitatorTvmSigner protocol definitions
  • TonapiProvider concrete implementation (lazy-imported to avoid hard httpx dep)
  • BoC parser, address normalization, Ed25519 verification utilities
  • register_tvm_exact() — V2 only, wildcard tvm:* network
  • 72 unit tests passing

TypeScript SDK (typescript/packages/mechanisms/tvm/)

  • ExactTvmScheme (client) — gasless USDT payment flow with W5R1 wallet signing
  • ExactTvmScheme (server) — price parsing matching EVM/SVM pattern
  • ExactTvmScheme (facilitator) — full Ed25519 signature verification (tweetnacl) + settle
  • ClientTvmSigner / FacilitatorTvmSigner type definitions
  • toClientTvmSigner() / toFacilitatorTvmSigner() factory functions
  • 49 unit tests passing (including real cryptographic fixtures)

Dependencies

  • Python: pytoniq-core>=0.1.36, PyNaCl>=1.5, httpx>=0.28.1 (optional tvm extra)
  • TypeScript: @ton/ton, @ton/core, @ton/crypto, @ton-api/client, @ton-api/ton-adapter, tweetnacl

Related

TON gasless payment flow

Client                    Server                   Facilitator
  |                         |                         |
  |--- GET /resource ------>|                         |
  |<-- 402 + requirements --|                         |
  |                         |                         |
  | [resolve jetton wallet] |                         |
  | [estimate gasless fee]  |                         |
  | [sign W5R1 transfer]    |                         |
  |                         |                         |
  |--- GET + x-402 header ->|                         |
  |                         |--- verify(payload) ---->|
  |                         |    [Ed25519 sig check]  |
  |                         |    [field validation]   |
  |                         |<-- {isValid: true} -----|
  |                         |--- settle(payload) ---->|
  |                         |    [gasless/send relay]  |
  |                         |<-- {success: true} -----|
  |<-- 200 + resource ------|                         |

Network identifiers

Network CAIP-2 Chain ID
TON Mainnet tvm:-239 -239
TON Testnet tvm:-3 -3

Test plan

  • Python: uv run pytest tests/unit/mechanisms/tvm/ -v — 72 tests passing
  • TypeScript: pnpm test in typescript/packages/mechanisms/tvm/ — 49 tests passing
  • E2E: testnet gasless payment flow (in facilitator repo)
  • Integration: pip install "git+https://github.com/ohld/x402.git@feat/mechanism-tvm-exact#subdirectory=python/x402" with [tvm] extra

ohld added 2 commits March 12, 2026 20:30
Add @x402/tvm (TypeScript) and x402[tvm] (Python) mechanism packages
implementing the exact payment scheme for TON blockchain.

Python (mechanisms/tvm/):
- Full gasless USDT payment flow via TONAPI relay
- Ed25519 signature verification for W5R1 wallets
- BoC parser for external messages, jetton transfers
- 6-rule payment verification (protocol, signature, intent, replay,
  relay safety, simulation)
- Idempotent settlement with state machine
- 72 unit tests

TypeScript (@x402/tvm):
- SchemeNetworkClient/Server/Facilitator implementations
- W5R1 wallet signing with @ton/ton SDK
- Gasless estimate + settlement via TONAPI
- CAIP-2 network IDs: tvm:-239 (mainnet), tvm:-3 (testnet)
- 48 unit tests

Refs: spec PR x402-foundation#1455, live facilitator at ton-facilitator.okhlopkov.com
…x dep

- TS facilitator: add full Ed25519 signature verification using tweetnacl
- TS client: compute commission from gasless estimate, propagate stateInit
- Python: add httpx to tvm extras, lazy-import TonapiProvider
- Update facilitator tests with real cryptographic fixtures
@cb-heimdall
Copy link
Copy Markdown

cb-heimdall commented Mar 12, 2026

🟡 Heimdall Review Status

Requirement Status More Info
Reviews 🟡 0/1
Denominator calculation
Show calculation
1 if user is bot 0
1 if user is external 0
2 if repo is sensitive 0
From .codeflow.yml 1
Additional review requirements
Show calculation
Max 0
0
From CODEOWNERS 0
Global minimum 0
Max 1
1
1 if commit is unverified 1
Sum 2

@vercel
Copy link
Copy Markdown

vercel bot commented Mar 12, 2026

@ohld is attempting to deploy a commit to the Coinbase Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions github-actions bot added typescript sdk Changes to core v2 packages python labels Mar 12, 2026
@phdargen phdargen self-assigned this Mar 16, 2026
@phdargen
Copy link
Copy Markdown
Contributor

FYI, we have some guidelines for the implementation of new networks here: https://github.com/coinbase/x402/blob/main/CONTRIBUTING.md#new-chains

ohld added 3 commits March 16, 2026 17:57
Codex review fixes:
- TS client /prepare now sends {walletAddress, walletPublicKey, paymentRequirements}
  matching Python and the actual facilitator API
- TS facilitator delegates verify/settle to facilitator HTTP service instead of
  local BoC parsing (fixes security gap: was checking JSON fields but not BoC content)
- TS /settle sends x402 envelope format matching facilitator API
- validUntil comes from facilitator /prepare response, not invented locally
- Remove unused error constants and Cell import
- Update README: self-relay architecture, remove gasless/toFacilitatorTvmSigner docs
- Update client tests to mock fetch instead of removed signer methods
- Update changelog: "self-relay gas sponsorship" instead of "gasless"
…nly for PR 2)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@github-actions github-actions bot removed the python label Mar 17, 2026
@ohld
Copy link
Copy Markdown
Author

ohld commented Mar 17, 2026

Thanks for the pointer to CONTRIBUTING.md, @phdargen. Splitting this PR to TypeScript-only per the guidelines — just pushed a commit removing all Python TVM files. Python implementation will follow in a separate PR after TS merges.

Testnet E2E setup: happy to prepare everything for testing — deploy a test jetton on TON testnet, fund wallets with testnet TON and test tokens, share private keys privately.

If you'd like me to set it up for you, ping me on Telegram: https://t.me/okhlopkov

Or self-service:

  • Testnet TON: @testgiver_ton_bot on Telegram (2 TON per request)
  • Deploy test jetton: @ton/ton SDK, TEP-74 standard
  • Testnet explorer: https://testnet.tonviewer.com
  • Facilitator testnet mode: TESTNET=true env var

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@github-actions github-actions bot added ecosystem Additions to ecosystem site examples Changes to examples ci website labels Mar 17, 2026
The TVM facilitator scheme now accepts both response formats from
the facilitator API, ensuring compatibility with facilitators that
use either convention.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@cb-heimdall
Copy link
Copy Markdown

Review Error for SAFIR143 @ 2026-03-17 04:47:32 UTC
User failed mfa authentication, either user does not exist or public email is not set on your github profile. \ see go/mfa-help

ohld added 4 commits March 19, 2026 14:14
Breaking changes:
- Client resolves seqno + jetton wallet via TON RPC (default: toncenter.com)
- ExactTvmScheme constructor now accepts optional { rpcUrl, apiKey }
- Removed nonce from TvmPaymentPayload (dedup uses BoC hash)
- Facilitator dedup tracks BoC hashes instead of nonces

Aligns with spec review: no /prepare endpoint, client uses standard
RPC calls like SVM/Stellar/Aptos.
- Client uses paymentRequirements.maxTimeoutSeconds for validUntil
  instead of hardcoded 300s
- Facilitator forwards maxTimeoutSeconds to external /verify and /settle
@ton-api/client and @ton-api/ton-adapter were leftover from the
gasless relay approach. tweetnacl is only used in tests.
Address TON Core team review feedback:
- settlementBoc encodes internal message (not external)
- Payload reduced to {settlementBoc, asset} only
- Remove redundant fields (from, to, amount, walletPublicKey)
- Set bounce=true on internal messages
- Remove facilitatorUrl from PaymentRequirements extra
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci ecosystem Additions to ecosystem site examples Changes to examples sdk Changes to core v2 packages typescript website

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants