@opendatalabs/vana-sdk 3.23.0 → 4.0.0
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/README.md +57 -0
- package/dist/auth/errors.cjs +1 -1
- package/dist/auth/web3-signed.cjs +1 -1
- package/dist/crypto/envelope/job-stream.test.d.ts +1 -0
- package/dist/crypto/envelope/job.cjs +593 -38
- package/dist/crypto/envelope/job.cjs.map +1 -1
- package/dist/crypto/envelope/job.d.ts +44 -27
- package/dist/crypto/envelope/job.js +589 -37
- package/dist/crypto/envelope/job.js.map +1 -1
- package/dist/direct/access-request-client.cjs +1 -1
- package/dist/direct/connect-flow.cjs +43 -1
- package/dist/direct/connect-flow.cjs.map +1 -1
- package/dist/direct/connect-flow.d.ts +12 -0
- package/dist/direct/connect-flow.js +43 -1
- package/dist/direct/connect-flow.js.map +1 -1
- package/dist/direct/controller.cjs +1 -1
- package/dist/direct/errors.cjs +1 -1
- package/dist/direct/escrow-payment.cjs.map +1 -1
- package/dist/direct/escrow-payment.d.ts +2 -2
- package/dist/direct/escrow-payment.js.map +1 -1
- package/dist/direct/personal-server-read.cjs +2 -2
- package/dist/direct/use-direct-vana-connect.cjs +2 -1
- package/dist/direct/use-direct-vana-connect.cjs.map +1 -1
- package/dist/direct/use-direct-vana-connect.d.ts +3 -1
- package/dist/direct/use-direct-vana-connect.js +2 -1
- package/dist/direct/use-direct-vana-connect.js.map +1 -1
- package/dist/error-entry-points.test.d.ts +1 -0
- package/dist/errors.cjs +7 -0
- package/dist/errors.cjs.map +1 -1
- package/dist/errors.d.ts +10 -0
- package/dist/errors.js +6 -0
- package/dist/errors.js.map +1 -1
- package/dist/index.browser.d.ts +4 -4
- package/dist/index.browser.js +882 -518
- package/dist/index.browser.js.map +4 -4
- package/dist/index.node.cjs +1035 -705
- package/dist/index.node.cjs.map +4 -4
- package/dist/index.node.d.ts +4 -4
- package/dist/index.node.js +989 -567
- package/dist/index.node.js.map +4 -4
- package/dist/protocol/data-point-deletion.cjs +1 -1
- package/dist/protocol/derivative-questions.cjs +1 -1
- package/dist/protocol/derivative-status.cjs +1 -1
- package/dist/protocol/eip712.cjs +28 -2
- package/dist/protocol/eip712.cjs.map +1 -1
- package/dist/protocol/eip712.d.ts +70 -0
- package/dist/protocol/eip712.js +24 -1
- package/dist/protocol/eip712.js.map +1 -1
- package/dist/protocol/escrow.cjs +154 -2
- package/dist/protocol/escrow.cjs.map +1 -1
- package/dist/protocol/escrow.d.ts +145 -2
- package/dist/protocol/escrow.js +152 -1
- package/dist/protocol/escrow.js.map +1 -1
- package/dist/protocol/fixtures/moksha-identity.json +20 -0
- package/dist/protocol/gateway.cjs +1 -1
- package/dist/protocol/gateway.cjs.map +1 -1
- package/dist/protocol/gateway.d.ts +8 -38
- package/dist/protocol/gateway.js.map +1 -1
- package/dist/protocol/identity.cjs +7 -2
- package/dist/protocol/identity.cjs.map +1 -1
- package/dist/protocol/identity.js +7 -2
- package/dist/protocol/identity.js.map +1 -1
- package/dist/protocol/jobs-client.cjs +73 -21
- package/dist/protocol/jobs-client.cjs.map +1 -1
- package/dist/protocol/jobs-client.d.ts +7 -5
- package/dist/protocol/jobs-client.js +74 -21
- package/dist/protocol/jobs-client.js.map +1 -1
- package/dist/protocol/jobs.cjs +0 -3
- package/dist/protocol/jobs.cjs.map +1 -1
- package/dist/protocol/jobs.d.ts +21 -13
- package/dist/protocol/jobs.js +0 -2
- package/dist/protocol/jobs.js.map +1 -1
- package/dist/protocol/lineage.cjs +1 -1
- package/dist/protocol/personal-server-data.cjs +1 -1
- package/dist/protocol/personal-server-write.cjs +1 -1
- package/dist/protocol/write-request.cjs +1 -1
- package/dist/protocol/write-signer.cjs +1 -1
- package/dist/react.cjs.map +1 -1
- package/dist/react.d.ts +1 -1
- package/dist/react.js.map +1 -1
- package/dist/session-relay/client.cjs +1 -1
- package/dist/session-relay/errors.cjs +1 -1
- package/dist/session-relay/index.cjs +1 -1
- package/package.json +1 -1
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { sha256 } from "@noble/hashes/sha2";
|
|
2
2
|
import { bytesToHex, fromHex, isAddress, isHex, toHex } from "viem";
|
|
3
|
+
import { CURVE, FORMAT, MAC } from "../ecies/constants.js";
|
|
3
4
|
import {
|
|
4
5
|
deserializeECIES,
|
|
6
|
+
ECIESError,
|
|
5
7
|
serializeECIES
|
|
6
8
|
} from "../ecies/interface.js";
|
|
7
9
|
import {
|
|
@@ -11,6 +13,20 @@ import {
|
|
|
11
13
|
import { fromBase64, toBase64 } from "../../utils/encoding.js";
|
|
12
14
|
const textDecoder = new TextDecoder();
|
|
13
15
|
const textEncoder = new TextEncoder();
|
|
16
|
+
const MAX_JOB_RESULT_HEADER_BYTES = 4096;
|
|
17
|
+
const WRAPPED_KEY_LENGTH_BYTES = 4;
|
|
18
|
+
const FRAME_LENGTH_BYTES = 4;
|
|
19
|
+
const FRAME_FLAGS_BYTES = 1;
|
|
20
|
+
const AES_KEY_BYTES = 32;
|
|
21
|
+
const AES_GCM_NONCE_BYTES = 12;
|
|
22
|
+
const AES_GCM_TAG_BYTES = 16;
|
|
23
|
+
const WRAPPED_METADATA_LENGTH_BYTES = 4;
|
|
24
|
+
const WRAPPED_KEY_PLAINTEXT_PREFIX_BYTES = AES_KEY_BYTES + AES_GCM_NONCE_BYTES + WRAPPED_METADATA_LENGTH_BYTES;
|
|
25
|
+
const MAX_WRAPPED_KEY_BYTES = 8192;
|
|
26
|
+
const FINAL_FRAME_FLAG = 1;
|
|
27
|
+
const MAX_CHUNK_INDEX = 4294967295;
|
|
28
|
+
const JOB_RESULT_FORMAT_VERSION = 2;
|
|
29
|
+
const JOB_RESULT_CHUNK_BYTES = 1024 * 1024;
|
|
14
30
|
class JobEnvelopeError extends Error {
|
|
15
31
|
constructor(message) {
|
|
16
32
|
super(message);
|
|
@@ -42,21 +58,68 @@ function requestPlaintext(envelope) {
|
|
|
42
58
|
validateRequestEnvelope(envelope);
|
|
43
59
|
return canonicalJsonBytes(envelope);
|
|
44
60
|
}
|
|
45
|
-
function
|
|
46
|
-
return
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
body: result.body
|
|
54
|
-
})
|
|
55
|
-
);
|
|
61
|
+
function resultMetadata(result) {
|
|
62
|
+
return {
|
|
63
|
+
v: result.v,
|
|
64
|
+
jobId: result.jobId,
|
|
65
|
+
scope: result.scope,
|
|
66
|
+
version: result.version,
|
|
67
|
+
contentType: result.contentType
|
|
68
|
+
};
|
|
56
69
|
}
|
|
57
70
|
function encryptedBytesToBase64(encrypted) {
|
|
58
71
|
return toBase64(fromHex(`0x${serializeECIES(encrypted)}`, "bytes"));
|
|
59
72
|
}
|
|
73
|
+
function encryptedToBytes(encrypted) {
|
|
74
|
+
const bytes = new Uint8Array(
|
|
75
|
+
encrypted.iv.length + encrypted.ephemPublicKey.length + encrypted.ciphertext.length + encrypted.mac.length
|
|
76
|
+
);
|
|
77
|
+
let offset = 0;
|
|
78
|
+
bytes.set(encrypted.iv, offset);
|
|
79
|
+
offset += encrypted.iv.length;
|
|
80
|
+
bytes.set(encrypted.ephemPublicKey, offset);
|
|
81
|
+
offset += encrypted.ephemPublicKey.length;
|
|
82
|
+
bytes.set(encrypted.ciphertext, offset);
|
|
83
|
+
offset += encrypted.ciphertext.length;
|
|
84
|
+
bytes.set(encrypted.mac, offset);
|
|
85
|
+
return bytes;
|
|
86
|
+
}
|
|
87
|
+
function bytesToEncrypted(bytes) {
|
|
88
|
+
const absoluteMinLength = FORMAT.IV_LENGTH + 1 + MAC.LENGTH + 1;
|
|
89
|
+
if (bytes.length < absoluteMinLength) {
|
|
90
|
+
throw new ECIESError(
|
|
91
|
+
`Invalid ECIES data: too short (${bytes.length} bytes, minimum ${absoluteMinLength} bytes required)`,
|
|
92
|
+
"DECRYPTION_FAILED"
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
const prefix = bytes[FORMAT.EPHEMERAL_KEY_OFFSET];
|
|
96
|
+
if (prefix !== CURVE.PREFIX.UNCOMPRESSED) {
|
|
97
|
+
throw new ECIESError(
|
|
98
|
+
`Invalid ephemeral public key: must be uncompressed format (0x04 prefix), got 0x${prefix.toString(16).padStart(2, "0")}`,
|
|
99
|
+
"DECRYPTION_FAILED"
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
const ephemKeySize = CURVE.UNCOMPRESSED_PUBLIC_KEY_LENGTH;
|
|
103
|
+
const minLength = FORMAT.IV_LENGTH + ephemKeySize + MAC.LENGTH + 1;
|
|
104
|
+
if (bytes.length < minLength) {
|
|
105
|
+
throw new ECIESError(
|
|
106
|
+
`Invalid ECIES data: too short (${bytes.length} bytes, minimum ${minLength} bytes required)`,
|
|
107
|
+
"DECRYPTION_FAILED"
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
return {
|
|
111
|
+
iv: bytes.subarray(FORMAT.IV_OFFSET, FORMAT.IV_OFFSET + FORMAT.IV_LENGTH),
|
|
112
|
+
ephemPublicKey: bytes.subarray(
|
|
113
|
+
FORMAT.EPHEMERAL_KEY_OFFSET,
|
|
114
|
+
FORMAT.EPHEMERAL_KEY_OFFSET + ephemKeySize
|
|
115
|
+
),
|
|
116
|
+
ciphertext: bytes.subarray(
|
|
117
|
+
FORMAT.EPHEMERAL_KEY_OFFSET + ephemKeySize,
|
|
118
|
+
bytes.length - MAC.LENGTH
|
|
119
|
+
),
|
|
120
|
+
mac: bytes.subarray(bytes.length - MAC.LENGTH)
|
|
121
|
+
};
|
|
122
|
+
}
|
|
60
123
|
function base64ToEncrypted(ciphertext) {
|
|
61
124
|
return deserializeECIES(toHex(fromBase64(ciphertext)));
|
|
62
125
|
}
|
|
@@ -157,11 +220,11 @@ function validateRequestEnvelope(value) {
|
|
|
157
220
|
requireString(value.auth, "auth", "job request envelope");
|
|
158
221
|
return value;
|
|
159
222
|
}
|
|
160
|
-
function
|
|
161
|
-
requirePlainObject(value, "
|
|
223
|
+
function validateResultMetadata(value) {
|
|
224
|
+
requirePlainObject(value, "metadata", "job result");
|
|
162
225
|
requireExactKeys(
|
|
163
226
|
value,
|
|
164
|
-
["v", "jobId", "scope", "version", "contentType"
|
|
227
|
+
["v", "jobId", "scope", "version", "contentType"],
|
|
165
228
|
"job result"
|
|
166
229
|
);
|
|
167
230
|
if (value.v !== JOB_PROTOCOL_VERSION) {
|
|
@@ -172,14 +235,222 @@ function validateResult(value) {
|
|
|
172
235
|
for (const field of ["jobId", "scope", "contentType"]) {
|
|
173
236
|
requireString(value[field], field, "job result");
|
|
174
237
|
}
|
|
175
|
-
if (typeof value.body !== "string") {
|
|
176
|
-
throw new JobEnvelopeError("Invalid job result: missing body");
|
|
177
|
-
}
|
|
178
238
|
if (value.version !== null && typeof value.version !== "string") {
|
|
179
239
|
throw new JobEnvelopeError("Invalid job result: missing version");
|
|
180
240
|
}
|
|
181
241
|
return value;
|
|
182
242
|
}
|
|
243
|
+
function validateResult(value) {
|
|
244
|
+
requirePlainObject(value, "result", "job result");
|
|
245
|
+
requireExactKeys(
|
|
246
|
+
value,
|
|
247
|
+
["v", "jobId", "scope", "version", "contentType", "body"],
|
|
248
|
+
"job result"
|
|
249
|
+
);
|
|
250
|
+
validateResultMetadata(resultMetadata(value));
|
|
251
|
+
if (!(value.body instanceof Uint8Array)) {
|
|
252
|
+
throw new JobEnvelopeError("Invalid job result: missing body");
|
|
253
|
+
}
|
|
254
|
+
return value;
|
|
255
|
+
}
|
|
256
|
+
function webCrypto() {
|
|
257
|
+
if (typeof globalThis.crypto === "undefined" || typeof globalThis.crypto.getRandomValues !== "function" || !globalThis.crypto.subtle) {
|
|
258
|
+
throw new JobEnvelopeError(
|
|
259
|
+
"Job result streaming requires the Web Crypto API"
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
return globalThis.crypto;
|
|
263
|
+
}
|
|
264
|
+
function uint32(value) {
|
|
265
|
+
const bytes = new Uint8Array(4);
|
|
266
|
+
new DataView(bytes.buffer).setUint32(0, value);
|
|
267
|
+
return bytes;
|
|
268
|
+
}
|
|
269
|
+
function readUint32(bytes, offset = 0) {
|
|
270
|
+
return new DataView(bytes.buffer, bytes.byteOffset + offset, 4).getUint32(0);
|
|
271
|
+
}
|
|
272
|
+
function wrappedKeyPlaintext(contentKey, noncePrefix, metadataBytes) {
|
|
273
|
+
const plaintext = new Uint8Array(
|
|
274
|
+
WRAPPED_KEY_PLAINTEXT_PREFIX_BYTES + metadataBytes.length
|
|
275
|
+
);
|
|
276
|
+
plaintext.set(contentKey, 0);
|
|
277
|
+
plaintext.set(noncePrefix, AES_KEY_BYTES);
|
|
278
|
+
new DataView(plaintext.buffer).setUint32(
|
|
279
|
+
AES_KEY_BYTES + AES_GCM_NONCE_BYTES,
|
|
280
|
+
metadataBytes.length
|
|
281
|
+
);
|
|
282
|
+
plaintext.set(metadataBytes, WRAPPED_KEY_PLAINTEXT_PREFIX_BYTES);
|
|
283
|
+
return plaintext;
|
|
284
|
+
}
|
|
285
|
+
function parseWrappedKeyPlaintext(plaintext) {
|
|
286
|
+
if (plaintext.length < WRAPPED_KEY_PLAINTEXT_PREFIX_BYTES) {
|
|
287
|
+
throw new JobEnvelopeError("Invalid job result: wrapped key is truncated");
|
|
288
|
+
}
|
|
289
|
+
const metadataLength = readUint32(
|
|
290
|
+
plaintext,
|
|
291
|
+
AES_KEY_BYTES + AES_GCM_NONCE_BYTES
|
|
292
|
+
);
|
|
293
|
+
if (metadataLength > MAX_JOB_RESULT_HEADER_BYTES) {
|
|
294
|
+
throw new JobEnvelopeError(
|
|
295
|
+
`Invalid job result: header exceeds ${MAX_JOB_RESULT_HEADER_BYTES} bytes`
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
if (plaintext.length !== WRAPPED_KEY_PLAINTEXT_PREFIX_BYTES + metadataLength) {
|
|
299
|
+
throw new JobEnvelopeError(
|
|
300
|
+
"Invalid job result: header length exceeds payload"
|
|
301
|
+
);
|
|
302
|
+
}
|
|
303
|
+
const metadataBytes = plaintext.slice(WRAPPED_KEY_PLAINTEXT_PREFIX_BYTES);
|
|
304
|
+
const metadata = validateResultMetadata(
|
|
305
|
+
parseObject(metadataBytes, "job result header")
|
|
306
|
+
);
|
|
307
|
+
return {
|
|
308
|
+
contentKey: plaintext.slice(0, AES_KEY_BYTES),
|
|
309
|
+
noncePrefix: plaintext.slice(
|
|
310
|
+
AES_KEY_BYTES,
|
|
311
|
+
AES_KEY_BYTES + AES_GCM_NONCE_BYTES
|
|
312
|
+
),
|
|
313
|
+
metadataBytes,
|
|
314
|
+
metadata
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
async function importAesKey(contentKey, usage) {
|
|
318
|
+
return webCrypto().subtle.importKey(
|
|
319
|
+
"raw",
|
|
320
|
+
contentKey,
|
|
321
|
+
{ name: "AES-GCM" },
|
|
322
|
+
false,
|
|
323
|
+
[usage]
|
|
324
|
+
);
|
|
325
|
+
}
|
|
326
|
+
function chunkNonce(noncePrefix, chunkIndex) {
|
|
327
|
+
if (chunkIndex > MAX_CHUNK_INDEX) {
|
|
328
|
+
throw new JobEnvelopeError("Invalid job result: too many chunks");
|
|
329
|
+
}
|
|
330
|
+
const nonce = noncePrefix.slice();
|
|
331
|
+
const view = new DataView(nonce.buffer, nonce.byteOffset, nonce.byteLength);
|
|
332
|
+
view.setUint32(8, view.getUint32(8) ^ chunkIndex);
|
|
333
|
+
return nonce;
|
|
334
|
+
}
|
|
335
|
+
function chunkAad(metadataBytes, chunkIndex, final, plaintextLength) {
|
|
336
|
+
const aad = new Uint8Array(14 + metadataBytes.length);
|
|
337
|
+
aad[0] = JOB_RESULT_FORMAT_VERSION;
|
|
338
|
+
aad.set(uint32(metadataBytes.length), 1);
|
|
339
|
+
aad.set(metadataBytes, 5);
|
|
340
|
+
aad.set(uint32(chunkIndex), 5 + metadataBytes.length);
|
|
341
|
+
aad[9 + metadataBytes.length] = final ? FINAL_FRAME_FLAG : 0;
|
|
342
|
+
aad.set(uint32(plaintextLength), 10 + metadataBytes.length);
|
|
343
|
+
return aad;
|
|
344
|
+
}
|
|
345
|
+
async function encryptResultChunk(key, noncePrefix, metadataBytes, plaintext, chunkIndex, final) {
|
|
346
|
+
const encrypted = new Uint8Array(
|
|
347
|
+
await webCrypto().subtle.encrypt(
|
|
348
|
+
{
|
|
349
|
+
name: "AES-GCM",
|
|
350
|
+
iv: chunkNonce(noncePrefix, chunkIndex),
|
|
351
|
+
additionalData: chunkAad(
|
|
352
|
+
metadataBytes,
|
|
353
|
+
chunkIndex,
|
|
354
|
+
final,
|
|
355
|
+
plaintext.length
|
|
356
|
+
),
|
|
357
|
+
tagLength: AES_GCM_TAG_BYTES * 8
|
|
358
|
+
},
|
|
359
|
+
key,
|
|
360
|
+
plaintext
|
|
361
|
+
)
|
|
362
|
+
);
|
|
363
|
+
const frame = new Uint8Array(
|
|
364
|
+
FRAME_LENGTH_BYTES + FRAME_FLAGS_BYTES + encrypted.length
|
|
365
|
+
);
|
|
366
|
+
new DataView(frame.buffer).setUint32(0, encrypted.length);
|
|
367
|
+
frame[FRAME_LENGTH_BYTES] = final ? FINAL_FRAME_FLAG : 0;
|
|
368
|
+
frame.set(encrypted, FRAME_LENGTH_BYTES + FRAME_FLAGS_BYTES);
|
|
369
|
+
return frame;
|
|
370
|
+
}
|
|
371
|
+
function verifyResultBindings(metadata, expect) {
|
|
372
|
+
if (metadata.jobId !== expect.jobId) {
|
|
373
|
+
throw new JobEnvelopeError(
|
|
374
|
+
`Job result ID ${metadata.jobId} does not match expected job ID ${expect.jobId}`
|
|
375
|
+
);
|
|
376
|
+
}
|
|
377
|
+
if (expect.scope !== void 0 && metadata.scope !== expect.scope) {
|
|
378
|
+
throw new JobEnvelopeError(
|
|
379
|
+
`Job result scope ${metadata.scope} does not match expected scope ${expect.scope}`
|
|
380
|
+
);
|
|
381
|
+
}
|
|
382
|
+
if (expect.version !== void 0 && metadata.version !== expect.version) {
|
|
383
|
+
throw new JobEnvelopeError(
|
|
384
|
+
`Job result version ${String(metadata.version)} does not match expected version ${String(expect.version)}`
|
|
385
|
+
);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
class ByteStreamReader {
|
|
389
|
+
reader;
|
|
390
|
+
current;
|
|
391
|
+
offset = 0;
|
|
392
|
+
ended = false;
|
|
393
|
+
constructor(stream) {
|
|
394
|
+
this.reader = stream.getReader();
|
|
395
|
+
}
|
|
396
|
+
async ensureBytes() {
|
|
397
|
+
while (!this.current || this.offset === this.current.length) {
|
|
398
|
+
if (this.ended) return false;
|
|
399
|
+
const next = await this.reader.read();
|
|
400
|
+
if (next.done) {
|
|
401
|
+
this.ended = true;
|
|
402
|
+
this.current = void 0;
|
|
403
|
+
return false;
|
|
404
|
+
}
|
|
405
|
+
if (!(next.value instanceof Uint8Array)) {
|
|
406
|
+
throw new JobEnvelopeError(
|
|
407
|
+
"Invalid job result: stream yielded non-byte data"
|
|
408
|
+
);
|
|
409
|
+
}
|
|
410
|
+
this.current = next.value;
|
|
411
|
+
this.offset = 0;
|
|
412
|
+
}
|
|
413
|
+
return true;
|
|
414
|
+
}
|
|
415
|
+
async readExactOrNull(length) {
|
|
416
|
+
if (!await this.ensureBytes()) return null;
|
|
417
|
+
const bytes = new Uint8Array(length);
|
|
418
|
+
let written = 0;
|
|
419
|
+
while (written < length) {
|
|
420
|
+
if (!await this.ensureBytes()) {
|
|
421
|
+
throw new JobEnvelopeError("Invalid job result: truncated payload");
|
|
422
|
+
}
|
|
423
|
+
const available = this.current.length - this.offset;
|
|
424
|
+
const count = Math.min(available, length - written);
|
|
425
|
+
bytes.set(
|
|
426
|
+
this.current.subarray(this.offset, this.offset + count),
|
|
427
|
+
written
|
|
428
|
+
);
|
|
429
|
+
this.offset += count;
|
|
430
|
+
written += count;
|
|
431
|
+
}
|
|
432
|
+
return bytes;
|
|
433
|
+
}
|
|
434
|
+
async readExact(length) {
|
|
435
|
+
const bytes = await this.readExactOrNull(length);
|
|
436
|
+
if (!bytes) {
|
|
437
|
+
throw new JobEnvelopeError("Invalid job result: truncated payload");
|
|
438
|
+
}
|
|
439
|
+
return bytes;
|
|
440
|
+
}
|
|
441
|
+
async hasRemaining() {
|
|
442
|
+
return this.ensureBytes();
|
|
443
|
+
}
|
|
444
|
+
async cancel(reason) {
|
|
445
|
+
await this.reader.cancel(reason);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
function normalizeChunkError(error) {
|
|
449
|
+
if (error instanceof JobEnvelopeError) return error;
|
|
450
|
+
return new JobEnvelopeError(
|
|
451
|
+
`Invalid job result: chunk authentication failed${error instanceof Error ? `: ${error.message}` : ""}`
|
|
452
|
+
);
|
|
453
|
+
}
|
|
183
454
|
async function sealJobRequest(envelope, enclavePublicKey, ecies) {
|
|
184
455
|
const encrypted = await ecies.encrypt(
|
|
185
456
|
fromHex(enclavePublicKey, "bytes"),
|
|
@@ -196,45 +467,326 @@ async function openJobRequest(ciphertext, privateKey, ecies) {
|
|
|
196
467
|
parseObject(plaintext, "job request envelope")
|
|
197
468
|
);
|
|
198
469
|
}
|
|
199
|
-
async function
|
|
200
|
-
|
|
201
|
-
const
|
|
470
|
+
async function sealJobResultStream(metadataValue, builderPublicKey, ecies) {
|
|
471
|
+
const metadata = validateResultMetadata(metadataValue);
|
|
472
|
+
const metadataBytes = canonicalJsonBytes(metadata);
|
|
473
|
+
if (metadataBytes.length > MAX_JOB_RESULT_HEADER_BYTES) {
|
|
474
|
+
throw new JobEnvelopeError(
|
|
475
|
+
`Invalid job result: header exceeds ${MAX_JOB_RESULT_HEADER_BYTES} bytes`
|
|
476
|
+
);
|
|
477
|
+
}
|
|
478
|
+
const crypto = webCrypto();
|
|
479
|
+
const contentKey = crypto.getRandomValues(new Uint8Array(AES_KEY_BYTES));
|
|
480
|
+
const noncePrefix = crypto.getRandomValues(
|
|
481
|
+
new Uint8Array(AES_GCM_NONCE_BYTES)
|
|
482
|
+
);
|
|
483
|
+
const key = await importAesKey(contentKey, "encrypt");
|
|
484
|
+
const keyPlaintext = wrappedKeyPlaintext(
|
|
485
|
+
contentKey,
|
|
486
|
+
noncePrefix,
|
|
487
|
+
metadataBytes
|
|
488
|
+
);
|
|
489
|
+
const encryptedKey = await ecies.encrypt(
|
|
202
490
|
fromHex(builderPublicKey, "bytes"),
|
|
203
|
-
|
|
491
|
+
keyPlaintext
|
|
204
492
|
);
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
493
|
+
contentKey.fill(0);
|
|
494
|
+
keyPlaintext.fill(0);
|
|
495
|
+
const wrappedKey = encryptedToBytes(encryptedKey);
|
|
496
|
+
if (wrappedKey.length > MAX_WRAPPED_KEY_BYTES) {
|
|
497
|
+
throw new JobEnvelopeError("Invalid job result: wrapped key is too large");
|
|
498
|
+
}
|
|
499
|
+
const header = new Uint8Array(
|
|
500
|
+
1 + WRAPPED_KEY_LENGTH_BYTES + wrappedKey.length
|
|
501
|
+
);
|
|
502
|
+
header[0] = JOB_RESULT_FORMAT_VERSION;
|
|
503
|
+
new DataView(header.buffer).setUint32(1, wrappedKey.length);
|
|
504
|
+
header.set(wrappedKey, 1 + WRAPPED_KEY_LENGTH_BYTES);
|
|
505
|
+
let pending = new Uint8Array(JOB_RESULT_CHUNK_BYTES);
|
|
506
|
+
let pendingLength = 0;
|
|
507
|
+
let chunkIndex = 0;
|
|
508
|
+
const transform = new TransformStream({
|
|
509
|
+
async transform(chunk, controller) {
|
|
510
|
+
if (!(chunk instanceof Uint8Array)) {
|
|
511
|
+
throw new JobEnvelopeError(
|
|
512
|
+
"Invalid job result: stream yielded non-byte data"
|
|
513
|
+
);
|
|
514
|
+
}
|
|
515
|
+
let offset = 0;
|
|
516
|
+
while (offset < chunk.length) {
|
|
517
|
+
if (pendingLength === JOB_RESULT_CHUNK_BYTES) {
|
|
518
|
+
controller.enqueue(
|
|
519
|
+
await encryptResultChunk(
|
|
520
|
+
key,
|
|
521
|
+
noncePrefix,
|
|
522
|
+
metadataBytes,
|
|
523
|
+
pending,
|
|
524
|
+
chunkIndex,
|
|
525
|
+
false
|
|
526
|
+
)
|
|
527
|
+
);
|
|
528
|
+
chunkIndex += 1;
|
|
529
|
+
pending = new Uint8Array(JOB_RESULT_CHUNK_BYTES);
|
|
530
|
+
pendingLength = 0;
|
|
531
|
+
}
|
|
532
|
+
const count = Math.min(
|
|
533
|
+
chunk.length - offset,
|
|
534
|
+
JOB_RESULT_CHUNK_BYTES - pendingLength
|
|
535
|
+
);
|
|
536
|
+
pending.set(chunk.subarray(offset, offset + count), pendingLength);
|
|
537
|
+
pendingLength += count;
|
|
538
|
+
offset += count;
|
|
539
|
+
}
|
|
540
|
+
},
|
|
541
|
+
async flush(controller) {
|
|
542
|
+
controller.enqueue(
|
|
543
|
+
await encryptResultChunk(
|
|
544
|
+
key,
|
|
545
|
+
noncePrefix,
|
|
546
|
+
metadataBytes,
|
|
547
|
+
pending.subarray(0, pendingLength),
|
|
548
|
+
chunkIndex,
|
|
549
|
+
true
|
|
550
|
+
)
|
|
551
|
+
);
|
|
552
|
+
}
|
|
553
|
+
});
|
|
554
|
+
return { header, transform };
|
|
208
555
|
}
|
|
209
|
-
async function
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
556
|
+
async function sealJobResult(result, builderPublicKey, ecies) {
|
|
557
|
+
validateResult(result);
|
|
558
|
+
const sealed = await sealJobResultStream(
|
|
559
|
+
resultMetadata(result),
|
|
560
|
+
builderPublicKey,
|
|
561
|
+
ecies
|
|
213
562
|
);
|
|
214
|
-
const
|
|
215
|
-
|
|
563
|
+
const frameCount = Math.max(
|
|
564
|
+
1,
|
|
565
|
+
Math.ceil(result.body.length / JOB_RESULT_CHUNK_BYTES)
|
|
566
|
+
);
|
|
567
|
+
const size = sealed.header.length + result.body.length + frameCount * (FRAME_LENGTH_BYTES + FRAME_FLAGS_BYTES + AES_GCM_TAG_BYTES);
|
|
568
|
+
const bytes = new Uint8Array(size);
|
|
569
|
+
bytes.set(sealed.header, 0);
|
|
570
|
+
let outputOffset = sealed.header.length;
|
|
571
|
+
const reader = sealed.transform.readable.getReader();
|
|
572
|
+
const drain = (async () => {
|
|
573
|
+
for (; ; ) {
|
|
574
|
+
const next = await reader.read();
|
|
575
|
+
if (next.done) break;
|
|
576
|
+
bytes.set(next.value, outputOffset);
|
|
577
|
+
outputOffset += next.value.length;
|
|
578
|
+
}
|
|
579
|
+
})();
|
|
580
|
+
const writer = sealed.transform.writable.getWriter();
|
|
581
|
+
const fill = (async () => {
|
|
582
|
+
for (let offset = 0; offset < result.body.length; offset += JOB_RESULT_CHUNK_BYTES) {
|
|
583
|
+
await writer.write(
|
|
584
|
+
result.body.subarray(offset, offset + JOB_RESULT_CHUNK_BYTES)
|
|
585
|
+
);
|
|
586
|
+
}
|
|
587
|
+
await writer.close();
|
|
588
|
+
})();
|
|
589
|
+
await Promise.all([drain, fill]);
|
|
590
|
+
if (outputOffset !== bytes.length) {
|
|
591
|
+
throw new JobEnvelopeError("Invalid job result: sealed size mismatch");
|
|
592
|
+
}
|
|
593
|
+
return { bytes, hash: bytesToHex(sha256(bytes)), size };
|
|
594
|
+
}
|
|
595
|
+
async function openJobResultStream(bytesStream, builderPrivateKey, ecies, expect) {
|
|
596
|
+
const reader = new ByteStreamReader(bytesStream);
|
|
597
|
+
const headerPrefix = await reader.readExact(1 + WRAPPED_KEY_LENGTH_BYTES);
|
|
598
|
+
const formatVersion = headerPrefix[0];
|
|
599
|
+
if (formatVersion !== JOB_RESULT_FORMAT_VERSION) {
|
|
216
600
|
throw new JobEnvelopeError(
|
|
217
|
-
`
|
|
601
|
+
`Unsupported job result format: ${String(formatVersion)}`
|
|
218
602
|
);
|
|
219
603
|
}
|
|
220
|
-
|
|
604
|
+
const wrappedKeyLength = readUint32(headerPrefix, 1);
|
|
605
|
+
if (wrappedKeyLength === 0 || wrappedKeyLength > MAX_WRAPPED_KEY_BYTES) {
|
|
606
|
+
throw new JobEnvelopeError(
|
|
607
|
+
"Invalid job result: invalid wrapped key length"
|
|
608
|
+
);
|
|
609
|
+
}
|
|
610
|
+
const wrappedKey = await reader.readExact(wrappedKeyLength);
|
|
611
|
+
const keyPlaintext = await ecies.decrypt(
|
|
612
|
+
fromHex(builderPrivateKey, "bytes"),
|
|
613
|
+
bytesToEncrypted(wrappedKey)
|
|
614
|
+
);
|
|
615
|
+
const parsed = parseWrappedKeyPlaintext(keyPlaintext);
|
|
616
|
+
verifyResultBindings(parsed.metadata, expect);
|
|
617
|
+
const key = await importAesKey(parsed.contentKey, "decrypt");
|
|
618
|
+
parsed.contentKey.fill(0);
|
|
619
|
+
keyPlaintext.fill(0);
|
|
620
|
+
let chunkIndex = 0;
|
|
621
|
+
let finished = false;
|
|
622
|
+
const body = new ReadableStream({
|
|
623
|
+
async pull(controller) {
|
|
624
|
+
if (finished) {
|
|
625
|
+
controller.close();
|
|
626
|
+
return;
|
|
627
|
+
}
|
|
628
|
+
try {
|
|
629
|
+
const frameHeader = await reader.readExactOrNull(
|
|
630
|
+
FRAME_LENGTH_BYTES + FRAME_FLAGS_BYTES
|
|
631
|
+
);
|
|
632
|
+
if (!frameHeader) {
|
|
633
|
+
throw new JobEnvelopeError("Invalid job result: missing final chunk");
|
|
634
|
+
}
|
|
635
|
+
const encryptedLength = readUint32(frameHeader);
|
|
636
|
+
if (encryptedLength < AES_GCM_TAG_BYTES || encryptedLength > JOB_RESULT_CHUNK_BYTES + AES_GCM_TAG_BYTES) {
|
|
637
|
+
throw new JobEnvelopeError(
|
|
638
|
+
"Invalid job result: invalid chunk length"
|
|
639
|
+
);
|
|
640
|
+
}
|
|
641
|
+
const flags = frameHeader[FRAME_LENGTH_BYTES];
|
|
642
|
+
if (flags !== 0 && flags !== FINAL_FRAME_FLAG) {
|
|
643
|
+
throw new JobEnvelopeError("Invalid job result: invalid chunk flags");
|
|
644
|
+
}
|
|
645
|
+
const final = flags === FINAL_FRAME_FLAG;
|
|
646
|
+
const plaintextLength = encryptedLength - AES_GCM_TAG_BYTES;
|
|
647
|
+
if (!final && plaintextLength !== JOB_RESULT_CHUNK_BYTES) {
|
|
648
|
+
throw new JobEnvelopeError(
|
|
649
|
+
"Invalid job result: short non-final chunk"
|
|
650
|
+
);
|
|
651
|
+
}
|
|
652
|
+
const encrypted = await reader.readExact(encryptedLength);
|
|
653
|
+
const plaintext = new Uint8Array(
|
|
654
|
+
await webCrypto().subtle.decrypt(
|
|
655
|
+
{
|
|
656
|
+
name: "AES-GCM",
|
|
657
|
+
iv: chunkNonce(parsed.noncePrefix, chunkIndex),
|
|
658
|
+
additionalData: chunkAad(
|
|
659
|
+
parsed.metadataBytes,
|
|
660
|
+
chunkIndex,
|
|
661
|
+
final,
|
|
662
|
+
plaintextLength
|
|
663
|
+
),
|
|
664
|
+
tagLength: AES_GCM_TAG_BYTES * 8
|
|
665
|
+
},
|
|
666
|
+
key,
|
|
667
|
+
encrypted
|
|
668
|
+
)
|
|
669
|
+
);
|
|
670
|
+
if (plaintext.length !== plaintextLength) {
|
|
671
|
+
throw new JobEnvelopeError(
|
|
672
|
+
"Invalid job result: decrypted chunk length mismatch"
|
|
673
|
+
);
|
|
674
|
+
}
|
|
675
|
+
chunkIndex += 1;
|
|
676
|
+
if (final) {
|
|
677
|
+
if (await reader.hasRemaining()) {
|
|
678
|
+
throw new JobEnvelopeError(
|
|
679
|
+
"Invalid job result: bytes follow final chunk"
|
|
680
|
+
);
|
|
681
|
+
}
|
|
682
|
+
finished = true;
|
|
683
|
+
}
|
|
684
|
+
controller.enqueue(plaintext);
|
|
685
|
+
if (finished) controller.close();
|
|
686
|
+
} catch (error) {
|
|
687
|
+
controller.error(normalizeChunkError(error));
|
|
688
|
+
void reader.cancel(error).catch(() => void 0);
|
|
689
|
+
}
|
|
690
|
+
},
|
|
691
|
+
cancel(reason) {
|
|
692
|
+
return reader.cancel(reason);
|
|
693
|
+
}
|
|
694
|
+
});
|
|
695
|
+
return { metadata: parsed.metadata, body };
|
|
696
|
+
}
|
|
697
|
+
function bufferedResultBodySize(sealedBytes) {
|
|
698
|
+
if (sealedBytes.length < 1 + WRAPPED_KEY_LENGTH_BYTES) {
|
|
699
|
+
throw new JobEnvelopeError("Invalid job result: truncated payload");
|
|
700
|
+
}
|
|
701
|
+
if (sealedBytes[0] !== JOB_RESULT_FORMAT_VERSION) {
|
|
221
702
|
throw new JobEnvelopeError(
|
|
222
|
-
`
|
|
703
|
+
`Unsupported job result format: ${String(sealedBytes[0])}`
|
|
223
704
|
);
|
|
224
705
|
}
|
|
225
|
-
|
|
706
|
+
const wrappedKeyLength = readUint32(sealedBytes, 1);
|
|
707
|
+
if (wrappedKeyLength === 0 || wrappedKeyLength > MAX_WRAPPED_KEY_BYTES) {
|
|
226
708
|
throw new JobEnvelopeError(
|
|
227
|
-
|
|
709
|
+
"Invalid job result: invalid wrapped key length"
|
|
228
710
|
);
|
|
229
711
|
}
|
|
230
|
-
|
|
712
|
+
let offset = 1 + WRAPPED_KEY_LENGTH_BYTES + wrappedKeyLength;
|
|
713
|
+
if (offset > sealedBytes.length) {
|
|
714
|
+
throw new JobEnvelopeError("Invalid job result: truncated payload");
|
|
715
|
+
}
|
|
716
|
+
let bodySize = 0;
|
|
717
|
+
let sawFinal = false;
|
|
718
|
+
while (offset < sealedBytes.length) {
|
|
719
|
+
if (sealedBytes.length - offset < FRAME_LENGTH_BYTES + FRAME_FLAGS_BYTES) {
|
|
720
|
+
throw new JobEnvelopeError("Invalid job result: truncated payload");
|
|
721
|
+
}
|
|
722
|
+
const encryptedLength = readUint32(sealedBytes, offset);
|
|
723
|
+
const flags = sealedBytes[offset + FRAME_LENGTH_BYTES];
|
|
724
|
+
if (encryptedLength < AES_GCM_TAG_BYTES || encryptedLength > JOB_RESULT_CHUNK_BYTES + AES_GCM_TAG_BYTES || flags !== 0 && flags !== FINAL_FRAME_FLAG) {
|
|
725
|
+
throw new JobEnvelopeError("Invalid job result: invalid chunk framing");
|
|
726
|
+
}
|
|
727
|
+
const plaintextLength = encryptedLength - AES_GCM_TAG_BYTES;
|
|
728
|
+
if (flags === 0 && plaintextLength !== JOB_RESULT_CHUNK_BYTES) {
|
|
729
|
+
throw new JobEnvelopeError("Invalid job result: short non-final chunk");
|
|
730
|
+
}
|
|
731
|
+
offset += FRAME_LENGTH_BYTES + FRAME_FLAGS_BYTES;
|
|
732
|
+
if (encryptedLength > sealedBytes.length - offset) {
|
|
733
|
+
throw new JobEnvelopeError("Invalid job result: truncated payload");
|
|
734
|
+
}
|
|
735
|
+
offset += encryptedLength;
|
|
736
|
+
bodySize += plaintextLength;
|
|
737
|
+
if (flags === FINAL_FRAME_FLAG) {
|
|
738
|
+
sawFinal = true;
|
|
739
|
+
if (offset !== sealedBytes.length) {
|
|
740
|
+
throw new JobEnvelopeError(
|
|
741
|
+
"Invalid job result: bytes follow final chunk"
|
|
742
|
+
);
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
if (!sawFinal) {
|
|
747
|
+
throw new JobEnvelopeError("Invalid job result: missing final chunk");
|
|
748
|
+
}
|
|
749
|
+
return bodySize;
|
|
750
|
+
}
|
|
751
|
+
async function openJobResult(sealedBytes, builderPrivateKey, ecies, expect) {
|
|
752
|
+
const bodySize = bufferedResultBodySize(sealedBytes);
|
|
753
|
+
const stream = new ReadableStream({
|
|
754
|
+
start(controller) {
|
|
755
|
+
controller.enqueue(sealedBytes);
|
|
756
|
+
controller.close();
|
|
757
|
+
}
|
|
758
|
+
});
|
|
759
|
+
const opened = await openJobResultStream(
|
|
760
|
+
stream,
|
|
761
|
+
builderPrivateKey,
|
|
762
|
+
ecies,
|
|
763
|
+
expect
|
|
764
|
+
);
|
|
765
|
+
const body = new Uint8Array(bodySize);
|
|
766
|
+
let offset = 0;
|
|
767
|
+
await opened.body.pipeTo(
|
|
768
|
+
new WritableStream({
|
|
769
|
+
write(chunk) {
|
|
770
|
+
body.set(chunk, offset);
|
|
771
|
+
offset += chunk.length;
|
|
772
|
+
}
|
|
773
|
+
})
|
|
774
|
+
);
|
|
775
|
+
if (offset !== body.length) {
|
|
776
|
+
throw new JobEnvelopeError("Invalid job result: opened size mismatch");
|
|
777
|
+
}
|
|
778
|
+
return { ...opened.metadata, body };
|
|
231
779
|
}
|
|
232
780
|
export {
|
|
781
|
+
JOB_RESULT_CHUNK_BYTES,
|
|
782
|
+
JOB_RESULT_FORMAT_VERSION,
|
|
233
783
|
JobEnvelopeError,
|
|
234
784
|
canonicalJobRequestBytes,
|
|
235
785
|
openJobRequest,
|
|
236
786
|
openJobResult,
|
|
787
|
+
openJobResultStream,
|
|
237
788
|
sealJobRequest,
|
|
238
|
-
sealJobResult
|
|
789
|
+
sealJobResult,
|
|
790
|
+
sealJobResultStream
|
|
239
791
|
};
|
|
240
792
|
//# sourceMappingURL=job.js.map
|