x402
We provide four client and server schemes to facilitate private x402 payment scenarios with varying preconditions and privacy requirements.
Schemes
Generally, the bermuda::transfer scheme should be preferred as it provides the best level of privacy for both payer and payee. bermuda::anyhow is a convenience wrapper scheme, combining bermuda::transfer with bermuda::deposit as fallback in case of insufficient Bermuda balance of the payer. Applicability of a specific scheme varies per use case:
| Scheme | Payer | Payee Balance |
|---|---|---|
| bermuda::deposit | public | private |
| bermuda::transfer | private | private |
| bermuda::anyhow | private/public | private |
| bermuda::withdraw | private | public |
Exports
Our x402 support is manifested in three SDK exports. These interfaces and types follow the conventions laid out by the @x402/* family of packages, and as such can be used as direct drop-ins to realize private payments over x402:
x402Fetch(scheme, signer?, spender?)
Wraps fetch with a x402 Bermuda client.
x402ClientScheme(scheme, signer?, spender?)
Creates a x402 Bermuda client scheme.
x402ServerScheme(scheme)
Creates a x402 Bermuda server scheme.
Example
- Client
- Server
On the client-side just create a wrapped version of fetch with our x402Fetch utility:
let fetchAndPayPrivately = bermuda.x402Fetch('bermuda::anyhow', walletClient, account)
walletClientmust be aviem.WalletClientdue to@x402/*dependencies
Just as the other wrapFetchWithPayment variants the returned function automatically responds to HTTP 402 server responses with private payment payloads produced by given Bermuda account, ultimately resolving the underlying resource.
An example express server integration:
import express from 'express'
import { x402ResourceServer, HTTPFacilitatorClient } from '@x402/core/server'
import { x402HTTPResourceServer } from '@x402/core/http'
import { paymentMiddlewareFromHTTPServer } from '@x402/express'
import sdk, { x402ServerScheme } from '@bermuda/sdk'
let bermuda = sdk('base-sepolia')
let payee = await bermuda.account({ seed: 'my-merchant-account' }).then(kp => kp.address())
let routes = {
'GET /cat': {
accepts: {
scheme: 'bermuda::anyhow',
network: 'eip155:84532' as `${string}:${string}`,
payTo: payee,
price: '$5',
asset: bermuda.config.USDC
}
}
}
let facilitatorClient = new HTTPFacilitatorClient({ url: bermuda.config.relayer })
let resourceServer = new x402ResourceServer(facilitatorClient)
resourceServer.register('eip155:*', new x402ServerScheme('bermuda::anyhow'))
let httpServer = new x402HTTPResourceServer(resourceServer, routes)
let app = express()
app.use(paymentMiddlewareFromHTTPServer(httpServer))
app.get('/cat', (req, res) => res.json({ data: '(•˕ •マ.ᐟ' }))
app.listen(4022, () => console.log(`x402 server up at :4022`))
The provided server scheme, x402ServerScheme exported from @bermuda/sdk, is generic and can be used with any other server framework.