Skip to content

Instantly share code, notes, and snippets.

@Grubba27
Created June 21, 2023 20:31
Show Gist options
  • Save Grubba27/8bbf9ed200258577ee1afddcacea9176 to your computer and use it in GitHub Desktop.
Save Grubba27/8bbf9ed200258577ee1afddcacea9176 to your computer and use it in GitHub Desktop.
Generate public address using message and private from metamask
package cripto
import (
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
)
// from: https://gist.github.com/dcb9/385631846097e1f59e3cba3b1d42f3ed#file-eth_sign_verify-go
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()
}
// if return is "" something went wrong
func GeneratePublicAddress(message string, signature string) string {
sig := hexutil.MustDecode(signature)
msg := accounts.TextHash([]byte(message))
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 ""
}
recoveredAddr := crypto.PubkeyToAddress(*recovered)
return recoveredAddr.Hex()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment