x402-trust-guard 0.2.0 → 0.4.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 +9 -2
- package/index.d.ts +9 -1
- package/index.js +8 -5
- package/package.json +1 -1
- package/proofs.js +65 -31
- package/verdict.js +4 -1
package/README.md
CHANGED
|
@@ -23,9 +23,16 @@ about the endpoint and the address it wants paid, and **stops the payment** when
|
|
|
23
23
|
It also reads the seller's payout proof straight from DNS (`_x402-payto` TXT records), so an address swapped
|
|
24
24
|
on a compromised server is caught even between the board's refreshes, without asking anyone. The lookup is
|
|
25
25
|
cached by the record's TTL (at most 24 hours) and only repeated live when the quoted address isn't in the
|
|
26
|
-
cached set, so the normal path adds no DNS round trip.
|
|
27
|
-
|
|
26
|
+
cached set, so the normal path adds no DNS round trip. An address that isn't in the seller's DNS
|
|
27
|
+
proof is blocked. One that is, but is new to the seller's record (listed in the last 72 hours), is blocked too
|
|
28
|
+
by default: a domain takeover looks exactly like a wallet change at first, so new payout addresses wait out a
|
|
29
|
+
hold (`onNewPayout: "allow"` turns that into `caution`). Sellers who list a new address first and switch after
|
|
30
|
+
the hold never hit it. If DNS can't be reached, it never blocks
|
|
28
31
|
on that alone. [Cache policy](https://github.com/benmcalillycreative/x402-trust-board/blob/main/docs/payout-proof-spec.md#61-caching).
|
|
32
|
+
The result says what the DNS check found in the spec's terms: `dns_outcome` is `verified`, `mismatch`,
|
|
33
|
+
`malformed`, `absent` or `unchecked`, with the proof's `dns_signature` (`valid`, `invalid`, `unverifiable`
|
|
34
|
+
or `none`) and `dns_issued` date when it's verified. Only `mismatch` blocks.
|
|
35
|
+
[Outcomes](https://github.com/benmcalillycreative/x402-trust-board/blob/main/docs/payout-proof-spec.md#62-outcomes).
|
|
29
36
|
|
|
30
37
|
Price problems (asking well above the listing), flaky uptime, low grades and thin history come back as `caution`,
|
|
31
38
|
which you can choose to block too (`onCaution: "block"`).
|
package/index.d.ts
CHANGED
|
@@ -6,10 +6,16 @@ export interface CheckResult {
|
|
|
6
6
|
reasons: string[];
|
|
7
7
|
endpoint: Record<string, unknown> | null;
|
|
8
8
|
seller: Record<string, unknown> | null;
|
|
9
|
-
pay_to: { address: string; proven_by_seller: string | null; matches_listing: boolean | null; contradicts_seller_proof: boolean } | null;
|
|
9
|
+
pay_to: { address: string; proven_by_seller: string | null; matches_listing: boolean | null; contradicts_seller_proof: boolean; on_hold?: boolean } | null;
|
|
10
10
|
board: string;
|
|
11
11
|
/** Present when the seller's DNS payout proof was read live: none | match | mismatch | error. */
|
|
12
12
|
dns_live?: "none" | "match" | "mismatch" | "error";
|
|
13
|
+
/** The live DNS check in the spec's terms (payout-proof spec, section 6.2). */
|
|
14
|
+
dns_outcome?: "verified" | "mismatch" | "malformed" | "absent" | "unchecked" | null;
|
|
15
|
+
/** Signature status of the DNS proof that listed this address (only with dns_outcome "verified"). */
|
|
16
|
+
dns_signature?: "valid" | "invalid" | "unverifiable" | "none";
|
|
17
|
+
/** The proof's Issued date (reported, never used to soften a mismatch). */
|
|
18
|
+
dns_issued?: string;
|
|
13
19
|
}
|
|
14
20
|
export interface GuardOptions {
|
|
15
21
|
mode?: "paid" | "free";
|
|
@@ -21,6 +27,8 @@ export interface GuardOptions {
|
|
|
21
27
|
dnsProofs?: boolean;
|
|
22
28
|
/** When the seller's DNS proof doesn't list the quoted address. Default "block". */
|
|
23
29
|
onProofMismatch?: "block" | "allow";
|
|
30
|
+
/** A new address inside the 72-hour hold (it is in the seller's proof, but recently). Default "block"; "allow" makes it "caution". */
|
|
31
|
+
onNewPayout?: "block" | "allow";
|
|
24
32
|
/** DNS-over-HTTPS JSON endpoint. Default Cloudflare. */
|
|
25
33
|
doh?: string;
|
|
26
34
|
onResult?: (result: CheckResult | { url: string; verdict: "error"; reasons: string[] }) => void;
|
package/index.js
CHANGED
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
// board's refreshes. That lookup is cached by the record's TTL (at most 24 hours) and only repeated
|
|
15
15
|
// live when the quoted address isn't in the cached set: no DNS round trip on the normal path.
|
|
16
16
|
import { parseTarget, verdict } from "./verdict.js";
|
|
17
|
-
import { ProofCache, reconcile
|
|
17
|
+
import { ProofCache, reconcile } from "./proofs.js";
|
|
18
18
|
|
|
19
19
|
export const DEFAULT_API = "https://api.settlewatch.com";
|
|
20
20
|
export const DEFAULT_BOARD = "https://settlewatch.com/";
|
|
@@ -41,6 +41,8 @@ export class PaymentBlocked extends Error {
|
|
|
41
41
|
* @param {boolean} [opts.dnsProofs=true] check the seller's DNS payout proof live (cached by its TTL)
|
|
42
42
|
* @param {"block"|"allow"} [opts.onProofMismatch="block"] the seller's DNS proof doesn't list the address
|
|
43
43
|
* (use "allow" only for sellers that quote a fresh address for every payment)
|
|
44
|
+
* @param {"block"|"allow"} [opts.onNewPayout="block"] the address is in the seller's proof but new, inside the
|
|
45
|
+
* 72-hour hold (spec section 7). "allow" treats it as "caution" instead, so onCaution decides
|
|
44
46
|
* @param {string} [opts.doh] DNS-over-HTTPS JSON endpoint (default Cloudflare)
|
|
45
47
|
* @param {(result: object) => void} [opts.onResult] called with every verdict (for logging)
|
|
46
48
|
* @param {typeof fetch} [opts.fetch]
|
|
@@ -50,7 +52,7 @@ export class PaymentBlocked extends Error {
|
|
|
50
52
|
export function withTrustGuard(client, opts = {}) {
|
|
51
53
|
const o = {
|
|
52
54
|
mode: "paid", onCaution: "allow", onUnknown: "allow", onError: "allow", cacheSeconds: 86400,
|
|
53
|
-
dnsProofs: true, onProofMismatch: "block",
|
|
55
|
+
dnsProofs: true, onProofMismatch: "block", onNewPayout: "block",
|
|
54
56
|
api: DEFAULT_API, board: DEFAULT_BOARD, fetch: globalThis.fetch, ...opts,
|
|
55
57
|
};
|
|
56
58
|
const cache = new Map();
|
|
@@ -64,10 +66,10 @@ export function withTrustGuard(client, opts = {}) {
|
|
|
64
66
|
if (!res.ok) throw new Error(`board returned HTTP ${res.status}`);
|
|
65
67
|
const seller = await res.json();
|
|
66
68
|
const result = verdict(target, seller, payTo, o.board);
|
|
67
|
-
// The board's file says the address contradicts the seller's proof
|
|
68
|
-
if (o.dnsProofs && result.pay_to?.contradicts_seller_proof) {
|
|
69
|
+
// The board's file says the address contradicts the seller's proof, or holds it as new: ask DNS live.
|
|
70
|
+
if (o.dnsProofs && (result.pay_to?.contradicts_seller_proof || seller.payout?.held?.length)) {
|
|
69
71
|
const live = await proofs.check(target.host, payTo, { fresh: true });
|
|
70
|
-
return reconcile(result, live
|
|
72
|
+
return reconcile(result, live);
|
|
71
73
|
}
|
|
72
74
|
return result;
|
|
73
75
|
}
|
|
@@ -114,6 +116,7 @@ export function withTrustGuard(client, opts = {}) {
|
|
|
114
116
|
}
|
|
115
117
|
if (!result) return;
|
|
116
118
|
if (live && live.status !== "mismatch") result = reconcile(result, live);
|
|
119
|
+
if (result.pay_to?.on_hold && result.verdict === "avoid" && o.onNewPayout === "allow") result = { ...result, verdict: "caution" };
|
|
117
120
|
o.onResult?.(result);
|
|
118
121
|
const block =
|
|
119
122
|
result.verdict === "avoid" ||
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "x402-trust-guard",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Check an x402 seller before your agent pays it: blocks switched or burn wallets, big overcharges and payout addresses the seller never proved. One line on top of @x402/fetch.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "index.js",
|
package/proofs.js
CHANGED
|
@@ -50,13 +50,17 @@ export function joinTxt(data) {
|
|
|
50
50
|
return parts.length ? parts.join("") : String(data);
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
+
const ADDRESS = /^(0x[0-9a-fA-F]{40}|[1-9A-HJ-NP-Za-km-z]{32,44})$/; // EVM, or Solana (base58)
|
|
54
|
+
|
|
55
|
+
/** One TXT record -> a proof, { malformed: true } for an x402payto1 record that can't be used, or null. */
|
|
53
56
|
export function parseRecord(text, domain) {
|
|
54
57
|
const f = {};
|
|
55
58
|
for (const part of text.split(";")) {
|
|
56
59
|
const i = part.indexOf("=");
|
|
57
60
|
if (i > 0) f[part.slice(0, i).trim().toLowerCase()] = part.slice(i + 1).trim();
|
|
58
61
|
}
|
|
59
|
-
if (f.v !== "x402payto1"
|
|
62
|
+
if (f.v !== "x402payto1") return null; // some other TXT record at that name
|
|
63
|
+
if (!f.a || !ADDRESS.test(f.a)) return { malformed: true };
|
|
60
64
|
return { domain, address: f.a, issued: f.d || null, signature: f.s || null };
|
|
61
65
|
}
|
|
62
66
|
|
|
@@ -83,42 +87,59 @@ export class ProofCache {
|
|
|
83
87
|
if (j.Status !== 0 && j.Status !== 3) throw new Error(`DNS lookup failed (status ${j.Status})`);
|
|
84
88
|
const answers = (j.Answer || []).filter((a) => a.type === 16);
|
|
85
89
|
const ttl = Math.min(...answers.map((a) => a.TTL), ...(j.Authority || []).map((a) => a.TTL), MAX_SECONDS);
|
|
86
|
-
|
|
90
|
+
const parsed = answers.map((a) => parseRecord(joinTxt(a.data), name)).filter(Boolean);
|
|
91
|
+
return { records: parsed.filter((r) => !r.malformed), malformed: parsed.some((r) => r.malformed), ttl };
|
|
87
92
|
}
|
|
88
93
|
|
|
89
94
|
async load(host) {
|
|
90
|
-
const proofs = new Map();
|
|
95
|
+
const proofs = new Map(); // address -> level
|
|
96
|
+
const details = new Map(); // address -> { signature, issued } (spec 6.2: signature status rides with the proof)
|
|
91
97
|
let ttl = MAX_SECONDS;
|
|
98
|
+
let malformed = false;
|
|
92
99
|
for (const name of dnsNames(host)) {
|
|
93
100
|
const r = await this.query(name);
|
|
94
101
|
ttl = Math.min(ttl, r.ttl);
|
|
102
|
+
malformed = malformed || r.malformed;
|
|
95
103
|
for (const rec of r.records) {
|
|
96
|
-
let
|
|
104
|
+
let signature = rec.signature ? "unverifiable" : "none";
|
|
97
105
|
if (rec.signature && rec.issued && this.verify) {
|
|
98
106
|
try {
|
|
99
|
-
|
|
107
|
+
const ok = await this.verify(rec.address, message(rec.domain, rec.address, rec.issued), rec.signature);
|
|
108
|
+
// false only when the verifier is sure (it can check contract wallets); null means "can't tell here".
|
|
109
|
+
if (ok === true) signature = "valid";
|
|
110
|
+
else if (ok === false) signature = "invalid";
|
|
100
111
|
} catch { /* unverifiable here; still a DNS proof */ }
|
|
101
112
|
}
|
|
102
113
|
const key = norm(rec.address);
|
|
103
|
-
if (proofs.get(key)
|
|
114
|
+
if (proofs.get(key) === "dns_signed") continue;
|
|
115
|
+
proofs.set(key, signature === "valid" ? "dns_signed" : "dns");
|
|
116
|
+
details.set(key, { signature, issued: rec.issued });
|
|
104
117
|
}
|
|
105
118
|
}
|
|
106
119
|
const now = this.now();
|
|
107
|
-
const entry = { proofs,
|
|
120
|
+
const entry = { proofs, details, malformed, fetchedAt: now,
|
|
121
|
+
until: now + Math.max(MIN_SECONDS, Math.min(ttl, MAX_SECONDS)) * 1000 };
|
|
108
122
|
this.entries.set(norm(host), entry);
|
|
109
123
|
return entry;
|
|
110
124
|
}
|
|
111
125
|
|
|
112
126
|
/**
|
|
113
|
-
* Is payTo in the seller's live DNS proofs?
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
*
|
|
117
|
-
*
|
|
127
|
+
* Is payTo in the seller's live DNS proofs? `outcome` uses the spec's names (section 6.2); `status` is the
|
|
128
|
+
* older field, kept for compatibility.
|
|
129
|
+
* outcome "absent" status "none" no DNS proof (absence proves nothing), or shared hosting
|
|
130
|
+
* outcome "malformed" status "none" an x402payto1 record exists but can't be used; decide as if absent
|
|
131
|
+
* outcome "verified" status "match" it's proven: { level, signature, issued }
|
|
132
|
+
* outcome "mismatch" status "mismatch" proofs exist and this address isn't in them: { proven, rechecked }
|
|
133
|
+
* outcome "unchecked" status "error" DNS couldn't be read; decide on other evidence
|
|
134
|
+
* signature: "valid" | "invalid" | "unverifiable" | "none" (a contract wallet without a chain check is
|
|
135
|
+
* "unverifiable", never "invalid").
|
|
118
136
|
* fresh: true skips the cache (still rate-limited per domain).
|
|
119
137
|
*/
|
|
120
138
|
async check(host, payTo, { fresh = false } = {}) {
|
|
121
|
-
if (!payTo || !dnsNames(host).length) return { status: "none" };
|
|
139
|
+
if (!payTo || !dnsNames(host).length) return { status: "none", outcome: "absent" };
|
|
140
|
+
const none = (e) => ({ status: "none", outcome: e.malformed ? "malformed" : "absent" });
|
|
141
|
+
const match = (e) => ({ status: "match", outcome: "verified", level: e.proofs.get(norm(payTo)),
|
|
142
|
+
...(e.details?.get(norm(payTo)) || {}) });
|
|
122
143
|
const key = norm(host);
|
|
123
144
|
try {
|
|
124
145
|
let entry = this.entries.get(key);
|
|
@@ -128,44 +149,57 @@ export class ProofCache {
|
|
|
128
149
|
entry = await this.load(host);
|
|
129
150
|
rechecked = true;
|
|
130
151
|
}
|
|
131
|
-
if (!entry.proofs.size) return
|
|
132
|
-
if (entry.proofs.has(norm(payTo))) return
|
|
152
|
+
if (!entry.proofs.size) return none(entry);
|
|
153
|
+
if (entry.proofs.has(norm(payTo))) return match(entry);
|
|
133
154
|
if (!rechecked && canRefetch) {
|
|
134
155
|
entry = await this.load(host); // the only path that pays for a live lookup
|
|
135
156
|
rechecked = true;
|
|
136
|
-
if (entry.proofs.has(norm(payTo))) return
|
|
137
|
-
if (!entry.proofs.size) return
|
|
157
|
+
if (entry.proofs.has(norm(payTo))) return match(entry);
|
|
158
|
+
if (!entry.proofs.size) return none(entry);
|
|
138
159
|
}
|
|
139
|
-
return { status: "mismatch", proven: [...entry.proofs.keys()], rechecked };
|
|
160
|
+
return { status: "mismatch", outcome: "mismatch", proven: [...entry.proofs.keys()], rechecked };
|
|
140
161
|
} catch (e) {
|
|
141
|
-
return { status: "error", error: String(e.message || e) };
|
|
162
|
+
return { status: "error", outcome: "unchecked", error: String(e.message || e) };
|
|
142
163
|
}
|
|
143
164
|
}
|
|
144
165
|
}
|
|
145
166
|
|
|
146
167
|
const PROOF_REASON = "The address you're about to pay isn't in the payout proof this seller published.";
|
|
147
|
-
const
|
|
168
|
+
const HELD = "The seller's DNS proof lists this address, but it's new: payout addresses count only after a 72-hour hold " +
|
|
169
|
+
"(a domain takeover looks exactly like a wallet change at first). If the seller changed wallets on purpose, it clears by itself.";
|
|
148
170
|
|
|
149
|
-
/** The board's seller data with one more proof
|
|
171
|
+
/** The board's seller data with one more proof (kept for callers that rebuild a verdict). */
|
|
150
172
|
export const withProof = (seller, proof) =>
|
|
151
173
|
seller ? { ...seller, payout: { ...(seller.payout || {}), proofs: [...(seller.payout?.proofs || []), proof] } } : seller;
|
|
152
174
|
|
|
175
|
+
/** The spec-named fields of a live check, for the result (spec 6.2). */
|
|
176
|
+
const liveFields = (live) => ({
|
|
177
|
+
dns_outcome: live.outcome || null,
|
|
178
|
+
...(live.signature ? { dns_signature: live.signature } : {}),
|
|
179
|
+
...(live.issued ? { dns_issued: live.issued } : {}),
|
|
180
|
+
});
|
|
181
|
+
|
|
153
182
|
/**
|
|
154
183
|
* Combine a verdict built from the board's data (refreshed every few hours) with a live DNS check.
|
|
155
|
-
* - The
|
|
156
|
-
* the
|
|
157
|
-
*
|
|
184
|
+
* - The live proof lists the address, but the board's record of this seller doesn't (yet), or holds it as new:
|
|
185
|
+
* the address is inside the hold for new payout addresses (spec section 7), so "avoid", with
|
|
186
|
+
* pay_to.on_hold set. A seller who lists the new address first and waits out the hold never gets here.
|
|
158
187
|
* - The board saw nothing wrong, but the live DNS proof doesn't list the address: "avoid".
|
|
188
|
+
* The third argument is accepted and ignored (older callers passed a verdict rebuilder).
|
|
159
189
|
*/
|
|
160
|
-
export function reconcile(result, live
|
|
190
|
+
export function reconcile(result, live) {
|
|
161
191
|
if (!result || !result.pay_to || !live) return result;
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
192
|
+
const out = { ...result, reasons: [...(result.reasons || [])], pay_to: { ...result.pay_to }, dns_live: live.status,
|
|
193
|
+
...liveFields(live) };
|
|
194
|
+
const held = (result.seller?.payout?.held || []).map(norm).includes(norm(result.pay_to.address));
|
|
195
|
+
if (live.status === "match" && (result.pay_to.contradicts_seller_proof || held)) {
|
|
196
|
+
out.pay_to.contradicts_seller_proof = true;
|
|
197
|
+
out.pay_to.on_hold = true;
|
|
198
|
+
out.reasons = out.reasons.filter((r) => r !== PROOF_REASON);
|
|
199
|
+
if (!out.reasons.includes(HELD)) out.reasons.push(HELD);
|
|
200
|
+
out.verdict = "avoid";
|
|
201
|
+
return out;
|
|
167
202
|
}
|
|
168
|
-
const out = { ...result, reasons: [...(result.reasons || [])], pay_to: { ...result.pay_to }, dns_live: live.status };
|
|
169
203
|
if (live.status === "match" && !out.pay_to.proven_by_seller) out.pay_to.proven_by_seller = live.level;
|
|
170
204
|
if (live.status === "mismatch") {
|
|
171
205
|
out.pay_to.contradicts_seller_proof = true;
|
package/verdict.js
CHANGED
|
@@ -36,7 +36,10 @@ const norm = (a) => (a || "").trim().toLowerCase();
|
|
|
36
36
|
/** Is the address the agent is about to pay one the seller has proven is theirs? */
|
|
37
37
|
export function payToCheck(seller, endpoint, payTo) {
|
|
38
38
|
if (!payTo) return null;
|
|
39
|
-
const
|
|
39
|
+
const all = seller?.payout?.proofs || [];
|
|
40
|
+
// DNS decides when it exists: a hijacked web server can rewrite its own proof file, not the DNS.
|
|
41
|
+
const proofs = all.some((p) => String(p.level || "").startsWith("dns"))
|
|
42
|
+
? all.filter((p) => String(p.level || "").startsWith("dns")) : all;
|
|
40
43
|
const proof = proofs.find((p) => norm(p.address) === norm(payTo));
|
|
41
44
|
const strong = proofs.some((p) => p.level !== "manifest");
|
|
42
45
|
return {
|