CodeSampleX

示例

ip-address 10.7.0

已验证示例 — npm ip-address 10.7.0. contract 在 node 22 · linux debian/x64 · docker 上运行并通过: Address6.prototype.isInSubnet checks whether an IPv6 address falls…

sha256:b358959130ee3d79a180962ea4ffd49636e8a2cae5409b97da43eb5a76889157

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

执行证据

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

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

验证运行环境

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

案例

HOW
目标
verify pkg:npm/ip-address@10.7.0
包
创建时间
2026-09-07T00:32:50Z

契约

  1. Address6.prototype.isInSubnet checks whether an IPv6 address falls within a target subnet network
  2. Address6 subnet methods startAddress, endAddress, networkForm, and subnetMaskAddress compute network boundaries
  3. Address6.fromAddress4 converts an IPv4 address to an IPv6-mapped address and to4in6 formats it as dotted-decimal
  4. Address6.fromBigInt and bigInt perform lossless two-way conversion between 128-bit integers and IPv6 addresses
  5. Address6.reverseForm generates standard ip6.arpa domain names and Address6.fromArpa parses them back to Address6
  6. Address6.isValid validates full IPv6 addresses and CIDR subnets while rejecting invalid syntax and IPv4 strings

文件

  • PROMPT.md
  • csx.json
  • index.mjs
  • 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 pkg:npm/ip-address@10.7.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/ip-address@10.7.0

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:36763bf9b4d3e14382b9624e95eeabc8401da4131e8b309d49fd3c0a2c1d8c3b","contract":["Address6.prototype.isInSubnet checks whether an IPv6 address falls within a target subnet network","Address6 subnet methods startAddress, endAddress, networkForm, and subnetMaskAddress compute network boundaries","Address6.fromAddress4 converts an IPv4 address to an IPv6-mapped address and to4in6 formats it as dotted-decimal","Address6.fromBigInt and bigInt perform lossless two-way conversion between 128-bit integers and IPv6 addresses","Address6.reverseForm generates standard ip6.arpa domain names and Address6.fromArpa parses them back to Address6","Address6.isValid validates full IPv6 addresses and CIDR subnets while rejecting invalid syntax and IPv4 strings"],"goal":"verify pkg:npm/ip-address@10.7.0","kind":"HOW","packages":["pkg:npm/ip-address@10.7.0"],"schemaVersion":1},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"npm","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/ip-address@10.7.0"],"schemaVersion":1,"subject":"pkg:npm/ip-address@10.7.0","verifierAdapter":"node-typescript@1"}
index.mjs
import { Address6 } from 'ip-address';

/**
 * Checks if a candidate IPv6 address falls within a target subnet.
 *
 * @param {string} address - IPv6 address string.
 * @param {string} subnet - IPv6 network CIDR string.
 * @returns {boolean}
 */
export function isAddressInSubnet(address, subnet) {
  const addr = new Address6(address);
  const sub = new Address6(subnet);
  return addr.isInSubnet(sub);
}

/**
 * Calculates network boundaries for an IPv6 CIDR prefix.
 *
 * @param {string} cidrString - IPv6 CIDR string.
 * @returns {{ start: string, end: string, network: string, mask: string, wildcard: string }}
 */
export function getSubnetBoundaries(cidrString) {
  const addr = new Address6(cidrString);
  return {
    start: addr.startAddress().correctForm(),
    end: addr.endAddress().correctForm(),
    network: addr.networkForm(),
    mask: addr.subnetMaskAddress().correctForm(),
    wildcard: addr.wildcardMask().correctForm()
  };
}

/**
 * Maps an IPv4 address string to an IPv6-mapped address and formats it.
 *
 * @param {string} ipv4String - IPv4 dotted-decimal string.
 * @returns {{ mapped: string, dotted: string }}
 */
export function mapIpv4ToIpv6(ipv4String) {
  const addr6 = Address6.fromAddress4(ipv4String);
  return {
    mapped: addr6.correctForm(),
    dotted: addr6.to4in6()
  };
}

/**
 * Converts between a BigInt and an Address6 instance.
 *
 * @param {string | bigint} value
 * @returns {{ bigInt: bigint, correctForm: string }}
 */
export function convertBigIntIpv6(value) {
  if (typeof value === 'bigint') {
    const addr = Address6.fromBigInt(value);
    return { bigInt: value, correctForm: addr.correctForm() };
  }
  const addr = new Address6(value);
  return { bigInt: addr.bigInt(), correctForm: addr.correctForm() };
}

/**
 * Formats an IPv6 address as an ip6.arpa reverse DNS domain and parses it back.
 *
 * @param {string} address - IPv6 address string.
 * @returns {{ reverse: string, parsed: string }}
 */
export function handleReverseDns(address) {
  const addr = new Address6(address);
  const reverse = addr.reverseForm();
  const parsed = Address6.fromArpa(reverse);
  return {
    reverse,
    parsed: parsed.correctForm()
  };
}

/**
 * Validates whether the given string is a valid IPv6 address or CIDR notation.
 *
 * @param {string} input - Address string to validate.
 * @returns {boolean}
 */
export function validateIpv6(input) {
  return Address6.isValid(input);
}

