@bcts/components 1.0.0-alpha.8 → 1.0.0-beta.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.
@@ -0,0 +1,996 @@
1
+ require("./index.cjs");
2
+ let _bcts_dcbor = require("@bcts/dcbor");
3
+ let _bcts_tags = require("@bcts/tags");
4
+ let _bcts_crypto = require("@bcts/crypto");
5
+ let _bcts_uniform_resources = require("@bcts/uniform-resources");
6
+ //#region src/error.ts
7
+ /**
8
+ * Copyright © 2023-2026 Blockchain Commons, LLC
9
+ * Copyright © 2025-2026 Parity Technologies
10
+ *
11
+ *
12
+ * Error types for cryptographic and component operations
13
+ *
14
+ * Ported from bc-components-rust/src/error.rs
15
+ *
16
+ * This module provides a unified error handling system that matches the Rust
17
+ * implementation's error variants with full structural parity:
18
+ *
19
+ * - InvalidSize: Invalid data size for the specified type
20
+ * - InvalidData: Invalid data format or content
21
+ * - DataTooShort: Data too short for the expected type
22
+ * - Crypto: Cryptographic operation failed
23
+ * - Cbor: CBOR encoding or decoding error
24
+ * - Sskr: SSKR error
25
+ * - Ssh: SSH key operation failed
26
+ * - Uri: URI parsing failed
27
+ * - Compression: Data compression/decompression failed
28
+ * - PostQuantum: Post-quantum cryptography library error
29
+ * - LevelMismatch: Signature level mismatch
30
+ * - SshAgent: SSH agent operation failed
31
+ * - Hex: Hex decoding error
32
+ * - Utf8: UTF-8 conversion error
33
+ * - Env: Environment variable error
34
+ * - SshAgentClient: SSH agent client error
35
+ * - General: General error with custom message
36
+ */
37
+ /**
38
+ * Error kind enum matching Rust's Error variants.
39
+ *
40
+ * This enum allows programmatic checking of error types, matching the
41
+ * Rust enum variants exactly.
42
+ */
43
+ let ErrorKind = /* @__PURE__ */ function(ErrorKind) {
44
+ /** Invalid data size for the specified type */
45
+ ErrorKind["InvalidSize"] = "InvalidSize";
46
+ /** Invalid data format or content */
47
+ ErrorKind["InvalidData"] = "InvalidData";
48
+ /** Data too short for the expected type */
49
+ ErrorKind["DataTooShort"] = "DataTooShort";
50
+ /** Cryptographic operation failed */
51
+ ErrorKind["Crypto"] = "Crypto";
52
+ /** CBOR encoding or decoding error */
53
+ ErrorKind["Cbor"] = "Cbor";
54
+ /** SSKR error */
55
+ ErrorKind["Sskr"] = "Sskr";
56
+ /** SSH key operation failed */
57
+ ErrorKind["Ssh"] = "Ssh";
58
+ /** URI parsing failed */
59
+ ErrorKind["Uri"] = "Uri";
60
+ /** Data compression/decompression failed */
61
+ ErrorKind["Compression"] = "Compression";
62
+ /** Post-quantum cryptography library error */
63
+ ErrorKind["PostQuantum"] = "PostQuantum";
64
+ /** Signature level mismatch */
65
+ ErrorKind["LevelMismatch"] = "LevelMismatch";
66
+ /** SSH agent operation failed */
67
+ ErrorKind["SshAgent"] = "SshAgent";
68
+ /** Hex decoding error */
69
+ ErrorKind["Hex"] = "Hex";
70
+ /** UTF-8 conversion error */
71
+ ErrorKind["Utf8"] = "Utf8";
72
+ /** Environment variable error */
73
+ ErrorKind["Env"] = "Env";
74
+ /** SSH agent client error */
75
+ ErrorKind["SshAgentClient"] = "SshAgentClient";
76
+ /** General error with custom message */
77
+ ErrorKind["General"] = "General";
78
+ return ErrorKind;
79
+ }({});
80
+ /**
81
+ * Error type for cryptographic and component operations.
82
+ *
83
+ * This class provides full structural parity with the Rust Error enum,
84
+ * including:
85
+ * - An `errorKind` property for programmatic error type checking
86
+ * - Structured `errorData` for accessing error-specific fields
87
+ * - Factory methods matching Rust's impl block
88
+ */
89
+ var CryptoError = class CryptoError extends Error {
90
+ /** The error kind for programmatic type checking */
91
+ errorKind;
92
+ /** Structured error data matching Rust's error variants */
93
+ errorData;
94
+ constructor(message, errorData) {
95
+ super(message);
96
+ this.name = "CryptoError";
97
+ this.errorKind = errorData.kind;
98
+ this.errorData = errorData;
99
+ const ErrorWithStackTrace = Error;
100
+ if (typeof ErrorWithStackTrace.captureStackTrace === "function") ErrorWithStackTrace.captureStackTrace(this, CryptoError);
101
+ }
102
+ /**
103
+ * Create an invalid size error.
104
+ *
105
+ * Rust equivalent: `Error::InvalidSize { data_type, expected, actual }`
106
+ *
107
+ * @param expected - The expected size
108
+ * @param actual - The actual size received
109
+ */
110
+ static invalidSize(expected, actual) {
111
+ return CryptoError.invalidSizeForType("data", expected, actual);
112
+ }
113
+ /**
114
+ * Create an invalid size error with a data type name.
115
+ *
116
+ * Rust equivalent: `Error::invalid_size(data_type, expected, actual)`
117
+ *
118
+ * @param dataType - The name of the data type
119
+ * @param expected - The expected size
120
+ * @param actual - The actual size received
121
+ */
122
+ static invalidSizeForType(dataType, expected, actual) {
123
+ return new CryptoError(`invalid ${dataType} size: expected ${expected}, got ${actual}`, {
124
+ kind: "InvalidSize",
125
+ dataType,
126
+ expected,
127
+ actual
128
+ });
129
+ }
130
+ /**
131
+ * Create an invalid data error.
132
+ *
133
+ * @param message - Description of what's invalid
134
+ */
135
+ static invalidData(message) {
136
+ return CryptoError.invalidDataForType("data", message);
137
+ }
138
+ /**
139
+ * Create an invalid data error with a data type name.
140
+ *
141
+ * Rust equivalent: `Error::invalid_data(data_type, reason)`
142
+ *
143
+ * @param dataType - The name of the data type
144
+ * @param reason - The reason the data is invalid
145
+ */
146
+ static invalidDataForType(dataType, reason) {
147
+ return new CryptoError(`invalid ${dataType}: ${reason}`, {
148
+ kind: "InvalidData",
149
+ dataType,
150
+ reason
151
+ });
152
+ }
153
+ /**
154
+ * Create a data too short error.
155
+ *
156
+ * Rust equivalent: `Error::data_too_short(data_type, minimum, actual)`
157
+ *
158
+ * @param dataType - The name of the data type
159
+ * @param minimum - The minimum required size
160
+ * @param actual - The actual size received
161
+ */
162
+ static dataTooShort(dataType, minimum, actual) {
163
+ return new CryptoError(`data too short: ${dataType} expected at least ${minimum}, got ${actual}`, {
164
+ kind: "DataTooShort",
165
+ dataType,
166
+ minimum,
167
+ actual
168
+ });
169
+ }
170
+ /**
171
+ * Create an invalid format error.
172
+ *
173
+ * @param message - Description of the format error
174
+ */
175
+ static invalidFormat(message) {
176
+ return CryptoError.invalidDataForType("format", message);
177
+ }
178
+ /**
179
+ * Create an invalid input error.
180
+ *
181
+ * @param message - Description of the invalid input
182
+ */
183
+ static invalidInput(message) {
184
+ return CryptoError.invalidDataForType("input", message);
185
+ }
186
+ /**
187
+ * Create a cryptographic operation failed error.
188
+ *
189
+ * Rust equivalent: `Error::crypto(msg)`
190
+ *
191
+ * @param message - Description of the failure
192
+ */
193
+ static cryptoOperation(message) {
194
+ return CryptoError.crypto(message);
195
+ }
196
+ /**
197
+ * Create a crypto error.
198
+ *
199
+ * Rust equivalent: `Error::Crypto(msg)`
200
+ *
201
+ * @param message - Description of the failure
202
+ */
203
+ static crypto(message) {
204
+ return new CryptoError(`cryptographic operation failed: ${message}`, {
205
+ kind: "Crypto",
206
+ message
207
+ });
208
+ }
209
+ /**
210
+ * Create a post-quantum cryptography error.
211
+ *
212
+ * Rust equivalent: `Error::post_quantum(msg)`
213
+ *
214
+ * @param message - Description of the failure
215
+ */
216
+ static postQuantum(message) {
217
+ return new CryptoError(`post-quantum cryptography error: ${message}`, {
218
+ kind: "PostQuantum",
219
+ message
220
+ });
221
+ }
222
+ /**
223
+ * Create a signature level mismatch error.
224
+ *
225
+ * Rust equivalent: `Error::LevelMismatch`
226
+ */
227
+ static levelMismatch() {
228
+ return new CryptoError("signature level does not match key level", { kind: "LevelMismatch" });
229
+ }
230
+ /**
231
+ * Create a CBOR error.
232
+ *
233
+ * Rust equivalent: `Error::Cbor(err)`
234
+ *
235
+ * @param message - Description of the CBOR error
236
+ */
237
+ static cbor(message) {
238
+ return new CryptoError(`CBOR error: ${message}`, {
239
+ kind: "Cbor",
240
+ message
241
+ });
242
+ }
243
+ /**
244
+ * Create a hex decoding error.
245
+ *
246
+ * Rust equivalent: `Error::Hex(err)`
247
+ *
248
+ * @param message - Description of the hex error
249
+ */
250
+ static hex(message) {
251
+ return new CryptoError(`hex decoding error: ${message}`, {
252
+ kind: "Hex",
253
+ message
254
+ });
255
+ }
256
+ /**
257
+ * Create a UTF-8 conversion error.
258
+ *
259
+ * Rust equivalent: `Error::Utf8(err)`
260
+ *
261
+ * @param message - Description of the UTF-8 error
262
+ */
263
+ static utf8(message) {
264
+ return new CryptoError(`UTF-8 conversion error: ${message}`, {
265
+ kind: "Utf8",
266
+ message
267
+ });
268
+ }
269
+ /**
270
+ * Create a compression error.
271
+ *
272
+ * Rust equivalent: `Error::compression(msg)`
273
+ *
274
+ * @param message - Description of the compression error
275
+ */
276
+ static compression(message) {
277
+ return new CryptoError(`compression error: ${message}`, {
278
+ kind: "Compression",
279
+ message
280
+ });
281
+ }
282
+ /**
283
+ * Create a URI parsing error.
284
+ *
285
+ * Rust equivalent: `Error::Uri(err)`
286
+ *
287
+ * @param message - Description of the URI error
288
+ */
289
+ static uri(message) {
290
+ return new CryptoError(`invalid URI: ${message}`, {
291
+ kind: "Uri",
292
+ message
293
+ });
294
+ }
295
+ /**
296
+ * Create an SSKR error.
297
+ *
298
+ * Rust equivalent: `Error::Sskr(err)`
299
+ *
300
+ * @param message - Description of the SSKR error
301
+ */
302
+ static sskr(message) {
303
+ return new CryptoError(`SSKR error: ${message}`, {
304
+ kind: "Sskr",
305
+ message
306
+ });
307
+ }
308
+ /**
309
+ * Create an SSH operation error.
310
+ *
311
+ * Rust equivalent: `Error::ssh(msg)`
312
+ *
313
+ * @param message - Description of the SSH error
314
+ */
315
+ static ssh(message) {
316
+ return new CryptoError(`SSH operation failed: ${message}`, {
317
+ kind: "Ssh",
318
+ message
319
+ });
320
+ }
321
+ /**
322
+ * Create an SSH agent error.
323
+ *
324
+ * Rust equivalent: `Error::ssh_agent(msg)`
325
+ *
326
+ * @param message - Description of the SSH agent error
327
+ */
328
+ static sshAgent(message) {
329
+ return new CryptoError(`SSH agent error: ${message}`, {
330
+ kind: "SshAgent",
331
+ message
332
+ });
333
+ }
334
+ /**
335
+ * Create an SSH agent client error.
336
+ *
337
+ * Rust equivalent: `Error::ssh_agent_client(msg)`
338
+ *
339
+ * @param message - Description of the SSH agent client error
340
+ */
341
+ static sshAgentClient(message) {
342
+ return new CryptoError(`SSH agent client error: ${message}`, {
343
+ kind: "SshAgentClient",
344
+ message
345
+ });
346
+ }
347
+ /**
348
+ * Create an environment variable error.
349
+ *
350
+ * Rust equivalent: `Error::Env(err)`
351
+ *
352
+ * @param message - Description of the environment error
353
+ */
354
+ static env(message) {
355
+ return new CryptoError(`environment variable error: ${message}`, {
356
+ kind: "Env",
357
+ message
358
+ });
359
+ }
360
+ /**
361
+ * Create a general error with a custom message.
362
+ *
363
+ * Rust equivalent: `Error::general(msg)` / `Error::General(msg)`
364
+ *
365
+ * @param message - The error message
366
+ */
367
+ static general(message) {
368
+ return new CryptoError(message, {
369
+ kind: "General",
370
+ message
371
+ });
372
+ }
373
+ /**
374
+ * Check if this error is of a specific kind.
375
+ *
376
+ * @param kind - The error kind to check
377
+ */
378
+ isKind(kind) {
379
+ return this.errorKind === kind;
380
+ }
381
+ /**
382
+ * Check if this is an InvalidSize error.
383
+ */
384
+ isInvalidSize() {
385
+ return this.errorKind === "InvalidSize";
386
+ }
387
+ /**
388
+ * Check if this is an InvalidData error.
389
+ */
390
+ isInvalidData() {
391
+ return this.errorKind === "InvalidData";
392
+ }
393
+ /**
394
+ * Check if this is a DataTooShort error.
395
+ */
396
+ isDataTooShort() {
397
+ return this.errorKind === "DataTooShort";
398
+ }
399
+ /**
400
+ * Check if this is a Crypto error.
401
+ */
402
+ isCrypto() {
403
+ return this.errorKind === "Crypto";
404
+ }
405
+ /**
406
+ * Check if this is a Cbor error.
407
+ */
408
+ isCbor() {
409
+ return this.errorKind === "Cbor";
410
+ }
411
+ /**
412
+ * Check if this is an Sskr error.
413
+ */
414
+ isSskr() {
415
+ return this.errorKind === "Sskr";
416
+ }
417
+ /**
418
+ * Check if this is an Ssh error.
419
+ */
420
+ isSsh() {
421
+ return this.errorKind === "Ssh";
422
+ }
423
+ /**
424
+ * Check if this is a Uri error.
425
+ */
426
+ isUri() {
427
+ return this.errorKind === "Uri";
428
+ }
429
+ /**
430
+ * Check if this is a Compression error.
431
+ */
432
+ isCompression() {
433
+ return this.errorKind === "Compression";
434
+ }
435
+ /**
436
+ * Check if this is a PostQuantum error.
437
+ */
438
+ isPostQuantum() {
439
+ return this.errorKind === "PostQuantum";
440
+ }
441
+ /**
442
+ * Check if this is a LevelMismatch error.
443
+ */
444
+ isLevelMismatch() {
445
+ return this.errorKind === "LevelMismatch";
446
+ }
447
+ /**
448
+ * Check if this is an SshAgent error.
449
+ */
450
+ isSshAgent() {
451
+ return this.errorKind === "SshAgent";
452
+ }
453
+ /**
454
+ * Check if this is a Hex error.
455
+ */
456
+ isHex() {
457
+ return this.errorKind === "Hex";
458
+ }
459
+ /**
460
+ * Check if this is a Utf8 error.
461
+ */
462
+ isUtf8() {
463
+ return this.errorKind === "Utf8";
464
+ }
465
+ /**
466
+ * Check if this is an Env error.
467
+ */
468
+ isEnv() {
469
+ return this.errorKind === "Env";
470
+ }
471
+ /**
472
+ * Check if this is an SshAgentClient error.
473
+ */
474
+ isSshAgentClient() {
475
+ return this.errorKind === "SshAgentClient";
476
+ }
477
+ /**
478
+ * Check if this is a General error.
479
+ */
480
+ isGeneral() {
481
+ return this.errorKind === "General";
482
+ }
483
+ };
484
+ /**
485
+ * Type guard to check if a result is an Error.
486
+ */
487
+ function isError(result) {
488
+ return result instanceof Error;
489
+ }
490
+ /**
491
+ * Type guard to check if a result is a CryptoError.
492
+ */
493
+ function isCryptoError(result) {
494
+ return result instanceof CryptoError;
495
+ }
496
+ /**
497
+ * Type guard to check if an error is a CryptoError of a specific kind.
498
+ */
499
+ function isCryptoErrorKind(result, kind) {
500
+ return isCryptoError(result) && result.errorKind === kind;
501
+ }
502
+ //#endregion
503
+ //#region src/utils.ts
504
+ /**
505
+ * Copyright © 2023-2026 Blockchain Commons, LLC
506
+ * Copyright © 2025-2026 Parity Technologies
507
+ *
508
+ *
509
+ * Utility functions for byte array conversions and comparisons.
510
+ *
511
+ * These functions provide cross-platform support for common byte manipulation
512
+ * operations needed in cryptographic and encoding contexts.
513
+ *
514
+ * @packageDocumentation
515
+ */
516
+ /**
517
+ * Convert a Uint8Array to a lowercase hexadecimal string.
518
+ *
519
+ * @param data - The byte array to convert
520
+ * @returns A lowercase hex string representation (2 characters per byte)
521
+ *
522
+ * @example
523
+ * ```typescript
524
+ * const bytes = new Uint8Array([0xde, 0xad, 0xbe, 0xef]);
525
+ * bytesToHex(bytes); // "deadbeef"
526
+ * ```
527
+ */
528
+ function bytesToHex(data) {
529
+ return Array.from(data).map((b) => b.toString(16).padStart(2, "0")).join("");
530
+ }
531
+ /**
532
+ * Convert a hexadecimal string to a Uint8Array.
533
+ *
534
+ * @param hex - A hex string (must have even length, case-insensitive)
535
+ * @returns The decoded byte array
536
+ * @throws {Error} If the hex string has odd length or contains invalid characters
537
+ *
538
+ * @example
539
+ * ```typescript
540
+ * hexToBytes("deadbeef"); // Uint8Array([0xde, 0xad, 0xbe, 0xef])
541
+ * hexToBytes("DEADBEEF"); // Uint8Array([0xde, 0xad, 0xbe, 0xef])
542
+ * hexToBytes("xyz"); // throws Error: Invalid hex string
543
+ * ```
544
+ */
545
+ function hexToBytes(hex) {
546
+ if (hex.length % 2 !== 0) throw new Error(`Hex string must have even length, got ${hex.length}`);
547
+ if (!/^[0-9A-Fa-f]*$/.test(hex)) throw new Error("Invalid hex string: contains non-hexadecimal characters");
548
+ const data = new Uint8Array(hex.length / 2);
549
+ for (let i = 0; i < hex.length; i += 2) data[i / 2] = parseInt(hex.substring(i, i + 2), 16);
550
+ return data;
551
+ }
552
+ /**
553
+ * Convert a Uint8Array to a base64-encoded string.
554
+ *
555
+ * This function works in both browser and Node.js environments.
556
+ * Uses btoa which is available in browsers and Node.js 16+.
557
+ *
558
+ * @param data - The byte array to encode
559
+ * @returns A base64-encoded string
560
+ *
561
+ * @example
562
+ * ```typescript
563
+ * const bytes = new Uint8Array([72, 101, 108, 108, 111]); // "Hello"
564
+ * toBase64(bytes); // "SGVsbG8="
565
+ * ```
566
+ */
567
+ function toBase64(data) {
568
+ let binary = "";
569
+ for (const byte of data) binary += String.fromCharCode(byte);
570
+ return btoa(binary);
571
+ }
572
+ /**
573
+ * Convert a base64-encoded string to a Uint8Array.
574
+ *
575
+ * This function works in both browser and Node.js environments.
576
+ * Uses atob which is available in browsers and Node.js 16+.
577
+ *
578
+ * @param base64 - A base64-encoded string
579
+ * @returns The decoded byte array
580
+ *
581
+ * @example
582
+ * ```typescript
583
+ * fromBase64("SGVsbG8="); // Uint8Array([72, 101, 108, 108, 111])
584
+ * ```
585
+ */
586
+ function fromBase64(base64) {
587
+ const binary = atob(base64);
588
+ const bytes = new Uint8Array(binary.length);
589
+ for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
590
+ return bytes;
591
+ }
592
+ /**
593
+ * Compare two Uint8Arrays for equality using constant-time comparison.
594
+ *
595
+ * This function is designed to be resistant to timing attacks by always
596
+ * comparing all bytes regardless of where a difference is found. The
597
+ * comparison time depends only on the length of the arrays, not on where
598
+ * they differ.
599
+ *
600
+ * **Security Note**: If the arrays have different lengths, this function
601
+ * returns `false` immediately, which does leak length information. For
602
+ * cryptographic uses where length should also be secret, ensure both
603
+ * arrays are the same length before comparison.
604
+ *
605
+ * @param a - First byte array
606
+ * @param b - Second byte array
607
+ * @returns `true` if both arrays have the same length and identical contents
608
+ *
609
+ * @example
610
+ * ```typescript
611
+ * const key1 = new Uint8Array([1, 2, 3, 4]);
612
+ * const key2 = new Uint8Array([1, 2, 3, 4]);
613
+ * const key3 = new Uint8Array([1, 2, 3, 5]);
614
+ *
615
+ * bytesEqual(key1, key2); // true
616
+ * bytesEqual(key1, key3); // false
617
+ * ```
618
+ */
619
+ function bytesEqual(a, b) {
620
+ if (a.length !== b.length) return false;
621
+ let result = 0;
622
+ for (let i = 0; i < a.length; i++) result |= a[i] ^ b[i];
623
+ return result === 0;
624
+ }
625
+ //#endregion
626
+ //#region src/digest.ts
627
+ /**
628
+ * Copyright © 2023-2026 Blockchain Commons, LLC
629
+ * Copyright © 2025-2026 Parity Technologies
630
+ *
631
+ *
632
+ * SHA-256 cryptographic digest (32 bytes)
633
+ *
634
+ * Ported from bc-components-rust/src/digest.rs
635
+ *
636
+ * A `Digest` represents the cryptographic hash of some data. In this
637
+ * implementation, SHA-256 is used, which produces a 32-byte hash value.
638
+ * Digests are used throughout the crate for data verification and as unique
639
+ * identifiers derived from data.
640
+ *
641
+ * # CBOR Serialization
642
+ *
643
+ * `Digest` implements the CBOR tagged encoding interfaces, which means it can be
644
+ * serialized to and deserialized from CBOR with a specific tag (TAG_DIGEST = 40001).
645
+ *
646
+ * # UR Serialization
647
+ *
648
+ * When serialized as a Uniform Resource (UR), a `Digest` is represented as a
649
+ * binary blob with the type "digest".
650
+ *
651
+ * @example
652
+ * ```typescript
653
+ * import { Digest } from '@bcts/components';
654
+ *
655
+ * // Create a digest from a string
656
+ * const data = new TextEncoder().encode("hello world");
657
+ * const digest = Digest.fromImage(data);
658
+ *
659
+ * // Validate that the digest matches the original data
660
+ * console.log(digest.validate(data)); // true
661
+ *
662
+ * // Create a digest from a hex string
663
+ * const hexString = "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9";
664
+ * const digest2 = Digest.fromHex(hexString);
665
+ *
666
+ * // Retrieve the digest as hex
667
+ * console.log(digest2.hex()); // b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
668
+ * ```
669
+ */
670
+ var Digest = class Digest {
671
+ static DIGEST_SIZE = _bcts_crypto.SHA256_SIZE;
672
+ _data;
673
+ constructor(data) {
674
+ if (data.length !== Digest.DIGEST_SIZE) throw CryptoError.invalidSize(Digest.DIGEST_SIZE, data.length);
675
+ this._data = new Uint8Array(data);
676
+ }
677
+ /**
678
+ * Get the digest data.
679
+ */
680
+ data() {
681
+ return this._data;
682
+ }
683
+ /**
684
+ * Create a Digest from a 32-byte array.
685
+ */
686
+ static fromData(data) {
687
+ return new Digest(new Uint8Array(data));
688
+ }
689
+ /**
690
+ * Create a Digest from data, validating the length.
691
+ * Alias for fromData for compatibility with Rust API.
692
+ */
693
+ static fromDataRef(data) {
694
+ return Digest.fromData(data);
695
+ }
696
+ /**
697
+ * Create a Digest from hex string.
698
+ *
699
+ * @throws Error if the hex string is not exactly 64 characters.
700
+ */
701
+ static fromHex(hex) {
702
+ return new Digest(hexToBytes(hex));
703
+ }
704
+ /**
705
+ * Compute SHA-256 digest of data (called "image" in Rust).
706
+ *
707
+ * @param image - The data to hash
708
+ */
709
+ static fromImage(image) {
710
+ const hashData = (0, _bcts_crypto.sha256)(image);
711
+ return new Digest(new Uint8Array(hashData));
712
+ }
713
+ /**
714
+ * Compute SHA-256 digest from multiple data parts.
715
+ *
716
+ * The parts are concatenated and then hashed.
717
+ *
718
+ * @param imageParts - Array of byte arrays to concatenate and hash
719
+ */
720
+ static fromImageParts(imageParts) {
721
+ const totalLength = imageParts.reduce((sum, part) => sum + part.length, 0);
722
+ const buf = new Uint8Array(totalLength);
723
+ let offset = 0;
724
+ for (const part of imageParts) {
725
+ buf.set(part, offset);
726
+ offset += part.length;
727
+ }
728
+ return Digest.fromImage(buf);
729
+ }
730
+ /**
731
+ * Compute SHA-256 digest from an array of Digests.
732
+ *
733
+ * The digest bytes are concatenated and then hashed.
734
+ *
735
+ * @param digests - Array of Digests to combine
736
+ */
737
+ static fromDigests(digests) {
738
+ const buf = new Uint8Array(digests.length * Digest.DIGEST_SIZE);
739
+ let offset = 0;
740
+ for (const digest of digests) {
741
+ buf.set(digest._data, offset);
742
+ offset += Digest.DIGEST_SIZE;
743
+ }
744
+ return Digest.fromImage(buf);
745
+ }
746
+ /**
747
+ * Compute SHA-256 digest of data (legacy alias for fromImage).
748
+ * @deprecated Use fromImage instead
749
+ */
750
+ static hash(data) {
751
+ return Digest.fromImage(data);
752
+ }
753
+ /**
754
+ * Get the raw digest bytes as a copy.
755
+ */
756
+ toData() {
757
+ return new Uint8Array(this._data);
758
+ }
759
+ /**
760
+ * Get a reference to the raw digest bytes.
761
+ */
762
+ asBytes() {
763
+ return this._data;
764
+ }
765
+ /**
766
+ * Get hex string representation.
767
+ */
768
+ hex() {
769
+ return bytesToHex(this._data);
770
+ }
771
+ /**
772
+ * Get hex string representation (alias for hex()).
773
+ */
774
+ toHex() {
775
+ return this.hex();
776
+ }
777
+ /**
778
+ * Get base64 representation.
779
+ */
780
+ toBase64() {
781
+ return toBase64(this._data);
782
+ }
783
+ /**
784
+ * Get the first four bytes of the digest as a hexadecimal string.
785
+ * Useful for short descriptions.
786
+ */
787
+ shortDescription() {
788
+ return bytesToHex(this._data.slice(0, 4));
789
+ }
790
+ /**
791
+ * Validate the digest against the given image.
792
+ *
793
+ * The image is hashed with SHA-256 and compared to this digest.
794
+ * @returns `true` if the digest matches the image.
795
+ */
796
+ validate(image) {
797
+ return this.equals(Digest.fromImage(image));
798
+ }
799
+ /**
800
+ * Compare with another Digest.
801
+ */
802
+ equals(other) {
803
+ if (this._data.length !== other._data.length) return false;
804
+ for (let i = 0; i < this._data.length; i++) if (this._data[i] !== other._data[i]) return false;
805
+ return true;
806
+ }
807
+ /**
808
+ * Compare digests lexicographically.
809
+ */
810
+ compare(other) {
811
+ for (let i = 0; i < this._data.length; i++) {
812
+ const a = this._data[i];
813
+ const b = other._data[i];
814
+ if (a < b) return -1;
815
+ if (a > b) return 1;
816
+ }
817
+ return 0;
818
+ }
819
+ /**
820
+ * Get string representation.
821
+ */
822
+ toString() {
823
+ return `Digest(${this.hex()})`;
824
+ }
825
+ /**
826
+ * A Digest is its own digest provider - returns itself.
827
+ */
828
+ digest() {
829
+ return this;
830
+ }
831
+ /**
832
+ * Returns the CBOR tags associated with Digest.
833
+ */
834
+ cborTags() {
835
+ return (0, _bcts_dcbor.tagsForValues)([_bcts_tags.DIGEST.value]);
836
+ }
837
+ /**
838
+ * Returns the untagged CBOR encoding (as a byte string).
839
+ */
840
+ untaggedCbor() {
841
+ return (0, _bcts_dcbor.toByteString)(this._data);
842
+ }
843
+ /**
844
+ * Returns the tagged CBOR encoding.
845
+ */
846
+ taggedCbor() {
847
+ return (0, _bcts_dcbor.createTaggedCbor)(this);
848
+ }
849
+ /**
850
+ * Returns the tagged value in CBOR binary representation.
851
+ */
852
+ taggedCborData() {
853
+ return this.taggedCbor().toData();
854
+ }
855
+ /**
856
+ * Creates a Digest by decoding it from untagged CBOR.
857
+ */
858
+ fromUntaggedCbor(cbor) {
859
+ const data = (0, _bcts_dcbor.expectBytes)(cbor);
860
+ return Digest.fromData(data);
861
+ }
862
+ /**
863
+ * Creates a Digest by decoding it from tagged CBOR.
864
+ */
865
+ fromTaggedCbor(cbor) {
866
+ (0, _bcts_dcbor.validateTag)(cbor, this.cborTags());
867
+ const content = (0, _bcts_dcbor.extractTaggedContent)(cbor);
868
+ return this.fromUntaggedCbor(content);
869
+ }
870
+ /**
871
+ * Static method to decode from tagged CBOR.
872
+ */
873
+ static fromTaggedCbor(cbor) {
874
+ return new Digest(new Uint8Array(Digest.DIGEST_SIZE)).fromTaggedCbor(cbor);
875
+ }
876
+ /**
877
+ * Static method to decode from tagged CBOR binary data.
878
+ */
879
+ static fromTaggedCborData(data) {
880
+ const cbor = (0, _bcts_dcbor.decodeCbor)(data);
881
+ return Digest.fromTaggedCbor(cbor);
882
+ }
883
+ /**
884
+ * Static method to decode from untagged CBOR binary data.
885
+ */
886
+ static fromUntaggedCborData(data) {
887
+ const bytes = (0, _bcts_dcbor.expectBytes)((0, _bcts_dcbor.decodeCbor)(data));
888
+ return Digest.fromData(bytes);
889
+ }
890
+ /**
891
+ * Returns the UR representation of the Digest.
892
+ * Note: URs use untagged CBOR since the type is conveyed by the UR type itself.
893
+ */
894
+ ur() {
895
+ return _bcts_uniform_resources.UR.new("digest", this.untaggedCbor());
896
+ }
897
+ /**
898
+ * Returns the UR string representation.
899
+ */
900
+ urString() {
901
+ return this.ur().string();
902
+ }
903
+ /**
904
+ * Creates a Digest from a UR.
905
+ */
906
+ static fromUR(ur) {
907
+ ur.checkType("digest");
908
+ return new Digest(new Uint8Array(Digest.DIGEST_SIZE)).fromUntaggedCbor(ur.cbor());
909
+ }
910
+ /**
911
+ * Creates a Digest from a UR string.
912
+ */
913
+ static fromURString(urString) {
914
+ const ur = _bcts_uniform_resources.UR.fromURString(urString);
915
+ return Digest.fromUR(ur);
916
+ }
917
+ /**
918
+ * Validate the given data against the digest, if any.
919
+ *
920
+ * Returns `true` if the digest is `undefined` or if the digest matches the
921
+ * image's digest. Returns `false` if the digest does not match.
922
+ */
923
+ static validateOpt(image, digest) {
924
+ if (digest === void 0) return true;
925
+ return digest.validate(image);
926
+ }
927
+ };
928
+ //#endregion
929
+ Object.defineProperty(exports, "CryptoError", {
930
+ enumerable: true,
931
+ get: function() {
932
+ return CryptoError;
933
+ }
934
+ });
935
+ Object.defineProperty(exports, "Digest", {
936
+ enumerable: true,
937
+ get: function() {
938
+ return Digest;
939
+ }
940
+ });
941
+ Object.defineProperty(exports, "ErrorKind", {
942
+ enumerable: true,
943
+ get: function() {
944
+ return ErrorKind;
945
+ }
946
+ });
947
+ Object.defineProperty(exports, "bytesEqual", {
948
+ enumerable: true,
949
+ get: function() {
950
+ return bytesEqual;
951
+ }
952
+ });
953
+ Object.defineProperty(exports, "bytesToHex", {
954
+ enumerable: true,
955
+ get: function() {
956
+ return bytesToHex;
957
+ }
958
+ });
959
+ Object.defineProperty(exports, "fromBase64", {
960
+ enumerable: true,
961
+ get: function() {
962
+ return fromBase64;
963
+ }
964
+ });
965
+ Object.defineProperty(exports, "hexToBytes", {
966
+ enumerable: true,
967
+ get: function() {
968
+ return hexToBytes;
969
+ }
970
+ });
971
+ Object.defineProperty(exports, "isCryptoError", {
972
+ enumerable: true,
973
+ get: function() {
974
+ return isCryptoError;
975
+ }
976
+ });
977
+ Object.defineProperty(exports, "isCryptoErrorKind", {
978
+ enumerable: true,
979
+ get: function() {
980
+ return isCryptoErrorKind;
981
+ }
982
+ });
983
+ Object.defineProperty(exports, "isError", {
984
+ enumerable: true,
985
+ get: function() {
986
+ return isError;
987
+ }
988
+ });
989
+ Object.defineProperty(exports, "toBase64", {
990
+ enumerable: true,
991
+ get: function() {
992
+ return toBase64;
993
+ }
994
+ });
995
+
996
+ //# sourceMappingURL=digest-DL5sTq8T.cjs.map