Skip to content

Commit

Permalink
fix to much old sign format
Browse files Browse the repository at this point in the history
  • Loading branch information
koltsov-iv committed Oct 18, 2023
1 parent cfae70c commit beac44b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
3 changes: 2 additions & 1 deletion types/keypair/secp256k1/private_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
24 changes: 20 additions & 4 deletions types/keypair/secp256k1/public_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit beac44b

Please sign in to comment.