@agirails/sdk 2.0.4 → 2.2.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 +536 -87
- package/dist/adapters/BasicAdapter.d.ts.map +1 -1
- package/dist/adapters/BasicAdapter.js +8 -0
- package/dist/adapters/BasicAdapter.js.map +1 -1
- package/dist/adapters/StandardAdapter.d.ts +10 -5
- package/dist/adapters/StandardAdapter.d.ts.map +1 -1
- package/dist/adapters/StandardAdapter.js +19 -6
- package/dist/adapters/StandardAdapter.js.map +1 -1
- package/dist/config/networks.d.ts +9 -0
- package/dist/config/networks.d.ts.map +1 -1
- package/dist/config/networks.js +25 -10
- package/dist/config/networks.js.map +1 -1
- package/dist/index.d.ts +6 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +31 -1
- package/dist/index.js.map +1 -1
- package/dist/level0/provide.d.ts.map +1 -1
- package/dist/level0/provide.js +2 -1
- package/dist/level0/provide.js.map +1 -1
- package/dist/level1/Agent.d.ts.map +1 -1
- package/dist/level1/Agent.js +11 -3
- package/dist/level1/Agent.js.map +1 -1
- package/dist/protocol/ACTPKernel.d.ts.map +1 -1
- package/dist/protocol/ACTPKernel.js +7 -5
- package/dist/protocol/ACTPKernel.js.map +1 -1
- package/dist/protocol/DIDResolver.js +1 -1
- package/dist/protocol/DIDResolver.js.map +1 -1
- package/dist/protocol/EASHelper.d.ts.map +1 -1
- package/dist/protocol/EASHelper.js +2 -3
- package/dist/protocol/EASHelper.js.map +1 -1
- package/dist/protocol/MessageSigner.d.ts.map +1 -1
- package/dist/protocol/MessageSigner.js +8 -8
- package/dist/protocol/MessageSigner.js.map +1 -1
- package/dist/runtime/BlockchainRuntime.d.ts +7 -0
- package/dist/runtime/BlockchainRuntime.d.ts.map +1 -1
- package/dist/runtime/BlockchainRuntime.js +38 -22
- package/dist/runtime/BlockchainRuntime.js.map +1 -1
- package/dist/runtime/IACTPRuntime.d.ts +15 -0
- package/dist/runtime/IACTPRuntime.d.ts.map +1 -1
- package/dist/runtime/MockRuntime.d.ts +7 -0
- package/dist/runtime/MockRuntime.d.ts.map +1 -1
- package/dist/runtime/MockRuntime.js +15 -4
- package/dist/runtime/MockRuntime.js.map +1 -1
- package/dist/runtime/types/MockState.d.ts +5 -2
- package/dist/runtime/types/MockState.d.ts.map +1 -1
- package/dist/runtime/types/MockState.js.map +1 -1
- package/dist/storage/ArchiveBundleBuilder.d.ts +150 -0
- package/dist/storage/ArchiveBundleBuilder.d.ts.map +1 -0
- package/dist/storage/ArchiveBundleBuilder.js +468 -0
- package/dist/storage/ArchiveBundleBuilder.js.map +1 -0
- package/dist/storage/ArweaveClient.d.ts +271 -0
- package/dist/storage/ArweaveClient.d.ts.map +1 -0
- package/dist/storage/ArweaveClient.js +761 -0
- package/dist/storage/ArweaveClient.js.map +1 -0
- package/dist/storage/FilebaseClient.d.ts +193 -0
- package/dist/storage/FilebaseClient.d.ts.map +1 -0
- package/dist/storage/FilebaseClient.js +643 -0
- package/dist/storage/FilebaseClient.js.map +1 -0
- package/dist/storage/index.d.ts +47 -0
- package/dist/storage/index.d.ts.map +1 -0
- package/dist/storage/index.js +64 -0
- package/dist/storage/index.js.map +1 -0
- package/dist/storage/types.d.ts +291 -0
- package/dist/storage/types.d.ts.map +1 -0
- package/dist/storage/types.js +18 -0
- package/dist/storage/types.js.map +1 -0
- package/dist/types/state.d.ts +5 -4
- package/dist/types/state.d.ts.map +1 -1
- package/dist/types/state.js +10 -9
- package/dist/types/state.js.map +1 -1
- package/dist/utils/IPFSClient.d.ts.map +1 -1
- package/dist/utils/IPFSClient.js +5 -2
- package/dist/utils/IPFSClient.js.map +1 -1
- package/dist/utils/NonceManager.d.ts.map +1 -1
- package/dist/utils/NonceManager.js +3 -2
- package/dist/utils/NonceManager.js.map +1 -1
- package/dist/utils/UsedAttestationTracker.d.ts.map +1 -1
- package/dist/utils/UsedAttestationTracker.js +3 -3
- package/dist/utils/UsedAttestationTracker.js.map +1 -1
- package/dist/utils/circuitBreaker.d.ts +136 -0
- package/dist/utils/circuitBreaker.d.ts.map +1 -0
- package/dist/utils/circuitBreaker.js +253 -0
- package/dist/utils/circuitBreaker.js.map +1 -0
- package/dist/utils/retry.d.ts +120 -0
- package/dist/utils/retry.d.ts.map +1 -0
- package/dist/utils/retry.js +260 -0
- package/dist/utils/retry.js.map +1 -0
- package/dist/utils/validation.d.ts +100 -0
- package/dist/utils/validation.d.ts.map +1 -1
- package/dist/utils/validation.js +248 -1
- package/dist/utils/validation.js.map +1 -1
- package/package.json +14 -2
- package/src/adapters/BasicAdapter.ts +11 -0
- package/src/adapters/StandardAdapter.ts +26 -6
- package/src/config/networks.ts +34 -10
- package/src/index.ts +54 -0
- package/src/level0/provide.ts +2 -1
- package/src/level1/Agent.ts +13 -3
- package/src/protocol/ACTPKernel.ts +7 -5
- package/src/protocol/DIDResolver.ts +1 -1
- package/src/protocol/EASHelper.ts +2 -5
- package/src/protocol/MessageSigner.ts +8 -14
- package/src/runtime/BlockchainRuntime.ts +39 -45
- package/src/runtime/IACTPRuntime.ts +16 -0
- package/src/runtime/MockRuntime.ts +16 -4
- package/src/runtime/types/MockState.ts +5 -2
- package/src/storage/ArchiveBundleBuilder.ts +563 -0
- package/src/storage/ArweaveClient.ts +945 -0
- package/src/storage/FilebaseClient.ts +790 -0
- package/src/storage/index.ts +96 -0
- package/src/storage/types.ts +348 -0
- package/src/types/state.ts +10 -9
- package/src/utils/IPFSClient.ts +5 -4
- package/src/utils/NonceManager.ts +3 -2
- package/src/utils/UsedAttestationTracker.ts +3 -5
- package/src/utils/circuitBreaker.ts +324 -0
- package/src/utils/fsSafe.ts +5 -0
- package/src/utils/retry.ts +365 -0
- package/src/utils/validation.ts +295 -1
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Retry Utility - Exponential Backoff with Jitter (P1-2)
|
|
4
|
+
*
|
|
5
|
+
* Implements retry logic for storage operations with:
|
|
6
|
+
* - Exponential backoff (doubles delay each attempt)
|
|
7
|
+
* - Random jitter (prevents thundering herd)
|
|
8
|
+
* - Maximum retry limit
|
|
9
|
+
* - Configurable retry conditions
|
|
10
|
+
*
|
|
11
|
+
* @module utils/retry
|
|
12
|
+
*/
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.withRetryResult = exports.withRetry = exports.calculateBackoffDelay = exports.isRetryableError = void 0;
|
|
15
|
+
const errors_1 = require("../errors");
|
|
16
|
+
// ============================================================================
|
|
17
|
+
// Constants
|
|
18
|
+
// ============================================================================
|
|
19
|
+
const DEFAULT_MAX_ATTEMPTS = 3;
|
|
20
|
+
const DEFAULT_INITIAL_DELAY_MS = 1000;
|
|
21
|
+
const DEFAULT_MAX_DELAY_MS = 10000; // Reduced from 30s to 10s (NEW-4: prevent long stalls)
|
|
22
|
+
const DEFAULT_BACKOFF_MULTIPLIER = 2;
|
|
23
|
+
const DEFAULT_JITTER_FACTOR = 0.1;
|
|
24
|
+
/**
|
|
25
|
+
* Default list of retryable HTTP status codes
|
|
26
|
+
*/
|
|
27
|
+
const RETRYABLE_STATUS_CODES = new Set([
|
|
28
|
+
408, // Request Timeout
|
|
29
|
+
429, // Too Many Requests
|
|
30
|
+
500, // Internal Server Error
|
|
31
|
+
502, // Bad Gateway
|
|
32
|
+
503, // Service Unavailable
|
|
33
|
+
504 // Gateway Timeout
|
|
34
|
+
]);
|
|
35
|
+
/**
|
|
36
|
+
* Default list of retryable error codes
|
|
37
|
+
*/
|
|
38
|
+
const RETRYABLE_ERROR_CODES = new Set([
|
|
39
|
+
'ECONNRESET',
|
|
40
|
+
'ECONNREFUSED',
|
|
41
|
+
'ETIMEDOUT',
|
|
42
|
+
'ENOTFOUND',
|
|
43
|
+
'ENETUNREACH',
|
|
44
|
+
'EAI_AGAIN',
|
|
45
|
+
'EPIPE',
|
|
46
|
+
'EHOSTUNREACH'
|
|
47
|
+
]);
|
|
48
|
+
// ============================================================================
|
|
49
|
+
// Functions
|
|
50
|
+
// ============================================================================
|
|
51
|
+
/**
|
|
52
|
+
* Default function to determine if error is retryable
|
|
53
|
+
*
|
|
54
|
+
* @param error - Error to check
|
|
55
|
+
* @returns True if error should trigger retry
|
|
56
|
+
*/
|
|
57
|
+
function isRetryableError(error) {
|
|
58
|
+
if (!error)
|
|
59
|
+
return false;
|
|
60
|
+
// Rate limit errors are always retryable
|
|
61
|
+
if (error instanceof errors_1.StorageRateLimitError) {
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
const e = error;
|
|
65
|
+
// Check HTTP status codes
|
|
66
|
+
if (e.statusCode && RETRYABLE_STATUS_CODES.has(e.statusCode)) {
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
if (e.status && RETRYABLE_STATUS_CODES.has(e.status)) {
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
// Check error codes (network errors)
|
|
73
|
+
if (e.code && RETRYABLE_ERROR_CODES.has(e.code)) {
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
76
|
+
// Check for timeout errors
|
|
77
|
+
if (e.name === 'AbortError' || e.name === 'TimeoutError') {
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
// Check message for common retryable patterns
|
|
81
|
+
const message = String(e.message || e).toLowerCase();
|
|
82
|
+
if (message.includes('timeout') ||
|
|
83
|
+
message.includes('rate limit') ||
|
|
84
|
+
message.includes('too many requests') ||
|
|
85
|
+
message.includes('service unavailable') ||
|
|
86
|
+
message.includes('temporarily unavailable')) {
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
exports.isRetryableError = isRetryableError;
|
|
92
|
+
/**
|
|
93
|
+
* Calculate delay with exponential backoff and jitter
|
|
94
|
+
*
|
|
95
|
+
* @param attempt - Current attempt number (1-based)
|
|
96
|
+
* @param options - Retry options
|
|
97
|
+
* @returns Delay in milliseconds
|
|
98
|
+
*/
|
|
99
|
+
function calculateBackoffDelay(attempt, options = {}) {
|
|
100
|
+
const { initialDelayMs = DEFAULT_INITIAL_DELAY_MS, maxDelayMs = DEFAULT_MAX_DELAY_MS, backoffMultiplier = DEFAULT_BACKOFF_MULTIPLIER, jitterFactor = DEFAULT_JITTER_FACTOR } = options;
|
|
101
|
+
// Exponential backoff: initialDelay * (multiplier ^ (attempt - 1))
|
|
102
|
+
const exponentialDelay = initialDelayMs * Math.pow(backoffMultiplier, attempt - 1);
|
|
103
|
+
// Cap at max delay
|
|
104
|
+
const cappedDelay = Math.min(exponentialDelay, maxDelayMs);
|
|
105
|
+
// Add jitter: ±jitterFactor
|
|
106
|
+
const jitter = cappedDelay * jitterFactor * (Math.random() * 2 - 1);
|
|
107
|
+
const finalDelay = Math.max(0, cappedDelay + jitter);
|
|
108
|
+
return Math.round(finalDelay);
|
|
109
|
+
}
|
|
110
|
+
exports.calculateBackoffDelay = calculateBackoffDelay;
|
|
111
|
+
/**
|
|
112
|
+
* Sleep for specified duration
|
|
113
|
+
*
|
|
114
|
+
* @param ms - Milliseconds to sleep
|
|
115
|
+
*/
|
|
116
|
+
function sleep(ms) {
|
|
117
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Execute async operation with retry logic
|
|
121
|
+
*
|
|
122
|
+
* Implements exponential backoff with jitter for handling transient failures.
|
|
123
|
+
* Particularly useful for storage operations that may fail due to:
|
|
124
|
+
* - Rate limiting
|
|
125
|
+
* - Network timeouts
|
|
126
|
+
* - Temporary service unavailability
|
|
127
|
+
*
|
|
128
|
+
* @param operation - Async function to execute
|
|
129
|
+
* @param options - Retry configuration
|
|
130
|
+
* @returns Promise resolving to operation result or throwing final error
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* ```typescript
|
|
134
|
+
* // Basic usage
|
|
135
|
+
* const result = await withRetry(
|
|
136
|
+
* () => client.uploadJSON(data),
|
|
137
|
+
* { maxAttempts: 3 }
|
|
138
|
+
* );
|
|
139
|
+
*
|
|
140
|
+
* // With logging
|
|
141
|
+
* const result = await withRetry(
|
|
142
|
+
* () => client.downloadJSON(cid),
|
|
143
|
+
* {
|
|
144
|
+
* maxAttempts: 5,
|
|
145
|
+
* onRetry: (attempt, error, delay) => {
|
|
146
|
+
* console.log(`Retry ${attempt}, waiting ${delay}ms:`, error);
|
|
147
|
+
* }
|
|
148
|
+
* }
|
|
149
|
+
* );
|
|
150
|
+
* ```
|
|
151
|
+
*/
|
|
152
|
+
async function withRetry(operation, options = {}) {
|
|
153
|
+
const { maxAttempts = DEFAULT_MAX_ATTEMPTS, isRetryable = isRetryableError, onRetry } = options;
|
|
154
|
+
let lastError;
|
|
155
|
+
const startTime = Date.now();
|
|
156
|
+
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
|
157
|
+
try {
|
|
158
|
+
return await operation();
|
|
159
|
+
}
|
|
160
|
+
catch (error) {
|
|
161
|
+
lastError = error;
|
|
162
|
+
// Check if this is the last attempt
|
|
163
|
+
if (attempt >= maxAttempts) {
|
|
164
|
+
break;
|
|
165
|
+
}
|
|
166
|
+
// Check if error is retryable
|
|
167
|
+
if (!isRetryable(error)) {
|
|
168
|
+
break;
|
|
169
|
+
}
|
|
170
|
+
// Calculate delay
|
|
171
|
+
let delayMs = calculateBackoffDelay(attempt, options);
|
|
172
|
+
// If rate limit error with retry-after header, use that instead
|
|
173
|
+
if (error instanceof errors_1.StorageRateLimitError && error.details?.retryAfter) {
|
|
174
|
+
delayMs = Math.max(delayMs, error.details.retryAfter * 1000);
|
|
175
|
+
}
|
|
176
|
+
// Notify callback
|
|
177
|
+
if (onRetry) {
|
|
178
|
+
onRetry(attempt, error, delayMs);
|
|
179
|
+
}
|
|
180
|
+
// Wait before retry
|
|
181
|
+
await sleep(delayMs);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
// All retries failed
|
|
185
|
+
throw lastError;
|
|
186
|
+
}
|
|
187
|
+
exports.withRetry = withRetry;
|
|
188
|
+
/**
|
|
189
|
+
* Execute async operation with retry logic, returning detailed result
|
|
190
|
+
*
|
|
191
|
+
* Unlike `withRetry`, this function never throws - it returns a result object
|
|
192
|
+
* with success/failure status and metadata.
|
|
193
|
+
*
|
|
194
|
+
* @param operation - Async function to execute
|
|
195
|
+
* @param options - Retry configuration
|
|
196
|
+
* @returns Promise resolving to detailed result object
|
|
197
|
+
*
|
|
198
|
+
* @example
|
|
199
|
+
* ```typescript
|
|
200
|
+
* const result = await withRetryResult(
|
|
201
|
+
* () => client.uploadJSON(data),
|
|
202
|
+
* { maxAttempts: 3 }
|
|
203
|
+
* );
|
|
204
|
+
*
|
|
205
|
+
* if (result.success) {
|
|
206
|
+
* console.log('Uploaded:', result.result);
|
|
207
|
+
* } else {
|
|
208
|
+
* console.error(`Failed after ${result.attempts} attempts:`, result.error);
|
|
209
|
+
* }
|
|
210
|
+
* ```
|
|
211
|
+
*/
|
|
212
|
+
async function withRetryResult(operation, options = {}) {
|
|
213
|
+
const { maxAttempts = DEFAULT_MAX_ATTEMPTS, isRetryable = isRetryableError, onRetry } = options;
|
|
214
|
+
let lastError;
|
|
215
|
+
const startTime = Date.now();
|
|
216
|
+
let attempts = 0;
|
|
217
|
+
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
|
218
|
+
attempts = attempt;
|
|
219
|
+
try {
|
|
220
|
+
const result = await operation();
|
|
221
|
+
return {
|
|
222
|
+
result,
|
|
223
|
+
attempts,
|
|
224
|
+
totalTimeMs: Date.now() - startTime,
|
|
225
|
+
success: true
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
catch (error) {
|
|
229
|
+
lastError = error;
|
|
230
|
+
// Check if this is the last attempt
|
|
231
|
+
if (attempt >= maxAttempts) {
|
|
232
|
+
break;
|
|
233
|
+
}
|
|
234
|
+
// Check if error is retryable
|
|
235
|
+
if (!isRetryable(error)) {
|
|
236
|
+
break;
|
|
237
|
+
}
|
|
238
|
+
// Calculate delay
|
|
239
|
+
let delayMs = calculateBackoffDelay(attempt, options);
|
|
240
|
+
// If rate limit error with retry-after header, use that instead
|
|
241
|
+
if (error instanceof errors_1.StorageRateLimitError && error.details?.retryAfter) {
|
|
242
|
+
delayMs = Math.max(delayMs, error.details.retryAfter * 1000);
|
|
243
|
+
}
|
|
244
|
+
// Notify callback
|
|
245
|
+
if (onRetry) {
|
|
246
|
+
onRetry(attempt, error, delayMs);
|
|
247
|
+
}
|
|
248
|
+
// Wait before retry
|
|
249
|
+
await sleep(delayMs);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return {
|
|
253
|
+
error: lastError,
|
|
254
|
+
attempts,
|
|
255
|
+
totalTimeMs: Date.now() - startTime,
|
|
256
|
+
success: false
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
exports.withRetryResult = withRetryResult;
|
|
260
|
+
//# sourceMappingURL=retry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"retry.js","sourceRoot":"","sources":["../../src/utils/retry.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;GAUG;;;AAEH,sCAAkD;AAoDlD,+EAA+E;AAC/E,YAAY;AACZ,+EAA+E;AAE/E,MAAM,oBAAoB,GAAG,CAAC,CAAC;AAC/B,MAAM,wBAAwB,GAAG,IAAI,CAAC;AACtC,MAAM,oBAAoB,GAAG,KAAK,CAAC,CAAC,uDAAuD;AAC3F,MAAM,0BAA0B,GAAG,CAAC,CAAC;AACrC,MAAM,qBAAqB,GAAG,GAAG,CAAC;AAElC;;GAEG;AACH,MAAM,sBAAsB,GAAG,IAAI,GAAG,CAAC;IACrC,GAAG,EAAE,kBAAkB;IACvB,GAAG,EAAE,oBAAoB;IACzB,GAAG,EAAE,wBAAwB;IAC7B,GAAG,EAAE,cAAc;IACnB,GAAG,EAAE,sBAAsB;IAC3B,GAAG,CAAE,kBAAkB;CACxB,CAAC,CAAC;AAEH;;GAEG;AACH,MAAM,qBAAqB,GAAG,IAAI,GAAG,C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,SAAS;QAChB,QAAQ;QACR,WAAW,EAAE,IAAI,CAAC,GAAG,EAAE,GAAG,SAAS;QACnC,OAAO,EAAE,KAAK;KACf,CAAC;AACJ,CAAC;AA9DD,0CA8DC"}
|
|
@@ -1,6 +1,30 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Input validation utilities
|
|
3
3
|
*/
|
|
4
|
+
/** Ethereum address validation pattern */
|
|
5
|
+
export declare const ADDRESS_PATTERN: RegExp;
|
|
6
|
+
/** Transaction ID (bytes32) validation pattern */
|
|
7
|
+
export declare const TX_ID_PATTERN: RegExp;
|
|
8
|
+
/** Hash (bytes32) validation pattern */
|
|
9
|
+
export declare const HASH_PATTERN: RegExp;
|
|
10
|
+
/** Signature (65 bytes = 130 hex chars) validation pattern */
|
|
11
|
+
export declare const SIGNATURE_PATTERN: RegExp;
|
|
12
|
+
/** CID validation pattern (CIDv0 or CIDv1) */
|
|
13
|
+
export declare const CID_PATTERN: RegExp;
|
|
14
|
+
/** Arweave TX ID pattern (43 characters, base64url) */
|
|
15
|
+
export declare const ARWEAVE_TX_ID_PATTERN: RegExp;
|
|
16
|
+
/** Semver pattern for version validation */
|
|
17
|
+
export declare const SEMVER_PATTERN: RegExp;
|
|
18
|
+
/**
|
|
19
|
+
* Allowed IPFS gateway domains
|
|
20
|
+
* Only whitelisted gateways are allowed for downloads
|
|
21
|
+
*/
|
|
22
|
+
export declare const ALLOWED_IPFS_GATEWAYS: readonly ["ipfs.filebase.io", "gateway.pinata.cloud", "cloudflare-ipfs.com", "ipfs.io", "dweb.link", "w3s.link", "nftstorage.link"];
|
|
23
|
+
/**
|
|
24
|
+
* Allowed Arweave gateway domains
|
|
25
|
+
* Only whitelisted gateways are allowed for downloads
|
|
26
|
+
*/
|
|
27
|
+
export declare const ALLOWED_ARWEAVE_GATEWAYS: readonly ["arweave.net", "gateway.irys.xyz", "arweave.dev"];
|
|
4
28
|
/**
|
|
5
29
|
* Validate Ethereum address
|
|
6
30
|
*/
|
|
@@ -43,4 +67,80 @@ export declare function validateTxId(txId: string, fieldName?: string): void;
|
|
|
43
67
|
* @throws {ValidationError} If endpoint is invalid or points to private IP
|
|
44
68
|
*/
|
|
45
69
|
export declare function validateEndpointURL(endpoint: string, fieldName?: string): Promise<void>;
|
|
70
|
+
/**
|
|
71
|
+
* Validate IPFS CID format
|
|
72
|
+
*
|
|
73
|
+
* @param cid - IPFS CID to validate
|
|
74
|
+
* @param fieldName - Field name for error messages
|
|
75
|
+
* @throws {InvalidCIDError} If CID is invalid
|
|
76
|
+
*/
|
|
77
|
+
export declare function validateCID(cid: string, fieldName?: string): void;
|
|
78
|
+
/**
|
|
79
|
+
* Validate Arweave transaction ID format
|
|
80
|
+
*
|
|
81
|
+
* @param txId - Arweave TX ID to validate
|
|
82
|
+
* @param fieldName - Field name for error messages
|
|
83
|
+
* @throws {InvalidArweaveTxIdError} If TX ID is invalid
|
|
84
|
+
*/
|
|
85
|
+
export declare function validateArweaveTxId(txId: string, fieldName?: string): void;
|
|
86
|
+
/**
|
|
87
|
+
* Validate gateway URL against whitelist (SSRF Protection - P0-1)
|
|
88
|
+
*
|
|
89
|
+
* SECURITY FIX: Only allow downloads from whitelisted gateway domains.
|
|
90
|
+
* This prevents SSRF attacks where attacker controls the gateway URL.
|
|
91
|
+
*
|
|
92
|
+
* @param url - Full gateway URL to validate
|
|
93
|
+
* @param allowedGateways - List of allowed gateway domains
|
|
94
|
+
* @param fieldName - Field name for error messages
|
|
95
|
+
* @throws {ValidationError} If gateway is not whitelisted
|
|
96
|
+
*/
|
|
97
|
+
export declare function validateGatewayURL(url: string, allowedGateways: readonly string[], fieldName?: string): void;
|
|
98
|
+
/**
|
|
99
|
+
* Validate semver version string
|
|
100
|
+
*
|
|
101
|
+
* @param version - Version string to validate
|
|
102
|
+
* @param fieldName - Field name for error messages
|
|
103
|
+
* @throws {ValidationError} If version is invalid
|
|
104
|
+
*/
|
|
105
|
+
export declare function validateSemver(version: string, fieldName?: string): void;
|
|
106
|
+
/**
|
|
107
|
+
* Validate hash (bytes32) format
|
|
108
|
+
*
|
|
109
|
+
* @param hash - Hash to validate
|
|
110
|
+
* @param fieldName - Field name for error messages
|
|
111
|
+
* @throws {ValidationError} If hash is invalid
|
|
112
|
+
*/
|
|
113
|
+
export declare function validateHash(hash: string, fieldName?: string): void;
|
|
114
|
+
/**
|
|
115
|
+
* Validate signature format (65 bytes)
|
|
116
|
+
*
|
|
117
|
+
* @param signature - Signature to validate
|
|
118
|
+
* @param fieldName - Field name for error messages
|
|
119
|
+
* @throws {ValidationError} If signature is invalid
|
|
120
|
+
*/
|
|
121
|
+
export declare function validateSignature(signature: string, fieldName?: string): void;
|
|
122
|
+
/**
|
|
123
|
+
* Sanitize error messages to remove sensitive data
|
|
124
|
+
*
|
|
125
|
+
* SECURITY FIX (P0-2): Removes credentials, private keys, and other
|
|
126
|
+
* sensitive data from error messages before logging/returning.
|
|
127
|
+
*
|
|
128
|
+
* @param error - Error to sanitize
|
|
129
|
+
* @returns Sanitized error message
|
|
130
|
+
*/
|
|
131
|
+
export declare function sanitizeErrorMessage(error: unknown): string;
|
|
132
|
+
/**
|
|
133
|
+
* Create a safe error object for external consumption
|
|
134
|
+
*
|
|
135
|
+
* SECURITY FIX (P0-2): Returns error without stack trace or sensitive details
|
|
136
|
+
*
|
|
137
|
+
* @param error - Original error
|
|
138
|
+
* @param operation - What operation failed
|
|
139
|
+
* @returns Safe error object
|
|
140
|
+
*/
|
|
141
|
+
export declare function createSafeError(error: unknown, operation: string): {
|
|
142
|
+
message: string;
|
|
143
|
+
code: string;
|
|
144
|
+
operation: string;
|
|
145
|
+
};
|
|
46
146
|
//# sourceMappingURL=validation.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../../src/utils/validation.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../../src/utils/validation.ts"],"names":[],"mappings":"AASA;;GAEG;A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AQxE;AAED;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,GAAE,MAAe,GAAG,IAAI,CAWlF;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,kBAAkB,CAChC,GAAG,EAAE,MAAM,EACX,eAAe,EAAE,SAAS,MAAM,EAAE,EAClC,SAAS,GAAE,MAAqB,GAC/B,IAAI,CA0CN;AAED;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,MAAM,EAAE,SAAS,GAAE,MAAkB,GAAG,IAAI,CAQnF;AAED;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,GAAE,MAAe,GAAG,IAAI,CAQ3E;AAED;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,SAAS,EAAE,MAAM,EAAE,SAAS,GAAE,MAAoB,GAAG,IAAI,CAW1F;AAMD;;;;;;;;GAQG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAsC3D;AAED;;;;;;;;GAQG;AACH,wBAAgB,eAAe,CAC7B,KAAK,EAAE,OAAO,EACd,SAAS,EAAE,MAAM,GAChB;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,CAStD"}
|
package/dist/utils/validation.js
CHANGED
|
@@ -23,12 +23,57 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.validateEndpointURL = exports.validateTxId = exports.validateDisputeWindow = exports.validateDeadline = exports.validateAmount = exports.validateAddress = void 0;
|
|
26
|
+
exports.createSafeError = exports.sanitizeErrorMessage = exports.validateSignature = exports.validateHash = exports.validateSemver = exports.validateGatewayURL = exports.validateArweaveTxId = exports.validateCID = exports.validateEndpointURL = exports.validateTxId = exports.validateDisputeWindow = exports.validateDeadline = exports.validateAmount = exports.validateAddress = exports.ALLOWED_ARWEAVE_GATEWAYS = exports.ALLOWED_IPFS_GATEWAYS = exports.SEMVER_PATTERN = exports.ARWEAVE_TX_ID_PATTERN = exports.CID_PATTERN = exports.SIGNATURE_PATTERN = exports.HASH_PATTERN = exports.TX_ID_PATTERN = exports.ADDRESS_PATTERN = void 0;
|
|
27
27
|
const ethers_1 = require("ethers");
|
|
28
28
|
const errors_1 = require("../errors");
|
|
29
29
|
/**
|
|
30
30
|
* Input validation utilities
|
|
31
31
|
*/
|
|
32
|
+
// ============================================================================
|
|
33
|
+
// Shared Validation Patterns (AIP-7 Storage)
|
|
34
|
+
// ============================================================================
|
|
35
|
+
/** Ethereum address validation pattern */
|
|
36
|
+
exports.ADDRESS_PATTERN = /^0x[a-fA-F0-9]{40}$/;
|
|
37
|
+
/** Transaction ID (bytes32) validation pattern */
|
|
38
|
+
exports.TX_ID_PATTERN = /^0x[a-fA-F0-9]{64}$/;
|
|
39
|
+
/** Hash (bytes32) validation pattern */
|
|
40
|
+
exports.HASH_PATTERN = /^0x[a-fA-F0-9]{64}$/;
|
|
41
|
+
/** Signature (65 bytes = 130 hex chars) validation pattern */
|
|
42
|
+
exports.SIGNATURE_PATTERN = /^0x[a-fA-F0-9]{130}$/;
|
|
43
|
+
/** CID validation pattern (CIDv0 or CIDv1) */
|
|
44
|
+
exports.CID_PATTERN = /^(Qm[1-9A-HJ-NP-Za-km-z]{44}|b[a-z2-7]{58,})$/;
|
|
45
|
+
/** Arweave TX ID pattern (43 characters, base64url) */
|
|
46
|
+
exports.ARWEAVE_TX_ID_PATTERN = /^[a-zA-Z0-9_-]{43}$/;
|
|
47
|
+
/** Semver pattern for version validation */
|
|
48
|
+
exports.SEMVER_PATTERN = /^\d+\.\d+\.\d+$/;
|
|
49
|
+
// ============================================================================
|
|
50
|
+
// Gateway URL Whitelist (SSRF Protection - P0-1)
|
|
51
|
+
// ============================================================================
|
|
52
|
+
/**
|
|
53
|
+
* Allowed IPFS gateway domains
|
|
54
|
+
* Only whitelisted gateways are allowed for downloads
|
|
55
|
+
*/
|
|
56
|
+
exports.ALLOWED_IPFS_GATEWAYS = [
|
|
57
|
+
'ipfs.filebase.io',
|
|
58
|
+
'gateway.pinata.cloud',
|
|
59
|
+
'cloudflare-ipfs.com',
|
|
60
|
+
'ipfs.io',
|
|
61
|
+
'dweb.link',
|
|
62
|
+
'w3s.link',
|
|
63
|
+
'nftstorage.link'
|
|
64
|
+
];
|
|
65
|
+
/**
|
|
66
|
+
* Allowed Arweave gateway domains
|
|
67
|
+
* Only whitelisted gateways are allowed for downloads
|
|
68
|
+
*/
|
|
69
|
+
exports.ALLOWED_ARWEAVE_GATEWAYS = [
|
|
70
|
+
'arweave.net',
|
|
71
|
+
'gateway.irys.xyz',
|
|
72
|
+
'arweave.dev'
|
|
73
|
+
];
|
|
74
|
+
// ============================================================================
|
|
75
|
+
// Validation Functions
|
|
76
|
+
// ============================================================================
|
|
32
77
|
/**
|
|
33
78
|
* Validate Ethereum address
|
|
34
79
|
*/
|
|
@@ -116,13 +161,19 @@ function isPrivateIP(ip) {
|
|
|
116
161
|
}
|
|
117
162
|
}
|
|
118
163
|
// IPv6 patterns (without brackets)
|
|
164
|
+
// Includes both standard ::ffff: and alternative ::ffff:0: notation (NEW-2 fix)
|
|
119
165
|
const ipv6PrivatePatterns = [
|
|
120
166
|
/^::1$/, // IPv6 loopback
|
|
121
167
|
/^::ffff:127\./, // IPv4-mapped localhost
|
|
168
|
+
/^::ffff:0:127\./, // Alternative IPv4-mapped localhost
|
|
122
169
|
/^::ffff:10\./, // IPv4-mapped private 10.x
|
|
170
|
+
/^::ffff:0:10\./, // Alternative IPv4-mapped private 10.x
|
|
123
171
|
/^::ffff:192\.168\./, // IPv4-mapped private 192.168.x
|
|
172
|
+
/^::ffff:0:192\.168\./, // Alternative IPv4-mapped private 192.168.x
|
|
124
173
|
/^::ffff:172\.(1[6-9]|2\d|3[01])\./, // IPv4-mapped private 172.16-31.x
|
|
174
|
+
/^::ffff:0:172\.(1[6-9]|2\d|3[01])\./, // Alternative IPv4-mapped private 172.16-31.x
|
|
125
175
|
/^::ffff:169\.254\./, // IPv4-mapped link-local (CRITICAL: AWS metadata)
|
|
176
|
+
/^::ffff:0:169\.254\./, // Alternative IPv4-mapped link-local
|
|
126
177
|
/^fc00:/i, // IPv6 ULA fc00::/7
|
|
127
178
|
/^fd/i, // IPv6 ULA fd00::/8
|
|
128
179
|
/^fe80:/i // IPv6 link-local fe80::/10
|
|
@@ -220,4 +271,200 @@ async function validateEndpointURL(endpoint, fieldName = 'endpoint') {
|
|
|
220
271
|
// IPFS endpoints skip DNS check (no DNS resolution for IPFS CIDs)
|
|
221
272
|
}
|
|
222
273
|
exports.validateEndpointURL = validateEndpointURL;
|
|
274
|
+
// ============================================================================
|
|
275
|
+
// Storage Validation Functions (AIP-7)
|
|
276
|
+
// ============================================================================
|
|
277
|
+
/**
|
|
278
|
+
* Validate IPFS CID format
|
|
279
|
+
*
|
|
280
|
+
* @param cid - IPFS CID to validate
|
|
281
|
+
* @param fieldName - Field name for error messages
|
|
282
|
+
* @throws {InvalidCIDError} If CID is invalid
|
|
283
|
+
*/
|
|
284
|
+
function validateCID(cid, fieldName = 'cid') {
|
|
285
|
+
if (!cid || typeof cid !== 'string') {
|
|
286
|
+
throw new errors_1.InvalidCIDError(String(cid), 'CID is required');
|
|
287
|
+
}
|
|
288
|
+
if (!exports.CID_PATTERN.test(cid)) {
|
|
289
|
+
throw new errors_1.InvalidCIDError(cid, 'Invalid CID format (expected CIDv0 Qm... or CIDv1 bafy...)');
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
exports.validateCID = validateCID;
|
|
293
|
+
/**
|
|
294
|
+
* Validate Arweave transaction ID format
|
|
295
|
+
*
|
|
296
|
+
* @param txId - Arweave TX ID to validate
|
|
297
|
+
* @param fieldName - Field name for error messages
|
|
298
|
+
* @throws {InvalidArweaveTxIdError} If TX ID is invalid
|
|
299
|
+
*/
|
|
300
|
+
function validateArweaveTxId(txId, fieldName = 'txId') {
|
|
301
|
+
if (!txId || typeof txId !== 'string') {
|
|
302
|
+
throw new errors_1.InvalidArweaveTxIdError(String(txId), 'TX ID is required');
|
|
303
|
+
}
|
|
304
|
+
if (!exports.ARWEAVE_TX_ID_PATTERN.test(txId)) {
|
|
305
|
+
throw new errors_1.InvalidArweaveTxIdError(txId, 'Invalid format (expected 43 character base64url string)');
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
exports.validateArweaveTxId = validateArweaveTxId;
|
|
309
|
+
/**
|
|
310
|
+
* Validate gateway URL against whitelist (SSRF Protection - P0-1)
|
|
311
|
+
*
|
|
312
|
+
* SECURITY FIX: Only allow downloads from whitelisted gateway domains.
|
|
313
|
+
* This prevents SSRF attacks where attacker controls the gateway URL.
|
|
314
|
+
*
|
|
315
|
+
* @param url - Full gateway URL to validate
|
|
316
|
+
* @param allowedGateways - List of allowed gateway domains
|
|
317
|
+
* @param fieldName - Field name for error messages
|
|
318
|
+
* @throws {ValidationError} If gateway is not whitelisted
|
|
319
|
+
*/
|
|
320
|
+
function validateGatewayURL(url, allowedGateways, fieldName = 'gatewayUrl') {
|
|
321
|
+
if (!url || typeof url !== 'string') {
|
|
322
|
+
throw new errors_1.ValidationError(fieldName, 'Gateway URL is required');
|
|
323
|
+
}
|
|
324
|
+
let parsedUrl;
|
|
325
|
+
try {
|
|
326
|
+
parsedUrl = new URL(url);
|
|
327
|
+
}
|
|
328
|
+
catch {
|
|
329
|
+
throw new errors_1.ValidationError(fieldName, 'Invalid URL format');
|
|
330
|
+
}
|
|
331
|
+
// Must be HTTPS
|
|
332
|
+
if (parsedUrl.protocol !== 'https:') {
|
|
333
|
+
throw new errors_1.ValidationError(fieldName, 'Gateway URL must use HTTPS');
|
|
334
|
+
}
|
|
335
|
+
// NEW-3: Validate port (must be 443 or default, prevents port bypass attacks)
|
|
336
|
+
const port = parsedUrl.port;
|
|
337
|
+
if (port && !['443', ''].includes(port)) {
|
|
338
|
+
throw new errors_1.ValidationError(fieldName, `Gateway URL must use standard HTTPS port (443). Found port: ${port}. ` +
|
|
339
|
+
`Non-standard ports may bypass whitelist intent.`);
|
|
340
|
+
}
|
|
341
|
+
// Check hostname against whitelist
|
|
342
|
+
const hostname = parsedUrl.hostname.toLowerCase();
|
|
343
|
+
const isAllowed = allowedGateways.some(gateway => hostname === gateway.toLowerCase() ||
|
|
344
|
+
hostname.endsWith('.' + gateway.toLowerCase()));
|
|
345
|
+
if (!isAllowed) {
|
|
346
|
+
throw new errors_1.ValidationError(fieldName, `Gateway "${hostname}" is not in the allowed list. ` +
|
|
347
|
+
`Allowed gateways: ${allowedGateways.join(', ')}. ` +
|
|
348
|
+
`This restriction prevents SSRF attacks.`);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
exports.validateGatewayURL = validateGatewayURL;
|
|
352
|
+
/**
|
|
353
|
+
* Validate semver version string
|
|
354
|
+
*
|
|
355
|
+
* @param version - Version string to validate
|
|
356
|
+
* @param fieldName - Field name for error messages
|
|
357
|
+
* @throws {ValidationError} If version is invalid
|
|
358
|
+
*/
|
|
359
|
+
function validateSemver(version, fieldName = 'version') {
|
|
360
|
+
if (!version || typeof version !== 'string') {
|
|
361
|
+
throw new errors_1.ValidationError(fieldName, 'Version is required');
|
|
362
|
+
}
|
|
363
|
+
if (!exports.SEMVER_PATTERN.test(version)) {
|
|
364
|
+
throw new errors_1.ValidationError(fieldName, 'Must be semver format (e.g., 1.0.0)');
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
exports.validateSemver = validateSemver;
|
|
368
|
+
/**
|
|
369
|
+
* Validate hash (bytes32) format
|
|
370
|
+
*
|
|
371
|
+
* @param hash - Hash to validate
|
|
372
|
+
* @param fieldName - Field name for error messages
|
|
373
|
+
* @throws {ValidationError} If hash is invalid
|
|
374
|
+
*/
|
|
375
|
+
function validateHash(hash, fieldName = 'hash') {
|
|
376
|
+
if (!hash || typeof hash !== 'string') {
|
|
377
|
+
throw new errors_1.ValidationError(fieldName, 'Hash is required');
|
|
378
|
+
}
|
|
379
|
+
if (!exports.HASH_PATTERN.test(hash)) {
|
|
380
|
+
throw new errors_1.ValidationError(fieldName, 'Invalid hash format (expected bytes32 hex string)');
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
exports.validateHash = validateHash;
|
|
384
|
+
/**
|
|
385
|
+
* Validate signature format (65 bytes)
|
|
386
|
+
*
|
|
387
|
+
* @param signature - Signature to validate
|
|
388
|
+
* @param fieldName - Field name for error messages
|
|
389
|
+
* @throws {ValidationError} If signature is invalid
|
|
390
|
+
*/
|
|
391
|
+
function validateSignature(signature, fieldName = 'signature') {
|
|
392
|
+
if (!signature || typeof signature !== 'string') {
|
|
393
|
+
throw new errors_1.ValidationError(fieldName, 'Signature is required');
|
|
394
|
+
}
|
|
395
|
+
if (!exports.SIGNATURE_PATTERN.test(signature)) {
|
|
396
|
+
throw new errors_1.ValidationError(fieldName, 'Invalid signature format (expected 65 bytes = 0x + 130 hex chars)');
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
exports.validateSignature = validateSignature;
|
|
400
|
+
// ============================================================================
|
|
401
|
+
// Error Sanitization (P0-2)
|
|
402
|
+
// ============================================================================
|
|
403
|
+
/**
|
|
404
|
+
* Sanitize error messages to remove sensitive data
|
|
405
|
+
*
|
|
406
|
+
* SECURITY FIX (P0-2): Removes credentials, private keys, and other
|
|
407
|
+
* sensitive data from error messages before logging/returning.
|
|
408
|
+
*
|
|
409
|
+
* @param error - Error to sanitize
|
|
410
|
+
* @returns Sanitized error message
|
|
411
|
+
*/
|
|
412
|
+
function sanitizeErrorMessage(error) {
|
|
413
|
+
if (!error)
|
|
414
|
+
return 'Unknown error';
|
|
415
|
+
let message = '';
|
|
416
|
+
if (error instanceof Error) {
|
|
417
|
+
message = error.message;
|
|
418
|
+
}
|
|
419
|
+
else if (typeof error === 'string') {
|
|
420
|
+
message = error;
|
|
421
|
+
}
|
|
422
|
+
else {
|
|
423
|
+
message = String(error);
|
|
424
|
+
}
|
|
425
|
+
// Patterns to redact
|
|
426
|
+
const sensitivePatterns = [
|
|
427
|
+
// Private keys (hex)
|
|
428
|
+
/0x[a-fA-F0-9]{64}/g,
|
|
429
|
+
// AWS access key IDs
|
|
430
|
+
/AKIA[0-9A-Z]{16}/g,
|
|
431
|
+
// AWS secret keys (40 chars)
|
|
432
|
+
/[a-zA-Z0-9/+=]{40}/g,
|
|
433
|
+
// Bearer tokens
|
|
434
|
+
/Bearer\s+[a-zA-Z0-9._-]+/gi,
|
|
435
|
+
// API keys (generic pattern)
|
|
436
|
+
/api[_-]?key[=:]\s*["']?[a-zA-Z0-9_-]+["']?/gi,
|
|
437
|
+
// Secret in URL query params
|
|
438
|
+
/secret[=][^&\s]+/gi,
|
|
439
|
+
// Password in URL
|
|
440
|
+
/password[=][^&\s]+/gi,
|
|
441
|
+
// Authorization headers
|
|
442
|
+
/authorization[=:]\s*["']?[^"'\s]+["']?/gi
|
|
443
|
+
];
|
|
444
|
+
let sanitized = message;
|
|
445
|
+
for (const pattern of sensitivePatterns) {
|
|
446
|
+
sanitized = sanitized.replace(pattern, '[REDACTED]');
|
|
447
|
+
}
|
|
448
|
+
return sanitized;
|
|
449
|
+
}
|
|
450
|
+
exports.sanitizeErrorMessage = sanitizeErrorMessage;
|
|
451
|
+
/**
|
|
452
|
+
* Create a safe error object for external consumption
|
|
453
|
+
*
|
|
454
|
+
* SECURITY FIX (P0-2): Returns error without stack trace or sensitive details
|
|
455
|
+
*
|
|
456
|
+
* @param error - Original error
|
|
457
|
+
* @param operation - What operation failed
|
|
458
|
+
* @returns Safe error object
|
|
459
|
+
*/
|
|
460
|
+
function createSafeError(error, operation) {
|
|
461
|
+
const sanitizedMessage = sanitizeErrorMessage(error);
|
|
462
|
+
// Don't expose internal details - generic message with operation context
|
|
463
|
+
return {
|
|
464
|
+
message: `Operation failed: ${operation}. ${sanitizedMessage}`,
|
|
465
|
+
code: error?.code || 'UNKNOWN_ERROR',
|
|
466
|
+
operation
|
|
467
|
+
};
|
|
468
|
+
}
|
|
469
|
+
exports.createSafeError = createSafeError;
|
|
223
470
|
//# sourceMappingURL=validation.js.map
|