@celo/buy-gateway 0.8.4 → 0.8.6
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 +38 -1
- package/dist/config.d.ts +54 -0
- package/dist/config.js +127 -2
- package/dist/config.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/judged.d.ts +60 -0
- package/dist/judged.js +113 -0
- package/dist/judged.js.map +1 -0
- package/dist/server.d.ts +34 -3
- package/dist/server.js +195 -91
- package/dist/server.js.map +1 -1
- package/dist/upstream.d.ts +5 -37
- package/package.json +4 -4
package/dist/judged.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The judged envelope: which paid requests a provider is judged on.
|
|
3
|
+
*
|
|
4
|
+
* A host that writes reputation for a provider (ERC-8004 feedback, a served
|
|
5
|
+
* rate) should judge it only on the work it sells as normal service. A request
|
|
6
|
+
* can be accepted, paid and forwarded and still be heavier than that: a large
|
|
7
|
+
* JSON-RPC batch of traces, or a log query over a range nobody serves quickly.
|
|
8
|
+
* When such a request fails, the buyer asked for it; the provider did not fail
|
|
9
|
+
* at its job. The endpoint's `judged` block declares the envelope, and every
|
|
10
|
+
* completion says whether its request was inside it. Nothing here refuses,
|
|
11
|
+
* prices or refunds anything: an unjudged request is served and paid exactly
|
|
12
|
+
* like a judged one.
|
|
13
|
+
*
|
|
14
|
+
* Total by construction: any input, however odd, yields a judgment and never a
|
|
15
|
+
* throw. Odd input is unjudged, the conservative answer. A reason is a short
|
|
16
|
+
* machine code built from the config and fixed text, never from the caller's
|
|
17
|
+
* input, so it is safe to log and cheap to group by.
|
|
18
|
+
*/
|
|
19
|
+
/** Calls that run the EVM, lower-cased for the case-blind match. */
|
|
20
|
+
const EXECUTION_METHODS = new Set(['eth_call', 'eth_estimategas', 'eth_createaccesslist']);
|
|
21
|
+
const JUDGED = { judged: true };
|
|
22
|
+
const unjudged = (reason) => ({ judged: false, reason });
|
|
23
|
+
/** A block number as eth_getLogs takes it: hex, at most 64 bits. */
|
|
24
|
+
const HEX_QUANTITY = /^0x[0-9a-fA-F]{1,16}$/;
|
|
25
|
+
/**
|
|
26
|
+
* Judge one request body, as parsed by the endpoint's JSON check. `body` is
|
|
27
|
+
* undefined when nothing was parsed (JSON never parses to undefined).
|
|
28
|
+
*/
|
|
29
|
+
export function judgeRequest(envelope, body) {
|
|
30
|
+
if (!envelope)
|
|
31
|
+
return JUDGED;
|
|
32
|
+
if (body === undefined)
|
|
33
|
+
return unjudged('not_json');
|
|
34
|
+
const max = envelope.maxJsonArrayLength;
|
|
35
|
+
if (max !== undefined && Array.isArray(body) && body.length > max) {
|
|
36
|
+
return unjudged(`batch_over_${max}`);
|
|
37
|
+
}
|
|
38
|
+
const rpc = envelope.jsonRpc;
|
|
39
|
+
if (!rpc)
|
|
40
|
+
return JUDGED;
|
|
41
|
+
const calls = Array.isArray(body) ? body : [body];
|
|
42
|
+
if (calls.length === 0)
|
|
43
|
+
return unjudged('malformed_call');
|
|
44
|
+
const maxRange = rpc.maxLogsBlockRange;
|
|
45
|
+
const maxExecution = rpc.maxExecutionCalls;
|
|
46
|
+
let logsBlocks = 0n;
|
|
47
|
+
let executionCalls = 0;
|
|
48
|
+
for (const entry of calls) {
|
|
49
|
+
const judgment = judgeCall(rpc, entry);
|
|
50
|
+
if (!judgment.judged)
|
|
51
|
+
return judgment;
|
|
52
|
+
if (judgment.logsBlocks !== undefined && maxRange !== undefined) {
|
|
53
|
+
logsBlocks += judgment.logsBlocks;
|
|
54
|
+
if (logsBlocks > BigInt(maxRange))
|
|
55
|
+
return unjudged(`logs_range_over_${maxRange}`);
|
|
56
|
+
}
|
|
57
|
+
if (judgment.executes && maxExecution !== undefined) {
|
|
58
|
+
executionCalls += 1;
|
|
59
|
+
if (executionCalls > maxExecution)
|
|
60
|
+
return unjudged(`execution_calls_over_${maxExecution}`);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return JUDGED;
|
|
64
|
+
}
|
|
65
|
+
function judgeCall(rpc, entry) {
|
|
66
|
+
if (!isObject(entry) || typeof entry.method !== 'string')
|
|
67
|
+
return unjudged('malformed_call');
|
|
68
|
+
// Case-blind: a node that matched methods without case would otherwise run
|
|
69
|
+
// an excluded method, or an unbounded eth_getLogs, as judged work.
|
|
70
|
+
const method = entry.method.toLowerCase();
|
|
71
|
+
const excluded = rpc.excludeMethods?.find((pattern) => {
|
|
72
|
+
const p = pattern.toLowerCase();
|
|
73
|
+
return p.endsWith('*') ? method.startsWith(p.slice(0, -1)) : method === p;
|
|
74
|
+
});
|
|
75
|
+
if (excluded !== undefined)
|
|
76
|
+
return unjudged(`method_${excluded}`);
|
|
77
|
+
if (method === 'eth_getlogs' && rpc.maxLogsBlockRange !== undefined) {
|
|
78
|
+
return judgeLogs(entry.params);
|
|
79
|
+
}
|
|
80
|
+
if (EXECUTION_METHODS.has(method))
|
|
81
|
+
return { judged: true, executes: true };
|
|
82
|
+
return JUDGED;
|
|
83
|
+
}
|
|
84
|
+
function judgeLogs(params) {
|
|
85
|
+
if (!Array.isArray(params) || !isObject(params[0]))
|
|
86
|
+
return unjudged('malformed_call');
|
|
87
|
+
const filter = params[0];
|
|
88
|
+
const { blockHash, fromBlock, toBlock } = filter;
|
|
89
|
+
if (blockHash !== undefined) {
|
|
90
|
+
// A hash beside a range is refused by geth and read one way or the other
|
|
91
|
+
// by other nodes: odd input, not judged.
|
|
92
|
+
if (typeof blockHash !== 'string' || fromBlock !== undefined || toBlock !== undefined) {
|
|
93
|
+
return unjudged('malformed_call');
|
|
94
|
+
}
|
|
95
|
+
return { judged: true, logsBlocks: 1n };
|
|
96
|
+
}
|
|
97
|
+
if (typeof fromBlock !== 'string' ||
|
|
98
|
+
typeof toBlock !== 'string' ||
|
|
99
|
+
!HEX_QUANTITY.test(fromBlock) ||
|
|
100
|
+
!HEX_QUANTITY.test(toBlock)) {
|
|
101
|
+
return unjudged('logs_range_unbounded');
|
|
102
|
+
}
|
|
103
|
+
const from = BigInt(fromBlock);
|
|
104
|
+
const to = BigInt(toBlock);
|
|
105
|
+
// The node refuses an inverted range; odd input either way.
|
|
106
|
+
if (to < from)
|
|
107
|
+
return unjudged('malformed_call');
|
|
108
|
+
return { judged: true, logsBlocks: to - from + 1n };
|
|
109
|
+
}
|
|
110
|
+
function isObject(value) {
|
|
111
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
112
|
+
}
|
|
113
|
+
//# sourceMappingURL=judged.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"judged.js","sourceRoot":"","sources":["../src/judged.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAkCH,oEAAoE;AACpE,MAAM,iBAAiB,GAAG,IAAI,GAAG,CAAC,CAAC,UAAU,EAAE,iBAAiB,EAAE,sBAAsB,CAAC,CAAC,CAAC;AAI3F,MAAM,MAAM,GAAa,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC;AAC1C,MAAM,QAAQ,GAAG,CAAC,MAAc,EAAY,EAAE,CAAC,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC,CAAC;AAE3E,oEAAoE;AACpE,MAAM,YAAY,GAAG,uBAAuB,CAAC;AAE7C;;;GAGG;AACH,MAAM,UAAU,YAAY,CAAC,QAAoC,EAAE,IAAa;IAC9E,IAAI,CAAC,QAAQ;QAAE,OAAO,MAAM,CAAC;IAC7B,IAAI,IAAI,KAAK,SAAS;QAAE,OAAO,QAAQ,CAAC,UAAU,CAAC,CAAC;IACpD,MAAM,GAAG,GAAG,QAAQ,CAAC,kBAAkB,CAAC;IACxC,IAAI,GAAG,KAAK,SAAS,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,MAAM,GAAG,GAAG,EAAE,CAAC;QAClE,OAAO,QAAQ,CAAC,cAAc,GAAG,EAAE,CAAC,CAAC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AE,SAAS,EAAE,OAAO,EAAE,GAAG,MAAM,CAAC;IACjD,IAAI,SAAS,KAAK,SAAS,EAAE,CAAC;QAC5B,yEAAyE;QACzE,yCAAyC;QACzC,IAAI,OAAO,SAAS,KAAK,QAAQ,IAAI,SAAS,KAAK,SAAS,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;YACtF,OAAO,QAAQ,CAAC,gBAAgB,CAAC,CAAC;QACpC,CAAC;QACD,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,UAAU,EAAE,EAAE,EAAE,CAAC;IAC1C,CAAC;IACD,IACE,OAAO,SAAS,KAAK,QAAQ;QAC7B,OAAO,OAAO,KAAK,QAAQ;QAC3B,CAAC,YAAY,CAAC,IAAI,CAAC,SAAS,CAAC;QAC7B,CAAC,YAAY,CAAC,IAAI,CAAC,OAAO,CAAC,EAC3B,CAAC;QACD,OAAO,QAAQ,CAAC,sBAAsB,CAAC,CAAC;IAC1C,CAAC;IACD,MAAM,IAAI,GAAG,MAAM,CAAC,SAAS,CAAC,CAAC;IAC/B,MAAM,EAAE,GAAG,MAAM,CAAC,OAAO,CAAC,CAAC;IAC3B,4DAA4D;IAC5D,IAAI,EAAE,GAAG,IAAI;QAAE,OAAO,QAAQ,CAAC,gBAAgB,CAAC,CAAC;IACjD,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,UAAU,EAAE,EAAE,GAAG,IAAI,GAAG,EAAE,EAAE,CAAC;AACtD,CAAC;AAED,SAAS,QAAQ,CAAC,KAAc;IAC9B,OAAO,KAAK,KAAK,IAAI,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAC9E,CAAC"}
|
package/dist/server.d.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
import http from 'node:http';
|
|
6
6
|
import type { PaymentRequirements } from '@celo/buy-core';
|
|
7
|
-
import { type FacilitatorEvents, type RemoteFacilitator } from '@celo/buy-facilitator';
|
|
7
|
+
import { type FacilitatorEvents, type PaymentProtocols, type RemoteFacilitator } from '@celo/buy-facilitator';
|
|
8
8
|
import type { LocalAccount } from 'viem';
|
|
9
9
|
import { type EndpointConfig, type EndpointRestrictions, type PaywallConfig, type ServiceConfig } from './config.js';
|
|
10
10
|
import type { UpstreamAdmission } from './upstream.js';
|
|
@@ -81,6 +81,13 @@ export type GatewayOptions = {
|
|
|
81
81
|
* away (verification, attestation or settlement failed). A throw is swallowed.
|
|
82
82
|
*/
|
|
83
83
|
onRefusal?: (entry: GatewayRefusal) => void;
|
|
84
|
+
/**
|
|
85
|
+
* Payment protocols to speak beside x402 v1, which is always on: x402 v2
|
|
86
|
+
* and MPP's `evm` `charge`. Both off by default, and a request that carries
|
|
87
|
+
* `X-PAYMENT` is handled the same whatever is set here. MPP is not offered
|
|
88
|
+
* on an `upto` price or on a route with a `self` requirement.
|
|
89
|
+
*/
|
|
90
|
+
protocols?: PaymentProtocols;
|
|
84
91
|
/**
|
|
85
92
|
* Called once per paid request after the gateway is done serving it, with
|
|
86
93
|
* the settlement and what the caller got. Never on a 402, a verify or settle
|
|
@@ -132,6 +139,16 @@ export type GatewayCompletion = ServeOutcome & {
|
|
|
132
139
|
/** The settlement tx; null in mock mode, where nothing settles. */
|
|
133
140
|
transaction: string | null;
|
|
134
141
|
mock: boolean;
|
|
142
|
+
/**
|
|
143
|
+
* Whether the request was inside the endpoint's `judged` envelope: the work
|
|
144
|
+
* the provider is judged on (judged.ts). Always true without an envelope.
|
|
145
|
+
* Serving, pricing and what is owed do not depend on it.
|
|
146
|
+
*/
|
|
147
|
+
judged: boolean;
|
|
148
|
+
/** Why it was not judged, a short machine code; only when `judged` is false. */
|
|
149
|
+
unjudgedReason?: string;
|
|
150
|
+
/** Whole milliseconds from the start of serving, after settlement, to this event. */
|
|
151
|
+
elapsedMs: number;
|
|
135
152
|
};
|
|
136
153
|
export type Gateway = {
|
|
137
154
|
server: http.Server;
|
|
@@ -159,6 +176,18 @@ export declare function codingsUndiciDecodes(): Promise<Set<string>>;
|
|
|
159
176
|
* be unreachable, kept because "should" is not a security boundary.
|
|
160
177
|
*/
|
|
161
178
|
export declare function upstreamTarget(service: ServiceConfig, endpoint: EndpointConfig, pathname: string, search: string): URL | undefined;
|
|
179
|
+
/**
|
|
180
|
+
* The tokens a priced endpoint is offered in: `config.tokens`, else its price
|
|
181
|
+
* currency. The 402 challenge quotes exactly these, so anything that
|
|
182
|
+
* advertises the route (a catalogue, a discovery feed) should call this rather
|
|
183
|
+
* than restate it.
|
|
184
|
+
*/
|
|
185
|
+
export declare function offeredTokens(config: PaywallConfig, endpoint: EndpointConfig): string[];
|
|
186
|
+
/**
|
|
187
|
+
* The protocols one endpoint speaks. The identity requirement travels inside
|
|
188
|
+
* the x402 offer and MPP has no place for it, so a `self` route is x402 only.
|
|
189
|
+
*/
|
|
190
|
+
export declare function endpointProtocols(protocols: PaymentProtocols | undefined, endpoint: EndpointConfig): PaymentProtocols | undefined;
|
|
162
191
|
/** One x402 offer per token the endpoint accepts, at the same price, in config order. */
|
|
163
192
|
export declare function offersFor(config: PaywallConfig, endpoint: EndpointConfig, resource: string): PaymentRequirements[];
|
|
164
193
|
/** Build the x402 requirements for one endpoint in one token (default: its price currency). */
|
|
@@ -166,15 +195,17 @@ export declare function requirementsFor(config: PaywallConfig, endpoint: Endpoin
|
|
|
166
195
|
export declare function validateTokens(config: PaywallConfig): void;
|
|
167
196
|
/**
|
|
168
197
|
* Apply an endpoint's JSON restrictions to a request body.
|
|
169
|
-
* With `jsonFields` or `
|
|
198
|
+
* With `jsonFields`, `jsonOnlyFields` or `textOnlyMessages` the body handed back is re-serialized
|
|
170
199
|
* from the parsed value: JSON.parse keeps the LAST of two duplicate keys and
|
|
171
200
|
* an upstream may keep the first, so forwarding the original bytes could let
|
|
172
|
-
* the upstream act on a value this check never saw.
|
|
201
|
+
* the upstream act on a value this check never saw. `value` is what was
|
|
202
|
+
* parsed, when anything was, for the judged envelope to read.
|
|
173
203
|
*/
|
|
174
204
|
export declare function checkJsonBody(body: Buffer, restrictions: EndpointRestrictions | undefined): {
|
|
175
205
|
ok: true;
|
|
176
206
|
body: Buffer;
|
|
177
207
|
json?: true;
|
|
208
|
+
value?: unknown;
|
|
178
209
|
} | {
|
|
179
210
|
ok: false;
|
|
180
211
|
reason: string;
|