export { Address6 };
package-lock.json
{
  "name": "ip-address-ipv6-subnet-sample",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "ip-address-ipv6-subnet-sample",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "ip-address": "10.7.0"
      }
    },
    "node_modules/ip-address": {
      "version": "10.7.0",
      "resolved": "https://registry.npmjs.org/ip-address/-/ip-address-10.7.0.tgz",
      "integrity": "sha512-BGFsyJd5mpXp3rK6jIdADLNgpJUK1jnjzvYF8lK+VyDab9JAmqN0YOKDdP17HlgKb2+ehPgDc8EtnRLbGCAMhA==",
      "license": "MIT",
      "engines": {
        "node": ">= 12"
      }
    }
  }
}
package.json
{
  "name": "ip-address-ipv6-subnet-sample",
  "version": "1.0.0",
  "type": "module",
  "license": "MIT-0",
  "dependencies": {
    "ip-address": "10.7.0"
  },
  "scripts": {
    "test": "node test/contract.mjs"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:npm/ip-address@10.7.0",
  "kind": "HOW",
  "packages": [
    "pkg:npm/ip-address@10.7.0"
  ]
}
test/contract.mjs
import assert from 'node:assert/strict';
import {
  Address6,
  isAddressInSubnet,
  getSubnetBoundaries,
  mapIpv4ToIpv6,
  convertBigIntIpv6,
  handleReverseDns,
  validateIpv6
} from '../index.mjs';

// Contract 1: Address6.prototype.isInSubnet checks whether an IPv6 address falls within a target subnet network
{
  const targetSubnet = new Address6('2001:db8::/32');
  const insideHost = new Address6('2001:db8:abcd::1');
  const edgeHost = new Address6('2001:db8:ffff:ffff:ffff:ffff:ffff:ffff');
  const outsideHost = new Address6('2001:db9::1');

  assert.equal(insideHost.isInSubnet(targetSubnet), true);
  assert.equal(edgeHost.isInSubnet(targetSubnet), true);
  assert.equal(outsideHost.isInSubnet(targetSubnet), false);

  assert.equal(isAddressInSubnet('2001:db8:1234::1', '2001:db8::/32'), true);
  assert.equal(isAddressInSubnet('fe80::1', '2001:db8::/32'), false);
}

// Contract 2: Address6 subnet methods startAddress, endAddress, networkForm, and subnetMaskAddress compute network boundaries
{
  const cidr = new Address6('2001:db8:abcd:1234::5/64');
  assert.equal(cidr.startAddress().correctForm(), '2001:db8:abcd:1234::');
  assert.equal(cidr.endAddress().correctForm(), '2001:db8:abcd:1234:ffff:ffff:ffff:ffff');
  assert.equal(cidr.networkForm(), '2001:db8:abcd:1234::/64');
  assert.equal(cidr.subnetMaskAddress().correctForm(), 'ffff:ffff:ffff:ffff::');
  assert.equal(cidr.wildcardMask().correctForm(), '::ffff:ffff:ffff:ffff');

  const bounds = getSubnetBoundaries('2001:db8:abcd:1234::5/64');
  assert.equal(bounds.start, '2001:db8:abcd:1234::');
  assert.equal(bounds.end, '2001:db8:abcd:1234:ffff:ffff:ffff:ffff');
  assert.equal(bounds.network, '2001:db8:abcd:1234::/64');
  assert.equal(bounds.mask, 'ffff:ffff:ffff:ffff::');
  assert.equal(bounds.wildcard, '::ffff:ffff:ffff:ffff');
}

// Contract 3: Address6.fromAddress4 converts an IPv4 address to an IPv6-mapped address and to4in6 formats it as dotted-decimal
{
  const v4Str = '192.168.1.100';
  const mapped = Address6.fromAddress4(v4Str);
  assert.equal(mapped.correctForm(), '::ffff:c0a8:164');
  assert.equal(mapped.to4in6(), '::ffff:192.168.1.100');

  const helperRes = mapIpv4ToIpv6('10.0.0.1');
  assert.equal(helperRes.mapped, '::ffff:a00:1');
  assert.equal(helperRes.dotted, '::ffff:10.0.0.1');
}

// Contract 4: Address6.fromBigInt and bigInt perform lossless two-way conversion between 128-bit integers and IPv6 addresses
{
  const original = new Address6('2001:db8::1');
  const bi = original.bigInt();
  assert.equal(bi, 42540766411282592856903984951653826561n);

  const restored = Address6.fromBigInt(bi);
  assert.equal(restored.correctForm(), '2001:db8::1');

  const helper = convertBigIntIpv6('::1');
  assert.equal(helper.bigInt, 1n);
  assert.equal(helper.correctForm, '::1');

  const helperFromBi = convertBigIntIpv6(1n);
  assert.equal(helperFromBi.bigInt, 1n);
  assert.equal(helperFromBi.correctForm, '::1');
}

// Contract 5: Address6.reverseForm generates standard ip6.arpa domain names and Address6.fromArpa parses them back to Address6
{
  const addr = new Address6('2001:db8::1');
  const reverseStr = addr.reverseForm();
  assert.equal(reverseStr, '1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa.');

  const parsedBack = Address6.fromArpa(reverseStr);
  assert.equal(parsedBack.correctForm(), '2001:db8::1');

  const dnsHelper = handleReverseDns('2001:db8::abcd');
  assert.equal(dnsHelper.reverse, 'd.c.b.a.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.b.d.0.1.0.0.2.ip6.arpa.');
  assert.equal(dnsHelper.parsed, '2001:db8::abcd');
}

// Contract 6: Address6.isValid validates full IPv6 addresses and CIDR subnets while rejecting invalid syntax and IPv4 strings
{
  assert.equal(validateIpv6('2001:db8::1'), true);
  assert.equal(validateIpv6('2001:db8::/32'), true);
  assert.equal(validateIpv6('::1'), true);
  assert.equal(validateIpv6('fe80::1ff:fe23:4567:890a'), true);

  assert.equal(validateIpv6('192.168.1.1'), false);
  assert.equal(validateIpv6('2001:::1'), false);
  assert.equal(validateIpv6('invalid-ip-string'), false);
  assert.equal(validateIpv6(''), false);
}

console.log('All contract assertions verified successfully.');

原始种子者

匿名