CodeSampleX

Exemplo

@noble/hashes 1.4.0: toBytes

Amostra verificada para npm @noble/hashes 1.4.0: toBytes. O contrato rodou em node 22 · linux debian/x64 · docker e passou: toBytes is exported as a function…

sha256:0aced4f64f47e965ddf38a5f12a0be1cc323d248562a52e0bb52918d7e6036e1

Esta rede oferece uma coisa: uma amostra que compila. Ela a executou em um sandbox e guardou o recibo assinado. Não classifica nem garante nada — se o mesmo código compila onde você está, ela não mediu. Quantas chaves de assinatura distintas enviaram um recibo de contrato aprovado. Uma é só o autor; mais de uma significa que outra pessoa também o compilou. Uma chave é gerada por conta própria e não tem identidade registrada por trás, então conta chaves, não pessoas. MIT-0

Evidência de execução

O ambiente declarado e as execuções assinadas ficam separados, para você ver exatamente o que esta amostra executou e onde.

Base da evidência
Contrato assinado aprovado
Recibos de verificação
1
Chaves de assinatura que o compilaram
1
Ambiente declarado node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

Ambientes das execuções de verificação

Ambiente Contrato Etapas Execução
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

Caso

HOW
Objetivo
verify @noble/hashes.toBytes in pkg:npm/%40noble/hashes@1.4.0
Pacotes
Símbolos
  • @noble/hashes.toBytes
Ambiente
node 22.23.2
Criado
2026-09-06T10:55:51Z

Contrato

  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

Arquivos

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

Baixar o artefato de código-fonte (tar.gz)

Código-fonte

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.');

Seeder de origem

anônimo