Skip to content

Commit

Permalink
reverted
Browse files Browse the repository at this point in the history
  • Loading branch information
brennanjl committed Jan 17, 2025
1 parent f7707c3 commit 0316fee
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 41 deletions.
30 changes: 21 additions & 9 deletions core/crypto/auth/eth_personal_sign.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package auth

import (
"bytes"
"encoding/hex"
"fmt"

"github.com/kwilteam/kwil-db/core/crypto"
ethCrypto "github.com/ethereum/go-ethereum/crypto"
"golang.org/x/crypto/sha3"
)

Expand Down Expand Up @@ -62,16 +61,29 @@ func eip55ChecksumAddr(addr [20]byte) string {
// Verify verifies applies the Ethereum TextHash digest and verifies the signature
func (EthSecp256k1Authenticator) Verify(identity []byte, msg []byte, signature []byte) error {
hash := textHash(msg)
pubkey, err := crypto.RecoverSecp256k1KeyFromSigHash(hash, signature)
if err != nil {
return err
}

addr := crypto.EthereumAddressFromPubKey(pubkey)
if len(signature) == 65 {
signature = signature[:64]
}

if !bytes.Equal(addr, identity) {
return fmt.Errorf("invalid signature: expected address %x, received %x", identity, addr)
if !ethCrypto.VerifySignature(identity, hash, signature) {
return fmt.Errorf("invalid signature")
}

return nil

/*
pubkey, err := crypto.RecoverSecp256k1KeyFromSigHash(hash, signature)
if err != nil {
return err
}
addr := crypto.EthereumAddressFromPubKey(pubkey)
if !bytes.Equal(addr, identity) {
return fmt.Errorf("invalid signature: expected address %x, received %x", identity, addr)
}
return nil
*/
}
43 changes: 11 additions & 32 deletions core/crypto/secp256k1.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/decred/dcrd/dcrec/secp256k1/v4" // key/curve
"github.com/decred/dcrd/dcrec/secp256k1/v4/ecdsa" // signature algorithm
ethCrypto "github.com/ethereum/go-ethereum/crypto"
"golang.org/x/crypto/sha3"
)

Expand Down Expand Up @@ -164,41 +163,21 @@ func (k *Secp256k1PublicKey) VerifyRaw(data, rawSig []byte) (success bool, err e
}
}()

panic("WAHAHAH")

if len(rawSig) == 65 {
// we choose `VerifySignature` since it doesn't care recovery ID
// it expects signature in 64 byte [R || S] format
rawSig = rawSig[:len(rawSig)-1]
if len(rawSig) != 65 {
return false, errors.New("invalid signature length")
}

if len(rawSig) != 64 {
return false, fmt.Errorf("secp256k1: invalid signature length: expected: 64 or 65, received: %d", len(rawSig))
}
rawSig = rawSig[:RecoveryIDOffset]

if !ethCrypto.VerifySignature(k.Bytes(), data, rawSig) {
return false, nil
var r, s secp256k1.ModNScalar
if r.SetByteSlice(rawSig[:32]) {
return false, errors.New("r value overflow")
}

return true, nil
/*
if len(rawSig) != 65 {
return false, errors.New("invalid signature length")
}
rawSig = rawSig[:RecoveryIDOffset]
var r, s secp256k1.ModNScalar
if r.SetByteSlice(rawSig[:32]) {
return false, errors.New("r value overflow")
}
if s.SetByteSlice(rawSig[32:]) {
return false, errors.New("s value overflow")
}
sig := ecdsa.NewSignature(&r, &s)
return sig.Verify(data, (*secp256k1.PublicKey)(k)), nil
*/
if s.SetByteSlice(rawSig[32:]) {
return false, errors.New("s value overflow")
}
sig := ecdsa.NewSignature(&r, &s)
return sig.Verify(data, (*secp256k1.PublicKey)(k)), nil
}

// SignatureLength indicates the byte length required to carry a signature with recovery id.
Expand Down

0 comments on commit 0316fee

Please sign in to comment.