Skip to content

[WIP] feat: replace accounting service with MPP (Machine Payments Protocol)#326

Draft
itstauq wants to merge 6 commits intomainfrom
tauquir/mpp-integration
Draft

[WIP] feat: replace accounting service with MPP (Machine Payments Protocol)#326
itstauq wants to merge 6 commits intomainfrom
tauquir/mpp-integration

Conversation

@itstauq
Copy link
Member

@itstauq itstauq commented Mar 20, 2026

Summary

Replaces the old external accounting service with MPP (Machine Payments Protocol) using the Tempo blockchain. Changes span Hub backend, aggregator, frontend, and TypeScript SDK.

Backend

  • New /api/v1/wallet/* endpoints: create, import, update, balance, transactions, pay
  • Tempo blockchain queries via Web3 (tempo_utils.py)
  • wallet_address + wallet_private_key on UserModel
  • wallet_private_key excluded from all API response schemas
  • Structured error codes in /pay: INSUFFICIENT_BALANCE, BLOCKCHAIN_TIMEOUT, INVALID_CHALLENGE, PAYMENT_FAILED
  • Duplicate wallet creation guard (returns existing wallet)
  • Removed: accounting_client.py, accounting endpoints, accounting config, accounting exceptions, accounting registration flow

Aggregator

  • MPP 402 payment handler: aggregator → Syft Space (402) → Hub /wallet/pay → retry with X-Payment
  • Shared mpp_payment.py helper
  • user_token replaces transaction_tokens in ChatRequest
  • payment_failed status with specific error messages
  • Connection error messages now include the URL that failed

Frontend

  • WalletProvider replaces AccountingProvider
  • Wallet settings tab (create/import/manage)
  • Balance indicator with "Set up wallet" nudge
  • Removed: accounting context, hooks, queries, types

TypeScript SDK

  • AccountingResource rewritten with wallet methods
  • Chat sends user_token instead of transaction_tokens

TODOs

  • Encrypt wallet private key at rest — Currently stored as plaintext in PostgreSQL. Needs envelope encryption before production.
  • Add tests for wallet and MPP flows — ~1800 lines of accounting tests deleted, no wallet/MPP tests added. Need coverage for: wallet CRUD, /pay endpoint, aggregator 402 retry, SDK chat flow.
  • Update Python SDKsdk/python/ still has old accounting methods.
  • Update Go SDKsdk/golang/ still has old accounting methods.
  • Rate limit /pay endpoint — Per-user rate limiting to prevent runaway 402 payment loops when wallet has insufficient funds.
  • Catch MPP VerificationError in Syft Space — Syft Space policy pre-hook doesn't catch VerificationError from MPP SDK, so "insufficient funds" becomes a generic 500 instead of a meaningful error (Syft Space side fix).
  • Surface Syft Space 500 errors in aggregator — When Syft Space returns 500 after payment, the aggregator shows a generic error. Should parse the response body for the actual failure reason.
  • Payment-specific error states in chat UI — Frontend shows "No Documents Retrieved" for all data source failures. Should distinguish between "no results found", "payment failed", and "connection error".

Test plan

  • Wallet creation via Settings → Wallet tab
  • Balance shows Tempo testnet data (or "Set up wallet" nudge)
  • Registration no longer creates accounting account
  • Old /api/v1/accounting/* endpoints return 404
  • New /api/v1/wallet/* endpoints work
  • MPP 402 payment flow: aggregator handles 402 → Hub pays → retry succeeds
  • wallet_private_key not in any API response
  • Connection errors include the failed URL
  • End-to-end chat with paid endpoint (blocked by testnet wallet funding)
  • Frontend typecheck + lint pass
  • Backend syntax check passes

itstauq added 6 commits March 21, 2026 02:06
Replace the old external accounting service with MPP/Tempo blockchain
payments across Hub backend, aggregator, frontend, and TypeScript SDK.

Backend:
- New wallet endpoints: create, import, update, balance, transactions, pay
- Tempo blockchain queries via Web3 (balance + transaction history)
- wallet_address + wallet_private_key on UserModel (private key excluded
  from all API response schemas)
- Structured error codes in /pay: INSUFFICIENT_BALANCE, BLOCKCHAIN_TIMEOUT,
  INVALID_CHALLENGE, CHALLENGE_TOO_LARGE
- Duplicate wallet creation guard
- Removed: accounting_client.py, accounting endpoints, accounting config,
  accounting exceptions, accounting registration flow

Aggregator:
- 402 payment handler: aggregator gets 402 from Syft Space → calls Hub
  /wallet/pay → retries with X-Payment header
- Shared mpp_payment.py helper for data_source and model clients
- user_token replaces transaction_tokens in ChatRequest
- payment_failed status for clear error propagation

Frontend:
- WalletProvider context replaces AccountingProvider
- Wallet settings tab (create/import/manage wallet)
- Balance indicator with "Set up wallet" nudge
- Transaction display with wallet address truncation
- Removed: accounting context, hooks, queries, types

SDK (TypeScript):
- AccountingResource rewritten with wallet methods
- Chat flow sends user_token instead of transaction_tokens
- getTransactionTokens() deprecated (no-op)
When the aggregator can't reach a Syft Space endpoint, show the URL
that failed and suggest checking the public URL in Settings.
- Clear wallet_private_key when updating address via PUT /wallet
- Fix frontend sending {address} instead of {wallet_address}
- Fix transaction direction by comparing against wallet address
- Add TODOs for NATS tunnel MPP and PAT auth gaps
- Removed address-only update endpoint (no use case without private key)
- Removed UpdateWalletAddressRequest schema, frontend method, SDK method
- "Change Wallet" UI replaced with "Import Different Wallet"
- Transaction direction now compares wallet addresses only (no email)
- walletAddress is required prop on TransactionItem/TransactionList
- Remove unused transaction_token param from aggregator clients
- Run ruff format on wallet.py and exceptions.py
- Remove eslint-disable comment from wallet-context.tsx
- Update backend-test CI matrix from [3.10, 3.11, 3.12] to [3.12, 3.13]
- Format aggregator model.py with ruff
- Add mypy override for mpp.* (uses Python 3.12+ type syntax)
- Remove test_get_accounting_credentials (endpoint was deleted)
@itstauq itstauq force-pushed the tauquir/mpp-integration branch from 6410137 to 0c8ef2e Compare March 20, 2026 22:43
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.

1 participant