@casper-ecosystem/casper-eip-712

中文文档
1.2.1发布于 2026/05/14Apache-2.0TypeScriptESM + CJS构建来源已验证近 30 天下载 1.3万

EIP-712 typed data hashing and verification for Casper Network

原始使用文档
文档目录7 节
  1. Install
  2. Quick start
  3. Casper-specific domains
  4. Building the canonical type string
  5. Rust-mirroring convenience helpers
  6. API surface
  7. Development

@casper-ecosystem/casper-eip-712

TypeScript companion package for the Rust casper-eip-712 crate.

It provides EIP-712 typed data hashing plus ECDSA signature recovery for Casper-oriented flows, including support for Casper-specific domain fields when you supply explicit domain types.

Install

npm install @casper-ecosystem/casper-eip-712

Quick start

import {
  hashTypedData,
  recoverTypedDataSigner,
  verifySignature,
  PermitTypes,
  type PermitMessage,
} from "@casper-ecosystem/casper-eip-712";

const domain = {
  name: "My Token",
  version: "1",
  chainId: 1,
  verifyingContract: "0x1111111111111111111111111111111111111111",
};

const message: PermitMessage = {
  owner: "0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  spender: "0xbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
  value: "0x01",
  nonce: "0x00",
  deadline: "0xffff",
};

const signature = new Uint8Array(65); // Illustrative placeholder only; use a real 65-byte signature in production.
const digest = hashTypedData(domain, PermitTypes, "Permit", message);
const signer = recoverTypedDataSigner(domain, PermitTypes, "Permit", message, signature);
const isValid = verifySignature(digest, signature, "0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");

Casper-specific domains

If your domain uses Casper-specific fields such as chain_name or contract_package_hash, pass explicit domainTypes so the package knows the intended domain schema.

The chain_name field is recommended to be a CAIP-2 chain id of the form casper:<chainspec_name> — for example casper:casper for mainnet and casper:casper-test for testnet.

import { hashTypedData } from "@casper-ecosystem/casper-eip-712";

const domain = {
  name: "CSPR",
  version: "1",
  chain_name: "casper:casper-test",
  contract_package_hash:
    "0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
};

const digest = hashTypedData(domain, types, "Permit", message, {
  domainTypes: [
    { name: "name", type: "string" },
    { name: "version", type: "string" },
    { name: "chain_name", type: "string" },
    { name: "contract_package_hash", type: "bytes32" },
  ],
});

Building the canonical type string

When you need the EIP-712 canonical type string for a primary type use buildCanonicalTypeString. It resolves nested struct dependencies and appends them in alphabetical order, as required by the EIP-712 spec.

import { buildCanonicalTypeString, computeTypeHash } from "@casper-ecosystem/casper-eip-712";

const types = {
  Person: [
    { name: "name", type: "string" },
    { name: "wallet", type: "address" },
  ],
  Mail: [
    { name: "from", type: "Person" },
    { name: "to", type: "Person" },
    { name: "contents", type: "string" },
  ],
};

const typeString = buildCanonicalTypeString("Mail", types);
// "Mail(Person from,Person to,string contents)Person(string name,address wallet)"

const typeHash = computeTypeHash(typeString);

Rust-mirroring convenience helpers

For callers that want the lower-level shape from the Rust crate, the package also exposes:

import {
  buildDomain,
  CASPER_DOMAIN_TYPES,
  computeTypeHash,
  encodeAddress,
  encodeUint256,
  hashTypedDataRaw,
} from "@casper-ecosystem/casper-eip-712";

const domain = buildDomain(
  "CasperToken",
  "1",
  "casper:casper-test",
  "0x7777777777777777777777777777777777777777777777777777777777777777",
);

const typeHash = computeTypeHash(
  "Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)",
);

const encodedStruct = new Uint8Array(32 * 5);
encodedStruct.set(encodeAddress("0x2222222222222222222222222222222222222222"), 0);
encodedStruct.set(encodeAddress("0x3333333333333333333333333333333333333333"), 32);
encodedStruct.set(encodeUint256(1n), 64);
encodedStruct.set(encodeUint256(0n), 96);
encodedStruct.set(encodeUint256(999999n), 128);

const digest = hashTypedDataRaw(domain, typeHash, encodedStruct, {
  domainTypes: CASPER_DOMAIN_TYPES,
});

API surface

  • buildDomain(name, version, chainName, contractPackageHash)
  • hashDomainSeparator(domain, domainTypes?)
  • hashStruct(primaryType, types, message)
  • hashTypedData(domain, types, primaryType, message, options?)
  • hashTypedDataRaw(domain, typeHash, encodedStruct, options?)
  • buildCanonicalTypeString(primaryType, types)
  • computeTypeHash(typeString)
  • recoverAddress(digest, signature)
  • recoverTypedDataSigner(domain, types, primaryType, message, signature, options?)
  • verifySignature(digest, signature, expectedAddress)
  • Encoding helpers: encodeAddress, encodeUint256, encodeUint64, encodeBytes32, encodeBytes, encodeString, encodeBool, encodeField
  • Domain helpers: CASPER_DOMAIN_TYPES, buildDomainTypeString
  • Prebuilt message types: PermitTypes, ApprovalTypes, TransferTypes

Development

npm test
npm run build

Cross-language parity is covered by test vectors in ../tests/vectors.json.

版本与发布记录

@casper-ecosystem/casper-eip-712 共发布 5 个版本,首次发布于 2026/03/17;过去 12 个月发布了 5 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.2.1latest2026/05/145 个月前
1.2.02026/04/266 个月前
1.1.02026/04/106 个月前
1.0.12026/03/187 个月前
1.0.02026/03/177 个月前

关于 @casper-ecosystem/casper-eip-712 的常见问题

@casper-ecosystem/casper-eip-712 是什么?

EIP-712 typed data hashing and verification for Casper Network

如何安装 @casper-ecosystem/casper-eip-712?

在项目目录运行 npm install @casper-ecosystem/casper-eip-712,或使用 pnpm add / yarn add / bun add。

@casper-ecosystem/casper-eip-712 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@casper-ecosystem/casper-eip-712 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@casper-ecosystem/casper-eip-712 可以商用吗?

@casper-ecosystem/casper-eip-712 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@casper-ecosystem/casper-eip-712 的下载量有多少?

根据 npm 官方下载统计,@casper-ecosystem/casper-eip-712 在 2026/10/09 – 2026/11/07 这 30 天内被下载 13,017 次,日均约 434 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@casper-ecosystem/casper-eip-712 还在维护吗?

最新版本 1.2.1 发布于 2026/05/14(5 个月前);至今共发布 5 个版本,其中过去 12 个月发布了 5 个;首次发布于 2026/03/17。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。