trustgate-middleware 1.0.0
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/README.md +276 -0
- package/dashboard-dist/assets/index-CTD7-SPR.js +9 -0
- package/dashboard-dist/assets/index-PEgnqtSj.css +2 -0
- package/dashboard-dist/index.html +16 -0
- package/dist/classify.d.ts +14 -0
- package/dist/classify.d.ts.map +1 -0
- package/dist/classify.js +106 -0
- package/dist/classify.js.map +1 -0
- package/dist/emitter.d.ts +7 -0
- package/dist/emitter.d.ts.map +1 -0
- package/dist/emitter.js +36 -0
- package/dist/emitter.js.map +1 -0
- package/dist/index.d.ts +82 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +221 -0
- package/dist/index.js.map +1 -0
- package/dist/pricing.d.ts +65 -0
- package/dist/pricing.d.ts.map +1 -0
- package/dist/pricing.js +61 -0
- package/dist/pricing.js.map +1 -0
- package/dist/scoring.d.ts +38 -0
- package/dist/scoring.d.ts.map +1 -0
- package/dist/scoring.js +106 -0
- package/dist/scoring.js.map +1 -0
- package/dist/store.d.ts +9 -0
- package/dist/store.d.ts.map +1 -0
- package/dist/store.js +72 -0
- package/dist/store.js.map +1 -0
- package/dist/types.d.ts +59 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +7 -0
- package/dist/types.js.map +1 -0
- package/package.json +46 -0
package/README.md
ADDED
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
# trustgate-middleware
|
|
2
|
+
|
|
3
|
+
The trust layer for the agent economy.
|
|
4
|
+
One middleware. Four trust tiers. OWS wallet signatures. Dynamic pricing. Real-time dashboard.
|
|
5
|
+
|
|
6
|
+
Every API request is classified by identity, scored by behavior, and priced by trust — automatically.
|
|
7
|
+
|
|
8
|
+
## Install
|
|
9
|
+
```bash
|
|
10
|
+
npm install trustgate-middleware
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## 3-Line Integration
|
|
14
|
+
```ts
|
|
15
|
+
import { Hono } from 'hono'
|
|
16
|
+
import { trustgate } from 'trustgate-middleware'
|
|
17
|
+
|
|
18
|
+
const app = new Hono()
|
|
19
|
+
app.use('/api/*', trustgate({ payTo: '0xYourWallet' }))
|
|
20
|
+
// Done. Every request is now classified, scored, and priced.
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## What Happens to Every Request
|
|
24
|
+
Request arrives
|
|
25
|
+
→ Identity check (World ID? AgentKit? Wallet? Nothing?)
|
|
26
|
+
→ Tier assigned (HUMAN / HUMAN_AGENT / ANON_BOT / BLOCKED)
|
|
27
|
+
→ Trust score calculated (0-90, four-factor formula)
|
|
28
|
+
→ Price determined (trust score → dynamic x402 price)
|
|
29
|
+
→ Response headers set (X-TrustGate-Tier, X-TrustGate-Trust-Score)
|
|
30
|
+
→ WebSocket event emitted (dashboard updates in real-time)
|
|
31
|
+
→ Request continues (or 402/403 if payment needed/blocked)
|
|
32
|
+
|
|
33
|
+
## The Four Trust Tiers
|
|
34
|
+
|
|
35
|
+
| Tier | Identity | Access | How Detected |
|
|
36
|
+
|------|----------|--------|--------------|
|
|
37
|
+
| **HUMAN** | World ID verified | Free | Cryptographic proof of unique personhood |
|
|
38
|
+
| **HUMAN_AGENT** | AgentKit registered | $0.001/req | On-chain agent registration linked to World ID |
|
|
39
|
+
| **ANON_BOT (OWS)** | OWS wallet signature | $0.003-$0.01/req | EIP-191 cryptographic signature via Open Wallet Standard |
|
|
40
|
+
| **ANON_BOT** | Wallet address only | $0.003-$0.01/req | x402 payment signature or wallet header |
|
|
41
|
+
| **BLOCKED** | Nothing | Denied (403) | No identity, no wallet, no trust |
|
|
42
|
+
|
|
43
|
+
Verified humans always pass free. Trusted agents pay less. Unknown bots pay full price. Blocked traffic is rejected.
|
|
44
|
+
|
|
45
|
+
## Trust Score Formula
|
|
46
|
+
|
|
47
|
+
Every agent builds a reputation score (0-90) based on four factors, inspired by Stanford's EigenTrust algorithm:
|
|
48
|
+
TrustScore = Identity(0-50) + Behavior(0-25) + Reputation(0-15) - Risk(0-30)
|
|
49
|
+
|
|
50
|
+
**Identity (0-50 points)** — Who are you?
|
|
51
|
+
- World ID proof: 50 pts (cryptographic, unfakeable)
|
|
52
|
+
- AgentKit registration: 35 pts (on-chain, human-linked)
|
|
53
|
+
- OWS-verified wallet: 20 pts (EIP-191 signature, unfakeable identity)
|
|
54
|
+
- Payment-verified wallet: 15 pts (has funds, willing to pay)
|
|
55
|
+
- Self-reported address: 5 pts (weak, unverified)
|
|
56
|
+
- Nothing: 0 pts
|
|
57
|
+
|
|
58
|
+
**Behavior (0-25 points)** — How do you act?
|
|
59
|
+
- Payment success rate (0-10): consistent payers score higher
|
|
60
|
+
- Request regularity (0-5): steady patterns beat erratic bursts
|
|
61
|
+
- Endpoint diversity (0-5): broad usage beats single-endpoint hammering
|
|
62
|
+
- Request pacing (0-5): <30 RPM = 5pts, 30-60 = 2pts, >60 = 0pts
|
|
63
|
+
|
|
64
|
+
**Reputation (0-15 points)** — How long have you been here?
|
|
65
|
+
- Account age (0-5): older = more trusted
|
|
66
|
+
- Volume (0-5): logarithmic scale, rewards sustained activity
|
|
67
|
+
- Consistency (0-5): daily active ratio over total days
|
|
68
|
+
|
|
69
|
+
**Risk Penalty (0-30 points subtracted)**
|
|
70
|
+
- Inactivity decay: dormant agents lose trust
|
|
71
|
+
- Frequency spikes: sudden traffic surges trigger surge pricing
|
|
72
|
+
- Failed payments: payment failures erode trust fast
|
|
73
|
+
- Sybil detection: same IP + multiple addresses = penalty
|
|
74
|
+
|
|
75
|
+
## Dynamic Pricing
|
|
76
|
+
|
|
77
|
+
Trust score maps directly to x402 price per request:
|
|
78
|
+
|
|
79
|
+
| Score | Category | Price |
|
|
80
|
+
|-------|----------|-------|
|
|
81
|
+
| 80-100 | Highly Trusted | Free |
|
|
82
|
+
| 60-79 | Trusted | $0.001 |
|
|
83
|
+
| 40-59 | Building Trust | $0.003 |
|
|
84
|
+
| 20-39 | Low Trust | $0.007 |
|
|
85
|
+
| 1-19 | Minimal Trust | $0.01 |
|
|
86
|
+
| 0 | No Trust | Blocked |
|
|
87
|
+
|
|
88
|
+
Higher trust = lower cost. The incentive is built into the economics — agents that behave well pay less over time.
|
|
89
|
+
|
|
90
|
+
## Configuration
|
|
91
|
+
```ts
|
|
92
|
+
import { trustgate } from 'trustgate-middleware'
|
|
93
|
+
|
|
94
|
+
app.use('/api/*', trustgate({
|
|
95
|
+
// Required: wallet address where x402 payments are sent
|
|
96
|
+
payTo: '0xYourWalletAddress',
|
|
97
|
+
|
|
98
|
+
// Optional: blockchain network (default: Base Sepolia)
|
|
99
|
+
network: 'eip155:84532',
|
|
100
|
+
|
|
101
|
+
// Optional: enables HUMAN tier with World ID verification
|
|
102
|
+
worldId: {
|
|
103
|
+
rpId: 'rp_your_app_id', // from developer.world.org
|
|
104
|
+
signingKey: '0xYourSigningKey', // from World developer portal
|
|
105
|
+
},
|
|
106
|
+
|
|
107
|
+
// Optional: serve built-in monitoring dashboard
|
|
108
|
+
dashboard: true,
|
|
109
|
+
|
|
110
|
+
// Optional: standalone WebSocket port (dev only)
|
|
111
|
+
wsPort: 4022,
|
|
112
|
+
}))
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
Without `worldId`, the HUMAN tier is disabled — all traffic is classified as HUMAN_AGENT, ANON_BOT, or BLOCKED. Add your World ID credentials to enable free access for verified humans.
|
|
116
|
+
|
|
117
|
+
## Response Headers
|
|
118
|
+
|
|
119
|
+
Every response includes classification metadata:
|
|
120
|
+
X-TrustGate-Tier: HUMAN_AGENT
|
|
121
|
+
X-TrustGate-Trust-Score: 75
|
|
122
|
+
X-TrustGate-Identity: agentkit
|
|
123
|
+
|
|
124
|
+
Your API can read these headers to customize responses per tier.
|
|
125
|
+
|
|
126
|
+
## OWS Signature Verification
|
|
127
|
+
|
|
128
|
+
TrustGate supports the [Open Wallet Standard](https://openwallet.sh) for cryptographic agent identity. Agents authenticate by signing requests with their wallet using EIP-191 signatures.
|
|
129
|
+
|
|
130
|
+
```ts
|
|
131
|
+
app.use('/api/*', trustgate({ payTo: '0xYourWallet' }))
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
Agents send these headers:
|
|
135
|
+
|
|
136
|
+
| Header | Description |
|
|
137
|
+
|--------|-------------|
|
|
138
|
+
| `x-ows-signature` | EIP-191 signature (hex) |
|
|
139
|
+
| `x-ows-message` | Signed message: `<host>:<path>:<timestamp>` |
|
|
140
|
+
| `x-ows-address` | Wallet address (EVM) |
|
|
141
|
+
| `x-ows-timestamp` | Unix timestamp (ms), 5-min window |
|
|
142
|
+
|
|
143
|
+
TrustGate verifies the signature using `ethers.verifyMessage()`, recovers the signer address, and classifies as `ANON_BOT` with `owsVerified=true`. OWS-verified agents get a **+5 identity score boost** (20 pts vs 15 pts for payment-verified), resulting in lower dynamic pricing over time.
|
|
144
|
+
|
|
145
|
+
## x402 Payment Flow
|
|
146
|
+
|
|
147
|
+
When a non-human request arrives without payment:
|
|
148
|
+
|
|
149
|
+
1. TrustGate returns `402 Payment Required` with an x402 payment spec
|
|
150
|
+
2. The spec includes: USDC amount, wallet address, chain, facilitator URL
|
|
151
|
+
3. The agent pays USDC on Base Sepolia
|
|
152
|
+
4. The agent retries with the payment signature
|
|
153
|
+
5. TrustGate verifies payment via the x402 facilitator
|
|
154
|
+
6. Request proceeds, trust score updates
|
|
155
|
+
|
|
156
|
+
All payments are real USDC on Base Sepolia, verified on-chain.
|
|
157
|
+
|
|
158
|
+
## Built-in Dashboard
|
|
159
|
+
|
|
160
|
+
The package includes a pre-built real-time monitoring dashboard:
|
|
161
|
+
```ts
|
|
162
|
+
import { trustgate, trustgateDashboard, attachWebSocketToServer } from 'trustgate-middleware'
|
|
163
|
+
import { Hono } from 'hono'
|
|
164
|
+
import { serve } from '@hono/node-server'
|
|
165
|
+
|
|
166
|
+
const app = new Hono()
|
|
167
|
+
app.use('/api/*', trustgate({ payTo: '0xYourWallet' }))
|
|
168
|
+
trustgateDashboard(app)
|
|
169
|
+
|
|
170
|
+
const server = serve({ fetch: app.fetch, port: 3000 })
|
|
171
|
+
attachWebSocketToServer(server)
|
|
172
|
+
|
|
173
|
+
// With World ID verification routes auto-mounted:
|
|
174
|
+
trustgateDashboard(app, {
|
|
175
|
+
rpId: 'rp_your_app_id',
|
|
176
|
+
signingKey: '0xYourSigningKey',
|
|
177
|
+
})
|
|
178
|
+
// Auto-creates /trustgate/verify-context and /trustgate/verify-human
|
|
179
|
+
|
|
180
|
+
// Dashboard at http://localhost:3000/trustgate
|
|
181
|
+
// Real-time events via WebSocket at /ws
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
The dashboard shows:
|
|
185
|
+
- Live request flow visualization (particle canvas)
|
|
186
|
+
- Traffic distribution by tier (donut chart)
|
|
187
|
+
- Trust leaderboard (top agents ranked by score)
|
|
188
|
+
- Recent traffic feed (every classification event)
|
|
189
|
+
- Revenue counter (USDC earned from agent payments)
|
|
190
|
+
|
|
191
|
+
No configuration needed. One function call. Dashboard appears.
|
|
192
|
+
|
|
193
|
+
## Exports
|
|
194
|
+
```ts
|
|
195
|
+
// Middleware
|
|
196
|
+
import { trustgate } from 'trustgate-middleware'
|
|
197
|
+
|
|
198
|
+
// Dashboard
|
|
199
|
+
import { trustgateDashboard, attachWebSocketToServer } from 'trustgate-middleware'
|
|
200
|
+
|
|
201
|
+
// Types
|
|
202
|
+
import type { Tier, AgentProfile, TrustGateEvent, TrustGateConfig } from 'trustgate-middleware'
|
|
203
|
+
|
|
204
|
+
// Trust scoring (use directly for custom logic)
|
|
205
|
+
import { calculateTrustScore, getTrustBreakdown } from 'trustgate-middleware'
|
|
206
|
+
|
|
207
|
+
// Pricing
|
|
208
|
+
import { getPrice, calculatePlatformFee, getSimpleHirePriceBand } from 'trustgate-middleware'
|
|
209
|
+
|
|
210
|
+
// Agent store
|
|
211
|
+
import { getAllAgents, getAgent, recordRequest } from 'trustgate-middleware'
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
## Example: The Roast Oracle
|
|
215
|
+
|
|
216
|
+
A demo site protected by trustgate-middleware:
|
|
217
|
+
```ts
|
|
218
|
+
import { Hono } from 'hono'
|
|
219
|
+
import { serve } from '@hono/node-server'
|
|
220
|
+
import { trustgate, trustgateDashboard, attachWebSocketToServer } from 'trustgate-middleware'
|
|
221
|
+
|
|
222
|
+
const app = new Hono()
|
|
223
|
+
|
|
224
|
+
app.use('/api/*', trustgate({
|
|
225
|
+
payTo: '0x976aE51C1bc10Adfa65014cd42dc2c2cf62Fd232',
|
|
226
|
+
worldId: {
|
|
227
|
+
rpId: 'rp_your_id',
|
|
228
|
+
signingKey: '0xYourKey',
|
|
229
|
+
},
|
|
230
|
+
}))
|
|
231
|
+
|
|
232
|
+
trustgateDashboard(app)
|
|
233
|
+
|
|
234
|
+
app.get('/api/joke', (c) => {
|
|
235
|
+
const tier = c.req.header('X-TrustGate-Tier')
|
|
236
|
+
return c.json({
|
|
237
|
+
joke: "Why do AI agents use TrustGate? Trust issues.",
|
|
238
|
+
tier,
|
|
239
|
+
price: tier === 'HUMAN' ? 'free' : 'paid'
|
|
240
|
+
})
|
|
241
|
+
})
|
|
242
|
+
|
|
243
|
+
const server = serve({ fetch: app.fetch, port: 3000 })
|
|
244
|
+
attachWebSocketToServer(server)
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
Humans access free. Bots pay. Blocked traffic never reaches your endpoint.
|
|
248
|
+
|
|
249
|
+
## OWS Policy Engine
|
|
250
|
+
|
|
251
|
+
For agents using OWS wallets, you can enforce spend limits at the wallet level using the OWS Policy Engine. This prevents overspend when TrustGate applies surge pricing.
|
|
252
|
+
|
|
253
|
+
1. Create a policy that queries TrustGate's pricing API
|
|
254
|
+
2. Attach the policy to an API key
|
|
255
|
+
3. When an agent tries to pay, the policy checks the current price
|
|
256
|
+
4. If price exceeds the max, the wallet refuses to sign
|
|
257
|
+
|
|
258
|
+
See the [main repo](https://github.com/open-biz/trustgate) for the policy executable and configuration.
|
|
259
|
+
|
|
260
|
+
## Network
|
|
261
|
+
|
|
262
|
+
Currently supports **Base Sepolia** (testnet). Mainnet support planned.
|
|
263
|
+
|
|
264
|
+
## Academic References
|
|
265
|
+
|
|
266
|
+
The trust scoring formula is based on peer-reviewed research:
|
|
267
|
+
- EigenTrust (Kamvar, Schlosser & Garcia-Molina, 2003) — reputation through consistent behavior
|
|
268
|
+
- PeerTrust (Xiong & Liu, 2004) — multi-dimensional behavioral context
|
|
269
|
+
- EigenTrust++ (Fan et al., 2012) — attack-resilient trust management
|
|
270
|
+
|
|
271
|
+
## Built for
|
|
272
|
+
|
|
273
|
+
AgentKit Hackathon by World, Coinbase & XMTP
|
|
274
|
+
Extended with Open Wallet Standard integration for the OWS Hackathon
|
|
275
|
+
|
|
276
|
+
[npm](https://npmjs.com/package/trustgate-middleware) · [GitHub](https://github.com/open-biz/trustgate) · [Live Demo](https://trustgate-hackathon-production.up.railway.app)
|