@bcts/envelope 1.0.0-alpha.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/LICENSE +48 -0
  2. package/README.md +23 -0
  3. package/dist/index.cjs +2646 -0
  4. package/dist/index.cjs.map +1 -0
  5. package/dist/index.d.cts +782 -0
  6. package/dist/index.d.cts.map +1 -0
  7. package/dist/index.d.mts +782 -0
  8. package/dist/index.d.mts.map +1 -0
  9. package/dist/index.iife.js +2644 -0
  10. package/dist/index.iife.js.map +1 -0
  11. package/dist/index.mjs +2552 -0
  12. package/dist/index.mjs.map +1 -0
  13. package/package.json +84 -0
  14. package/src/base/assertion.ts +179 -0
  15. package/src/base/assertions.ts +153 -0
  16. package/src/base/cbor.ts +122 -0
  17. package/src/base/digest.ts +204 -0
  18. package/src/base/elide.ts +390 -0
  19. package/src/base/envelope-decodable.ts +186 -0
  20. package/src/base/envelope-encodable.ts +71 -0
  21. package/src/base/envelope.ts +988 -0
  22. package/src/base/error.ts +421 -0
  23. package/src/base/index.ts +56 -0
  24. package/src/base/leaf.ts +147 -0
  25. package/src/base/queries.ts +244 -0
  26. package/src/base/walk.ts +215 -0
  27. package/src/base/wrap.ts +26 -0
  28. package/src/extension/attachment.ts +280 -0
  29. package/src/extension/compress.ts +176 -0
  30. package/src/extension/encrypt.ts +297 -0
  31. package/src/extension/expression.ts +404 -0
  32. package/src/extension/index.ts +72 -0
  33. package/src/extension/proof.ts +227 -0
  34. package/src/extension/recipient.ts +440 -0
  35. package/src/extension/salt.ts +114 -0
  36. package/src/extension/signature.ts +398 -0
  37. package/src/extension/types.ts +92 -0
  38. package/src/format/diagnostic.ts +116 -0
  39. package/src/format/hex.ts +25 -0
  40. package/src/format/index.ts +13 -0
  41. package/src/format/tree.ts +168 -0
  42. package/src/index.ts +32 -0
  43. package/src/utils/index.ts +8 -0
  44. package/src/utils/string.ts +48 -0
