space-data-module-sdk 0.9.4 → 0.9.6

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.
Files changed (52) hide show
  1. package/docs/_shell/consumer-assets.html +3 -3
  2. package/docs/browser-wasmedge-isomorphic.html +3 -3
  3. package/docs/byo-wasm-quickstart.html +3 -3
  4. package/docs/conformance.html +3 -3
  5. package/docs/events-abi.html +3 -3
  6. package/docs/families/analytics.html +3 -3
  7. package/docs/families/attitude.html +3 -3
  8. package/docs/families/behavior.html +3 -3
  9. package/docs/families/breakup.html +3 -3
  10. package/docs/families/conjunction.html +3 -3
  11. package/docs/families/data-source.html +3 -3
  12. package/docs/families/effects.html +3 -3
  13. package/docs/families/environment.html +3 -3
  14. package/docs/families/estimation.html +3 -3
  15. package/docs/families/gnc.html +3 -3
  16. package/docs/families/maneuver.html +3 -3
  17. package/docs/families/obstruction.html +3 -3
  18. package/docs/families/propagator.html +3 -3
  19. package/docs/families/propulsion.html +3 -3
  20. package/docs/families/reentry.html +3 -3
  21. package/docs/families/rf.html +3 -3
  22. package/docs/families/scheduler.html +3 -3
  23. package/docs/families/sensor.html +3 -3
  24. package/docs/families/signature.html +3 -3
  25. package/docs/flatsql-host-contract.html +3 -3
  26. package/docs/flatsql-streaming-standard.html +3 -3
  27. package/docs/gpu-module-abi.html +3 -3
  28. package/docs/index.html +3 -3
  29. package/docs/isomorphic-pthreads.html +3 -3
  30. package/docs/isomorphic-sdn-runtime-plan.html +3 -3
  31. package/docs/language-runtime-matrix.html +3 -3
  32. package/docs/module-bundle-runtime-plan.html +3 -3
  33. package/docs/module-publication-standard.html +3 -3
  34. package/docs/node-red-default-node-parity.html +3 -3
  35. package/docs/protect-and-sign.html +3 -3
  36. package/docs/protocol-installation.html +3 -3
  37. package/docs/publication-submission.html +3 -3
  38. package/docs/secrets-capability.html +3 -3
  39. package/docs/testing-harness.html +3 -3
  40. package/docs/tri-runtime-parity-gate.html +3 -3
  41. package/docs/tri-runtime-parity.html +3 -3
  42. package/docs/wallet-callback.html +1 -1
  43. package/package.json +2 -2
  44. package/src/auth/permissions.js +13 -1
  45. package/src/compiler/compileModule.js +3 -2
  46. package/src/host/browserHost.js +8 -19
  47. package/src/host/hostWorkerBundleSources.js +3 -3
  48. package/src/host/nodeHost.js +11 -10
  49. package/src/host/sabHostcallChannel.js +3 -0
  50. package/src/host/typedDispatch.js +4 -2
  51. package/src/index.d.ts +2 -0
  52. package/src/utils/wasmCrypto.js +56 -9
@@ -29,7 +29,9 @@ import {
29
29
  hkdfBytes,
30
30
  secp256k1PublicKey,
31
31
  secp256k1SignDigest,
32
+ secp256k1SignMessage,
32
33
  secp256k1VerifyDigest,
34
+ secp256k1VerifyMessage,
33
35
  sha512Bytes,
34
36
  x25519PublicKey,
35
37
  x25519SharedSecret,
@@ -2521,13 +2523,12 @@ export class NodeHost {
2521
2523
  ),
2522
2524
  ),
2523
2525
  // Message-based ECDSA-DER contract (parity with the Go/C++ hosts and the
2524
- // synchronous host-crypto ABI): sha256 the message internally, then use
2525
- // the digest-based primitives with DER <-> raw conversion.
2526
+ // synchronous host-crypto ABI): ECDSA over sha256(message) - the wallet's
2527
+ // message API hashes once - with DER <-> raw conversion.
2526
2528
  sign: async (message, privateKey) =>
2527
2529
  this.#withCapability("crypto_sign", "crypto.secp256k1.sign", async () => {
