跳到正文
Skillpedia
通用编程280k · #367

nodejs-keccak256

它能做什么

这个工具可以确保你的 JavaScript 和 TypeScript 代码在为以太坊计算“哈希”(一种把数据转换成唯一字符串的函数)时使用正确的方法。用错函数可能导致交易等关键操作出错。

适用场景

  • ✓编写与以太坊交互的代码时。
  • ✓需要为函数或事件生成唯一标识符时。
  • ✓怀疑以太坊相关代码出现异常错误时。

技能链接

https://github.com/affaan-m/ECC/blob/main/skills/nodejs-keccak256/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/affaan-m/ECC/blob/main/skills/nodejs-keccak256/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“我怎样才能用 JavaScript 正确计算以太坊的消息哈希?”

每周 5 个新技能,1 分钟读完。

工作原理

它的做法是用一个符合正确 Keccak-256 标准的哈希函数,替换 Node.js 默认的哈希函数(两者并不相同)。这样就不会让你的代码在毫无提示的情况下产生错误结果。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Node.js Keccak-256

Ethereum uses Keccak-256, not the NIST-standardized SHA3 variant exposed by Node's crypto.createHash('sha3-256').

When to Use

  • Computing Ethereum function selectors or event topics
  • Building EIP-712, signature, Merkle, or storage-slot helpers in JS/TS
  • Reviewing any code that hashes Ethereum data with Node crypto directly

How It Works

The two algorithms produce different outputs for the same input, and Node will not warn you.

import crypto from 'crypto';
import { keccak256, toUtf8Bytes } from 'ethers';

const data = 'hello';
const nistSha3 = crypto.createHash('sha3-256').update(data).digest('hex');
const keccak = keccak256(toUtf8Bytes(data)).slice(2);

console.log(nistSha3 === keccak); // false

Examples

ethers v6

import { keccak256, toUtf8Bytes, solidityPackedKeccak256, id } from 'ethers';

const hash = keccak256(new Uint8Array([0x01, 0x02]));
const hash2 = keccak256(toUtf8Bytes('hello'));
const topic = id('Transfer(address,address,uint256)');
const packed = solidityPackedKeccak256(
  ['address', 'uint256'],
  ['0x742d35Cc6634C0532925a3b8D4C9B569890FaC1c', 100n],
);

viem

import { keccak256, toBytes } from 'viem';

const hash = keccak256(toBytes('hello'));

web3.js

const hash = web3.utils.keccak256('hello');
const packed = web3.utils.soliditySha3(
  { type: 'address', value: '0x742d35Cc6634C0532925a3b8D4C9B569890FaC1c' },
  { type: 'uint256', value: '100' },
);

Common patterns

import { id, keccak256, AbiCoder } from 'ethers';

const selector = id('transfer(address,uint256)').slice(0, 10);
const typeHash = keccak256(toUtf8Bytes('Transfer(address from,address to,uint256 value)'));

function getMappingSlot(key: string, mappingSlot: number): string {
  return keccak256(
    AbiCoder.defaultAbiCoder().encode(['address', 'uint256'], [key, mappingSlot]),
  );
}

Address from public key

import { keccak256 } from 'ethers';

function pubkeyToAddress(pubkeyBytes: Uint8Array): string {
  const hash = keccak256(pubkeyBytes.slice(1));
  return '0x' + hash.slice(-40);
}

Audit your codebase

grep -rn "createHash.*sha3" --include="*.ts" --include="*.js" --exclude-dir=node_modules .
grep -rn "keccak256" --include="*.ts" --include="*.js" . | grep -v node_modules

Rule

For Ethereum contexts, never use crypto.createHash('sha3-256'). Use Keccak-aware helpers from ethers, viem, web3, or another explicit Keccak implementation.

常见问题

Node.js 的哈希为什么和以太坊的 Keccak-256 不一样?

Node.js 使用的是 SHA3 标准,而以太坊使用的是略有不同的 Keccak-256 标准。两者看起来很像,但对同样的数据会得出不同的结果。

如果用错了哈希会怎样?

你的代码在与以太坊网络交互时可能会失败,比如发送交易、签名消息或验证数据。这类错误往往很难追查。

我需要了解 Keccak-256 的内部原理吗?

不需要。这个工具会替你处理这些细节,你只需使用正确的函数,无需担心其中的复杂性。

详情

作者的原始描述

Prevent Ethereum hashing bugs in JavaScript and TypeScript. Node's sha3-256 is NIST SHA3, not Ethereum Keccak-256, and silently breaks selectors, signatures, storage slots, and address derivation. Use when hashing for Ethereum in JavaScript or TypeScript, or when a selector, signature, storage slot, or derived address is wrong.

repo: affaan-m/ECC

热度

GitHub 星标
280k
排名
#367
许可证
MIT
更新时间
2026/10/15

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能