@bcts/gstp 1.0.0-alpha.14

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,851 @@
1
+ import { ARID, Encrypter, PrivateKeys, Signer } from "@bcts/components";
2
+ import { Envelope, EnvelopeEncodableValue, Event, Expression, Function, ParameterID, Request } from "@bcts/envelope";
3
+ import { XIDDocument } from "@bcts/xid";
4
+
5
+ //#region src/error.d.ts
6
+
7
+ /**
8
+ * GSTP Error Types
9
+ *
10
+ * Error types returned when operating on GSTP messages.
11
+ * Ported from gstp-rust/src/error.rs
12
+ */
13
+ /**
14
+ * Error codes for GSTP operations.
15
+ */
16
+ declare enum GstpErrorCode {
17
+ /** Sender must have an encryption key. */
18
+ SENDER_MISSING_ENCRYPTION_KEY = "SENDER_MISSING_ENCRYPTION_KEY",
19
+ /** Recipient must have an encryption key. */
20
+ RECIPIENT_MISSING_ENCRYPTION_KEY = "RECIPIENT_MISSING_ENCRYPTION_KEY",
21
+ /** Sender must have a verification key. */
22
+ SENDER_MISSING_VERIFICATION_KEY = "SENDER_MISSING_VERIFICATION_KEY",
23
+ /** Continuation has expired. */
24
+ CONTINUATION_EXPIRED = "CONTINUATION_EXPIRED",
25
+ /** Continuation ID is invalid. */
26
+ CONTINUATION_ID_INVALID = "CONTINUATION_ID_INVALID",
27
+ /** Peer continuation must be encrypted. */
28
+ PEER_CONTINUATION_NOT_ENCRYPTED = "PEER_CONTINUATION_NOT_ENCRYPTED",
29
+ /** Requests must contain a peer continuation. */
30
+ MISSING_PEER_CONTINUATION = "MISSING_PEER_CONTINUATION",
31
+ /** Error from envelope operations. */
32
+ ENVELOPE = "ENVELOPE",
33
+ /** Error from XID operations. */
34
+ XID = "XID",
35
+ }
36
+ /**
37
+ * Error class for GSTP operations.
38
+ *
39
+ * Provides specific error types that can occur during GSTP message
40
+ * creation, sealing, and parsing operations.
41
+ */
42
+ declare class GstpError extends Error {
43
+ readonly code: GstpErrorCode;
44
+ readonly cause?: Error;
45
+ constructor(code: GstpErrorCode, message: string, cause?: Error);
46
+ /**
47
+ * Returned when the sender is missing an encryption key.
48
+ */
49
+ static senderMissingEncryptionKey(): GstpError;
50
+ /**
51
+ * Returned when the recipient is missing an encryption key.
52
+ */
53
+ static recipientMissingEncryptionKey(): GstpError;
54
+ /**
55
+ * Returned when the sender is missing a verification key.
56
+ */
57
+ static senderMissingVerificationKey(): GstpError;
58
+ /**
59
+ * Returned when the continuation has expired.
60
+ */
61
+ static continuationExpired(): GstpError;
62
+ /**
63
+ * Returned when the continuation ID is invalid.
64
+ */
65
+ static continuationIdInvalid(): GstpError;
66
+ /**
67
+ * Returned when the peer continuation is not encrypted.
68
+ */
69
+ static peerContinuationNotEncrypted(): GstpError;
70
+ /**
71
+ * Returned when a request is missing the peer continuation.
72
+ */
73
+ static missingPeerContinuation(): GstpError;
74
+ /**
75
+ * Envelope error wrapper.
76
+ */
77
+ static envelope(cause?: Error): GstpError;
78
+ /**
79
+ * XID error wrapper.
80
+ */
81
+ static xid(cause?: Error): GstpError;
82
+ }
83
+ //#endregion
84
+ //#region src/continuation.d.ts
85
+ /**
86
+ * Represents an encrypted state continuation.
87
+ *
88
+ * Continuations provide a way to maintain state across message exchanges
89
+ * without requiring local storage. The state is encrypted and embedded
90
+ * directly in the message envelope.
91
+ *
92
+ * @example
93
+ * ```typescript
94
+ * import { Continuation } from '@bcts/gstp';
95
+ *
96
+ * // Create a continuation with state
97
+ * const continuation = new Continuation("session state data")
98
+ * .withValidId(requestId)
99
+ * .withValidUntil(new Date(Date.now() + 60000)); // Valid for 60 seconds
100
+ *
101
+ * // Convert to envelope (optionally encrypted)
102
+ * const envelope = continuation.toEnvelope(recipientPublicKey);
103
+ * ```
104
+ */
105
+ declare class Continuation {
106
+ private readonly _state;
107
+ private readonly _validId;
108
+ private readonly _validUntil;
109
+ /**
110
+ * Creates a new Continuation with the given state.
111
+ *
112
+ * The state can be any value that implements EnvelopeEncodable.
113
+ *
114
+ * @param state - The state to embed in the continuation
115
+ * @param validId - Optional ID for validation
116
+ * @param validUntil - Optional expiration date
117
+ */
118
+ constructor(state: EnvelopeEncodableValue, validId?: ARID, validUntil?: Date);
119
+ /**
120
+ * Creates a new continuation with a specific valid ID.
121
+ *
122
+ * @param validId - The ID to use for validation
123
+ * @returns A new Continuation instance with the valid ID set
124
+ */
125
+ withValidId(validId: ARID): Continuation;
126
+ /**
127
+ * Creates a new continuation with an optional valid ID.
128
+ *
129
+ * @param validId - The ID to use for validation, or undefined
130
+ * @returns A new Continuation instance with the valid ID set
131
+ */
132
+ withOptionalValidId(validId: ARID | undefined): Continuation;
133
+ /**
134
+ * Creates a new continuation with a specific valid until date.
135
+ *
136
+ * @param validUntil - The date until which the continuation is valid
137
+ * @returns A new Continuation instance with the valid until date set
138
+ */
139
+ withValidUntil(validUntil: Date): Continuation;
140
+ /**
141
+ * Creates a new continuation with an optional valid until date.
142
+ *
143
+ * @param validUntil - The date until which the continuation is valid, or undefined
144
+ * @returns A new Continuation instance with the valid until date set
145
+ */
146
+ withOptionalValidUntil(validUntil: Date | undefined): Continuation;
147
+ /**
148
+ * Creates a new continuation with a validity duration from now.
149
+ *
150
+ * @param durationMs - The duration in milliseconds for which the continuation is valid
151
+ * @returns A new Continuation instance with the valid until date set
152
+ */
153
+ withValidDuration(durationMs: number): Continuation;
154
+ /**
155
+ * Returns the state envelope of the continuation.
156
+ */
157
+ state(): Envelope;
158
+ /**
159
+ * Returns the valid ID of the continuation, if set.
160
+ */
161
+ id(): ARID | undefined;
162
+ /**
163
+ * Returns the valid until date of the continuation, if set.
164
+ */
165
+ validUntil(): Date | undefined;
166
+ /**
167
+ * Checks if the continuation is valid at the given time.
168
+ *
169
+ * If no valid_until is set, always returns true.
170
+ * If no time is provided, always returns true.
171
+ *
172
+ * @param now - The time to check against, or undefined to skip time validation
173
+ * @returns true if the continuation is valid at the given time
174
+ */
175
+ isValidDate(now?: Date): boolean;
176
+ /**
177
+ * Checks if the continuation has the expected ID.
178
+ *
179
+ * If no valid_id is set, always returns true.
180
+ * If no ID is provided for checking, always returns true.
181
+ *
182
+ * @param id - The ID to check against, or undefined to skip ID validation
183
+ * @returns true if the continuation has the expected ID
184
+ */
185
+ isValidId(id?: ARID): boolean;
186
+ /**
187
+ * Checks if the continuation is valid (both date and ID).
188
+ *
189
+ * @param now - The time to check against, or undefined to skip time validation
190
+ * @param id - The ID to check against, or undefined to skip ID validation
191
+ * @returns true if the continuation is valid
192
+ */
193
+ isValid(now?: Date, id?: ARID): boolean;
194
+ /**
195
+ * Converts the continuation to an envelope.
196
+ *
197
+ * If a recipient is provided, the envelope is encrypted to that recipient.
198
+ *
199
+ * @param recipient - Optional recipient to encrypt the envelope to
200
+ * @returns The continuation as an envelope
201
+ */
202
+ toEnvelope(recipient?: Encrypter): Envelope;
203
+ /**
204
+ * Parses a continuation from an envelope.
205
+ *
206
+ * @param encryptedEnvelope - The envelope to parse
207
+ * @param expectedId - Optional ID to validate against
208
+ * @param now - Optional time to validate against
209
+ * @param recipient - Optional private keys to decrypt with
210
+ * @returns The parsed continuation
211
+ * @throws GstpError if validation fails or parsing fails
212
+ */
213
+ static tryFromEnvelope(encryptedEnvelope: Envelope, expectedId?: ARID, now?: Date, recipient?: PrivateKeys): Continuation;
214
+ /**
215
+ * Checks equality with another continuation.
216
+ *
217
+ * Two continuations are equal if they have the same state, ID, and valid_until.
218
+ *
219
+ * @param other - The continuation to compare with
220
+ * @returns true if the continuations are equal
221
+ */
222
+ equals(other: Continuation): boolean;
223
+ /**
224
+ * Returns a string representation of the continuation.
225
+ */
226
+ toString(): string;
227
+ }
228
+ //#endregion
229
+ //#region src/sealed-request.d.ts
230
+ /**
231
+ * Interface that defines the behavior of a sealed request.
232
+ *
233
+ * Extends RequestBehavior with additional methods for managing
234
+ * sender information and state continuations.
235
+ */
236
+ interface SealedRequestBehavior {
237
+ /**
238
+ * Adds state to the request that the receiver must return in the response.
239
+ */
240
+ withState(state: EnvelopeEncodableValue): SealedRequest;
241
+ /**
242
+ * Adds optional state to the request.
243
+ */
244
+ withOptionalState(state: EnvelopeEncodableValue | undefined): SealedRequest;
245
+ /**
246
+ * Adds a continuation previously received from the recipient.
247
+ */
248
+ withPeerContinuation(peerContinuation: Envelope): SealedRequest;
249
+ /**
250
+ * Adds an optional continuation previously received from the recipient.
251
+ */
252
+ withOptionalPeerContinuation(peerContinuation: Envelope | undefined): SealedRequest;
253
+ /**
254
+ * Returns the underlying request.
255
+ */
256
+ request(): Request;
257
+ /**
258
+ * Returns the sender of the request.
259
+ */
260
+ sender(): XIDDocument;
261
+ /**
262
+ * Returns the state to be sent to the recipient.
263
+ */
264
+ state(): Envelope | undefined;
265
+ /**
266
+ * Returns the continuation received from the recipient.
267
+ */
268
+ peerContinuation(): Envelope | undefined;
269
+ }
270
+ /**
271
+ * A sealed request that combines a Request with sender information and
272
+ * state continuations for secure communication.
273
+ *
274
+ * @example
275
+ * ```typescript
276
+ * import { SealedRequest, ARID } from '@bcts/gstp';
277
+ * import { XIDDocument } from '@bcts/xid';
278
+ *
279
+ * // Create sender XID document
280
+ * const sender = XIDDocument.new();
281
+ * const requestId = ARID.new();
282
+ *
283
+ * // Create a sealed request
284
+ * const request = SealedRequest.new("getBalance", requestId, sender)
285
+ * .withParameter("account", "alice")
286
+ * .withState("session-state-data")
287
+ * .withNote("Balance check");
288
+ *
289
+ * // Convert to sealed envelope
290
+ * const envelope = request.toEnvelope(
291
+ * new Date(Date.now() + 60000), // Valid for 60 seconds
292
+ * senderPrivateKey,
293
+ * recipientXIDDocument
294
+ * );
295
+ * ```
296
+ */
297
+ declare class SealedRequest implements SealedRequestBehavior {
298
+ private _request;
299
+ private readonly _sender;
300
+ private _state;
301
+ private _peerContinuation;
302
+ private constructor();
303
+ /**
304
+ * Creates a new sealed request with the given function, ID, and sender.
305
+ *
306
+ * @param func - The function to call (string name or Function object)
307
+ * @param id - The request ID
308
+ * @param sender - The sender's XID document
309
+ */
310
+ static new(func: string | number | Function, id: ARID, sender: XIDDocument): SealedRequest;
311
+ /**
312
+ * Creates a new sealed request with an expression body.
313
+ *
314
+ * @param body - The expression body
315
+ * @param id - The request ID
316
+ * @param sender - The sender's XID document
317
+ */
318
+ static newWithBody(body: Expression, id: ARID, sender: XIDDocument): SealedRequest;
319
+ /**
320
+ * Adds a parameter to the request.
321
+ */
322
+ withParameter(parameter: ParameterID, value: EnvelopeEncodableValue): SealedRequest;
323
+ /**
324
+ * Adds an optional parameter to the request.
325
+ */
326
+ withOptionalParameter(parameter: ParameterID, value: EnvelopeEncodableValue | undefined): SealedRequest;
327
+ /**
328
+ * Returns the function of the request.
329
+ */
330
+ function(): Function;
331
+ /**
332
+ * Returns the expression envelope of the request.
333
+ */
334
+ expressionEnvelope(): Envelope;
335
+ /**
336
+ * Returns the object for a parameter.
337
+ */
338
+ objectForParameter(param: ParameterID): Envelope | undefined;
339
+ /**
340
+ * Returns all objects for a parameter.
341
+ */
342
+ objectsForParameter(param: ParameterID): Envelope[];
343
+ /**
344
+ * Extracts an object for a parameter as a specific type.
345
+ */
346
+ extractObjectForParameter<T>(param: ParameterID): T;
347
+ /**
348
+ * Extracts an optional object for a parameter.
349
+ */
350
+ extractOptionalObjectForParameter<T>(param: ParameterID): T | undefined;
351
+ /**
352
+ * Extracts all objects for a parameter as a specific type.
353
+ */
354
+ extractObjectsForParameter<T>(param: ParameterID): T[];
355
+ /**
356
+ * Adds a note to the request.
357
+ */
358
+ withNote(note: string): SealedRequest;
359
+ /**
360
+ * Adds a date to the request.
361
+ */
362
+ withDate(date: Date): SealedRequest;
363
+ /**
364
+ * Returns the body of the request.
365
+ */
366
+ body(): Expression;
367
+ /**
368
+ * Returns the ID of the request.
369
+ */
370
+ id(): ARID;
371
+ /**
372
+ * Returns the note of the request.
373
+ */
374
+ note(): string;
375
+ /**
376
+ * Returns the date of the request.
377
+ */
378
+ date(): Date | undefined;
379
+ /**
380
+ * Adds state to the request that the receiver must return in the response.
381
+ */
382
+ withState(state: EnvelopeEncodableValue): SealedRequest;
383
+ /**
384
+ * Adds optional state to the request.
385
+ */
386
+ withOptionalState(state: EnvelopeEncodableValue | undefined): SealedRequest;
387
+ /**
388
+ * Adds a continuation previously received from the recipient.
389
+ */
390
+ withPeerContinuation(peerContinuation: Envelope): SealedRequest;
391
+ /**
392
+ * Adds an optional continuation previously received from the recipient.
393
+ */
394
+ withOptionalPeerContinuation(peerContinuation: Envelope | undefined): SealedRequest;
395
+ /**
396
+ * Returns the underlying request.
397
+ */
398
+ request(): Request;
399
+ /**
400
+ * Returns the sender of the request.
401
+ */
402
+ sender(): XIDDocument;
403
+ /**
404
+ * Returns the state to be sent to the recipient.
405
+ */
406
+ state(): Envelope | undefined;
407
+ /**
408
+ * Returns the continuation received from the recipient.
409
+ */
410
+ peerContinuation(): Envelope | undefined;
411
+ /**
412
+ * Converts the sealed request to a Request.
413
+ */
414
+ toRequest(): Request;
415
+ /**
416
+ * Converts the sealed request to an Expression.
417
+ */
418
+ toExpression(): Expression;
419
+ /**
420
+ * Creates an envelope that can be decrypted by zero or one recipient.
421
+ *
422
+ * @param validUntil - Optional expiration date for the continuation
423
+ * @param signer - Optional signer for the envelope
424
+ * @param recipient - Optional recipient XID document for encryption
425
+ * @returns The sealed request as an envelope
426
+ */
427
+ toEnvelope(validUntil?: Date, signer?: Signer, recipient?: XIDDocument): Envelope;
428
+ /**
429
+ * Creates an envelope that can be decrypted by zero or more recipients.
430
+ *
431
+ * @param validUntil - Optional expiration date for the continuation
432
+ * @param signer - Optional signer for the envelope
433
+ * @param recipients - Array of recipient XID documents for encryption
434
+ * @returns The sealed request as an envelope
435
+ */
436
+ toEnvelopeForRecipients(validUntil?: Date, signer?: Signer, recipients?: XIDDocument[]): Envelope;
437
+ /**
438
+ * Parses a sealed request from an encrypted envelope.
439
+ *
440
+ * @param encryptedEnvelope - The encrypted envelope to parse
441
+ * @param expectedId - Optional expected request ID for validation
442
+ * @param now - Optional current time for continuation validation
443
+ * @param recipient - The recipient's private keys for decryption
444
+ * @returns The parsed sealed request
445
+ */
446
+ static tryFromEnvelope(encryptedEnvelope: Envelope, expectedId: ARID | undefined, now: Date | undefined, recipient: PrivateKeys): SealedRequest;
447
+ /**
448
+ * Returns a string representation of the sealed request.
449
+ */
450
+ toString(): string;
451
+ /**
452
+ * Checks equality with another sealed request.
453
+ */
454
+ equals(other: SealedRequest): boolean;
455
+ }
456
+ //#endregion
457
+ //#region src/sealed-response.d.ts
458
+ /**
459
+ * Interface that defines the behavior of a sealed response.
460
+ *
461
+ * Extends ResponseBehavior with additional methods for managing
462
+ * sender information and state continuations.
463
+ */
464
+ interface SealedResponseBehavior {
465
+ /**
466
+ * Adds state to the response that the peer may return at some future time.
467
+ */
468
+ withState(state: EnvelopeEncodableValue): SealedResponse;
469
+ /**
470
+ * Adds optional state to the response.
471
+ */
472
+ withOptionalState(state: EnvelopeEncodableValue | undefined): SealedResponse;
473
+ /**
474
+ * Adds a continuation previously received from the recipient.
475
+ */
476
+ withPeerContinuation(peerContinuation: Envelope | undefined): SealedResponse;
477
+ /**
478
+ * Returns the sender of the response.
479
+ */
480
+ sender(): XIDDocument;
481
+ /**
482
+ * Returns the state to be sent to the peer.
483
+ */
484
+ state(): Envelope | undefined;
485
+ /**
486
+ * Returns the continuation received from the peer.
487
+ */
488
+ peerContinuation(): Envelope | undefined;
489
+ }
490
+ /**
491
+ * A sealed response that combines a Response with sender information and
492
+ * state continuations for secure communication.
493
+ *
494
+ * @example
495
+ * ```typescript
496
+ * import { SealedResponse, ARID } from '@bcts/gstp';
497
+ * import { XIDDocument } from '@bcts/xid';
498
+ *
499
+ * // Create sender XID document
500
+ * const sender = XIDDocument.new();
501
+ * const requestId = ARID.new();
502
+ *
503
+ * // Create a successful sealed response
504
+ * const response = SealedResponse.newSuccess(requestId, sender)
505
+ * .withResult("Operation completed")
506
+ * .withState("next-page-state");
507
+ *
508
+ * // Convert to sealed envelope
509
+ * const envelope = response.toEnvelope(
510
+ * new Date(Date.now() + 60000), // Valid for 60 seconds
511
+ * senderPrivateKey,
512
+ * recipientXIDDocument
513
+ * );
514
+ * ```
515
+ */
516
+ declare class SealedResponse implements SealedResponseBehavior {
517
+ private _response;
518
+ private readonly _sender;
519
+ private _state;
520
+ private _peerContinuation;
521
+ private constructor();
522
+ /**
523
+ * Creates a new successful sealed response.
524
+ *
525
+ * @param id - The request ID this response is for
526
+ * @param sender - The sender's XID document
527
+ */
528
+ static newSuccess(id: ARID, sender: XIDDocument): SealedResponse;
529
+ /**
530
+ * Creates a new failure sealed response.
531
+ *
532
+ * @param id - The request ID this response is for
533
+ * @param sender - The sender's XID document
534
+ */
535
+ static newFailure(id: ARID, sender: XIDDocument): SealedResponse;
536
+ /**
537
+ * Creates a new early failure sealed response.
538
+ *
539
+ * An early failure takes place before the message has been decrypted,
540
+ * and therefore the ID and sender public key are not known.
541
+ *
542
+ * @param sender - The sender's XID document
543
+ */
544
+ static newEarlyFailure(sender: XIDDocument): SealedResponse;
545
+ /**
546
+ * Adds state to the response that the peer may return at some future time.
547
+ *
548
+ * @throws Error if called on a failed response
549
+ */
550
+ withState(state: EnvelopeEncodableValue): SealedResponse;
551
+ /**
552
+ * Adds optional state to the response.
553
+ */
554
+ withOptionalState(state: EnvelopeEncodableValue | undefined): SealedResponse;
555
+ /**
556
+ * Adds a continuation previously received from the recipient.
557
+ */
558
+ withPeerContinuation(peerContinuation: Envelope | undefined): SealedResponse;
559
+ /**
560
+ * Returns the sender of the response.
561
+ */
562
+ sender(): XIDDocument;
563
+ /**
564
+ * Returns the state to be sent to the peer.
565
+ */
566
+ state(): Envelope | undefined;
567
+ /**
568
+ * Returns the continuation received from the peer.
569
+ */
570
+ peerContinuation(): Envelope | undefined;
571
+ /**
572
+ * Sets the result value for a successful response.
573
+ */
574
+ withResult(result: EnvelopeEncodableValue): SealedResponse;
575
+ /**
576
+ * Sets an optional result value for a successful response.
577
+ * If the result is undefined, the value of the response will be the null envelope.
578
+ */
579
+ withOptionalResult(result: EnvelopeEncodableValue | undefined): SealedResponse;
580
+ /**
581
+ * Sets the error value for a failure response.
582
+ */
583
+ withError(error: EnvelopeEncodableValue): SealedResponse;
584
+ /**
585
+ * Sets an optional error value for a failure response.
586
+ * If the error is undefined, the value of the response will be the unknown value.
587
+ */
588
+ withOptionalError(error: EnvelopeEncodableValue | undefined): SealedResponse;
589
+ /**
590
+ * Returns true if this is a successful response.
591
+ */
592
+ isOk(): boolean;
593
+ /**
594
+ * Returns true if this is a failure response.
595
+ */
596
+ isErr(): boolean;
597
+ /**
598
+ * Returns the ID of the request this response is for, if known.
599
+ */
600
+ id(): ARID | undefined;
601
+ /**
602
+ * Returns the ID of the request this response is for.
603
+ * @throws Error if the ID is not known
604
+ */
605
+ expectId(): ARID;
606
+ /**
607
+ * Returns the result envelope if this is a successful response.
608
+ * @throws Error if this is a failure response
609
+ */
610
+ result(): Envelope;
611
+ /**
612
+ * Extracts the result as a specific type.
613
+ */
614
+ extractResult<T>(decoder: (cbor: unknown) => T): T;
615
+ /**
616
+ * Returns the error envelope if this is a failure response.
617
+ * @throws Error if this is a successful response
618
+ */
619
+ error(): Envelope;
620
+ /**
621
+ * Extracts the error as a specific type.
622
+ */
623
+ extractError<T>(decoder: (cbor: unknown) => T): T;
624
+ /**
625
+ * Creates an envelope that can be decrypted by zero or one recipient.
626
+ *
627
+ * @param validUntil - Optional expiration date for the continuation
628
+ * @param signer - Optional signer for the envelope
629
+ * @param recipient - Optional recipient XID document for encryption
630
+ * @returns The sealed response as an envelope
631
+ */
632
+ toEnvelope(validUntil?: Date, signer?: Signer, recipient?: XIDDocument): Envelope;
633
+ /**
634
+ * Creates an envelope that can be decrypted by zero or more recipients.
635
+ *
636
+ * @param validUntil - Optional expiration date for the continuation
637
+ * @param signer - Optional signer for the envelope
638
+ * @param recipients - Array of recipient XID documents for encryption
639
+ * @returns The sealed response as an envelope
640
+ */
641
+ toEnvelopeForRecipients(validUntil?: Date, signer?: Signer, recipients?: XIDDocument[]): Envelope;
642
+ /**
643
+ * Parses a sealed response from an encrypted envelope.
644
+ *
645
+ * @param encryptedEnvelope - The encrypted envelope to parse
646
+ * @param expectedId - Optional expected request ID for validation
647
+ * @param now - Optional current time for continuation validation
648
+ * @param recipientPrivateKey - The recipient's private keys for decryption
649
+ * @returns The parsed sealed response
650
+ */
651
+ static tryFromEncryptedEnvelope(encryptedEnvelope: Envelope, expectedId: ARID | undefined, now: Date | undefined, recipientPrivateKey: PrivateKeys): SealedResponse;
652
+ /**
653
+ * Returns a string representation of the sealed response.
654
+ */
655
+ toString(): string;
656
+ /**
657
+ * Checks equality with another sealed response.
658
+ */
659
+ equals(other: SealedResponse): boolean;
660
+ }
661
+ //#endregion
662
+ //#region src/sealed-event.d.ts
663
+ /**
664
+ * Interface that defines the behavior of a sealed event.
665
+ *
666
+ * Extends EventBehavior with additional methods for managing
667
+ * sender information and state continuations.
668
+ */
669
+ interface SealedEventBehavior<T extends EnvelopeEncodableValue> {
670
+ /**
671
+ * Adds state to the event that the receiver must return in the response.
672
+ */
673
+ withState(state: EnvelopeEncodableValue): SealedEvent<T>;
674
+ /**
675
+ * Adds optional state to the event.
676
+ */
677
+ withOptionalState(state: EnvelopeEncodableValue | undefined): SealedEvent<T>;
678
+ /**
679
+ * Adds a continuation previously received from the recipient.
680
+ */
681
+ withPeerContinuation(peerContinuation: Envelope): SealedEvent<T>;
682
+ /**
683
+ * Adds an optional continuation previously received from the recipient.
684
+ */
685
+ withOptionalPeerContinuation(peerContinuation: Envelope | undefined): SealedEvent<T>;
686
+ /**
687
+ * Returns the underlying event.
688
+ */
689
+ event(): Event<T>;
690
+ /**
691
+ * Returns the sender of the event.
692
+ */
693
+ sender(): XIDDocument;
694
+ /**
695
+ * Returns the state to be sent to the recipient.
696
+ */
697
+ state(): Envelope | undefined;
698
+ /**
699
+ * Returns the continuation received from the recipient.
700
+ */
701
+ peerContinuation(): Envelope | undefined;
702
+ }
703
+ /**
704
+ * A sealed event that combines an Event with sender information and
705
+ * state continuations for secure communication.
706
+ *
707
+ * @typeParam T - The type of content this event carries
708
+ *
709
+ * @example
710
+ * ```typescript
711
+ * import { SealedEvent, ARID } from '@bcts/gstp';
712
+ * import { XIDDocument } from '@bcts/xid';
713
+ *
714
+ * // Create sender XID document
715
+ * const sender = XIDDocument.new();
716
+ * const eventId = ARID.new();
717
+ *
718
+ * // Create a sealed event
719
+ * const event = SealedEvent.new("System notification", eventId, sender)
720
+ * .withNote("Status update")
721
+ * .withDate(new Date());
722
+ *
723
+ * // Convert to sealed envelope
724
+ * const envelope = event.toEnvelope(
725
+ * new Date(Date.now() + 60000), // Valid for 60 seconds
726
+ * senderPrivateKey,
727
+ * recipientXIDDocument
728
+ * );
729
+ * ```
730
+ */
731
+ declare class SealedEvent<T extends EnvelopeEncodableValue> implements SealedEventBehavior<T> {
732
+ private _event;
733
+ private readonly _sender;
734
+ private _state;
735
+ private _peerContinuation;
736
+ private constructor();
737
+ /**
738
+ * Creates a new sealed event with the given content, ID, and sender.
739
+ *
740
+ * @param content - The content of the event
741
+ * @param id - The event ID
742
+ * @param sender - The sender's XID document
743
+ */
744
+ static new<T extends EnvelopeEncodableValue>(content: T, id: ARID, sender: XIDDocument): SealedEvent<T>;
745
+ /**
746
+ * Adds a note to the event.
747
+ */
748
+ withNote(note: string): SealedEvent<T>;
749
+ /**
750
+ * Adds a date to the event.
751
+ */
752
+ withDate(date: Date): SealedEvent<T>;
753
+ /**
754
+ * Returns the content of the event.
755
+ */
756
+ content(): T;
757
+ /**
758
+ * Returns the ID of the event.
759
+ */
760
+ id(): ARID;
761
+ /**
762
+ * Returns the note of the event.
763
+ */
764
+ note(): string;
765
+ /**
766
+ * Returns the date of the event.
767
+ */
768
+ date(): Date | undefined;
769
+ /**
770
+ * Adds state to the event that the receiver must return in the response.
771
+ */
772
+ withState(state: EnvelopeEncodableValue): SealedEvent<T>;
773
+ /**
774
+ * Adds optional state to the event.
775
+ */
776
+ withOptionalState(state: EnvelopeEncodableValue | undefined): SealedEvent<T>;
777
+ /**
778
+ * Adds a continuation previously received from the recipient.
779
+ */
780
+ withPeerContinuation(peerContinuation: Envelope): SealedEvent<T>;
781
+ /**
782
+ * Adds an optional continuation previously received from the recipient.
783
+ */
784
+ withOptionalPeerContinuation(peerContinuation: Envelope | undefined): SealedEvent<T>;
785
+ /**
786
+ * Returns the underlying event.
787
+ */
788
+ event(): Event<T>;
789
+ /**
790
+ * Returns the sender of the event.
791
+ */
792
+ sender(): XIDDocument;
793
+ /**
794
+ * Returns the state to be sent to the recipient.
795
+ */
796
+ state(): Envelope | undefined;
797
+ /**
798
+ * Returns the continuation received from the recipient.
799
+ */
800
+ peerContinuation(): Envelope | undefined;
801
+ /**
802
+ * Converts the sealed event to an Event.
803
+ */
804
+ toEvent(): Event<T>;
805
+ /**
806
+ * Creates an envelope that can be decrypted by zero or one recipient.
807
+ *
808
+ * @param validUntil - Optional expiration date for the continuation
809
+ * @param signer - Optional signer for the envelope
810
+ * @param recipient - Optional recipient XID document for encryption
811
+ * @returns The sealed event as an envelope
812
+ */
813
+ toEnvelope(validUntil?: Date, signer?: Signer, recipient?: XIDDocument): Envelope;
814
+ /**
815
+ * Creates an envelope that can be decrypted by zero or more recipients.
816
+ *
817
+ * @param validUntil - Optional expiration date for the continuation
818
+ * @param signer - Optional signer for the envelope
819
+ * @param recipients - Array of recipient XID documents for encryption
820
+ * @returns The sealed event as an envelope
821
+ */
822
+ toEnvelopeForRecipients(validUntil?: Date, signer?: Signer, recipients?: XIDDocument[]): Envelope;
823
+ /**
824
+ * Parses a sealed event from an encrypted envelope.
825
+ *
826
+ * @param encryptedEnvelope - The encrypted envelope to parse
827
+ * @param expectedId - Optional expected event ID for validation
828
+ * @param now - Optional current time for continuation validation
829
+ * @param recipientPrivateKey - The recipient's private keys for decryption
830
+ * @param contentExtractor - Function to extract content from envelope
831
+ * @returns The parsed sealed event
832
+ */
833
+ static tryFromEnvelope<T extends EnvelopeEncodableValue>(encryptedEnvelope: Envelope, expectedId: ARID | undefined, now: Date | undefined, recipientPrivateKey: PrivateKeys, contentExtractor?: (env: Envelope) => T): SealedEvent<T>;
834
+ /**
835
+ * Returns a string representation of the sealed event.
836
+ */
837
+ toString(): string;
838
+ /**
839
+ * Checks equality with another sealed event.
840
+ */
841
+ equals(other: SealedEvent<T>): boolean;
842
+ }
843
+ declare namespace prelude_d_exports {
844
+ export { Continuation, GstpError, GstpErrorCode, SealedEvent, SealedEventBehavior, SealedRequest, SealedRequestBehavior, SealedResponse, SealedResponseBehavior };
845
+ }
846
+ //#endregion
847
+ //#region src/index.d.ts
848
+ declare const VERSION = "0.13.0";
849
+ //#endregion
850
+ export { Continuation, GstpError, GstpErrorCode, SealedEvent, type SealedEventBehavior, SealedRequest, type SealedRequestBehavior, SealedResponse, type SealedResponseBehavior, VERSION, prelude_d_exports as prelude };
851
+ //# sourceMappingURL=index.d.mts.map