Reference@t402/cosmos

@t402/cosmos

Cosmos Hub ecosystem implementation for T402 payments using native bank transfers on Noble chain.

Installation

npm install @t402/cosmos

Overview

The @t402/cosmos package enables T402 payments on the Cosmos ecosystem using native USDC transfers on Noble chain. It uses the exact-direct scheme — the client executes the token transfer directly on-chain, then provides the transaction hash as proof of payment.

Key features:

  • Noble chain — native USDC issuance via Circle’s Cross-Chain Transfer Protocol (CCTP)
  • Direct settlement — client submits the transfer, no facilitator relay needed
  • Instant finality — Tendermint consensus provides immediate transaction finality
  • Low fees — typical gas cost ~0.005 USDC per transaction
  • Bech32 addresses — human-readable noble1... format

Supported Networks

NetworkCAIP-2 IDRPC EndpointREST API
Noble Mainnetcosmos:noble-1https://noble-rpc.polkachu.comhttps://noble-api.polkachu.com
Noble Testnetcosmos:grand-1https://rpc.testnet.noble.strange.lovehttps://api.testnet.noble.strange.love

Quick Start

import { ExactDirectCosmosClient } from '@t402/cosmos/exact-direct/client'
 
const client = new ExactDirectCosmosClient({
  address: 'noble1abc...xyz',
  signAndBroadcast: async (txRaw) => {
    // Use your Cosmos wallet to sign and broadcast
    const result = await wallet.signAndBroadcast(txRaw)
    return result.transactionHash
  }
})
 
// Create payment payload (executes transfer on-chain)
const payload = await client.createPaymentPayload(2, requirements)

Client Usage

import { ExactDirectCosmosClient } from '@t402/cosmos/exact-direct/client'
 
const client = new ExactDirectCosmosClient({
  address: 'noble1ejc2c2gvk46h7kyulx9fus85vdpq0zdjwkfav0',
  signAndBroadcast: async (txRaw) => {
    // Sign and broadcast the MsgSend transaction
    const result = await wallet.signAndBroadcast(txRaw)
    return result.transactionHash
  },
  gasLimit: '200000',      // optional, default 200000
  gasPrice: '0.025uusdc'   // optional, default 0.025uusdc
})
 
// Create payment payload (executes transfer on-chain)
const payload = await client.createPaymentPayload(2, requirements)

Server Usage

import { ExactDirectCosmosServer } from '@t402/cosmos/exact-direct/server'
 
const server = new ExactDirectCosmosServer({
  rpcUrl: 'https://noble-rpc.polkachu.com',
  restUrl: 'https://noble-api.polkachu.com'
})
 
// Enhance requirements with Cosmos-specific fields
const enhanced = await server.enhancePaymentRequirements(baseRequirements)

Facilitator Usage

import { ExactDirectCosmosFacilitator } from '@t402/cosmos/exact-direct/facilitator'
 
const facilitator = new ExactDirectCosmosFacilitator({
  rpcUrl: 'https://noble-rpc.polkachu.com',
  restUrl: 'https://noble-api.polkachu.com'
})
 
// Verify the transaction hash from client
const result = await facilitator.verify(payload, requirements)
// For exact-direct, settle is a no-op (already settled by client)
const settled = await facilitator.settle(payload, requirements)

PaymentRequirements

const requirements = {
  scheme: 'exact-direct',
  network: 'cosmos:noble-1',
  amount: '1000000', // 1 USDC (6 decimals)
  asset: 'uusdc', // denomination (microUSDC)
  payTo: 'noble1ejc2c2gvk46h7kyulx9fus85vdpq0zdjwkfav0',
  maxTimeoutSeconds: 300
}

Payload Structure

interface ExactDirectCosmosPayload {
  txHash: string;    // Transaction hash (hex, no 0x prefix)
  from: string;      // noble1... sender address
  to: string;        // noble1... recipient address
  amount: string;    // Amount in uusdc (smallest unit)
  denom?: string;    // Token denomination (default: uusdc)
}

Payment Flow

  1. Client creates a /cosmos.bank.v1beta1.MsgSend transaction
  2. Client signs and broadcasts the transaction to Noble chain
  3. Transaction executes with Tendermint consensus (instant finality)
  4. Client returns the transaction hash as proof of payment
  5. Facilitator verifies the transaction via RPC or REST API

The exact-direct scheme means the client settles the payment directly on-chain. The facilitator only verifies the transaction — it does not relay or execute it.

Token Details

PropertyValue
TokenNative USDC (Circle CCTP)
Denominationuusdc (micro-USDC)
Decimals6
Facilitator Address (Mainnet)noble1ejc2c2gvk46h7kyulx9fus85vdpq0zdjwkfav0
Facilitator Address (Testnet)noble1ejc2c2gvk46h7kyulx9fus85vdpq0zdjwkfav0

Gas Configuration

ParameterValue
Gas Limit200,000
Gas Price0.025 uusdc
Typical Fee~0.005 USDC (5,000 uusdc)

Error Codes

CodeDescription
invalid_payload_structureMissing txHash, from, to, or amount
network_mismatchTransaction network doesn’t match requirements
amount_mismatchTransferred amount less than required
recipient_mismatchTransfer recipient doesn’t match payTo
denom_mismatchToken denomination doesn’t match requirements
transaction_not_foundTransaction hash not found on-chain
transaction_failedTransaction exists but failed

Multi-SDK Examples

import { ExactDirectCosmosClient } from '@t402/cosmos/exact-direct/client'
import { t402Client } from '@t402/fetch'
import { registerExactDirectCosmosClient } from '@t402/cosmos/exact-direct/client'
 
// Setup client
const cosmosClient = new ExactDirectCosmosClient({
  address: wallet.address,
  signAndBroadcast: wallet.signAndBroadcast
})
 
// Register with t402 client
const client = new t402Client()
registerExactDirectCosmosClient(client, {
  address: wallet.address,
  signAndBroadcast: wallet.signAndBroadcast
})
 
// Make paid request
const response = await client.fetch('https://api.example.com/premium')

Example: Express Integration

import express from 'express'
import { paymentMiddleware } from '@t402/express'
 
const app = express()
 
app.use(paymentMiddleware({
  'GET /api/premium': {
    price: '$0.01',
    network: 'cosmos:noble-1',
    asset: 'uusdc',
    payTo: 'noble1ejc2c2gvk46h7kyulx9fus85vdpq0zdjwkfav0',
    description: 'Premium API access'
  }
}))
 
app.get('/api/premium', (req, res) => {
  res.json({ data: 'premium content' })
})
 
app.listen(3000)

Additional Resources