@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
|
@@ -0,0 +1,1308 @@
|
|
|
1
|
+
var bctsGstp = (function(exports, _bcts_components, _bcts_envelope, _bcts_known_values, _bcts_xid) {
|
|
2
|
+
|
|
3
|
+
//#region rolldown:runtime
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __exportAll = (all, symbols) => {
|
|
6
|
+
let target = {};
|
|
7
|
+
for (var name in all) {
|
|
8
|
+
__defProp(target, name, {
|
|
9
|
+
get: all[name],
|
|
10
|
+
enumerable: true
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
if (symbols) {
|
|
14
|
+
__defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
15
|
+
}
|
|
16
|
+
return target;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
//#endregion
|
|
20
|
+
|
|
21
|
+
//#region src/error.ts
|
|
22
|
+
/**
|
|
23
|
+
* GSTP Error Types
|
|
24
|
+
*
|
|
25
|
+
* Error types returned when operating on GSTP messages.
|
|
26
|
+
* Ported from gstp-rust/src/error.rs
|
|
27
|
+
*/
|
|
28
|
+
/**
|
|
29
|
+
* Error codes for GSTP operations.
|
|
30
|
+
*/
|
|
31
|
+
let GstpErrorCode = /* @__PURE__ */ function(GstpErrorCode$1) {
|
|
32
|
+
/** Sender must have an encryption key. */
|
|
33
|
+
GstpErrorCode$1["SENDER_MISSING_ENCRYPTION_KEY"] = "SENDER_MISSING_ENCRYPTION_KEY";
|
|
34
|
+
/** Recipient must have an encryption key. */
|
|
35
|
+
GstpErrorCode$1["RECIPIENT_MISSING_ENCRYPTION_KEY"] = "RECIPIENT_MISSING_ENCRYPTION_KEY";
|
|
36
|
+
/** Sender must have a verification key. */
|
|
37
|
+
GstpErrorCode$1["SENDER_MISSING_VERIFICATION_KEY"] = "SENDER_MISSING_VERIFICATION_KEY";
|
|
38
|
+
/** Continuation has expired. */
|
|
39
|
+
GstpErrorCode$1["CONTINUATION_EXPIRED"] = "CONTINUATION_EXPIRED";
|
|
40
|
+
/** Continuation ID is invalid. */
|
|
41
|
+
GstpErrorCode$1["CONTINUATION_ID_INVALID"] = "CONTINUATION_ID_INVALID";
|
|
42
|
+
/** Peer continuation must be encrypted. */
|
|
43
|
+
GstpErrorCode$1["PEER_CONTINUATION_NOT_ENCRYPTED"] = "PEER_CONTINUATION_NOT_ENCRYPTED";
|
|
44
|
+
/** Requests must contain a peer continuation. */
|
|
45
|
+
GstpErrorCode$1["MISSING_PEER_CONTINUATION"] = "MISSING_PEER_CONTINUATION";
|
|
46
|
+
/** Error from envelope operations. */
|
|
47
|
+
GstpErrorCode$1["ENVELOPE"] = "ENVELOPE";
|
|
48
|
+
/** Error from XID operations. */
|
|
49
|
+
GstpErrorCode$1["XID"] = "XID";
|
|
50
|
+
return GstpErrorCode$1;
|
|
51
|
+
}({});
|
|
52
|
+
/**
|
|
53
|
+
* Error class for GSTP operations.
|
|
54
|
+
*
|
|
55
|
+
* Provides specific error types that can occur during GSTP message
|
|
56
|
+
* creation, sealing, and parsing operations.
|
|
57
|
+
*/
|
|
58
|
+
var GstpError = class GstpError extends Error {
|
|
59
|
+
code;
|
|
60
|
+
constructor(code, message, cause) {
|
|
61
|
+
super(message);
|
|
62
|
+
this.name = "GstpError";
|
|
63
|
+
this.code = code;
|
|
64
|
+
if (cause !== void 0) this.cause = cause;
|
|
65
|
+
if ("captureStackTrace" in Error) Error.captureStackTrace(this, GstpError);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Returned when the sender is missing an encryption key.
|
|
69
|
+
*/
|
|
70
|
+
static senderMissingEncryptionKey() {
|
|
71
|
+
return new GstpError(GstpErrorCode.SENDER_MISSING_ENCRYPTION_KEY, "sender must have an encryption key");
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Returned when the recipient is missing an encryption key.
|
|
75
|
+
*/
|
|
76
|
+
static recipientMissingEncryptionKey() {
|
|
77
|
+
return new GstpError(GstpErrorCode.RECIPIENT_MISSING_ENCRYPTION_KEY, "recipient must have an encryption key");
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Returned when the sender is missing a verification key.
|
|
81
|
+
*/
|
|
82
|
+
static senderMissingVerificationKey() {
|
|
83
|
+
return new GstpError(GstpErrorCode.SENDER_MISSING_VERIFICATION_KEY, "sender must have a verification key");
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Returned when the continuation has expired.
|
|
87
|
+
*/
|
|
88
|
+
static continuationExpired() {
|
|
89
|
+
return new GstpError(GstpErrorCode.CONTINUATION_EXPIRED, "continuation expired");
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Returned when the continuation ID is invalid.
|
|
93
|
+
*/
|
|
94
|
+
static continuationIdInvalid() {
|
|
95
|
+
return new GstpError(GstpErrorCode.CONTINUATION_ID_INVALID, "continuation ID invalid");
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Returned when the peer continuation is not encrypted.
|
|
99
|
+
*/
|
|
100
|
+
static peerContinuationNotEncrypted() {
|
|
101
|
+
return new GstpError(GstpErrorCode.PEER_CONTINUATION_NOT_ENCRYPTED, "peer continuation must be encrypted");
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Returned when a request is missing the peer continuation.
|
|
105
|
+
*/
|
|
106
|
+
static missingPeerContinuation() {
|
|
107
|
+
return new GstpError(GstpErrorCode.MISSING_PEER_CONTINUATION, "requests must contain a peer continuation");
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Envelope error wrapper.
|
|
111
|
+
*/
|
|
112
|
+
static envelope(cause) {
|
|
113
|
+
return new GstpError(GstpErrorCode.ENVELOPE, "envelope error", cause);
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* XID error wrapper.
|
|
117
|
+
*/
|
|
118
|
+
static xid(cause) {
|
|
119
|
+
return new GstpError(GstpErrorCode.XID, "XID error", cause);
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
//#endregion
|
|
124
|
+
//#region src/continuation.ts
|
|
125
|
+
/**
|
|
126
|
+
* Continuation - Encrypted State Continuations (ESC)
|
|
127
|
+
*
|
|
128
|
+
* Continuations embed encrypted state data directly into messages,
|
|
129
|
+
* eliminating the need for local state storage and enhancing security
|
|
130
|
+
* for devices with limited storage or requiring distributed state management.
|
|
131
|
+
*
|
|
132
|
+
* Ported from gstp-rust/src/continuation.rs
|
|
133
|
+
*/
|
|
134
|
+
/**
|
|
135
|
+
* Represents an encrypted state continuation.
|
|
136
|
+
*
|
|
137
|
+
* Continuations provide a way to maintain state across message exchanges
|
|
138
|
+
* without requiring local storage. The state is encrypted and embedded
|
|
139
|
+
* directly in the message envelope.
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* ```typescript
|
|
143
|
+
* import { Continuation } from '@bcts/gstp';
|
|
144
|
+
*
|
|
145
|
+
* // Create a continuation with state
|
|
146
|
+
* const continuation = new Continuation("session state data")
|
|
147
|
+
* .withValidId(requestId)
|
|
148
|
+
* .withValidUntil(new Date(Date.now() + 60000)); // Valid for 60 seconds
|
|
149
|
+
*
|
|
150
|
+
* // Convert to envelope (optionally encrypted)
|
|
151
|
+
* const envelope = continuation.toEnvelope(recipientPublicKey);
|
|
152
|
+
* ```
|
|
153
|
+
*/
|
|
154
|
+
var Continuation = class Continuation {
|
|
155
|
+
_state;
|
|
156
|
+
_validId;
|
|
157
|
+
_validUntil;
|
|
158
|
+
/**
|
|
159
|
+
* Creates a new Continuation with the given state.
|
|
160
|
+
*
|
|
161
|
+
* The state can be any value that implements EnvelopeEncodable.
|
|
162
|
+
*
|
|
163
|
+
* @param state - The state to embed in the continuation
|
|
164
|
+
* @param validId - Optional ID for validation
|
|
165
|
+
* @param validUntil - Optional expiration date
|
|
166
|
+
*/
|
|
167
|
+
constructor(state, validId, validUntil) {
|
|
168
|
+
this._state = _bcts_envelope.Envelope.new(state);
|
|
169
|
+
this._validId = validId;
|
|
170
|
+
this._validUntil = validUntil;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Creates a new continuation with a specific valid ID.
|
|
174
|
+
*
|
|
175
|
+
* @param validId - The ID to use for validation
|
|
176
|
+
* @returns A new Continuation instance with the valid ID set
|
|
177
|
+
*/
|
|
178
|
+
withValidId(validId) {
|
|
179
|
+
return new Continuation(this._state, validId, this._validUntil);
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Creates a new continuation with an optional valid ID.
|
|
183
|
+
*
|
|
184
|
+
* @param validId - The ID to use for validation, or undefined
|
|
185
|
+
* @returns A new Continuation instance with the valid ID set
|
|
186
|
+
*/
|
|
187
|
+
withOptionalValidId(validId) {
|
|
188
|
+
return new Continuation(this._state, validId, this._validUntil);
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Creates a new continuation with a specific valid until date.
|
|
192
|
+
*
|
|
193
|
+
* @param validUntil - The date until which the continuation is valid
|
|
194
|
+
* @returns A new Continuation instance with the valid until date set
|
|
195
|
+
*/
|
|
196
|
+
withValidUntil(validUntil) {
|
|
197
|
+
return new Continuation(this._state, this._validId, validUntil);
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Creates a new continuation with an optional valid until date.
|
|
201
|
+
*
|
|
202
|
+
* @param validUntil - The date until which the continuation is valid, or undefined
|
|
203
|
+
* @returns A new Continuation instance with the valid until date set
|
|
204
|
+
*/
|
|
205
|
+
withOptionalValidUntil(validUntil) {
|
|
206
|
+
return new Continuation(this._state, this._validId, validUntil);
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Creates a new continuation with a validity duration from now.
|
|
210
|
+
*
|
|
211
|
+
* @param durationMs - The duration in milliseconds for which the continuation is valid
|
|
212
|
+
* @returns A new Continuation instance with the valid until date set
|
|
213
|
+
*/
|
|
214
|
+
withValidDuration(durationMs) {
|
|
215
|
+
const validUntil = new Date(Date.now() + durationMs);
|
|
216
|
+
return new Continuation(this._state, this._validId, validUntil);
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Returns the state envelope of the continuation.
|
|
220
|
+
*/
|
|
221
|
+
state() {
|
|
222
|
+
return this._state;
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Returns the valid ID of the continuation, if set.
|
|
226
|
+
*/
|
|
227
|
+
id() {
|
|
228
|
+
return this._validId;
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Returns the valid until date of the continuation, if set.
|
|
232
|
+
*/
|
|
233
|
+
validUntil() {
|
|
234
|
+
return this._validUntil;
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Checks if the continuation is valid at the given time.
|
|
238
|
+
*
|
|
239
|
+
* If no valid_until is set, always returns true.
|
|
240
|
+
* If no time is provided, always returns true.
|
|
241
|
+
*
|
|
242
|
+
* @param now - The time to check against, or undefined to skip time validation
|
|
243
|
+
* @returns true if the continuation is valid at the given time
|
|
244
|
+
*/
|
|
245
|
+
isValidDate(now) {
|
|
246
|
+
if (this._validUntil === void 0) return true;
|
|
247
|
+
if (now === void 0) return true;
|
|
248
|
+
return now.getTime() <= this._validUntil.getTime();
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Checks if the continuation has the expected ID.
|
|
252
|
+
*
|
|
253
|
+
* If no valid_id is set, always returns true.
|
|
254
|
+
* If no ID is provided for checking, always returns true.
|
|
255
|
+
*
|
|
256
|
+
* @param id - The ID to check against, or undefined to skip ID validation
|
|
257
|
+
* @returns true if the continuation has the expected ID
|
|
258
|
+
*/
|
|
259
|
+
isValidId(id) {
|
|
260
|
+
if (this._validId === void 0) return true;
|
|
261
|
+
if (id === void 0) return true;
|
|
262
|
+
return this._validId.equals(id);
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Checks if the continuation is valid (both date and ID).
|
|
266
|
+
*
|
|
267
|
+
* @param now - The time to check against, or undefined to skip time validation
|
|
268
|
+
* @param id - The ID to check against, or undefined to skip ID validation
|
|
269
|
+
* @returns true if the continuation is valid
|
|
270
|
+
*/
|
|
271
|
+
isValid(now, id) {
|
|
272
|
+
return this.isValidDate(now) && this.isValidId(id);
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Converts the continuation to an envelope.
|
|
276
|
+
*
|
|
277
|
+
* If a recipient is provided, the envelope is encrypted to that recipient.
|
|
278
|
+
*
|
|
279
|
+
* @param recipient - Optional recipient to encrypt the envelope to
|
|
280
|
+
* @returns The continuation as an envelope
|
|
281
|
+
*/
|
|
282
|
+
toEnvelope(recipient) {
|
|
283
|
+
let envelope = this._state;
|
|
284
|
+
if (this._validId !== void 0) envelope = envelope.addAssertion(_bcts_known_values.ID, this._validId);
|
|
285
|
+
if (this._validUntil !== void 0) envelope = envelope.addAssertion(_bcts_known_values.VALID_UNTIL, this._validUntil.toISOString());
|
|
286
|
+
if (recipient !== void 0) envelope = envelope.encryptToRecipients([recipient]);
|
|
287
|
+
return envelope;
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Parses a continuation from an envelope.
|
|
291
|
+
*
|
|
292
|
+
* @param encryptedEnvelope - The envelope to parse
|
|
293
|
+
* @param expectedId - Optional ID to validate against
|
|
294
|
+
* @param now - Optional time to validate against
|
|
295
|
+
* @param recipient - Optional private keys to decrypt with
|
|
296
|
+
* @returns The parsed continuation
|
|
297
|
+
* @throws GstpError if validation fails or parsing fails
|
|
298
|
+
*/
|
|
299
|
+
static tryFromEnvelope(encryptedEnvelope, expectedId, now, recipient) {
|
|
300
|
+
let envelope = encryptedEnvelope;
|
|
301
|
+
if (recipient !== void 0) try {
|
|
302
|
+
envelope = encryptedEnvelope.decryptToRecipient(recipient);
|
|
303
|
+
} catch (e) {
|
|
304
|
+
throw GstpError.envelope(e instanceof Error ? e : new Error(String(e)));
|
|
305
|
+
}
|
|
306
|
+
const state = envelope.subject();
|
|
307
|
+
let validId;
|
|
308
|
+
try {
|
|
309
|
+
const idObj = envelope.objectForPredicate(_bcts_known_values.ID);
|
|
310
|
+
if (idObj !== void 0) {
|
|
311
|
+
const leafCbor = idObj.asLeaf();
|
|
312
|
+
if (leafCbor !== void 0) validId = _bcts_components.ARID.fromTaggedCborData(leafCbor.toData());
|
|
313
|
+
}
|
|
314
|
+
} catch {}
|
|
315
|
+
let validUntil;
|
|
316
|
+
try {
|
|
317
|
+
const validUntilObj = envelope.objectForPredicate(_bcts_known_values.VALID_UNTIL);
|
|
318
|
+
if (validUntilObj !== void 0) {
|
|
319
|
+
const dateStr = validUntilObj.asText();
|
|
320
|
+
if (dateStr !== void 0) validUntil = new Date(dateStr);
|
|
321
|
+
}
|
|
322
|
+
} catch {}
|
|
323
|
+
const continuation = new Continuation(state, validId, validUntil);
|
|
324
|
+
if (!continuation.isValidDate(now)) throw GstpError.continuationExpired();
|
|
325
|
+
if (!continuation.isValidId(expectedId)) throw GstpError.continuationIdInvalid();
|
|
326
|
+
return continuation;
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* Checks equality with another continuation.
|
|
330
|
+
*
|
|
331
|
+
* Two continuations are equal if they have the same state, ID, and valid_until.
|
|
332
|
+
*
|
|
333
|
+
* @param other - The continuation to compare with
|
|
334
|
+
* @returns true if the continuations are equal
|
|
335
|
+
*/
|
|
336
|
+
equals(other) {
|
|
337
|
+
if (!this._state.digest().equals(other._state.digest())) return false;
|
|
338
|
+
if (this._validId === void 0 && other._validId === void 0) {} else if (this._validId !== void 0 && other._validId !== void 0) {
|
|
339
|
+
if (!this._validId.equals(other._validId)) return false;
|
|
340
|
+
} else return false;
|
|
341
|
+
if (this._validUntil === void 0 && other._validUntil === void 0) {} else if (this._validUntil !== void 0 && other._validUntil !== void 0) {
|
|
342
|
+
if (this._validUntil.getTime() !== other._validUntil.getTime()) return false;
|
|
343
|
+
} else return false;
|
|
344
|
+
return true;
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* Returns a string representation of the continuation.
|
|
348
|
+
*/
|
|
349
|
+
toString() {
|
|
350
|
+
const parts = [`Continuation(state: ${this._state.formatFlat()}`];
|
|
351
|
+
if (this._validId !== void 0) parts.push(`id: ${this._validId.shortDescription()}`);
|
|
352
|
+
if (this._validUntil !== void 0) parts.push(`validUntil: ${this._validUntil.toISOString()}`);
|
|
353
|
+
return `${parts.join(", ")})`;
|
|
354
|
+
}
|
|
355
|
+
};
|
|
356
|
+
|
|
357
|
+
//#endregion
|
|
358
|
+
//#region src/sealed-request.ts
|
|
359
|
+
/**
|
|
360
|
+
* A sealed request that combines a Request with sender information and
|
|
361
|
+
* state continuations for secure communication.
|
|
362
|
+
*
|
|
363
|
+
* @example
|
|
364
|
+
* ```typescript
|
|
365
|
+
* import { SealedRequest, ARID } from '@bcts/gstp';
|
|
366
|
+
* import { XIDDocument } from '@bcts/xid';
|
|
367
|
+
*
|
|
368
|
+
* // Create sender XID document
|
|
369
|
+
* const sender = XIDDocument.new();
|
|
370
|
+
* const requestId = ARID.new();
|
|
371
|
+
*
|
|
372
|
+
* // Create a sealed request
|
|
373
|
+
* const request = SealedRequest.new("getBalance", requestId, sender)
|
|
374
|
+
* .withParameter("account", "alice")
|
|
375
|
+
* .withState("session-state-data")
|
|
376
|
+
* .withNote("Balance check");
|
|
377
|
+
*
|
|
378
|
+
* // Convert to sealed envelope
|
|
379
|
+
* const envelope = request.toEnvelope(
|
|
380
|
+
* new Date(Date.now() + 60000), // Valid for 60 seconds
|
|
381
|
+
* senderPrivateKey,
|
|
382
|
+
* recipientXIDDocument
|
|
383
|
+
* );
|
|
384
|
+
* ```
|
|
385
|
+
*/
|
|
386
|
+
var SealedRequest = class SealedRequest {
|
|
387
|
+
_request;
|
|
388
|
+
_sender;
|
|
389
|
+
_state;
|
|
390
|
+
_peerContinuation;
|
|
391
|
+
constructor(request, sender, state, peerContinuation) {
|
|
392
|
+
this._request = request;
|
|
393
|
+
this._sender = sender;
|
|
394
|
+
this._state = state;
|
|
395
|
+
this._peerContinuation = peerContinuation;
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* Creates a new sealed request with the given function, ID, and sender.
|
|
399
|
+
*
|
|
400
|
+
* @param func - The function to call (string name or Function object)
|
|
401
|
+
* @param id - The request ID
|
|
402
|
+
* @param sender - The sender's XID document
|
|
403
|
+
*/
|
|
404
|
+
static new(func, id, sender) {
|
|
405
|
+
return new SealedRequest(_bcts_envelope.Request.new(func, id), sender);
|
|
406
|
+
}
|
|
407
|
+
/**
|
|
408
|
+
* Creates a new sealed request with an expression body.
|
|
409
|
+
*
|
|
410
|
+
* @param body - The expression body
|
|
411
|
+
* @param id - The request ID
|
|
412
|
+
* @param sender - The sender's XID document
|
|
413
|
+
*/
|
|
414
|
+
static newWithBody(body, id, sender) {
|
|
415
|
+
return new SealedRequest(_bcts_envelope.Request.newWithBody(body, id), sender);
|
|
416
|
+
}
|
|
417
|
+
/**
|
|
418
|
+
* Adds a parameter to the request.
|
|
419
|
+
*/
|
|
420
|
+
withParameter(parameter, value) {
|
|
421
|
+
this._request = this._request.withParameter(parameter, value);
|
|
422
|
+
return this;
|
|
423
|
+
}
|
|
424
|
+
/**
|
|
425
|
+
* Adds an optional parameter to the request.
|
|
426
|
+
*/
|
|
427
|
+
withOptionalParameter(parameter, value) {
|
|
428
|
+
if (value !== void 0) this._request = this._request.withParameter(parameter, value);
|
|
429
|
+
return this;
|
|
430
|
+
}
|
|
431
|
+
/**
|
|
432
|
+
* Returns the function of the request.
|
|
433
|
+
*/
|
|
434
|
+
function() {
|
|
435
|
+
return this._request.function();
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
438
|
+
* Returns the expression envelope of the request.
|
|
439
|
+
*/
|
|
440
|
+
expressionEnvelope() {
|
|
441
|
+
return this._request.expressionEnvelope();
|
|
442
|
+
}
|
|
443
|
+
/**
|
|
444
|
+
* Returns the object for a parameter.
|
|
445
|
+
*/
|
|
446
|
+
objectForParameter(param) {
|
|
447
|
+
return this._request.body().getParameter(param);
|
|
448
|
+
}
|
|
449
|
+
/**
|
|
450
|
+
* Returns all objects for a parameter.
|
|
451
|
+
*/
|
|
452
|
+
objectsForParameter(param) {
|
|
453
|
+
const obj = this._request.body().getParameter(param);
|
|
454
|
+
return obj !== void 0 ? [obj] : [];
|
|
455
|
+
}
|
|
456
|
+
/**
|
|
457
|
+
* Extracts an object for a parameter as a specific type.
|
|
458
|
+
*/
|
|
459
|
+
extractObjectForParameter(param) {
|
|
460
|
+
const envelope = this.objectForParameter(param);
|
|
461
|
+
if (envelope === void 0) throw GstpError.envelope(/* @__PURE__ */ new Error(`Parameter not found: ${param}`));
|
|
462
|
+
return envelope.extractSubject((cbor) => {
|
|
463
|
+
if (cbor.isInteger()) return cbor.toInteger();
|
|
464
|
+
if (cbor.isText()) return cbor.toText();
|
|
465
|
+
if (cbor.isBool()) return cbor.toBool();
|
|
466
|
+
if (cbor.isNumber()) return cbor.toNumber();
|
|
467
|
+
if (cbor.isByteString()) return cbor.toByteString();
|
|
468
|
+
return cbor;
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
/**
|
|
472
|
+
* Extracts an optional object for a parameter.
|
|
473
|
+
*/
|
|
474
|
+
extractOptionalObjectForParameter(param) {
|
|
475
|
+
const envelope = this.objectForParameter(param);
|
|
476
|
+
if (envelope === void 0) return;
|
|
477
|
+
return envelope.extractSubject((cbor) => {
|
|
478
|
+
if (cbor.isInteger()) return cbor.toInteger();
|
|
479
|
+
if (cbor.isText()) return cbor.toText();
|
|
480
|
+
if (cbor.isBool()) return cbor.toBool();
|
|
481
|
+
if (cbor.isNumber()) return cbor.toNumber();
|
|
482
|
+
if (cbor.isByteString()) return cbor.toByteString();
|
|
483
|
+
return cbor;
|
|
484
|
+
});
|
|
485
|
+
}
|
|
486
|
+
/**
|
|
487
|
+
* Extracts all objects for a parameter as a specific type.
|
|
488
|
+
*/
|
|
489
|
+
extractObjectsForParameter(param) {
|
|
490
|
+
return this.objectsForParameter(param).map((env) => env.extractSubject((cbor) => cbor));
|
|
491
|
+
}
|
|
492
|
+
/**
|
|
493
|
+
* Adds a note to the request.
|
|
494
|
+
*/
|
|
495
|
+
withNote(note) {
|
|
496
|
+
this._request = this._request.withNote(note);
|
|
497
|
+
return this;
|
|
498
|
+
}
|
|
499
|
+
/**
|
|
500
|
+
* Adds a date to the request.
|
|
501
|
+
*/
|
|
502
|
+
withDate(date) {
|
|
503
|
+
this._request = this._request.withDate(date);
|
|
504
|
+
return this;
|
|
505
|
+
}
|
|
506
|
+
/**
|
|
507
|
+
* Returns the body of the request.
|
|
508
|
+
*/
|
|
509
|
+
body() {
|
|
510
|
+
return this._request.body();
|
|
511
|
+
}
|
|
512
|
+
/**
|
|
513
|
+
* Returns the ID of the request.
|
|
514
|
+
*/
|
|
515
|
+
id() {
|
|
516
|
+
return this._request.id();
|
|
517
|
+
}
|
|
518
|
+
/**
|
|
519
|
+
* Returns the note of the request.
|
|
520
|
+
*/
|
|
521
|
+
note() {
|
|
522
|
+
return this._request.note();
|
|
523
|
+
}
|
|
524
|
+
/**
|
|
525
|
+
* Returns the date of the request.
|
|
526
|
+
*/
|
|
527
|
+
date() {
|
|
528
|
+
return this._request.date();
|
|
529
|
+
}
|
|
530
|
+
/**
|
|
531
|
+
* Adds state to the request that the receiver must return in the response.
|
|
532
|
+
*/
|
|
533
|
+
withState(state) {
|
|
534
|
+
this._state = _bcts_envelope.Envelope.new(state);
|
|
535
|
+
return this;
|
|
536
|
+
}
|
|
537
|
+
/**
|
|
538
|
+
* Adds optional state to the request.
|
|
539
|
+
*/
|
|
540
|
+
withOptionalState(state) {
|
|
541
|
+
this._state = state !== void 0 ? _bcts_envelope.Envelope.new(state) : void 0;
|
|
542
|
+
return this;
|
|
543
|
+
}
|
|
544
|
+
/**
|
|
545
|
+
* Adds a continuation previously received from the recipient.
|
|
546
|
+
*/
|
|
547
|
+
withPeerContinuation(peerContinuation) {
|
|
548
|
+
this._peerContinuation = peerContinuation;
|
|
549
|
+
return this;
|
|
550
|
+
}
|
|
551
|
+
/**
|
|
552
|
+
* Adds an optional continuation previously received from the recipient.
|
|
553
|
+
*/
|
|
554
|
+
withOptionalPeerContinuation(peerContinuation) {
|
|
555
|
+
this._peerContinuation = peerContinuation;
|
|
556
|
+
return this;
|
|
557
|
+
}
|
|
558
|
+
/**
|
|
559
|
+
* Returns the underlying request.
|
|
560
|
+
*/
|
|
561
|
+
request() {
|
|
562
|
+
return this._request;
|
|
563
|
+
}
|
|
564
|
+
/**
|
|
565
|
+
* Returns the sender of the request.
|
|
566
|
+
*/
|
|
567
|
+
sender() {
|
|
568
|
+
return this._sender;
|
|
569
|
+
}
|
|
570
|
+
/**
|
|
571
|
+
* Returns the state to be sent to the recipient.
|
|
572
|
+
*/
|
|
573
|
+
state() {
|
|
574
|
+
return this._state;
|
|
575
|
+
}
|
|
576
|
+
/**
|
|
577
|
+
* Returns the continuation received from the recipient.
|
|
578
|
+
*/
|
|
579
|
+
peerContinuation() {
|
|
580
|
+
return this._peerContinuation;
|
|
581
|
+
}
|
|
582
|
+
/**
|
|
583
|
+
* Converts the sealed request to a Request.
|
|
584
|
+
*/
|
|
585
|
+
toRequest() {
|
|
586
|
+
return this._request;
|
|
587
|
+
}
|
|
588
|
+
/**
|
|
589
|
+
* Converts the sealed request to an Expression.
|
|
590
|
+
*/
|
|
591
|
+
toExpression() {
|
|
592
|
+
return this._request.body();
|
|
593
|
+
}
|
|
594
|
+
/**
|
|
595
|
+
* Creates an envelope that can be decrypted by zero or one recipient.
|
|
596
|
+
*
|
|
597
|
+
* @param validUntil - Optional expiration date for the continuation
|
|
598
|
+
* @param signer - Optional signer for the envelope
|
|
599
|
+
* @param recipient - Optional recipient XID document for encryption
|
|
600
|
+
* @returns The sealed request as an envelope
|
|
601
|
+
*/
|
|
602
|
+
toEnvelope(validUntil, signer, recipient) {
|
|
603
|
+
const recipients = recipient !== void 0 ? [recipient] : [];
|
|
604
|
+
return this.toEnvelopeForRecipients(validUntil, signer, recipients);
|
|
605
|
+
}
|
|
606
|
+
/**
|
|
607
|
+
* Creates an envelope that can be decrypted by zero or more recipients.
|
|
608
|
+
*
|
|
609
|
+
* @param validUntil - Optional expiration date for the continuation
|
|
610
|
+
* @param signer - Optional signer for the envelope
|
|
611
|
+
* @param recipients - Array of recipient XID documents for encryption
|
|
612
|
+
* @returns The sealed request as an envelope
|
|
613
|
+
*/
|
|
614
|
+
toEnvelopeForRecipients(validUntil, signer, recipients) {
|
|
615
|
+
const continuation = new Continuation(this._state ?? _bcts_envelope.Envelope.new(null), this.id(), validUntil);
|
|
616
|
+
const senderEncryptionKey = this._sender.inceptionKey()?.publicKeys()?.encapsulationPublicKey();
|
|
617
|
+
if (senderEncryptionKey === void 0) throw GstpError.senderMissingEncryptionKey();
|
|
618
|
+
const senderContinuation = continuation.toEnvelope(senderEncryptionKey);
|
|
619
|
+
let result = this._request.toEnvelope();
|
|
620
|
+
result = result.addAssertion(_bcts_known_values.SENDER, this._sender.toEnvelope());
|
|
621
|
+
result = result.addAssertion(_bcts_known_values.SENDER_CONTINUATION, senderContinuation);
|
|
622
|
+
if (this._peerContinuation !== void 0) result = result.addAssertion(_bcts_known_values.RECIPIENT_CONTINUATION, this._peerContinuation);
|
|
623
|
+
if (signer !== void 0) result = result.sign(signer);
|
|
624
|
+
if (recipients !== void 0 && recipients.length > 0) {
|
|
625
|
+
const recipientKeys = recipients.map((recipient) => {
|
|
626
|
+
const key = recipient.encryptionKey();
|
|
627
|
+
if (key === void 0) throw GstpError.recipientMissingEncryptionKey();
|
|
628
|
+
return key;
|
|
629
|
+
});
|
|
630
|
+
result = result.wrap().encryptSubjectToRecipients(recipientKeys);
|
|
631
|
+
}
|
|
632
|
+
return result;
|
|
633
|
+
}
|
|
634
|
+
/**
|
|
635
|
+
* Parses a sealed request from an encrypted envelope.
|
|
636
|
+
*
|
|
637
|
+
* @param encryptedEnvelope - The encrypted envelope to parse
|
|
638
|
+
* @param expectedId - Optional expected request ID for validation
|
|
639
|
+
* @param now - Optional current time for continuation validation
|
|
640
|
+
* @param recipient - The recipient's private keys for decryption
|
|
641
|
+
* @returns The parsed sealed request
|
|
642
|
+
*/
|
|
643
|
+
static tryFromEnvelope(encryptedEnvelope, expectedId, now, recipient) {
|
|
644
|
+
let signedEnvelope;
|
|
645
|
+
try {
|
|
646
|
+
signedEnvelope = encryptedEnvelope.decryptToRecipient(recipient);
|
|
647
|
+
} catch (e) {
|
|
648
|
+
throw GstpError.envelope(e instanceof Error ? e : new Error(String(e)));
|
|
649
|
+
}
|
|
650
|
+
let sender;
|
|
651
|
+
try {
|
|
652
|
+
const senderEnvelope = signedEnvelope.tryUnwrap().objectForPredicate(_bcts_known_values.SENDER);
|
|
653
|
+
if (senderEnvelope === void 0) throw new Error("Missing sender");
|
|
654
|
+
sender = _bcts_xid.XIDDocument.fromEnvelope(senderEnvelope);
|
|
655
|
+
} catch (e) {
|
|
656
|
+
throw GstpError.xid(e instanceof Error ? e : new Error(String(e)));
|
|
657
|
+
}
|
|
658
|
+
const senderVerificationKey = sender.inceptionKey()?.publicKeys()?.signingPublicKey();
|
|
659
|
+
if (senderVerificationKey === void 0) throw GstpError.senderMissingVerificationKey();
|
|
660
|
+
let requestEnvelope;
|
|
661
|
+
try {
|
|
662
|
+
requestEnvelope = signedEnvelope.verify(senderVerificationKey);
|
|
663
|
+
} catch (e) {
|
|
664
|
+
throw GstpError.envelope(e instanceof Error ? e : new Error(String(e)));
|
|
665
|
+
}
|
|
666
|
+
const peerContinuation = requestEnvelope.optionalObjectForPredicate(_bcts_known_values.SENDER_CONTINUATION);
|
|
667
|
+
if (peerContinuation !== void 0) {
|
|
668
|
+
if (!peerContinuation.subject().isEncrypted()) throw GstpError.peerContinuationNotEncrypted();
|
|
669
|
+
} else throw GstpError.missingPeerContinuation();
|
|
670
|
+
const encryptedContinuation = requestEnvelope.optionalObjectForPredicate(_bcts_known_values.RECIPIENT_CONTINUATION);
|
|
671
|
+
let state;
|
|
672
|
+
if (encryptedContinuation !== void 0) state = Continuation.tryFromEnvelope(encryptedContinuation, expectedId, now, recipient).state();
|
|
673
|
+
let request;
|
|
674
|
+
try {
|
|
675
|
+
request = _bcts_envelope.Request.fromEnvelope(requestEnvelope);
|
|
676
|
+
} catch (e) {
|
|
677
|
+
throw GstpError.envelope(e instanceof Error ? e : new Error(String(e)));
|
|
678
|
+
}
|
|
679
|
+
return new SealedRequest(request, sender, state, peerContinuation);
|
|
680
|
+
}
|
|
681
|
+
/**
|
|
682
|
+
* Returns a string representation of the sealed request.
|
|
683
|
+
*/
|
|
684
|
+
toString() {
|
|
685
|
+
const stateStr = this._state !== void 0 ? this._state.formatFlat() : "None";
|
|
686
|
+
const peerStr = this._peerContinuation !== void 0 ? "Some" : "None";
|
|
687
|
+
return `SealedRequest(${this._request.summary()}, state: ${stateStr}, peer_continuation: ${peerStr})`;
|
|
688
|
+
}
|
|
689
|
+
/**
|
|
690
|
+
* Checks equality with another sealed request.
|
|
691
|
+
*/
|
|
692
|
+
equals(other) {
|
|
693
|
+
if (!this._request.equals(other._request)) return false;
|
|
694
|
+
if (!this._sender.xid().equals(other._sender.xid())) return false;
|
|
695
|
+
if (this._state === void 0 && other._state === void 0) {} else if (this._state !== void 0 && other._state !== void 0) {
|
|
696
|
+
if (!this._state.digest().equals(other._state.digest())) return false;
|
|
697
|
+
} else return false;
|
|
698
|
+
if (this._peerContinuation === void 0 && other._peerContinuation === void 0) {} else if (this._peerContinuation !== void 0 && other._peerContinuation !== void 0) {
|
|
699
|
+
if (!this._peerContinuation.digest().equals(other._peerContinuation.digest())) return false;
|
|
700
|
+
} else return false;
|
|
701
|
+
return true;
|
|
702
|
+
}
|
|
703
|
+
};
|
|
704
|
+
|
|
705
|
+
//#endregion
|
|
706
|
+
//#region src/sealed-response.ts
|
|
707
|
+
/**
|
|
708
|
+
* A sealed response that combines a Response with sender information and
|
|
709
|
+
* state continuations for secure communication.
|
|
710
|
+
*
|
|
711
|
+
* @example
|
|
712
|
+
* ```typescript
|
|
713
|
+
* import { SealedResponse, ARID } from '@bcts/gstp';
|
|
714
|
+
* import { XIDDocument } from '@bcts/xid';
|
|
715
|
+
*
|
|
716
|
+
* // Create sender XID document
|
|
717
|
+
* const sender = XIDDocument.new();
|
|
718
|
+
* const requestId = ARID.new();
|
|
719
|
+
*
|
|
720
|
+
* // Create a successful sealed response
|
|
721
|
+
* const response = SealedResponse.newSuccess(requestId, sender)
|
|
722
|
+
* .withResult("Operation completed")
|
|
723
|
+
* .withState("next-page-state");
|
|
724
|
+
*
|
|
725
|
+
* // Convert to sealed envelope
|
|
726
|
+
* const envelope = response.toEnvelope(
|
|
727
|
+
* new Date(Date.now() + 60000), // Valid for 60 seconds
|
|
728
|
+
* senderPrivateKey,
|
|
729
|
+
* recipientXIDDocument
|
|
730
|
+
* );
|
|
731
|
+
* ```
|
|
732
|
+
*/
|
|
733
|
+
var SealedResponse = class SealedResponse {
|
|
734
|
+
_response;
|
|
735
|
+
_sender;
|
|
736
|
+
_state;
|
|
737
|
+
_peerContinuation;
|
|
738
|
+
constructor(response, sender, state, peerContinuation) {
|
|
739
|
+
this._response = response;
|
|
740
|
+
this._sender = sender;
|
|
741
|
+
this._state = state;
|
|
742
|
+
this._peerContinuation = peerContinuation;
|
|
743
|
+
}
|
|
744
|
+
/**
|
|
745
|
+
* Creates a new successful sealed response.
|
|
746
|
+
*
|
|
747
|
+
* @param id - The request ID this response is for
|
|
748
|
+
* @param sender - The sender's XID document
|
|
749
|
+
*/
|
|
750
|
+
static newSuccess(id, sender) {
|
|
751
|
+
return new SealedResponse(_bcts_envelope.Response.newSuccess(id), sender);
|
|
752
|
+
}
|
|
753
|
+
/**
|
|
754
|
+
* Creates a new failure sealed response.
|
|
755
|
+
*
|
|
756
|
+
* @param id - The request ID this response is for
|
|
757
|
+
* @param sender - The sender's XID document
|
|
758
|
+
*/
|
|
759
|
+
static newFailure(id, sender) {
|
|
760
|
+
return new SealedResponse(_bcts_envelope.Response.newFailure(id), sender);
|
|
761
|
+
}
|
|
762
|
+
/**
|
|
763
|
+
* Creates a new early failure sealed response.
|
|
764
|
+
*
|
|
765
|
+
* An early failure takes place before the message has been decrypted,
|
|
766
|
+
* and therefore the ID and sender public key are not known.
|
|
767
|
+
*
|
|
768
|
+
* @param sender - The sender's XID document
|
|
769
|
+
*/
|
|
770
|
+
static newEarlyFailure(sender) {
|
|
771
|
+
return new SealedResponse(_bcts_envelope.Response.newEarlyFailure(), sender);
|
|
772
|
+
}
|
|
773
|
+
/**
|
|
774
|
+
* Adds state to the response that the peer may return at some future time.
|
|
775
|
+
*
|
|
776
|
+
* @throws Error if called on a failed response
|
|
777
|
+
*/
|
|
778
|
+
withState(state) {
|
|
779
|
+
if (!this._response.isOk()) throw new Error("Cannot set state on a failed response");
|
|
780
|
+
this._state = _bcts_envelope.Envelope.new(state);
|
|
781
|
+
return this;
|
|
782
|
+
}
|
|
783
|
+
/**
|
|
784
|
+
* Adds optional state to the response.
|
|
785
|
+
*/
|
|
786
|
+
withOptionalState(state) {
|
|
787
|
+
if (state !== void 0) return this.withState(state);
|
|
788
|
+
this._state = void 0;
|
|
789
|
+
return this;
|
|
790
|
+
}
|
|
791
|
+
/**
|
|
792
|
+
* Adds a continuation previously received from the recipient.
|
|
793
|
+
*/
|
|
794
|
+
withPeerContinuation(peerContinuation) {
|
|
795
|
+
this._peerContinuation = peerContinuation;
|
|
796
|
+
return this;
|
|
797
|
+
}
|
|
798
|
+
/**
|
|
799
|
+
* Returns the sender of the response.
|
|
800
|
+
*/
|
|
801
|
+
sender() {
|
|
802
|
+
return this._sender;
|
|
803
|
+
}
|
|
804
|
+
/**
|
|
805
|
+
* Returns the state to be sent to the peer.
|
|
806
|
+
*/
|
|
807
|
+
state() {
|
|
808
|
+
return this._state;
|
|
809
|
+
}
|
|
810
|
+
/**
|
|
811
|
+
* Returns the continuation received from the peer.
|
|
812
|
+
*/
|
|
813
|
+
peerContinuation() {
|
|
814
|
+
return this._peerContinuation;
|
|
815
|
+
}
|
|
816
|
+
/**
|
|
817
|
+
* Sets the result value for a successful response.
|
|
818
|
+
*/
|
|
819
|
+
withResult(result) {
|
|
820
|
+
this._response = this._response.withResult(result);
|
|
821
|
+
return this;
|
|
822
|
+
}
|
|
823
|
+
/**
|
|
824
|
+
* Sets an optional result value for a successful response.
|
|
825
|
+
* If the result is undefined, the value of the response will be the null envelope.
|
|
826
|
+
*/
|
|
827
|
+
withOptionalResult(result) {
|
|
828
|
+
this._response = this._response.withOptionalResult(result);
|
|
829
|
+
return this;
|
|
830
|
+
}
|
|
831
|
+
/**
|
|
832
|
+
* Sets the error value for a failure response.
|
|
833
|
+
*/
|
|
834
|
+
withError(error) {
|
|
835
|
+
this._response = this._response.withError(error);
|
|
836
|
+
return this;
|
|
837
|
+
}
|
|
838
|
+
/**
|
|
839
|
+
* Sets an optional error value for a failure response.
|
|
840
|
+
* If the error is undefined, the value of the response will be the unknown value.
|
|
841
|
+
*/
|
|
842
|
+
withOptionalError(error) {
|
|
843
|
+
this._response = this._response.withOptionalError(error);
|
|
844
|
+
return this;
|
|
845
|
+
}
|
|
846
|
+
/**
|
|
847
|
+
* Returns true if this is a successful response.
|
|
848
|
+
*/
|
|
849
|
+
isOk() {
|
|
850
|
+
return this._response.isOk();
|
|
851
|
+
}
|
|
852
|
+
/**
|
|
853
|
+
* Returns true if this is a failure response.
|
|
854
|
+
*/
|
|
855
|
+
isErr() {
|
|
856
|
+
return this._response.isErr();
|
|
857
|
+
}
|
|
858
|
+
/**
|
|
859
|
+
* Returns the ID of the request this response is for, if known.
|
|
860
|
+
*/
|
|
861
|
+
id() {
|
|
862
|
+
return this._response.id();
|
|
863
|
+
}
|
|
864
|
+
/**
|
|
865
|
+
* Returns the ID of the request this response is for.
|
|
866
|
+
* @throws Error if the ID is not known
|
|
867
|
+
*/
|
|
868
|
+
expectId() {
|
|
869
|
+
return this._response.expectId();
|
|
870
|
+
}
|
|
871
|
+
/**
|
|
872
|
+
* Returns the result envelope if this is a successful response.
|
|
873
|
+
* @throws Error if this is a failure response
|
|
874
|
+
*/
|
|
875
|
+
result() {
|
|
876
|
+
return this._response.result();
|
|
877
|
+
}
|
|
878
|
+
/**
|
|
879
|
+
* Extracts the result as a specific type.
|
|
880
|
+
*/
|
|
881
|
+
extractResult(decoder) {
|
|
882
|
+
return this._response.extractResult(decoder);
|
|
883
|
+
}
|
|
884
|
+
/**
|
|
885
|
+
* Returns the error envelope if this is a failure response.
|
|
886
|
+
* @throws Error if this is a successful response
|
|
887
|
+
*/
|
|
888
|
+
error() {
|
|
889
|
+
return this._response.error();
|
|
890
|
+
}
|
|
891
|
+
/**
|
|
892
|
+
* Extracts the error as a specific type.
|
|
893
|
+
*/
|
|
894
|
+
extractError(decoder) {
|
|
895
|
+
return this._response.extractError(decoder);
|
|
896
|
+
}
|
|
897
|
+
/**
|
|
898
|
+
* Creates an envelope that can be decrypted by zero or one recipient.
|
|
899
|
+
*
|
|
900
|
+
* @param validUntil - Optional expiration date for the continuation
|
|
901
|
+
* @param signer - Optional signer for the envelope
|
|
902
|
+
* @param recipient - Optional recipient XID document for encryption
|
|
903
|
+
* @returns The sealed response as an envelope
|
|
904
|
+
*/
|
|
905
|
+
toEnvelope(validUntil, signer, recipient) {
|
|
906
|
+
const recipients = recipient !== void 0 ? [recipient] : [];
|
|
907
|
+
return this.toEnvelopeForRecipients(validUntil, signer, recipients);
|
|
908
|
+
}
|
|
909
|
+
/**
|
|
910
|
+
* Creates an envelope that can be decrypted by zero or more recipients.
|
|
911
|
+
*
|
|
912
|
+
* @param validUntil - Optional expiration date for the continuation
|
|
913
|
+
* @param signer - Optional signer for the envelope
|
|
914
|
+
* @param recipients - Array of recipient XID documents for encryption
|
|
915
|
+
* @returns The sealed response as an envelope
|
|
916
|
+
*/
|
|
917
|
+
toEnvelopeForRecipients(validUntil, signer, recipients) {
|
|
918
|
+
let senderContinuation;
|
|
919
|
+
if (this._state !== void 0) {
|
|
920
|
+
const continuation = new Continuation(this._state, void 0, validUntil);
|
|
921
|
+
const senderEncryptionKey = this._sender.inceptionKey()?.publicKeys()?.encapsulationPublicKey();
|
|
922
|
+
if (senderEncryptionKey === void 0) throw GstpError.senderMissingEncryptionKey();
|
|
923
|
+
senderContinuation = continuation.toEnvelope(senderEncryptionKey);
|
|
924
|
+
}
|
|
925
|
+
let result = this._response.toEnvelope();
|
|
926
|
+
result = result.addAssertion(_bcts_known_values.SENDER, this._sender.toEnvelope());
|
|
927
|
+
if (senderContinuation !== void 0) result = result.addAssertion(_bcts_known_values.SENDER_CONTINUATION, senderContinuation);
|
|
928
|
+
if (this._peerContinuation !== void 0) result = result.addAssertion(_bcts_known_values.RECIPIENT_CONTINUATION, this._peerContinuation);
|
|
929
|
+
if (signer !== void 0) result = result.sign(signer);
|
|
930
|
+
if (recipients !== void 0 && recipients.length > 0) {
|
|
931
|
+
const recipientKeys = recipients.map((recipient) => {
|
|
932
|
+
const key = recipient.encryptionKey();
|
|
933
|
+
if (key === void 0) throw GstpError.recipientMissingEncryptionKey();
|
|
934
|
+
return key;
|
|
935
|
+
});
|
|
936
|
+
result = result.wrap().encryptSubjectToRecipients(recipientKeys);
|
|
937
|
+
}
|
|
938
|
+
return result;
|
|
939
|
+
}
|
|
940
|
+
/**
|
|
941
|
+
* Parses a sealed response from an encrypted envelope.
|
|
942
|
+
*
|
|
943
|
+
* @param encryptedEnvelope - The encrypted envelope to parse
|
|
944
|
+
* @param expectedId - Optional expected request ID for validation
|
|
945
|
+
* @param now - Optional current time for continuation validation
|
|
946
|
+
* @param recipientPrivateKey - The recipient's private keys for decryption
|
|
947
|
+
* @returns The parsed sealed response
|
|
948
|
+
*/
|
|
949
|
+
static tryFromEncryptedEnvelope(encryptedEnvelope, expectedId, now, recipientPrivateKey) {
|
|
950
|
+
let signedEnvelope;
|
|
951
|
+
try {
|
|
952
|
+
signedEnvelope = encryptedEnvelope.decryptToRecipient(recipientPrivateKey);
|
|
953
|
+
} catch (e) {
|
|
954
|
+
throw GstpError.envelope(e instanceof Error ? e : new Error(String(e)));
|
|
955
|
+
}
|
|
956
|
+
let sender;
|
|
957
|
+
try {
|
|
958
|
+
const senderEnvelope = signedEnvelope.tryUnwrap().objectForPredicate(_bcts_known_values.SENDER);
|
|
959
|
+
if (senderEnvelope === void 0) throw new Error("Missing sender");
|
|
960
|
+
sender = _bcts_xid.XIDDocument.fromEnvelope(senderEnvelope);
|
|
961
|
+
} catch (e) {
|
|
962
|
+
throw GstpError.xid(e instanceof Error ? e : new Error(String(e)));
|
|
963
|
+
}
|
|
964
|
+
const senderVerificationKey = sender.inceptionKey()?.publicKeys()?.signingPublicKey();
|
|
965
|
+
if (senderVerificationKey === void 0) throw GstpError.senderMissingVerificationKey();
|
|
966
|
+
let responseEnvelope;
|
|
967
|
+
try {
|
|
968
|
+
responseEnvelope = signedEnvelope.verify(senderVerificationKey);
|
|
969
|
+
} catch (e) {
|
|
970
|
+
throw GstpError.envelope(e instanceof Error ? e : new Error(String(e)));
|
|
971
|
+
}
|
|
972
|
+
const peerContinuation = responseEnvelope.optionalObjectForPredicate(_bcts_known_values.SENDER_CONTINUATION);
|
|
973
|
+
if (peerContinuation !== void 0) {
|
|
974
|
+
if (!peerContinuation.subject().isEncrypted()) throw GstpError.peerContinuationNotEncrypted();
|
|
975
|
+
}
|
|
976
|
+
const encryptedContinuation = responseEnvelope.optionalObjectForPredicate(_bcts_known_values.RECIPIENT_CONTINUATION);
|
|
977
|
+
let state;
|
|
978
|
+
if (encryptedContinuation !== void 0) {
|
|
979
|
+
const stateEnv = Continuation.tryFromEnvelope(encryptedContinuation, expectedId, now, recipientPrivateKey).state();
|
|
980
|
+
if (stateEnv.isNull()) state = void 0;
|
|
981
|
+
else state = stateEnv;
|
|
982
|
+
}
|
|
983
|
+
let response;
|
|
984
|
+
try {
|
|
985
|
+
response = _bcts_envelope.Response.fromEnvelope(responseEnvelope);
|
|
986
|
+
} catch (e) {
|
|
987
|
+
throw GstpError.envelope(e instanceof Error ? e : new Error(String(e)));
|
|
988
|
+
}
|
|
989
|
+
return new SealedResponse(response, sender, state, peerContinuation);
|
|
990
|
+
}
|
|
991
|
+
/**
|
|
992
|
+
* Returns a string representation of the sealed response.
|
|
993
|
+
*/
|
|
994
|
+
toString() {
|
|
995
|
+
const stateStr = this._state !== void 0 ? this._state.formatFlat() : "None";
|
|
996
|
+
const peerStr = this._peerContinuation !== void 0 ? "Some" : "None";
|
|
997
|
+
return `SealedResponse(${this._response.summary()}, state: ${stateStr}, peer_continuation: ${peerStr})`;
|
|
998
|
+
}
|
|
999
|
+
/**
|
|
1000
|
+
* Checks equality with another sealed response.
|
|
1001
|
+
*/
|
|
1002
|
+
equals(other) {
|
|
1003
|
+
if (!this._response.equals(other._response)) return false;
|
|
1004
|
+
if (!this._sender.xid().equals(other._sender.xid())) return false;
|
|
1005
|
+
if (this._state === void 0 && other._state === void 0) {} else if (this._state !== void 0 && other._state !== void 0) {
|
|
1006
|
+
if (!this._state.digest().equals(other._state.digest())) return false;
|
|
1007
|
+
} else return false;
|
|
1008
|
+
if (this._peerContinuation === void 0 && other._peerContinuation === void 0) {} else if (this._peerContinuation !== void 0 && other._peerContinuation !== void 0) {
|
|
1009
|
+
if (!this._peerContinuation.digest().equals(other._peerContinuation.digest())) return false;
|
|
1010
|
+
} else return false;
|
|
1011
|
+
return true;
|
|
1012
|
+
}
|
|
1013
|
+
};
|
|
1014
|
+
|
|
1015
|
+
//#endregion
|
|
1016
|
+
//#region src/sealed-event.ts
|
|
1017
|
+
/**
|
|
1018
|
+
* A sealed event that combines an Event with sender information and
|
|
1019
|
+
* state continuations for secure communication.
|
|
1020
|
+
*
|
|
1021
|
+
* @typeParam T - The type of content this event carries
|
|
1022
|
+
*
|
|
1023
|
+
* @example
|
|
1024
|
+
* ```typescript
|
|
1025
|
+
* import { SealedEvent, ARID } from '@bcts/gstp';
|
|
1026
|
+
* import { XIDDocument } from '@bcts/xid';
|
|
1027
|
+
*
|
|
1028
|
+
* // Create sender XID document
|
|
1029
|
+
* const sender = XIDDocument.new();
|
|
1030
|
+
* const eventId = ARID.new();
|
|
1031
|
+
*
|
|
1032
|
+
* // Create a sealed event
|
|
1033
|
+
* const event = SealedEvent.new("System notification", eventId, sender)
|
|
1034
|
+
* .withNote("Status update")
|
|
1035
|
+
* .withDate(new Date());
|
|
1036
|
+
*
|
|
1037
|
+
* // Convert to sealed envelope
|
|
1038
|
+
* const envelope = event.toEnvelope(
|
|
1039
|
+
* new Date(Date.now() + 60000), // Valid for 60 seconds
|
|
1040
|
+
* senderPrivateKey,
|
|
1041
|
+
* recipientXIDDocument
|
|
1042
|
+
* );
|
|
1043
|
+
* ```
|
|
1044
|
+
*/
|
|
1045
|
+
var SealedEvent = class SealedEvent {
|
|
1046
|
+
_event;
|
|
1047
|
+
_sender;
|
|
1048
|
+
_state;
|
|
1049
|
+
_peerContinuation;
|
|
1050
|
+
constructor(event, sender, state, peerContinuation) {
|
|
1051
|
+
this._event = event;
|
|
1052
|
+
this._sender = sender;
|
|
1053
|
+
this._state = state;
|
|
1054
|
+
this._peerContinuation = peerContinuation;
|
|
1055
|
+
}
|
|
1056
|
+
/**
|
|
1057
|
+
* Creates a new sealed event with the given content, ID, and sender.
|
|
1058
|
+
*
|
|
1059
|
+
* @param content - The content of the event
|
|
1060
|
+
* @param id - The event ID
|
|
1061
|
+
* @param sender - The sender's XID document
|
|
1062
|
+
*/
|
|
1063
|
+
static new(content, id, sender) {
|
|
1064
|
+
return new SealedEvent(_bcts_envelope.Event.new(content, id), sender);
|
|
1065
|
+
}
|
|
1066
|
+
/**
|
|
1067
|
+
* Adds a note to the event.
|
|
1068
|
+
*/
|
|
1069
|
+
withNote(note) {
|
|
1070
|
+
this._event = this._event.withNote(note);
|
|
1071
|
+
return this;
|
|
1072
|
+
}
|
|
1073
|
+
/**
|
|
1074
|
+
* Adds a date to the event.
|
|
1075
|
+
*/
|
|
1076
|
+
withDate(date) {
|
|
1077
|
+
this._event = this._event.withDate(date);
|
|
1078
|
+
return this;
|
|
1079
|
+
}
|
|
1080
|
+
/**
|
|
1081
|
+
* Returns the content of the event.
|
|
1082
|
+
*/
|
|
1083
|
+
content() {
|
|
1084
|
+
return this._event.content();
|
|
1085
|
+
}
|
|
1086
|
+
/**
|
|
1087
|
+
* Returns the ID of the event.
|
|
1088
|
+
*/
|
|
1089
|
+
id() {
|
|
1090
|
+
return this._event.id();
|
|
1091
|
+
}
|
|
1092
|
+
/**
|
|
1093
|
+
* Returns the note of the event.
|
|
1094
|
+
*/
|
|
1095
|
+
note() {
|
|
1096
|
+
return this._event.note();
|
|
1097
|
+
}
|
|
1098
|
+
/**
|
|
1099
|
+
* Returns the date of the event.
|
|
1100
|
+
*/
|
|
1101
|
+
date() {
|
|
1102
|
+
return this._event.date();
|
|
1103
|
+
}
|
|
1104
|
+
/**
|
|
1105
|
+
* Adds state to the event that the receiver must return in the response.
|
|
1106
|
+
*/
|
|
1107
|
+
withState(state) {
|
|
1108
|
+
this._state = _bcts_envelope.Envelope.new(state);
|
|
1109
|
+
return this;
|
|
1110
|
+
}
|
|
1111
|
+
/**
|
|
1112
|
+
* Adds optional state to the event.
|
|
1113
|
+
*/
|
|
1114
|
+
withOptionalState(state) {
|
|
1115
|
+
if (state !== void 0) return this.withState(state);
|
|
1116
|
+
this._state = void 0;
|
|
1117
|
+
return this;
|
|
1118
|
+
}
|
|
1119
|
+
/**
|
|
1120
|
+
* Adds a continuation previously received from the recipient.
|
|
1121
|
+
*/
|
|
1122
|
+
withPeerContinuation(peerContinuation) {
|
|
1123
|
+
this._peerContinuation = peerContinuation;
|
|
1124
|
+
return this;
|
|
1125
|
+
}
|
|
1126
|
+
/**
|
|
1127
|
+
* Adds an optional continuation previously received from the recipient.
|
|
1128
|
+
*/
|
|
1129
|
+
withOptionalPeerContinuation(peerContinuation) {
|
|
1130
|
+
this._peerContinuation = peerContinuation;
|
|
1131
|
+
return this;
|
|
1132
|
+
}
|
|
1133
|
+
/**
|
|
1134
|
+
* Returns the underlying event.
|
|
1135
|
+
*/
|
|
1136
|
+
event() {
|
|
1137
|
+
return this._event;
|
|
1138
|
+
}
|
|
1139
|
+
/**
|
|
1140
|
+
* Returns the sender of the event.
|
|
1141
|
+
*/
|
|
1142
|
+
sender() {
|
|
1143
|
+
return this._sender;
|
|
1144
|
+
}
|
|
1145
|
+
/**
|
|
1146
|
+
* Returns the state to be sent to the recipient.
|
|
1147
|
+
*/
|
|
1148
|
+
state() {
|
|
1149
|
+
return this._state;
|
|
1150
|
+
}
|
|
1151
|
+
/**
|
|
1152
|
+
* Returns the continuation received from the recipient.
|
|
1153
|
+
*/
|
|
1154
|
+
peerContinuation() {
|
|
1155
|
+
return this._peerContinuation;
|
|
1156
|
+
}
|
|
1157
|
+
/**
|
|
1158
|
+
* Converts the sealed event to an Event.
|
|
1159
|
+
*/
|
|
1160
|
+
toEvent() {
|
|
1161
|
+
return this._event;
|
|
1162
|
+
}
|
|
1163
|
+
/**
|
|
1164
|
+
* Creates an envelope that can be decrypted by zero or one recipient.
|
|
1165
|
+
*
|
|
1166
|
+
* @param validUntil - Optional expiration date for the continuation
|
|
1167
|
+
* @param signer - Optional signer for the envelope
|
|
1168
|
+
* @param recipient - Optional recipient XID document for encryption
|
|
1169
|
+
* @returns The sealed event as an envelope
|
|
1170
|
+
*/
|
|
1171
|
+
toEnvelope(validUntil, signer, recipient) {
|
|
1172
|
+
const recipients = recipient !== void 0 ? [recipient] : [];
|
|
1173
|
+
return this.toEnvelopeForRecipients(validUntil, signer, recipients);
|
|
1174
|
+
}
|
|
1175
|
+
/**
|
|
1176
|
+
* Creates an envelope that can be decrypted by zero or more recipients.
|
|
1177
|
+
*
|
|
1178
|
+
* @param validUntil - Optional expiration date for the continuation
|
|
1179
|
+
* @param signer - Optional signer for the envelope
|
|
1180
|
+
* @param recipients - Array of recipient XID documents for encryption
|
|
1181
|
+
* @returns The sealed event as an envelope
|
|
1182
|
+
*/
|
|
1183
|
+
toEnvelopeForRecipients(validUntil, signer, recipients) {
|
|
1184
|
+
const senderEncryptionKey = this._sender.inceptionKey()?.publicKeys()?.encapsulationPublicKey();
|
|
1185
|
+
if (senderEncryptionKey === void 0) throw GstpError.senderMissingEncryptionKey();
|
|
1186
|
+
let senderContinuation;
|
|
1187
|
+
if (this._state !== void 0) senderContinuation = new Continuation(this._state, void 0, validUntil).toEnvelope(senderEncryptionKey);
|
|
1188
|
+
else if (validUntil !== void 0) senderContinuation = new Continuation(_bcts_envelope.Envelope.new(null), void 0, validUntil).toEnvelope(senderEncryptionKey);
|
|
1189
|
+
let result = this._event.toEnvelope();
|
|
1190
|
+
result = result.addAssertion(_bcts_known_values.SENDER, this._sender.toEnvelope());
|
|
1191
|
+
if (senderContinuation !== void 0) result = result.addAssertion(_bcts_known_values.SENDER_CONTINUATION, senderContinuation);
|
|
1192
|
+
if (this._peerContinuation !== void 0) result = result.addAssertion(_bcts_known_values.RECIPIENT_CONTINUATION, this._peerContinuation);
|
|
1193
|
+
if (signer !== void 0) result = result.sign(signer);
|
|
1194
|
+
if (recipients !== void 0 && recipients.length > 0) {
|
|
1195
|
+
const recipientKeys = recipients.map((recipient) => {
|
|
1196
|
+
const key = recipient.encryptionKey();
|
|
1197
|
+
if (key === void 0) throw GstpError.recipientMissingEncryptionKey();
|
|
1198
|
+
return key;
|
|
1199
|
+
});
|
|
1200
|
+
result = result.wrap().encryptSubjectToRecipients(recipientKeys);
|
|
1201
|
+
}
|
|
1202
|
+
return result;
|
|
1203
|
+
}
|
|
1204
|
+
/**
|
|
1205
|
+
* Parses a sealed event from an encrypted envelope.
|
|
1206
|
+
*
|
|
1207
|
+
* @param encryptedEnvelope - The encrypted envelope to parse
|
|
1208
|
+
* @param expectedId - Optional expected event ID for validation
|
|
1209
|
+
* @param now - Optional current time for continuation validation
|
|
1210
|
+
* @param recipientPrivateKey - The recipient's private keys for decryption
|
|
1211
|
+
* @param contentExtractor - Function to extract content from envelope
|
|
1212
|
+
* @returns The parsed sealed event
|
|
1213
|
+
*/
|
|
1214
|
+
static tryFromEnvelope(encryptedEnvelope, expectedId, now, recipientPrivateKey, contentExtractor) {
|
|
1215
|
+
let signedEnvelope;
|
|
1216
|
+
try {
|
|
1217
|
+
signedEnvelope = encryptedEnvelope.decryptToRecipient(recipientPrivateKey);
|
|
1218
|
+
} catch (e) {
|
|
1219
|
+
throw GstpError.envelope(e instanceof Error ? e : new Error(String(e)));
|
|
1220
|
+
}
|
|
1221
|
+
let sender;
|
|
1222
|
+
try {
|
|
1223
|
+
const senderEnvelope = signedEnvelope.tryUnwrap().objectForPredicate(_bcts_known_values.SENDER);
|
|
1224
|
+
if (senderEnvelope === void 0) throw new Error("Missing sender");
|
|
1225
|
+
sender = _bcts_xid.XIDDocument.fromEnvelope(senderEnvelope);
|
|
1226
|
+
} catch (e) {
|
|
1227
|
+
throw GstpError.xid(e instanceof Error ? e : new Error(String(e)));
|
|
1228
|
+
}
|
|
1229
|
+
const senderVerificationKey = sender.inceptionKey()?.publicKeys()?.signingPublicKey();
|
|
1230
|
+
if (senderVerificationKey === void 0) throw GstpError.senderMissingVerificationKey();
|
|
1231
|
+
let eventEnvelope;
|
|
1232
|
+
try {
|
|
1233
|
+
eventEnvelope = signedEnvelope.verify(senderVerificationKey);
|
|
1234
|
+
} catch (e) {
|
|
1235
|
+
throw GstpError.envelope(e instanceof Error ? e : new Error(String(e)));
|
|
1236
|
+
}
|
|
1237
|
+
const peerContinuation = eventEnvelope.optionalObjectForPredicate(_bcts_known_values.SENDER_CONTINUATION);
|
|
1238
|
+
if (peerContinuation !== void 0) {
|
|
1239
|
+
if (!peerContinuation.subject().isEncrypted()) throw GstpError.peerContinuationNotEncrypted();
|
|
1240
|
+
}
|
|
1241
|
+
const encryptedContinuation = eventEnvelope.optionalObjectForPredicate(_bcts_known_values.RECIPIENT_CONTINUATION);
|
|
1242
|
+
let state;
|
|
1243
|
+
if (encryptedContinuation !== void 0) state = Continuation.tryFromEnvelope(encryptedContinuation, expectedId, now, recipientPrivateKey).state();
|
|
1244
|
+
let event;
|
|
1245
|
+
try {
|
|
1246
|
+
if (contentExtractor !== void 0) event = _bcts_envelope.Event.fromEnvelope(eventEnvelope, contentExtractor);
|
|
1247
|
+
else event = _bcts_envelope.Event.stringFromEnvelope(eventEnvelope);
|
|
1248
|
+
} catch (e) {
|
|
1249
|
+
throw GstpError.envelope(e instanceof Error ? e : new Error(String(e)));
|
|
1250
|
+
}
|
|
1251
|
+
return new SealedEvent(event, sender, state, peerContinuation);
|
|
1252
|
+
}
|
|
1253
|
+
/**
|
|
1254
|
+
* Returns a string representation of the sealed event.
|
|
1255
|
+
*/
|
|
1256
|
+
toString() {
|
|
1257
|
+
const stateStr = this._state !== void 0 ? this._state.formatFlat() : "None";
|
|
1258
|
+
const peerStr = this._peerContinuation !== void 0 ? "Some" : "None";
|
|
1259
|
+
return `SealedEvent(${this._event.summary()}, state: ${stateStr}, peer_continuation: ${peerStr})`;
|
|
1260
|
+
}
|
|
1261
|
+
/**
|
|
1262
|
+
* Checks equality with another sealed event.
|
|
1263
|
+
*/
|
|
1264
|
+
equals(other) {
|
|
1265
|
+
if (!this._event.equals(other._event)) return false;
|
|
1266
|
+
if (!this._sender.xid().equals(other._sender.xid())) return false;
|
|
1267
|
+
if (this._state === void 0 && other._state === void 0) {} else if (this._state !== void 0 && other._state !== void 0) {
|
|
1268
|
+
if (!this._state.digest().equals(other._state.digest())) return false;
|
|
1269
|
+
} else return false;
|
|
1270
|
+
if (this._peerContinuation === void 0 && other._peerContinuation === void 0) {} else if (this._peerContinuation !== void 0 && other._peerContinuation !== void 0) {
|
|
1271
|
+
if (!this._peerContinuation.digest().equals(other._peerContinuation.digest())) return false;
|
|
1272
|
+
} else return false;
|
|
1273
|
+
return true;
|
|
1274
|
+
}
|
|
1275
|
+
};
|
|
1276
|
+
|
|
1277
|
+
//#endregion
|
|
1278
|
+
//#region src/prelude.ts
|
|
1279
|
+
var prelude_exports = /* @__PURE__ */ __exportAll({
|
|
1280
|
+
Continuation: () => Continuation,
|
|
1281
|
+
GstpError: () => GstpError,
|
|
1282
|
+
GstpErrorCode: () => GstpErrorCode,
|
|
1283
|
+
SealedEvent: () => SealedEvent,
|
|
1284
|
+
SealedRequest: () => SealedRequest,
|
|
1285
|
+
SealedResponse: () => SealedResponse
|
|
1286
|
+
});
|
|
1287
|
+
|
|
1288
|
+
//#endregion
|
|
1289
|
+
//#region src/index.ts
|
|
1290
|
+
const VERSION = "0.13.0";
|
|
1291
|
+
|
|
1292
|
+
//#endregion
|
|
1293
|
+
exports.Continuation = Continuation;
|
|
1294
|
+
exports.GstpError = GstpError;
|
|
1295
|
+
exports.GstpErrorCode = GstpErrorCode;
|
|
1296
|
+
exports.SealedEvent = SealedEvent;
|
|
1297
|
+
exports.SealedRequest = SealedRequest;
|
|
1298
|
+
exports.SealedResponse = SealedResponse;
|
|
1299
|
+
exports.VERSION = VERSION;
|
|
1300
|
+
Object.defineProperty(exports, 'prelude', {
|
|
1301
|
+
enumerable: true,
|
|
1302
|
+
get: function () {
|
|
1303
|
+
return prelude_exports;
|
|
1304
|
+
}
|
|
1305
|
+
});
|
|
1306
|
+
return exports;
|
|
1307
|
+
})({}, bctsComponents, bctsEnvelope, bctsKnownValues, bctsXid);
|
|
1308
|
+
//# sourceMappingURL=index.iife.js.map
|