You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import{l402Fetch}from'@te-btc/cashu-l402';// Provide your own Lightning payment functionconstpayInvoice=async(bolt11: string)=>{constresult=awaitmyLndClient.sendPayment(bolt11);return{preimage: result.preimage,feeSats: result.fee};};// Fetch with automatic 402 handlingconstresponse=awaitl402Fetch('https://api.example.com/premium',payInvoice,{},1000,// max 1000 sats);
API
Cashu Paywall (NUT-24)
Function
Description
parseCashuAuthHeader(header)
Extract token from Cashu <token> header
buildCashuChallenge(config)
Build WWW-Authenticate challenge
verifyCashuPayment(token, config)
Full verification + double-spend prevention
detectPaymentMethod(header)
Detect L402 vs Cashu vs none
buildDualChallenge(config, l402?)
Both challenges in one 402 response
L402 Server
Function
Description
createL402Challenge(params)
Generate macaroon + request invoice
verifyL402Token(params)
Verify macaroon signature + preimage
signMacaroon(payload, rootKey)
HMAC-SHA256 sign a macaroon
verifyMacaroon(token, rootKey)
Verify macaroon (timing-safe)
verifyPreimage(preimage, rHash)
SHA-256 preimage check (constant-time)
L402 Client
Function
Description
parseL402Challenge(header)
Parse macaroon + invoice from WWW-Authenticate
buildL402Header(token)
Build L402 <mac>:<preimage> header
l402Fetch(url, payFn, opts?, max?)
Auto-pay fetch with token caching
Spending Conditions
Function
Description
parseNut10Secret(secret)
Parse NUT-10 well-known secret format
detectConditions(proof)
Detect P2PK, HTLC, PoS, time-locks on a proof
extractConditionCaveats(info)
Convert conditions to macaroon caveats
prevalidateCondition(proof, time?)
Client-side locktime validation
Spend Router
Function
Description
routePayment(params)
Choose optimal backend (cashu/lightning/fedimint)
Ecosystem Positioning
This library occupies a unique niche — no standalone TypeScript L402+Cashu bridge exists.
Complements Lightning Agent Tools (Lightning Labs, Feb 2026) — agents use their MCP server for LN reconnaissance, our library for Cashu settlement. Validates Fewsats pattern — policy engine + L402 for safe agent payments. Ahead of NIST — AI Agent Standards Initiative (early 2026) is formalizing the security controls this library already implements.