@fiderail/guard 0.0.0-stage → 2.3.2
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 +55 -2
- package/dist/adapters.d.ts +82 -0
- package/dist/adapters.js +263 -0
- package/dist/index.d.ts +128 -0
- package/dist/index.js +870 -0
- package/dist/mandate.d.ts +66 -0
- package/dist/mandate.js +228 -0
- package/dist/sqlite-ledger.d.ts +65 -0
- package/dist/sqlite-ledger.js +410 -0
- package/package.json +55 -4
|
@@ -0,0 +1,410 @@
|
|
|
1
|
+
import crypto from "node:crypto";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { DatabaseSync } from "node:sqlite";
|
|
5
|
+
import { normalizeChainAddress, normalizeChainName } from "@fiderail/spec";
|
|
6
|
+
function getUtcDayKey(nowDate = new Date()) {
|
|
7
|
+
return nowDate.toISOString().slice(0, 10);
|
|
8
|
+
}
|
|
9
|
+
function buildTrustCacheKey(chain, address) {
|
|
10
|
+
const normChain = normalizeChainName(chain);
|
|
11
|
+
if (normChain) {
|
|
12
|
+
return `${normChain}:${normalizeChainAddress(normChain, address)}`;
|
|
13
|
+
}
|
|
14
|
+
return `${String(chain || "").trim().toLowerCase()}:${String(address || "").trim()}`;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Concurrent-safe embedded SQLite WAL edge ledger (zero npm deps; uses Node 22+ `node:sqlite`).
|
|
18
|
+
* Uses `BEGIN IMMEDIATE` write locks so concurrent Promise.all bursts AND multiple separate
|
|
19
|
+
* agent processes sharing the same `.db` file cannot overspend `maxDailyUsdc`.
|
|
20
|
+
*/
|
|
21
|
+
export function createSqliteSpendLedger(dbPath = ":memory:") {
|
|
22
|
+
const isMemory = !dbPath || dbPath === ":memory:";
|
|
23
|
+
if (!isMemory) {
|
|
24
|
+
fs.mkdirSync(path.dirname(path.resolve(dbPath)), { recursive: true });
|
|
25
|
+
}
|
|
26
|
+
const db = new DatabaseSync(dbPath);
|
|
27
|
+
db.exec(`
|
|
28
|
+
PRAGMA journal_mode = WAL;
|
|
29
|
+
PRAGMA synchronous = NORMAL;
|
|
30
|
+
PRAGMA busy_timeout = 5000;
|
|
31
|
+
|
|
32
|
+
CREATE TABLE IF NOT EXISTS daily_spend (
|
|
33
|
+
day TEXT PRIMARY KEY,
|
|
34
|
+
spent_usdc REAL NOT NULL DEFAULT 0,
|
|
35
|
+
tx_count INTEGER NOT NULL DEFAULT 0,
|
|
36
|
+
updated_at TEXT NOT NULL
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
CREATE TABLE IF NOT EXISTS pending_reservations (
|
|
40
|
+
reservation_id TEXT PRIMARY KEY,
|
|
41
|
+
day TEXT NOT NULL,
|
|
42
|
+
amount_usdc REAL NOT NULL,
|
|
43
|
+
created_at_ms INTEGER NOT NULL
|
|
44
|
+
);
|
|
45
|
+
CREATE INDEX IF NOT EXISTS idx_res_day ON pending_reservations(day);
|
|
46
|
+
|
|
47
|
+
CREATE TABLE IF NOT EXISTS recent_payments (
|
|
48
|
+
fingerprint TEXT PRIMARY KEY,
|
|
49
|
+
last_paid_at_ms INTEGER NOT NULL,
|
|
50
|
+
count INTEGER NOT NULL DEFAULT 1
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
CREATE TABLE IF NOT EXISTS recoverable_proofs (
|
|
54
|
+
fingerprint TEXT PRIMARY KEY,
|
|
55
|
+
proof_value TEXT NOT NULL,
|
|
56
|
+
quote_digest TEXT NOT NULL,
|
|
57
|
+
expires_at_ms INTEGER NOT NULL
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
CREATE TABLE IF NOT EXISTS endpoint_health (
|
|
61
|
+
endpoint_key TEXT PRIMARY KEY,
|
|
62
|
+
consecutive_failures INTEGER NOT NULL DEFAULT 0,
|
|
63
|
+
last_failure_at_ms INTEGER,
|
|
64
|
+
last_status INTEGER,
|
|
65
|
+
circuit_open_until_ms INTEGER NOT NULL DEFAULT 0
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
CREATE TABLE IF NOT EXISTS mandate_spend (
|
|
69
|
+
mandate_id TEXT PRIMARY KEY,
|
|
70
|
+
spent_usd REAL NOT NULL DEFAULT 0,
|
|
71
|
+
tx_count INTEGER NOT NULL DEFAULT 0,
|
|
72
|
+
updated_at TEXT NOT NULL
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
CREATE TABLE IF NOT EXISTS revoked_mandates (
|
|
76
|
+
mandate_id TEXT PRIMARY KEY,
|
|
77
|
+
reason TEXT,
|
|
78
|
+
revoked_at TEXT NOT NULL
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
CREATE TABLE IF NOT EXISTS trust_cache (
|
|
82
|
+
cache_key TEXT PRIMARY KEY,
|
|
83
|
+
profile_json TEXT NOT NULL,
|
|
84
|
+
expires_at_ms INTEGER NOT NULL,
|
|
85
|
+
updated_at_ms INTEGER NOT NULL
|
|
86
|
+
);
|
|
87
|
+
`);
|
|
88
|
+
function runImmediate(fn) {
|
|
89
|
+
db.exec("BEGIN IMMEDIATE");
|
|
90
|
+
try {
|
|
91
|
+
const res = fn();
|
|
92
|
+
db.exec("COMMIT");
|
|
93
|
+
return res;
|
|
94
|
+
}
|
|
95
|
+
catch (err) {
|
|
96
|
+
try {
|
|
97
|
+
db.exec("ROLLBACK");
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
// ignore rollback error
|
|
101
|
+
}
|
|
102
|
+
throw err;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
const stmtGetDaily = db.prepare(`SELECT spent_usdc, tx_count FROM daily_spend WHERE day = ?`);
|
|
106
|
+
const stmtUpsertDaily = db.prepare(`
|
|
107
|
+
INSERT INTO daily_spend (day, spent_usdc, tx_count, updated_at)
|
|
108
|
+
VALUES (?, ?, 1, ?)
|
|
109
|
+
ON CONFLICT (day) DO UPDATE SET
|
|
110
|
+
spent_usdc = ROUND(daily_spend.spent_usdc + excluded.spent_usdc, 6),
|
|
111
|
+
tx_count = daily_spend.tx_count + 1,
|
|
112
|
+
updated_at = excluded.updated_at
|
|
113
|
+
`);
|
|
114
|
+
const stmtPruneExpiredRes = db.prepare(`DELETE FROM pending_reservations WHERE created_at_ms < ?`);
|
|
115
|
+
const stmtSumRes = db.prepare(`SELECT COALESCE(SUM(amount_usdc), 0) AS total FROM pending_reservations WHERE day = ?`);
|
|
116
|
+
const stmtInsertRes = db.prepare(`INSERT INTO pending_reservations (reservation_id, day, amount_usdc, created_at_ms) VALUES (?, ?, ?, ?)`);
|
|
117
|
+
const stmtGetRes = db.prepare(`SELECT day, amount_usdc FROM pending_reservations WHERE reservation_id = ?`);
|
|
118
|
+
const stmtDeleteRes = db.prepare(`DELETE FROM pending_reservations WHERE reservation_id = ?`);
|
|
119
|
+
const stmtUpsertProof = db.prepare(`
|
|
120
|
+
INSERT OR REPLACE INTO recoverable_proofs (fingerprint, proof_value, quote_digest, expires_at_ms)
|
|
121
|
+
VALUES (?, ?, ?, ?)
|
|
122
|
+
`);
|
|
123
|
+
const stmtGetProof = db.prepare(`SELECT * FROM recoverable_proofs WHERE fingerprint = ?`);
|
|
124
|
+
const stmtDeleteProof = db.prepare(`DELETE FROM recoverable_proofs WHERE fingerprint = ?`);
|
|
125
|
+
const stmtGetIdemp = db.prepare(`SELECT last_paid_at_ms, count FROM recent_payments WHERE fingerprint = ?`);
|
|
126
|
+
const stmtPruneIdemp = db.prepare(`DELETE FROM recent_payments WHERE last_paid_at_ms < ?`);
|
|
127
|
+
const stmtUpsertIdemp = db.prepare(`
|
|
128
|
+
INSERT INTO recent_payments (fingerprint, last_paid_at_ms, count)
|
|
129
|
+
VALUES (?, ?, 1)
|
|
130
|
+
ON CONFLICT (fingerprint) DO UPDATE SET
|
|
131
|
+
last_paid_at_ms = excluded.last_paid_at_ms,
|
|
132
|
+
count = recent_payments.count + 1
|
|
133
|
+
`);
|
|
134
|
+
const stmtGetHealth = db.prepare(`SELECT * FROM endpoint_health WHERE endpoint_key = ?`);
|
|
135
|
+
const stmtUpsertHealth = db.prepare(`
|
|
136
|
+
INSERT OR REPLACE INTO endpoint_health (
|
|
137
|
+
endpoint_key, consecutive_failures, last_failure_at_ms, last_status, circuit_open_until_ms
|
|
138
|
+
) VALUES (?, ?, ?, ?, ?)
|
|
139
|
+
`);
|
|
140
|
+
const stmtDeleteHealth = db.prepare(`DELETE FROM endpoint_health WHERE endpoint_key = ?`);
|
|
141
|
+
const stmtGetMandateSpend = db.prepare(`SELECT spent_usd FROM mandate_spend WHERE mandate_id = ?`);
|
|
142
|
+
const stmtUpsertMandateSpend = db.prepare(`
|
|
143
|
+
INSERT INTO mandate_spend (mandate_id, spent_usd, tx_count, updated_at)
|
|
144
|
+
VALUES (?, ?, 1, ?)
|
|
145
|
+
ON CONFLICT (mandate_id) DO UPDATE SET
|
|
146
|
+
spent_usd = ROUND(mandate_spend.spent_usd + excluded.spent_usd, 6),
|
|
147
|
+
tx_count = mandate_spend.tx_count + 1,
|
|
148
|
+
updated_at = excluded.updated_at
|
|
149
|
+
`);
|
|
150
|
+
const stmtRevokeMandate = db.prepare(`
|
|
151
|
+
INSERT OR REPLACE INTO revoked_mandates (mandate_id, reason, revoked_at) VALUES (?, ?, ?)
|
|
152
|
+
`);
|
|
153
|
+
const stmtCheckRevoked = db.prepare(`SELECT 1 AS ok FROM revoked_mandates WHERE mandate_id = ?`);
|
|
154
|
+
const stmtListRevoked = db.prepare(`SELECT mandate_id FROM revoked_mandates`);
|
|
155
|
+
const stmtGetCache = db.prepare(`SELECT profile_json, expires_at_ms FROM trust_cache WHERE cache_key = ?`);
|
|
156
|
+
const stmtSetCache = db.prepare(`
|
|
157
|
+
INSERT OR REPLACE INTO trust_cache (cache_key, profile_json, expires_at_ms, updated_at_ms)
|
|
158
|
+
VALUES (?, ?, ?, ?)
|
|
159
|
+
`);
|
|
160
|
+
const stmtDeleteCacheKey = db.prepare(`DELETE FROM trust_cache WHERE cache_key = ?`);
|
|
161
|
+
const stmtClearCache = db.prepare(`DELETE FROM trust_cache`);
|
|
162
|
+
function getSpentTodayUsdc(nowDate = new Date()) {
|
|
163
|
+
const row = stmtGetDaily.get(getUtcDayKey(nowDate));
|
|
164
|
+
return Number((row?.spent_usdc || 0).toFixed(6));
|
|
165
|
+
}
|
|
166
|
+
function getPendingReservedTodayUsdc(nowDate = new Date(), nowMs = Date.now()) {
|
|
167
|
+
stmtPruneExpiredRes.run(nowMs - 120000);
|
|
168
|
+
const row = stmtSumRes.get(getUtcDayKey(nowDate));
|
|
169
|
+
return Number((row?.total || 0).toFixed(6));
|
|
170
|
+
}
|
|
171
|
+
function getEffectiveSpentTodayUsdc(nowDate = new Date()) {
|
|
172
|
+
return Number((getSpentTodayUsdc(nowDate) + getPendingReservedTodayUsdc(nowDate)).toFixed(6));
|
|
173
|
+
}
|
|
174
|
+
function getTxCountToday(nowDate = new Date()) {
|
|
175
|
+
const row = stmtGetDaily.get(getUtcDayKey(nowDate));
|
|
176
|
+
return Number(row?.tx_count || 0);
|
|
177
|
+
}
|
|
178
|
+
function recordSpend(amountUsdc, nowDate = new Date()) {
|
|
179
|
+
const amt = Number(amountUsdc);
|
|
180
|
+
if (!Number.isFinite(amt) || amt <= 0)
|
|
181
|
+
return getSpentTodayUsdc(nowDate);
|
|
182
|
+
const day = getUtcDayKey(nowDate);
|
|
183
|
+
return runImmediate(() => {
|
|
184
|
+
stmtUpsertDaily.run(day, Number(amt.toFixed(6)), nowDate.toISOString());
|
|
185
|
+
const row = stmtGetDaily.get(day);
|
|
186
|
+
return Number((row?.spent_usdc || 0).toFixed(6));
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
function reserveBudget(amountUsdc, maxDailyUsdc, nowDate = new Date()) {
|
|
190
|
+
const amt = Number(amountUsdc);
|
|
191
|
+
const day = getUtcDayKey(nowDate);
|
|
192
|
+
const nowMs = Date.now();
|
|
193
|
+
if (!Number.isFinite(amt) || amt <= 0) {
|
|
194
|
+
return {
|
|
195
|
+
ok: false,
|
|
196
|
+
reservationId: null,
|
|
197
|
+
effectiveSpentUsdc: getEffectiveSpentTodayUsdc(nowDate),
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
return runImmediate(() => {
|
|
201
|
+
stmtPruneExpiredRes.run(nowMs - 120000);
|
|
202
|
+
const spentRow = stmtGetDaily.get(day);
|
|
203
|
+
const resRow = stmtSumRes.get(day);
|
|
204
|
+
const effective = Number(((spentRow?.spent_usdc || 0) + (resRow?.total || 0)).toFixed(6));
|
|
205
|
+
const cap = Number(maxDailyUsdc);
|
|
206
|
+
if (Number.isFinite(cap) && effective + amt > cap + 1e-9) {
|
|
207
|
+
return { ok: false, reservationId: null, effectiveSpentUsdc: effective };
|
|
208
|
+
}
|
|
209
|
+
const reservationId = `sres_${crypto.randomBytes(6).toString("hex")}_${nowMs}`;
|
|
210
|
+
stmtInsertRes.run(reservationId, day, Number(amt.toFixed(6)), nowMs);
|
|
211
|
+
return {
|
|
212
|
+
ok: true,
|
|
213
|
+
reservationId,
|
|
214
|
+
effectiveSpentUsdc: Number((effective + amt).toFixed(6)),
|
|
215
|
+
};
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
function commitReservation(reservationId, nowDate = new Date()) {
|
|
219
|
+
const day = getUtcDayKey(nowDate);
|
|
220
|
+
return runImmediate(() => {
|
|
221
|
+
const rec = stmtGetRes.get(reservationId);
|
|
222
|
+
if (!rec) {
|
|
223
|
+
const spentRow = stmtGetDaily.get(day);
|
|
224
|
+
return Number((spentRow?.spent_usdc || 0).toFixed(6));
|
|
225
|
+
}
|
|
226
|
+
stmtDeleteRes.run(reservationId);
|
|
227
|
+
stmtUpsertDaily.run(rec.day, Number(rec.amount_usdc.toFixed(6)), nowDate.toISOString());
|
|
228
|
+
const spentRow = stmtGetDaily.get(rec.day);
|
|
229
|
+
return Number((spentRow?.spent_usdc || 0).toFixed(6));
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
function releaseReservation(reservationId) {
|
|
233
|
+
if (!reservationId)
|
|
234
|
+
return false;
|
|
235
|
+
const info = stmtDeleteRes.run(reservationId);
|
|
236
|
+
return Number(info.changes || 0) > 0;
|
|
237
|
+
}
|
|
238
|
+
function recordRecoverableProof(fingerprint, { proofValue, quoteDigest, ttlMs = 60000, nowMs = Date.now(), }) {
|
|
239
|
+
if (!fingerprint || !proofValue)
|
|
240
|
+
return;
|
|
241
|
+
stmtUpsertProof.run(fingerprint, proofValue, quoteDigest, nowMs + Math.max(1000, Number(ttlMs) || 60000));
|
|
242
|
+
}
|
|
243
|
+
function getRecoverableProof(fingerprint, nowMs = Date.now()) {
|
|
244
|
+
const row = stmtGetProof.get(fingerprint);
|
|
245
|
+
if (!row)
|
|
246
|
+
return null;
|
|
247
|
+
if (Number(row.expires_at_ms) <= nowMs) {
|
|
248
|
+
stmtDeleteProof.run(fingerprint);
|
|
249
|
+
return null;
|
|
250
|
+
}
|
|
251
|
+
return {
|
|
252
|
+
proofValue: row.proof_value,
|
|
253
|
+
quoteDigest: row.quote_digest,
|
|
254
|
+
expiresAtMs: Number(row.expires_at_ms),
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
function clearRecoverableProof(fingerprint) {
|
|
258
|
+
if (!fingerprint)
|
|
259
|
+
return;
|
|
260
|
+
stmtDeleteProof.run(fingerprint);
|
|
261
|
+
}
|
|
262
|
+
function checkIdempotency(fingerprint, windowMs = 60000, nowMs = Date.now()) {
|
|
263
|
+
if (!fingerprint || windowMs <= 0) {
|
|
264
|
+
return { isDuplicate: false, secondsSinceLast: null };
|
|
265
|
+
}
|
|
266
|
+
stmtPruneIdemp.run(nowMs - Math.max(windowMs * 2, 300000));
|
|
267
|
+
const existing = stmtGetIdemp.get(fingerprint);
|
|
268
|
+
if (!existing)
|
|
269
|
+
return { isDuplicate: false, secondsSinceLast: null };
|
|
270
|
+
const elapsedMs = nowMs - Number(existing.last_paid_at_ms || 0);
|
|
271
|
+
if (elapsedMs >= 0 && elapsedMs < windowMs) {
|
|
272
|
+
return {
|
|
273
|
+
isDuplicate: true,
|
|
274
|
+
secondsSinceLast: Number((elapsedMs / 1000).toFixed(2)),
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
return { isDuplicate: false, secondsSinceLast: null };
|
|
278
|
+
}
|
|
279
|
+
function recordPaymentFingerprint(fingerprint, nowMs = Date.now()) {
|
|
280
|
+
if (!fingerprint)
|
|
281
|
+
return;
|
|
282
|
+
stmtUpsertIdemp.run(fingerprint, nowMs);
|
|
283
|
+
}
|
|
284
|
+
function checkCircuitBreaker(endpointKey, nowMs = Date.now()) {
|
|
285
|
+
const rec = stmtGetHealth.get(endpointKey);
|
|
286
|
+
if (!rec) {
|
|
287
|
+
return { open: false, retryAfterSec: 0, consecutiveFailures: 0, lastStatus: null };
|
|
288
|
+
}
|
|
289
|
+
const openUntil = Number(rec.circuit_open_until_ms || 0);
|
|
290
|
+
if (openUntil > nowMs) {
|
|
291
|
+
return {
|
|
292
|
+
open: true,
|
|
293
|
+
retryAfterSec: Math.max(1, Math.ceil((openUntil - nowMs) / 1000)),
|
|
294
|
+
consecutiveFailures: Number(rec.consecutive_failures || 0),
|
|
295
|
+
lastStatus: rec.last_status ?? null,
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
if (openUntil > 0 && openUntil <= nowMs) {
|
|
299
|
+
stmtUpsertHealth.run(endpointKey, 0, rec.last_failure_at_ms, rec.last_status, 0);
|
|
300
|
+
}
|
|
301
|
+
return {
|
|
302
|
+
open: false,
|
|
303
|
+
retryAfterSec: 0,
|
|
304
|
+
consecutiveFailures: openUntil > 0 && openUntil <= nowMs ? 0 : Number(rec.consecutive_failures || 0),
|
|
305
|
+
lastStatus: rec.last_status ?? null,
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
function recordEndpointFailure(endpointKey, { status = null, maxConsecutiveFailures = 3, cooldownMs = 600000, nowMs = Date.now() } = {}) {
|
|
309
|
+
return runImmediate(() => {
|
|
310
|
+
const prev = stmtGetHealth.get(endpointKey);
|
|
311
|
+
const consecutiveFailures = Number(prev?.consecutive_failures || 0) + 1;
|
|
312
|
+
const threshold = Math.max(1, Number(maxConsecutiveFailures) || 3);
|
|
313
|
+
const opened = consecutiveFailures >= threshold;
|
|
314
|
+
const circuitOpenUntilMs = opened ? nowMs + Math.max(1000, Number(cooldownMs) || 600000) : 0;
|
|
315
|
+
stmtUpsertHealth.run(endpointKey, consecutiveFailures, nowMs, status, circuitOpenUntilMs);
|
|
316
|
+
return {
|
|
317
|
+
opened,
|
|
318
|
+
consecutiveFailures,
|
|
319
|
+
retryAfterSec: opened ? Math.ceil((circuitOpenUntilMs - nowMs) / 1000) : 0,
|
|
320
|
+
};
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
function recordEndpointSuccess(endpointKey) {
|
|
324
|
+
stmtDeleteHealth.run(endpointKey);
|
|
325
|
+
}
|
|
326
|
+
function getMandateSpentUsd(mandateId) {
|
|
327
|
+
const row = stmtGetMandateSpend.get(mandateId);
|
|
328
|
+
return Number((row?.spent_usd || 0).toFixed(6));
|
|
329
|
+
}
|
|
330
|
+
function recordMandateSpend(mandateId, amountUsd) {
|
|
331
|
+
const amt = Number(amountUsd);
|
|
332
|
+
if (!mandateId || !Number.isFinite(amt) || amt <= 0)
|
|
333
|
+
return getMandateSpentUsd(mandateId);
|
|
334
|
+
return runImmediate(() => {
|
|
335
|
+
stmtUpsertMandateSpend.run(mandateId, Number(amt.toFixed(6)), new Date().toISOString());
|
|
336
|
+
const row = stmtGetMandateSpend.get(mandateId);
|
|
337
|
+
return Number((row?.spent_usd || 0).toFixed(6));
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
function revokeMandate(mandateId, reason = "REVOKED") {
|
|
341
|
+
if (!mandateId)
|
|
342
|
+
return;
|
|
343
|
+
stmtRevokeMandate.run(mandateId, reason, new Date().toISOString());
|
|
344
|
+
}
|
|
345
|
+
function isMandateRevoked(mandateId) {
|
|
346
|
+
if (!mandateId)
|
|
347
|
+
return false;
|
|
348
|
+
return Boolean(stmtCheckRevoked.get(mandateId));
|
|
349
|
+
}
|
|
350
|
+
function getRevokedMandateIds() {
|
|
351
|
+
const rows = stmtListRevoked.all();
|
|
352
|
+
return new Set(rows.map((r) => r.mandate_id));
|
|
353
|
+
}
|
|
354
|
+
function getCachedTrust(chain, address, nowMs = Date.now()) {
|
|
355
|
+
const key = buildTrustCacheKey(chain, address);
|
|
356
|
+
const row = stmtGetCache.get(key);
|
|
357
|
+
if (!row)
|
|
358
|
+
return null;
|
|
359
|
+
if (Number(row.expires_at_ms) <= nowMs) {
|
|
360
|
+
stmtDeleteCacheKey.run(key);
|
|
361
|
+
return null;
|
|
362
|
+
}
|
|
363
|
+
try {
|
|
364
|
+
return JSON.parse(row.profile_json);
|
|
365
|
+
}
|
|
366
|
+
catch {
|
|
367
|
+
return null;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
function setCachedTrust(chain, address, profile, ttlMs = 30000, nowMs = Date.now()) {
|
|
371
|
+
if (ttlMs <= 0)
|
|
372
|
+
return;
|
|
373
|
+
const key = buildTrustCacheKey(chain, address);
|
|
374
|
+
stmtSetCache.run(key, JSON.stringify(profile), nowMs + ttlMs, nowMs);
|
|
375
|
+
}
|
|
376
|
+
function clearTrustCache() {
|
|
377
|
+
stmtClearCache.run();
|
|
378
|
+
}
|
|
379
|
+
function close() {
|
|
380
|
+
db.close();
|
|
381
|
+
}
|
|
382
|
+
return {
|
|
383
|
+
getSpentTodayUsdc,
|
|
384
|
+
getPendingReservedTodayUsdc,
|
|
385
|
+
getEffectiveSpentTodayUsdc,
|
|
386
|
+
getTxCountToday,
|
|
387
|
+
recordSpend,
|
|
388
|
+
reserveBudget,
|
|
389
|
+
commitReservation,
|
|
390
|
+
releaseReservation,
|
|
391
|
+
recordRecoverableProof,
|
|
392
|
+
getRecoverableProof,
|
|
393
|
+
clearRecoverableProof,
|
|
394
|
+
checkIdempotency,
|
|
395
|
+
recordPaymentFingerprint,
|
|
396
|
+
checkCircuitBreaker,
|
|
397
|
+
recordEndpointFailure,
|
|
398
|
+
recordEndpointSuccess,
|
|
399
|
+
getMandateSpentUsd,
|
|
400
|
+
recordMandateSpend,
|
|
401
|
+
revokeMandate,
|
|
402
|
+
isMandateRevoked,
|
|
403
|
+
getRevokedMandateIds,
|
|
404
|
+
getCachedTrust,
|
|
405
|
+
setCachedTrust,
|
|
406
|
+
clearTrustCache,
|
|
407
|
+
close,
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
//# sourceMappingURL=sqlite-ledger.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,57 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fiderail/guard",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "2.3.2",
|
|
4
|
+
"description": "Non-custodial buyer-side Guard-and-Pay x402 adapter with atomic budget holds, outbound secret scrubber, quote binding, circuit breaker, and SAR receipts.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"README.md",
|
|
16
|
+
"LICENSE",
|
|
17
|
+
"dist",
|
|
18
|
+
"!dist/**/*.map",
|
|
19
|
+
"!dist/**/*.tsbuildinfo"
|
|
20
|
+
],
|
|
21
|
+
"scripts": {
|
|
22
|
+
"prepublishOnly": "tsc -b"
|
|
23
|
+
},
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public"
|
|
26
|
+
},
|
|
27
|
+
"engines": {
|
|
28
|
+
"node": ">=22.13.0"
|
|
29
|
+
},
|
|
30
|
+
"homepage": "https://fiderail.com",
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "git+https://gitlab.com/fiderail/fiderail.git",
|
|
34
|
+
"directory": "packages/guard"
|
|
35
|
+
},
|
|
36
|
+
"bugs": {
|
|
37
|
+
"url": "https://gitlab.com/fiderail/fiderail/-/issues"
|
|
38
|
+
},
|
|
39
|
+
"keywords": [
|
|
40
|
+
"fiderail",
|
|
41
|
+
"ai-agent",
|
|
42
|
+
"payment-firewall",
|
|
43
|
+
"guard-and-pay",
|
|
44
|
+
"x402",
|
|
45
|
+
"a2a",
|
|
46
|
+
"mcp-pay",
|
|
47
|
+
"session-key",
|
|
48
|
+
"sqlite",
|
|
49
|
+
"capability-mandate"
|
|
50
|
+
],
|
|
51
|
+
"dependencies": {
|
|
52
|
+
"@fiderail/core": "2.3.2",
|
|
53
|
+
"@fiderail/spec": "2.3.2",
|
|
54
|
+
"@fiderail/zk-types": "2.3.2"
|
|
55
|
+
},
|
|
56
|
+
"license": "MIT"
|
|
57
|
+
}
|