Skip to content

Instantly share code, notes, and snippets.

@RyoJerryYu
Forked from dcb9/eth_sign_verify.go
Last active March 29, 2022 03:53
Show Gist options
  • Save RyoJerryYu/acabb8783dc70e0aefd7fb77a213628c to your computer and use it in GitHub Desktop.
Save RyoJerryYu/acabb8783dc70e0aefd7fb77a213628c to your computer and use it in GitHub Desktop.
package verify
import (
"testing"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/assert"
)
func VerifySig(from, sigHex string, msg []byte) bool {
sig, err := hexutil.Decode(sigHex)
if err != nil {
return false
}
msg = accounts.TextHash(msg)
sig[crypto.RecoveryIDOffset] -= 27
pub, err := crypto.SigToPub(msg, sig)
if err != nil {
return false
}
addr := crypto.PubkeyToAddress(*pub)
givenAddr := common.HexToAddress(from)
return addr == givenAddr
}
func TestVerifySig(t *testing.T) {
from := "0x925a0ffacd6f5336d07ad01bef4717cb67064c98"
sigHex := "0xdce77c911b3b4062be94110f392ffc040cb43600fad3b8483319fb9e7fa6241e14321efa7b7865d4841548f27be41124ec57ecbb5daf1bf30571e629d445c8511b"
msg := []byte("0x8c386d0f0125c77139c8")
assert.True(t, VerifySig(from, sigHex, msg))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment