CodeSampleX

Beispiel

@noble/hashes 1.4.0: toBytes

Verifiziertes Beispiel für npm @noble/hashes 1.4.0: toBytes. Der Vertrag lief auf node 22 · linux debian/x64 · docker und bestand.

sha256:0aced4f64f47e965ddf38a5f12a0be1cc323d248562a52e0bb52918d7e6036e1

Dieses Netzwerk bietet eine Sache: ein Sample, das baut. Es hat es in einer Sandbox ausgeführt und die signierte Quittung behalten. Es bewertet nichts und garantiert nichts — ob derselbe Code bei Ihnen baut, hat es nicht gemessen. Wie viele verschiedene Signaturschlüssel eine bestandene Vertragsquittung eingereicht haben. Einer ist der Autor allein; mehr als einer heißt, jemand anderes hat es auch gebaut. Ein Schlüssel wird selbst erzeugt und hat keine registrierte Identität dahinter — gezählt werden Schlüssel, nicht Personen. MIT-0

Ausführungsbelege

Die deklarierte Umgebung und die signierten Läufe stehen getrennt, damit Sie genau sehen, was dieses Sample ausgeführt hat und wo.

Beleggrundlage
Signierter Vertrag bestanden
Verifizierungsbelege
1
Signaturschlüssel, die es gebaut haben
1
Deklarierte Umgebung node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

Umgebungen der Verifizierungsläufe

Umgebung Contract Stufen Lauf
node 22 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22@sha256:8a34c4ab3ea2…
2026-09-06

Fall

HOW
Ziel
verify @noble/hashes.toBytes in pkg:npm/%40noble/hashes@1.4.0
Pakete
Symbole
  • @noble/hashes.toBytes
Umgebung
node 22.23.2
Erstellt
2026-09-06T10:55:51Z

Contract

  1. toBytes is exported as a function from @noble/hashes/utils
  2. toBytes converts string input to UTF-8 encoded Uint8Array
  3. toBytes passes Uint8Array input through without copying
  4. toBytes converts empty string to empty Uint8Array
  5. toBytes handles multibyte Unicode characters and emojis
  6. toBytes throws an Error for invalid input types

Dateien

  • PROMPT.md
  • csx.json
  • package-lock.json
  • package.json
  • spec.json
  • src/index.js
  • test/contract.mjs

Quellartefakt herunterladen (tar.gz)

Quelltext

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify @noble/hashes.toBytes in pkg:npm/%40noble/hashes@1.4.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/%40noble/hashes@1.4.0
Demonstrate these symbols/APIs:
  - @noble/hashes.toBytes
Constraints:
  - executionContext: node
Required runtime conditions:
  - ecosystem: npm
  - language: javascript
  - moduleSystem: cjs
  - packageManager: npm@10.9.8
  - runtime: node@22.23.2

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:c13a108ed25b05821e8eb3767eab2d1a6d1db0240355669ef82cc3e8357aea37","constraints":{"executionContext":"node"},"contract":["toBytes is exported as a function from @noble/hashes/utils","toBytes converts string input to UTF-8 encoded Uint8Array","toBytes passes Uint8Array input through without copying","toBytes converts empty string to empty Uint8Array","toBytes handles multibyte Unicode characters and emojis","toBytes throws an Error for invalid input types"],"goal":"verify @noble/hashes.toBytes in pkg:npm/%40noble/hashes@1.4.0","kind":"HOW","packages":["pkg:npm/%40noble/hashes@1.4.0"],"schemaVersion":1,"symbols":["@noble/hashes.toBytes"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","executionContext":"node","language":"javascript","libc":"glibc","libcVersion":"2.39","moduleSystem":"cjs","os":"linux","osVersionBucket":"24","packageManager":"npm","packageManagerVersion":"10.9.8","runtime":"node","runtimeVersion":"22.23.2","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/%40noble/hashes@1.4.0"],"schemaVersion":1,"subject":"pkg:npm/%40noble/hashes@1.4.0","symbols":["@noble/hashes.toBytes"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "sample-noble-hashes-to-bytes",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-noble-hashes-to-bytes",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "@noble/hashes": "1.4.0"
      }
    },
    "node_modules/@noble/hashes": {
      "version": "1.4.0",
      "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-1.4.0.tgz",
      "integrity": "sha512-V1JJ1WTRUqHHrOSh597hURcMqVKVGL/ea3kv0gSnEdsEZ0/+VyPghM1lMNGc00z7CIQorSvbKpuJkxvuHbvdbg==",
      "license": "MIT",
      "engines": {
        "node": ">= 16"
      },
      "funding": {
        "url": "https://paulmillr.com/funding/"
      }
    }
  }
}
package.json
{
  "name": "sample-noble-hashes-to-bytes",
  "version": "1.0.0",
  "private": true,
  "description": "Clean-room code sample for @noble/hashes toBytes",
  "main": "src/index.js",
  "license": "MIT-0",
  "dependencies": {
    "@noble/hashes": "1.4.0"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify @noble/hashes.toBytes in pkg:npm/%40noble/hashes@1.4.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/%40noble/hashes@1.4.0"
  ],
  "symbols": [
    "@noble/hashes.toBytes"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@10.9.8",
    "runtime": "node@22.23.2"
  }
}
src/index.js
'use strict';

