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