Last active
August 13, 2024 16:01
-
-
Save dcb9/385631846097e1f59e3cba3b1d42f3ed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"github.com/ethereum/go-ethereum/accounts" | |
"github.com/ethereum/go-ethereum/common/hexutil" | |
"github.com/ethereum/go-ethereum/crypto" | |
) | |
func main() { | |
fmt.Println(verifySig( | |
"0x0EaE3eF6CC7176553E6B45d94e9eFDE2Da7B82a5", | |
"0x34850b7e36e635783df0563c7202c3ac776df59db5015d2b6f0add33955bb5c43ce35efb5ce695a243bc4c5dc4298db40cd765f3ea5612d2d57da1e4933b2f201b", | |
[]byte("Example `personal_sign` message"), | |
)) | |
} | |
func verifySig(from, sigHex string, msg []byte) bool { | |
sig := hexutil.MustDecode(sigHex) | |
msg = accounts.TextHash(msg) | |
if sig[crypto.RecoveryIDOffset] == 27 || sig[crypto.RecoveryIDOffset] == 28 { | |
sig[crypto.RecoveryIDOffset] -= 27 // Transform yellow paper V from 27/28 to 0/1 | |
} | |
recovered, err := crypto.SigToPub(msg, sig) | |
if err != nil { | |
return false | |
} | |
recoveredAddr := crypto.PubkeyToAddress(*recovered) | |
return from == recoveredAddr.Hex() | |
} |
+1
@psmithson
in that case you should use something like
return strings.ToLower(from) == strings.ToLower(recoveredAddress.Hex())
@psmithson in that case you should use something like
return strings.ToLower(from) == strings.ToLower(recoveredAddress.Hex())
or even better
return strings.EqualFold(from, recoveredAddr.Hex())
Very helpful!
here's another example:
https://github.com/verity-team/dws/blob/main/internal/delphi/server/server.go#L127
Sign in with Ethereum - frontend
https://docs.metamask.io/wallet/how-to/sign-data/siwe/
Signature checker in backend with go-ethereum with this code.
@valterlobo Very useful, thanks for sharing
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In our case, the last line had a bug, in which it needs the address to be lower cased, otherwise the comparison fails. Fix this bug lowering the case of the right parameter, like this:
return from == strings.ToLower(recoveredAddress.Hex())