const { toBytes } = require('@noble/hashes/utils');

/**
 * Normalizes input (string or Uint8Array) into a Uint8Array.
 * If input is a string, it is UTF-8 encoded into a Uint8Array.
 * If input is already a Uint8Array, it is returned directly without copying.
 *
 * @param {string | Uint8Array} data - String or Uint8Array to normalize.
 * @returns {Uint8Array} Normalized byte array.
 */
function normalizeBytes(data) {
  return toBytes(data);
}

module.exports = {
  toBytes,
  normalizeBytes
};
test/contract.mjs
import assert from 'node:assert/strict';
import { toBytes, normalizeBytes } from '../src/index.js';
import * as utils from '@noble/hashes/utils';

// 1. toBytes is exported as a function from @noble/hashes/utils
assert.equal(typeof utils.toBytes, 'function', 'toBytes must be exported from @noble/hashes/utils');
assert.equal(typeof toBytes, 'function', 'toBytes helper must be a function');
assert.equal(typeof normalizeBytes, 'function', 'normalizeBytes helper must be a function');

// 2. toBytes converts string input to UTF-8 encoded Uint8Array
const asciiStr = 'hello world';
const asciiBytes = toBytes(asciiStr);
assert(asciiBytes instanceof Uint8Array, 'result must be a Uint8Array');
assert.deepEqual(
  Array.from(asciiBytes),
  [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100],
  'ASCII string must match expected byte values'
);
assert.deepEqual(
  Array.from(normalizeBytes(asciiStr)),
  [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100],
  'normalizeBytes wrapper must match toBytes output'
);

// 3. toBytes passes Uint8Array input through without copying
const original = new Uint8Array([10, 20, 30, 40]);
const result = toBytes(original);
assert.equal(result, original, 'Uint8Array input must return same reference without copying');
assert.equal(normalizeBytes(original), original, 'normalizeBytes must preserve Uint8Array reference');

// 4. toBytes converts empty string to empty Uint8Array
const emptyRes = toBytes('');
assert(emptyRes instanceof Uint8Array, 'empty string must return Uint8Array');
assert.equal(emptyRes.length, 0, 'empty string must produce 0-length Uint8Array');

// 5. toBytes handles multibyte Unicode characters and emojis
const unicodeStr = 'noble ⚡ 한글 🔒';
const unicodeBytes = toBytes(unicodeStr);
const expectedUnicode = Array.from(new TextEncoder().encode(unicodeStr));
assert.deepEqual(
  Array.from(unicodeBytes),
  expectedUnicode,
  'multibyte unicode string must match TextEncoder bytes'
);

// 6. toBytes throws an Error for invalid input types
const invalidInputs = [
  123,
  null,
  undefined,
  true,
  false,
  {},
  [1, 2, 3]
];

for (const input of invalidInputs) {
  assert.throws(
    () => toBytes(input),
    {
      name: 'Error',
      message: 'Uint8Array expected'
    },
    `toBytes must throw 'Uint8Array expected' for invalid input: ${String(input)}`
  );
}

console.log('All @noble/hashes.toBytes contract tests passed.');

Ursprungs-Seeder

anonym