本文へスキップ
Skillpedia
一般的なプログラミング280k · #367

nodejs-keccak256

できること

このツールは、JavaScriptやTypeScriptのコードでEthereumに対応した正しい「ハッシュ」関数(データを一意の文字列に変換する関数)を使えるようにします。誤った関数を使うと、トランザクションなどの重要な処理でエラーが発生する可能性があります。

使う場面

  • ✓Ethereumと連携するコードを書くとき。
  • ✓関数やイベントの一意な識別子を生成する必要があるとき。
  • ✓Ethereumのコードで予期しないエラーが起きていると思われるとき。

スキルのリンク

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

コピーしてAI(Claude、ChatGPT、Cursor)に貼り付け、指示に従うよう頼んでください。

使い方

いちばん簡単な方法:メッセージをコピーしてAIに貼り付けるだけ。AIがスキルを読んで指示に従います。

貼り付け用メッセージ

このスキルを使って手伝ってください:https://github.com/affaan-m/ECC/blob/main/skills/nodejs-keccak256/SKILL.md このリンクのSKILL.mdファイルを読んで、私の作業でその指示に従ってください。
こういうスキルを、AIが自分で見つけてくれたら?無料のSkillpedia MCPをインストール。1行で、作業の前に15,000+のスキルから探してくれます。1分でインストール →

AIへの頼み方の例

“JavaScriptでEthereum向けにメッセージの「ハッシュ」を正しく計算するにはどうすればよいですか?”

毎週5つの新しいスキル。1分で読めます。

仕組み

Node.jsに標準で備わっているハッシュ関数は、Ethereumが使う方式とは異なります。このツールは、その標準関数を正しいKeccak-256の仕様に従うものに置き換えます。これにより、警告なしに誤った結果が出ることを防ぎます。

技術的な内容(元の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のハッシュとEthereumのKeccak-256が異なるのはなぜですか?

Node.jsはSHA3という規格を使い、Ethereumは少し異なるKeccak-256という規格を使います。見た目は似ていますが、同じデータに対しても異なる出力になります。

誤ったハッシュを使うとどうなりますか?

Ethereumネットワークとの連携で失敗する可能性があります。たとえば、トランザクションの送信、メッセージの署名、データの検証などで問題が起きることがあります。原因を特定しにくいエラーになることもあります。

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

ログイン して投票しましょう。

コメント

  • …

このリポジトリの他のスキル

似ているスキル