CodeSampleX

サンプル

@noble/hashes 1.4.0: toBytes

検証済みサンプル — npm @noble/hashes 1.4.0: toBytes. node 22 · linux debian/x64 · docker で contract を実行し、成功しました: assert toBytes converts UTF-8 strings to Uint8Array…

sha256:67a8ed982345740d2158637d023cf67688a7b58f378fb5aaa9a2f57643bc3329

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。 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 toBytes in pkg:npm/%40noble/hashes@1.4.0
パッケージ
シンボル
  • toBytes
環境
node 22.23.2
作成日
2026-09-06T11:33:08Z

コントラクト

  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

ファイル

  • PROMPT.md
  • csx.json
  • index.js
  • package-lock.json
  • package.json
  • spec.json
  • 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 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.');

オリジンシーダー

匿名