@aztec/ethereum 0.87.2 → 0.87.3-nightly.20250528

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/src/utils.ts CHANGED
@@ -168,28 +168,6 @@ export function formatViemError(error: any, abi: Abi = ErrorsAbi): FormattedViem
168
168
  const errorClone = structuredClone(error);
169
169
 
170
170
  // Helper function to recursively remove ABI properties
171
- const stripAbis = (obj: any) => {
172
- if (!obj || typeof obj !== 'object') {
173
- return;
174
- }
175
-
176
- // Delete ABI property at current level
177
- if ('abi' in obj) {
178
- delete obj.abi;
179
- }
180
-
181
- // Process cause property
182
- if (obj.cause) {
183
- stripAbis(obj.cause);
184
- }
185
-
186
- // Process arrays and objects
187
- Object.values(obj).forEach(value => {
188
- if (value && typeof value === 'object') {
189
- stripAbis(value);
190
- }
191
- });
192
- };
193
171
 
194
172
  // Strip ABIs from the clone
195
173
  stripAbis(errorClone);
@@ -203,156 +181,6 @@ export function formatViemError(error: any, abi: Abi = ErrorsAbi): FormattedViem
203
181
  return new FormattedViemError(error.message, (error as any)?.metaMessages);
204
182
  }
205
183
 
206
- const truncateHex = (hex: string, length = 100) => {
207
- if (!hex || typeof hex !== 'string') {
208
- return hex;
209
- }
210
- if (!hex.startsWith('0x')) {
211
- return hex;
212
- }
213
- if (hex.length <= length * 2) {
214
- return hex;
215
- }
216
- // For extremely large hex strings, use more aggressive truncation
217
- if (hex.length > 10000) {
218
- return `${hex.slice(0, length)}...<${hex.length - length * 2} chars omitted>...${hex.slice(-length)}`;
219
- }
220
- return `${hex.slice(0, length)}...${hex.slice(-length)}`;
221
- };
222
-
223
- const replaceHexStrings = (
224
- text: string,
225
- options: {
226
- minLength?: number;
227
- maxLength?: number;
228
- truncateLength?: number;
229
- pattern?: RegExp;
230
- transform?: (hex: string) => string;
231
- } = {},
232
- ): string => {
233
- const {
234
- minLength = 10,
235
- maxLength = Infinity,
236
- truncateLength = 100,
237
- pattern,
238
- transform = hex => truncateHex(hex, truncateLength),
239
- } = options;
240
-
241
- const hexRegex = pattern ?? new RegExp(`(0x[a-fA-F0-9]{${minLength},${maxLength}})`, 'g');
242
- return text.replace(hexRegex, match => transform(match));
243
- };
244
-
245
- const formatRequestBody = (body: string) => {
246
- try {
247
- // Special handling for eth_sendRawTransaction
248
- if (body.includes('"method":"eth_sendRawTransaction"')) {
249
- try {
250
- const parsed = JSON.parse(body);
251
- if (parsed.params && Array.isArray(parsed.params) && parsed.params.length > 0) {
252
- // These are likely large transaction hex strings
253
- parsed.params = parsed.params.map((param: any) => {
254
- if (typeof param === 'string' && param.startsWith('0x') && param.length > 1000) {
255
- return truncateHex(param, 200);
256
- }
257
- return param;
258
- });
259
- }
260
- return JSON.stringify(parsed, null, 2);
261
- } catch {
262
- // If specific parsing fails, fall back to regex-based truncation
263
- return replaceHexStrings(body, {
264
- pattern: /"params":\s*\[\s*"(0x[a-fA-F0-9]{1000,})"\s*\]/g,
265
- transform: hex => `"params":["${truncateHex(hex, 200)}"]`,
266
- });
267
- }
268
- }
269
-
270
- // For extremely large request bodies, use simple truncation instead of parsing
271
- if (body.length > 50000) {
272
- const jsonStart = body.indexOf('{');
273
- const jsonEnd = body.lastIndexOf('}');
274
- if (jsonStart >= 0 && jsonEnd > jsonStart) {
275
- return replaceHexStrings(body, { minLength: 10000, truncateLength: 200 });
276
- }
277
- }
278
-
279
- const parsed = JSON.parse(body);
280
-
281
- // Recursively process all parameters that might contain hex strings
282
- const processParams = (obj: any): any => {
283
- if (Array.isArray(obj)) {
284
- return obj.map(item => processParams(item));
285
- }
286
- if (typeof obj === 'object' && obj !== null) {
287
- const result: any = {};
288
- for (const [key, value] of Object.entries(obj)) {
289
- result[key] = processParams(value);
290
- }
291
- return result;
292
- }
293
- if (typeof obj === 'string') {
294
- if (obj.startsWith('0x')) {
295
- return truncateHex(obj);
296
- }
297
- }
298
- return obj;
299
- };
300
-
301
- // Process the entire request body
302
- const processed = processParams(parsed);
303
- return JSON.stringify(processed, null, 2);
304
- } catch {
305
- // If JSON parsing fails, do a simple truncation of any large hex strings
306
- return replaceHexStrings(body, { minLength: 1000, truncateLength: 150 });
307
- }
308
- };
309
-
310
- const extractAndFormatRequestBody = (message: string): string => {
311
- // First check if message is extremely large and contains very large hex strings
312
- if (message.length > 50000) {
313
- message = replaceHexStrings(message, { minLength: 10000, truncateLength: 200 });
314
- }
315
-
316
- // Add a specific check for RPC calls with large params
317
- if (message.includes('"method":"eth_sendRawTransaction"')) {
318
- message = replaceHexStrings(message, {
319
- pattern: /"params":\s*\[\s*"(0x[a-fA-F0-9]{1000,})"\s*\]/g,
320
- transform: hex => `"params":["${truncateHex(hex, 200)}"]`,
321
- });
322
- }
323
-
324
- // First handle Request body JSON
325
- const requestBodyRegex = /Request body: ({[\s\S]*?})\n/g;
326
- let result = message.replace(requestBodyRegex, (match, body) => {
327
- return `Request body: ${formatRequestBody(body)}\n`;
328
- });
329
-
330
- // Then handle Arguments section
331
- const argsRegex = /((?:Request |Estimate Gas )?Arguments:[\s\S]*?(?=\n\n|$))/g;
332
- result = result.replace(argsRegex, section => {
333
- const lines = section.split('\n');
334
- const processedLines = lines.map(line => {
335
- // Check if line contains a colon followed by content
336
- const colonIndex = line.indexOf(':');
337
- if (colonIndex !== -1) {
338
- const [prefix, content] = [line.slice(0, colonIndex + 1), line.slice(colonIndex + 1).trim()];
339
- // If content contains a hex string, truncate it
340
- if (content.includes('0x')) {
341
- const processedContent = replaceHexStrings(content);
342
- return `${prefix} ${processedContent}`;
343
- }
344
- }
345
- return line;
346
- });
347
- return processedLines.join('\n');
348
- });
349
-
350
- // Finally, catch any remaining hex strings in the message
351
- result = replaceHexStrings(result);
352
-
353
- return result;
354
- };
355
-
356
184
  // Extract the actual error message and highlight it for clarity