2528
- const digest = await sha256Bytes(toUint8Array(message));
2529
- const rawSignature = await secp256k1SignDigest(
2530
- toUint8Array(digest),
2530
+ const rawSignature = await secp256k1SignMessage(
2531
+ toUint8Array(message),
2531
2532
  toUint8Array(privateKey),
2532
2533
  );
2533
2534
  return rawSignatureToDer(rawSignature);
@@ -2537,9 +2538,8 @@ export class NodeHost {
2537
2538
  "crypto_verify",
2538
2539
  "crypto.secp256k1.verify",
2539
2540
  async () => {
2540
- const digest = await sha256Bytes(toUint8Array(message));
2541
- const result = await secp256k1VerifyDigest(
2542
- toUint8Array(digest),
2541
+ const result = await secp256k1VerifyMessage(
2542
+ toUint8Array(message),
2543
2543
  derSignatureToRaw(signature),
2544
2544
  toUint8Array(publicKey),
2545
2545
  );
@@ -2651,8 +2651,9 @@ export class NodeHost {
2651
2651
  `keyslot "${slotId}" is not a 32-byte secp256k1 private key`,
2652
2652
  );
2653
2653
  }
2654
- const digest = await sha256Bytes(payload);
2655
- const rawSignature = await secp256k1SignDigest(digest, keyMaterial);
2654
+ // ECDSA over sha256(payload), as the Go node's ecdsa.Sign(sha256(payload)):
2655
+ // the wallet's message API hashes once.
2656
+ const rawSignature = await secp256k1SignMessage(payload, keyMaterial);
2656
2657
  return {
2657
2658
  signature: bytesToBase64(rawSignatureToDer(rawSignature)),
2658
2659
  algorithm: "secp256k1",
@@ -65,6 +65,8 @@ function serializeChannelError(error, operation) {
65
65
  code: error?.code ?? null,
66
66
  operation: error?.operation ?? operation ?? null,
67
67
  capability: error?.capability ?? null,
68
+ // the typed hostcall's error code (typedDispatch.js hostcallErrorCode), so it survives the channel
69
+ hostcallCode: error?.hostcallCode ?? null,
68
70
  };
69
71
  }
70
72
 
@@ -141,6 +143,7 @@ export function createSabHostcallClientDispatch(options = {}) {
141
143
  error.code = value?.code ?? null;
142
144
  error.operation = value?.operation ?? operation;
143
145
  error.capability = value?.capability ?? null;
146
+ if (value?.hostcallCode) error.hostcallCode = value.hostcallCode;
144
147
  throw error;
145
148
  }
146
149
  return value;
@@ -625,7 +625,8 @@ const ADAPTERS = {
625
625
  },
626
626
  };
627
627
  function data(v) { return { raw: { data: bytesOf(v) ?? new Uint8Array(0) } }; }
628
- function bool(v) { return { result: { VALUE: Boolean(v) } }; }
628
+ // a verify's answer: a boolean, or the hosts' {result: boolean} (the secp256k1 verifies) - an object is not "true"
629
+ function bool(v) { return { result: { VALUE: v !== null && typeof v === "object" ? v.result === true : Boolean(v) } }; }
629
630
  function socket(extra = () => ({})) {
630
631
  return {
631
632
  in: (call) => {
@@ -666,7 +667,8 @@ export function hostcallErrorCode(error) {
666
667
  if (/^Unknown .* operation|not available in the synchronous hostcall ABI|does not implement/.test(error?.message ?? "")) return "unsupported";
667
668
  // a string or list that does not fit its aligned record (the Go node's recordErr)
668
669
  if (/exceeds? the record/.test(error?.message ?? "")) return "too-large";
669
- if (error instanceof RangeError || error instanceof TypeError) return "invalid-params";
670
+ // (by name too: an error that crossed the worker harness's channel is a plain Error carrying the name)
671
+ if (error instanceof RangeError || error instanceof TypeError || name === "RangeError" || name === "TypeError") return "invalid-params";
670
672
  return "failed";
671
673
  }
672
674
 
package/src/index.d.ts CHANGED
@@ -680,6 +680,7 @@ export function assertDeploymentAuthorization(options: {
680
680
  now?: number;
681
681
  }): boolean;
682
682
 
683
+ /** signDigest signs the 32-byte digest as given (no further hashing). */
683
684
  export function createHdWalletSigner(options: {
684
685
  signDigest: (digest: Uint8Array) => Promise<Uint8Array>;
685
686
  publicKeyHex: string;
@@ -689,6 +690,7 @@ export function createHdWalletSigner(options: {
689
690
  curve?: string;
690
691
  }): Signer;
691
692
 
693
+ /** verifyDigest checks a signature over sha256(canonical bytes) as given (no further hashing). */
692
694
  export function createHdWalletVerifier(options: {
693
695
  verifyDigest: (
694
696
  digest: Uint8Array,
@@ -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) {