@@ -0,0 +1,421 @@
1
+ /// Error types returned when operating on Gordian Envelopes.
2
+ ///
3
+ /// These errors capture various conditions that can occur when working with
4
+ /// envelopes, including structure validation, operation constraints, and
5
+ /// extension-specific errors.
6
+ ///
7
+ /// The errors are organized by category, reflecting the base envelope
8
+ /// specification and various extensions defined in the Gordian Envelope
9
+ /// Internet Draft and Blockchain Commons Research (BCR) documents.
10
+
11
+ export enum ErrorCode {
12
+ // Base Specification
13
+ ALREADY_ELIDED = "ALREADY_ELIDED",
14
+ AMBIGUOUS_PREDICATE = "AMBIGUOUS_PREDICATE",
15
+ INVALID_DIGEST = "INVALID_DIGEST",
16
+ INVALID_FORMAT = "INVALID_FORMAT",
17
+ MISSING_DIGEST = "MISSING_DIGEST",
18
+ NONEXISTENT_PREDICATE = "NONEXISTENT_PREDICATE",
19
+ NOT_WRAPPED = "NOT_WRAPPED",
20
+ NOT_LEAF = "NOT_LEAF",
21
+ NOT_ASSERTION = "NOT_ASSERTION",
22
+ INVALID_ASSERTION = "INVALID_ASSERTION",
23
+
24
+ // Attachments Extension
25
+ INVALID_ATTACHMENT = "INVALID_ATTACHMENT",
26
+ NONEXISTENT_ATTACHMENT = "NONEXISTENT_ATTACHMENT",
27
+ AMBIGUOUS_ATTACHMENT = "AMBIGUOUS_ATTACHMENT",
28
+
29
+ // Compression Extension
30
+ ALREADY_COMPRESSED = "ALREADY_COMPRESSED",
31
+ NOT_COMPRESSED = "NOT_COMPRESSED",
32
+
33
+ // Symmetric Encryption Extension
34
+ ALREADY_ENCRYPTED = "ALREADY_ENCRYPTED",
35
+ NOT_ENCRYPTED = "NOT_ENCRYPTED",
36
+
37
+ // Known Values Extension
38
+ NOT_KNOWN_VALUE = "NOT_KNOWN_VALUE",
39
+
40
+ // Public Key Encryption Extension
41
+ UNKNOWN_RECIPIENT = "UNKNOWN_RECIPIENT",
42
+
43
+ // Encrypted Key Extension
44
+ UNKNOWN_SECRET = "UNKNOWN_SECRET",
45
+
46
+ // Public Key Signing Extension
47
+ UNVERIFIED_SIGNATURE = "UNVERIFIED_SIGNATURE",
48
+ INVALID_OUTER_SIGNATURE_TYPE = "INVALID_OUTER_SIGNATURE_TYPE",
49
+ INVALID_INNER_SIGNATURE_TYPE = "INVALID_INNER_SIGNATURE_TYPE",
50
+ UNVERIFIED_INNER_SIGNATURE = "UNVERIFIED_INNER_SIGNATURE",
51
+ INVALID_SIGNATURE_TYPE = "INVALID_SIGNATURE_TYPE",
52
+
53
+ // SSKR Extension
54
+ INVALID_SHARES = "INVALID_SHARES",
55
+ SSKR = "SSKR",
56
+
57
+ // Types Extension
58
+ INVALID_TYPE = "INVALID_TYPE",
59
+ AMBIGUOUS_TYPE = "AMBIGUOUS_TYPE",
60
+
61
+ // Expressions Extension
62
+ UNEXPECTED_RESPONSE_ID = "UNEXPECTED_RESPONSE_ID",
63
+ INVALID_RESPONSE = "INVALID_RESPONSE",
64
+
65
+ // External errors
66
+ CBOR = "CBOR",
67
+ COMPONENTS = "COMPONENTS",
68
+ GENERAL = "GENERAL",
69
+ }
70
+
71
+ export class EnvelopeError extends Error {
72
+ readonly code: ErrorCode;
73
+ declare readonly cause?: Error;
74
+
75
+ constructor(code: ErrorCode, message: string, cause?: Error) {
76
+ super(message);
77
+ this.name = "EnvelopeError";
78
+ this.code = code;
79
+ if (cause !== undefined) {
80
+ this.cause = cause;
81
+ }
82
+
83
+ // Maintains proper stack trace for where our error was thrown (only available on V8)
84
+ if ("captureStackTrace" in Error) {
85
+ (
86
+ Error as {
87
+ captureStackTrace(target: object, constructor: typeof EnvelopeError): void;
88
+ }
89
+ ).captureStackTrace(this, EnvelopeError);
90
+ }
91
+ }
92
+
93
+ //
94
+ // Base Specification
95
+ /// Returned when attempting to compress or encrypt an envelope that has
96
+ /// already been elided.
97
+ ///
98
+ /// This error occurs because an elided envelope only contains a digest
99
+ /// reference and no longer has a subject that can be compressed or
100
+ /// encrypted.
101
+ static alreadyElided(): EnvelopeError {
102
+ return new EnvelopeError(
103
+ ErrorCode.ALREADY_ELIDED,
104
+ "envelope was elided, so it cannot be compressed or encrypted",
105
+ );
106
+ }
107
+
108
+ /// Returned when attempting to retrieve an assertion by predicate, but
109
+ /// multiple matching assertions exist.
110
+ ///
111
+ /// For queries that expect a single result (like `objectForPredicate`),
112
+ /// having multiple matching assertions is ambiguous and requires more
113
+ /// specific targeting.
114
+ static ambiguousPredicate(): EnvelopeError {
115
+ return new EnvelopeError(
116
+ ErrorCode.AMBIGUOUS_PREDICATE,
117
+ "more than one assertion matches the predicate",
118
+ );
119
+ }
120
+
121
+ /// Returned when a digest validation fails.
122
+ ///
123
+ /// This can occur when unwrapping an envelope, verifying signatures, or
124
+ /// other operations that rely on the integrity of envelope digests.
125
+ static invalidDigest(): EnvelopeError {
126
+ return new EnvelopeError(ErrorCode.INVALID_DIGEST, "digest did not match");
127
+ }
128
+
129
+ /// Returned when an envelope's format is invalid.
130
+ ///
131
+ /// This typically occurs during parsing or decoding of an envelope from
132
+ /// CBOR.
133
+ static invalidFormat(): EnvelopeError {
134
+ return new EnvelopeError(ErrorCode.INVALID_FORMAT, "invalid format");
135
+ }
136
+
137
+ /// Returned when a digest is expected but not found.
138
+ ///
139
+ /// This can occur when working with envelope structures that require digest
140
+ /// information, such as when working with elided envelopes.
141
+ static missingDigest(): EnvelopeError {
142
+ return new EnvelopeError(ErrorCode.MISSING_DIGEST, "a digest was expected but not found");
143
+ }
144
+
145
+ /// Returned when attempting to retrieve an assertion by predicate, but no
146
+ /// matching assertion exists.
147
+ ///
148
+ /// This error occurs with functions like `objectForPredicate` when the
149
+ /// specified predicate doesn't match any assertion in the envelope.
150
+ static nonexistentPredicate(): EnvelopeError {
151
+ return new EnvelopeError(ErrorCode.NONEXISTENT_PREDICATE, "no assertion matches the predicate");
152
+ }
153
+
154
+ /// Returned when attempting to unwrap an envelope that wasn't wrapped.
155
+ ///
156
+ /// This error occurs when calling `Envelope.tryUnwrap` on an
157
+ /// envelope that doesn't have the wrapped format.
158
+ static notWrapped(): EnvelopeError {
159
+ return new EnvelopeError(
160
+ ErrorCode.NOT_WRAPPED,
161
+ "cannot unwrap an envelope that was not wrapped",
162
+ );
163
+ }
164
+
165
+ /// Returned when expecting an envelope's subject to be a leaf, but it
166
+ /// isn't.
167
+ ///
168
+ /// This error occurs when calling methods that require access to a leaf
169
+ /// value but the envelope's subject is an assertion, node, or elided.
170
+ static notLeaf(): EnvelopeError {
171
+ return new EnvelopeError(ErrorCode.NOT_LEAF, "the envelope's subject is not a leaf");
172
+ }
173
+
174
+ /// Returned when expecting an envelope's subject to be an assertion, but it
175
+ /// isn't.
176
+ ///
177
+ /// This error occurs when calling methods that require an assertion
178
+ /// structure but the envelope's subject has a different format.
179
+ static notAssertion(): EnvelopeError {
180
+ return new EnvelopeError(ErrorCode.NOT_ASSERTION, "the envelope's subject is not an assertion");
181
+ }
182
+
183
+ /// Returned when assertion is invalid
184
+ static invalidAssertion(): EnvelopeError {
185
+ return new EnvelopeError(
186
+ ErrorCode.INVALID_ASSERTION,
187
+ "assertion must be a map with exactly one element",
188
+ );
189
+ }
190
+
191
+ //
192
+ // Attachments Extension
193
+ /// Returned when an attachment's format is invalid.
194
+ ///
195
+ /// This error occurs when an envelope contains an attachment with an
196
+ /// invalid structure according to the Envelope Attachment specification
197
+ /// (BCR-2023-006).
198
+ static invalidAttachment(): EnvelopeError {
199
+ return new EnvelopeError(ErrorCode.INVALID_ATTACHMENT, "invalid attachment");
200
+ }
201
+
202
+ /// Returned when an attachment is requested but does not exist.
203
+ ///
204
+ /// This error occurs when attempting to retrieve an attachment by ID that
205
+ /// doesn't exist in the envelope.
206
+ static nonexistentAttachment(): EnvelopeError {
207
+ return new EnvelopeError(ErrorCode.NONEXISTENT_ATTACHMENT, "nonexistent attachment");
208
+ }
209
+
210
+ /// Returned when multiple attachments match a single query.
211
+ ///
212
+ /// This error occurs when multiple attachments have the same ID, making
213
+ /// it ambiguous which attachment should be returned.
214
+ static ambiguousAttachment(): EnvelopeError {
215
+ return new EnvelopeError(ErrorCode.AMBIGUOUS_ATTACHMENT, "ambiguous attachment");
216
+ }
217
+
218
+ //
219
+ // Compression Extension
220
+ /// Returned when attempting to compress an envelope that is already
221
+ /// compressed.
222
+ ///
223
+ /// This error occurs when calling compression functions on an envelope that
224
+ /// already has compressed content, as defined in BCR-2023-005.
225
+ static alreadyCompressed(): EnvelopeError {
226
+ return new EnvelopeError(ErrorCode.ALREADY_COMPRESSED, "envelope was already compressed");
227
+ }
228
+
229
+ /// Returned when attempting to decompress an envelope that is not
230
+ /// compressed.
231
+ ///
232
+ /// This error occurs when calling decompression functions on an envelope
233
+ /// that doesn't contain compressed content.
234
+ static notCompressed(): EnvelopeError {
235
+ return new EnvelopeError(
236
+ ErrorCode.NOT_COMPRESSED,
237
+ "cannot decompress an envelope that was not compressed",
238
+ );
239
+ }
240
+
241
+ //
242
+ // Symmetric Encryption Extension
243
+ /// Returned when attempting to encrypt an envelope that is already
244
+ /// encrypted or compressed.
245
+ ///
246
+ /// This error occurs to prevent multiple layers of encryption or encryption
247
+ /// of compressed data, which could reduce security, as defined in
248
+ /// BCR-2023-004.
249
+ static alreadyEncrypted(): EnvelopeError {
250
+ return new EnvelopeError(
251
+ ErrorCode.ALREADY_ENCRYPTED,
252
+ "envelope was already encrypted or compressed, so it cannot be encrypted",
253
+ );
254
+ }
255
+
256
+ /// Returned when attempting to decrypt an envelope that is not encrypted.
257
+ ///
258
+ /// This error occurs when calling decryption functions on an envelope that
259
+ /// doesn't contain encrypted content.
260
+ static notEncrypted(): EnvelopeError {
261
+ return new EnvelopeError(
262
+ ErrorCode.NOT_ENCRYPTED,
263
+ "cannot decrypt an envelope that was not encrypted",
264
+ );
265
+ }
266
+
267
+ //
268
+ // Known Values Extension
269
+ /// Returned when expecting an envelope's subject to be a known value, but
270
+ /// it isn't.
271
+ ///
272
+ /// This error occurs when calling methods that require a known value (as
273
+ /// defined in BCR-2023-003) but the envelope's subject is a different
274
+ /// type.
275
+ static notKnownValue(): EnvelopeError {
276
+ return new EnvelopeError(
277
+ ErrorCode.NOT_KNOWN_VALUE,
278
+ "the envelope's subject is not a known value",
279
+ );
280
+ }
281
+
282
+ //
283
+ // Public Key Encryption Extension
284
+ /// Returned when attempting to decrypt an envelope with a recipient that
285
+ /// doesn't match.
286
+ ///
287
+ /// This error occurs when trying to use a private key to decrypt an
288
+ /// envelope that wasn't encrypted for the corresponding public key.
289
+ static unknownRecipient(): EnvelopeError {
290
+ return new EnvelopeError(ErrorCode.UNKNOWN_RECIPIENT, "unknown recipient");
291
+ }
292
+
293
+ //
294
+ // Encrypted Key Extension
295
+ /// Returned when attempting to decrypt an envelope with a secret that
296
+ /// doesn't match.
297
+ ///
298
+ /// This error occurs when trying to use a secret that does not correspond
299
+ /// to the expected recipient, preventing successful decryption.
300
+ static unknownSecret(): EnvelopeError {
301
+ return new EnvelopeError(ErrorCode.UNKNOWN_SECRET, "secret not found");
302
+ }
303
+
304
+ //
305
+ // Public Key Signing Extension
306
+ /// Returned when a signature verification fails.
307
+ ///
308
+ /// This error occurs when a signature does not validate against its
309
+ /// purported public key.
310
+ static unverifiedSignature(): EnvelopeError {
311
+ return new EnvelopeError(ErrorCode.UNVERIFIED_SIGNATURE, "could not verify a signature");
312
+ }
313
+
314
+ /// Returned when the outer signature object type is not `Signature`.
315
+ static invalidOuterSignatureType(): EnvelopeError {
316
+ return new EnvelopeError(
317
+ ErrorCode.INVALID_OUTER_SIGNATURE_TYPE,
318
+ "unexpected outer signature object type",
319
+ );
320
+ }
321
+
322
+ /// Returned when the inner signature object type is not `Signature`.
323
+ static invalidInnerSignatureType(): EnvelopeError {
324
+ return new EnvelopeError(
325
+ ErrorCode.INVALID_INNER_SIGNATURE_TYPE,
326
+ "unexpected inner signature object type",
327
+ );
328
+ }
329
+
330
+ /// Returned when the inner signature is not made with the same key as the
331
+ /// outer signature.
332
+ static unverifiedInnerSignature(): EnvelopeError {
333
+ return new EnvelopeError(
334
+ ErrorCode.UNVERIFIED_INNER_SIGNATURE,
335
+ "inner signature not made with same key as outer signature",
336
+ );
337
+ }
338
+
339
+ /// Returned when the signature object is not a `Signature`.
340
+ static invalidSignatureType(): EnvelopeError {
341
+ return new EnvelopeError(ErrorCode.INVALID_SIGNATURE_TYPE, "unexpected signature object type");
342
+ }
343
+
344
+ //
345
+ // SSKR Extension
346
+ /// Returned when SSKR shares are invalid or insufficient for
347
+ /// reconstruction.
348
+ ///
349
+ /// This error occurs when attempting to join SSKR shares that are
350
+ /// malformed, from different splits, or insufficient to meet the
351
+ /// recovery threshold.
352
+ static invalidShares(): EnvelopeError {
353
+ return new EnvelopeError(ErrorCode.INVALID_SHARES, "invalid SSKR shares");
354
+ }
355
+
356
+ /// SSKR error wrapper
357
+ static sskr(message: string, cause?: Error): EnvelopeError {
358
+ return new EnvelopeError(ErrorCode.SSKR, `sskr error: ${message}`, cause);
359
+ }
360
+
361
+ //
362
+ // Types Extension
363
+ /// Returned when an envelope contains an invalid type.
364
+ ///
365
+ /// This error occurs when an envelope's type information doesn't match
366
+ /// the expected format or value.
367
+ static invalidType(): EnvelopeError {
368
+ return new EnvelopeError(ErrorCode.INVALID_TYPE, "invalid type");
369
+ }
370
+
371
+ /// Returned when an envelope contains ambiguous type information.
372
+ ///
373
+ /// This error occurs when multiple type assertions exist that conflict
374
+ /// with each other or create ambiguity about the envelope's type.
375
+ static ambiguousType(): EnvelopeError {
376
+ return new EnvelopeError(ErrorCode.AMBIGUOUS_TYPE, "ambiguous type");
377
+ }
378
+
379
+ //
380
+ // Expressions Extension
381
+ /// Returned when a response envelope has an unexpected ID.
382
+ ///
383
+ /// This error occurs when processing a response envelope and the ID doesn't
384
+ /// match the expected request ID, as defined in BCR-2023-012.
385
+ static unexpectedResponseId(): EnvelopeError {
386
+ return new EnvelopeError(ErrorCode.UNEXPECTED_RESPONSE_ID, "unexpected response ID");
387
+ }
388
+
389
+ /// Returned when a response envelope is invalid.
390
+ static invalidResponse(): EnvelopeError {
391
+ return new EnvelopeError(ErrorCode.INVALID_RESPONSE, "invalid response");
392
+ }
393
+
394
+ //
395
+ // External errors
396
+ /// dcbor error wrapper
397
+ static cbor(message: string, cause?: Error): EnvelopeError {
398
+ return new EnvelopeError(ErrorCode.CBOR, `dcbor error: ${message}`, cause);
399
+ }
400
+
401
+ /// Components error wrapper
402
+ static components(message: string, cause?: Error): EnvelopeError {
403
+ return new EnvelopeError(ErrorCode.COMPONENTS, `components error: ${message}`, cause);
404
+ }
405
+
406
+ /// General error wrapper
407
+ static general(message: string, cause?: Error): EnvelopeError {
408
+ return new EnvelopeError(ErrorCode.GENERAL, `general error: ${message}`, cause);
409
+ }
410
+
411
+ /// Create error with custom message (equivalent to Rust's Error::msg)
412
+ static msg(message: string): EnvelopeError {
413
+ return EnvelopeError.general(message);
414
+ }
415
+ }
416
+
417
+ /// Type alias for Result type (for Rust compatibility)
418
+ export type Result<T> = T;
419
+
420
+ /// Export for backward compatibility
421
+ export type { EnvelopeError as Error };
@@ -0,0 +1,56 @@
1
+ /// Base module exports for Gordian Envelope.
2
+ ///
3
+ /// This module provides the core functionality for working with Gordian
4
+ /// Envelopes, including the main Envelope class, assertions, digests,
5
+ /// error handling, and various utility functions.
6
+
7
+ // Core types
8
+ export { Envelope, type EnvelopeCase } from "./envelope";
9
+ export { Assertion } from "./assertion";
10
+ export { Digest, type DigestProvider } from "./digest";
11
+
12
+ // Error handling
13
+ export { EnvelopeError, ErrorCode } from "./error";
14
+
15
+ // Encodable/Decodable traits
16
+ export {
17
+ type EnvelopeEncodable,
18
+ type EnvelopeEncodableValue,
19
+ isEnvelopeEncodable,
20
+ } from "./envelope-encodable";
21
+
22
+ // CBOR encoding/decoding
23
+ export {
24
+ EnvelopeCBORTagged,
25
+ EnvelopeCBORTaggedEncodable,
26
+ EnvelopeCBORTaggedDecodable,
27
+ envelopeToCbor,
28
+ envelopeFromCbor,
29
+ envelopeToBytes,
30
+ envelopeFromBytes,
31
+ } from "./cbor";
32
+
33
+ // Envelope decoding utilities
34
+ export {
35
+ extractString,
36
+ extractNumber,
37
+ extractBoolean,
38
+ extractBytes,
39
+ extractNull,
40
+ EnvelopeDecoder,
41
+ } from "./envelope-decodable";
42
+
43
+ // Elision and selective disclosure
44
+ export { ObscureType, type ObscureAction, elideAction } from "./elide";
45
+
46
+ // Walking/traversal
47
+ export { EdgeType, edgeLabel, type Visitor } from "./walk";
48
+
49
+ // Import side-effect modules to register prototype extensions
50
+ import "./assertions";
51
+ import "./leaf";
52
+ import "./queries";
53
+ import "./elide";
54
+ import "./wrap";
55
+ import "./walk";
56
+ import "./envelope-decodable";
@@ -0,0 +1,147 @@
1
+ import type { Cbor } from "@bcts/dcbor";
2
+ import { isNumber, isNaN, asArray, asMap, asText } from "@bcts/dcbor";
3
+ import { Envelope } from "./envelope";
4
+
5
+ /// Provides methods for working with envelope leaf nodes,
6
+ /// which are dCBOR values of any kind.
7
+ ///
8
+ /// This module extends the Envelope class with convenience methods for
9
+ /// working with leaf values, including type checking and extraction.
10
+
11
+ // Note: Static methods Envelope.false() and Envelope.true() are implemented below
12
+ // but cannot be declared in TypeScript module augmentation due to reserved keywords.
13
+
14
+ /// Implementation of static false()
15
+ (Envelope as unknown as { false: () => Envelope }).false = function (): Envelope {
16
+ return Envelope.newLeaf(false);
17
+ };
18
+
19
+ /// Implementation of static true()
20
+ (Envelope as unknown as { true: () => Envelope }).true = function (): Envelope {
21
+ return Envelope.newLeaf(true);
22
+ };
23
+
24
+ /// Implementation of isFalse()
25
+ Envelope.prototype.isFalse = function (this: Envelope): boolean {
26
+ try {
27
+ return this.extractBoolean() === false;
28
+ } catch {
29
+ return false;
30
+ }
31
+ };
32
+
33
+ /// Implementation of isTrue()
34
+ Envelope.prototype.isTrue = function (this: Envelope): boolean {
35
+ try {
36
+ return this.extractBoolean() === true;
37
+ } catch {
38
+ return false;
39
+ }
40
+ };
41
+
42
+ /// Implementation of isBool()
43
+ Envelope.prototype.isBool = function (this: Envelope): boolean {
44
+ try {
45
+ const value = this.extractBoolean();
46
+ return typeof value === "boolean";
47
+ } catch {
48
+ return false;
49
+ }
50
+ };
51
+
52
+ /// Implementation of isNumber()
53
+ Envelope.prototype.isNumber = function (this: Envelope): boolean {
54
+ const leaf = this.asLeaf();
55
+ if (leaf === undefined) {
56
+ return false;
57
+ }
58
+
59
+ return isNumber(leaf);
60
+ };
61
+
62
+ /// Implementation of isSubjectNumber()
63
+ Envelope.prototype.isSubjectNumber = function (this: Envelope): boolean {
64
+ return this.subject().isNumber();
65
+ };
66
+
67
+ /// Implementation of isNaN()
68
+ Envelope.prototype.isNaN = function (this: Envelope): boolean {
69
+ const leaf = this.asLeaf();
70
+ if (leaf === undefined) {
71
+ return false;
72
+ }
73
+
74
+ // Check for NaN in CBOR simple types
75
+ if ("type" in leaf && leaf.type === 7) {
76
+ return isNaN(leaf as unknown as Parameters<typeof isNaN>[0]);
77
+ }
78
+ return false;
79
+ };
80
+
81
+ /// Implementation of isSubjectNaN()
82
+ Envelope.prototype.isSubjectNaN = function (this: Envelope): boolean {
83
+ return this.subject().isNaN();
84
+ };
85
+
86
+ /// Implementation of isNull()
87
+ Envelope.prototype.isNull = function (this: Envelope): boolean {
88
+ try {
89
+ this.extractNull();
90
+ return true;
91
+ } catch (_error) {
92
+ return false;
93
+ }
94
+ };
95
+
96
+ /// Implementation of tryByteString()
97
+ Envelope.prototype.tryByteString = function (this: Envelope): Uint8Array {
98
+ return this.extractBytes();
99
+ };
100
+
101
+ /// Implementation of asByteString()
102
+ Envelope.prototype.asByteString = function (this: Envelope): Uint8Array | undefined {
103
+ try {
104
+ return this.extractBytes();
105
+ } catch {
106
+ return undefined;
107
+ }
108
+ };
109
+
110
+ /// Implementation of asArray()
111
+ Envelope.prototype.asArray = function (this: Envelope): readonly Cbor[] | undefined {
112
+ const leaf = this.asLeaf();
113
+ if (leaf === undefined) {
114
+ return undefined;
115
+ }
116
+
117
+ return asArray(leaf);
118
+ };
119
+
120
+ /// Implementation of asMap()
121
+ Envelope.prototype.asMap = function (this: Envelope) {
122
+ const leaf = this.asLeaf();
123
+ if (leaf === undefined) {
124
+ return undefined;
125
+ }
126
+
127
+ return asMap(leaf);
128
+ };
129
+
130
+ /// Implementation of asText()
131
+ Envelope.prototype.asText = function (this: Envelope): string | undefined {
132
+ const leaf = this.asLeaf();
133
+ if (leaf === undefined) {
134
+ return undefined;
135
+ }
136
+
137
+ return asText(leaf);
138
+ };
139
+
140
+ /// Implementation of asLeaf()
141
+ Envelope.prototype.asLeaf = function (this: Envelope): Cbor | undefined {
142
+ const c = this.case();
143
+ if (c.type === "leaf") {
144
+ return c.cbor;
145
+ }
146
+ return undefined;
147
+ };