@t402/wdk-protocol
v2.9.0
Published
T402 payment protocol for Tether WDK wallet apps
Maintainers
Readme
@t402/wdk-protocol
T402 payment protocol integration for the WDK wallet apps.
Installation
pnpm add @t402/wdk-protocolPeer dependencies:
pnpm add @tetherto/wdk viemUsage
import { T402Protocol } from '@t402/wdk-protocol';
import { T402WDK } from '@t402/wdk';
const wdk = new T402WDK(seedPhrase, { arbitrum: rpcUrl });
const t402 = await T402Protocol.create(wdk, {
chains: ['ethereum', 'arbitrum'],
});
// Auto-pay for HTTP 402 resources
const { response, receipt } = await t402.fetch('https://api.example.com/premium');
console.log('Status:', response.status);
if (receipt) {
console.log('Paid:', receipt.amount, 'on', receipt.network);
}Step-by-step Payment
// 1. Get payment requirements
const requirements = await t402.getRequirements('https://api.example.com/premium');
// 2. Sign the payment
const payload = await t402.signPayment(requirements);
// 3. Submit with signed payment header
const response = await t402.submitPayment(
'https://api.example.com/premium',
payload,
);API
T402Protocol.create(wdk, config?)
Async factory that initializes the protocol with EVM signer registration and chain family detection.
T402Protocol
| Method | Returns | Description |
|--------|---------|-------------|
| fetch(url, init?) | Promise<T402FetchResult> | Fetch with automatic 402 payment handling |
| getRequirements(url, init?) | Promise<PaymentRequired> | Extract payment requirements from a 402 response |
| signPayment(paymentRequired) | Promise<PaymentPayload> | Sign a payment using the registered mechanism |
| submitPayment(url, payload, init?) | Promise<Response> | Submit a request with a pre-signed payment |
| canHandleNetwork(network) | boolean | Check if a CAIP-2 network is supported |
| getChains() | string[] | List configured chains |
| getRegisteredFamilies() | Set<string> | Registered chain families (evm, ton, solana, ...) |
| getHttpClient() | t402HTTPClient | Underlying t402 HTTP client |
| getWdk() | T402WDK | Underlying WDK instance |
Chain Detection Utilities
detectChainFamily(network)-- CAIP-2 to chain family (e.g."eip155:42161"->"evm")detectChainFamilyFromName(name)-- Chain name to family (e.g."arbitrum"->"evm")isEvmNetwork(network)-- Check if CAIP-2 network is EVMgetEvmChainName(network)-- CAIP-2 to chain name (e.g."eip155:42161"->"arbitrum")EVM_CHAIN_MAP-- Map of chain names to CAIP-2 identifiers
HTTP Utilities
extractPaymentRequired(response)-- Parsepayment-requiredorx-paymentheader from a 402 response
Related Packages
@t402/wdk-- Core WDK wallet toolkit@t402/core-- Protocol types and HTTP client@t402/evm-- EVM mechanism (EIP-3009)
License
Apache-2.0
Trademark Notice
T402 is an independent open-source project. It is not affiliated with, endorsed by, sponsored by, or in any way officially connected with Tether Operations Limited, Tether Holdings, or any of their affiliates.
"Tether", "USDT", "USDT0", "USDC", and other token names and trademarks referenced are the property of their respective owners and are used nominatively to describe token interoperability or to reference public open-source products (Apache 2.0). Compatibility with Tether's open-source WDK is at the user's option and does not represent a formal partnership.
