From beac44bcef9f4b0a2546399a35e113b1f3b1a5dd Mon Sep 17 00:00:00 2001 From: Ilya Koltsov Date: Wed, 18 Oct 2023 20:01:53 +0100 Subject: [PATCH] fix to much old sign format --- types/keypair/secp256k1/private_key.go | 3 ++- types/keypair/secp256k1/public_key.go | 24 ++++++++++++++++++++---- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/types/keypair/secp256k1/private_key.go b/types/keypair/secp256k1/private_key.go index b5970ee..55d671e 100644 --- a/types/keypair/secp256k1/private_key.go +++ b/types/keypair/secp256k1/private_key.go @@ -19,7 +19,8 @@ func (v PrivateKey) PublicKeyBytes() []byte { func (v PrivateKey) Sign(mes []byte) ([]byte, error) { hash := sha256.Sum256(mes) - return ecdsa.Sign(v.key, hash[:]).Serialize(), nil + // Return the signature as a concatenation of the R and S values in big-endian to match the old signature format. + return ecdsa.SignCompact(v.key, hash[:], false)[1:], nil } func NewPrivateKeyFromPemFile(path string) (PrivateKey, error) { diff --git a/types/keypair/secp256k1/public_key.go b/types/keypair/secp256k1/public_key.go index 3a06aa3..123e451 100644 --- a/types/keypair/secp256k1/public_key.go +++ b/types/keypair/secp256k1/public_key.go @@ -22,10 +22,26 @@ func (v PublicKey) Bytes() []byte { // VerifySignature verifies a signature of the form R || S. // It rejects signatures which are not in lower-S form. func (v PublicKey) VerifySignature(msg []byte, sigStr []byte) bool { - signature, err := ecdsa.ParseDERSignature(sigStr) - if err != nil { - log.Println(err) - return false + var signature *ecdsa.Signature + var err error + // if old signature len = 64, parse it as raw signature + if len(sigStr) == 64 { + // Split the signature bytes into r and s values and parse them into ModNScalar + var r, s secp256k1.ModNScalar + var bytesR [32]byte + var bytesS [32]byte + copy(bytesR[:], sigStr[:32]) + copy(bytesS[:], sigStr[32:]) + r.SetBytes(&bytesR) + s.SetBytes(&bytesS) + + signature = ecdsa.NewSignature(&r, &s) + } else { + signature, err = ecdsa.ParseDERSignature(sigStr) + if err != nil { + log.Println(err) + return false + } } hash := sha256.Sum256(msg) return signature.Verify(hash[:], v.key)