@opendatalabs/vana-sdk 3.22.0-pr.209.70d3733 → 3.22.0-pr.210.0a5d07b

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.
@@ -19,6 +19,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
19
19
  var job_exports = {};
20
20
  __export(job_exports, {
21
21
  JobEnvelopeError: () => JobEnvelopeError,
22
+ canonicalJobRequestBytes: () => canonicalJobRequestBytes,
22
23
  openJobRequest: () => openJobRequest,
23
24
  openJobResult: () => openJobResult,
24
25
  sealJobRequest: () => sealJobRequest,
@@ -28,6 +29,7 @@ module.exports = __toCommonJS(job_exports);
28
29
  var import_sha2 = require("@noble/hashes/sha2");
29
30
  var import_viem = require("viem");
30
31
  var import_interface = require("../ecies/interface");
32
+ var import_jobs = require("../../protocol/jobs");
31
33
  var import_encoding = require("../../utils/encoding");
32
34
  const textDecoder = new TextDecoder();
33
35
  const textEncoder = new TextEncoder();
@@ -37,25 +39,30 @@ class JobEnvelopeError extends Error {
37
39
  this.name = "JobEnvelopeError";
38
40
  }
39
41
  }
42
+ function sortJsonKeys(value) {
43
+ if (Array.isArray(value)) {
44
+ return value.map(sortJsonKeys);
45
+ }
46
+ if (value !== null && typeof value === "object") {
47
+ return Object.fromEntries(
48
+ Object.keys(value).sort().map((key) => [
49
+ key,
50
+ sortJsonKeys(value[key])
51
+ ])
52
+ );
53
+ }
54
+ return value;
55
+ }
56
+ function canonicalJsonBytes(value) {
57
+ return textEncoder.encode(JSON.stringify(sortJsonKeys(value)));
58
+ }
59
+ function canonicalJobRequestBytes(request) {
60
+ validateJobRequest(request);
61
+ return canonicalJsonBytes(request);
62
+ }
40
63
  function requestPlaintext(envelope) {
41
- const { request, auth } = envelope;
42
- return textEncoder.encode(
43
- JSON.stringify({
44
- request: {
45
- v: request.v,
46
- jobId: request.jobId,
47
- owner: request.owner,
48
- builder: request.builder,
49
- builderPublicKey: request.builderPublicKey,
50
- grantId: request.grantId,
51
- scope: request.scope,
52
- operation: request.operation,
53
- pinnedVersion: request.pinnedVersion,
54
- deadline: request.deadline
55
- },
56
- auth
57
- })
58
- );
64
+ validateRequestEnvelope(envelope);
65
+ return canonicalJsonBytes(envelope);
59
66
  }
60
67
  function resultPlaintext(result) {
61
68
  return textEncoder.encode(
@@ -87,42 +94,99 @@ function parseObject(plaintext, kind) {
87
94
  throw new JobEnvelopeError(`Invalid ${kind}: malformed JSON`);
88
95
  }
89
96
  }
97
+ function isPlainObject(value) {
98
+ if (value === null || typeof value !== "object") return false;
99
+ const prototype = Object.getPrototypeOf(value);
100
+ return prototype === Object.prototype || prototype === null;
101
+ }
102
+ function requirePlainObject(value, field, kind) {
103
+ if (!isPlainObject(value)) {
104
+ throw new JobEnvelopeError(`Invalid ${kind}: invalid ${field}`);
105
+ }
106
+ }
107
+ function requireExactKeys(value, expectedKeys, kind) {
108
+ for (const key of expectedKeys) {
109
+ if (!Object.hasOwn(value, key) || value[key] === void 0) {
110
+ throw new JobEnvelopeError(`Invalid ${kind}: missing ${key}`);
111
+ }
112
+ }
113
+ const expected = new Set(expectedKeys);
114
+ for (const key of Reflect.ownKeys(value)) {
115
+ if (!expected.has(key)) {
116
+ throw new JobEnvelopeError(
117
+ `Invalid ${kind}: unknown field ${String(key)}`
118
+ );
119
+ }
120
+ }
121
+ }
90
122
  function requireString(value, field, kind) {
91
123
  if (typeof value !== "string" || value.length === 0) {
92
124
  throw new JobEnvelopeError(`Invalid ${kind}: missing ${field}`);
93
125
  }
94
126
  }
95
- function validateRequestEnvelope(value) {
96
- const request = value.request;
97
- if (request === null || typeof request !== "object" || Array.isArray(request)) {
98
- throw new JobEnvelopeError("Invalid job request envelope: missing request");
99
- }
100
- const fields = request;
101
- if (fields.v !== 1) {
127
+ function validateJobRequest(value) {
128
+ requirePlainObject(value, "request", "job request");
129
+ requireExactKeys(
130
+ value,
131
+ [
132
+ "v",
133
+ "jobId",
134
+ "owner",
135
+ "builder",
136
+ "builderPublicKey",
137
+ "grantId",
138
+ "scope",
139
+ "operation",
140
+ "pinnedVersion",
141
+ "deadline"
142
+ ],
143
+ "job request"
144
+ );
145
+ if (value.v !== import_jobs.JOB_PROTOCOL_VERSION) {
102
146
  throw new JobEnvelopeError(
103
- `Unsupported job request version: ${String(fields.v)}`
147
+ `Unsupported job request version: ${String(value.v)}`
104
148
  );
105
149
  }
106
- for (const field of [
107
- "jobId",
108
- "owner",
109
- "builder",
110
- "builderPublicKey",
111
- "grantId",
112
- "scope",
113
- "operation",
114
- "deadline"
115
- ]) {
116
- requireString(fields[field], field, "job request");
117
- }
118
- if (fields.pinnedVersion !== null && typeof fields.pinnedVersion !== "string") {
150
+ requireString(value.jobId, "jobId", "job request");
151
+ if (typeof value.owner !== "string" || !(0, import_viem.isAddress)(value.owner)) {
152
+ throw new JobEnvelopeError("Invalid job request: invalid owner");
153
+ }
154
+ if (typeof value.builder !== "string" || !(0, import_viem.isAddress)(value.builder)) {
155
+ throw new JobEnvelopeError("Invalid job request: invalid builder");
156
+ }
157
+ if (typeof value.builderPublicKey !== "string" || !(0, import_viem.isHex)(value.builderPublicKey)) {
158
+ throw new JobEnvelopeError("Invalid job request: invalid builderPublicKey");
159
+ }
160
+ if (typeof value.grantId !== "string" || !(0, import_viem.isHex)(value.grantId)) {
161
+ throw new JobEnvelopeError("Invalid job request: invalid grantId");
162
+ }
163
+ requireString(value.scope, "scope", "job request");
164
+ if (!import_jobs.JOB_OPERATIONS.includes(value.operation)) {
165
+ throw new JobEnvelopeError("Invalid job request: invalid operation");
166
+ }
167
+ if (value.pinnedVersion !== null && typeof value.pinnedVersion !== "string") {
119
168
  throw new JobEnvelopeError("Invalid job request: missing pinnedVersion");
120
169
  }
170
+ if (typeof value.deadline !== "string" || !Number.isFinite(Date.parse(value.deadline))) {
171
+ throw new JobEnvelopeError("Invalid job request: invalid deadline");
172
+ }
173
+ return value;
174
+ }
175
+ function validateRequestEnvelope(value) {
176
+ requirePlainObject(value, "envelope", "job request envelope");
177
+ requireExactKeys(value, ["request", "auth"], "job request envelope");
178
+ validateJobRequest(value.request);
121
179
  requireString(value.auth, "auth", "job request envelope");
122
180
  return value;
123
181
  }
124
182
  function validateResult(value) {
125
- if (value.v !== 1) {
183
+ requirePlainObject(value, "result", "job result");
184
+ requireExactKeys(
185
+ value,
186
+ ["v", "jobId", "scope", "version", "contentType", "body"],
187
+ "job result"
188
+ );
189
+ if (value.v !== import_jobs.JOB_PROTOCOL_VERSION) {
126
190
  throw new JobEnvelopeError(
127
191
  `Unsupported job result version: ${String(value.v)}`
128
192
  );
@@ -138,10 +202,10 @@ function validateResult(value) {
138
202
  }
139
203
  return value;
140
204
  }
141
- async function sealJobRequest(e, enclavePublicKey, ecies) {
205
+ async function sealJobRequest(envelope, enclavePublicKey, ecies) {
142
206
  const encrypted = await ecies.encrypt(
143
207
  (0, import_viem.fromHex)(enclavePublicKey, "bytes"),
144
- requestPlaintext(e)
208
+ requestPlaintext(envelope)
145
209
  );
146
210
  return encryptedBytesToBase64(encrypted);
147
211
  }
@@ -154,10 +218,11 @@ async function openJobRequest(ciphertext, privateKey, ecies) {
154
218
  parseObject(plaintext, "job request envelope")
155
219
  );
156
220
  }
157
- async function sealJobResult(r, builderPublicKey, ecies) {
221
+ async function sealJobResult(result, builderPublicKey, ecies) {
222
+ validateResult(result);
158
223
  const encrypted = await ecies.encrypt(
159
224
  (0, import_viem.fromHex)(builderPublicKey, "bytes"),
160
- resultPlaintext(r)
225
+ resultPlaintext(result)
161
226
  );
162
227
  const ciphertext = encryptedBytesToBase64(encrypted);
163
228
  const bytes = (0, import_encoding.fromBase64)(ciphertext);
@@ -189,6 +254,7 @@ async function openJobResult(ciphertext, builderPrivateKey, ecies, expect) {
189
254
  // Annotate the CommonJS export names for ESM import in node:
190
255
  0 && (module.exports = {
191
256
  JobEnvelopeError,
257
+ canonicalJobRequestBytes,
192
258
  openJobRequest,
193
259
  openJobResult,
194
260
  sealJobRequest,
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/crypto/envelope/job.ts"],"sourcesContent":["/**\n * ECIES envelopes for encrypted job requests and results.\n *\n * Plaintext is the UTF-8 encoding of `JSON.stringify(value)`, with properties\n * emitted in the order declared by the corresponding interface. The wire\n * ciphertext is base64 of `iv || ephemPub || ct || mac`, as specified by the\n * ECIES provider interface. This ordering is canonical enough for a plaintext\n * hash, although the Gateway hashes the raw ciphertext bytes, not plaintext.\n * Per the jobs flow in contract section 1, the builder verifies the decrypted\n * result's job ID, scope, and version bindings.\n *\n * @category Cryptography\n */\n\nimport { sha256 } from \"@noble/hashes/sha2\";\nimport { bytesToHex, fromHex, toHex, type Hex } from \"viem\";\nimport {\n deserializeECIES,\n serializeECIES,\n type ECIESProvider,\n} from \"../ecies/interface\";\nimport type { JobRequestEnvelope, JobResult } from \"../../protocol/jobs\";\nimport { fromBase64, toBase64 } from \"../../utils/encoding\";\n\nconst textDecoder = new TextDecoder();\nconst textEncoder = new TextEncoder();\n\n/** A decrypted job envelope did not match the jobs protocol. */\nexport class JobEnvelopeError extends Error {\n constructor(message: string) {\n super(message);\n this.name = \"JobEnvelopeError\";\n }\n}\n\nfunction requestPlaintext(envelope: JobRequestEnvelope): Uint8Array {\n const { request, auth } = envelope;\n return textEncoder.encode(\n JSON.stringify({\n request: {\n v: request.v,\n jobId: request.jobId,\n owner: request.owner,\n builder: request.builder,\n builderPublicKey: request.builderPublicKey,\n grantId: request.grantId,\n scope: request.scope,\n operation: request.operation,\n pinnedVersion: request.pinnedVersion,\n deadline: request.deadline,\n },\n auth,\n }),\n );\n}\n\nfunction resultPlaintext(result: JobResult): Uint8Array {\n return textEncoder.encode(\n JSON.stringify({\n v: result.v,\n jobId: result.jobId,\n scope: result.scope,\n version: result.version,\n contentType: result.contentType,\n body: result.body,\n }),\n );\n}\n\nfunction encryptedBytesToBase64(\n encrypted: Awaited<ReturnType<ECIESProvider[\"encrypt\"]>>,\n): string {\n return toBase64(fromHex(`0x${serializeECIES(encrypted)}`, \"bytes\"));\n}\n\nfunction base64ToEncrypted(ciphertext: string) {\n return deserializeECIES(toHex(fromBase64(ciphertext)));\n}\n\nfunction parseObject(\n plaintext: Uint8Array,\n kind: string,\n): Record<string, unknown> {\n try {\n const value: unknown = JSON.parse(textDecoder.decode(plaintext));\n if (value === null || typeof value !== \"object\" || Array.isArray(value)) {\n throw new JobEnvelopeError(`Invalid ${kind}: expected an object`);\n }\n return value as Record<string, unknown>;\n } catch (error) {\n if (error instanceof JobEnvelopeError) throw error;\n throw new JobEnvelopeError(`Invalid ${kind}: malformed JSON`);\n }\n}\n\nfunction requireString(\n value: unknown,\n field: string,\n kind: string,\n): asserts value is string {\n if (typeof value !== \"string\" || value.length === 0) {\n throw new JobEnvelopeError(`Invalid ${kind}: missing ${field}`);\n }\n}\n\nfunction validateRequestEnvelope(\n value: Record<string, unknown>,\n): JobRequestEnvelope {\n const request = value.request;\n if (\n request === null ||\n typeof request !== \"object\" ||\n Array.isArray(request)\n ) {\n throw new JobEnvelopeError(\"Invalid job request envelope: missing request\");\n }\n const fields = request as Record<string, unknown>;\n if (fields.v !== 1) {\n throw new JobEnvelopeError(\n `Unsupported job request version: ${String(fields.v)}`,\n );\n }\n for (const field of [\n \"jobId\",\n \"owner\",\n \"builder\",\n \"builderPublicKey\",\n \"grantId\",\n \"scope\",\n \"operation\",\n \"deadline\",\n ]) {\n requireString(fields[field], field, \"job request\");\n }\n if (\n fields.pinnedVersion !== null &&\n typeof fields.pinnedVersion !== \"string\"\n ) {\n throw new JobEnvelopeError(\"Invalid job request: missing pinnedVersion\");\n }\n requireString(value.auth, \"auth\", \"job request envelope\");\n return value as unknown as JobRequestEnvelope;\n}\n\nfunction validateResult(value: Record<string, unknown>): JobResult {\n if (value.v !== 1) {\n throw new JobEnvelopeError(\n `Unsupported job result version: ${String(value.v)}`,\n );\n }\n for (const field of [\"jobId\", \"scope\", \"contentType\"]) {\n requireString(value[field], field, \"job result\");\n }\n if (typeof value.body !== \"string\") {\n throw new JobEnvelopeError(\"Invalid job result: missing body\");\n }\n if (value.version !== null && typeof value.version !== \"string\") {\n throw new JobEnvelopeError(\"Invalid job result: missing version\");\n }\n return value as unknown as JobResult;\n}\n\nexport async function sealJobRequest(\n e: JobRequestEnvelope,\n enclavePublicKey: Hex,\n ecies: ECIESProvider,\n): Promise<string> {\n const encrypted = await ecies.encrypt(\n fromHex(enclavePublicKey, \"bytes\"),\n requestPlaintext(e),\n );\n return encryptedBytesToBase64(encrypted);\n}\n\nexport async function openJobRequest(\n ciphertext: string,\n privateKey: Uint8Array,\n ecies: ECIESProvider,\n): Promise<JobRequestEnvelope> {\n const plaintext = await ecies.decrypt(\n privateKey,\n base64ToEncrypted(ciphertext),\n );\n return validateRequestEnvelope(\n parseObject(plaintext, \"job request envelope\"),\n );\n}\n\nexport async function sealJobResult(\n r: JobResult,\n builderPublicKey: Hex,\n ecies: ECIESProvider,\n): Promise<{ ciphertext: string; hash: Hex; size: number }> {\n const encrypted = await ecies.encrypt(\n fromHex(builderPublicKey, \"bytes\"),\n resultPlaintext(r),\n );\n const ciphertext = encryptedBytesToBase64(encrypted);\n const bytes = fromBase64(ciphertext);\n return { ciphertext, hash: bytesToHex(sha256(bytes)), size: bytes.length };\n}\n\nexport async function openJobResult(\n ciphertext: string,\n builderPrivateKey: Hex,\n ecies: ECIESProvider,\n expect: { jobId: string; scope?: string; version?: string | null },\n): Promise<JobResult> {\n const plaintext = await ecies.decrypt(\n fromHex(builderPrivateKey, \"bytes\"),\n base64ToEncrypted(ciphertext),\n );\n const result = validateResult(parseObject(plaintext, \"job result\"));\n if (result.jobId !== expect.jobId) {\n throw new JobEnvelopeError(\n `Job result ID ${result.jobId} does not match expected job ID ${expect.jobId}`,\n );\n }\n if (expect.scope !== undefined && result.scope !== expect.scope) {\n throw new JobEnvelopeError(\n `Job result scope ${result.scope} does not match expected scope ${expect.scope}`,\n );\n }\n if (expect.version !== undefined && result.version !== expect.version) {\n throw new JobEnvelopeError(\n `Job result version ${String(result.version)}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B,OAAO,KAAK,mCAAmC,OAAO,KAAK;AAAA,IAC9E;AAAA,EACF;AACA,MAAI,OAAO,UAAU,UAAa,OAAO,UAAU,OAAO,OAAO;AAC/D,UAAM,IAAI;AAAA,MACR,oBAAoB,OAAO,KAAK,kCAAkC,OAAO,KAAK;AAAA,IAChF;AAAA,EACF;AACA,MAAI,OAAO,YAAY,UAAa,OAAO,YAAY,OAAO,SAAS;AACrE,UAAM,IAAI;AAAA,MACR,sBAAsB,OAAO,OAAO,OAAO,CAAC,oCAAoC,OAAO,OAAO,OAAO,CAAC;AAAA,IACxG;AAAA,EACF;AACA,SAAO;AACT;","names":[]}
1
+ {"version":3,"sources":["../../../src/crypto/envelope/job.ts"],"sourcesContent":["/**\n * ECIES envelopes for encrypted job requests and results.\n *\n * Request plaintext is UTF-8 JSON with object keys sorted recursively and\n * array order preserved. Result properties follow their interface order. The\n * wire ciphertext is base64 of `iv || ephemPub || ct || mac`, as specified by\n * the ECIES provider interface. The Gateway hashes raw ciphertext bytes, not\n * plaintext.\n * The builder verifies the decrypted result's job ID, scope, and version\n * bindings. The PS worker verifies\n * `auth.bodyHash === sha256(canonicalJobRequestBytes(request))`; the Gateway\n * never sees the plaintext. Flow: personal-server-ts\n * `docs/260903-jobs-contract.md`, section 1.\n *\n * @category Cryptography\n */\n\nimport { sha256 } from \"@noble/hashes/sha2\";\nimport { bytesToHex, fromHex, isAddress, isHex, toHex, type Hex } from \"viem\";\nimport {\n deserializeECIES,\n serializeECIES,\n type ECIESProvider,\n} from \"../ecies/interface\";\nimport {\n JOB_OPERATIONS,\n JOB_PROTOCOL_VERSION,\n type JobOperation,\n type JobRequest,\n type JobRequestEnvelope,\n type JobResult,\n} from \"../../protocol/jobs\";\nimport { fromBase64, toBase64 } from \"../../utils/encoding\";\n\nconst textDecoder = new TextDecoder();\nconst textEncoder = new TextEncoder();\n\n/** A job envelope or result did not match the jobs protocol. */\nexport class JobEnvelopeError extends Error {\n constructor(message: string) {\n super(message);\n this.name = \"JobEnvelopeError\";\n }\n}\n\nfunction sortJsonKeys(value: unknown): unknown {\n if (Array.isArray(value)) {\n return value.map(sortJsonKeys);\n }\n if (value !== null && typeof value === \"object\") {\n return Object.fromEntries(\n Object.keys(value)\n .sort()\n .map((key) => [\n key,\n sortJsonKeys((value as Record<string, unknown>)[key]),\n ]),\n );\n }\n return value;\n}\n\nfunction canonicalJsonBytes(value: unknown): Uint8Array {\n return textEncoder.encode(JSON.stringify(sortJsonKeys(value)));\n}\n\n/**\n * Returns the canonical UTF-8 JSON bytes committed to by the auth body hash.\n *\n * @param request - Job request to validate and serialize canonically.\n * @returns Recursively key-sorted, whitespace-free UTF-8 JSON bytes.\n * @throws {JobEnvelopeError} If the request does not match the protocol schema.\n */\nexport function canonicalJobRequestBytes(request: JobRequest): Uint8Array {\n validateJobRequest(request);\n return canonicalJsonBytes(request);\n}\n\nfunction requestPlaintext(envelope: JobRequestEnvelope): Uint8Array {\n validateRequestEnvelope(envelope);\n return canonicalJsonBytes(envelope);\n}\n\nfunction resultPlaintext(result: JobResult): Uint8Array {\n return textEncoder.encode(\n JSON.stringify({\n v: result.v,\n jobId: result.jobId,\n scope: result.scope,\n version: result.version,\n contentType: result.contentType,\n body: result.body,\n }),\n );\n}\n\nfunction encryptedBytesToBase64(\n encrypted: Awaited<ReturnType<ECIESProvider[\"encrypt\"]>>,\n): string {\n return toBase64(fromHex(`0x${serializeECIES(encrypted)}`, \"bytes\"));\n}\n\nfunction base64ToEncrypted(ciphertext: string) {\n return deserializeECIES(toHex(fromBase64(ciphertext)));\n}\n\nfunction parseObject(\n plaintext: Uint8Array,\n kind: string,\n): Record<string, unknown> {\n try {\n const value: unknown = JSON.parse(textDecoder.decode(plaintext));\n if (value === null || typeof value !== \"object\" || Array.isArray(value)) {\n throw new JobEnvelopeError(`Invalid ${kind}: expected an object`);\n }\n return value as Record<string, unknown>;\n } catch (error) {\n if (error instanceof JobEnvelopeError) throw error;\n throw new JobEnvelopeError(`Invalid ${kind}: malformed JSON`);\n }\n}\n\nfunction isPlainObject(value: unknown): value is Record<string, unknown> {\n if (value === null || typeof value !== \"object\") return false;\n const prototype = Object.getPrototypeOf(value);\n return prototype === Object.prototype || prototype === null;\n}\n\nfunction requirePlainObject(\n value: unknown,\n field: string,\n kind: string,\n): asserts value is Record<string, unknown> {\n if (!isPlainObject(value)) {\n throw new JobEnvelopeError(`Invalid ${kind}: invalid ${field}`);\n }\n}\n\nfunction requireExactKeys(\n value: Record<string, unknown>,\n expectedKeys: readonly string[],\n kind: string,\n): void {\n for (const key of expectedKeys) {\n if (!Object.hasOwn(value, key) || value[key] === undefined) {\n throw new JobEnvelopeError(`Invalid ${kind}: missing ${key}`);\n }\n }\n const expected = new Set<PropertyKey>(expectedKeys);\n for (const key of Reflect.ownKeys(value)) {\n if (!expected.has(key)) {\n throw new JobEnvelopeError(\n `Invalid ${kind}: unknown field ${String(key)}`,\n );\n }\n }\n}\n\nfunction requireString(\n value: unknown,\n field: string,\n kind: string,\n): asserts value is string {\n if (typeof value !== \"string\" || value.length === 0) {\n throw new JobEnvelopeError(`Invalid ${kind}: missing ${field}`);\n }\n}\n\nfunction validateJobRequest(value: unknown): JobRequest {\n requirePlainObject(value, \"request\", \"job request\");\n requireExactKeys(\n value,\n [\n \"v\",\n \"jobId\",\n \"owner\",\n \"builder\",\n \"builderPublicKey\",\n \"grantId\",\n \"scope\",\n \"operation\",\n \"pinnedVersion\",\n \"deadline\",\n ],\n \"job request\",\n );\n if (value.v !== JOB_PROTOCOL_VERSION) {\n throw new JobEnvelopeError(\n `Unsupported job request version: ${String(value.v)}`,\n );\n }\n requireString(value.jobId, \"jobId\", \"job request\");\n if (typeof value.owner !== \"string\" || !isAddress(value.owner)) {\n throw new JobEnvelopeError(\"Invalid job request: invalid owner\");\n }\n if (typeof value.builder !== \"string\" || !isAddress(value.builder)) {\n throw new JobEnvelopeError(\"Invalid job request: invalid builder\");\n }\n if (\n typeof value.builderPublicKey !== \"string\" ||\n !isHex(value.builderPublicKey)\n ) {\n throw new JobEnvelopeError(\"Invalid job request: invalid builderPublicKey\");\n }\n if (typeof value.grantId !== \"string\" || !isHex(value.grantId)) {\n throw new JobEnvelopeError(\"Invalid job request: invalid grantId\");\n }\n requireString(value.scope, \"scope\", \"job request\");\n if (!JOB_OPERATIONS.includes(value.operation as JobOperation)) {\n throw new JobEnvelopeError(\"Invalid job request: invalid operation\");\n }\n if (value.pinnedVersion !== null && typeof value.pinnedVersion !== \"string\") {\n throw new JobEnvelopeError(\"Invalid job request: missing pinnedVersion\");\n }\n if (\n typeof value.deadline !== \"string\" ||\n !Number.isFinite(Date.parse(value.deadline))\n ) {\n throw new JobEnvelopeError(\"Invalid job request: invalid deadline\");\n }\n return value as unknown as JobRequest;\n}\n\nfunction validateRequestEnvelope(value: unknown): JobRequestEnvelope {\n requirePlainObject(value, \"envelope\", \"job request envelope\");\n requireExactKeys(value, [\"request\", \"auth\"], \"job request envelope\");\n validateJobRequest(value.request);\n requireString(value.auth, \"auth\", \"job request envelope\");\n return value as unknown as JobRequestEnvelope;\n}\n\nfunction validateResult(value: unknown): JobResult {\n requirePlainObject(value, \"result\", \"job result\");\n requireExactKeys(\n value,\n [\"v\", \"jobId\", \"scope\", \"version\", \"contentType\", \"body\"],\n \"job result\",\n );\n if (value.v !== JOB_PROTOCOL_VERSION) {\n throw new JobEnvelopeError(\n `Unsupported job result version: ${String(value.v)}`,\n );\n }\n for (const field of [\"jobId\", \"scope\", \"contentType\"]) {\n requireString(value[field], field, \"job result\");\n }\n if (typeof value.body !== \"string\") {\n throw new JobEnvelopeError(\"Invalid job result: missing body\");\n }\n if (value.version !== null && typeof value.version !== \"string\") {\n throw new JobEnvelopeError(\"Invalid job result: missing version\");\n }\n return value as unknown as JobResult;\n}\n\n/**\n * Encrypts a validated job request envelope for a Personal Server enclave.\n *\n * The PS worker verifies\n * `auth.bodyHash === sha256(canonicalJobRequestBytes(request))`; the Gateway\n * never sees the plaintext.\n *\n * @param envelope - Request and builder Web3Signed authorization to encrypt.\n * @param enclavePublicKey - Public key returned by `GET /v1/identity?owner=`.\n * @param ecies - Injected ECIES implementation.\n * @returns Base64 ciphertext encoded as `iv || ephemPub || ct || mac`.\n * @throws {JobEnvelopeError} If the envelope or request is invalid.\n * @throws If ECIES encryption fails or the enclave public key is invalid.\n *\n * @example\n * ```ts\n * const identity = await fetch(`/v1/identity?owner=${owner}`).then((response) =>\n * response.json(),\n * );\n * const requestCiphertext = await sealJobRequest(\n * requestEnvelope,\n * identity.publicKey,\n * ecies,\n * );\n * await fetch(\"/v1/jobs\", {\n * method: \"POST\",\n * body: JSON.stringify({ ...submission, requestCiphertext }),\n * });\n * ```\n */\nexport async function sealJobRequest(\n envelope: JobRequestEnvelope,\n enclavePublicKey: Hex,\n ecies: ECIESProvider,\n): Promise<string> {\n const encrypted = await ecies.encrypt(\n fromHex(enclavePublicKey, \"bytes\"),\n requestPlaintext(envelope),\n );\n return encryptedBytesToBase64(encrypted);\n}\n\n/**\n * Decrypts and validates a job request envelope inside the enclave.\n *\n * @param ciphertext - Base64 `iv || ephemPub || ct || mac` ciphertext.\n * @param privateKey - Enclave key bytes, supplied as `Uint8Array` so the agent can zero them after use.\n * @param ecies - Injected ECIES implementation.\n * @returns The validated request envelope exactly as parsed from plaintext.\n * @throws {JobEnvelopeError} If plaintext is malformed or fails schema validation.\n * @throws If ciphertext decoding or ECIES decryption fails.\n */\nexport async function openJobRequest(\n ciphertext: string,\n privateKey: Uint8Array,\n ecies: ECIESProvider,\n): Promise<JobRequestEnvelope> {\n const plaintext = await ecies.decrypt(\n privateKey,\n base64ToEncrypted(ciphertext),\n );\n return validateRequestEnvelope(\n parseObject(plaintext, \"job request envelope\"),\n );\n}\n\n/**\n * Encrypts a validated job result for its builder and describes the ciphertext.\n *\n * `hash` is lowercase `0x` SHA-256 of decoded ciphertext bytes, not the\n * `sha256:` prefix used by Web3Signed `bodyHash`. `size` is that decoded byte\n * length. They equal the Gateway's `resultHash` and `resultSize`.\n *\n * @param result - Job result to validate and encrypt.\n * @param builderPublicKey - Builder wallet public key recorded in the request.\n * @param ecies - Injected ECIES implementation.\n * @returns Base64 ciphertext plus its Gateway-compatible hash and size.\n * @throws {JobEnvelopeError} If the result does not match the protocol schema.\n * @throws If ECIES encryption fails or the builder public key is invalid.\n */\nexport async function sealJobResult(\n result: JobResult,\n builderPublicKey: Hex,\n ecies: ECIESProvider,\n): Promise<{ ciphertext: string; hash: Hex; size: number }> {\n validateResult(result);\n const encrypted = await ecies.encrypt(\n fromHex(builderPublicKey, \"bytes\"),\n resultPlaintext(result),\n );\n const ciphertext = encryptedBytesToBase64(encrypted);\n const bytes = fromBase64(ciphertext);\n return { ciphertext, hash: bytesToHex(sha256(bytes)), size: bytes.length };\n}\n\n/**\n * Decrypts a job result and verifies its builder-visible protocol bindings.\n *\n * The builder private key is a `Hex` wallet key. For `expect.version`,\n * `undefined` skips the check while `null` requires a null result version.\n *\n * @param ciphertext - Base64 `iv || ephemPub || ct || mac` ciphertext.\n * @param builderPrivateKey - Builder's wallet private key as hex.\n * @param ecies - Injected ECIES implementation.\n * @param expect - Required job ID and optional scope and version bindings.\n * @returns The validated result when every supplied binding matches.\n * @throws {JobEnvelopeError} If plaintext is malformed, invalid, or a binding differs.\n * @throws If ciphertext decoding or ECIES decryption fails.\n *\n * @example\n * ```ts\n * const result = await openJobResult(\n * status.resultCiphertext,\n * key,\n * ecies,\n * { jobId, scope },\n * );\n * const body = fromBase64(result.body);\n * ```\n */\nexport async function openJobResult(\n ciphertext: string,\n builderPrivateKey: Hex,\n ecies: ECIESProvider,\n expect: { jobId: string; scope?: string; version?: string | null },\n): Promise<JobResult> {\n const plaintext = await ecies.decrypt(\n fromHex(builderPrivateKey, \"bytes\"),\n base64ToEncrypted(ciphertext),\n );\n const result = validateResult(parseObject(plaintext, \"job result\"));\n if (result.jobId !== expect.jobId) {\n throw new JobEnvelopeError(\n `Job result ID ${result.jobId} does not match expected job ID ${expect.jobId}`,\n );\n }\n if (expect.scope !== undefined && result.scope !== expect.scope) {\n throw new JobEnvelopeError(\n `Job result scope ${result.scope} does not match expected scope ${expect.scope}`,\n );\n }\n if (expect.version !== undefined && result.version !== expect.version) {\n throw new JobEnvelopeError(\n `Job result version ${String(result.version)} does not match expected version ${String(expect.version)}`,\n );\n }\n return result;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAiBA,kBAAuB;AACvB,kBAAuE;AACvE,uBAIO;AACP,kBAOO;AACP,sBAAqC;AAErC,MAAM,cAAc,IAAI,YAAY;AACpC,MAAM,cAAc,IAAI,YAAY;AAG7B,MAAM,yBAAyB,MAAM;AAAA,EAC1C,YAAY,SAAiB;AAC3B,UAAM,OAAO;AACb,SAAK,OAAO;AAAA,EACd;AACF;AAEA,SAAS,aAAa,OAAyB;AAC7C,MAAI,MAAM,QAAQ,KAAK,GAAG;AACxB,WAAO,MAAM,IAAI,YAAY;AAAA,EAC/B;AACA,MAAI,UAAU,QAAQ,OAAO,UAAU,UAAU;AAC/C,WAAO,OAAO;AAAA,MACZ,OAAO,KAAK,KAAK,EACd,KAAK,EACL,IAAI,CAAC,QAAQ;AAAA,QACZ;AAAA,QACA,aAAc,MAAkC,GAAG,CAAC;AAAA,MACtD,CAAC;AAAA,IACL;AAAA,EACF;AACA,SAAO;AACT;AAEA,SAAS,mBAAmB,OAA4B;AACtD,SAAO,YAAY,OAAO,KAAK,UAAU,aAAa,KAAK,CAAC,CAAC;AAC/D;AASO,SAAS,yBAAyB,SAAiC;AACxE,qBAAmB,OAAO;AAC1B,SAAO,mBAAmB,OAAO;AACnC;AAEA,SAAS,iBAAiB,UAA0C;AAClE,0BAAwB,QAAQ;AAChC,SAAO,mBAAmB,QAAQ;AACpC;AAEA,SAAS,gBAAgB,QAA+B;AACtD,SAAO,YAAY;AAAA,IACjB,KAAK,UAAU;AAAA,MACb,GAAG,OAAO;AAAA,MACV,OAAO,OAAO;AAAA,MACd,O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* ECIES envelopes for encrypted job requests and results.
3
3
  *
4
- * Plaintext is the UTF-8 encoding of `JSON.stringify(value)`, with properties
5
- * emitted in the order declared by the corresponding interface. The wire
6
- * ciphertext is base64 of `iv || ephemPub || ct || mac`, as specified by the
7
- * ECIES provider interface. This ordering is canonical enough for a plaintext
8
- * hash, although the Gateway hashes the raw ciphertext bytes, not plaintext.
9
- * Per the jobs flow in contract section 1, the builder verifies the decrypted
10
- * result's job ID, scope, and version bindings.
4
+ * Request plaintext is UTF-8 JSON with object keys sorted recursively and
5
+ * array order preserved. Result properties follow their interface order. The
6
+ * wire ciphertext is base64 of `iv || ephemPub || ct || mac`, as specified by
7
+ * the ECIES provider interface. The Gateway hashes raw ciphertext bytes, not
8
+ * plaintext.
9
+ * The builder verifies the decrypted result's job ID, scope, and version
10
+ * bindings. The PS worker verifies
11
+ * `auth.bodyHash === sha256(canonicalJobRequestBytes(request))`; the Gateway
12
+ * never sees the plaintext. Flow: personal-server-ts
13
+ * `docs/260903-jobs-contract.md`, section 1.
11
14
  *
12
15
  * @category Cryptography
13
16
  */
14
17
  import { type Hex } from "viem";
15
18
  import { type ECIESProvider } from "../ecies/interface.js";
16
- import type { JobRequestEnvelope, JobResult } from "../../protocol/jobs.js";
17
- /** A decrypted job envelope did not match the jobs protocol. */
19
+ import { type JobRequest, type JobRequestEnvelope, type JobResult } from "../../protocol/jobs.js";
20
+ /** A job envelope or result did not match the jobs protocol. */
18
21
  export declare class JobEnvelopeError extends Error {
19
22
  constructor(message: string);
20
23
  }
21
- export declare function sealJobRequest(e: JobRequestEnvelope, enclavePublicKey: Hex, ecies: ECIESProvider): Promise<string>;
24
+ /**
25
+ * Returns the canonical UTF-8 JSON bytes committed to by the auth body hash.
26
+ *
27
+ * @param request - Job request to validate and serialize canonically.
28
+ * @returns Recursively key-sorted, whitespace-free UTF-8 JSON bytes.
29
+ * @throws {JobEnvelopeError} If the request does not match the protocol schema.
30
+ */
31
+ export declare function canonicalJobRequestBytes(request: JobRequest): Uint8Array;
32
+ /**
33
+ * Encrypts a validated job request envelope for a Personal Server enclave.
34
+ *
35
+ * The PS worker verifies
36
+ * `auth.bodyHash === sha256(canonicalJobRequestBytes(request))`; the Gateway
37
+ * never sees the plaintext.
38
+ *
39
+ * @param envelope - Request and builder Web3Signed authorization to encrypt.
40
+ * @param enclavePublicKey - Public key returned by `GET /v1/identity?owner=`.
41
+ * @param ecies - Injected ECIES implementation.
42
+ * @returns Base64 ciphertext encoded as `iv || ephemPub || ct || mac`.
43
+ * @throws {JobEnvelopeError} If the envelope or request is invalid.
44
+ * @throws If ECIES encryption fails or the enclave public key is invalid.
45
+ *
46
+ * @example
47
+ * ```ts
48
+ * const identity = await fetch(`/v1/identity?owner=${owner}`).then((response) =>
49
+ * response.json(),
50
+ * );
51
+ * const requestCiphertext = await sealJobRequest(
52
+ * requestEnvelope,
53
+ * identity.publicKey,
54
+ * ecies,
55
+ * );
56
+ * await fetch("/v1/jobs", {
57
+ * method: "POST",
58
+ * body: JSON.stringify({ ...submission, requestCiphertext }),
59
+ * });
60
+ * ```
61
+ */
62
+ export declare function sealJobRequest(envelope: JobRequestEnvelope, enclavePublicKey: Hex, ecies: ECIESProvider): Promise<string>;
63
+ /**
64
+ * Decrypts and validates a job request envelope inside the enclave.
65
+ *
66
+ * @param ciphertext - Base64 `iv || ephemPub || ct || mac` ciphertext.
67
+ * @param privateKey - Enclave key bytes, supplied as `Uint8Array` so the agent can zero them after use.
68
+ * @param ecies - Injected ECIES implementation.
69
+ * @returns The validated request envelope exactly as parsed from plaintext.
70
+ * @throws {JobEnvelopeError} If plaintext is malformed or fails schema validation.
71
+ * @throws If ciphertext decoding or ECIES decryption fails.
72
+ */
22
73
  export declare function openJobRequest(ciphertext: string, privateKey: Uint8Array, ecies: ECIESProvider): Promise<JobRequestEnvelope>;
23
- export declare function sealJobResult(r: JobResult, builderPublicKey: Hex, ecies: ECIESProvider): Promise<{
74
+ /**
75
+ * Encrypts a validated job result for its builder and describes the ciphertext.
76
+ *
77
+ * `hash` is lowercase `0x` SHA-256 of decoded ciphertext bytes, not the
78
+ * `sha256:` prefix used by Web3Signed `bodyHash`. `size` is that decoded byte
79
+ * length. They equal the Gateway's `resultHash` and `resultSize`.
80
+ *
81
+ * @param result - Job result to validate and encrypt.
82
+ * @param builderPublicKey - Builder wallet public key recorded in the request.
83
+ * @param ecies - Injected ECIES implementation.
84
+ * @returns Base64 ciphertext plus its Gateway-compatible hash and size.
85
+ * @throws {JobEnvelopeError} If the result does not match the protocol schema.
86
+ * @throws If ECIES encryption fails or the builder public key is invalid.
87
+ */
88
+ export declare function sealJobResult(result: JobResult, builderPublicKey: Hex, ecies: ECIESProvider): Promise<{
24
89
  ciphertext: string;
25
90
  hash: Hex;
26
91
  size: number;
27
92
  }>;
93
+ /**
94
+ * Decrypts a job result and verifies its builder-visible protocol bindings.
95
+ *
96
+ * The builder private key is a `Hex` wallet key. For `expect.version`,
97
+ * `undefined` skips the check while `null` requires a null result version.
98
+ *
99
+ * @param ciphertext - Base64 `iv || ephemPub || ct || mac` ciphertext.
100
+ * @param builderPrivateKey - Builder's wallet private key as hex.
101
+ * @param ecies - Injected ECIES implementation.
102
+ * @param expect - Required job ID and optional scope and version bindings.
103
+ * @returns The validated result when every supplied binding matches.
104
+ * @throws {JobEnvelopeError} If plaintext is malformed, invalid, or a binding differs.
105
+ * @throws If ciphertext decoding or ECIES decryption fails.
106
+ *
107
+ * @example
108
+ * ```ts
109
+ * const result = await openJobResult(
110
+ * status.resultCiphertext,
111
+ * key,
112
+ * ecies,
113
+ * { jobId, scope },
114
+ * );
115
+ * const body = fromBase64(result.body);
116
+ * ```
117
+ */
28
118
  export declare function openJobResult(ciphertext: string, builderPrivateKey: Hex, ecies: ECIESProvider, expect: {
29
119
  jobId: string;
30
120
  scope?: string;
@@ -1,9 +1,13 @@
1
1
  import { sha256 } from "@noble/hashes/sha2";
2
- import { bytesToHex, fromHex, toHex } from "viem";
2
+ import { bytesToHex, fromHex, isAddress, isHex, toHex } from "viem";
3
3
  import {
4
4
  deserializeECIES,
5
5
  serializeECIES
6
6
  } from "../ecies/interface.js";
7
+ import {
8
+ JOB_OPERATIONS,
9
+ JOB_PROTOCOL_VERSION
10
+ } from "../../protocol/jobs.js";
7
11
  import { fromBase64, toBase64 } from "../../utils/encoding.js";
8
12
  const textDecoder = new TextDecoder();
9
13
  const textEncoder = new TextEncoder();
@@ -13,25 +17,30 @@ class JobEnvelopeError extends Error {
13
17
  this.name = "JobEnvelopeError";
14
18
  }
15
19
  }
20
+ function sortJsonKeys(value) {
21
+ if (Array.isArray(value)) {
22
+ return value.map(sortJsonKeys);
23
+ }
24
+ if (value !== null && typeof value === "object") {
25
+ return Object.fromEntries(
26
+ Object.keys(value).sort().map((key) => [
27
+ key,
28
+ sortJsonKeys(value[key])
29
+ ])
30
+ );
31
+ }
32
+ return value;
33
+ }
34
+ function canonicalJsonBytes(value) {
35
+ return textEncoder.encode(JSON.stringify(sortJsonKeys(value)));
36
+ }
37
+ function canonicalJobRequestBytes(request) {
38
+ validateJobRequest(request);
39
+ return canonicalJsonBytes(request);
40
+ }
16
41
  function requestPlaintext(envelope) {
17
- const { request, auth } = envelope;
18
- return textEncoder.encode(
19
- JSON.stringify({
20
- request: {
21
- v: request.v,
22
- jobId: request.jobId,
23
- owner: request.owner,
24
- builder: request.builder,
25
- builderPublicKey: request.builderPublicKey,
26
- grantId: request.grantId,
27
- scope: request.scope,
28
- operation: request.operation,
29
- pinnedVersion: request.pinnedVersion,
30
- deadline: request.deadline
31
- },
32
- auth
33
- })
34
- );
42
+ validateRequestEnvelope(envelope);
43
+ return canonicalJsonBytes(envelope);
35
44
  }
36
45
  function resultPlaintext(result) {
37
46
  return textEncoder.encode(
@@ -63,42 +72,99 @@ function parseObject(plaintext, kind) {
63
72
  throw new JobEnvelopeError(`Invalid ${kind}: malformed JSON`);
64
73
  }
65
74
  }
75
+ function isPlainObject(value) {
76
+ if (value === null || typeof value !== "object") return false;
77
+ const prototype = Object.getPrototypeOf(value);
78
+ return prototype === Object.prototype || prototype === null;
79
+ }
80
+ function requirePlainObject(value, field, kind) {
81
+ if (!isPlainObject(value)) {
82
+ throw new JobEnvelopeError(`Invalid ${kind}: invalid ${field}`);
83
+ }
84
+ }
85
+ function requireExactKeys(value, expectedKeys, kind) {
86
+ for (const key of expectedKeys) {
87
+ if (!Object.hasOwn(value, key) || value[key] === void 0) {
88
+ throw new JobEnvelopeError(`Invalid ${kind}: missing ${key}`);
89
+ }
90
+ }
91
+ const expected = new Set(expectedKeys);
92
+ for (const key of Reflect.ownKeys(value)) {
93
+ if (!expected.has(key)) {
94
+ throw new JobEnvelopeError(
95
+ `Invalid ${kind}: unknown field ${String(key)}`
96
+ );
97
+ }
98
+ }
99
+ }
66
100
  function requireString(value, field, kind) {
67
101
  if (typeof value !== "string" || value.length === 0) {
68
102
  throw new JobEnvelopeError(`Invalid ${kind}: missing ${field}`);
69
103
  }
70
104
  }
71
- function validateRequestEnvelope(value) {
72
- const request = value.request;
73
- if (request === null || typeof request !== "object" || Array.isArray(request)) {
74
- throw new JobEnvelopeError("Invalid job request envelope: missing request");
75
- }
76
- const fields = request;
77
- if (fields.v !== 1) {
105
+ function validateJobRequest(value) {
106
+ requirePlainObject(value, "request", "job request");
107
+ requireExactKeys(
108
+ value,
109
+ [
110
+ "v",
111
+ "jobId",
112
+ "owner",
113
+ "builder",
114
+ "builderPublicKey",
115
+ "grantId",
116
+ "scope",
117
+ "operation",
118
+ "pinnedVersion",
119
+ "deadline"
120
+ ],
121
+ "job request"
122
+ );
123
+ if (value.v !== JOB_PROTOCOL_VERSION) {
78
124
  throw new JobEnvelopeError(
79
- `Unsupported job request version: ${String(fields.v)}`
125
+ `Unsupported job request version: ${String(value.v)}`
80
126
  );
81
127
  }
82
- for (const field of [
83
- "jobId",
84
- "owner",
85
- "builder",
86
- "builderPublicKey",
87
- "grantId",
88
- "scope",
89
- "operation",
90
- "deadline"
91
- ]) {
92
- requireString(fields[field], field, "job request");
93
- }
94
- if (fields.pinnedVersion !== null && typeof fields.pinnedVersion !== "string") {
128
+ requireString(value.jobId, "jobId", "job request");
129
+ if (typeof value.owner !== "string" || !isAddress(value.owner)) {
130
+ throw new JobEnvelopeError("Invalid job request: invalid owner");
131
+ }
132
+ if (typeof value.builder !== "string" || !isAddress(value.builder)) {
133
+ throw new JobEnvelopeError("Invalid job request: invalid builder");
134
+ }
135
+ if (typeof value.builderPublicKey !== "string" || !isHex(value.builderPublicKey)) {
136
+ throw new JobEnvelopeError("Invalid job request: invalid builderPublicKey");
137
+ }
138
+ if (typeof value.grantId !== "string" || !isHex(value.grantId)) {
139
+ throw new JobEnvelopeError("Invalid job request: invalid grantId");
140
+ }
141
+ requireString(value.scope, "scope", "job request");
142
+ if (!JOB_OPERATIONS.includes(value.operation)) {
143
+ throw new JobEnvelopeError("Invalid job request: invalid operation");
144
+ }
145
+ if (value.pinnedVersion !== null && typeof value.pinnedVersion !== "string") {
95
146
  throw new JobEnvelopeError("Invalid job request: missing pinnedVersion");
96
147
  }
148
+ if (typeof value.deadline !== "string" || !Number.isFinite(Date.parse(value.deadline))) {
149
+ throw new JobEnvelopeError("Invalid job request: invalid deadline");
150
+ }
151
+ return value;
152
+ }
153
+ function validateRequestEnvelope(value) {
154
+ requirePlainObject(value, "envelope", "job request envelope");
155
+ requireExactKeys(value, ["request", "auth"], "job request envelope");
156
+ validateJobRequest(value.request);
97
157
  requireString(value.auth, "auth", "job request envelope");
98
158
  return value;
99
159
  }
100
160
  function validateResult(value) {
101
- if (value.v !== 1) {
161
+ requirePlainObject(value, "result", "job result");
162
+ requireExactKeys(
163
+ value,
164
+ ["v", "jobId", "scope", "version", "contentType", "body"],
165
+ "job result"
166
+ );
167
+ if (value.v !== JOB_PROTOCOL_VERSION) {
102
168
  throw new JobEnvelopeError(
103
169
  `Unsupported job result version: ${String(value.v)}`
104
170
  );
@@ -114,10 +180,10 @@ function validateResult(value) {
114
180
  }
115
181
  return value;
116
182
  }
117
- async function sealJobRequest(e, enclavePublicKey, ecies) {
183
+ async function sealJobRequest(envelope, enclavePublicKey, ecies) {
118
184
  const encrypted = await ecies.encrypt(
119
185
  fromHex(enclavePublicKey, "bytes"),
120
- requestPlaintext(e)
186
+ requestPlaintext(envelope)
121
187
  );
122
188
  return encryptedBytesToBase64(encrypted);
123
189
  }
@@ -130,10 +196,11 @@ async function openJobRequest(ciphertext, privateKey, ecies) {
130
196
  parseObject(plaintext, "job request envelope")
131
197
  );
132
198
  }
133
- async function sealJobResult(r, builderPublicKey, ecies) {
199
+ async function sealJobResult(result, builderPublicKey, ecies) {
200
+ validateResult(result);
134
201
  const encrypted = await ecies.encrypt(
135
202
  fromHex(builderPublicKey, "bytes"),
136
- resultPlaintext(r)
203
+ resultPlaintext(result)
137
204
  );
138
205
  const ciphertext = encryptedBytesToBase64(encrypted);
139
206
  const bytes = fromBase64(ciphertext);
@@ -164,6 +231,7 @@ async function openJobResult(ciphertext, builderPrivateKey, ecies, expect) {
164
231
  }
165
232
  export {
166
233
  JobEnvelopeError,
234
+ canonicalJobRequestBytes,
167
235
  openJobRequest,
168
236
  openJobResult,
169
237
  sealJobRequest,