Created
June 21, 2023 20:31
-
-
Save Grubba27/8bbf9ed200258577ee1afddcacea9176 to your computer and use it in GitHub Desktop.
Generate public address using message and private from metamask
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 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