@bsv/sdk 1.6.8 → 1.6.10

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.
Files changed (92) hide show
  1. package/README.md +9 -4
  2. package/dist/cjs/package.json +7 -5
  3. package/dist/cjs/src/wallet/substrates/HTTPWalletJSON.js +11 -3
  4. package/dist/cjs/src/wallet/substrates/HTTPWalletJSON.js.map +1 -1
  5. package/dist/cjs/src/wallet/substrates/WalletWireProcessor.js +1 -1
  6. package/dist/cjs/src/wallet/substrates/WalletWireProcessor.js.map +1 -1
  7. package/dist/cjs/src/wallet/substrates/utils/toOriginHeader.js +21 -0
  8. package/dist/cjs/src/wallet/substrates/utils/toOriginHeader.js.map +1 -0
  9. package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
  10. package/dist/esm/src/wallet/substrates/HTTPWalletJSON.js +9 -1
  11. package/dist/esm/src/wallet/substrates/HTTPWalletJSON.js.map +1 -1
  12. package/dist/esm/src/wallet/substrates/WalletWireProcessor.js +1 -1
  13. package/dist/esm/src/wallet/substrates/WalletWireProcessor.js.map +1 -1
  14. package/dist/esm/src/wallet/substrates/utils/toOriginHeader.js +17 -0
  15. package/dist/esm/src/wallet/substrates/utils/toOriginHeader.js.map +1 -0
  16. package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
  17. package/dist/types/src/wallet/substrates/HTTPWalletJSON.d.ts.map +1 -1
  18. package/dist/types/src/wallet/substrates/utils/toOriginHeader.d.ts +2 -0
  19. package/dist/types/src/wallet/substrates/utils/toOriginHeader.d.ts.map +1 -0
  20. package/dist/types/tsconfig.types.tsbuildinfo +1 -1
  21. package/dist/umd/bundle.js +1 -1
  22. package/docs/concepts/beef.md +84 -0
  23. package/docs/concepts/chain-tracking.md +122 -0
  24. package/docs/concepts/decentralized-identity.md +184 -0
  25. package/docs/concepts/fees.md +217 -0
  26. package/docs/concepts/identity-certificates.md +255 -0
  27. package/docs/concepts/index.md +62 -0
  28. package/docs/concepts/key-management.md +176 -0
  29. package/docs/concepts/script-templates.md +163 -0
  30. package/docs/concepts/sdk-philosophy.md +72 -0
  31. package/docs/concepts/signatures.md +179 -0
  32. package/docs/concepts/spv-verification.md +106 -0
  33. package/docs/concepts/transaction-encoding.md +148 -0
  34. package/docs/concepts/transaction-structure.md +63 -0
  35. package/docs/concepts/trust-model.md +123 -0
  36. package/docs/concepts/verification.md +219 -0
  37. package/docs/concepts/wallet-integration.md +95 -0
  38. package/docs/guides/direct-transaction-creation.md +137 -0
  39. package/docs/guides/http-client-configuration.md +414 -0
  40. package/docs/guides/index.md +30 -0
  41. package/docs/guides/transaction-signing-methods.md +268 -0
  42. package/docs/index.md +74 -0
  43. package/docs/reference/arc-config.md +698 -0
  44. package/docs/reference/brc-100.md +33 -0
  45. package/docs/reference/configuration.md +829 -0
  46. package/docs/reference/debugging.md +700 -0
  47. package/docs/reference/errors.md +547 -0
  48. package/docs/reference/index.md +98 -0
  49. package/docs/reference/network-config.md +914 -0
  50. package/docs/reference/op-codes.md +306 -0
  51. package/docs/reference/transaction-signatures.md +94 -0
  52. package/docs/{wallet.md → reference/wallet.md} +9 -0
  53. package/docs/requirements.txt +3 -0
  54. package/docs/tutorials/advanced-transaction.md +575 -0
  55. package/docs/tutorials/aes-encryption.md +947 -0
  56. package/docs/tutorials/authfetch-tutorial.md +957 -0
  57. package/docs/tutorials/ecdh-key-exchange.md +547 -0
  58. package/docs/tutorials/elliptic-curve-fundamentals.md +603 -0
  59. package/docs/tutorials/error-handling.md +1215 -0
  60. package/docs/tutorials/first-transaction-low-level.md +204 -0
  61. package/docs/tutorials/first-transaction.md +278 -0
  62. package/docs/tutorials/hashes-and-hmacs.md +814 -0
  63. package/docs/tutorials/identity-management.md +702 -0
  64. package/docs/tutorials/index.md +182 -0
  65. package/docs/tutorials/key-management.md +536 -0
  66. package/docs/tutorials/protowallet-development.md +716 -0
  67. package/docs/tutorials/script-construction.md +690 -0
  68. package/docs/tutorials/spv-merkle-proofs.md +682 -0
  69. package/docs/tutorials/testnet-transactions-low-level.md +352 -0
  70. package/docs/tutorials/transaction-broadcasting.md +535 -0
  71. package/docs/tutorials/transaction-types.md +419 -0
  72. package/docs/tutorials/type-42.md +582 -0
  73. package/docs/tutorials/uhrp-storage.md +579 -0
  74. package/package.json +7 -5
  75. package/src/transaction/__tests/Transaction.test.ts +1 -1
  76. package/src/wallet/substrates/HTTPWalletJSON.ts +11 -1
  77. package/src/wallet/substrates/WalletWireProcessor.ts +1 -1
  78. package/src/wallet/substrates/__tests/toOriginHeader.test.ts +34 -0
  79. package/src/wallet/substrates/utils/toOriginHeader.ts +15 -0
  80. package/docs/README.md +0 -21
  81. /package/docs/{auth.md → reference/auth.md} +0 -0
  82. /package/docs/{compat.md → reference/compat.md} +0 -0
  83. /package/docs/{identity.md → reference/identity.md} +0 -0
  84. /package/docs/{kvstore.md → reference/kvstore.md} +0 -0
  85. /package/docs/{messages.md → reference/messages.md} +0 -0
  86. /package/docs/{overlay-tools.md → reference/overlay-tools.md} +0 -0
  87. /package/docs/{primitives.md → reference/primitives.md} +0 -0
  88. /package/docs/{registry.md → reference/registry.md} +0 -0
  89. /package/docs/{script.md → reference/script.md} +0 -0
  90. /package/docs/{storage.md → reference/storage.md} +0 -0
  91. /package/docs/{totp.md → reference/totp.md} +0 -0
  92. /package/docs/{transaction.md → reference/transaction.md} +0 -0
