CodeSampleX

示例

@noble/hashes 1.4.0: toBytes

已验证示例 — npm @noble/hashes 1.4.0: toBytes. contract 在 node 22 · linux debian/x64 · docker 上运行并通过: toBytes is exported as a function from @noble/hashes/utils

sha256:0aced4f64f47e965ddf38a5f12a0be1cc323d248562a52e0bb52918d7e6036e1

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

验证运行环境

环境 契约 阶段 运行日期
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

案例

HOW
目标
verify @noble/hashes.toBytes in pkg:npm/%40noble/hashes@1.4.0
包
符号
  • @noble/hashes.toBytes
环境
node 22.23.2
创建时间
2026-09-06T10:55:51Z

契约

  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

文件

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

下载源代码构件 (tar.gz)

源代码

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

原始种子者

匿名