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