帮助开发者在 Node.js 与 TypeScript 中正确使用以太坊 Keccak-256 哈希。
复制安装指令,让 AI 自动完成配置 · 推荐新手
请帮我安装 askskill 上的 "nodejs-keccak256" 技能: 1. 下载 https://raw.githubusercontent.com/affaan-m/ECC/main/skills/nodejs-keccak256/SKILL.md 2. 保存为 ~/.claude/skills/nodejs-keccak256/SKILL.md 3. 装好后重载技能,告诉我可以用了
请检查这段 Node.js/TypeScript 代码是否把以太坊需要的 Keccak-256 误写成了 Node 内置的 sha3-256,并说明风险、给出正确替代方案和示例代码。
指出错误哈希用法、解释对选择器和签名等的影响,并给出可直接替换的正确代码。
我需要在 JavaScript 中计算 Solidity 函数签名的 4 字节 selector。请使用以太坊 Keccak-256 而不是 NIST SHA3,给出完整示例,并演示如何从函数签名字符串得到 selector。
返回可运行示例,正确计算 selector,并明确说明为何不能使用 sha3-256。
请帮我排查这段脚本中地址派生、事件 topic 或存储槽计算不一致的问题,重点检查是否混用了 sha3-256 与 Keccak-256,并输出修复建议、测试用例和注意事项。
输出问题定位结果、修复后的哈希方案,以及用于回归验证的测试建议。
Ethereum uses Keccak-256, not the NIST-standardized SHA3 variant exposed by Node's crypto.createHash('sha3-256').
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
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],
);
import { keccak256, toBytes } from 'viem';
const hash = keccak256(toBytes('hello'));
const hash = web3.utils.keccak256('hello');
const packed = web3.utils.soliditySha3(
{ type: 'address', value: '0x742d35Cc6634C0532925a3b8D4C9B569890FaC1c' },
{ type: 'uint256', value: '100' },
);
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]),
);
}
import { keccak256 } from 'ethers';
function pubkeyToAddress(pubkeyBytes: Uint8Array): string {
const hash = keccak256(pubkeyBytes.slice(1));
return '0x' + hash.slice(-40);
}
grep -rn "createHash.*sha3" --include="*.ts" --include="*.js" --exclude-dir=node_modules .
grep -rn "keccak256" --include="*.ts" --include="*.js" . | grep -v node_modules
For Ethereum contexts, never use crypto.createHash('sha3-256'). Use Keccak-aware helpers from ethers, viem, web3, or another explicit Keccak implementation.
帮助开发者使用 Bun 进行运行、打包、测试与依赖管理,并评估替代 Node 的时机。
帮助用户计算加密哈希并执行密钥派生等安全相关操作。