@bcts/components 1.0.0-beta.0 → 1.0.0-beta.1
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/index.cjs +1160 -183
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +17 -1
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +17 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.iife.js +799 -816
- package/dist/index.iife.js.map +1 -1
- package/dist/index.mjs +992 -14
- package/dist/index.mjs.map +1 -1
- package/package.json +11 -11
- package/dist/digest-B1bpgcdz.mjs +0 -943
- package/dist/digest-B1bpgcdz.mjs.map +0 -1
- package/dist/digest-DL5sTq8T.cjs +0 -996
- package/dist/digest-DL5sTq8T.cjs.map +0 -1
- package/dist/digest-DRakTOWS.cjs +0 -2
package/dist/index.mjs
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { a as fromBase64, c as CryptoError, d as isCryptoErrorKind, f as isError, i as bytesToHex, l as ErrorKind, o as hexToBytes, r as bytesEqual, s as toBase64, t as Digest, u as isCryptoError } from "./digest-B1bpgcdz.mjs";
|
|
2
1
|
import { CborDate, CborMap, cbor, createTaggedCbor, decodeCbor, expectArray, expectBytes, expectInteger, expectMap, expectNumber, expectText, expectUnsigned, extractTaggedContent, isArray, isBytes, isTagged, tagValue, tagsForValues, toByteString, toTaggedValue, validateTag } from "@bcts/dcbor";
|
|
3
|
-
import { ARID as ARID$1, COMPRESSED, COMPRESSED as COMPRESSED$1, EC_KEY, EC_KEY_V1, ENCRYPTED, ENCRYPTED as ENCRYPTED$1, ENCRYPTED_KEY, ENVELOPE, JSON as JSON$1, KNOWN_VALUE, LEAF, MLDSA_PRIVATE_KEY, MLDSA_PUBLIC_KEY, MLDSA_SIGNATURE, MLKEM_CIPHERTEXT, MLKEM_PRIVATE_KEY, MLKEM_PUBLIC_KEY, NONCE, PRIVATE_KEYS, PRIVATE_KEY_BASE, PUBLIC_KEYS, REFERENCE, SALT, SEALED_MESSAGE, SEED, SEED_V1, SIGNATURE, SIGNING_PRIVATE_KEY, SIGNING_PUBLIC_KEY, SSH_TEXT_PRIVATE_KEY, SSH_TEXT_PUBLIC_KEY, SSH_TEXT_SIGNATURE, SSKR_SHARE, SSKR_SHARE_V1, SYMMETRIC_KEY, URI as URI$1, UUID as UUID$1, X25519_PRIVATE_KEY, X25519_PUBLIC_KEY, XID as XID$1 } from "@bcts/tags";
|
|
2
|
+
import { ARID as ARID$1, COMPRESSED, COMPRESSED as COMPRESSED$1, DIGEST, EC_KEY, EC_KEY_V1, ENCRYPTED, ENCRYPTED as ENCRYPTED$1, ENCRYPTED_KEY, ENVELOPE, JSON as JSON$1, KNOWN_VALUE, LEAF, MLDSA_PRIVATE_KEY, MLDSA_PUBLIC_KEY, MLDSA_SIGNATURE, MLKEM_CIPHERTEXT, MLKEM_PRIVATE_KEY, MLKEM_PUBLIC_KEY, NONCE, PRIVATE_KEYS, PRIVATE_KEY_BASE, PUBLIC_KEYS, REFERENCE, SALT, SEALED_MESSAGE, SEED, SEED_V1, SIGNATURE, SIGNING_PRIVATE_KEY, SIGNING_PUBLIC_KEY, SSH_TEXT_PRIVATE_KEY, SSH_TEXT_PUBLIC_KEY, SSH_TEXT_SIGNATURE, SSKR_SHARE, SSKR_SHARE_V1, SYMMETRIC_KEY, URI as URI$1, UUID as UUID$1, X25519_PRIVATE_KEY, X25519_PUBLIC_KEY, XID as XID$1 } from "@bcts/tags";
|
|
4
3
|
import { deflateRaw, inflateRaw } from "pako";
|
|
5
|
-
import { ECDSA_PRIVATE_KEY_SIZE, ECDSA_PUBLIC_KEY_SIZE, ECDSA_SIGNATURE_SIZE, ECDSA_UNCOMPRESSED_PUBLIC_KEY_SIZE, ED25519_PRIVATE_KEY_SIZE, ED25519_PUBLIC_KEY_SIZE, ED25519_SIGNATURE_SIZE, SCHNORR_PUBLIC_KEY_SIZE, SCHNORR_SIGNATURE_SIZE, SYMMETRIC_NONCE_SIZE, X25519_PRIVATE_KEY_SIZE, X25519_PUBLIC_KEY_SIZE, aeadChaCha20Poly1305DecryptWithAad, aeadChaCha20Poly1305EncryptWithAad, argon2id, deriveAgreementPrivateKey, deriveSigningPrivateKey, ecdsaCompressPublicKey, ecdsaDecompressPublicKey, ecdsaDerivePrivateKey, ecdsaPublicKeyFromPrivateKey, ecdsaSign, ecdsaVerify, ed25519PublicKeyFromPrivateKey, ed25519Sign, ed25519Verify, hash, hkdfHmacSha256, pbkdf2HmacSha256, schnorrPublicKeyFromPrivateKey, schnorrSign, schnorrSignUsing, schnorrVerify, scryptOpt, x25519PublicKeyFromPrivateKey, x25519SharedKey } from "@bcts/crypto";
|
|
4
|
+
import { ECDSA_PRIVATE_KEY_SIZE, ECDSA_PUBLIC_KEY_SIZE, ECDSA_SIGNATURE_SIZE, ECDSA_UNCOMPRESSED_PUBLIC_KEY_SIZE, ED25519_PRIVATE_KEY_SIZE, ED25519_PUBLIC_KEY_SIZE, ED25519_SIGNATURE_SIZE, SCHNORR_PUBLIC_KEY_SIZE, SCHNORR_SIGNATURE_SIZE, SHA256_SIZE, SYMMETRIC_NONCE_SIZE, X25519_PRIVATE_KEY_SIZE, X25519_PUBLIC_KEY_SIZE, aeadChaCha20Poly1305DecryptWithAad, aeadChaCha20Poly1305EncryptWithAad, argon2id, deriveAgreementPrivateKey, deriveSigningPrivateKey, ecdsaCompressPublicKey, ecdsaDecompressPublicKey, ecdsaDerivePrivateKey, ecdsaPublicKeyFromPrivateKey, ecdsaSign, ecdsaVerify, ed25519PublicKeyFromPrivateKey, ed25519Sign, ed25519Verify, hash, hkdfHmacSha256, pbkdf2HmacSha256, schnorrPublicKeyFromPrivateKey, schnorrSign, schnorrSignUsing, schnorrVerify, scryptOpt, sha256, x25519PublicKeyFromPrivateKey, x25519SharedKey } from "@bcts/crypto";
|
|
6
5
|
import { UR, encodeBytemojisIdentifier, encodeBytewordsIdentifier } from "@bcts/uniform-resources";
|
|
7
6
|
import { SecureRandomNumberGenerator, rngNextInClosedRangeI32 } from "@bcts/rand";
|
|
8
7
|
import * as sr25519 from "@scure/sr25519";
|
|
@@ -16,6 +15,503 @@ import { base64 } from "@scure/base";
|
|
|
16
15
|
import { hmac } from "@noble/hashes/hmac.js";
|
|
17
16
|
import { ml_kem1024, ml_kem512, ml_kem768 } from "@noble/post-quantum/ml-kem.js";
|
|
18
17
|
import { GroupSpec as SSKRGroupSpec, Secret as SSKRSecret, Spec as SSKRSpec, sskrCombine, sskrGenerate, sskrGenerateUsing } from "@bcts/sskr";
|
|
18
|
+
//#region src/error.ts
|
|
19
|
+
/**
|
|
20
|
+
* Copyright © 2023-2026 Blockchain Commons, LLC
|
|
21
|
+
* Copyright © 2025-2026 Parity Technologies
|
|
22
|
+
*
|
|
23
|
+
*
|
|
24
|
+
* Error types for cryptographic and component operations
|
|
25
|
+
*
|
|
26
|
+
* Ported from bc-components-rust/src/error.rs
|
|
27
|
+
*
|
|
28
|
+
* This module provides a unified error handling system that matches the Rust
|
|
29
|
+
* implementation's error variants with full structural parity:
|
|
30
|
+
*
|
|
31
|
+
* - InvalidSize: Invalid data size for the specified type
|
|
32
|
+
* - InvalidData: Invalid data format or content
|
|
33
|
+
* - DataTooShort: Data too short for the expected type
|
|
34
|
+
* - Crypto: Cryptographic operation failed
|
|
35
|
+
* - Cbor: CBOR encoding or decoding error
|
|
36
|
+
* - Sskr: SSKR error
|
|
37
|
+
* - Ssh: SSH key operation failed
|
|
38
|
+
* - Uri: URI parsing failed
|
|
39
|
+
* - Compression: Data compression/decompression failed
|
|
40
|
+
* - PostQuantum: Post-quantum cryptography library error
|
|
41
|
+
* - LevelMismatch: Signature level mismatch
|
|
42
|
+
* - SshAgent: SSH agent operation failed
|
|
43
|
+
* - Hex: Hex decoding error
|
|
44
|
+
* - Utf8: UTF-8 conversion error
|
|
45
|
+
* - Env: Environment variable error
|
|
46
|
+
* - SshAgentClient: SSH agent client error
|
|
47
|
+
* - General: General error with custom message
|
|
48
|
+
*/
|
|
49
|
+
/**
|
|
50
|
+
* Error kind enum matching Rust's Error variants.
|
|
51
|
+
*
|
|
52
|
+
* This enum allows programmatic checking of error types, matching the
|
|
53
|
+
* Rust enum variants exactly.
|
|
54
|
+
*/
|
|
55
|
+
let ErrorKind = /* @__PURE__ */ function(ErrorKind) {
|
|
56
|
+
/** Invalid data size for the specified type */
|
|
57
|
+
ErrorKind["InvalidSize"] = "InvalidSize";
|
|
58
|
+
/** Invalid data format or content */
|
|
59
|
+
ErrorKind["InvalidData"] = "InvalidData";
|
|
60
|
+
/** Data too short for the expected type */
|
|
61
|
+
ErrorKind["DataTooShort"] = "DataTooShort";
|
|
62
|
+
/** Cryptographic operation failed */
|
|
63
|
+
ErrorKind["Crypto"] = "Crypto";
|
|
64
|
+
/** CBOR encoding or decoding error */
|
|
65
|
+
ErrorKind["Cbor"] = "Cbor";
|
|
66
|
+
/** SSKR error */
|
|
67
|
+
ErrorKind["Sskr"] = "Sskr";
|
|
68
|
+
/** SSH key operation failed */
|
|
69
|
+
ErrorKind["Ssh"] = "Ssh";
|
|
70
|
+
/** URI parsing failed */
|
|
71
|
+
ErrorKind["Uri"] = "Uri";
|
|
72
|
+
/** Data compression/decompression failed */
|
|
73
|
+
ErrorKind["Compression"] = "Compression";
|
|
74
|
+
/** Post-quantum cryptography library error */
|
|
75
|
+
ErrorKind["PostQuantum"] = "PostQuantum";
|
|
76
|
+
/** Signature level mismatch */
|
|
77
|
+
ErrorKind["LevelMismatch"] = "LevelMismatch";
|
|
78
|
+
/** SSH agent operation failed */
|
|
79
|
+
ErrorKind["SshAgent"] = "SshAgent";
|
|
80
|
+
/** Hex decoding error */
|
|
81
|
+
ErrorKind["Hex"] = "Hex";
|
|
82
|
+
/** UTF-8 conversion error */
|
|
83
|
+
ErrorKind["Utf8"] = "Utf8";
|
|
84
|
+
/** Environment variable error */
|
|
85
|
+
ErrorKind["Env"] = "Env";
|
|
86
|
+
/** SSH agent client error */
|
|
87
|
+
ErrorKind["SshAgentClient"] = "SshAgentClient";
|
|
88
|
+
/** General error with custom message */
|
|
89
|
+
ErrorKind["General"] = "General";
|
|
90
|
+
return ErrorKind;
|
|
91
|
+
}({});
|
|
92
|
+
/**
|
|
93
|
+
* Error type for cryptographic and component operations.
|
|
94
|
+
*
|
|
95
|
+
* This class provides full structural parity with the Rust Error enum,
|
|
96
|
+
* including:
|
|
97
|
+
* - An `errorKind` property for programmatic error type checking
|
|
98
|
+
* - Structured `errorData` for accessing error-specific fields
|
|
99
|
+
* - Factory methods matching Rust's impl block
|
|
100
|
+
*/
|
|
101
|
+
var CryptoError = class CryptoError extends Error {
|
|
102
|
+
/** The error kind for programmatic type checking */
|
|
103
|
+
errorKind;
|
|
104
|
+
/** Structured error data matching Rust's error variants */
|
|
105
|
+
errorData;
|
|
106
|
+
constructor(message, errorData) {
|
|
107
|
+
super(message);
|
|
108
|
+
this.name = "CryptoError";
|
|
109
|
+
this.errorKind = errorData.kind;
|
|
110
|
+
this.errorData = errorData;
|
|
111
|
+
const ErrorWithStackTrace = Error;
|
|
112
|
+
if (typeof ErrorWithStackTrace.captureStackTrace === "function") ErrorWithStackTrace.captureStackTrace(this, CryptoError);
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Create an invalid size error.
|
|
116
|
+
*
|
|
117
|
+
* Rust equivalent: `Error::InvalidSize { data_type, expected, actual }`
|
|
118
|
+
*
|
|
119
|
+
* @param expected - The expected size
|
|
120
|
+
* @param actual - The actual size received
|
|
121
|
+
*/
|
|
122
|
+
static invalidSize(expected, actual) {
|
|
123
|
+
return CryptoError.invalidSizeForType("data", expected, actual);
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Create an invalid size error with a data type name.
|
|
127
|
+
*
|
|
128
|
+
* Rust equivalent: `Error::invalid_size(data_type, expected, actual)`
|
|
129
|
+
*
|
|
130
|
+
* @param dataType - The name of the data type
|
|
131
|
+
* @param expected - The expected size
|
|
132
|
+
* @param actual - The actual size received
|
|
133
|
+
*/
|
|
134
|
+
static invalidSizeForType(dataType, expected, actual) {
|
|
135
|
+
return new CryptoError(`invalid ${dataType} size: expected ${expected}, got ${actual}`, {
|
|
136
|
+
kind: "InvalidSize",
|
|
137
|
+
dataType,
|
|
138
|
+
expected,
|
|
139
|
+
actual
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Create an invalid data error.
|
|
144
|
+
*
|
|
145
|
+
* @param message - Description of what's invalid
|
|
146
|
+
*/
|
|
147
|
+
static invalidData(message) {
|
|
148
|
+
return CryptoError.invalidDataForType("data", message);
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Create an invalid data error with a data type name.
|
|
152
|
+
*
|
|
153
|
+
* Rust equivalent: `Error::invalid_data(data_type, reason)`
|
|
154
|
+
*
|
|
155
|
+
* @param dataType - The name of the data type
|
|
156
|
+
* @param reason - The reason the data is invalid
|
|
157
|
+
*/
|
|
158
|
+
static invalidDataForType(dataType, reason) {
|
|
159
|
+
return new CryptoError(`invalid ${dataType}: ${reason}`, {
|
|
160
|
+
kind: "InvalidData",
|
|
161
|
+
dataType,
|
|
162
|
+
reason
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Create a data too short error.
|
|
167
|
+
*
|
|
168
|
+
* Rust equivalent: `Error::data_too_short(data_type, minimum, actual)`
|
|
169
|
+
*
|
|
170
|
+
* @param dataType - The name of the data type
|
|
171
|
+
* @param minimum - The minimum required size
|
|
172
|
+
* @param actual - The actual size received
|
|
173
|
+
*/
|
|
174
|
+
static dataTooShort(dataType, minimum, actual) {
|
|
175
|
+
return new CryptoError(`data too short: ${dataType} expected at least ${minimum}, got ${actual}`, {
|
|
176
|
+
kind: "DataTooShort",
|
|
177
|
+
dataType,
|
|
178
|
+
minimum,
|
|
179
|
+
actual
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Create an invalid format error.
|
|
184
|
+
*
|
|
185
|
+
* @param message - Description of the format error
|
|
186
|
+
*/
|
|
187
|
+
static invalidFormat(message) {
|
|
188
|
+
return CryptoError.invalidDataForType("format", message);
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Create an invalid input error.
|
|
192
|
+
*
|
|
193
|
+
* @param message - Description of the invalid input
|
|
194
|
+
*/
|
|
195
|
+
static invalidInput(message) {
|
|
196
|
+
return CryptoError.invalidDataForType("input", message);
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Create a cryptographic operation failed error.
|
|
200
|
+
*
|
|
201
|
+
* Rust equivalent: `Error::crypto(msg)`
|
|
202
|
+
*
|
|
203
|
+
* @param message - Description of the failure
|
|
204
|
+
*/
|
|
205
|
+
static cryptoOperation(message) {
|
|
206
|
+
return CryptoError.crypto(message);
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Create a crypto error.
|
|
210
|
+
*
|
|
211
|
+
* Rust equivalent: `Error::Crypto(msg)`
|
|
212
|
+
*
|
|
213
|
+
* @param message - Description of the failure
|
|
214
|
+
*/
|
|
215
|
+
static crypto(message) {
|
|
216
|
+
return new CryptoError(`cryptographic operation failed: ${message}`, {
|
|
217
|
+
kind: "Crypto",
|
|
218
|
+
message
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Create a post-quantum cryptography error.
|
|
223
|
+
*
|
|
224
|
+
* Rust equivalent: `Error::post_quantum(msg)`
|
|
225
|
+
*
|
|
226
|
+
* @param message - Description of the failure
|
|
227
|
+
*/
|
|
228
|
+
static postQuantum(message) {
|
|
229
|
+
return new CryptoError(`post-quantum cryptography error: ${message}`, {
|
|
230
|
+
kind: "PostQuantum",
|
|
231
|
+
message
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Create a signature level mismatch error.
|
|
236
|
+
*
|
|
237
|
+
* Rust equivalent: `Error::LevelMismatch`
|
|
238
|
+
*/
|
|
239
|
+
static levelMismatch() {
|
|
240
|
+
return new CryptoError("signature level does not match key level", { kind: "LevelMismatch" });
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Create a CBOR error.
|
|
244
|
+
*
|
|
245
|
+
* Rust equivalent: `Error::Cbor(err)`
|
|
246
|
+
*
|
|
247
|
+
* @param message - Description of the CBOR error
|
|
248
|
+
*/
|
|
249
|
+
static cbor(message) {
|
|
250
|
+
return new CryptoError(`CBOR error: ${message}`, {
|
|
251
|
+
kind: "Cbor",
|
|
252
|
+
message
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Create a hex decoding error.
|
|
257
|
+
*
|
|
258
|
+
* Rust equivalent: `Error::Hex(err)`
|
|
259
|
+
*
|
|
260
|
+
* @param message - Description of the hex error
|
|
261
|
+
*/
|
|
262
|
+
static hex(message) {
|
|
263
|
+
return new CryptoError(`hex decoding error: ${message}`, {
|
|
264
|
+
kind: "Hex",
|
|
265
|
+
message
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Create a UTF-8 conversion error.
|
|
270
|
+
*
|
|
271
|
+
* Rust equivalent: `Error::Utf8(err)`
|
|
272
|
+
*
|
|
273
|
+
* @param message - Description of the UTF-8 error
|
|
274
|
+
*/
|
|
275
|
+
static utf8(message) {
|
|
276
|
+
return new CryptoError(`UTF-8 conversion error: ${message}`, {
|
|
277
|
+
kind: "Utf8",
|
|
278
|
+
message
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Create a compression error.
|
|
283
|
+
*
|
|
284
|
+
* Rust equivalent: `Error::compression(msg)`
|
|
285
|
+
*
|
|
286
|
+
* @param message - Description of the compression error
|
|
287
|
+
*/
|
|
288
|
+
static compression(message) {
|
|
289
|
+
return new CryptoError(`compression error: ${message}`, {
|
|
290
|
+
kind: "Compression",
|
|
291
|
+
message
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Create a URI parsing error.
|
|
296
|
+
*
|
|
297
|
+
* Rust equivalent: `Error::Uri(err)`
|
|
298
|
+
*
|
|
299
|
+
* @param message - Description of the URI error
|
|
300
|
+
*/
|
|
301
|
+
static uri(message) {
|
|
302
|
+
return new CryptoError(`invalid URI: ${message}`, {
|
|
303
|
+
kind: "Uri",
|
|
304
|
+
message
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* Create an SSKR error.
|
|
309
|
+
*
|
|
310
|
+
* Rust equivalent: `Error::Sskr(err)`
|
|
311
|
+
*
|
|
312
|
+
* @param message - Description of the SSKR error
|
|
313
|
+
*/
|
|
314
|
+
static sskr(message) {
|
|
315
|
+
return new CryptoError(`SSKR error: ${message}`, {
|
|
316
|
+
kind: "Sskr",
|
|
317
|
+
message
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Create an SSH operation error.
|
|
322
|
+
*
|
|
323
|
+
* Rust equivalent: `Error::ssh(msg)`
|
|
324
|
+
*
|
|
325
|
+
* @param message - Description of the SSH error
|
|
326
|
+
*/
|
|
327
|
+
static ssh(message) {
|
|
328
|
+
return new CryptoError(`SSH operation failed: ${message}`, {
|
|
329
|
+
kind: "Ssh",
|
|
330
|
+
message
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Create an SSH agent error.
|
|
335
|
+
*
|
|
336
|
+
* Rust equivalent: `Error::ssh_agent(msg)`
|
|
337
|
+
*
|
|
338
|
+
* @param message - Description of the SSH agent error
|
|
339
|
+
*/
|
|
340
|
+
static sshAgent(message) {
|
|
341
|
+
return new CryptoError(`SSH agent error: ${message}`, {
|
|
342
|
+
kind: "SshAgent",
|
|
343
|
+
message
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* Create an SSH agent client error.
|
|
348
|
+
*
|
|
349
|
+
* Rust equivalent: `Error::ssh_agent_client(msg)`
|
|
350
|
+
*
|
|
351
|
+
* @param message - Description of the SSH agent client error
|
|
352
|
+
*/
|
|
353
|
+
static sshAgentClient(message) {
|
|
354
|
+
return new CryptoError(`SSH agent client error: ${message}`, {
|
|
355
|
+
kind: "SshAgentClient",
|
|
356
|
+
message
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* Create an environment variable error.
|
|
361
|
+
*
|
|
362
|
+
* Rust equivalent: `Error::Env(err)`
|
|
363
|
+
*
|
|
364
|
+
* @param message - Description of the environment error
|
|
365
|
+
*/
|
|
366
|
+
static env(message) {
|
|
367
|
+
return new CryptoError(`environment variable error: ${message}`, {
|
|
368
|
+
kind: "Env",
|
|
369
|
+
message
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
/**
|
|
373
|
+
* Create a general error with a custom message.
|
|
374
|
+
*
|
|
375
|
+
* Rust equivalent: `Error::general(msg)` / `Error::General(msg)`
|
|
376
|
+
*
|
|
377
|
+
* @param message - The error message
|
|
378
|
+
*/
|
|
379
|
+
static general(message) {
|
|
380
|
+
return new CryptoError(message, {
|
|
381
|
+
kind: "General",
|
|
382
|
+
message
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
/**
|
|
386
|
+
* Check if this error is of a specific kind.
|
|
387
|
+
*
|
|
388
|
+
* @param kind - The error kind to check
|
|
389
|
+
*/
|
|
390
|
+
isKind(kind) {
|
|
391
|
+
return this.errorKind === kind;
|
|
392
|
+
}
|
|
393
|
+
/**
|
|
394
|
+
* Check if this is an InvalidSize error.
|
|
395
|
+
*/
|
|
396
|
+
isInvalidSize() {
|
|
397
|
+
return this.errorKind === "InvalidSize";
|
|
398
|
+
}
|
|
399
|
+
/**
|
|
400
|
+
* Check if this is an InvalidData error.
|
|
401
|
+
*/
|
|
402
|
+
isInvalidData() {
|
|
403
|
+
return this.errorKind === "InvalidData";
|
|
404
|
+
}
|
|
405
|
+
/**
|
|
406
|
+
* Check if this is a DataTooShort error.
|
|
407
|
+
*/
|
|
408
|
+
isDataTooShort() {
|
|
409
|
+
return this.errorKind === "DataTooShort";
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* Check if this is a Crypto error.
|
|
413
|
+
*/
|
|
414
|
+
isCrypto() {
|
|
415
|
+
return this.errorKind === "Crypto";
|
|
416
|
+
}
|
|
417
|
+
/**
|
|
418
|
+
* Check if this is a Cbor error.
|
|
419
|
+
*/
|
|
420
|
+
isCbor() {
|
|
421
|
+
return this.errorKind === "Cbor";
|
|
422
|
+
}
|
|
423
|
+
/**
|
|
424
|
+
* Check if this is an Sskr error.
|
|
425
|
+
*/
|
|
426
|
+
isSskr() {
|
|
427
|
+
return this.errorKind === "Sskr";
|
|
428
|
+
}
|
|
429
|
+
/**
|
|
430
|
+
* Check if this is an Ssh error.
|
|
431
|
+
*/
|
|
432
|
+
isSsh() {
|
|
433
|
+
return this.errorKind === "Ssh";
|
|
434
|
+
}
|
|
435
|
+
/**
|
|
436
|
+
* Check if this is a Uri error.
|
|
437
|
+
*/
|
|
438
|
+
isUri() {
|
|
439
|
+
return this.errorKind === "Uri";
|
|
440
|
+
}
|
|
441
|
+
/**
|
|
442
|
+
* Check if this is a Compression error.
|
|
443
|
+
*/
|
|
444
|
+
isCompression() {
|
|
445
|
+
return this.errorKind === "Compression";
|
|
446
|
+
}
|
|
447
|
+
/**
|
|
448
|
+
* Check if this is a PostQuantum error.
|
|
449
|
+
*/
|
|
450
|
+
isPostQuantum() {
|
|
451
|
+
return this.errorKind === "PostQuantum";
|
|
452
|
+
}
|
|
453
|
+
/**
|
|
454
|
+
* Check if this is a LevelMismatch error.
|
|
455
|
+
*/
|
|
456
|
+
isLevelMismatch() {
|
|
457
|
+
return this.errorKind === "LevelMismatch";
|
|
458
|
+
}
|
|
459
|
+
/**
|
|
460
|
+
* Check if this is an SshAgent error.
|
|
461
|
+
*/
|
|
462
|
+
isSshAgent() {
|
|
463
|
+
return this.errorKind === "SshAgent";
|
|
464
|
+
}
|
|
465
|
+
/**
|
|
466
|
+
* Check if this is a Hex error.
|
|
467
|
+
*/
|
|
468
|
+
isHex() {
|
|
469
|
+
return this.errorKind === "Hex";
|
|
470
|
+
}
|
|
471
|
+
/**
|
|
472
|
+
* Check if this is a Utf8 error.
|
|
473
|
+
*/
|
|
474
|
+
isUtf8() {
|
|
475
|
+
return this.errorKind === "Utf8";
|
|
476
|
+
}
|
|
477
|
+
/**
|
|
478
|
+
* Check if this is an Env error.
|
|
479
|
+
*/
|
|
480
|
+
isEnv() {
|
|
481
|
+
return this.errorKind === "Env";
|
|
482
|
+
}
|
|
483
|
+
/**
|
|
484
|
+
* Check if this is an SshAgentClient error.
|
|
485
|
+
*/
|
|
486
|
+
isSshAgentClient() {
|
|
487
|
+
return this.errorKind === "SshAgentClient";
|
|
488
|
+
}
|
|
489
|
+
/**
|
|
490
|
+
* Check if this is a General error.
|
|
491
|
+
*/
|
|
492
|
+
isGeneral() {
|
|
493
|
+
return this.errorKind === "General";
|
|
494
|
+
}
|
|
495
|
+
};
|
|
496
|
+
/**
|
|
497
|
+
* Type guard to check if a result is an Error.
|
|
498
|
+
*/
|
|
499
|
+
function isError(result) {
|
|
500
|
+
return result instanceof Error;
|
|
501
|
+
}
|
|
502
|
+
/**
|
|
503
|
+
* Type guard to check if a result is a CryptoError.
|
|
504
|
+
*/
|
|
505
|
+
function isCryptoError(result) {
|
|
506
|
+
return result instanceof CryptoError;
|
|
507
|
+
}
|
|
508
|
+
/**
|
|
509
|
+
* Type guard to check if an error is a CryptoError of a specific kind.
|
|
510
|
+
*/
|
|
511
|
+
function isCryptoErrorKind(result, kind) {
|
|
512
|
+
return isCryptoError(result) && result.errorKind === kind;
|
|
513
|
+
}
|
|
514
|
+
//#endregion
|
|
19
515
|
//#region src/private-key-data-provider.ts
|
|
20
516
|
/**
|
|
21
517
|
* Type guard to check if an object implements PrivateKeyDataProvider
|
|
@@ -38,6 +534,129 @@ function isDecrypter(obj) {
|
|
|
38
534
|
return typeof obj === "object" && obj !== null && "encapsulationPrivateKey" in obj && typeof obj.encapsulationPrivateKey === "function" && "decapsulateSharedSecret" in obj && typeof obj.decapsulateSharedSecret === "function";
|
|
39
535
|
}
|
|
40
536
|
//#endregion
|
|
537
|
+
//#region src/utils.ts
|
|
538
|
+
/**
|
|
539
|
+
* Copyright © 2023-2026 Blockchain Commons, LLC
|
|
540
|
+
* Copyright © 2025-2026 Parity Technologies
|
|
541
|
+
*
|
|
542
|
+
*
|
|
543
|
+
* Utility functions for byte array conversions and comparisons.
|
|
544
|
+
*
|
|
545
|
+
* These functions provide cross-platform support for common byte manipulation
|
|
546
|
+
* operations needed in cryptographic and encoding contexts.
|
|
547
|
+
*
|
|
548
|
+
* @packageDocumentation
|
|
549
|
+
*/
|
|
550
|
+
/**
|
|
551
|
+
* Convert a Uint8Array to a lowercase hexadecimal string.
|
|
552
|
+
*
|
|
553
|
+
* @param data - The byte array to convert
|
|
554
|
+
* @returns A lowercase hex string representation (2 characters per byte)
|
|
555
|
+
*
|
|
556
|
+
* @example
|
|
557
|
+
* ```typescript
|
|
558
|
+
* const bytes = new Uint8Array([0xde, 0xad, 0xbe, 0xef]);
|
|
559
|
+
* bytesToHex(bytes); // "deadbeef"
|
|
560
|
+
* ```
|
|
561
|
+
*/
|
|
562
|
+
function bytesToHex(data) {
|
|
563
|
+
return Array.from(data).map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
564
|
+
}
|
|
565
|
+
/**
|
|
566
|
+
* Convert a hexadecimal string to a Uint8Array.
|
|
567
|
+
*
|
|
568
|
+
* @param hex - A hex string (must have even length, case-insensitive)
|
|
569
|
+
* @returns The decoded byte array
|
|
570
|
+
* @throws {Error} If the hex string has odd length or contains invalid characters
|
|
571
|
+
*
|
|
572
|
+
* @example
|
|
573
|
+
* ```typescript
|
|
574
|
+
* hexToBytes("deadbeef"); // Uint8Array([0xde, 0xad, 0xbe, 0xef])
|
|
575
|
+
* hexToBytes("DEADBEEF"); // Uint8Array([0xde, 0xad, 0xbe, 0xef])
|
|
576
|
+
* hexToBytes("xyz"); // throws Error: Invalid hex string
|
|
577
|
+
* ```
|
|
578
|
+
*/
|
|
579
|
+
function hexToBytes(hex) {
|
|
580
|
+
if (hex.length % 2 !== 0) throw new Error(`Hex string must have even length, got ${hex.length}`);
|
|
581
|
+
if (!/^[0-9A-Fa-f]*$/.test(hex)) throw new Error("Invalid hex string: contains non-hexadecimal characters");
|
|
582
|
+
const data = new Uint8Array(hex.length / 2);
|
|
583
|
+
for (let i = 0; i < hex.length; i += 2) data[i / 2] = parseInt(hex.substring(i, i + 2), 16);
|
|
584
|
+
return data;
|
|
585
|
+
}
|
|
586
|
+
/**
|
|
587
|
+
* Convert a Uint8Array to a base64-encoded string.
|
|
588
|
+
*
|
|
589
|
+
* This function works in both browser and Node.js environments.
|
|
590
|
+
* Uses btoa which is available in browsers and Node.js 16+.
|
|
591
|
+
*
|
|
592
|
+
* @param data - The byte array to encode
|
|
593
|
+
* @returns A base64-encoded string
|
|
594
|
+
*
|
|
595
|
+
* @example
|
|
596
|
+
* ```typescript
|
|
597
|
+
* const bytes = new Uint8Array([72, 101, 108, 108, 111]); // "Hello"
|
|
598
|
+
* toBase64(bytes); // "SGVsbG8="
|
|
599
|
+
* ```
|
|
600
|
+
*/
|
|
601
|
+
function toBase64(data) {
|
|
602
|
+
let binary = "";
|
|
603
|
+
for (const byte of data) binary += String.fromCharCode(byte);
|
|
604
|
+
return btoa(binary);
|
|
605
|
+
}
|
|
606
|
+
/**
|
|
607
|
+
* Convert a base64-encoded string to a Uint8Array.
|
|
608
|
+
*
|
|
609
|
+
* This function works in both browser and Node.js environments.
|
|
610
|
+
* Uses atob which is available in browsers and Node.js 16+.
|
|
611
|
+
*
|
|
612
|
+
* @param base64 - A base64-encoded string
|
|
613
|
+
* @returns The decoded byte array
|
|
614
|
+
*
|
|
615
|
+
* @example
|
|
616
|
+
* ```typescript
|
|
617
|
+
* fromBase64("SGVsbG8="); // Uint8Array([72, 101, 108, 108, 111])
|
|
618
|
+
* ```
|
|
619
|
+
*/
|
|
620
|
+
function fromBase64(base64) {
|
|
621
|
+
const binary = atob(base64);
|
|
622
|
+
const bytes = new Uint8Array(binary.length);
|
|
623
|
+
for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
|
|
624
|
+
return bytes;
|
|
625
|
+
}
|
|
626
|
+
/**
|
|
627
|
+
* Compare two Uint8Arrays for equality using constant-time comparison.
|
|
628
|
+
*
|
|
629
|
+
* This function is designed to be resistant to timing attacks by always
|
|
630
|
+
* comparing all bytes regardless of where a difference is found. The
|
|
631
|
+
* comparison time depends only on the length of the arrays, not on where
|
|
632
|
+
* they differ.
|
|
633
|
+
*
|
|
634
|
+
* **Security Note**: If the arrays have different lengths, this function
|
|
635
|
+
* returns `false` immediately, which does leak length information. For
|
|
636
|
+
* cryptographic uses where length should also be secret, ensure both
|
|
637
|
+
* arrays are the same length before comparison.
|
|
638
|
+
*
|
|
639
|
+
* @param a - First byte array
|
|
640
|
+
* @param b - Second byte array
|
|
641
|
+
* @returns `true` if both arrays have the same length and identical contents
|
|
642
|
+
*
|
|
643
|
+
* @example
|
|
644
|
+
* ```typescript
|
|
645
|
+
* const key1 = new Uint8Array([1, 2, 3, 4]);
|
|
646
|
+
* const key2 = new Uint8Array([1, 2, 3, 4]);
|
|
647
|
+
* const key3 = new Uint8Array([1, 2, 3, 5]);
|
|
648
|
+
*
|
|
649
|
+
* bytesEqual(key1, key2); // true
|
|
650
|
+
* bytesEqual(key1, key3); // false
|
|
651
|
+
* ```
|
|
652
|
+
*/
|
|
653
|
+
function bytesEqual(a, b) {
|
|
654
|
+
if (a.length !== b.length) return false;
|
|
655
|
+
let result = 0;
|
|
656
|
+
for (let i = 0; i < a.length; i++) result |= a[i] ^ b[i];
|
|
657
|
+
return result === 0;
|
|
658
|
+
}
|
|
659
|
+
//#endregion
|
|
41
660
|
//#region src/json.ts
|
|
42
661
|
/**
|
|
43
662
|
* Copyright © 2023-2026 Blockchain Commons, LLC
|
|
@@ -217,6 +836,309 @@ var JSON = class JSON {
|
|
|
217
836
|
}
|
|
218
837
|
};
|
|
219
838
|
//#endregion
|
|
839
|
+
//#region src/digest.ts
|
|
840
|
+
/**
|
|
841
|
+
* Copyright © 2023-2026 Blockchain Commons, LLC
|
|
842
|
+
* Copyright © 2025-2026 Parity Technologies
|
|
843
|
+
*
|
|
844
|
+
*
|
|
845
|
+
* SHA-256 cryptographic digest (32 bytes)
|
|
846
|
+
*
|
|
847
|
+
* Ported from bc-components-rust/src/digest.rs
|
|
848
|
+
*
|
|
849
|
+
* A `Digest` represents the cryptographic hash of some data. In this
|
|
850
|
+
* implementation, SHA-256 is used, which produces a 32-byte hash value.
|
|
851
|
+
* Digests are used throughout the crate for data verification and as unique
|
|
852
|
+
* identifiers derived from data.
|
|
853
|
+
*
|
|
854
|
+
* # CBOR Serialization
|
|
855
|
+
*
|
|
856
|
+
* `Digest` implements the CBOR tagged encoding interfaces, which means it can be
|
|
857
|
+
* serialized to and deserialized from CBOR with a specific tag (TAG_DIGEST = 40001).
|
|
858
|
+
*
|
|
859
|
+
* # UR Serialization
|
|
860
|
+
*
|
|
861
|
+
* When serialized as a Uniform Resource (UR), a `Digest` is represented as a
|
|
862
|
+
* binary blob with the type "digest".
|
|
863
|
+
*
|
|
864
|
+
* @example
|
|
865
|
+
* ```typescript
|
|
866
|
+
* import { Digest } from '@bcts/components';
|
|
867
|
+
*
|
|
868
|
+
* // Create a digest from a string
|
|
869
|
+
* const data = new TextEncoder().encode("hello world");
|
|
870
|
+
* const digest = Digest.fromImage(data);
|
|
871
|
+
*
|
|
872
|
+
* // Validate that the digest matches the original data
|
|
873
|
+
* console.log(digest.validate(data)); // true
|
|
874
|
+
*
|
|
875
|
+
* // Create a digest from a hex string
|
|
876
|
+
* const hexString = "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9";
|
|
877
|
+
* const digest2 = Digest.fromHex(hexString);
|
|
878
|
+
*
|
|
879
|
+
* // Retrieve the digest as hex
|
|
880
|
+
* console.log(digest2.hex()); // b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
|
|
881
|
+
* ```
|
|
882
|
+
*/
|
|
883
|
+
var Digest = class Digest {
|
|
884
|
+
static DIGEST_SIZE = SHA256_SIZE;
|
|
885
|
+
_data;
|
|
886
|
+
constructor(data) {
|
|
887
|
+
if (data.length !== Digest.DIGEST_SIZE) throw CryptoError.invalidSize(Digest.DIGEST_SIZE, data.length);
|
|
888
|
+
this._data = new Uint8Array(data);
|
|
889
|
+
}
|
|
890
|
+
/**
|
|
891
|
+
* Get the digest data.
|
|
892
|
+
*/
|
|
893
|
+
data() {
|
|
894
|
+
return this._data;
|
|
895
|
+
}
|
|
896
|
+
/**
|
|
897
|
+
* Create a Digest from a 32-byte array.
|
|
898
|
+
*/
|
|
899
|
+
static fromData(data) {
|
|
900
|
+
return new Digest(new Uint8Array(data));
|
|
901
|
+
}
|
|
902
|
+
/**
|
|
903
|
+
* Create a Digest from data, validating the length.
|
|
904
|
+
* Alias for fromData for compatibility with Rust API.
|
|
905
|
+
*/
|
|
906
|
+
static fromDataRef(data) {
|
|
907
|
+
return Digest.fromData(data);
|
|
908
|
+
}
|
|
909
|
+
/**
|
|
910
|
+
* Create a Digest from hex string.
|
|
911
|
+
*
|
|
912
|
+
* @throws Error if the hex string is not exactly 64 characters.
|
|
913
|
+
*/
|
|
914
|
+
static fromHex(hex) {
|
|
915
|
+
return new Digest(hexToBytes(hex));
|
|
916
|
+
}
|
|
917
|
+
/**
|
|
918
|
+
* Compute SHA-256 digest of data (called "image" in Rust).
|
|
919
|
+
*
|
|
920
|
+
* @param image - The data to hash
|
|
921
|
+
*/
|
|
922
|
+
static fromImage(image) {
|
|
923
|
+
const hashData = sha256(image);
|
|
924
|
+
return new Digest(new Uint8Array(hashData));
|
|
925
|
+
}
|
|
926
|
+
/**
|
|
927
|
+
* Compute SHA-256 digest from multiple data parts.
|
|
928
|
+
*
|
|
929
|
+
* The parts are concatenated and then hashed.
|
|
930
|
+
*
|
|
931
|
+
* @param imageParts - Array of byte arrays to concatenate and hash
|
|
932
|
+
*/
|
|
933
|
+
static fromImageParts(imageParts) {
|
|
934
|
+
const totalLength = imageParts.reduce((sum, part) => sum + part.length, 0);
|
|
935
|
+
const buf = new Uint8Array(totalLength);
|
|
936
|
+
let offset = 0;
|
|
937
|
+
for (const part of imageParts) {
|
|
938
|
+
buf.set(part, offset);
|
|
939
|
+
offset += part.length;
|
|
940
|
+
}
|
|
941
|
+
return Digest.fromImage(buf);
|
|
942
|
+
}
|
|
943
|
+
/**
|
|
944
|
+
* Compute SHA-256 digest from an array of Digests.
|
|
945
|
+
*
|
|
946
|
+
* The digest bytes are concatenated and then hashed.
|
|
947
|
+
*
|
|
948
|
+
* @param digests - Array of Digests to combine
|
|
949
|
+
*/
|
|
950
|
+
static fromDigests(digests) {
|
|
951
|
+
const buf = new Uint8Array(digests.length * Digest.DIGEST_SIZE);
|
|
952
|
+
let offset = 0;
|
|
953
|
+
for (const digest of digests) {
|
|
954
|
+
buf.set(digest._data, offset);
|
|
955
|
+
offset += Digest.DIGEST_SIZE;
|
|
956
|
+
}
|
|
957
|
+
return Digest.fromImage(buf);
|
|
958
|
+
}
|
|
959
|
+
/**
|
|
960
|
+
* Compute SHA-256 digest of data (legacy alias for fromImage).
|
|
961
|
+
* @deprecated Use fromImage instead
|
|
962
|
+
*/
|
|
963
|
+
static hash(data) {
|
|
964
|
+
return Digest.fromImage(data);
|
|
965
|
+
}
|
|
966
|
+
/**
|
|
967
|
+
* Get the raw digest bytes as a copy.
|
|
968
|
+
*/
|
|
969
|
+
toData() {
|
|
970
|
+
return new Uint8Array(this._data);
|
|
971
|
+
}
|
|
972
|
+
/**
|
|
973
|
+
* Get a reference to the raw digest bytes.
|
|
974
|
+
*/
|
|
975
|
+
asBytes() {
|
|
976
|
+
return this._data;
|
|
977
|
+
}
|
|
978
|
+
/**
|
|
979
|
+
* Get hex string representation.
|
|
980
|
+
*/
|
|
981
|
+
hex() {
|
|
982
|
+
return bytesToHex(this._data);
|
|
983
|
+
}
|
|
984
|
+
/**
|
|
985
|
+
* Get hex string representation (alias for hex()).
|
|
986
|
+
*/
|
|
987
|
+
toHex() {
|
|
988
|
+
return this.hex();
|
|
989
|
+
}
|
|
990
|
+
/**
|
|
991
|
+
* Get base64 representation.
|
|
992
|
+
*/
|
|
993
|
+
toBase64() {
|
|
994
|
+
return toBase64(this._data);
|
|
995
|
+
}
|
|
996
|
+
/**
|
|
997
|
+
* Get the first four bytes of the digest as a hexadecimal string.
|
|
998
|
+
* Useful for short descriptions.
|
|
999
|
+
*/
|
|
1000
|
+
shortDescription() {
|
|
1001
|
+
return bytesToHex(this._data.slice(0, 4));
|
|
1002
|
+
}
|
|
1003
|
+
/**
|
|
1004
|
+
* Validate the digest against the given image.
|
|
1005
|
+
*
|
|
1006
|
+
* The image is hashed with SHA-256 and compared to this digest.
|
|
1007
|
+
* @returns `true` if the digest matches the image.
|
|
1008
|
+
*/
|
|
1009
|
+
validate(image) {
|
|
1010
|
+
return this.equals(Digest.fromImage(image));
|
|
1011
|
+
}
|
|
1012
|
+
/**
|
|
1013
|
+
* Compare with another Digest.
|
|
1014
|
+
*/
|
|
1015
|
+
equals(other) {
|
|
1016
|
+
if (this._data.length !== other._data.length) return false;
|
|
1017
|
+
for (let i = 0; i < this._data.length; i++) if (this._data[i] !== other._data[i]) return false;
|
|
1018
|
+
return true;
|
|
1019
|
+
}
|
|
1020
|
+
/**
|
|
1021
|
+
* Compare digests lexicographically.
|
|
1022
|
+
*/
|
|
1023
|
+
compare(other) {
|
|
1024
|
+
for (let i = 0; i < this._data.length; i++) {
|
|
1025
|
+
const a = this._data[i];
|
|
1026
|
+
const b = other._data[i];
|
|
1027
|
+
if (a < b) return -1;
|
|
1028
|
+
if (a > b) return 1;
|
|
1029
|
+
}
|
|
1030
|
+
return 0;
|
|
1031
|
+
}
|
|
1032
|
+
/**
|
|
1033
|
+
* Get string representation.
|
|
1034
|
+
*/
|
|
1035
|
+
toString() {
|
|
1036
|
+
return `Digest(${this.hex()})`;
|
|
1037
|
+
}
|
|
1038
|
+
/**
|
|
1039
|
+
* A Digest is its own digest provider - returns itself.
|
|
1040
|
+
*/
|
|
1041
|
+
digest() {
|
|
1042
|
+
return this;
|
|
1043
|
+
}
|
|
1044
|
+
/**
|
|
1045
|
+
* Returns the CBOR tags associated with Digest.
|
|
1046
|
+
*/
|
|
1047
|
+
cborTags() {
|
|
1048
|
+
return tagsForValues([DIGEST.value]);
|
|
1049
|
+
}
|
|
1050
|
+
/**
|
|
1051
|
+
* Returns the untagged CBOR encoding (as a byte string).
|
|
1052
|
+
*/
|
|
1053
|
+
untaggedCbor() {
|
|
1054
|
+
return toByteString(this._data);
|
|
1055
|
+
}
|
|
1056
|
+
/**
|
|
1057
|
+
* Returns the tagged CBOR encoding.
|
|
1058
|
+
*/
|
|
1059
|
+
taggedCbor() {
|
|
1060
|
+
return createTaggedCbor(this);
|
|
1061
|
+
}
|
|
1062
|
+
/**
|
|
1063
|
+
* Returns the tagged value in CBOR binary representation.
|
|
1064
|
+
*/
|
|
1065
|
+
taggedCborData() {
|
|
1066
|
+
return this.taggedCbor().toData();
|
|
1067
|
+
}
|
|
1068
|
+
/**
|
|
1069
|
+
* Creates a Digest by decoding it from untagged CBOR.
|
|
1070
|
+
*/
|
|
1071
|
+
fromUntaggedCbor(cbor) {
|
|
1072
|
+
const data = expectBytes(cbor);
|
|
1073
|
+
return Digest.fromData(data);
|
|
1074
|
+
}
|
|
1075
|
+
/**
|
|
1076
|
+
* Creates a Digest by decoding it from tagged CBOR.
|
|
1077
|
+
*/
|
|
1078
|
+
fromTaggedCbor(cbor) {
|
|
1079
|
+
validateTag(cbor, this.cborTags());
|
|
1080
|
+
const content = extractTaggedContent(cbor);
|
|
1081
|
+
return this.fromUntaggedCbor(content);
|
|
1082
|
+
}
|
|
1083
|
+
/**
|
|
1084
|
+
* Static method to decode from tagged CBOR.
|
|
1085
|
+
*/
|
|
1086
|
+
static fromTaggedCbor(cbor) {
|
|
1087
|
+
return new Digest(new Uint8Array(Digest.DIGEST_SIZE)).fromTaggedCbor(cbor);
|
|
1088
|
+
}
|
|
1089
|
+
/**
|
|
1090
|
+
* Static method to decode from tagged CBOR binary data.
|
|
1091
|
+
*/
|
|
1092
|
+
static fromTaggedCborData(data) {
|
|
1093
|
+
const cbor = decodeCbor(data);
|
|
1094
|
+
return Digest.fromTaggedCbor(cbor);
|
|
1095
|
+
}
|
|
1096
|
+
/**
|
|
1097
|
+
* Static method to decode from untagged CBOR binary data.
|
|
1098
|
+
*/
|
|
1099
|
+
static fromUntaggedCborData(data) {
|
|
1100
|
+
const bytes = expectBytes(decodeCbor(data));
|
|
1101
|
+
return Digest.fromData(bytes);
|
|
1102
|
+
}
|
|
1103
|
+
/**
|
|
1104
|
+
* Returns the UR representation of the Digest.
|
|
1105
|
+
* Note: URs use untagged CBOR since the type is conveyed by the UR type itself.
|
|
1106
|
+
*/
|
|
1107
|
+
ur() {
|
|
1108
|
+
return UR.new("digest", this.untaggedCbor());
|
|
1109
|
+
}
|
|
1110
|
+
/**
|
|
1111
|
+
* Returns the UR string representation.
|
|
1112
|
+
*/
|
|
1113
|
+
urString() {
|
|
1114
|
+
return this.ur().string();
|
|
1115
|
+
}
|
|
1116
|
+
/**
|
|
1117
|
+
* Creates a Digest from a UR.
|
|
1118
|
+
*/
|
|
1119
|
+
static fromUR(ur) {
|
|
1120
|
+
ur.checkType("digest");
|
|
1121
|
+
return new Digest(new Uint8Array(Digest.DIGEST_SIZE)).fromUntaggedCbor(ur.cbor());
|
|
1122
|
+
}
|
|
1123
|
+
/**
|
|
1124
|
+
* Creates a Digest from a UR string.
|
|
1125
|
+
*/
|
|
1126
|
+
static fromURString(urString) {
|
|
1127
|
+
const ur = UR.fromURString(urString);
|
|
1128
|
+
return Digest.fromUR(ur);
|
|
1129
|
+
}
|
|
1130
|
+
/**
|
|
1131
|
+
* Validate the given data against the digest, if any.
|
|
1132
|
+
*
|
|
1133
|
+
* Returns `true` if the digest is `undefined` or if the digest matches the
|
|
1134
|
+
* image's digest. Returns `false` if the digest does not match.
|
|
1135
|
+
*/
|
|
1136
|
+
static validateOpt(image, digest) {
|
|
1137
|
+
if (digest === void 0) return true;
|
|
1138
|
+
return digest.validate(image);
|
|
1139
|
+
}
|
|
1140
|
+
};
|
|
1141
|
+
//#endregion
|
|
220
1142
|
//#region src/compressed.ts
|
|
221
1143
|
/**
|
|
222
1144
|
* Copyright © 2023-2026 Blockchain Commons, LLC
|
|
@@ -717,15 +1639,48 @@ var HKDFRng = class HKDFRng {
|
|
|
717
1639
|
//#endregion
|
|
718
1640
|
//#region src/digest-provider.ts
|
|
719
1641
|
/**
|
|
1642
|
+
* Copyright © 2023-2026 Blockchain Commons, LLC
|
|
1643
|
+
* Copyright © 2025-2026 Parity Technologies
|
|
1644
|
+
*
|
|
1645
|
+
*
|
|
1646
|
+
* DigestProvider interface for types that can provide a cryptographic digest.
|
|
1647
|
+
*
|
|
1648
|
+
* Ported from bc-components-rust/src/digest_provider.rs
|
|
1649
|
+
*
|
|
1650
|
+
* A type that can provide a single unique digest that characterizes its contents.
|
|
1651
|
+
* This trait is used to define a common interface for objects that can produce
|
|
1652
|
+
* a cryptographic digest (hash) of their content.
|
|
1653
|
+
*
|
|
1654
|
+
* @example
|
|
1655
|
+
* ```typescript
|
|
1656
|
+
* import { DigestProvider, Digest } from '@bcts/components';
|
|
1657
|
+
*
|
|
1658
|
+
* class Document implements DigestProvider {
|
|
1659
|
+
* private content: Uint8Array;
|
|
1660
|
+
* private cachedDigest?: Digest;
|
|
1661
|
+
*
|
|
1662
|
+
* constructor(content: Uint8Array) {
|
|
1663
|
+
* this.content = content;
|
|
1664
|
+
* }
|
|
1665
|
+
*
|
|
1666
|
+
* digest(): Digest {
|
|
1667
|
+
* if (!this.cachedDigest) {
|
|
1668
|
+
* this.cachedDigest = Digest.fromImage(this.content);
|
|
1669
|
+
* }
|
|
1670
|
+
* return this.cachedDigest;
|
|
1671
|
+
* }
|
|
1672
|
+
* }
|
|
1673
|
+
* ```
|
|
1674
|
+
*/
|
|
1675
|
+
/**
|
|
720
1676
|
* Helper function to get a digest from a byte array.
|
|
721
1677
|
* This provides DigestProvider-like functionality for raw bytes.
|
|
722
1678
|
*
|
|
723
1679
|
* @param data - The byte array to hash
|
|
724
1680
|
* @returns A Promise resolving to a Digest of the data
|
|
725
1681
|
*/
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
return Digest.fromImage(data);
|
|
1682
|
+
function digestFromBytes(data) {
|
|
1683
|
+
return Promise.resolve(Digest.fromImage(data));
|
|
729
1684
|
}
|
|
730
1685
|
//#endregion
|
|
731
1686
|
//#region src/nonce.ts
|
|
@@ -5126,6 +6081,7 @@ var MLDSAPrivateKey = class MLDSAPrivateKey {
|
|
|
5126
6081
|
*/
|
|
5127
6082
|
const MAX_UINT32 = 4294967295;
|
|
5128
6083
|
var SshBufferReader = class {
|
|
6084
|
+
bytes;
|
|
5129
6085
|
view;
|
|
5130
6086
|
offset;
|
|
5131
6087
|
constructor(bytes) {
|
|
@@ -6225,7 +7181,7 @@ var SSHPrivateKey = class SSHPrivateKey {
|
|
|
6225
7181
|
if (pubBytes.length !== ED25519_PUBLIC_LEN) throw new Error(`SSHPrivateKey ed25519: public key length ${pubBytes.length} != ${ED25519_PUBLIC_LEN}`);
|
|
6226
7182
|
if (publicKey.data.kind !== "ed25519" || !bytesEqual$1(pubBytes, publicKey.data.pubBytes)) throw new Error("SSHPrivateKey ed25519: outer/inner public-key mismatch");
|
|
6227
7183
|
const combined = innerReader.readString();
|
|
6228
|
-
if (combined.length !==
|
|
7184
|
+
if (combined.length !== 64) throw new Error(`SSHPrivateKey ed25519: combined seed||public length ${combined.length} != 64`);
|
|
6229
7185
|
if (!bytesEqual$1(combined.subarray(ED25519_SEED_LEN), pubBytes)) throw new Error("SSHPrivateKey ed25519: combined-blob public tail does not match public field");
|
|
6230
7186
|
data = {
|
|
6231
7187
|
kind: "ed25519",
|
|
@@ -6316,7 +7272,7 @@ var SSHPrivateKey = class SSHPrivateKey {
|
|
|
6316
7272
|
switch (this.data.kind) {
|
|
6317
7273
|
case "ed25519": {
|
|
6318
7274
|
w.writeString(this.data.pubBytes);
|
|
6319
|
-
const combined = new Uint8Array(
|
|
7275
|
+
const combined = new Uint8Array(64);
|
|
6320
7276
|
combined.set(this.data.seed, 0);
|
|
6321
7277
|
combined.set(this.data.pubBytes, ED25519_SEED_LEN);
|
|
6322
7278
|
w.writeString(combined);
|
|
@@ -9476,6 +10432,16 @@ var PrivateKeyBase = class PrivateKeyBase {
|
|
|
9476
10432
|
return EncapsulationPrivateKey.fromX25519PrivateKey(this.x25519PrivateKey());
|
|
9477
10433
|
}
|
|
9478
10434
|
/**
|
|
10435
|
+
* Decapsulate a shared secret from a ciphertext.
|
|
10436
|
+
*
|
|
10437
|
+
* Implements the `Decrypter` interface so a `PrivateKeyBase` can be used
|
|
10438
|
+
* directly as a recipient key, mirroring Rust `impl Decrypter for
|
|
10439
|
+
* PrivateKeyBase`.
|
|
10440
|
+
*/
|
|
10441
|
+
decapsulateSharedSecret(ciphertext) {
|
|
10442
|
+
return this.encapsulationPrivateKey().decapsulateSharedSecret(ciphertext);
|
|
10443
|
+
}
|
|
10444
|
+
/**
|
|
9479
10445
|
* Derive a PrivateKeys container with Ed25519 signing and X25519 agreement keys.
|
|
9480
10446
|
*
|
|
9481
10447
|
* @returns PrivateKeys containing the derived signing and encapsulation keys
|
|
@@ -11014,6 +11980,12 @@ var SigningPublicKey = class SigningPublicKey {
|
|
|
11014
11980
|
*/
|
|
11015
11981
|
static fromUntaggedCborData(data) {
|
|
11016
11982
|
const cborValue = decodeCbor(data);
|
|
11983
|
+
return SigningPublicKey.fromUntaggedCbor(cborValue);
|
|
11984
|
+
}
|
|
11985
|
+
/**
|
|
11986
|
+
* Static method to decode from untagged CBOR.
|
|
11987
|
+
*/
|
|
11988
|
+
static fromUntaggedCbor(cborValue) {
|
|
11017
11989
|
return new SigningPublicKey("Ed25519", void 0, void 0, Ed25519PublicKey.from(new Uint8Array(ED25519_PUBLIC_KEY_SIZE))).fromUntaggedCbor(cborValue);
|
|
11018
11990
|
}
|
|
11019
11991
|
/**
|
|
@@ -11024,7 +11996,7 @@ var SigningPublicKey = class SigningPublicKey {
|
|
|
11024
11996
|
* Returns the UR representation of the signing public key.
|
|
11025
11997
|
*/
|
|
11026
11998
|
ur() {
|
|
11027
|
-
return UR.new(SigningPublicKey.UR_TYPE, this.
|
|
11999
|
+
return UR.new(SigningPublicKey.UR_TYPE, this.untaggedCbor());
|
|
11028
12000
|
}
|
|
11029
12001
|
/**
|
|
11030
12002
|
* Returns the UR string representation of the signing public key.
|
|
@@ -11037,7 +12009,7 @@ var SigningPublicKey = class SigningPublicKey {
|
|
|
11037
12009
|
*/
|
|
11038
12010
|
static fromUR(ur) {
|
|
11039
12011
|
ur.checkType(SigningPublicKey.UR_TYPE);
|
|
11040
|
-
return SigningPublicKey.
|
|
12012
|
+
return SigningPublicKey.fromUntaggedCbor(ur.cbor());
|
|
11041
12013
|
}
|
|
11042
12014
|
/**
|
|
11043
12015
|
* Creates a SigningPublicKey from a UR string.
|
|
@@ -11761,6 +12733,12 @@ var SigningPrivateKey = class SigningPrivateKey {
|
|
|
11761
12733
|
*/
|
|
11762
12734
|
static fromUntaggedCborData(data) {
|
|
11763
12735
|
const cborValue = decodeCbor(data);
|
|
12736
|
+
return SigningPrivateKey.fromUntaggedCbor(cborValue);
|
|
12737
|
+
}
|
|
12738
|
+
/**
|
|
12739
|
+
* Static method to decode from untagged CBOR.
|
|
12740
|
+
*/
|
|
12741
|
+
static fromUntaggedCbor(cborValue) {
|
|
11764
12742
|
return new SigningPrivateKey("Ed25519", void 0, Ed25519PrivateKey.from(new Uint8Array(ED25519_PRIVATE_KEY_SIZE))).fromUntaggedCbor(cborValue);
|
|
11765
12743
|
}
|
|
11766
12744
|
/**
|
|
@@ -11771,7 +12749,7 @@ var SigningPrivateKey = class SigningPrivateKey {
|
|
|
11771
12749
|
* Returns the UR representation of the signing private key.
|
|
11772
12750
|
*/
|
|
11773
12751
|
ur() {
|
|
11774
|
-
return UR.new(SigningPrivateKey.UR_TYPE, this.
|
|
12752
|
+
return UR.new(SigningPrivateKey.UR_TYPE, this.untaggedCbor());
|
|
11775
12753
|
}
|
|
11776
12754
|
/**
|
|
11777
12755
|
* Returns the UR string representation of the signing private key.
|
|
@@ -11784,7 +12762,7 @@ var SigningPrivateKey = class SigningPrivateKey {
|
|
|
11784
12762
|
*/
|
|
11785
12763
|
static fromUR(ur) {
|
|
11786
12764
|
ur.checkType(SigningPrivateKey.UR_TYPE);
|
|
11787
|
-
return SigningPrivateKey.
|
|
12765
|
+
return SigningPrivateKey.fromUntaggedCbor(ur.cbor());
|
|
11788
12766
|
}
|
|
11789
12767
|
/**
|
|
11790
12768
|
* Creates a SigningPrivateKey from a UR string.
|
|
@@ -13932,7 +14910,7 @@ var SSKRShareCbor = class SSKRShareCbor {
|
|
|
13932
14910
|
* Static method to decode from tagged CBOR.
|
|
13933
14911
|
*/
|
|
13934
14912
|
static fromTaggedCbor(cborValue) {
|
|
13935
|
-
return new SSKRShareCbor(new Uint8Array(
|
|
14913
|
+
return new SSKRShareCbor(new Uint8Array(21)).fromTaggedCbor(cborValue);
|
|
13936
14914
|
}
|
|
13937
14915
|
/**
|
|
13938
14916
|
* Static method to decode from tagged CBOR binary data.
|
|
@@ -13946,7 +14924,7 @@ var SSKRShareCbor = class SSKRShareCbor {
|
|
|
13946
14924
|
*/
|
|
13947
14925
|
static fromUntaggedCborData(data) {
|
|
13948
14926
|
const cborValue = decodeCbor(data);
|
|
13949
|
-
return new SSKRShareCbor(new Uint8Array(
|
|
14927
|
+
return new SSKRShareCbor(new Uint8Array(21)).fromUntaggedCbor(cborValue);
|
|
13950
14928
|
}
|
|
13951
14929
|
};
|
|
13952
14930
|
/**
|