space-data-module-sdk 0.9.3 → 0.9.5

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.
@@ -32,6 +32,7 @@ import { ExitClass, assertWasmEdgeVersionMatchesPin } from "./parityHarness.js";
32
32
  import { normalizeWasmEdgeOutcome } from "./wasmedgeOutput.js";
33
33
  import { isWasiThreadsModule } from "../host/wasiThreadHost.js";
34
34
  import { resolveWasmEdgeWasiThreadsRunner } from "./buildWasmEdgeRunner.js";
35
+ import { PARITY_HOSTCALL_FILES, encodeParityHostcallAnswers, importsHostcallBridge } from "./parityHostcalls.js";
35
36
 
36
37
  const execFile = promisify(execFileCallback);
37
38
  const __dirname = path.dirname(fileURLToPath(import.meta.url));
@@ -134,9 +135,9 @@ function wasmedgeInvocationArgs(planCase, plan, threadCount) {
134
135
  // direct surface of an artifact with a command entry; the WasmEdge CLI runs
135
136
  // `_start` or nothing, so a direct-surface comparison on it would silently
136
137
  // compare two different surfaces.
137
- function surfaceArgs(context, threaded, lane) {
138
+ function surfaceArgs(context, useRunner, lane) {
138
139
  if (context.surface !== "direct") return [];
139
- if (!threaded) {
140
+ if (!useRunner) {
140
141
  throw new Error(
141
142
  `${lane}: the WasmEdge CLI serves only the command surface of a single-thread artifact; ` +
142
143
  "it cannot take part in a direct-surface parity run.",
@@ -145,6 +146,24 @@ function surfaceArgs(context, threaded, lane) {
145
146
  return ["--sdm-direct"];
146
147
  }
147
148
 
149
+ // How a WasmEdge lane runs the artifact. The plain CLI provides no host imports,
150
+ // so an artifact that imports the hostcall bridge (space_data_module_host) runs
151
+ // on the SDK runner too, threaded or not; the runner replays the staged answers.
152
+ async function wasmEdgeArtifact(context) {
153
+ const wasmModule = await WebAssembly.compile(context.loadableBytes ?? context.wasmBytes);
154
+ const threaded = isWasiThreadsModule(wasmModule);
155
+ const bridged = importsHostcallBridge(wasmModule);
156
+ return { threaded, bridged, useRunner: threaded || bridged };
157
+ }
158
+
159
+ // Stage the case's hostcall answers beside the module; the runner flags that name them.
160
+ async function stageHostcallAnswers(workdir, planCase) {
161
+ const answers = encodeParityHostcallAnswers(planCase.config ?? {});
162
+ await writeFile(path.join(workdir, PARITY_HOSTCALL_FILES.config), answers.config);
163
+ await writeFile(path.join(workdir, PARITY_HOSTCALL_FILES.unsupported), answers.unsupported);
164
+ return ["--sdm-hostcall-config", PARITY_HOSTCALL_FILES.config, "--sdm-hostcall-unsupported", PARITY_HOSTCALL_FILES.unsupported];
165
+ }
166
+
148
167
  async function stageModuleWorkdir(context, label) {
149
168
  const workdir = await mkdtemp(path.join(os.tmpdir(), `sdm-parity-${label}-`));
150
169
  // Stage the canonical loadable bytes (publication records stripped by the
@@ -179,8 +198,8 @@ export async function resolveWasmEdgeBinary(context = {}) {
179
198
  }
180
199
 
181
200
  export async function runNativeWasmEdgeLane(context) {
182
- const threaded = isWasiThreadsModule(await WebAssembly.compile(context.loadableBytes ?? context.wasmBytes));
183
- const binary = threaded
201
+ const { threaded, bridged, useRunner } = await wasmEdgeArtifact(context);
202
+ const binary = useRunner
184
203
  ? await resolveWasmEdgeWasiThreadsRunner(context)
185
204
  : await resolveWasmEdgeBinary(context);
186
205
  let versionOutput;
@@ -198,16 +217,18 @@ export async function runNativeWasmEdgeLane(context) {
198
217
  `native binary ${binary}`,
199
218
  );
200
219
 
201
- const directArgs = surfaceArgs(context, threaded, "native WasmEdge lane");
220
+ const directArgs = surfaceArgs(context, useRunner, "native WasmEdge lane");
202
221
  const workdir = await stageModuleWorkdir(context, "native");
203
222
  const runs = [];
204
223
  try {
205
224
  for (const planCase of context.plan.cases) {
225
+ const hostcallArgs = bridged ? await stageHostcallAnswers(workdir, planCase) : [];
206
226
  for (const threadCount of planCase.threadCounts) {
207
227
  const outcome = await spawnWithStdin(
208
228
  binary,
209
229
  [...(threaded ? ["--sdm-thread-stats"] : []),
210
230
  ...directArgs,
231
+ ...hostcallArgs,
211
232
  ...wasmedgeInvocationArgs(planCase, context.plan, threadCount)],
212
233
  {
213
234
  cwd: workdir,
@@ -295,8 +316,8 @@ export async function ensureDockerParityImage(context) {
295
316
  }
296
317
 
297
318
  export async function runDockerWasmEdgeLane(context) {
298
- const threaded = isWasiThreadsModule(await WebAssembly.compile(context.loadableBytes ?? context.wasmBytes));
299
- if (threaded) context = { ...context, pin: { ...context.pin, dockerImage: context.pin.dockerRunnerImage } };
319
+ const { threaded, bridged, useRunner } = await wasmEdgeArtifact(context);
320
+ if (useRunner) context = { ...context, pin: { ...context.pin, dockerImage: context.pin.dockerRunnerImage } };
300
321
  const dockerBinary = context.dockerBinary ?? "docker";
301
322
  try {
302
323
  await execFile(dockerBinary, ["--version"]);
@@ -305,13 +326,14 @@ export async function runDockerWasmEdgeLane(context) {
305
326
  `docker WasmEdge lane: cannot execute "${dockerBinary}" (${error.message}).`,
306
327
  );
307
328
  }
308
- const directArgs = surfaceArgs(context, threaded, "docker WasmEdge lane");
329
+ const directArgs = surfaceArgs(context, useRunner, "docker WasmEdge lane");
309
330
  const image = await ensureDockerParityImage(context);
310
331
 
311
332
  const workdir = await stageModuleWorkdir(context, "docker");
312
333
  const runs = [];
313
334
  try {
314
335
  for (const planCase of context.plan.cases) {
336
+ const hostcallArgs = bridged ? await stageHostcallAnswers(workdir, planCase) : [];
315
337
  for (const threadCount of planCase.threadCounts) {
316
338
  const dockerArgs = [
317
339
  "run",
@@ -327,11 +349,12 @@ export async function runDockerWasmEdgeLane(context) {
327
349
  if (context.dockerPlatform) {
328
350
  dockerArgs.push("--platform", String(context.dockerPlatform));
329
351
  }
330
- if (threaded) dockerArgs.push("--entrypoint", "/opt/wasmedge/bin/sdm-wasi-threads-runner");
352
+ if (useRunner) dockerArgs.push("--entrypoint", "/opt/wasmedge/bin/sdm-wasi-threads-runner");
331
353
  dockerArgs.push(
332
354
  image,
333
355
  ...(threaded ? ["--sdm-thread-stats"] : []),
334
356
  ...directArgs,
357
+ ...hostcallArgs,
335
358
  ...wasmedgeInvocationArgs(planCase, context.plan, threadCount),
336
359
  );
337
360
  const outcome = await spawnWithStdin(dockerBinary, dockerArgs, {
@@ -422,6 +445,7 @@ function browserPlanPayload(context) {
422
445
  env: planCase.env,
423
446
  args: [MODULE_BASENAME, ...planCase.args],
424
447
  threadCounts: planCase.threadCounts,
448
+ config: planCase.config ?? {},
425
449
  })),
426
450
  });
427
451
  }
@@ -57,21 +57,68 @@ export async function secp256k1PublicKey(privateKey) {
57
57
  return wallet.curves.publicKeyFromPrivate(toUint8Array(privateKey));
58
58
  }
59
59
 
60
+ // secp256k1 ECDSA. The wallet's sign / verify hash their input with sha256 (a message API); signRecoverable and
61
+ // recover take a 32-byte hash as given. Message APIs (the Go node's crypto.secp256k1.sign / verify and keyslot.sign:
62
+ // ECDSA over sha256(message)) pass the message to secp256k1SignMessage / secp256k1VerifyMessage; digest APIs
63
+ // (signDigest / verifyDigest: ECDSA over the given digest) use secp256k1SignDigest / secp256k1VerifyDigest.
64
+
65
+ /** ECDSA over sha256(message): the 64-byte raw (r || s) signature. */
66
+ export async function secp256k1SignMessage(message, privateKey) {
67
+ const wallet = await getWasmWallet();
68
+ return wallet.curves.secp256k1.sign(toUint8Array(message), toUint8Array(privateKey));
69
+ }
70
+
71
+ /** Whether a 64-byte raw (r || s) signature is ECDSA over sha256(message) for the public key. */
72
+ export async function secp256k1VerifyMessage(message, signature, publicKey) {
73
+ const wallet = await getWasmWallet();
74
+ return wallet.curves.secp256k1.verify(toUint8Array(message), toUint8Array(signature), toUint8Array(publicKey));
75
+ }
76
+
77
+ function assertDigest(digest) {
78
+ if (digest.length !== 32) {
79
+ throw Object.assign(new RangeError(`secp256k1 digest must be 32 bytes, got ${digest.length}.`), { hostcallCode: "invalid-params" });
80
+ }
81
+ return digest;
82
+ }
83
+
84
+ /** A 33-byte compressed secp256k1 public key (a 65-byte uncompressed 0x04 || X || Y is compressed). */
85
+ function compressedSecp256k1Key(publicKey) {
86
+ if (publicKey.length === 33) return publicKey;
87
+ if (publicKey.length === 65 && publicKey[0] === 0x04) {
88
+ const out = new Uint8Array(33);
89
+ out[0] = publicKey[64] & 1 ? 0x03 : 0x02;
90
+ out.set(publicKey.subarray(1, 33), 1);
91
+ return out;
92
+ }
93
+ throw new RangeError(`secp256k1 public key must be 33 or 65 bytes, got ${publicKey.length}.`);
94
+ }
95
+
96
+ /** ECDSA over the given 32-byte digest (not hashed again): the 64-byte raw (r || s) signature. */
60
97
  export async function secp256k1SignDigest(digest, privateKey) {
61
98
  const wallet = await getWasmWallet();
62
- return wallet.curves.secp256k1.sign(
63
- toUint8Array(digest),
64
- toUint8Array(privateKey),
65
- );
99
+ return wallet.curves.secp256k1.signRecoverable(assertDigest(toUint8Array(digest)), toUint8Array(privateKey)).signature;
66
100
  }
67
101
 
102
+ /**
103
+ * Whether a 64-byte raw (r || s) signature is ECDSA over the given 32-byte digest for the public key: the key
104
+ * recovered with recovery id 0 or 1 equals it.
105
+ */
68
106
  export async function secp256k1VerifyDigest(digest, signature, publicKey) {
69
107
  const wallet = await getWasmWallet();
70
- return wallet.curves.secp256k1.verify(
71
- toUint8Array(digest),
72
- toUint8Array(signature),
73
- toUint8Array(publicKey),
74
- );
108
+ const hash = assertDigest(toUint8Array(digest));
109
+ const sig = toUint8Array(signature);
110
+ const expected = compressedSecp256k1Key(toUint8Array(publicKey));
111
+ if (sig.length !== 64) return false;
112
+ for (const recoveryId of [0, 1]) {
113
+ let recovered;
114
+ try {
115
+ recovered = toUint8Array(wallet.curves.secp256k1.recover(hash, sig, recoveryId));
116
+ } catch {
117
+ continue;
118
+ }
119
+ if (recovered.length === 33 && recovered.every((b, i) => b === expected[i])) return true;
120
+ }
121
+ return false;
75
122
  }
76
123
 
77
124
  export async function ed25519PublicKey(seed) {