-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlogging.ts
More file actions
245 lines (212 loc) · 6.91 KB
/
Copy pathlogging.ts
File metadata and controls
245 lines (212 loc) · 6.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import { inspect } from "node:util";
import type {
PaymentPayload,
PaymentRequirements,
SettleResponse,
VerifyResponse,
} from "@x402/core/types";
import {
EIP2612_GAS_SPONSORING,
ERC20_APPROVAL_GAS_SPONSORING,
extractEip2612GasSponsoringInfo,
extractErc20ApprovalGasSponsoringInfo,
validateEip2612GasSponsoringInfo,
validateErc20ApprovalGasSponsoringInfo,
} from "@x402/extensions";
import type {
DataSettlementLogJobData,
SettlementBatchData,
SettlementIndividualData,
} from "./all_networks_types_helpers.js";
type LogSummaryValue = boolean | number | string | undefined;
type LogSummary = Record<string, LogSummaryValue>;
export const DEBUG_LOGGING_ENABLED = process.env.FACILITATOR_DEBUG === "true";
function shortenValue(
value: string | undefined,
prefixLength = 10,
suffixLength = 6,
): string | undefined {
if (!value) {
return undefined;
}
if (value.length <= prefixLength + suffixLength + 3) {
return value;
}
return `${value.slice(0, prefixLength)}...${value.slice(-suffixLength)}`;
}
function summarizeObjectShape(value: unknown): string {
if (value === null) {
return "null";
}
if (Array.isArray(value)) {
return `array(${value.length})`;
}
if (typeof value === "object") {
const keys = Object.keys(value as Record<string, unknown>);
return keys.length > 0 ? `object(${keys.slice(0, 5).join(",")})` : "object(empty)";
}
if (typeof value === "string") {
return `string(${value.length})`;
}
return typeof value;
}
function summarizeSettlementBatchData(data: SettlementBatchData): LogSummary {
return {
teeId: shortenValue(data.teeId),
inputHash: shortenValue(data.inputHash),
outputHash: shortenValue(data.outputHash),
timestamp: data.timestamp,
};
}
function summarizeSettlementIndividualData(data: SettlementIndividualData): LogSummary {
return {
...summarizeSettlementBatchData(data),
ethAddress: shortenValue(data.ethAddress),
inputShape: summarizeObjectShape(data.input),
outputShape: summarizeObjectShape(data.output),
};
}
function asRecord(value: unknown): Record<string, unknown> | null {
if (!value || typeof value !== "object" || Array.isArray(value)) {
return null;
}
return value as Record<string, unknown>;
}
function summarizeExtensionInfoShape(extension: unknown): string | undefined {
const extensionRecord = asRecord(extension);
if (!extensionRecord) {
return undefined;
}
const infoRecord = asRecord(extensionRecord.info);
if (!infoRecord) {
return undefined;
}
const keys = Object.keys(infoRecord);
return keys.length > 0 ? keys.join(",") : undefined;
}
function diagnoseEip2612Extension(paymentPayload: PaymentPayload): LogSummary {
const extension = paymentPayload.extensions?.[EIP2612_GAS_SPONSORING.key];
if (!extension) {
return {};
}
const infoShape = summarizeExtensionInfoShape(extension);
const info = extractEip2612GasSponsoringInfo(paymentPayload);
if (info) {
return {
eip2612State: validateEip2612GasSponsoringInfo(info)
? "client-signed"
: "client-signed-invalid",
eip2612InfoShape: infoShape,
};
}
return {
eip2612State:
infoShape === "description,version" ? "server-declared-only" : "missing-required-fields",
eip2612InfoShape: infoShape,
probableIssue:
"eip2612 declared by server but signed permit fields are missing from paymentPayload.extensions",
};
}
function diagnoseErc20ApprovalExtension(paymentPayload: PaymentPayload): LogSummary {
const extension = paymentPayload.extensions?.[ERC20_APPROVAL_GAS_SPONSORING.key];
if (!extension) {
return {};
}
const infoShape = summarizeExtensionInfoShape(extension);
const info = extractErc20ApprovalGasSponsoringInfo(paymentPayload);
if (info) {
return {
erc20ApprovalState: validateErc20ApprovalGasSponsoringInfo(info)
? "client-signed"
: "client-signed-invalid",
erc20ApprovalInfoShape: infoShape,
};
}
return {
erc20ApprovalState:
infoShape === "description,version" ? "server-declared-only" : "missing-required-fields",
erc20ApprovalInfoShape: infoShape,
};
}
export function summarizePaymentRequirements(requirements: PaymentRequirements): LogSummary {
return {
scheme: requirements.scheme,
network: requirements.network,
asset: shortenValue(requirements.asset),
amount: requirements.amount,
payTo: shortenValue(requirements.payTo),
maxTimeoutSeconds: requirements.maxTimeoutSeconds,
};
}
export function summarizePaymentPayload(paymentPayload: PaymentPayload): LogSummary {
return {
x402Version: paymentPayload.x402Version,
resourceUrl: paymentPayload.resource?.url,
resourceMimeType: paymentPayload.resource?.mimeType,
acceptedScheme: paymentPayload.accepted.scheme,
acceptedNetwork: paymentPayload.accepted.network,
acceptedAsset: shortenValue(paymentPayload.accepted.asset),
acceptedAmount: paymentPayload.accepted.amount,
acceptedPayTo: shortenValue(paymentPayload.accepted.payTo),
payloadKeys: Object.keys(paymentPayload.payload).join(",") || undefined,
extensionKeys: paymentPayload.extensions
? Object.keys(paymentPayload.extensions).join(",") || undefined
: undefined,
...diagnoseEip2612Extension(paymentPayload),
...diagnoseErc20ApprovalExtension(paymentPayload),
};
}
export function summarizeVerifyResponse(result: VerifyResponse): LogSummary {
return {
isValid: result.isValid,
payer: shortenValue(result.payer),
invalidReason: result.invalidReason,
invalidMessage: result.invalidMessage,
extensionKeys: result.extensions
? Object.keys(result.extensions).join(",") || undefined
: undefined,
};
}
export function summarizeSettleResponse(result: SettleResponse): LogSummary {
return {
success: result.success,
payer: shortenValue(result.payer),
transaction: shortenValue(result.transaction),
network: result.network,
settledAmount: result.amount,
errorReason: result.errorReason,
errorMessage: result.errorMessage,
extensionKeys: result.extensions
? Object.keys(result.extensions).join(",") || undefined
: undefined,
};
}
export function summarizeError(error: unknown): LogSummary {
if (error instanceof Error) {
return {
errorName: error.name,
errorMessage: error.message,
};
}
return {
errorMessage: String(error),
};
}
export function summarizeDataSettlementJob(jobData: DataSettlementLogJobData): LogSummary {
if (jobData.settlementType === "batch") {
return {
settlementType: jobData.settlementType,
...summarizeSettlementBatchData(jobData.data),
};
}
return {
settlementType: jobData.settlementType,
...summarizeSettlementIndividualData(jobData.data),
};
}
export function debugLog(label: string, value: unknown): void {
if (!DEBUG_LOGGING_ENABLED) {
return;
}
console.log(`${label}\n${inspect(value, { depth: null, colors: false, compact: false })}`);
}