Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

web3 & go-ethereum generating different signature for same message & private key. #3524

Closed
MonarthS opened this issue May 18, 2020 · 4 comments

Comments

@MonarthS
Copy link

Trying to generate signature using web3:

signature = accounts.sign("hello", private_key);
console.log(signature);
// 0x0c196fc53f35d4f962a71d45111cbd5e9bb7005c97eb212d578b5f45d4da84315cc7ed41ba5f03e3f7a0f596db96276836d6094f005c78eede3bb278ab07b4c21b

Trying to generate signature using go-ethereum:

data := []byte("hello")
hash := crypto.Keccak256Hash(data)
fmt.Println(hash.Hex())
signature, err := crypto.Sign(hash.Bytes(), privateKey)
fmt.Println(hexutil.Encode(signature))
// 0xec911c13e732a6b72e40c731b32583db043c14c26014b4de31d1b1b35720a3580d392a739c525bfb27cebc8e12e5dce6209ad12ac2c1b278f59b4d2379bd72f700

Expected behavior

Signature should be same in web3 & go-ethereum.

Actual behavior

web3 & go-ethereum generating different signature for same message & private key.

Logs

The messageHash which is generated by web3 & go-ethereum are different.

web3:

messageHash: '0x50b2c43fd39106bafbba0da34fc430e1f91e3c96ea2acee2bc34119f92b37750'

go-ethereum:

messageHash: '0x1c8aff950685c2ed4bc3174f3472287b56d9517b9c948127319a09a7a36deac8'

Environment

node: v8.17.0
npm: 6.13.4
go: 1.14.2
OS: macOS Mojave 10.14.6

@MonarthS
Copy link
Author

Solve signature problem by adding \x19Ethereum Signed Message:\n as prefix of message in go-ethereum.

@SHocker-Yu
Copy link

SHocker-Yu commented Apr 12, 2022

please help, after add \x19Ethereum Signed Message:\n as prefix of message in go-ethereum, the signatures also different,
private_key is get by crypto.HexToECDSA(private_key)?

@SHocker-Yu
Copy link

Solve signature problem by adding length of the message,
message = fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(message), message)

@barry-en
Copy link

    message := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(msg), msg)
hash := crypto.Keccak256Hash([]byte(message))

signature, err := crypto.Sign(hash.Bytes(), privateKey)
if err != nil {
	fmt.Println(err)
}
signature[64] += 27
    fmt.Println("go:",hexutil.Encode(signature))

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

No branches or pull requests

3 participants