CodeSampleX

Exemple

@noble/hashes 1.4.0: toBytes

Échantillon vérifié pour npm @noble/hashes 1.4.0: toBytes. Le contrat s'est exécuté sur node 22 · linux debian/x64 · docker et a réussi.

sha256:67a8ed982345740d2158637d023cf67688a7b58f378fb5aaa9a2f57643bc3329

Ce réseau offre une seule chose : un échantillon qui compile. Il l'a exécuté dans un bac à sable et conservé le reçu signé. Il ne note rien et ne garantit rien : si le même code compile chez vous, il ne l'a pas mesuré. Combien de clés de signature distinctes ont déposé un reçu de contrat réussi. Une seule, c'est l'auteur ; plus d'une signifie que quelqu'un d'autre l'a compilé aussi. Une clé est auto-générée sans identité enregistrée derrière, donc on compte des clés, pas des personnes. MIT-0

Preuves d'exécution

L'environnement déclaré et les exécutions signées sont séparés, pour que vous voyiez exactement ce que cet échantillon a exécuté et où.

Base de preuve
Contrat signé réussi
Reçus de vérification
1
Clés de signature qui l’ont compilé
1
Environnement déclaré node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

Environnements des exécutions de vérification

Environnement Contrat Étapes Exécution
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

Cas

HOW
Objectif
verify toBytes in pkg:npm/%40noble/hashes@1.4.0
Paquets
Symboles
  • toBytes
Environnement
node 22.23.2
Créé
2026-09-06T11:33:08Z

Contrat

  1. assert toBytes converts UTF-8 strings to Uint8Array matching standard TextEncoder encoding
  2. assert toBytes converts multi-byte Unicode strings to correct UTF-8 byte sequences
  3. assert toBytes converts empty string to zero-length Uint8Array
  4. assert toBytes returns existing Uint8Array instances directly without copying reference
  5. assert toBytes accepts and preserves Uint8Array subclasses including Buffer
  6. assert toBytes throws an Error when invoked with non-string and non-byte values

Fichiers

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

Télécharger l’artefact source (tar.gz)

Code source

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 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:
  - toBytes

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:54f830e6b51500357f19aca704abac169103aa603e943350a6a8d73494d556ca","contract":["assert toBytes converts UTF-8 strings to Uint8Array matching standard TextEncoder encoding","assert toBytes converts multi-byte Unicode strings to correct UTF-8 byte sequences","assert toBytes converts empty string to zero-length Uint8Array","assert toBytes returns existing Uint8Array instances directly without copying reference","assert toBytes accepts and preserves Uint8Array subclasses including Buffer","assert toBytes throws an Error when invoked with non-string and non-byte values"],"goal":"verify toBytes in pkg:npm/%40noble/hashes@1.4.0","kind":"HOW","packages":["pkg:npm/%40noble/hashes@1.4.0"],"schemaVersion":1,"symbols":["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":["toBytes"],"verifierAdapter":"node-typescript@1"}
index.js
const { toBytes } = require('@noble/hashes/utils');

/**
 * Normalizes input to Uint8Array.
 * - Converts UTF-8 string input to a Uint8Array byte sequence.
 * - Passes existing Uint8Array input through without copying the instance.
 * - Throws an Error if input is neither a string nor a Uint8Array.
 *
 * @param {string | Uint8Array} data
 * @returns {Uint8Array}
 */
function normalizeToBytes(data) {
  return toBytes(data);
}

/**
 * Normalizes input to Uint8Array and returns the byte length.
 *
 * @param {string | Uint8Array} data
 * @returns {number}
 */
function byteLength(data) {
  return toBytes(data).length;
}

module.exports = {
  toBytes,
  normalizeToBytes,
  byteLength,
};
package-lock.json
{
  "name": "sample-noble-hashes-tobytes",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-noble-hashes-tobytes",
      "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-tobytes",
  "version": "1.0.0",
  "private": true,
  "description": "verify toBytes in pkg:npm/%40noble/hashes@1.4.0",
  "main": "index.js",
  "scripts": {
    "test": "node test/contract.mjs"
  },
  "dependencies": {
    "@noble/hashes": "1.4.0"
  },
  "license": "MIT-0"
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify toBytes in pkg:npm/%40noble/hashes@1.4.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/%40noble/hashes@1.4.0"
  ],
  "symbols": [
    "toBytes"
  ]
}
test/contract.mjs
import assert from 'node:assert';
import { toBytes } from '@noble/hashes/utils';
import { normalizeToBytes, byteLength } from '../index.js';

// 1. toBytes converts UTF-8 strings to Uint8Array matching standard TextEncoder encoding
const asciiText = 'hello world';
const expectedAsciiBytes = new TextEncoder().encode(asciiText);
const convertedAscii = toBytes(asciiText);
assert.ok(convertedAscii instanceof Uint8Array, 'toBytes must return a Uint8Array instance');
assert.deepStrictEqual(convertedAscii, expectedAsciiBytes, 'ASCII string must match TextEncoder bytes');

// 2. toBytes converts multi-byte Unicode strings to correct UTF-8 byte sequences
const unicodeText = 'cryptography 🔐 검증';
const expectedUnicodeBytes = new TextEncoder().encode(unicodeText);
const convertedUnicode = toBytes(unicodeText);
assert.deepStrictEqual(convertedUnicode, expectedUnicodeBytes, 'Unicode string must match TextEncoder UTF-8 bytes');

// 3. toBytes converts empty string to zero-length Uint8Array
const emptyBytes = toBytes('');
assert.ok(emptyBytes instanceof Uint8Array, 'Empty string must return a Uint8Array instance');
assert.strictEqual(emptyBytes.length, 0, 'Empty string must return a Uint8Array of length 0');

// 4. toBytes returns existing Uint8Array instances directly without copying reference
const existingBytes = new Uint8Array([10, 20, 30, 40, 50]);
const returnedBytes = toBytes(existingBytes);
assert.strictEqual(returnedBytes, existingBytes, 'Existing Uint8Array must be returned by reference without copy');

// 5. toBytes accepts and preserves Uint8Array subclasses including Buffer
const bufferData = Buffer.from([1, 2, 3, 4]);
const returnedBuffer = toBytes(bufferData);
assert.strictEqual(returnedBuffer, bufferData, 'Buffer instance must be returned by reference');

// 6. toBytes throws an Error when invoked with non-string and non-byte values
assert.throws(() => toBytes(42), /Uint8Array expected/, 'Number input must throw Uint8Array expected');
assert.throws(() => toBytes(null), /Uint8Array expected/, 'null input must throw Uint8Array expected');
assert.throws(() => toBytes(undefined), /Uint8Array expected/, 'undefined input must throw Uint8Array expected');
assert.throws(() => toBytes([1, 2, 3]), /Uint8Array expected/, 'Array input must throw Uint8Array expected');
assert.throws(() => toBytes({}), /Uint8Array expected/, 'Object input must throw Uint8Array expected');
assert.throws(() => toBytes(false), /Uint8Array expected/, 'Boolean input must throw Uint8Array expected');

// 7. Helper functions from index.js work correctly
assert.deepStrictEqual(normalizeToBytes('sample'), new TextEncoder().encode('sample'));
assert.strictEqual(byteLength('sample'), 6);
assert.strictEqual(byteLength(new Uint8Array(12)), 12);

console.log('All toBytes contract assertions passed.');

Seeder d'origine

anonyme