From bd6eed4e9752c379dfdd873a0c6e31f044230301 Mon Sep 17 00:00:00 2001 From: Roman Behma <13855864+begmaroman@users.noreply.github.com> Date: Wed, 31 Jul 2024 16:04:12 +0100 Subject: [PATCH] Removed signature normalisation (#111) --- types/sign.go | 34 +++++++++++----------------------- 1 file changed, 11 insertions(+), 23 deletions(-) diff --git a/types/sign.go b/types/sign.go index 56072eae..0eb2472c 100644 --- a/types/sign.go +++ b/types/sign.go @@ -2,42 +2,30 @@ package types import ( "crypto/ecdsa" - "math/big" + "errors" "strings" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" ) -// Sign the hashToSIgn +var ( + // ErrNonCanonicalSignature is returned when the signature is not canonical. + ErrNonCanonicalSignature = errors.New("received non-canonical signature") +) + +// Sign the hashToSIgn with the given privateKey. func Sign(privateKey *ecdsa.PrivateKey, hashToSign []byte) ([]byte, error) { sig, err := crypto.Sign(hashToSign, privateKey) if err != nil { return nil, err } - rBytes := sig[:32] - sBytes := sig[32:64] - vByte := sig[64] - - if strings.ToUpper(common.Bytes2Hex(sBytes)) > "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0" { - magicNumber := common.Hex2Bytes("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141") - sBig := big.NewInt(0).SetBytes(sBytes) - magicBig := big.NewInt(0).SetBytes(magicNumber) - s1 := magicBig.Sub(magicBig, sBig) - sBytes = s1.Bytes() - if vByte == 0 { - vByte = 1 - } else { - vByte = 0 - } + if strings.ToUpper(common.Bytes2Hex(sig[32:64])) > "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0" { + return nil, ErrNonCanonicalSignature } - vByte += 27 - actualSignature := []byte{} - actualSignature = append(actualSignature, rBytes...) - actualSignature = append(actualSignature, sBytes...) - actualSignature = append(actualSignature, vByte) + sig[64] += 27 - return actualSignature, nil + return sig, nil }