diff options
Diffstat (limited to 'cli/verify_signature.go')
| -rw-r--r-- | cli/verify_signature.go | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/cli/verify_signature.go b/cli/verify_signature.go new file mode 100644 index 0000000..631adac --- /dev/null +++ b/cli/verify_signature.go @@ -0,0 +1,58 @@ +package main + +import ( + "bytes" + "crypto/ecdsa" + "fmt" + "log" + + "github.com/ethereum/go-ethereum/common/hexutil" + "github.com/ethereum/go-ethereum/crypto" +) + +func main() { + privateKey, err := crypto.HexToECDSA("fad9c8855b740a0b7ed4c221dbad0f33a83a49cad6b3fe8d5817ac83d38b6a19") + if err != nil { + log.Fatal(err) + } + + publicKey := privateKey.Public() + publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey) + if !ok { + log.Fatal("error casting public key to ECDSA") + } + + publicKeyBytes := crypto.FromECDSAPub(publicKeyECDSA) + + data := []byte("decartography") + hash := crypto.Keccak256Hash(data) + fmt.Println(hash.Hex()) // 0xfe2b8f0bae0d344e95a421b9cc1a32ef07083cc85d007146259221fece9264a7 + + signature, err := crypto.Sign(hash.Bytes(), privateKey) + if err != nil { + log.Fatal(err) + } + + fmt.Println(hexutil.Encode(signature)) + + sigPublicKey, err := crypto.Ecrecover(hash.Bytes(), signature) + if err != nil { + log.Fatal(err) + } + + matches := bytes.Equal(sigPublicKey, publicKeyBytes) + fmt.Println(matches) // true + + sigPublicKeyECDSA, err := crypto.SigToPub(hash.Bytes(), signature) + if err != nil { + log.Fatal(err) + } + + sigPublicKeyBytes := crypto.FromECDSAPub(sigPublicKeyECDSA) + matches = bytes.Equal(sigPublicKeyBytes, publicKeyBytes) + fmt.Println(matches) // true + + signatureNoRecoverID := signature[:len(signature)-1] // remove recovery id + verified := crypto.VerifySignature(publicKeyBytes, hash.Bytes(), signatureNoRecoverID) + fmt.Println(verified) // true +}
\ No newline at end of file |
