space-data-module-sdk 0.8.26 → 0.9.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 +5 -4
- package/docs/assembly.js +0 -18
- package/docs/assets/ai-credits/ai-credits.js +116 -0
- package/docs/assets/sdn-chrome/sdn-chrome.css +434 -0
- package/docs/assets/sdn-chrome/sdn-chrome.js +236 -0
- package/docs/browser-wasmedge-isomorphic.html +29 -41
- package/docs/byo-wasm-quickstart.html +29 -41
- package/docs/conformance.html +29 -41
- package/docs/events-abi.html +29 -41
- package/docs/families/analytics.html +29 -41
- package/docs/families/attitude.html +29 -41
- package/docs/families/behavior.html +29 -41
- package/docs/families/breakup.html +29 -41
- package/docs/families/conjunction.html +29 -41
- package/docs/families/data-source.html +29 -41
- package/docs/families/effects.html +29 -41
- package/docs/families/environment.html +29 -41
- package/docs/families/estimation.html +29 -41
- package/docs/families/gnc.html +29 -41
- package/docs/families/maneuver.html +29 -41
- package/docs/families/obstruction.html +29 -41
- package/docs/families/propagator.html +29 -41
- package/docs/families/propulsion.html +29 -41
- package/docs/families/reentry.html +29 -41
- package/docs/families/rf.html +29 -41
- package/docs/families/scheduler.html +29 -41
- package/docs/families/sensor.html +29 -41
- package/docs/families/signature.html +29 -41
- package/docs/flatsql-host-contract.html +29 -41
- package/docs/flatsql-streaming-standard.html +29 -41
- package/docs/gpu-module-abi.html +29 -41
- package/docs/index.html +43 -47
- package/docs/isomorphic-pthreads.html +29 -41
- package/docs/isomorphic-sdn-runtime-plan.html +29 -41
- package/docs/language-runtime-matrix.html +29 -41
- package/docs/media/module-sdk-reel-poster.webp +0 -0
- package/docs/media/module-sdk-reel.mp4 +0 -0
- package/docs/media/module-sdk-reel.webm +0 -0
- package/docs/media/reel/reel.css +53 -0
- package/docs/media/reel/reel.js +272 -0
- package/docs/module-bundle-runtime-plan.html +29 -41
- package/docs/module-publication-standard.html +29 -41
- package/docs/node-red-default-node-parity.html +29 -41
- package/docs/protect-and-sign.html +29 -41
- package/docs/protocol-installation.html +29 -41
- package/docs/publication-submission.html +29 -41
- package/docs/secrets-capability.html +29 -41
- package/docs/styles.css +89 -157
- package/docs/testing-harness.html +29 -41
- package/docs/tri-runtime-parity-gate.html +29 -41
- package/docs/tri-runtime-parity.html +29 -41
- package/package.json +5 -3
- package/schemas/runtime/FlowNotice.fbs +39 -0
- package/schemas/runtime/HostOps.fbs +453 -0
- package/schemas/runtime/HttpRequest.fbs +25 -0
- package/schemas/runtime/HttpResponse.fbs +24 -0
- package/schemas/runtime/IngestRequest.fbs +58 -0
- package/schemas/runtime/IngestResult.fbs +23 -0
- package/schemas/runtime/ModuleConfig.fbs +41 -0
- package/schemas/runtime/RuntimeCommon.fbs +33 -0
- package/schemas/runtime/TimerTick.fbs +18 -0
- package/src/compiler/compileModule.js +20 -2
- package/src/generated/runtime/aligned/HostOps_aligned.js +3924 -0
- package/src/generated/runtime/aligned/HostOps_aligned.ts +4620 -0
- package/src/generated/runtime/aligned/HostOps_enums.js +14 -0
- package/src/generated/runtime/aligned/HostOps_enums.ts +7 -0
- package/src/generated/runtime/cpp/FlowNotice_generated.h +367 -0
- package/src/generated/runtime/cpp/HostOps_aligned.h +3547 -0
- package/src/generated/runtime/cpp/HttpRequest_generated.h +317 -0
- package/src/generated/runtime/cpp/HttpResponse_generated.h +315 -0
- package/src/generated/runtime/cpp/IngestRequest_generated.h +764 -0
- package/src/generated/runtime/cpp/IngestResult_generated.h +319 -0
- package/src/generated/runtime/cpp/ModuleConfig_generated.h +604 -0
- package/src/generated/runtime/cpp/RuntimeCommon_generated.h +410 -0
- package/src/generated/runtime/cpp/TimerTick_generated.h +229 -0
- package/src/generated/runtime/sdn/runtime/ArchiveTarget.js +78 -0
- package/src/generated/runtime/sdn/runtime/ArchiveTarget.ts +104 -0
- package/src/generated/runtime/sdn/runtime/ConfigEntry.js +202 -0
- package/src/generated/runtime/sdn/runtime/ConfigEntry.ts +251 -0
- package/src/generated/runtime/sdn/runtime/ConfigTable.js +80 -0
- package/src/generated/runtime/sdn/runtime/ConfigTable.ts +97 -0
- package/src/generated/runtime/sdn/runtime/FlowNotice.js +196 -0
- package/src/generated/runtime/sdn/runtime/FlowNotice.ts +254 -0
- package/src/generated/runtime/sdn/runtime/Header.js +79 -0
- package/src/generated/runtime/sdn/runtime/Header.ts +100 -0
- package/src/generated/runtime/sdn/runtime/HttpRequest.js +216 -0
- package/src/generated/runtime/sdn/runtime/HttpRequest.ts +265 -0
- package/src/generated/runtime/sdn/runtime/HttpResponse.js +216 -0
- package/src/generated/runtime/sdn/runtime/HttpResponse.ts +267 -0
- package/src/generated/runtime/sdn/runtime/IngestRequest.js +121 -0
- package/src/generated/runtime/sdn/runtime/IngestRequest.ts +155 -0
- package/src/generated/runtime/sdn/runtime/IngestResult.js +184 -0
- package/src/generated/runtime/sdn/runtime/IngestResult.ts +235 -0
- package/src/generated/runtime/sdn/runtime/ModuleConfig.js +92 -0
- package/src/generated/runtime/sdn/runtime/ModuleConfig.ts +114 -0
- package/src/generated/runtime/sdn/runtime/Provenance.js +288 -0
- package/src/generated/runtime/sdn/runtime/Provenance.ts +368 -0
- package/src/generated/runtime/sdn/runtime/SourceTags.js +181 -0
- package/src/generated/runtime/sdn/runtime/SourceTags.ts +253 -0
- package/src/generated/runtime/sdn/runtime/TimerTick.js +119 -0
- package/src/generated/runtime/sdn/runtime/TimerTick.ts +148 -0
- package/src/generated/runtime/sdn/runtime/configValueKind.js +12 -0
- package/src/generated/runtime/sdn/runtime/configValueKind.ts +19 -0
- package/src/generated/runtime/sdn/runtime/flowNoticeKind.js +11 -0
- package/src/generated/runtime/sdn/runtime/flowNoticeKind.ts +34 -0
- package/src/generated/runtime/sdn/runtime/reconcileMode.js +9 -0
- package/src/generated/runtime/sdn/runtime/reconcileMode.ts +23 -0
- package/src/generated/runtime/sdn/runtime.js +16 -0
- package/src/generated/runtime/sdn/runtime.ts +17 -0
- package/src/host/abi.js +20 -3
- package/src/host/browserHost.js +2 -0
- package/src/host/cpp/typedHostcall.hpp +201 -0
- package/src/host/hostWorkerBundleSources.js +3 -3
- package/src/host/nodeHost.js +1 -0
- package/src/host/typedDispatch.js +230 -0
- package/src/index.d.ts +2 -0
- package/src/invoke/codec.js +3 -2
- package/src/licensing/records.js +10 -1
- package/src/standards/runtimeContracts.js +536 -0
- package/src/standards/sharedCatalog.js +27 -0
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
// The SDK JS hosts' side of the typed hostcall (an SDS $PIV invocation, module -> host; see HOST_OPERATIONS in
|
|
2
|
+
// src/standards/runtimeContracts.js): a decoded call's aligned records, list ports and raw ports become the arguments
|
|
3
|
+
// the host's operation already takes, and its answer becomes the operation's result record, list and raw ports. The
|
|
4
|
+
// hosts' own methods are unchanged; only the wire is typed.
|
|
5
|
+
import {
|
|
6
|
+
HOST_OPERATIONS,
|
|
7
|
+
decodeHostCall,
|
|
8
|
+
encodeHostCallResult,
|
|
9
|
+
} from "../standards/runtimeContracts.js";
|
|
10
|
+
import * as hostOps from "../generated/runtime/aligned/HostOps_aligned.js";
|
|
11
|
+
import { base64ToBytes, bytesToBase64 } from "../utils/encoding.js";
|
|
12
|
+
|
|
13
|
+
const textDecoder = new TextDecoder();
|
|
14
|
+
const textEncoder = new TextEncoder();
|
|
15
|
+
const text = (bytes) => (bytes === undefined ? undefined : textDecoder.decode(bytes));
|
|
16
|
+
const bytesOf = (value) => (value === undefined || value === null ? undefined : value instanceof Uint8Array ? value : typeof value === "string" ? textEncoder.encode(value) : new Uint8Array(value));
|
|
17
|
+
const items = (values) => (values || []).map((v) => ({ VALUE: String(v) }));
|
|
18
|
+
const unix = (value) => BigInt(Math.round(value instanceof Date ? value.getTime() : Number(value)));
|
|
19
|
+
|
|
20
|
+
/** FIELD_NAME -> fieldName */
|
|
21
|
+
const camel = (field) => field.toLowerCase().replace(/_([a-z0-9])/g, (_, c) => c.toUpperCase());
|
|
22
|
+
/** fieldName -> FIELD_NAME */
|
|
23
|
+
const upper = (key) => key.replace(/([a-z0-9])([A-Z])/g, "$1_$2").toUpperCase();
|
|
24
|
+
|
|
25
|
+
/** A record's set fields as the host's camelCase options (zero / empty = not given). */
|
|
26
|
+
function optionsOf(record) {
|
|
27
|
+
const out = {};
|
|
28
|
+
for (const [field, value] of Object.entries(record || {})) {
|
|
29
|
+
if (value === null || value === undefined || value === "" || value === 0 || value === 0n || value === false) continue;
|
|
30
|
+
out[camel(field)] = typeof value === "bigint" ? Number(value) : value;
|
|
31
|
+
}
|
|
32
|
+
return out;
|
|
33
|
+
}
|
|
34
|
+
/** The host's camelCase answer as a record of `type` (fields the record does not have are left out). */
|
|
35
|
+
function recordOf(type, value) {
|
|
36
|
+
if (!type || !value || typeof value !== "object") return null;
|
|
37
|
+
const record = {};
|
|
38
|
+
for (const [key, v] of Object.entries(value)) {
|
|
39
|
+
if (v === null || v === undefined || v instanceof Uint8Array || typeof v === "object") continue;
|
|
40
|
+
record[upper(key)] = v;
|
|
41
|
+
}
|
|
42
|
+
return pick(type, record);
|
|
43
|
+
}
|
|
44
|
+
// Only the fields the record declares (the encoder refuses unknown fields).
|
|
45
|
+
function pick(type, record) {
|
|
46
|
+
const proto = hostOps[type]?.prototype;
|
|
47
|
+
if (!proto) return record;
|
|
48
|
+
return Object.fromEntries(Object.entries(record).filter(([k]) => typeof proto[`set${k}`] === "function"));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** "name: value" lines -> header object, and back. */
|
|
52
|
+
function headersIn(block) {
|
|
53
|
+
if (!block) return undefined;
|
|
54
|
+
const headers = {};
|
|
55
|
+
for (const line of text(block).split(/\r?\n/)) {
|
|
56
|
+
const at = line.indexOf(":");
|
|
57
|
+
if (at > 0) headers[line.slice(0, at).trim()] = line.slice(at + 1).trim();
|
|
58
|
+
}
|
|
59
|
+
return headers;
|
|
60
|
+
}
|
|
61
|
+
function headersOut(headers) {
|
|
62
|
+
if (!headers) return undefined;
|
|
63
|
+
const entries = typeof headers.entries === "function" && !(headers instanceof Map) && !Array.isArray(headers) ? Array.from(headers.entries()) : headers instanceof Map ? Array.from(headers) : Object.entries(headers);
|
|
64
|
+
return entries.map(([k, v]) => `${k}: ${Array.isArray(v) ? v.join(", ") : v}`).join("\r\n");
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const GCM_TAG = 16;
|
|
68
|
+
function splitGcm(data) {
|
|
69
|
+
if (!data || data.length < GCM_TAG) throw Object.assign(new Error("AES-GCM data is shorter than its 16-byte tag"), { hostcallCode: "invalid-params" });
|
|
70
|
+
return { ciphertext: data.subarray(0, data.length - GCM_TAG), tag: data.subarray(data.length - GCM_TAG) };
|
|
71
|
+
}
|
|
72
|
+
const concat = (a, b) => { const out = new Uint8Array(a.length + b.length); out.set(a, 0); out.set(b, a.length); return out; };
|
|
73
|
+
|
|
74
|
+
// Per operation: in(call) -> the host's params; out(value) -> { result, lists, raw }. Operations not listed here pass
|
|
75
|
+
// their record's fields as camelCase options and answer a record from the host's camelCase fields.
|
|
76
|
+
const ADAPTERS = {
|
|
77
|
+
"host.runtimeTarget": { out: (v) => ({ raw: { value: String(v) } }) },
|
|
78
|
+
"host.listCapabilities": { out: (v) => ({ lists: { items: items(v) } }) },
|
|
79
|
+
"host.listSupportedCapabilities": { out: (v) => ({ lists: { items: items(v) } }) },
|
|
80
|
+
"host.listOperations": { out: (v) => ({ lists: { items: items(v) } }) },
|
|
81
|
+
"host.hasCapability": { in: ({ params }) => ({ capability: params?.CAPABILITY }), out: (v) => ({ result: { VALUE: Boolean(v) } }) },
|
|
82
|
+
"clock.now": { out: (v) => ({ result: { UNIX_MS: unix(v) } }) },
|
|
83
|
+
"clock.nowIso": { out: (v) => ({ raw: { value: String(v) } }) },
|
|
84
|
+
"clock.monotonicNow": { out: (v) => ({ result: { VALUE: Number(v) } }) },
|
|
85
|
+
"random.bytes": { in: ({ params }) => ({ length: params?.LENGTH || 32 }), out: (v) => ({ raw: { data: bytesOf(v) } }) },
|
|
86
|
+
"timers.delay": { in: ({ params }) => ({ ms: params?.MS ?? 0 }), out: () => ({}) },
|
|
87
|
+
"schedule.parse": {
|
|
88
|
+
in: ({ params }) => ({ expression: params?.EXPRESSION }),
|
|
89
|
+
out: (v) => ({ lists: { fields: Object.entries(v || {}).filter(([, f]) => f && typeof f === "object" && Array.isArray(f.values)).map(([name, f]) => ({ NAME: name, SOURCE: String(f.source ?? ""), HAS_WILDCARD: Boolean(f.hasWildcard), VALUES: f.values })) } }),
|
|
90
|
+
},
|
|
91
|
+
"schedule.matches": { in: ({ params }) => ({ expression: params?.EXPRESSION, date: params?.AT_MS ? new Date(Number(params.AT_MS)) : new Date() }), out: (v) => ({ result: { VALUE: Boolean(v) } }) },
|
|
92
|
+
"schedule.next": { in: ({ params }) => ({ expression: params?.EXPRESSION, from: params?.AT_MS ? new Date(Number(params.AT_MS)) : new Date() }), out: (v) => ({ result: { UNIX_MS: unix(v) } }) },
|
|
93
|
+
"http.request": {
|
|
94
|
+
in: ({ params, raw }) => ({
|
|
95
|
+
url: text(raw.url), method: params?.METHOD || "GET", headers: headersIn(raw.headers), body: raw.body,
|
|
96
|
+
timeoutMs: params?.TIMEOUT_MS || undefined, responseType: "bytes", redirect: params?.NO_REDIRECTS ? "manual" : "follow",
|
|
97
|
+
maxBytes: params?.MAX_BYTES ? Number(params.MAX_BYTES) : undefined,
|
|
98
|
+
}),
|
|
99
|
+
out: (v, { params }) => {
|
|
100
|
+
const body = bytesOf(v?.body) ?? new Uint8Array(0);
|
|
101
|
+
if (params?.MAX_BYTES && body.length > Number(params.MAX_BYTES)) throw Object.assign(new Error(`HTTP body of ${body.length} bytes exceeds MAX_BYTES ${params.MAX_BYTES}`), { hostcallCode: "too-large" });
|
|
102
|
+
return { result: { STATUS: v?.status ?? 0, RETRIEVED_MS: BigInt(Date.now()) }, raw: { url: String(v?.url ?? ""), headers: headersOut(v?.headers) ?? "", body } };
|
|
103
|
+
},
|
|
104
|
+
},
|
|
105
|
+
"websocket.exchange": {
|
|
106
|
+
in: ({ params, raw }) => ({
|
|
107
|
+
url: text(raw.url), protocols: raw.protocols ? text(raw.protocols).split(",").map((p) => p.trim()).filter(Boolean) : undefined,
|
|
108
|
+
message: raw.message === undefined ? undefined : params?.MESSAGE_IS_TEXT ? text(raw.message) : raw.message,
|
|
109
|
+
timeoutMs: params?.TIMEOUT_MS || undefined, expectResponse: !params?.NO_RESPONSE, responseType: "bytes",
|
|
110
|
+
}),
|
|
111
|
+
out: (v) => ({ result: { PROTOCOL: v?.protocol ?? "", EXTENSIONS: v?.extensions ?? "", CLOSE_CODE: v?.closeCode ?? -1, CLOSE_REASON: v?.closeReason ?? "", BODY_IS_TEXT: typeof v?.body === "string" }, raw: { url: String(v?.url ?? ""), ...(v?.body == null ? {} : { body: bytesOf(v.body) }) } }),
|
|
112
|
+
},
|
|
113
|
+
"mqtt.publish": { in: ({ params, raw }) => ({ ...optionsOf(params), keepAliveSeconds: params?.KEEP_ALIVE_S || undefined, payload: raw.payload, password: text(raw.password) }), out: (v) => ({ result: recordOf("MqttResult", v) }) },
|
|
114
|
+
"mqtt.subscribeOnce": { in: ({ params, raw }) => ({ ...optionsOf(params), keepAliveSeconds: params?.KEEP_ALIVE_S || undefined, password: text(raw.password), responseType: "bytes" }), out: (v) => ({ result: recordOf("MqttResult", v), raw: v?.payload == null && v?.body == null ? {} : { body: bytesOf(v.payload ?? v.body) } }) },
|
|
115
|
+
"tcp.request": socket(),
|
|
116
|
+
"udp.request": socket(),
|
|
117
|
+
"tls.request": socket(({ raw }) => ({ ca: text(raw.ca), cert: text(raw.cert), key: text(raw.key) })),
|
|
118
|
+
"filesystem.resolvePath": { in: ({ raw }) => ({ path: text(raw.path) }), out: (v) => ({ raw: { path: String(v) } }) },
|
|
119
|
+
"filesystem.readFile": { in: ({ raw }) => ({ path: text(raw.path) }), out: (v) => ({ raw: { data: bytesOf(v) } }) },
|
|
120
|
+
"filesystem.writeFile": { in: ({ raw }) => ({ path: text(raw.path), value: raw.data ?? new Uint8Array(0) }), out: () => ({}) },
|
|
121
|
+
"filesystem.appendFile": { in: ({ raw }) => ({ path: text(raw.path), value: raw.data ?? new Uint8Array(0) }), out: () => ({}) },
|
|
122
|
+
"filesystem.deleteFile": { in: ({ raw }) => ({ path: text(raw.path) }), out: () => ({}) },
|
|
123
|
+
"filesystem.mkdir": { in: ({ params, raw }) => ({ path: text(raw.path), recursive: !params?.NOT_RECURSIVE }), out: () => ({}) },
|
|
124
|
+
"filesystem.readdir": { in: ({ raw }) => ({ path: text(raw.path) ?? "." }), out: (v) => ({ lists: { entries: (v || []).map((e) => ({ NAME: e.name, IS_FILE: Boolean(e.isFile), IS_DIRECTORY: Boolean(e.isDirectory) })) } }) },
|
|
125
|
+
"filesystem.stat": { in: ({ raw }) => ({ path: text(raw.path) }), out: (v) => ({ result: { SIZE: BigInt(v?.size ?? 0), IS_FILE: Boolean(v?.isFile), IS_DIRECTORY: Boolean(v?.isDirectory), CTIME_MS: Number(v?.ctimeMs ?? 0), MTIME_MS: Number(v?.mtimeMs ?? 0) }, raw: { path: String(v?.path ?? "") } }) },
|
|
126
|
+
"filesystem.rename": { in: ({ raw }) => ({ fromPath: text(raw.path), toPath: text(raw.to_path) }), out: () => ({}) },
|
|
127
|
+
"exec.execFile": {
|
|
128
|
+
in: ({ params, raw, lists }) => ({
|
|
129
|
+
file: text(raw.file), args: (lists.args || []).map((a) => a.VALUE), cwd: text(raw.cwd), input: raw.input,
|
|
130
|
+
env: lists.env ? Object.fromEntries(lists.env.map((e) => [e.NAME, e.VALUE])) : undefined, timeoutMs: params?.TIMEOUT_MS || undefined,
|
|
131
|
+
}),
|
|
132
|
+
out: (v) => ({ result: { EXIT_CODE: v?.exitCode ?? -1, SIGNAL: v?.signal ?? "" }, raw: { stdout: bytesOf(v?.stdout) ?? new Uint8Array(0), stderr: bytesOf(v?.stderr) ?? new Uint8Array(0) } }),
|
|
133
|
+
},
|
|
134
|
+
"context.get": { in: ({ params }) => ({ scope: params?.SCOPE, key: params?.KEY }), out: (v) => ({ result: { VALUE: v !== undefined && v !== null }, raw: v == null ? {} : { value: bytesOf(v) } }) },
|
|
135
|
+
"context.set": { in: ({ params, raw }) => ({ scope: params?.SCOPE, key: params?.KEY, value: raw.value ?? new Uint8Array(0) }), out: () => ({}) },
|
|
136
|
+
"context.delete": { in: ({ params }) => ({ scope: params?.SCOPE, key: params?.KEY }), out: (v) => ({ result: { VALUE: Boolean(v) } }) },
|
|
137
|
+
"context.listKeys": { in: ({ params }) => ({ scope: params?.SCOPE }), out: (v) => ({ lists: { items: items(v) } }) },
|
|
138
|
+
"context.listScopes": { out: (v) => ({ lists: { items: items(v) } }) },
|
|
139
|
+
// crypto: keys, data, signatures and nonces are raw frames; AES-GCM data carries its 16-byte tag at the end
|
|
140
|
+
"crypto.sha256": { in: ({ raw }) => ({ value: raw.data ?? new Uint8Array(0) }), out: data },
|
|
141
|
+
"crypto.sha512": { in: ({ raw }) => ({ value: raw.data ?? new Uint8Array(0) }), out: data },
|
|
142
|
+
"crypto.hkdf": { in: ({ params, raw }) => ({ ikm: raw.ikm, salt: raw.salt ?? new Uint8Array(0), info: raw.info, length: params?.LENGTH || 32 }), out: data },
|
|
143
|
+
"crypto.aesGcmEncrypt": { in: ({ raw }) => ({ key: raw.key, plaintext: raw.data ?? new Uint8Array(0), iv: raw.nonce, aad: raw.aad }), out: (v, { raw }) => ({ raw: { data: concat(v.ciphertext, v.tag), nonce: raw.nonce } }) },
|
|
144
|
+
"crypto.aesGcmDecrypt": { in: ({ raw }) => ({ key: raw.key, ...splitGcm(raw.data), iv: raw.nonce, aad: raw.aad }), out: data },
|
|
145
|
+
"crypto.x25519.generateKeypair": { out: (v) => ({ raw: { private_key: v.privateKey, public_key: v.publicKey } }) },
|
|
146
|
+
"crypto.x25519.publicKey": { in: ({ raw }) => ({ privateKey: raw.private_key }), out: data },
|
|
147
|
+
"crypto.x25519.sharedSecret": { in: ({ raw }) => ({ privateKey: raw.private_key, publicKey: raw.public_key }), out: data },
|
|
148
|
+
// the sealed box's envelope is its protected blob: ciphertext + tag with the SDS $REC / $ENC records appended
|
|
149
|
+
"crypto.sealedBox.encryptForRecipient": { in: ({ raw }) => ({ plaintext: raw.data ?? new Uint8Array(0), recipientPublicKey: raw.public_key, context: raw.info ? text(raw.info) : undefined }), out: (v) => ({ raw: { data: base64ToBytes(v.protectedBlobBase64) } }) },
|
|
150
|
+
"crypto.sealedBox.decryptFromEnvelope": { in: ({ raw }) => ({ envelope: { protectedBlobBase64: bytesToBase64(raw.envelope ?? raw.data) }, recipientPrivateKey: raw.private_key }), out: data },
|
|
151
|
+
"crypto.secp256k1.publicKeyFromPrivate": { in: ({ raw }) => ({ privateKey: raw.private_key }), out: data },
|
|
152
|
+
"crypto.secp256k1.sign": { in: ({ raw }) => ({ message: raw.data, privateKey: raw.private_key }), out: data },
|
|
153
|
+
"crypto.secp256k1.verify": { in: ({ raw }) => ({ message: raw.data, signature: raw.signature, publicKey: raw.public_key }), out: bool },
|
|
154
|
+
"crypto.secp256k1.signDigest": { in: ({ raw }) => ({ digest: raw.data, privateKey: raw.private_key }), out: data },
|
|
155
|
+
"crypto.secp256k1.verifyDigest": { in: ({ raw }) => ({ digest: raw.data, signature: raw.signature, publicKey: raw.public_key }), out: bool },
|
|
156
|
+
"crypto.ed25519.publicKeyFromSeed": { in: ({ raw }) => ({ seed: raw.private_key }), out: data },
|
|
157
|
+
"crypto.ed25519.sign": { in: ({ raw }) => ({ message: raw.data, seed: raw.private_key }), out: data },
|
|
158
|
+
"crypto.ed25519.verify": { in: ({ raw }) => ({ message: raw.data, signature: raw.signature, publicKey: raw.public_key }), out: bool },
|
|
159
|
+
};
|
|
160
|
+
function data(v) { return { raw: { data: bytesOf(v) ?? new Uint8Array(0) } }; }
|
|
161
|
+
function bool(v) { return { result: { VALUE: Boolean(v) } }; }
|
|
162
|
+
function socket(extra = () => ({})) {
|
|
163
|
+
return {
|
|
164
|
+
in: (call) => {
|
|
165
|
+
const { params, raw } = call;
|
|
166
|
+
const options = optionsOf(params);
|
|
167
|
+
delete options.noResponse; delete options.allowUnauthorized; delete options.serverName;
|
|
168
|
+
return {
|
|
169
|
+
...options, data: raw.data, responseEncoding: "bytes",
|
|
170
|
+
expectResponse: !params?.NO_RESPONSE, rejectUnauthorized: !params?.ALLOW_UNAUTHORIZED,
|
|
171
|
+
servername: params?.SERVER_NAME || undefined, ...extra(call),
|
|
172
|
+
};
|
|
173
|
+
},
|
|
174
|
+
out: (v) => ({ result: recordOf("SocketResult", v), raw: v?.body == null ? {} : { body: bytesOf(v.body) } }),
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** The host's params for a decoded typed call. */
|
|
179
|
+
export function hostParamsOf(call) {
|
|
180
|
+
const adapter = ADAPTERS[call.op];
|
|
181
|
+
if (adapter?.in) return adapter.in(call);
|
|
182
|
+
return { ...optionsOf(call.params), ...Object.fromEntries(Object.entries(call.raw).map(([k, v]) => [camel(k), v])) };
|
|
183
|
+
}
|
|
184
|
+
/** The typed answer ({ result, lists, raw }) for the host's value. */
|
|
185
|
+
export function typedResultOf(call, value) {
|
|
186
|
+
const adapter = ADAPTERS[call.op];
|
|
187
|
+
if (adapter?.out) return adapter.out(value, call);
|
|
188
|
+
const spec = HOST_OPERATIONS[call.op];
|
|
189
|
+
return { result: spec?.result ? recordOf(spec.result, value) : undefined };
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** The HOSTCALL_ERROR_CODES code for a host error. */
|
|
193
|
+
export function hostcallErrorCode(error) {
|
|
194
|
+
if (error?.hostcallCode) return error.hostcallCode;
|
|
195
|
+
const name = error?.name ?? error?.constructor?.name ?? "";
|
|
196
|
+
if (name === "HostCapabilityError" || error?.code === "host-capability-denied") return "not-authorized";
|
|
197
|
+
if (name === "HostFilesystemScopeError") return "sandbox";
|
|
198
|
+
if (name === "AbortError" || name === "TimeoutError" || /timed out/i.test(error?.message ?? "")) return "timeout";
|
|
199
|
+
if (/^Unknown .* operation|not available in the synchronous hostcall ABI|does not implement/.test(error?.message ?? "")) return "unsupported";
|
|
200
|
+
if (error instanceof RangeError || error instanceof TypeError) return "invalid-params";
|
|
201
|
+
return "failed";
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Answer one typed hostcall payload: decode the $PIV request, run `dispatch(op, params)` (sync, or a Promise when
|
|
206
|
+
* `allowAsync`), and encode the $PIV response. Errors become STATUS FAILED with their code; nothing throws.
|
|
207
|
+
*/
|
|
208
|
+
export function answerTypedHostCall(payload, dispatch, { allowAsync = false } = {}) {
|
|
209
|
+
let call;
|
|
210
|
+
const failure = (error) => encodeHostCallResult({ ok: false, error: { code: hostcallErrorCode(error), message: String(error?.message ?? error) } });
|
|
211
|
+
const success = (value) => {
|
|
212
|
+
try { return encodeHostCallResult({ ok: true, op: call.op, ...typedResultOf(call, value) }); } catch (error) { return failure(error); }
|
|
213
|
+
};
|
|
214
|
+
try {
|
|
215
|
+
call = decodeHostCall(payload);
|
|
216
|
+
if (!HOST_OPERATIONS[call.op]) throw Object.assign(new Error(`Unknown host operation "${call.op}".`), { hostcallCode: "unsupported" });
|
|
217
|
+
const value = dispatch(call.op, hostParamsOf(call));
|
|
218
|
+
if (value && typeof value.then === "function") {
|
|
219
|
+
if (!allowAsync) {
|
|
220
|
+
if (typeof value.catch === "function") value.catch(() => {});
|
|
221
|
+
throw Object.assign(new Error(`Operation "${call.op}" is not available in the synchronous hostcall ABI.`), { hostcallCode: "unsupported" });
|
|
222
|
+
}
|
|
223
|
+
return value.then(success, failure);
|
|
224
|
+
}
|
|
225
|
+
return success(value);
|
|
226
|
+
} catch (error) {
|
|
227
|
+
if (error && !error.hostcallCode && call === undefined) error.hostcallCode = "invalid-params";
|
|
228
|
+
return failure(error);
|
|
229
|
+
}
|
|
230
|
+
}
|
package/src/index.d.ts
CHANGED
|
@@ -3055,6 +3055,8 @@ export function encodeLicensingChallengeRequest(options: {
|
|
|
3055
3055
|
requestedTimeoutMs: number;
|
|
3056
3056
|
requestedAtMs: number;
|
|
3057
3057
|
providerPeerId: string;
|
|
3058
|
+
/** The requester's $EPM FlatBuffer; an allowlisted module requires it. */
|
|
3059
|
+
requesterEpm?: Uint8Array;
|
|
3058
3060
|
}): Uint8Array;
|
|
3059
3061
|
|
|
3060
3062
|
export function decodeLicensingChallengeMessage(
|
package/src/invoke/codec.js
CHANGED
|
@@ -406,7 +406,8 @@ function normalizeFrameTypeRef(frame = {}) {
|
|
|
406
406
|
typeRef.BYTE_LENGTH = readBoundedInteger(
|
|
407
407
|
typeRef.BYTE_LENGTH,
|
|
408
408
|
"SDS PIV/TAB aligned type byteLength",
|
|
409
|
-
|
|
409
|
+
// 0 = a variable-length aligned stream, as the decoder and the guest glue (OutputTypeMatches) admit
|
|
410
|
+
{ minimum: 0, maximum: UINT32_MAX },
|
|
410
411
|
);
|
|
411
412
|
} else if (
|
|
412
413
|
typeRef.REQUIRED_ALIGNMENT > 0 &&
|
|
@@ -440,7 +441,7 @@ function assertAlignedFrameSize(wireFormat, typeRef, size) {
|
|
|
440
441
|
if (wireFormat !== "aligned-binary") {
|
|
441
442
|
return;
|
|
442
443
|
}
|
|
443
|
-
if (size !== typeRef.BYTE_LENGTH) {
|
|
444
|
+
if (typeRef.BYTE_LENGTH > 0 && size !== typeRef.BYTE_LENGTH) {
|
|
444
445
|
throw new RangeError(
|
|
445
446
|
`SDS PIV/TAB aligned payload size ${size} does not match declared byteLength ${typeRef.BYTE_LENGTH}.`,
|
|
446
447
|
);
|
package/src/licensing/records.js
CHANGED
|
@@ -60,6 +60,15 @@ export function encodeLicensingChallengeRequest(options) {
|
|
|
60
60
|
const providerPeerIdOffset = builder.createString(
|
|
61
61
|
normalizeRequiredString(options.providerPeerId, "providerPeerId"),
|
|
62
62
|
);
|
|
63
|
+
// The requester's $EPM, re-sent per grant: an allowlisted module needs it to
|
|
64
|
+
// bind the requester's xpub to the signing key it is about to prove.
|
|
65
|
+
const requesterEpmOffset =
|
|
66
|
+
options.requesterEpm == null
|
|
67
|
+
? 0
|
|
68
|
+
: LCH.createRequesterEpmVector(
|
|
69
|
+
builder,
|
|
70
|
+
cloneRequiredBytes(options.requesterEpm, "requesterEpm"),
|
|
71
|
+
);
|
|
63
72
|
const root = LCH.createLCH(
|
|
64
73
|
builder,
|
|
65
74
|
licensingChallengeMessageType.Request,
|
|
@@ -79,7 +88,7 @@ export function encodeLicensingChallengeRequest(options) {
|
|
|
79
88
|
providerPeerIdOffset,
|
|
80
89
|
0,
|
|
81
90
|
0,
|
|
82
|
-
|
|
91
|
+
requesterEpmOffset,
|
|
83
92
|
);
|
|
84
93
|
LCH.finishLCHBuffer(builder, root);
|
|
85
94
|
return builder.asUint8Array();
|