357
185
  let formattedRes = extractAndFormatRequestBody(error?.message || String(error));
358
186
 
@@ -390,6 +218,179 @@ export function formatViemError(error: any, abi: Abi = ErrorsAbi): FormattedViem
390
218
  return new FormattedViemError(formattedRes.replace(/\\n/g, '\n'), error?.metaMessages);
391
219
  }
392
220
 
221
+ function stripAbis(obj: any) {
222
+ if (!obj || typeof obj !== 'object') {
223
+ return;
224
+ }
225
+
226
+ // Delete ABI property at current level
227
+ if ('abi' in obj) {
228
+ delete obj.abi;
229
+ }
230
+
231
+ // Process cause property
232
+ if (obj.cause) {
233
+ stripAbis(obj.cause);
234
+ }
235
+
236
+ // Process arrays and objects
237
+ Object.values(obj).forEach(value => {
238
+ if (value && typeof value === 'object') {
239
+ stripAbis(value);
240
+ }
241
+ });
242
+ }
243
+
244
+ function extractAndFormatRequestBody(message: string): string {
245
+ // First check if message is extremely large and contains very large hex strings
246
+ if (message.length > 50000) {
247
+ message = replaceHexStrings(message, { minLength: 10000, truncateLength: 200 });
248
+ }
249
+
250
+ // Add a specific check for RPC calls with large params
251
+ if (message.includes('"method":"eth_sendRawTransaction"')) {
252
+ message = replaceHexStrings(message, {
253
+ pattern: /"params":\s*\[\s*"(0x[a-fA-F0-9]{1000,})"\s*\]/g,
254
+ transform: hex => `"params":["${truncateHex(hex, 200)}"]`,
255
+ });
256
+ }
257
+
258
+ // First handle Request body JSON
259
+ const requestBodyRegex = /Request body: ({[\s\S]*?})\n/g;
260
+ let result = message.replace(requestBodyRegex, (match, body) => {
261
+ return `Request body: ${formatRequestBody(body)}\n`;
262
+ });
263
+
264
+ // Then handle Arguments section
265
+ const argsRegex = /((?:Request |Estimate Gas )?Arguments:[\s\S]*?(?=\n\n|$))/g;
266
+ result = result.replace(argsRegex, section => {
267
+ const lines = section.split('\n');
268
+ const processedLines = lines.map(line => {
269
+ // Check if line contains a colon followed by content
270
+ const colonIndex = line.indexOf(':');
271
+ if (colonIndex !== -1) {
272
+ const [prefix, content] = [line.slice(0, colonIndex + 1), line.slice(colonIndex + 1).trim()];
273
+ // If content contains a hex string, truncate it
274
+ if (content.includes('0x')) {
275
+ const processedContent = replaceHexStrings(content);
276
+ return `${prefix} ${processedContent}`;
277
+ }
278
+ }
279
+ return line;
280
+ });
281
+ return processedLines.join('\n');
282
+ });
283
+
284
+ // Finally, catch any remaining hex strings in the message
285
+ result = replaceHexStrings(result);
286
+
287
+ return result;
288
+ }
289
+
290
+ function truncateHex(hex: string, length = 100) {
291
+ if (!hex || typeof hex !== 'string') {
292
+ return hex;
293
+ }
294
+ if (!hex.startsWith('0x')) {
295
+ return hex;
296
+ }
297
+ if (hex.length <= length * 2) {
298
+ return hex;
299
+ }
300
+ // For extremely large hex strings, use more aggressive truncation
301
+ if (hex.length > 10000) {
302
+ return `${hex.slice(0, length)}...<${hex.length - length * 2} chars omitted>...${hex.slice(-length)}`;
303
+ }
304
+ return `${hex.slice(0, length)}...${hex.slice(-length)}`;
305
+ }
306
+
307
+ function replaceHexStrings(
308
+ text: string,
309
+ options: {
310
+ minLength?: number;
311
+ maxLength?: number;
312
+ truncateLength?: number;
313
+ pattern?: RegExp;
314
+ transform?: (hex: string) => string;
315
+ } = {},
316
+ ): string {
317
+ const {
318
+ minLength = 10,
319
+ maxLength = Infinity,
320
+ truncateLength = 100,
321
+ pattern,
322
+ transform = hex => truncateHex(hex, truncateLength),
323
+ } = options;
324
+
325
+ const hexRegex = pattern ?? new RegExp(`(0x[a-fA-F0-9]{${minLength},${maxLength}})`, 'g');
326
+ return text.replace(hexRegex, match => transform(match));
327
+ }
328
+
329
+ function formatRequestBody(body: string) {
330
+ try {
331
+ // Special handling for eth_sendRawTransaction
332
+ if (body.includes('"method":"eth_sendRawTransaction"')) {
333
+ try {
334
+ const parsed = JSON.parse(body);
335
+ if (parsed.params && Array.isArray(parsed.params) && parsed.params.length > 0) {
336
+ // These are likely large transaction hex strings
337
+ parsed.params = parsed.params.map((param: any) => {
338
+ if (typeof param === 'string' && param.startsWith('0x') && param.length > 1000) {
339
+ return truncateHex(param, 200);
340
+ }
341
+ return param;
342
+ });
343
+ }
344
+ return JSON.stringify(parsed, null, 2);
345
+ } catch {
346
+ // If specific parsing fails, fall back to regex-based truncation
347
+ return replaceHexStrings(body, {
348
+ pattern: /"params":\s*\[\s*"(0x[a-fA-F0-9]{1000,})"\s*\]/g,
349
+ transform: hex => `"params":["${truncateHex(hex, 200)}"]`,
350
+ });
351
+ }
352
+ }
353
+
354
+ // For extremely large request bodies, use simple truncation instead of parsing
355
+ if (body.length > 50000) {
356
+ const jsonStart = body.indexOf('{');
357
+ const jsonEnd = body.lastIndexOf('}');
358
+ if (jsonStart >= 0 && jsonEnd > jsonStart) {
359
+ return replaceHexStrings(body, { minLength: 10000, truncateLength: 200 });
360
+ }
361
+ }
362
+
363
+ const parsed = JSON.parse(body);
364
+
365
+ // Process the entire request body
366
+ const processed = processParams(parsed);
367
+ return JSON.stringify(processed, null, 2);
368
+ } catch {
369
+ // If JSON parsing fails, do a simple truncation of any large hex strings
370
+ return replaceHexStrings(body, { minLength: 1000, truncateLength: 150 });
371
+ }
372
+ }
373
+
374
+ // Recursively process all parameters that might contain hex strings
375
+ function processParams(obj: any): any {
376
+ if (Array.isArray(obj)) {
377
+ return obj.map(item => processParams(item));
378
+ }
379
+ if (typeof obj === 'object' && obj !== null) {
380
+ const result: any = {};
381
+ for (const [key, value] of Object.entries(obj)) {
382
+ result[key] = processParams(value);
383
+ }
384
+ return result;
385
+ }
386
+ if (typeof obj === 'string') {
387
+ if (obj.startsWith('0x')) {
388
+ return truncateHex(obj);
389
+ }
390
+ }
391
+ return obj;
392
+ }
393
+
393
394
  export function tryGetCustomErrorName(err: any) {
394
395
  try {
395
396
  // See https://viem.sh/docs/contract/simulateContract#handling-custom-errors