@@ -0,0 +1,700 @@
1
+ # Debugging Reference
2
+
3
+ Complete guide for debugging, logging, and troubleshooting applications built with the BSV TypeScript SDK.
4
+
5
+ ## Debug Mode Activation
6
+
7
+ ### Environment Variables
8
+
9
+ ```bash
10
+ # Enable debug mode
11
+ export BSV_DEBUG=true
12
+
13
+ # Set debug level
14
+ export BSV_DEBUG_LEVEL=debug
15
+
16
+ # Enable specific debug categories
17
+ export BSV_DEBUG_CATEGORIES=wallet,transaction,network
18
+
19
+ # Enable performance debugging
20
+ export BSV_DEBUG_PERFORMANCE=true
21
+ ```
22
+
23
+ ### Programmatic Debug Configuration
24
+
25
+ ```typescript
26
+ import { SDKConfigBuilder } from '@bsv/sdk'
27
+
28
+ const debugConfig = new SDKConfigBuilder()
29
+ .logging({
30
+ level: 'debug',
31
+ outputs: [
32
+ { type: 'console', config: {} },
33
+ { type: 'file', config: { file: './debug.log' } }
34
+ ],
35
+ format: {
36
+ timestamp: true,
37
+ level: true,
38
+ component: true,
39
+ structured: true,
40
+ colors: true
41
+ },
42
+ performance: {
43
+ enabled: true,
44
+ threshold: 100, // Log operations taking >100ms
45
+ includeStackTrace: true
46
+ }
47
+ })
48
+ .build()
49
+ ```
50
+
51
+ ## SDK Logging System
52
+
53
+ ### Log Levels
54
+
55
+ ```typescript
56
+ enum LogLevel {
57
+ ERROR = 0, // Critical errors only
58
+ WARN = 1, // Warnings and errors
59
+ INFO = 2, // General information
60
+ DEBUG = 3, // Detailed debugging info
61
+ TRACE = 4 // Extremely verbose tracing
62
+ }
63
+ ```
64
+
65
+ ### Logger Interface
66
+
67
+ ```typescript
68
+ interface Logger {
69
+ error(message: string, context?: any): void
70
+ warn(message: string, context?: any): void
71
+ info(message: string, context?: any): void
72
+ debug(message: string, context?: any): void
73
+ trace(message: string, context?: any): void
74
+
75
+ // Performance logging
76
+ time(label: string): void
77
+ timeEnd(label: string): void
78
+
79
+ // Structured logging
80
+ log(level: LogLevel, message: string, context: any): void
81
+ }
82
+ ```
83
+
84
+ ### Creating Custom Loggers
85
+
86
+ ```typescript
87
+ class CustomLogger implements Logger {
88
+ private level: LogLevel = LogLevel.INFO
89
+
90
+ constructor(level: LogLevel = LogLevel.INFO) {
91
+ this.level = level
92
+ }
93
+
94
+ error(message: string, context?: any): void {
95
+ if (this.level >= LogLevel.ERROR) {
96
+ console.error(`[ERROR] ${new Date().toISOString()} ${message}`, context)
97
+ }
98
+ }
99
+
100
+ warn(message: string, context?: any): void {
101
+ if (this.level >= LogLevel.WARN) {
102
+ console.warn(`[WARN] ${new Date().toISOString()} ${message}`, context)
103
+ }
104
+ }
105
+
106
+ info(message: string, context?: any): void {
107
+ if (this.level >= LogLevel.INFO) {
108
+ console.info(`[INFO] ${new Date().toISOString()} ${message}`, context)
109
+ }
110
+ }
111
+
112
+ debug(message: string, context?: any): void {
113
+ if (this.level >= LogLevel.DEBUG) {
114
+ console.debug(`[DEBUG] ${new Date().toISOString()} ${message}`, context)
115
+ }
116
+ }
117
+
118
+ trace(message: string, context?: any): void {
119
+ if (this.level >= LogLevel.TRACE) {
120
+ console.trace(`[TRACE] ${new Date().toISOString()} ${message}`, context)
121
+ }
122
+ }
123
+
124
+ time(label: string): void {
125
+ console.time(label)
126
+ }
127
+
128
+ timeEnd(label: string): void {
129
+ console.timeEnd(label)
130
+ }
131
+
132
+ log(level: LogLevel, message: string, context: any): void {
133
+ const methods = [this.error, this.warn, this.info, this.debug, this.trace]
134
+ methods[level]?.call(this, message, context)
135
+ }
136
+ }
137
+ ```
138
+
139
+ ## Debug Categories
140
+
141
+ ### Wallet Debugging
142
+
143
+ ```typescript
144
+ // Enable wallet debugging
145
+ const walletDebugger = {
146
+ logConnection: (substrate: string, status: string) => {
147
+ console.debug(`[WALLET] Connection ${status} to ${substrate}`)
148
+ },
149
+
150
+ logAction: (action: string, args: any) => {
151
+ console.debug(`[WALLET] Action: ${action}`, {
152
+ args: JSON.stringify(args, null, 2),
153
+ timestamp: new Date().toISOString()
154
+ })
155
+ },
156
+
157
+ logError: (error: any, context: any) => {
158
+ console.error(`[WALLET] Error:`, {
159
+ error: error.message,
160
+ code: error.code,
161
+ context,
162
+ stack: error.stack
163
+ })
164
+ }
165
+ }
166
+
167
+ // Usage in wallet operations
168
+ try {
169
+ walletDebugger.logAction('createAction', { description: 'Test transaction' })
170
+ const result = await wallet.createAction({
171
+ description: 'Test transaction',
172
+ outputs: [{ satoshis: 100, lockingScript: '...' }]
173
+ })
174
+ walletDebugger.logConnection('substrate', 'success')
175
+ } catch (error) {
176
+ walletDebugger.logError(error, { operation: 'createAction' })
177
+ }
178
+ ```
179
+
180
+ ### Transaction Debugging
181
+
182
+ ```typescript
183
+ class TransactionDebugger {
184
+ static logConstruction(tx: Transaction): void {
185
+ console.debug(`[TRANSACTION] Construction:`, {
186
+ inputs: tx.inputs.length,
187
+ outputs: tx.outputs.length,
188
+ totalInput: tx.inputs.reduce((sum, input) => sum + input.satoshis, 0),
189
+ totalOutput: tx.outputs.reduce((sum, output) => sum + output.satoshis, 0),
190
+ fee: tx.getFee(),
191
+ size: tx.toHex().length / 2,
192
+ txid: Buffer.from(tx.id()).toString('hex')
193
+ })
194
+ }
195
+
196
+ static logSigning(tx: Transaction, inputIndex: number): void {
197
+ console.debug(`[TRANSACTION] Signing input ${inputIndex}:`, {
198
+ txid: Buffer.from(tx.id()).toString('hex'),
199
+ input: {
200
+ sourceTXID: tx.inputs[inputIndex].sourceTXID,
201
+ sourceOutputIndex: tx.inputs[inputIndex].sourceOutputIndex,
202
+ unlockingScript: tx.inputs[inputIndex].unlockingScript?.toASM()
203
+ }
204
+ })
205
+ }
206
+
207
+ static logValidation(tx: Transaction, isValid: boolean, errors?: string[]): void {
208
+ console.debug(`[TRANSACTION] Validation:`, {
209
+ txid: Buffer.from(tx.id()).toString('hex'),
210
+ valid: isValid,
211
+ errors: errors || [],
212
+ timestamp: new Date().toISOString()
213
+ })
214
+ }
215
+ }
216
+ ```
217
+
218
+ ### Network Debugging
219
+
220
+ ```typescript
221
+ class NetworkDebugger {
222
+ static logRequest(endpoint: string, method: string, data?: any): void {
223
+ console.debug(`[NETWORK] Request:`, {
224
+ endpoint,
225
+ method,
226
+ data: data ? JSON.stringify(data) : undefined,
227
+ timestamp: new Date().toISOString()
228
+ })
229
+ }
230
+
231
+ static logResponse(endpoint: string, status: number, data?: any): void {
232
+ console.debug(`[NETWORK] Response:`, {
233
+ endpoint,
234
+ status,
235
+ dataSize: data ? JSON.stringify(data).length : 0,
236
+ timestamp: new Date().toISOString()
237
+ })
238
+ }
239
+
240
+ static logError(endpoint: string, error: any): void {
241
+ console.error(`[NETWORK] Error:`, {
242
+ endpoint,
243
+ error: error.message,
244
+ code: error.code,
245
+ stack: error.stack,
246
+ timestamp: new Date().toISOString()
247
+ })
248
+ }
249
+ }
250
+ ```
251
+
252
+ ## Transaction Inspection Tools
253
+
254
+ ### Transaction Analyzer
255
+
256
+ ```typescript
257
+ class TransactionAnalyzer {
258
+ static analyze(tx: Transaction): TransactionAnalysis {
259
+ const analysis: TransactionAnalysis = {
260
+ basic: this.analyzeBasic(tx),
261
+ inputs: this.analyzeInputs(tx),
262
+ outputs: this.analyzeOutputs(tx),
263
+ scripts: this.analyzeScripts(tx),
264
+ fees: this.analyzeFees(tx),
265
+ size: this.analyzeSize(tx)
266
+ }
267
+
268
+ return analysis
269
+ }
270
+
271
+ private static analyzeBasic(tx: Transaction): BasicAnalysis {
272
+ return {
273
+ txid: Buffer.from(tx.id()).toString('hex'),
274
+ version: tx.version,
275
+ lockTime: tx.lockTime,
276
+ inputCount: tx.inputs.length,
277
+ outputCount: tx.outputs.length
278
+ }
279
+ }
280
+
281
+ private static analyzeInputs(tx: Transaction): InputAnalysis[] {
282
+ return tx.inputs.map((input, index) => ({
283
+ index,
284
+ sourceTXID: input.sourceTXID,
285
+ sourceOutputIndex: input.sourceOutputIndex,
286
+ satoshis: input.satoshis,
287
+ unlockingScript: {
288
+ asm: input.unlockingScript?.toASM(),
289
+ hex: input.unlockingScript?.toHex(),
290
+ size: input.unlockingScript?.toHex().length / 2
291
+ },
292
+ sequence: input.sequence
293
+ }))
294
+ }
295
+
296
+ private static analyzeOutputs(tx: Transaction): OutputAnalysis[] {
297
+ return tx.outputs.map((output, index) => ({
298
+ index,
299
+ satoshis: output.satoshis,
300
+ lockingScript: {
301
+ asm: output.lockingScript.toASM(),
302
+ hex: output.lockingScript.toHex(),
303
+ size: output.lockingScript.toHex().length / 2,
304
+ type: this.detectScriptType(output.lockingScript)
305
+ }
306
+ }))
307
+ }
308
+
309
+ private static detectScriptType(script: Script): string {
310
+ const asm = script.toASM()
311
+
312
+ if (asm.includes('OP_DUP OP_HASH160') && asm.includes('OP_EQUALVERIFY OP_CHECKSIG')) {
313
+ return 'P2PKH'
314
+ } else if (asm.includes('OP_HASH160') && asm.includes('OP_EQUAL')) {
315
+ return 'P2SH'
316
+ } else if (asm.startsWith('OP_RETURN')) {
317
+ return 'OP_RETURN'
318
+ } else {
319
+ return 'CUSTOM'
320
+ }
321
+ }
322
+ }
323
+
324
+ interface TransactionAnalysis {
325
+ basic: BasicAnalysis
326
+ inputs: InputAnalysis[]
327
+ outputs: OutputAnalysis[]
328
+ scripts: ScriptAnalysis
329
+ fees: FeeAnalysis
330
+ size: SizeAnalysis
331
+ }
332
+ ```
333
+
334
+ ### Script Debugger
335
+
336
+ ```typescript
337
+ class ScriptDebugger {
338
+ static debugScript(script: Script): ScriptDebugInfo {
339
+ const chunks = script.chunks
340
+ const debugInfo: ScriptDebugInfo = {
341
+ chunks: [],
342
+ opcodes: [],
343
+ data: [],
344
+ analysis: {
345
+ isValid: true,
346
+ errors: [],
347
+ warnings: []
348
+ }
349
+ }
350
+
351
+ chunks.forEach((chunk, index) => {
352
+ if (chunk.opcode !== undefined) {
353
+ debugInfo.opcodes.push({
354
+ index,
355
+ opcode: chunk.opcode,
356
+ name: this.getOpcodeName(chunk.opcode),
357
+ description: this.getOpcodeDescription(chunk.opcode)
358
+ })
359
+ }
360
+
361
+ if (chunk.data) {
362
+ debugInfo.data.push({
363
+ index,
364
+ data: chunk.data,
365
+ hex: Buffer.from(chunk.data).toString('hex'),
366
+ size: chunk.data.length
367
+ })
368
+ }
369
+ })
370
+
371
+ return debugInfo
372
+ }
373
+
374
+ private static getOpcodeName(opcode: number): string {
375
+ const opcodeMap: Record<number, string> = {
376
+ 0: 'OP_0',
377
+ 76: 'OP_PUSHDATA1',
378
+ 77: 'OP_PUSHDATA2',
379
+ 78: 'OP_PUSHDATA4',
380
+ 79: 'OP_1NEGATE',
381
+ 81: 'OP_1',
382
+ 82: 'OP_2',
383
+ // ... add more opcodes as needed
384
+ 118: 'OP_DUP',
385
+ 169: 'OP_HASH160',
386
+ 136: 'OP_EQUALVERIFY',
387
+ 172: 'OP_CHECKSIG'
388
+ }
389
+
390
+ return opcodeMap[opcode] || `OP_UNKNOWN_${opcode}`
391
+ }
392
+ }
393
+ ```
394
+
395
+ ## Performance Debugging
396
+
397
+ ### Performance Monitor
398
+
399
+ ```typescript
400
+ class PerformanceMonitor {
401
+ private static timers = new Map<string, number>()
402
+ private static metrics = new Map<string, PerformanceMetric>()
403
+
404
+ static startTimer(label: string): void {
405
+ this.timers.set(label, performance.now())
406
+ }
407
+
408
+ static endTimer(label: string): number {
409
+ const startTime = this.timers.get(label)
410
+ if (!startTime) {
411
+ console.warn(`Timer '${label}' was not started`)
412
+ return 0
413
+ }
414
+
415
+ const duration = performance.now() - startTime
416
+ this.timers.delete(label)
417
+
418
+ // Update metrics
419
+ const metric = this.metrics.get(label) || {
420
+ count: 0,
421
+ totalTime: 0,
422
+ avgTime: 0,
423
+ minTime: Infinity,
424
+ maxTime: 0
425
+ }
426
+
427
+ metric.count++
428
+ metric.totalTime += duration
429
+ metric.avgTime = metric.totalTime / metric.count
430
+ metric.minTime = Math.min(metric.minTime, duration)
431
+ metric.maxTime = Math.max(metric.maxTime, duration)
432
+
433
+ this.metrics.set(label, metric)
434
+
435
+ console.debug(`[PERFORMANCE] ${label}: ${duration.toFixed(2)}ms`)
436
+ return duration
437
+ }
438
+
439
+ static getMetrics(): Map<string, PerformanceMetric> {
440
+ return new Map(this.metrics)
441
+ }
442
+
443
+ static resetMetrics(): void {
444
+ this.metrics.clear()
445
+ this.timers.clear()
446
+ }
447
+ }
448
+
449
+ interface PerformanceMetric {
450
+ count: number
451
+ totalTime: number
452
+ avgTime: number
453
+ minTime: number
454
+ maxTime: number
455
+ }
456
+ ```
457
+
458
+ ### Memory Usage Monitoring
459
+
460
+ ```typescript
461
+ class MemoryMonitor {
462
+ static logMemoryUsage(label: string): void {
463
+ if (typeof process !== 'undefined' && process.memoryUsage) {
464
+ const usage = process.memoryUsage()
465
+ console.debug(`[MEMORY] ${label}:`, {
466
+ rss: `${Math.round(usage.rss / 1024 / 1024)}MB`,
467
+ heapTotal: `${Math.round(usage.heapTotal / 1024 / 1024)}MB`,
468
+ heapUsed: `${Math.round(usage.heapUsed / 1024 / 1024)}MB`,
469
+ external: `${Math.round(usage.external / 1024 / 1024)}MB`
470
+ })
471
+ } else if (typeof performance !== 'undefined' && (performance as any).memory) {
472
+ const memory = (performance as any).memory
473
+ console.debug(`[MEMORY] ${label}:`, {
474
+ used: `${Math.round(memory.usedJSHeapSize / 1024 / 1024)}MB`,
475
+ total: `${Math.round(memory.totalJSHeapSize / 1024 / 1024)}MB`,
476
+ limit: `${Math.round(memory.jsHeapSizeLimit / 1024 / 1024)}MB`
477
+ })
478
+ }
479
+ }
480
+ }
481
+ ```
482
+
483
+ ## Debug Utilities
484
+
485
+ ### Hex Dump Utility
486
+
487
+ ```typescript
488
+ class HexDump {
489
+ static dump(data: Uint8Array | number[], bytesPerLine: number = 16): string {
490
+ const bytes = Array.from(data)
491
+ let result = ''
492
+
493
+ for (let i = 0; i < bytes.length; i += bytesPerLine) {
494
+ const line = bytes.slice(i, i + bytesPerLine)
495
+ const offset = i.toString(16).padStart(8, '0')
496
+ const hex = line.map(b => b.toString(16).padStart(2, '0')).join(' ')
497
+ const ascii = line.map(b => (b >= 32 && b <= 126) ? String.fromCharCode(b) : '.').join('')
498
+
499
+ result += `${offset}: ${hex.padEnd(bytesPerLine * 3 - 1)} |${ascii}|\n`
500
+ }
501
+
502
+ return result
503
+ }
504
+
505
+ static logDump(data: Uint8Array | number[], label: string): void {
506
+ console.debug(`[HEX DUMP] ${label}:\n${this.dump(data)}`)
507
+ }
508
+ }
509
+ ```
510
+
511
+ ### Network Request Inspector
512
+
513
+ ```typescript
514
+ class NetworkInspector {
515
+ private static requests = new Map<string, NetworkRequest>()
516
+
517
+ static startRequest(id: string, url: string, method: string, data?: any): void {
518
+ this.requests.set(id, {
519
+ id,
520
+ url,
521
+ method,
522
+ data,
523
+ startTime: Date.now(),
524
+ status: 'pending'
525
+ })
526
+
527
+ console.debug(`[NETWORK] Starting request ${id}:`, {
528
+ url,
529
+ method,
530
+ dataSize: data ? JSON.stringify(data).length : 0
531
+ })
532
+ }
533
+
534
+ static endRequest(id: string, status: number, response?: any): void {
535
+ const request = this.requests.get(id)
536
+ if (!request) return
537
+
538
+ const duration = Date.now() - request.startTime
539
+ request.status = 'completed'
540
+ request.duration = duration
541
+ request.response = response
542
+
543
+ console.debug(`[NETWORK] Completed request ${id}:`, {
544
+ status,
545
+ duration: `${duration}ms`,
546
+ responseSize: response ? JSON.stringify(response).length : 0
547
+ })
548
+
549
+ // Clean up old requests
550
+ setTimeout(() => this.requests.delete(id), 60000)
551
+ }
552
+
553
+ static failRequest(id: string, error: any): void {
554
+ const request = this.requests.get(id)
555
+ if (!request) return
556
+
557
+ const duration = Date.now() - request.startTime
558
+ request.status = 'failed'
559
+ request.duration = duration
560
+ request.error = error
561
+
562
+ console.error(`[NETWORK] Failed request ${id}:`, {
563
+ error: error.message,
564
+ duration: `${duration}ms`
565
+ })
566
+ }
567
+
568
+ static getActiveRequests(): NetworkRequest[] {
569
+ return Array.from(this.requests.values()).filter(r => r.status === 'pending')
570
+ }
571
+ }
572
+
573
+ interface NetworkRequest {
574
+ id: string
575
+ url: string
576
+ method: string
577
+ data?: any
578
+ startTime: number
579
+ duration?: number
580
+ status: 'pending' | 'completed' | 'failed'
581
+ response?: any
582
+ error?: any
583
+ }
584
+ ```
585
+
586
+ ## Debug Configuration Examples
587
+
588
+ ### Development Debug Setup
589
+
590
+ ```typescript
591
+ // Enable comprehensive debugging for development
592
+ const devDebugConfig = {
593
+ logging: {
594
+ level: 'debug' as LogLevel,
595
+ outputs: [
596
+ { type: 'console', config: { colors: true } },
597
+ { type: 'file', config: { file: './dev-debug.log' } }
598
+ ],
599
+ format: {
600
+ timestamp: true,
601
+ level: true,
602
+ component: true,
603
+ structured: true,
604
+ colors: true
605
+ },
606
+ performance: {
607
+ enabled: true,
608
+ threshold: 50,
609
+ includeStackTrace: true
610
+ }
611
+ }
612
+ }
613
+ ```
614
+
615
+ ### Production Debug Setup
616
+
617
+ ```typescript
618
+ // Minimal debugging for production
619
+ const prodDebugConfig = {
620
+ logging: {
621
+ level: 'warn' as LogLevel,
622
+ outputs: [
623
+ { type: 'file', config: { file: '/var/log/bsv-app.log', rotation: true } }
624
+ ],
625
+ format: {
626
+ timestamp: true,
627
+ level: true,
628
+ component: false,
629
+ structured: true,
630
+ colors: false
631
+ },
632
+ performance: {
633
+ enabled: true,
634
+ threshold: 1000,
635
+ includeStackTrace: false
636
+ }
637
+ }
638
+ }
639
+ ```
640
+
641
+ ### Testing Debug Setup
642
+
643
+ ```typescript
644
+ // Debug configuration for testing
645
+ const testDebugConfig = {
646
+ logging: {
647
+ level: 'trace' as LogLevel,
648
+ outputs: [
649
+ { type: 'console', config: {} }
650
+ ],
651
+ format: {
652
+ timestamp: false,
653
+ level: true,
654
+ component: true,
655
+ structured: false,
656
+ colors: false
657
+ },
658
+ performance: {
659
+ enabled: false
660
+ }
661
+ }
662
+ }
663
+ ```
664
+
665
+ ## Troubleshooting Common Issues
666
+
667
+ ### Debug Checklist
668
+
669
+ 1. **Enable appropriate logging level**
670
+ ```typescript
671
+ // Set debug level based on issue severity
672
+ const config = { logging: { level: 'debug' } }
673
+ ```
674
+
675
+ 2. **Check network connectivity**
676
+ ```typescript
677
+ NetworkInspector.startRequest('health-check', 'https://api.whatsonchain.com/v1/bsv/main/chain/info', 'GET')
678
+ ```
679
+
680
+ 3. **Validate transaction structure**
681
+ ```typescript
682
+ const analysis = TransactionAnalyzer.analyze(transaction)
683
+ console.log('Transaction Analysis:', analysis)
684
+ ```
685
+
686
+ 4. **Monitor performance**
687
+ ```typescript
688
+ PerformanceMonitor.startTimer('wallet-operation')
689
+ // ... perform operation
690
+ PerformanceMonitor.endTimer('wallet-operation')
691
+ ```
692
+
693
+ 5. **Check memory usage**
694
+ ```typescript
695
+ MemoryMonitor.logMemoryUsage('before-operation')
696
+ // ... perform operation
697
+ MemoryMonitor.logMemoryUsage('after-operation')
698
+ ```
699
+
700
+ This comprehensive debugging reference provides developers with all the tools and techniques needed to effectively debug and troubleshoot applications built with the BSV TypeScript SDK.