Skip to content

tokoinofficial/go-sign-verify-message

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

sign-verify-message

This repos shows the way to sign/verify an ethereum message. It's used for backend of third party where interacts with Tokoin Payment System.

Tokoin Payment System

How to use

  • Sign message
func Sign(pk string, types []string, values []interface{}) (string, error) {
	privateKey, err := crypto.HexToECDSA(pk[2:])
	if err != nil {
		return "", err
	}

	hash := solsha3.SoliditySHA3(
		types,
		values,
	)

	signatureBytes, _ := crypto.Sign(SignHash(hash), privateKey)
	sig := hexutil.Encode(signatureBytes)
	signature := ""
	if sig[len(sig)-2:] == "00" {
		signature = sig[0:len(sig)-2] + "1b"
	} else {
		signature = sig[0:len(sig)-2] + "1c"
	}

	return signature, nil
}
  • Verify message
func Verify(from, sigHex string, msg []byte, ignoreVersion bool) bool {
	fromAddr := common.HexToAddress(from)

	sig := hexutil.MustDecode(sigHex)

	if !ignoreVersion {
		// If the version(v) is correct return the signer address
		if sig[64] != 27 && sig[64] != 28 {
			return false
		}

		// Version(v) of signature should be 27 or 28, but 0 and 1 are also possible versions
		sig[64] -= 27
	}

	pubKey, err := crypto.SigToPub(SignHash(msg), sig)
	if err != nil {
		return false
	}

	recoveredAddr := crypto.PubkeyToAddress(*pubKey)
	return fromAddr == recoveredAddr
}

How to run sample

  • go mod tidy
  • go run main.go

Demo

Created & Maintained By

Trong Dinh

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages