Skip to content

[Bug] 37_Signature: ECDSA.toEthSignedMessageHash should be internal, not public - #947

Open
pucedoteth wants to merge 1 commit into
AmazingAng:mainfrom
pucedoteth:fix-ecdsa-library-visibility
Open

pucedoteth wants to merge 1 commit into
AmazingAng:mainfrom
pucedoteth:fix-ecdsa-library-visibility

Conversation

@pucedoteth

Copy link
Copy Markdown

问题 / Problem

In 37_Signature/Signature.sol, the teaching ECDSA library declares two functions internal and one public:

library ECDSA {
    function verify(...) internal pure returns (bool) { ... }
    function recoverSigner(...) internal pure returns (address) { ... }
    function toEthSignedMessageHash(bytes32 hash) public pure returns (bytes32) { ... }  // <-- 
}

As chapter 17 (库合约) itself explains: 库合约中的函数可见性如果被设置为 public 或者 external,则在调用这个函数时会触发一次 delegatecall。

So this one public turns ECDSA into a separately deployed contract that must be linked into SignatureNFT. Compiling the chapter as-is:

$ forge build
$ jq '.bytecode.linkReferences' out/Signature.sol/SignatureNFT.json
{
  "src/37_Signature/Signature.sol": {
    "ECDSA": [ { "start": 4106, "length": 20 } ]
  }
}

The creation bytecode contains an unresolved placeholder __$e3d8e53151ebe0b7203e2d17c613eec2e4$__. Consequences for a learner following this chapter:

  1. Deploying SignatureNFT from the artifact with ethers.js / hardhat / web3 fails ("bytecode contains unresolved library placeholder") until ECDSA is deployed separately and linked in. Remix hides this by auto-deploying the library, so the chapter appears to work there and breaks everywhere else.
  2. Every mint() pays an extra DELEGATECALL to compute a pure keccak hash of a constant prefix.

This looks unintended rather than pedagogical:

  • The library's other two functions are internal.
  • The parallel MerkleProof library in chapter 36 is internal/private throughout and produces no link reference.
  • OpenZeppelin's own MessageHashUtils.toEthSignedMessageHash is internal.
  • This repo's English readme for this very chapter already says internal (Languages/en/37_Signature_en/readme.md:82) — only the .sol files and the zh/pt-br readmes still say public.

Chapter 17's Strings library is deliberately public to demonstrate library linking, so that one is left untouched.

修改 / Fix

public → internal, applied to the code and to the readme snippets that were still out of sync:

  • 37_Signature/Signature.sol
  • 37_Signature/readme.md
  • Languages/en/37_Signature_en/Signature.sol
  • Languages/pt-br/37_Signature/Signature.sol
  • Languages/pt-br/37_Signature/readme.md

After the change:

$ forge build
$ jq '.bytecode.linkReferences' out/Signature.sol/SignatureNFT.json
{}

SignatureNFT now compiles to fully linked bytecode and deploys standalone. The whole workspace still builds (forge build clean).

Note: the checked-in out/ artifacts are not regenerated in this PR — they will pick up the change on the next build.

The other two functions of this teaching ECDSA library are `internal`,
but `toEthSignedMessageHash` is `public`. As chapter 17 explains, a
`public`/`external` library function is reached through a `delegatecall`,
so the library becomes a separately deployed contract that has to be
linked: SignatureNFT's creation bytecode carries an unresolved
`__$e3d8e53151ebe0b7203e2d17c613eec2e4$__` placeholder, and deploying it
from the artifact fails until ECDSA is deployed and linked in.

Making it `internal` inlines the function, drops the link reference, and
removes a DELEGATECALL from every mint(). This matches MerkleProof in
chapter 36 and OpenZeppelin's MessageHashUtils, and matches the English
readme of this chapter, which already says `internal`.
@pucedoteth
pucedoteth force-pushed the fix-ecdsa-library-visibility branch from 5263b38 to e9da61f Compare August 17, 2026 00:11

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant