@absol-labs/agent 0.3.2 → 0.5.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 +378 -168
- package/dist/capability/invocation-capability.d.ts +184 -0
- package/dist/capability/invocation-capability.d.ts.map +1 -0
- package/dist/capability/invocation-capability.js +183 -0
- package/dist/capability/invocation-capability.js.map +1 -0
- package/dist/frameworks/agentkit.js +2 -2
- package/dist/frameworks/agentkit.js.map +1 -1
- package/dist/frameworks/eliza.js +2 -2
- package/dist/frameworks/eliza.js.map +1 -1
- package/dist/frameworks/langchain.js +2 -2
- package/dist/frameworks/langchain.js.map +1 -1
- package/dist/gateway/caller-auth-gateway.d.ts +106 -0
- package/dist/gateway/caller-auth-gateway.d.ts.map +1 -0
- package/dist/gateway/caller-auth-gateway.js +189 -0
- package/dist/gateway/caller-auth-gateway.js.map +1 -0
- package/dist/gateway/http-server.d.ts +49 -0
- package/dist/gateway/http-server.d.ts.map +1 -0
- package/dist/gateway/http-server.js +227 -0
- package/dist/gateway/http-server.js.map +1 -0
- package/dist/gateway/server-entry.d.ts +2 -0
- package/dist/gateway/server-entry.d.ts.map +1 -0
- package/dist/gateway/server-entry.js +30 -0
- package/dist/gateway/server-entry.js.map +1 -0
- package/dist/index.d.ts +7 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -1
- package/dist/index.js.map +1 -1
- package/dist/mcp/server.js +2 -2
- package/dist/mcp/server.js.map +1 -1
- package/dist/sdk/invoke.d.ts +122 -0
- package/dist/sdk/invoke.d.ts.map +1 -0
- package/dist/sdk/invoke.js +158 -0
- package/dist/sdk/invoke.js.map +1 -0
- package/dist/wallet/lifecycle.d.ts +101 -0
- package/dist/wallet/lifecycle.d.ts.map +1 -0
- package/dist/wallet/lifecycle.js +57 -0
- package/dist/wallet/lifecycle.js.map +1 -0
- package/dist/zktls/reclaim.d.ts +84 -5
- package/dist/zktls/reclaim.d.ts.map +1 -1
- package/dist/zktls/reclaim.js +47 -3
- package/dist/zktls/reclaim.js.map +1 -1
- package/dist/zktls/t2-delivery-proof.d.ts +296 -0
- package/dist/zktls/t2-delivery-proof.d.ts.map +1 -0
- package/dist/zktls/t2-delivery-proof.js +336 -0
- package/dist/zktls/t2-delivery-proof.js.map +1 -0
- package/package.json +6 -3
- package/src/capability/invocation-capability.ts +255 -0
- package/src/frameworks/agentkit.ts +2 -2
- package/src/frameworks/eliza.ts +2 -2
- package/src/frameworks/langchain.ts +2 -2
- package/src/gateway/caller-auth-gateway.ts +328 -0
- package/src/gateway/http-server.ts +325 -0
- package/src/gateway/server-entry.ts +38 -0
- package/src/index.ts +103 -0
- package/src/mcp/server.ts +2 -2
- package/src/sdk/invoke.ts +313 -0
- package/src/wallet/lifecycle.ts +158 -0
- package/src/zktls/reclaim.ts +88 -8
- package/src/zktls/t2-delivery-proof.ts +559 -0
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { type Address, type Hex } from "viem";
|
|
2
|
+
import { type SignedInvocationCapability } from "../capability/invocation-capability.js";
|
|
3
|
+
/**
|
|
4
|
+
* Seller-side caller-auth gateway (metrik-agent#63, impl of #62 part 1/2).
|
|
5
|
+
*
|
|
6
|
+
* Authorizes an incoming service request ONLY when it carries a valid
|
|
7
|
+
* `InvocationCapability` (see `../capability/invocation-capability.ts`) whose
|
|
8
|
+
* signer is the buyer of an active, funded, non-expired `StreamEscrowV2`
|
|
9
|
+
* stream matching this gateway's configured `serviceRef` - and the capability
|
|
10
|
+
* itself is unexpired, unreplayed, and bound to the exact method+path being
|
|
11
|
+
* called. Revocation is automatic: once a stream is closed/expired/reclaimed,
|
|
12
|
+
* the on-chain read step below fails closed and access stops with no extra
|
|
13
|
+
* bookkeeping.
|
|
14
|
+
*/
|
|
15
|
+
/** Machine-readable, closed set of rejection reasons - every failure mode is distinct. */
|
|
16
|
+
export type CallerAuthDenialReason = "missing-capability-header" | "malformed-capability-header" | "capability-expired" | "invalid-signature" | "method-mismatch" | "path-mismatch" | "service-ref-mismatch" | "nonce-replayed" | "stream-not-found" | "buyer-mismatch" | "stream-not-active" | "stream-expired" | "stream-not-funded";
|
|
17
|
+
export interface CallerAuthDecision {
|
|
18
|
+
readonly authorized: boolean;
|
|
19
|
+
readonly reason?: CallerAuthDenialReason;
|
|
20
|
+
/** Set only when `authorized === false`. */
|
|
21
|
+
readonly httpStatus?: 402 | 403;
|
|
22
|
+
/** Set only when `authorized === true`. */
|
|
23
|
+
readonly stream?: GatewayStreamView;
|
|
24
|
+
/** The decoded (but not necessarily valid) capability, when one was present. */
|
|
25
|
+
readonly capability?: SignedInvocationCapability;
|
|
26
|
+
}
|
|
27
|
+
/** The minimal on-chain stream fields the gateway needs to authorize a call. */
|
|
28
|
+
export interface GatewayStreamView {
|
|
29
|
+
readonly buyer: Address;
|
|
30
|
+
readonly serviceRef: Hex;
|
|
31
|
+
readonly deposit: bigint;
|
|
32
|
+
readonly claimedCumulative: bigint;
|
|
33
|
+
readonly expiresAt: number;
|
|
34
|
+
readonly status: "active" | "closed";
|
|
35
|
+
}
|
|
36
|
+
/** Pluggable on-chain stream reader, so the gateway is testable without a live RPC. */
|
|
37
|
+
export interface StreamReader {
|
|
38
|
+
getStream(streamId: Hex): Promise<GatewayStreamView>;
|
|
39
|
+
}
|
|
40
|
+
export declare class UnknownStreamGatewayError extends Error {
|
|
41
|
+
constructor(message: string, options?: {
|
|
42
|
+
readonly cause?: unknown;
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
/** Default `StreamReader`: real, read-only `MetrikClient.getStreamV2` (no wallet, no writes). */
|
|
46
|
+
export declare function createSdkStreamReader(options: {
|
|
47
|
+
readonly escrowAddress: Address;
|
|
48
|
+
readonly rpcUrl: string;
|
|
49
|
+
}): StreamReader;
|
|
50
|
+
/** Single-use nonce replay protection. */
|
|
51
|
+
export interface NonceReplayCache {
|
|
52
|
+
/** Returns `true` (and records the nonce) IFF it has not been consumed before. Fails closed on a repeat. */
|
|
53
|
+
consume(nonce: Hex, expiresAtSeconds: number): boolean;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* In-memory replay cache. Sufficient for a single-instance reference gateway;
|
|
57
|
+
* a multi-instance deployment should back this with a shared store (Redis,
|
|
58
|
+
* etc.) via the same `NonceReplayCache` interface.
|
|
59
|
+
*/
|
|
60
|
+
export declare class InMemoryNonceReplayCache implements NonceReplayCache {
|
|
61
|
+
private readonly seen;
|
|
62
|
+
consume(nonce: Hex, expiresAtSeconds: number): boolean;
|
|
63
|
+
private sweep;
|
|
64
|
+
}
|
|
65
|
+
export interface CallerAuthGatewayConfig {
|
|
66
|
+
/** The `StreamEscrowV2` this gateway's streams settle on. */
|
|
67
|
+
readonly escrowAddress: Address;
|
|
68
|
+
/** RPC used for the default `StreamReader` (ignored if `streamReader` is supplied). */
|
|
69
|
+
readonly rpcUrl?: string;
|
|
70
|
+
/** The service this gateway fronts. Every request's stream AND capability must match it. */
|
|
71
|
+
readonly serviceRef: Hex;
|
|
72
|
+
/** Chain id for EIP-712 domain separation. Default `84532` (Base Sepolia). */
|
|
73
|
+
readonly chainId?: number;
|
|
74
|
+
/** Capability header name. Default {@link CAPABILITY_HEADER_NAME}. */
|
|
75
|
+
readonly headerName?: string;
|
|
76
|
+
/** Override the on-chain reader (tests inject a fake; default is the real SDK reader). */
|
|
77
|
+
readonly streamReader?: StreamReader;
|
|
78
|
+
/** Override the replay cache (tests inject a fake; default is in-memory). */
|
|
79
|
+
readonly nonceCache?: NonceReplayCache;
|
|
80
|
+
/** Injectable clock (unix seconds), for tests. */
|
|
81
|
+
readonly now?: () => number;
|
|
82
|
+
}
|
|
83
|
+
export interface CallerAuthRequestInput {
|
|
84
|
+
/** Lower-cased-lookup-safe header map (e.g. Node's `IncomingMessage.headers`). */
|
|
85
|
+
readonly headers: Readonly<Record<string, string | undefined>>;
|
|
86
|
+
readonly method: string;
|
|
87
|
+
/** The request path (query/fragment are ignored - only the path is bound). */
|
|
88
|
+
readonly path: string;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Authorizes requests against stream-bound `InvocationCapability` headers.
|
|
92
|
+
* Framework-agnostic: `authorize()` takes a plain `{headers, method, path}`
|
|
93
|
+
* triple so it can front any HTTP server (Node `http`, Express, Fastify, ...).
|
|
94
|
+
* See `./http-server.ts` for a ready-to-run reverse-proxy reference server.
|
|
95
|
+
*/
|
|
96
|
+
export declare class CallerAuthGateway {
|
|
97
|
+
private readonly streamReader;
|
|
98
|
+
private readonly nonceCache;
|
|
99
|
+
private readonly headerName;
|
|
100
|
+
private readonly domain;
|
|
101
|
+
private readonly serviceRef;
|
|
102
|
+
private readonly now;
|
|
103
|
+
constructor(config: CallerAuthGatewayConfig);
|
|
104
|
+
authorize(request: CallerAuthRequestInput): Promise<CallerAuthDecision>;
|
|
105
|
+
}
|
|
106
|
+
//# sourceMappingURL=caller-auth-gateway.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"caller-auth-gateway.d.ts","sourceRoot":"","sources":["../../src/gateway/caller-auth-gateway.ts"],"names":[],"mappings":"AACA,OAAO,EAAoB,KAAK,OAAO,EAAE,KAAK,GAAG,EAAE,MAAM,MAAM,CAAC;AAEhE,OAAO,EAOL,KAAK,0BAA0B,EAChC,MAAM,wCAAwC,CAAC;AAEhD;;;;;;;;;;;GAWG;AAEH,0FAA0F;AAC1F,MAAM,MAAM,sBAAsB,GAC9B,2BAA2B,GAC3B,6BAA6B,GAC7B,oBAAoB,GACpB,mBAAmB,GACnB,iBAAiB,GACjB,eAAe,GACf,sBAAsB,GACtB,gBAAgB,GAChB,kBAAkB,GAClB,gBAAgB,GAChB,mBAAmB,GACnB,gBAAgB,GAChB,mBAAmB,CAAC;AAExB,MAAM,WAAW,kBAAkB;IACjC,QAAQ,CAAC,UAAU,EAAE,OAAO,CAAC;IAC7B,QAAQ,CAAC,MAAM,CAAC,EAAE,sBAAsB,CAAC;IACzC,4CAA4C;IAC5C,QAAQ,CAAC,UAAU,CAA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,kBAAkB,CAAC;CAgF/B"}
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { MetrikClient } from "@absol-labs/sdk";
|
|
2
|
+
import { isAddress, isHex } from "viem";
|
|
3
|
+
import { CAPABILITY_HEADER_NAME, decodeCapabilityHeader, hashInvocationPath, InvalidCapabilityHeaderError, recoverInvocationCapabilitySigner, } from "../capability/invocation-capability.js";
|
|
4
|
+
export class UnknownStreamGatewayError extends Error {
|
|
5
|
+
constructor(message, options) {
|
|
6
|
+
super(message, options);
|
|
7
|
+
this.name = "UnknownStreamGatewayError";
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
/** Default `StreamReader`: real, read-only `MetrikClient.getStreamV2` (no wallet, no writes). */
|
|
11
|
+
export function createSdkStreamReader(options) {
|
|
12
|
+
const metrik = MetrikClient.baseSepolia({
|
|
13
|
+
escrow: options.escrowAddress,
|
|
14
|
+
rpcUrl: options.rpcUrl,
|
|
15
|
+
});
|
|
16
|
+
return {
|
|
17
|
+
async getStream(streamId) {
|
|
18
|
+
try {
|
|
19
|
+
const stream = await metrik.getStreamV2(streamId);
|
|
20
|
+
return {
|
|
21
|
+
buyer: stream.buyer,
|
|
22
|
+
serviceRef: stream.serviceRef,
|
|
23
|
+
deposit: stream.deposit,
|
|
24
|
+
claimedCumulative: stream.claimedCumulative,
|
|
25
|
+
expiresAt: stream.expiresAt,
|
|
26
|
+
status: stream.status,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
catch (cause) {
|
|
30
|
+
throw new UnknownStreamGatewayError(`failed to read stream ${streamId}: ${cause instanceof Error ? cause.message : String(cause)}`, { cause });
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* In-memory replay cache. Sufficient for a single-instance reference gateway;
|
|
37
|
+
* a multi-instance deployment should back this with a shared store (Redis,
|
|
38
|
+
* etc.) via the same `NonceReplayCache` interface.
|
|
39
|
+
*/
|
|
40
|
+
export class InMemoryNonceReplayCache {
|
|
41
|
+
seen = new Map();
|
|
42
|
+
consume(nonce, expiresAtSeconds) {
|
|
43
|
+
this.sweep();
|
|
44
|
+
const key = nonce.toLowerCase();
|
|
45
|
+
if (this.seen.has(key)) {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
this.seen.set(key, expiresAtSeconds);
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
sweep() {
|
|
52
|
+
const nowSeconds = Math.floor(Date.now() / 1000);
|
|
53
|
+
for (const [key, expiresAtSeconds] of this.seen) {
|
|
54
|
+
if (expiresAtSeconds < nowSeconds) {
|
|
55
|
+
this.seen.delete(key);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Authorizes requests against stream-bound `InvocationCapability` headers.
|
|
62
|
+
* Framework-agnostic: `authorize()` takes a plain `{headers, method, path}`
|
|
63
|
+
* triple so it can front any HTTP server (Node `http`, Express, Fastify, ...).
|
|
64
|
+
* See `./http-server.ts` for a ready-to-run reverse-proxy reference server.
|
|
65
|
+
*/
|
|
66
|
+
export class CallerAuthGateway {
|
|
67
|
+
streamReader;
|
|
68
|
+
nonceCache;
|
|
69
|
+
headerName;
|
|
70
|
+
domain;
|
|
71
|
+
serviceRef;
|
|
72
|
+
now;
|
|
73
|
+
constructor(config) {
|
|
74
|
+
if (!isAddress(config.escrowAddress)) {
|
|
75
|
+
throw new Error("escrowAddress must be an EVM address");
|
|
76
|
+
}
|
|
77
|
+
if (!isHex(config.serviceRef, { strict: true }) ||
|
|
78
|
+
config.serviceRef.length !== 66) {
|
|
79
|
+
throw new Error("serviceRef must be a 32-byte hex value");
|
|
80
|
+
}
|
|
81
|
+
if (config.streamReader === undefined && config.rpcUrl === undefined) {
|
|
82
|
+
throw new Error("rpcUrl is required unless an explicit streamReader is supplied");
|
|
83
|
+
}
|
|
84
|
+
this.serviceRef = config.serviceRef;
|
|
85
|
+
this.headerName = (config.headerName ?? CAPABILITY_HEADER_NAME).toLowerCase();
|
|
86
|
+
this.streamReader =
|
|
87
|
+
config.streamReader ??
|
|
88
|
+
createSdkStreamReader({
|
|
89
|
+
escrowAddress: config.escrowAddress,
|
|
90
|
+
rpcUrl: config.rpcUrl,
|
|
91
|
+
});
|
|
92
|
+
this.nonceCache = config.nonceCache ?? new InMemoryNonceReplayCache();
|
|
93
|
+
this.domain = {
|
|
94
|
+
chainId: config.chainId ?? 84532,
|
|
95
|
+
verifyingContract: config.escrowAddress,
|
|
96
|
+
};
|
|
97
|
+
this.now = config.now ?? (() => Math.floor(Date.now() / 1000));
|
|
98
|
+
}
|
|
99
|
+
async authorize(request) {
|
|
100
|
+
const headerValue = lookupHeader(request.headers, this.headerName);
|
|
101
|
+
if (headerValue === undefined || headerValue.length === 0) {
|
|
102
|
+
return deny("missing-capability-header", 403);
|
|
103
|
+
}
|
|
104
|
+
let capability;
|
|
105
|
+
try {
|
|
106
|
+
capability = decodeCapabilityHeader(headerValue);
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
if (error instanceof InvalidCapabilityHeaderError) {
|
|
110
|
+
return deny("malformed-capability-header", 403);
|
|
111
|
+
}
|
|
112
|
+
throw error;
|
|
113
|
+
}
|
|
114
|
+
const nowSeconds = this.now();
|
|
115
|
+
if (nowSeconds >= capability.expiry) {
|
|
116
|
+
return deny("capability-expired", 403, capability);
|
|
117
|
+
}
|
|
118
|
+
let signer;
|
|
119
|
+
try {
|
|
120
|
+
signer = await recoverInvocationCapabilitySigner(capability, this.domain, capability.signature);
|
|
121
|
+
}
|
|
122
|
+
catch {
|
|
123
|
+
return deny("invalid-signature", 403, capability);
|
|
124
|
+
}
|
|
125
|
+
if (signer.toLowerCase() !== capability.buyer.toLowerCase()) {
|
|
126
|
+
return deny("invalid-signature", 403, capability);
|
|
127
|
+
}
|
|
128
|
+
if (capability.method.toUpperCase() !== request.method.toUpperCase()) {
|
|
129
|
+
return deny("method-mismatch", 403, capability);
|
|
130
|
+
}
|
|
131
|
+
const expectedPathHash = hashInvocationPath(request.path);
|
|
132
|
+
if (expectedPathHash.toLowerCase() !== capability.pathHash.toLowerCase()) {
|
|
133
|
+
return deny("path-mismatch", 403, capability);
|
|
134
|
+
}
|
|
135
|
+
if (capability.serviceRef.toLowerCase() !== this.serviceRef.toLowerCase()) {
|
|
136
|
+
return deny("service-ref-mismatch", 403, capability);
|
|
137
|
+
}
|
|
138
|
+
// Consume the nonce BEFORE the on-chain read: the replay check is the
|
|
139
|
+
// cheapest gate and must fail closed even if the RPC is slow/unavailable.
|
|
140
|
+
if (!this.nonceCache.consume(capability.nonce, capability.expiry)) {
|
|
141
|
+
return deny("nonce-replayed", 403, capability);
|
|
142
|
+
}
|
|
143
|
+
let stream;
|
|
144
|
+
try {
|
|
145
|
+
stream = await this.streamReader.getStream(capability.streamId);
|
|
146
|
+
}
|
|
147
|
+
catch {
|
|
148
|
+
return deny("stream-not-found", 403, capability);
|
|
149
|
+
}
|
|
150
|
+
if (stream.buyer.toLowerCase() !== capability.buyer.toLowerCase()) {
|
|
151
|
+
return deny("buyer-mismatch", 403, capability);
|
|
152
|
+
}
|
|
153
|
+
if (stream.serviceRef.toLowerCase() !== this.serviceRef.toLowerCase()) {
|
|
154
|
+
return deny("service-ref-mismatch", 403, capability);
|
|
155
|
+
}
|
|
156
|
+
if (stream.status !== "active") {
|
|
157
|
+
return deny("stream-not-active", 403, capability);
|
|
158
|
+
}
|
|
159
|
+
if (nowSeconds >= stream.expiresAt) {
|
|
160
|
+
return deny("stream-expired", 403, capability);
|
|
161
|
+
}
|
|
162
|
+
if (stream.deposit <= stream.claimedCumulative) {
|
|
163
|
+
return deny("stream-not-funded", 402, capability);
|
|
164
|
+
}
|
|
165
|
+
return { authorized: true, stream, capability };
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
function deny(reason, httpStatus, capability) {
|
|
169
|
+
return {
|
|
170
|
+
authorized: false,
|
|
171
|
+
reason,
|
|
172
|
+
httpStatus,
|
|
173
|
+
...(capability === undefined ? {} : { capability }),
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
function lookupHeader(headers, name) {
|
|
177
|
+
// Header maps may or may not already be lower-cased by the caller's framework;
|
|
178
|
+
// normalize defensively so a differently-cased key still resolves.
|
|
179
|
+
if (headers[name] !== undefined) {
|
|
180
|
+
return headers[name];
|
|
181
|
+
}
|
|
182
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
183
|
+
if (key.toLowerCase() === name) {
|
|
184
|
+
return value;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return undefined;
|
|
188
|
+
}
|
|
189
|
+
//# sourceMappingURL=caller-auth-gateway.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"caller-auth-gateway.js","sourceRoot":"","sources":["../../src/gateway/caller-auth-gateway.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,EAAE,SAAS,EAAE,KAAK,EAA0B,MAAM,MAAM,CAAC;AAEhE,OAAO,EACL,sBAAsB,EACtB,sBAAsB,EACtB,kBAAkB,EAClB,4BAA4B,EAC5B,iCAAiC,GAGlC,MAAM,wCAAwC,CAAC;AAyDhD,MAAM,OAAO,yBAA0B,SAAQ,KAAK;IAClD,YAAY,OAAe,EAAE,OAAsC;QACjE,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QACxB,IAAI,CAAC,IAAI,GAAG,2BAA2B,CAAC;IAC1C,CAAC;CACF;AAED,iGAAiG;AACjG,MAAM,UAAU,qBAAqB,CAAC,OAGrC;IACC,MAAM,MAAM,GAAG,YAAY,CAAC,WAAW,CAAC;QACtC,MAAM,EAAE,OAAO,CAAC,aAAa;QAC7B,MAAM,EAAE,OAAO,CAAC,MAAM;KACvB,CAAC,CAAC;IACH,OAAO;QACL,KAAK,CAAC,SAAS,CAAC,QAAQ;YACtB,I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;IAC/E,mEAAmE;IACnE,IAAI,OAAO,CAAC,IAAI,CAAC,KAAK,SAAS,EAAE,CAAC;QAChC,OAAO,OAAO,CAAC,IAAI,CAAC,CAAC;IACvB,CAAC;IACD,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;QACnD,IAAI,GAAG,CAAC,WAAW,EAAE,KAAK,IAAI,EAAE,CAAC;YAC/B,OAAO,KAAK,CAAC;QACf,CAAC;IACH,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { type Server } from "node:http";
|
|
2
|
+
import { type Address, type Hex } from "viem";
|
|
3
|
+
import { CallerAuthGateway } from "./caller-auth-gateway.js";
|
|
4
|
+
export interface CallerAuthGatewayServerConfig {
|
|
5
|
+
readonly gateway: CallerAuthGateway;
|
|
6
|
+
/** Base URL of the real service this gateway fronts (e.g. `https://svc.example.com`). */
|
|
7
|
+
readonly upstreamUrl: string;
|
|
8
|
+
/** Extra request-timeout for the upstream fetch, in ms. Default 30000. */
|
|
9
|
+
readonly upstreamTimeoutMs?: number;
|
|
10
|
+
}
|
|
11
|
+
export interface CallerAuthGatewayServer {
|
|
12
|
+
readonly httpServer: Server;
|
|
13
|
+
listen(port: number, host?: string): Promise<{
|
|
14
|
+
port: number;
|
|
15
|
+
host: string;
|
|
16
|
+
}>;
|
|
17
|
+
close(): Promise<void>;
|
|
18
|
+
}
|
|
19
|
+
export declare function createCallerAuthGatewayServer(config: CallerAuthGatewayServerConfig): CallerAuthGatewayServer;
|
|
20
|
+
export interface CallerAuthGatewayEnvConfig {
|
|
21
|
+
readonly host: string;
|
|
22
|
+
readonly port: number;
|
|
23
|
+
readonly escrowAddress: Address;
|
|
24
|
+
readonly rpcUrl: string;
|
|
25
|
+
readonly serviceRef: Hex;
|
|
26
|
+
readonly chainId: number;
|
|
27
|
+
readonly upstreamUrl: string;
|
|
28
|
+
readonly headerName?: string;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Parses the gateway's env contract:
|
|
32
|
+
* - `METRIK_GATEWAY_ESCROW_ADDRESS` (required) - the `StreamEscrowV2` this
|
|
33
|
+
* gateway checks streams against.
|
|
34
|
+
* - `METRIK_GATEWAY_RPC_URL` (required) - RPC used for the read-only stream check.
|
|
35
|
+
* - `METRIK_GATEWAY_SERVICE_REF` (required) - bytes32 `serviceRef` this gateway fronts.
|
|
36
|
+
* - `METRIK_GATEWAY_UPSTREAM_URL` (required) - the real service to proxy to on pass.
|
|
37
|
+
* - `METRIK_GATEWAY_CHAIN_ID` (optional) - default `84532` (Base Sepolia).
|
|
38
|
+
* - `METRIK_GATEWAY_HEADER_NAME` (optional) - default `x-metrik-capability`.
|
|
39
|
+
* - `PORT` / `METRIK_GATEWAY_PORT` (optional) - default `8787`.
|
|
40
|
+
* - `METRIK_GATEWAY_HOST` (optional) - default `0.0.0.0`.
|
|
41
|
+
*
|
|
42
|
+
* Fail-closed: throws if any required var is missing or malformed.
|
|
43
|
+
*/
|
|
44
|
+
export declare function parseCallerAuthGatewayEnvConfig(env?: NodeJS.ProcessEnv): CallerAuthGatewayEnvConfig;
|
|
45
|
+
/** Builds and starts the reference gateway server entirely from the environment. */
|
|
46
|
+
export declare function startCallerAuthGatewayServerFromEnv(env?: NodeJS.ProcessEnv): Promise<CallerAuthGatewayServer & {
|
|
47
|
+
readonly boundPort: number;
|
|
48
|
+
}>;
|
|
49
|
+
//# sourceMappingURL=http-server.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http-server.d.ts","sourceRoot":"","sources":["../../src/gateway/http-server.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,MAAM,EAEZ,MAAM,WAAW,CAAC;AAGnB,OAAO,EAAoB,KAAK,OAAO,EAAE,KAAK,GAAG,EAAE,MAAM,MAAM,CAAC;AAEhE,OAAO,EACL,iBA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,MAAM,CAAA;CAAE,CAAC,CAiBnE"}
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import { createServer, } from "node:http";
|
|
2
|
+
import { isAddress, isHex } from "viem";
|
|
3
|
+
import { CallerAuthGateway, } from "./caller-auth-gateway.js";
|
|
4
|
+
/**
|
|
5
|
+
* Reference standalone reverse-proxy gateway server (metrik-agent#63).
|
|
6
|
+
*
|
|
7
|
+
* A seller runs this in front of their real HTTP service. Every request must
|
|
8
|
+
* carry a valid `InvocationCapability` header (see
|
|
9
|
+
* `../capability/invocation-capability.ts`); on pass, the request is proxied
|
|
10
|
+
* to `upstreamUrl` verbatim and its response is returned unmodified. On fail,
|
|
11
|
+
* a `402`/`403` with a machine-readable JSON reason is returned and the
|
|
12
|
+
* upstream is never touched.
|
|
13
|
+
*/
|
|
14
|
+
/** Maximum buffered request body size (bytes) - bounds memory per request. */
|
|
15
|
+
const MAX_BODY_BYTES = 16 * 1024 * 1024;
|
|
16
|
+
/** Hop-by-hop headers that must never be forwarded verbatim to the upstream. */
|
|
17
|
+
const HOP_BY_HOP_HEADERS = new Set([
|
|
18
|
+
"connection",
|
|
19
|
+
"keep-alive",
|
|
20
|
+
"proxy-authenticate",
|
|
21
|
+
"proxy-authorization",
|
|
22
|
+
"te",
|
|
23
|
+
"trailer",
|
|
24
|
+
"transfer-encoding",
|
|
25
|
+
"upgrade",
|
|
26
|
+
"host",
|
|
27
|
+
"content-length",
|
|
28
|
+
]);
|
|
29
|
+
export function createCallerAuthGatewayServer(config) {
|
|
30
|
+
const upstreamBase = new URL(config.upstreamUrl);
|
|
31
|
+
const upstreamTimeoutMs = config.upstreamTimeoutMs ?? 30_000;
|
|
32
|
+
const httpServer = createServer((req, res) => {
|
|
33
|
+
handleRequest(req, res, config.gateway, upstreamBase, upstreamTimeoutMs).catch((error) => {
|
|
34
|
+
logError("unhandled request error", error);
|
|
35
|
+
if (!res.headersSent) {
|
|
36
|
+
writeJsonError(res, 500, "internal-error");
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
res.end();
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
return {
|
|
44
|
+
httpServer,
|
|
45
|
+
listen: (port, host = "0.0.0.0") => new Promise((resolve, reject) => {
|
|
46
|
+
const onError = (error) => reject(error);
|
|
47
|
+
httpServer.once("error", onError);
|
|
48
|
+
httpServer.listen(port, host, () => {
|
|
49
|
+
httpServer.removeListener("error", onError);
|
|
50
|
+
const address = httpServer.address();
|
|
51
|
+
resolve({ port: address.port, host });
|
|
52
|
+
});
|
|
53
|
+
}),
|
|
54
|
+
close: () => new Promise((resolve, reject) => {
|
|
55
|
+
httpServer.close((error) => (error ? reject(error) : resolve()));
|
|
56
|
+
}),
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
async function handleRequest(req, res, gateway, upstreamBase, upstreamTimeoutMs) {
|
|
60
|
+
const requestUrl = new URL(req.url ?? "/", "http://localhost");
|
|
61
|
+
const method = req.method ?? "GET";
|
|
62
|
+
const headers = normalizeIncomingHeaders(req.headers);
|
|
63
|
+
const decision = await gateway.authorize({
|
|
64
|
+
headers,
|
|
65
|
+
method,
|
|
66
|
+
path: requestUrl.pathname,
|
|
67
|
+
});
|
|
68
|
+
if (!decision.authorized) {
|
|
69
|
+
writeJsonError(res, decision.httpStatus ?? 403, decision.reason ?? "unauthorized");
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
let body;
|
|
73
|
+
if (method !== "GET" && method !== "HEAD") {
|
|
74
|
+
body = await readBody(req);
|
|
75
|
+
}
|
|
76
|
+
const upstreamHeaders = new Headers();
|
|
77
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
78
|
+
if (value === undefined || HOP_BY_HOP_HEADERS.has(key)) {
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
upstreamHeaders.set(key, value);
|
|
82
|
+
}
|
|
83
|
+
const upstreamUrl = new URL(requestUrl.pathname + requestUrl.search, upstreamBase);
|
|
84
|
+
const controller = new AbortController();
|
|
85
|
+
const timeout = setTimeout(() => controller.abort(), upstreamTimeoutMs);
|
|
86
|
+
try {
|
|
87
|
+
const upstreamResponse = await fetch(upstreamUrl, {
|
|
88
|
+
method,
|
|
89
|
+
headers: upstreamHeaders,
|
|
90
|
+
...(body === undefined ? {} : { body }),
|
|
91
|
+
signal: controller.signal,
|
|
92
|
+
});
|
|
93
|
+
res.writeHead(upstreamResponse.status, headersToObject(upstreamResponse.headers));
|
|
94
|
+
const buffer = Buffer.from(await upstreamResponse.arrayBuffer());
|
|
95
|
+
res.end(buffer);
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
logError("upstream request failed", error);
|
|
99
|
+
writeJsonError(res, 502, "upstream-unavailable");
|
|
100
|
+
}
|
|
101
|
+
finally {
|
|
102
|
+
clearTimeout(timeout);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
function normalizeIncomingHeaders(headers) {
|
|
106
|
+
const normalized = {};
|
|
107
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
108
|
+
if (value === undefined)
|
|
109
|
+
continue;
|
|
110
|
+
normalized[key.toLowerCase()] = Array.isArray(value) ? value[0] : value;
|
|
111
|
+
}
|
|
112
|
+
return normalized;
|
|
113
|
+
}
|
|
114
|
+
function headersToObject(headers) {
|
|
115
|
+
const result = {};
|
|
116
|
+
headers.forEach((value, key) => {
|
|
117
|
+
if (HOP_BY_HOP_HEADERS.has(key.toLowerCase()))
|
|
118
|
+
return;
|
|
119
|
+
result[key] = value;
|
|
120
|
+
});
|
|
121
|
+
return result;
|
|
122
|
+
}
|
|
123
|
+
class BodyTooLargeError extends Error {
|
|
124
|
+
}
|
|
125
|
+
async function readBody(req) {
|
|
126
|
+
const chunks = [];
|
|
127
|
+
let size = 0;
|
|
128
|
+
for await (const chunk of req) {
|
|
129
|
+
const buffer = chunk;
|
|
130
|
+
size += buffer.length;
|
|
131
|
+
if (size > MAX_BODY_BYTES) {
|
|
132
|
+
throw new BodyTooLargeError("request body too large");
|
|
133
|
+
}
|
|
134
|
+
chunks.push(buffer);
|
|
135
|
+
}
|
|
136
|
+
return Buffer.concat(chunks);
|
|
137
|
+
}
|
|
138
|
+
function writeJsonError(res, status, reason) {
|
|
139
|
+
if (res.headersSent) {
|
|
140
|
+
res.end();
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
res
|
|
144
|
+
.writeHead(status, { "Content-Type": "application/json" })
|
|
145
|
+
.end(JSON.stringify({ authorized: false, reason }));
|
|
146
|
+
}
|
|
147
|
+
function logError(context, error) {
|
|
148
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
149
|
+
console.error(`[metrik-caller-auth-gateway] ${context}: ${message}`);
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Parses the gateway's env contract:
|
|
153
|
+
* - `METRIK_GATEWAY_ESCROW_ADDRESS` (required) - the `StreamEscrowV2` this
|
|
154
|
+
* gateway checks streams against.
|
|
155
|
+
* - `METRIK_GATEWAY_RPC_URL` (required) - RPC used for the read-only stream check.
|
|
156
|
+
* - `METRIK_GATEWAY_SERVICE_REF` (required) - bytes32 `serviceRef` this gateway fronts.
|
|
157
|
+
* - `METRIK_GATEWAY_UPSTREAM_URL` (required) - the real service to proxy to on pass.
|
|
158
|
+
* - `METRIK_GATEWAY_CHAIN_ID` (optional) - default `84532` (Base Sepolia).
|
|
159
|
+
* - `METRIK_GATEWAY_HEADER_NAME` (optional) - default `x-metrik-capability`.
|
|
160
|
+
* - `PORT` / `METRIK_GATEWAY_PORT` (optional) - default `8787`.
|
|
161
|
+
* - `METRIK_GATEWAY_HOST` (optional) - default `0.0.0.0`.
|
|
162
|
+
*
|
|
163
|
+
* Fail-closed: throws if any required var is missing or malformed.
|
|
164
|
+
*/
|
|
165
|
+
export function parseCallerAuthGatewayEnvConfig(env = process.env) {
|
|
166
|
+
const escrowAddress = requireEnv(env, "METRIK_GATEWAY_ESCROW_ADDRESS");
|
|
167
|
+
if (!isAddress(escrowAddress)) {
|
|
168
|
+
throw new Error("METRIK_GATEWAY_ESCROW_ADDRESS must be an EVM address");
|
|
169
|
+
}
|
|
170
|
+
const rpcUrl = requireEnv(env, "METRIK_GATEWAY_RPC_URL");
|
|
171
|
+
const serviceRef = requireEnv(env, "METRIK_GATEWAY_SERVICE_REF");
|
|
172
|
+
if (!isHex(serviceRef, { strict: true }) || serviceRef.length !== 66) {
|
|
173
|
+
throw new Error("METRIK_GATEWAY_SERVICE_REF must be a 32-byte hex value");
|
|
174
|
+
}
|
|
175
|
+
const upstreamUrl = requireEnv(env, "METRIK_GATEWAY_UPSTREAM_URL");
|
|
176
|
+
// eslint-disable-next-line no-new -- validates the URL is well-formed.
|
|
177
|
+
new URL(upstreamUrl);
|
|
178
|
+
const chainIdRaw = env.METRIK_GATEWAY_CHAIN_ID ?? "84532";
|
|
179
|
+
const chainId = Number(chainIdRaw);
|
|
180
|
+
if (!Number.isInteger(chainId) || chainId <= 0) {
|
|
181
|
+
throw new Error(`invalid METRIK_GATEWAY_CHAIN_ID: ${chainIdRaw}`);
|
|
182
|
+
}
|
|
183
|
+
const portRaw = env.METRIK_GATEWAY_PORT ?? env.PORT ?? "8787";
|
|
184
|
+
const port = Number(portRaw);
|
|
185
|
+
if (!Number.isInteger(port) || port < 0 || port > 65535) {
|
|
186
|
+
throw new Error(`invalid port: ${portRaw}`);
|
|
187
|
+
}
|
|
188
|
+
const host = env.METRIK_GATEWAY_HOST ?? "0.0.0.0";
|
|
189
|
+
const headerName = env.METRIK_GATEWAY_HEADER_NAME;
|
|
190
|
+
return {
|
|
191
|
+
host,
|
|
192
|
+
port,
|
|
193
|
+
escrowAddress,
|
|
194
|
+
rpcUrl,
|
|
195
|
+
serviceRef,
|
|
196
|
+
chainId,
|
|
197
|
+
upstreamUrl,
|
|
198
|
+
...(headerName === undefined ? {} : { headerName }),
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
function requireEnv(env, name) {
|
|
202
|
+
const value = env[name];
|
|
203
|
+
if (value === undefined || value.trim().length === 0) {
|
|
204
|
+
throw new Error(`${name} is required (fail-closed)`);
|
|
205
|
+
}
|
|
206
|
+
return value;
|
|
207
|
+
}
|
|
208
|
+
/** Builds and starts the reference gateway server entirely from the environment. */
|
|
209
|
+
export async function startCallerAuthGatewayServerFromEnv(env = process.env) {
|
|
210
|
+
const config = parseCallerAuthGatewayEnvConfig(env);
|
|
211
|
+
const gateway = new CallerAuthGateway({
|
|
212
|
+
escrowAddress: config.escrowAddress,
|
|
213
|
+
rpcUrl: config.rpcUrl,
|
|
214
|
+
serviceRef: config.serviceRef,
|
|
215
|
+
chainId: config.chainId,
|
|
216
|
+
...(config.headerName === undefined
|
|
217
|
+
? {}
|
|
218
|
+
: { headerName: config.headerName }),
|
|
219
|
+
});
|
|
220
|
+
const server = createCallerAuthGatewayServer({
|
|
221
|
+
gateway,
|
|
222
|
+
upstreamUrl: config.upstreamUrl,
|
|
223
|
+
});
|
|
224
|
+
const { port } = await server.listen(config.port, config.host);
|
|
225
|
+
return Object.assign(server, { boundPort: port });
|
|
226
|
+
}
|
|
227
|
+
//# sourceMappingURL=http-server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http-server.js","sourceRoot":"","sources":["../../src/gateway/http-server.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,YAAY,GAIb,MAAM,WAAW,CAAC;AAGnB,OAAO,EAAE,SAAS,EAAE,KAAK,EAA0B,MAAM,MAAM,CAAC;AAEhE,OAAO,EACL,iBAAiB,GAElB,MAAM,0BAA0B,CAAC;AAElC;;;;;;;;;GASG;AAEH,8EAA8E;AAC9E,MAAM,cAAc,GAAG,EAAE,GAAG,IAAI,GAAG,IAAI,CAAC;AAExC,gFAAgF;AAChF,MAAM,kBAAkB,GAAG,IAAI,GAAG,CAAC;IACjC,YAAY;IACZ,YAAY;IACZ,oBAAoB;IACpB,qBAAqB;IACrB,IAAI;IACJ,SAAS;IACT,mBAAmB;IACnB,SA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AC;IACxB,IAAI,KAAK,KAAK,SAAS,IAAI,KAAK,CAAC,IAAI,EAAE,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACrD,MAAM,IAAI,KAAK,CAAC,GAAG,IAAI,4BAA4B,CAAC,CAAC;IACvD,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC;AAED,oFAAoF;AACpF,MAAM,CAAC,KAAK,UAAU,mCAAmC,CACvD,MAAyB,OAAO,CAAC,GAAG;IAEpC,MAAM,MAAM,GAAG,+BAA+B,CAAC,GAAG,CAAC,CAAC;IACpD,MAAM,OAAO,GAAG,IAAI,iBAAiB,CAAC;QACpC,aAAa,EAAE,MAAM,CAAC,aAAa;QACnC,MAAM,EAAE,MAAM,CAAC,MAAM;QACrB,UAAU,EAAE,MAAM,CAAC,UAAU;QAC7B,OAAO,EAAE,MAAM,CAAC,OAAO;QACvB,GAAG,CAAC,MAAM,CAAC,UAAU,KAAK,SAAS;YACjC,CAAC,CAAC,EAAE;YACJ,CAAC,CAAC,EAAE,UAAU,EAAE,MAAM,CAAC,UAAU,EAAE,CAAC;KACvC,CAAC,CAAC;IACH,MAAM,MAAM,GAAG,6BAA6B,CAAC;QAC3C,OAAO;QACP,WAAW,EAAE,MAAM,CAAC,WAAW;KAChC,CAAC,CAAC;IACH,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC;IAC/D,OAAO,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;AACpD,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server-entry.d.ts","sourceRoot":"","sources":["../../src/gateway/server-entry.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { startCallerAuthGatewayServerFromEnv } from "./http-server.js";
|
|
2
|
+
/**
|
|
3
|
+
* Runnable entrypoint for the reference caller-auth gateway (metrik-agent#63).
|
|
4
|
+
* Reads all configuration from the environment (see
|
|
5
|
+
* {@link parseCallerAuthGatewayEnvConfig}) and refuses to start if required
|
|
6
|
+
* config is missing (fail-closed).
|
|
7
|
+
*
|
|
8
|
+
* Start with: `pnpm gateway` (or `node dist/gateway/server-entry.js`).
|
|
9
|
+
*/
|
|
10
|
+
async function main() {
|
|
11
|
+
const server = await startCallerAuthGatewayServerFromEnv();
|
|
12
|
+
const host = process.env.METRIK_GATEWAY_HOST ?? "0.0.0.0";
|
|
13
|
+
console.error(`[metrik-caller-auth-gateway] listening on http://${host}:${server.boundPort}`);
|
|
14
|
+
const shutdown = (signal) => {
|
|
15
|
+
console.error(`[metrik-caller-auth-gateway] ${signal} received — shutting down`);
|
|
16
|
+
server
|
|
17
|
+
.close()
|
|
18
|
+
.then(() => process.exit(0))
|
|
19
|
+
.catch(() => process.exit(1));
|
|
20
|
+
};
|
|
21
|
+
process.on("SIGINT", () => shutdown("SIGINT"));
|
|
22
|
+
process.on("SIGTERM", () => shutdown("SIGTERM"));
|
|
23
|
+
}
|
|
24
|
+
main().catch((error) => {
|
|
25
|
+
console.error(error instanceof Error
|
|
26
|
+
? error.message
|
|
27
|
+
: "failed to start caller-auth gateway server");
|
|
28
|
+
process.exit(1);
|
|
29
|
+
});
|
|
30
|
+
//# sourceMappingURL=server-entry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server-entry.js","sourceRoot":"","sources":["../../src/gateway/server-entry.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mCAAmC,EAAE,MAAM,kBAAkB,CAAC;AAEvE;;;;;;;GAOG;AACH,KAAK,UAAU,IAAI;IACjB,MAAM,MAAM,GAAG,MAAM,mCAAmC,EAAE,CAAC;IAC3D,MAAM,IAAI,GAAG,OAAO,CAAC,GAAG,CAAC,mBAAmB,IAAI,SAAS,CAAC;IAC1D,OAAO,CAAC,KAAK,CACX,oDAAoD,IAAI,IAAI,MAAM,CAAC,SAAS,EAAE,CAC/E,CAAC;IAEF,MAAM,QAAQ,GAAG,CAAC,MAAc,EAAE,EAAE;QAClC,OAAO,CAAC,KAAK,CACX,gCAAgC,MAAM,2BAA2B,CAClE,CAAC;QACF,MAAM;aACH,KAAK,EAAE;aACP,IAAI,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;aAC3B,KAAK,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;IAClC,CAAC,CAAC;IACF,OAAO,CAAC,EAAE,CAAC,QAAQ,EAAE,GAAG,EAAE,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC,CAAC;IAC/C,OAAO,CAAC,EAAE,CAAC,SAAS,EAAE,GAAG,EAAE,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC,CAAC;AACnD,CAAC;AAED,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;IACrB,OAAO,CAAC,KAAK,CACX,KAAK,YAAY,KAAK;QACpB,CAAC,CAAC,KAAK,CAAC,OAAO;QACf,CAAC,CAAC,4CAA4C,CACjD,CAAC;IACF,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAClB,CAAC,CAAC,CAAC"}
|