@agirails/sdk 2.5.3 → 2.5.5
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/dist/ACTPClient.d.ts +18 -0
- package/dist/ACTPClient.d.ts.map +1 -1
- package/dist/ACTPClient.js +72 -23
- package/dist/ACTPClient.js.map +1 -1
- package/dist/adapters/BasicAdapter.d.ts +15 -0
- package/dist/adapters/BasicAdapter.d.ts.map +1 -1
- package/dist/adapters/BasicAdapter.js +33 -4
- package/dist/adapters/BasicAdapter.js.map +1 -1
- package/dist/adapters/StandardAdapter.d.ts +20 -3
- package/dist/adapters/StandardAdapter.d.ts.map +1 -1
- package/dist/adapters/StandardAdapter.js +90 -12
- package/dist/adapters/StandardAdapter.js.map +1 -1
- package/dist/cli/commands/publish.js +16 -4
- package/dist/cli/commands/publish.js.map +1 -1
- package/dist/cli/commands/register.js +16 -4
- package/dist/cli/commands/register.js.map +1 -1
- package/dist/cli/commands/tx.js +31 -3
- package/dist/cli/commands/tx.js.map +1 -1
- package/dist/config/networks.d.ts +10 -2
- package/dist/config/networks.d.ts.map +1 -1
- package/dist/config/networks.js +31 -22
- package/dist/config/networks.js.map +1 -1
- package/dist/level0/request.d.ts.map +1 -1
- package/dist/level0/request.js +2 -1
- package/dist/level0/request.js.map +1 -1
- package/dist/runtime/BlockchainRuntime.d.ts.map +1 -1
- package/dist/runtime/BlockchainRuntime.js +11 -5
- package/dist/runtime/BlockchainRuntime.js.map +1 -1
- package/dist/utils/IPFSClient.d.ts +3 -1
- package/dist/utils/IPFSClient.d.ts.map +1 -1
- package/dist/utils/IPFSClient.js +27 -7
- package/dist/utils/IPFSClient.js.map +1 -1
- package/dist/wallet/AutoWalletProvider.d.ts +11 -1
- package/dist/wallet/AutoWalletProvider.d.ts.map +1 -1
- package/dist/wallet/AutoWalletProvider.js +84 -19
- package/dist/wallet/AutoWalletProvider.js.map +1 -1
- package/dist/wallet/IWalletProvider.d.ts +34 -0
- package/dist/wallet/IWalletProvider.d.ts.map +1 -1
- package/dist/wallet/SmartWalletRouter.d.ts +128 -0
- package/dist/wallet/SmartWalletRouter.d.ts.map +1 -0
- package/dist/wallet/SmartWalletRouter.js +248 -0
- package/dist/wallet/SmartWalletRouter.js.map +1 -0
- package/dist/wallet/aa/DualNonceManager.d.ts +26 -1
- package/dist/wallet/aa/DualNonceManager.d.ts.map +1 -1
- package/dist/wallet/aa/DualNonceManager.js +140 -6
- package/dist/wallet/aa/DualNonceManager.js.map +1 -1
- package/package.json +3 -6
- package/src/ACTPClient.ts +0 -1579
- package/src/abi/ACTPKernel.json +0 -1356
- package/src/abi/AgentRegistry.json +0 -915
- package/src/abi/ERC20.json +0 -40
- package/src/abi/EscrowVault.json +0 -134
- package/src/abi/IdentityRegistry.json +0 -316
- package/src/adapters/AdapterRegistry.ts +0 -173
- package/src/adapters/AdapterRouter.ts +0 -416
- package/src/adapters/BaseAdapter.ts +0 -498
- package/src/adapters/BasicAdapter.ts +0 -514
- package/src/adapters/IAdapter.ts +0 -292
- package/src/adapters/StandardAdapter.ts +0 -555
- package/src/adapters/X402Adapter.ts +0 -731
- package/src/adapters/index.ts +0 -60
- package/src/builders/DeliveryProofBuilder.ts +0 -327
- package/src/builders/QuoteBuilder.ts +0 -483
- package/src/builders/index.ts +0 -17
- package/src/cli/commands/balance.ts +0 -110
- package/src/cli/commands/batch.ts +0 -487
- package/src/cli/commands/config.ts +0 -231
- package/src/cli/commands/deploy-check.ts +0 -364
- package/src/cli/commands/deploy-env.ts +0 -120
- package/src/cli/commands/diff.ts +0 -141
- package/src/cli/commands/init.ts +0 -469
- package/src/cli/commands/mint.ts +0 -116
- package/src/cli/commands/pay.ts +0 -113
- package/src/cli/commands/publish.ts +0 -475
- package/src/cli/commands/pull.ts +0 -124
- package/src/cli/commands/register.ts +0 -247
- package/src/cli/commands/simulate.ts +0 -345
- package/src/cli/commands/time.ts +0 -302
- package/src/cli/commands/tx.ts +0 -448
- package/src/cli/commands/watch.ts +0 -211
- package/src/cli/index.ts +0 -134
- package/src/cli/utils/client.ts +0 -252
- package/src/cli/utils/config.ts +0 -389
- package/src/cli/utils/output.ts +0 -465
- package/src/cli/utils/wallet.ts +0 -109
- package/src/config/agirailsmd.ts +0 -262
- package/src/config/networks.ts +0 -275
- package/src/config/pendingPublish.ts +0 -237
- package/src/config/publishPipeline.ts +0 -359
- package/src/config/syncOperations.ts +0 -279
- package/src/erc8004/ERC8004Bridge.ts +0 -462
- package/src/erc8004/ReputationReporter.ts +0 -468
- package/src/erc8004/index.ts +0 -61
- package/src/errors/index.ts +0 -427
- package/src/index.ts +0 -364
- package/src/level0/Provider.ts +0 -117
- package/src/level0/ServiceDirectory.ts +0 -131
- package/src/level0/index.ts +0 -10
- package/src/level0/provide.ts +0 -132
- package/src/level0/request.ts +0 -432
- package/src/level1/Agent.ts +0 -1426
- package/src/level1/index.ts +0 -10
- package/src/level1/pricing/PriceCalculator.ts +0 -255
- package/src/level1/pricing/PricingStrategy.ts +0 -198
- package/src/level1/types/Job.ts +0 -179
- package/src/level1/types/Options.ts +0 -291
- package/src/level1/types/index.ts +0 -8
- package/src/protocol/ACTPKernel.ts +0 -808
- package/src/protocol/AgentRegistry.ts +0 -559
- package/src/protocol/DIDManager.ts +0 -629
- package/src/protocol/DIDResolver.ts +0 -554
- package/src/protocol/EASHelper.ts +0 -378
- package/src/protocol/EscrowVault.ts +0 -255
- package/src/protocol/EventMonitor.ts +0 -204
- package/src/protocol/MessageSigner.ts +0 -510
- package/src/protocol/ProofGenerator.ts +0 -339
- package/src/protocol/QuoteBuilder.ts +0 -15
- package/src/registry/AgentRegistryClient.ts +0 -202
- package/src/runtime/BlockchainRuntime.ts +0 -1015
- package/src/runtime/IACTPRuntime.ts +0 -306
- package/src/runtime/MockRuntime.ts +0 -1298
- package/src/runtime/MockStateManager.ts +0 -577
- package/src/runtime/index.ts +0 -25
- package/src/runtime/types/MockState.ts +0 -237
- package/src/storage/ArchiveBundleBuilder.ts +0 -561
- package/src/storage/ArweaveClient.ts +0 -946
- package/src/storage/FilebaseClient.ts +0 -790
- package/src/storage/index.ts +0 -96
- package/src/storage/types.ts +0 -348
- package/src/types/adapter.ts +0 -310
- package/src/types/agent.ts +0 -79
- package/src/types/did.ts +0 -223
- package/src/types/eip712.ts +0 -175
- package/src/types/erc8004.ts +0 -293
- package/src/types/escrow.ts +0 -27
- package/src/types/index.ts +0 -17
- package/src/types/message.ts +0 -145
- package/src/types/state.ts +0 -87
- package/src/types/transaction.ts +0 -69
- package/src/types/x402.ts +0 -251
- package/src/utils/ErrorRecoveryGuide.ts +0 -676
- package/src/utils/Helpers.ts +0 -688
- package/src/utils/IPFSClient.ts +0 -368
- package/src/utils/Logger.ts +0 -484
- package/src/utils/NonceManager.ts +0 -591
- package/src/utils/RateLimiter.ts +0 -534
- package/src/utils/ReceivedNonceTracker.ts +0 -567
- package/src/utils/SDKLifecycle.ts +0 -416
- package/src/utils/SecureNonce.ts +0 -78
- package/src/utils/Semaphore.ts +0 -276
- package/src/utils/UsedAttestationTracker.ts +0 -385
- package/src/utils/canonicalJson.ts +0 -38
- package/src/utils/circuitBreaker.ts +0 -324
- package/src/utils/computeTypeHash.ts +0 -48
- package/src/utils/fsSafe.ts +0 -80
- package/src/utils/index.ts +0 -80
- package/src/utils/retry.ts +0 -364
- package/src/utils/security.ts +0 -418
- package/src/utils/validation.ts +0 -540
- package/src/wallet/AutoWalletProvider.ts +0 -299
- package/src/wallet/EOAWalletProvider.ts +0 -69
- package/src/wallet/IWalletProvider.ts +0 -135
- package/src/wallet/aa/BundlerClient.ts +0 -274
- package/src/wallet/aa/DualNonceManager.ts +0 -173
- package/src/wallet/aa/PaymasterClient.ts +0 -174
- package/src/wallet/aa/TransactionBatcher.ts +0 -353
- package/src/wallet/aa/UserOpBuilder.ts +0 -246
- package/src/wallet/aa/constants.ts +0 -60
- package/src/wallet/keystore.ts +0 -240
package/src/utils/Logger.ts
DELETED
|
@@ -1,484 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Logger - Structured Logging Framework for ACTP SDK
|
|
3
|
-
*
|
|
4
|
-
* SECURITY FIX (M-6): Comprehensive logging with:
|
|
5
|
-
* - Log levels (debug, info, warn, error)
|
|
6
|
-
* - Structured metadata
|
|
7
|
-
* - Sensitive data filtering
|
|
8
|
-
* - Configurable output
|
|
9
|
-
*
|
|
10
|
-
* @module utils/Logger
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Log levels in order of severity
|
|
15
|
-
*/
|
|
16
|
-
export type LogLevel = 'debug' | 'info' | 'warn' | 'error';
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Log entry structure
|
|
20
|
-
*/
|
|
21
|
-
export interface LogEntry {
|
|
22
|
-
/** Timestamp */
|
|
23
|
-
timestamp: string;
|
|
24
|
-
/** Log level */
|
|
25
|
-
level: LogLevel;
|
|
26
|
-
/** Log message */
|
|
27
|
-
message: string;
|
|
28
|
-
/** Source module/component */
|
|
29
|
-
source?: string;
|
|
30
|
-
/** Additional metadata */
|
|
31
|
-
metadata?: Record<string, unknown>;
|
|
32
|
-
/** Error details (if applicable) */
|
|
33
|
-
error?: {
|
|
34
|
-
name: string;
|
|
35
|
-
message: string;
|
|
36
|
-
stack?: string;
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Logger configuration
|
|
42
|
-
*/
|
|
43
|
-
export interface LoggerConfig {
|
|
44
|
-
/** Minimum log level to output */
|
|
45
|
-
minLevel?: LogLevel;
|
|
46
|
-
/** Source identifier for this logger */
|
|
47
|
-
source?: string;
|
|
48
|
-
/** Whether to include timestamps */
|
|
49
|
-
timestamps?: boolean;
|
|
50
|
-
/** Whether to filter sensitive data */
|
|
51
|
-
filterSensitive?: boolean;
|
|
52
|
-
/** Custom output handler */
|
|
53
|
-
output?: (entry: LogEntry) => void;
|
|
54
|
-
/** Whether logging is enabled */
|
|
55
|
-
enabled?: boolean;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Sensitive key name patterns (for checking object keys)
|
|
60
|
-
*
|
|
61
|
-
* SECURITY FIX (NEW-HIGH-3): NO GLOBAL FLAG on patterns used with .test()
|
|
62
|
-
* Global regex maintains lastIndex state, causing alternating match/no-match
|
|
63
|
-
* on consecutive calls, potentially leaking sensitive data intermittently.
|
|
64
|
-
*/
|
|
65
|
-
const SENSITIVE_KEY_PATTERNS: RegExp[] = [
|
|
66
|
-
/privateKey/i,
|
|
67
|
-
/secret/i,
|
|
68
|
-
/password/i,
|
|
69
|
-
/apiKey/i,
|
|
70
|
-
/authorization/i,
|
|
71
|
-
/mnemonic/i,
|
|
72
|
-
/seed/i,
|
|
73
|
-
];
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Sensitive value patterns (for redacting from string values)
|
|
77
|
-
*
|
|
78
|
-
* SECURITY FIX (NEW-HIGH-3): These are PATTERN STRINGS that get converted
|
|
79
|
-
* to fresh RegExp instances with /g flag for each replace() call.
|
|
80
|
-
* This avoids lastIndex state pollution between calls.
|
|
81
|
-
*/
|
|
82
|
-
const SENSITIVE_VALUE_PATTERNS: string[] = [
|
|
83
|
-
'bearer\\s+[a-zA-Z0-9\\-_.]+', // Bearer tokens
|
|
84
|
-
'0x[a-fA-F0-9]{64}', // Private keys (64 hex chars)
|
|
85
|
-
'0x[a-fA-F0-9]{128}', // Extended private keys
|
|
86
|
-
];
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Log level priority (higher = more severe)
|
|
90
|
-
*/
|
|
91
|
-
const LOG_LEVEL_PRIORITY: Record<LogLevel, number> = {
|
|
92
|
-
debug: 0,
|
|
93
|
-
info: 1,
|
|
94
|
-
warn: 2,
|
|
95
|
-
error: 3,
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Structured Logger for ACTP SDK
|
|
100
|
-
*
|
|
101
|
-
* @example
|
|
102
|
-
* ```typescript
|
|
103
|
-
* const logger = new Logger({ source: 'BlockchainRuntime', minLevel: 'info' });
|
|
104
|
-
*
|
|
105
|
-
* logger.info('Transaction created', { txId: '0x...' });
|
|
106
|
-
* logger.error('Transaction failed', { txId: '0x...' }, error);
|
|
107
|
-
* ```
|
|
108
|
-
*/
|
|
109
|
-
export class Logger {
|
|
110
|
-
private config: Required<LoggerConfig>;
|
|
111
|
-
|
|
112
|
-
constructor(config: LoggerConfig = {}) {
|
|
113
|
-
this.config = {
|
|
114
|
-
minLevel: config.minLevel ?? 'info',
|
|
115
|
-
source: config.source ?? 'ACTP-SDK',
|
|
116
|
-
timestamps: config.timestamps ?? true,
|
|
117
|
-
filterSensitive: config.filterSensitive ?? true,
|
|
118
|
-
output: config.output ?? this.defaultOutput.bind(this),
|
|
119
|
-
enabled: config.enabled ?? true,
|
|
120
|
-
};
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* Create child logger with inherited config
|
|
125
|
-
*/
|
|
126
|
-
child(source: string): Logger {
|
|
127
|
-
return new Logger({
|
|
128
|
-
...this.config,
|
|
129
|
-
source: `${this.config.source}:${source}`,
|
|
130
|
-
});
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* Log debug message
|
|
135
|
-
*/
|
|
136
|
-
debug(message: string, metadata?: Record<string, unknown>): void {
|
|
137
|
-
this.log('debug', message, metadata);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* Log info message
|
|
142
|
-
*/
|
|
143
|
-
info(message: string, metadata?: Record<string, unknown>): void {
|
|
144
|
-
this.log('info', message, metadata);
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* Log warning message
|
|
149
|
-
*/
|
|
150
|
-
warn(message: string, metadata?: Record<string, unknown>): void {
|
|
151
|
-
this.log('warn', message, metadata);
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* Log error message
|
|
156
|
-
*/
|
|
157
|
-
error(message: string, metadata?: Record<string, unknown>, error?: Error): void {
|
|
158
|
-
this.log('error', message, metadata, error);
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
* Core logging method
|
|
163
|
-
*/
|
|
164
|
-
private log(
|
|
165
|
-
level: LogLevel,
|
|
166
|
-
message: string,
|
|
167
|
-
metadata?: Record<string, unknown>,
|
|
168
|
-
error?: Error
|
|
169
|
-
): void {
|
|
170
|
-
if (!this.config.enabled) {
|
|
171
|
-
return;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
// Check log level
|
|
175
|
-
if (LOG_LEVEL_PRIORITY[level] < LOG_LEVEL_PRIORITY[this.config.minLevel]) {
|
|
176
|
-
return;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
// Build log entry
|
|
180
|
-
const entry: LogEntry = {
|
|
181
|
-
timestamp: this.config.timestamps ? new Date().toISOString() : '',
|
|
182
|
-
level,
|
|
183
|
-
message,
|
|
184
|
-
source: this.config.source,
|
|
185
|
-
};
|
|
186
|
-
|
|
187
|
-
// Add metadata (filtered for sensitive data)
|
|
188
|
-
if (metadata) {
|
|
189
|
-
entry.metadata = this.config.filterSensitive
|
|
190
|
-
? this.filterSensitiveData(metadata)
|
|
191
|
-
: metadata;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
// Add error details
|
|
195
|
-
if (error) {
|
|
196
|
-
entry.error = {
|
|
197
|
-
name: error.name,
|
|
198
|
-
message: error.message,
|
|
199
|
-
stack: error.stack,
|
|
200
|
-
};
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
// Output the log
|
|
204
|
-
this.config.output(entry);
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
/**
|
|
208
|
-
* Filter sensitive data from metadata
|
|
209
|
-
*
|
|
210
|
-
* SECURITY FIX (NEW-HIGH-3): Uses separate pattern arrays for keys and values.
|
|
211
|
-
* Key patterns have no /g flag (used with .test()).
|
|
212
|
-
* Value patterns are strings converted to fresh RegExp instances per call.
|
|
213
|
-
*/
|
|
214
|
-
private filterSensitiveData(obj: Record<string, unknown>): Record<string, unknown> {
|
|
215
|
-
const filtered: Record<string, unknown> = {};
|
|
216
|
-
|
|
217
|
-
for (const [key, value] of Object.entries(obj)) {
|
|
218
|
-
// SECURITY FIX (NEW-HIGH-3): Check if key matches sensitive pattern
|
|
219
|
-
// Using patterns without /g flag - safe to use with .test()
|
|
220
|
-
const isSensitiveKey = SENSITIVE_KEY_PATTERNS.some((pattern) => pattern.test(key));
|
|
221
|
-
|
|
222
|
-
if (isSensitiveKey) {
|
|
223
|
-
filtered[key] = '[REDACTED]';
|
|
224
|
-
continue;
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
// Recursively filter nested objects
|
|
228
|
-
if (value && typeof value === 'object' && !Array.isArray(value)) {
|
|
229
|
-
filtered[key] = this.filterSensitiveData(value as Record<string, unknown>);
|
|
230
|
-
} else if (Array.isArray(value)) {
|
|
231
|
-
// SECURITY FIX: Also filter arrays
|
|
232
|
-
filtered[key] = value.map((item) => {
|
|
233
|
-
if (typeof item === 'string') {
|
|
234
|
-
return this.redactSensitiveValues(item);
|
|
235
|
-
} else if (item && typeof item === 'object') {
|
|
236
|
-
return this.filterSensitiveData(item as Record<string, unknown>);
|
|
237
|
-
}
|
|
238
|
-
return item;
|
|
239
|
-
});
|
|
240
|
-
} else if (typeof value === 'string') {
|
|
241
|
-
// SECURITY FIX (NEW-HIGH-3): Redact sensitive patterns from values
|
|
242
|
-
filtered[key] = this.redactSensitiveValues(value);
|
|
243
|
-
} else {
|
|
244
|
-
filtered[key] = value;
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
return filtered;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
/**
|
|
252
|
-
* Redact sensitive patterns from a string value
|
|
253
|
-
*
|
|
254
|
-
* SECURITY FIX (NEW-HIGH-3): Creates fresh RegExp instances with /gi flag
|
|
255
|
-
* for each call, avoiding lastIndex state pollution.
|
|
256
|
-
*/
|
|
257
|
-
private redactSensitiveValues(value: string): string {
|
|
258
|
-
let result = value;
|
|
259
|
-
|
|
260
|
-
for (const patternStr of SENSITIVE_VALUE_PATTERNS) {
|
|
261
|
-
// Create fresh RegExp instance with global+case-insensitive flags
|
|
262
|
-
// This avoids lastIndex state issues from reusing regex instances
|
|
263
|
-
const pattern = new RegExp(patternStr, 'gi');
|
|
264
|
-
result = result.replace(pattern, '[REDACTED]');
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
return result;
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
/**
|
|
271
|
-
* Default console output handler
|
|
272
|
-
*/
|
|
273
|
-
private defaultOutput(entry: LogEntry): void {
|
|
274
|
-
const prefix = entry.timestamp ? `[${entry.timestamp}] ` : '';
|
|
275
|
-
const source = entry.source ? `[${entry.source}] ` : '';
|
|
276
|
-
const levelStr = entry.level.toUpperCase().padEnd(5);
|
|
277
|
-
|
|
278
|
-
const baseMessage = `${prefix}${levelStr} ${source}${entry.message}`;
|
|
279
|
-
|
|
280
|
-
switch (entry.level) {
|
|
281
|
-
case 'debug':
|
|
282
|
-
console.debug(baseMessage, entry.metadata || '');
|
|
283
|
-
break;
|
|
284
|
-
case 'info':
|
|
285
|
-
console.info(baseMessage, entry.metadata || '');
|
|
286
|
-
break;
|
|
287
|
-
case 'warn':
|
|
288
|
-
console.warn(baseMessage, entry.metadata || '');
|
|
289
|
-
break;
|
|
290
|
-
case 'error':
|
|
291
|
-
console.error(baseMessage, entry.metadata || '', entry.error || '');
|
|
292
|
-
break;
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
/**
|
|
297
|
-
* Enable logging
|
|
298
|
-
*/
|
|
299
|
-
enable(): void {
|
|
300
|
-
this.config.enabled = true;
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
/**
|
|
304
|
-
* Disable logging
|
|
305
|
-
*/
|
|
306
|
-
disable(): void {
|
|
307
|
-
this.config.enabled = false;
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
/**
|
|
311
|
-
* Set minimum log level
|
|
312
|
-
*/
|
|
313
|
-
setLevel(level: LogLevel): void {
|
|
314
|
-
this.config.minLevel = level;
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
/**
|
|
319
|
-
* Global SDK logger instance
|
|
320
|
-
*/
|
|
321
|
-
export const sdkLogger = new Logger({
|
|
322
|
-
source: 'ACTP-SDK',
|
|
323
|
-
minLevel: process.env.ACTP_LOG_LEVEL as LogLevel || 'info',
|
|
324
|
-
enabled: process.env.ACTP_LOGGING !== 'false',
|
|
325
|
-
});
|
|
326
|
-
|
|
327
|
-
/**
|
|
328
|
-
* Metrics/monitoring hook interface
|
|
329
|
-
*
|
|
330
|
-
* SECURITY FIX (M-7): Metrics and monitoring hooks
|
|
331
|
-
*/
|
|
332
|
-
export interface MetricsHook {
|
|
333
|
-
/** Called when a transaction is created */
|
|
334
|
-
onTransactionCreated?: (txId: string, metadata: Record<string, unknown>) => void;
|
|
335
|
-
/** Called when escrow is linked */
|
|
336
|
-
onEscrowLinked?: (txId: string, escrowId: string, amount: string) => void;
|
|
337
|
-
/** Called when state transitions */
|
|
338
|
-
onStateTransition?: (txId: string, fromState: string, toState: string) => void;
|
|
339
|
-
/** Called when escrow is released */
|
|
340
|
-
onEscrowReleased?: (escrowId: string, amount: string) => void;
|
|
341
|
-
/** Called on errors */
|
|
342
|
-
onError?: (error: Error, context: Record<string, unknown>) => void;
|
|
343
|
-
/** Called for performance metrics */
|
|
344
|
-
onPerformance?: (operation: string, durationMs: number, metadata?: Record<string, unknown>) => void;
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
/**
|
|
348
|
-
* Metrics collector for SDK operations
|
|
349
|
-
*/
|
|
350
|
-
export class MetricsCollector {
|
|
351
|
-
private hooks: MetricsHook[] = [];
|
|
352
|
-
private readonly logger: Logger;
|
|
353
|
-
|
|
354
|
-
constructor(logger?: Logger) {
|
|
355
|
-
this.logger = logger ?? sdkLogger.child('Metrics');
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
/**
|
|
359
|
-
* Register a metrics hook
|
|
360
|
-
*/
|
|
361
|
-
addHook(hook: MetricsHook): void {
|
|
362
|
-
this.hooks.push(hook);
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
/**
|
|
366
|
-
* Remove a metrics hook
|
|
367
|
-
*/
|
|
368
|
-
removeHook(hook: MetricsHook): void {
|
|
369
|
-
const index = this.hooks.indexOf(hook);
|
|
370
|
-
if (index > -1) {
|
|
371
|
-
this.hooks.splice(index, 1);
|
|
372
|
-
}
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
/**
|
|
376
|
-
* Emit transaction created event
|
|
377
|
-
*/
|
|
378
|
-
transactionCreated(txId: string, metadata: Record<string, unknown>): void {
|
|
379
|
-
this.logger.info('Transaction created', { txId, ...metadata });
|
|
380
|
-
for (const hook of this.hooks) {
|
|
381
|
-
try {
|
|
382
|
-
hook.onTransactionCreated?.(txId, metadata);
|
|
383
|
-
} catch (error) {
|
|
384
|
-
this.logger.error('Metrics hook error', { hook: 'onTransactionCreated' }, error as Error);
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
/**
|
|
390
|
-
* Emit escrow linked event
|
|
391
|
-
*/
|
|
392
|
-
escrowLinked(txId: string, escrowId: string, amount: string): void {
|
|
393
|
-
this.logger.info('Escrow linked', { txId, escrowId, amount });
|
|
394
|
-
for (const hook of this.hooks) {
|
|
395
|
-
try {
|
|
396
|
-
hook.onEscrowLinked?.(txId, escrowId, amount);
|
|
397
|
-
} catch (error) {
|
|
398
|
-
this.logger.error('Metrics hook error', { hook: 'onEscrowLinked' }, error as Error);
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
/**
|
|
404
|
-
* Emit state transition event
|
|
405
|
-
*/
|
|
406
|
-
stateTransition(txId: string, fromState: string, toState: string): void {
|
|
407
|
-
this.logger.info('State transition', { txId, fromState, toState });
|
|
408
|
-
for (const hook of this.hooks) {
|
|
409
|
-
try {
|
|
410
|
-
hook.onStateTransition?.(txId, fromState, toState);
|
|
411
|
-
} catch (error) {
|
|
412
|
-
this.logger.error('Metrics hook error', { hook: 'onStateTransition' }, error as Error);
|
|
413
|
-
}
|
|
414
|
-
}
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
/**
|
|
418
|
-
* Emit escrow released event
|
|
419
|
-
*/
|
|
420
|
-
escrowReleased(escrowId: string, amount: string): void {
|
|
421
|
-
this.logger.info('Escrow released', { escrowId, amount });
|
|
422
|
-
for (const hook of this.hooks) {
|
|
423
|
-
try {
|
|
424
|
-
hook.onEscrowReleased?.(escrowId, amount);
|
|
425
|
-
} catch (error) {
|
|
426
|
-
this.logger.error('Metrics hook error', { hook: 'onEscrowReleased' }, error as Error);
|
|
427
|
-
}
|
|
428
|
-
}
|
|
429
|
-
}
|
|
430
|
-
|
|
431
|
-
/**
|
|
432
|
-
* Emit error event
|
|
433
|
-
*/
|
|
434
|
-
recordError(error: Error, context: Record<string, unknown>): void {
|
|
435
|
-
this.logger.error('Error recorded', context, error);
|
|
436
|
-
for (const hook of this.hooks) {
|
|
437
|
-
try {
|
|
438
|
-
hook.onError?.(error, context);
|
|
439
|
-
} catch (hookError) {
|
|
440
|
-
this.logger.error('Metrics hook error', { hook: 'onError' }, hookError as Error);
|
|
441
|
-
}
|
|
442
|
-
}
|
|
443
|
-
}
|
|
444
|
-
|
|
445
|
-
/**
|
|
446
|
-
* Emit performance metric
|
|
447
|
-
*/
|
|
448
|
-
recordPerformance(operation: string, durationMs: number, metadata?: Record<string, unknown>): void {
|
|
449
|
-
this.logger.debug('Performance', { operation, durationMs, ...metadata });
|
|
450
|
-
for (const hook of this.hooks) {
|
|
451
|
-
try {
|
|
452
|
-
hook.onPerformance?.(operation, durationMs, metadata);
|
|
453
|
-
} catch (error) {
|
|
454
|
-
this.logger.error('Metrics hook error', { hook: 'onPerformance' }, error as Error);
|
|
455
|
-
}
|
|
456
|
-
}
|
|
457
|
-
}
|
|
458
|
-
|
|
459
|
-
/**
|
|
460
|
-
* Helper to time an operation
|
|
461
|
-
*/
|
|
462
|
-
async timeOperation<T>(
|
|
463
|
-
operation: string,
|
|
464
|
-
fn: () => Promise<T>,
|
|
465
|
-
metadata?: Record<string, unknown>
|
|
466
|
-
): Promise<T> {
|
|
467
|
-
const startTime = Date.now();
|
|
468
|
-
try {
|
|
469
|
-
const result = await fn();
|
|
470
|
-
const durationMs = Date.now() - startTime;
|
|
471
|
-
this.recordPerformance(operation, durationMs, { ...metadata, success: true });
|
|
472
|
-
return result;
|
|
473
|
-
} catch (error) {
|
|
474
|
-
const durationMs = Date.now() - startTime;
|
|
475
|
-
this.recordPerformance(operation, durationMs, { ...metadata, success: false });
|
|
476
|
-
throw error;
|
|
477
|
-
}
|
|
478
|
-
}
|
|
479
|
-
}
|
|
480
|
-
|
|
481
|
-
/**
|
|
482
|
-
* Global metrics collector instance
|
|
483
|
-
*/
|
|
484
|
-
export const sdkMetrics = new MetricsCollector();
|