@elizaos/plugin-x402 2.0.0-beta.1 → 2.0.3-beta.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +151 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +20 -28399
- package/dist/index.js.map +7 -107
- package/dist/payment-wrapper.d.ts.map +1 -1
- package/dist/startup-validator.d.ts.map +1 -1
- package/package.json +29 -9
- package/src/__tests__/core-test-mock.ts +10 -0
- package/src/index.ts +7 -5
- package/src/payment-wrapper.test.ts +234 -0
- package/src/payment-wrapper.ts +17 -11
- package/src/startup-validator.test.ts +86 -0
- package/src/startup-validator.ts +3 -1
- package/src/x402-replay-guard.ts +1 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Shaw Walters and elizaOS Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
# @elizaos/plugin-x402
|
|
2
|
+
|
|
3
|
+
x402 micropayment middleware for elizaOS plugin HTTP routes.
|
|
4
|
+
|
|
5
|
+
This package lets Eliza agent plugins gate their HTTP routes behind the [x402 protocol](https://x402.org), requiring on-chain micropayments before serving responses. It handles HTTP 402 negotiation, payment verification (on-chain and facilitator-backed), and replay protection in one integration point.
|
|
6
|
+
|
|
7
|
+
## What it does
|
|
8
|
+
|
|
9
|
+
- **Declares payment requirements** via HTTP 402 responses with `accepts` arrays describing payment networks, token addresses, amounts, and resource URLs — compatible with x402scan indexing.
|
|
10
|
+
- **Verifies payments** using three strategies, tried in priority order:
|
|
11
|
+
1. Standard `X-Payment` / `PAYMENT-SIGNATURE` payloads (x402-fetch / CDP-style) verified and settled via a facilitator.
|
|
12
|
+
2. Direct on-chain proofs: Solana SPL token transfers (via `@solana/web3.js`), EVM ERC-20 transfers and EIP-712 authorizations (via `viem`).
|
|
13
|
+
3. Facilitator payment IDs via `X-Payment-Id` header.
|
|
14
|
+
- **Prevents replay attacks** using an atomic in-process guard plus a durable store backed by `runtime.setCache`/`getCache` (shared across processes when using the same DB).
|
|
15
|
+
- **Emits runtime events** on payment required (`PAYMENT_REQUIRED`) and verified (`PAYMENT_VERIFIED`).
|
|
16
|
+
|
|
17
|
+
## Supported networks and tokens
|
|
18
|
+
|
|
19
|
+
| Config name | Network | Token |
|
|
20
|
+
|-------------|---------|-------|
|
|
21
|
+
| `base_usdc` | Base (chain 8453) | USDC |
|
|
22
|
+
| `solana_usdc` | Solana | USDC |
|
|
23
|
+
| `polygon_usdc` | Polygon (chain 137) | USDC |
|
|
24
|
+
| `bsc_usdc` | BSC (chain 56) | USDC |
|
|
25
|
+
| `base_elizaos` | Base | elizaOS token |
|
|
26
|
+
| `solana_elizaos` | Solana | elizaOS token |
|
|
27
|
+
| `solana_degenai` | Solana | degenai |
|
|
28
|
+
|
|
29
|
+
## Enabling in a plugin
|
|
30
|
+
|
|
31
|
+
Install (it declares `@elizaos/core` as a peer dependency; in this monorepo that resolves via `workspace:*`):
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
bun add @elizaos/plugin-x402
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Wrap your route array before returning them from your plugin:
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
import { applyPaymentProtection } from '@elizaos/plugin-x402';
|
|
41
|
+
import type { Plugin, Route } from '@elizaos/core';
|
|
42
|
+
|
|
43
|
+
const routes: Route[] = applyPaymentProtection([
|
|
44
|
+
{
|
|
45
|
+
type: 'GET',
|
|
46
|
+
path: '/api/premium-data',
|
|
47
|
+
public: true,
|
|
48
|
+
x402: {
|
|
49
|
+
priceInCents: 10, // $0.10
|
|
50
|
+
paymentConfigs: ['base_usdc', 'solana_usdc'],
|
|
51
|
+
},
|
|
52
|
+
handler: async (req, res, runtime) => {
|
|
53
|
+
res.json({ result: 'premium content' });
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
]);
|
|
57
|
+
|
|
58
|
+
export const myPlugin: Plugin = { name: 'my-plugin', routes };
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
`applyPaymentProtection` validates config at call time and throws on misconfiguration so the agent fails fast at startup.
|
|
62
|
+
|
|
63
|
+
## Shorthand with character defaults
|
|
64
|
+
|
|
65
|
+
Set `x402: true` on a route to inherit price and payment configs from the agent character:
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
// In character config:
|
|
69
|
+
character.settings.x402 = {
|
|
70
|
+
defaultPriceInCents: 10,
|
|
71
|
+
defaultPaymentConfigs: ['base_usdc', 'solana_usdc'],
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
// In route:
|
|
75
|
+
{ type: 'GET', path: '/api/resource', x402: true, handler: ... }
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Required environment variables
|
|
79
|
+
|
|
80
|
+
Set your payout wallet addresses. If unset, bundled example addresses are used (startup warns in dev, errors in production).
|
|
81
|
+
|
|
82
|
+
```
|
|
83
|
+
# Payout wallets (set the networks you want to accept)
|
|
84
|
+
SOLANA_PUBLIC_KEY=<your-solana-wallet>
|
|
85
|
+
BASE_PUBLIC_KEY=<your-base-evm-wallet>
|
|
86
|
+
POLYGON_PUBLIC_KEY=<your-polygon-evm-wallet>
|
|
87
|
+
BSC_PUBLIC_KEY=<your-bsc-evm-wallet>
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Optional environment variables
|
|
91
|
+
|
|
92
|
+
```
|
|
93
|
+
# Facilitator service (recommended for standard x402-fetch clients)
|
|
94
|
+
X402_FACILITATOR_URL=https://your-facilitator.example.com
|
|
95
|
+
|
|
96
|
+
# Override individual facilitator endpoints
|
|
97
|
+
X402_FACILITATOR_VERIFY_URL=https://your-facilitator.example.com/api/v1/x402/verify
|
|
98
|
+
X402_FACILITATOR_SETTLE_URL=https://your-facilitator.example.com/api/v1/x402/settle
|
|
99
|
+
|
|
100
|
+
# Public base URL for this server (used in resource URLs)
|
|
101
|
+
X402_BASE_URL=https://your-agent.example.com
|
|
102
|
+
|
|
103
|
+
# Development: skip all payment verification
|
|
104
|
+
X402_TEST_MODE=true
|
|
105
|
+
|
|
106
|
+
# Replay protection: use in-memory only (no DB persistence)
|
|
107
|
+
X402_REPLAY_DURABLE=0
|
|
108
|
+
|
|
109
|
+
# Custom RPC endpoints
|
|
110
|
+
SOLANA_RPC_URL=https://your-rpc.solana.com
|
|
111
|
+
BASE_RPC_URL=https://your-rpc.base.org
|
|
112
|
+
|
|
113
|
+
# Token prices for non-stablecoin configs (exact decimal string, dollars)
|
|
114
|
+
ELIZAOS_PRICE_USD=0.05
|
|
115
|
+
AI16Z_PRICE_USD=0.50
|
|
116
|
+
DEGENAI_PRICE_USD=0.01
|
|
117
|
+
|
|
118
|
+
# Debug logging
|
|
119
|
+
DEBUG_X402_PAYMENTS=true
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Custom payment configs
|
|
123
|
+
|
|
124
|
+
Register additional token/network combinations at plugin init time:
|
|
125
|
+
|
|
126
|
+
```typescript
|
|
127
|
+
import { registerX402Config } from '@elizaos/plugin-x402';
|
|
128
|
+
|
|
129
|
+
registerX402Config('mytoken_base', {
|
|
130
|
+
network: 'BASE',
|
|
131
|
+
assetNamespace: 'erc20',
|
|
132
|
+
assetReference: '0x<token-contract-address>',
|
|
133
|
+
paymentAddress: process.env.BASE_PUBLIC_KEY!,
|
|
134
|
+
symbol: 'MYTOKEN',
|
|
135
|
+
chainId: '8453',
|
|
136
|
+
});
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
Then reference `'mytoken_base'` in your route's `paymentConfigs` array.
|
|
140
|
+
|
|
141
|
+
## Payment headers
|
|
142
|
+
|
|
143
|
+
Clients send payment credentials in HTTP headers:
|
|
144
|
+
|
|
145
|
+
| Header | Purpose |
|
|
146
|
+
|--------|---------|
|
|
147
|
+
| `X-Payment` or `PAYMENT-SIGNATURE` | Standard x402 payload (base64 JSON with `x402Version`, `accepted`, `payload`) |
|
|
148
|
+
| `X-Payment-Proof` | Legacy proof: Solana tx sig, EVM tx hash, or colon-delimited format |
|
|
149
|
+
| `X-Payment-Id` | Facilitator-issued payment ID |
|
|
150
|
+
|
|
151
|
+
On success the response includes a `PAYMENT-RESPONSE` header from the facilitator and emits a `PAYMENT_VERIFIED` event on the agent runtime.
|
package/dist/index.d.ts
CHANGED
|
@@ -38,8 +38,8 @@ export type { BuiltInPaymentConfig, CharacterX402Settings, PaymentEnabledRoute,
|
|
|
38
38
|
export type { Network } from "./payment-config.js";
|
|
39
39
|
export { atomicAmountForPriceInCents, BUILT_IN_NETWORKS, getBaseUrl, getPaymentAddress, getPaymentConfig, getX402Health, listX402Configs, PAYMENT_ADDRESSES, PAYMENT_CONFIGS, type PaymentConfigDefinition, registerX402Config, toResourceUrl, toX402Network, } from "./payment-config.js";
|
|
40
40
|
export { applyPaymentProtection, createPaymentAwareHandler, isRoutePaymentWrapped, X402_ROUTE_PAYMENT_WRAPPED, } from "./payment-wrapper.js";
|
|
41
|
-
export { resolveEffectiveX402, X402_EVENT_PAYMENT_REQUIRED, X402_EVENT_PAYMENT_VERIFIED, } from "./x402-resolve.js";
|
|
42
41
|
export { type StartupValidationResult, validateAndThrowIfInvalid, validateX402Startup, } from "./startup-validator.js";
|
|
42
|
+
export { resolveEffectiveX402, X402_EVENT_PAYMENT_REQUIRED, X402_EVENT_PAYMENT_VERIFIED, } from "./x402-resolve.js";
|
|
43
43
|
export { type Accepts, createAccepts, createX402Response, type OutputSchema, validateAccepts, validateX402Response, type X402Response, type X402ScanNetwork, } from "./x402-types.js";
|
|
44
44
|
import type { Plugin } from "@elizaos/core";
|
|
45
45
|
/**
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AAEH,YAAY,EACV,oBAAoB,EACpB,qBAAqB,EACrB,mBAAmB,EACnB,UAAU,EACV,oBAAoB,EACpB,oBAAoB,GACrB,MAAM,eAAe,CAAC;AAEvB,YAAY,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;AACnD,OAAO,EACL,2BAA2B,EAC3B,iBAAiB,EACjB,UAAU,EACV,iBAAiB,EACjB,gBAAgB,EAChB,aAAa,EACb,eAAe,EACf,iBAAiB,EACjB,eAAe,EACf,KAAK,uBAAuB,EAC5B,kBAAkB,EAClB,aAAa,EACb,aAAa,GACd,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EACL,sBAAsB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