From 0316fee5d16a1a6743ae32d625536e70e432eb26 Mon Sep 17 00:00:00 2001 From: Brennan Lamey Date: Fri, 17 Jan 2025 00:40:02 -0600 Subject: [PATCH] reverted --- core/crypto/auth/eth_personal_sign.go | 30 +++++++++++++------ core/crypto/secp256k1.go | 43 +++++++-------------------- 2 files changed, 32 insertions(+), 41 deletions(-) diff --git a/core/crypto/auth/eth_personal_sign.go b/core/crypto/auth/eth_personal_sign.go index e0bc87bea..c6c27f5d5 100644 --- a/core/crypto/auth/eth_personal_sign.go +++ b/core/crypto/auth/eth_personal_sign.go @@ -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" ) @@ -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 + */ } diff --git a/core/crypto/secp256k1.go b/core/crypto/secp256k1.go index 73abb2fb0..aa8cba889 100644 --- a/core/crypto/secp256k1.go +++ b/core/crypto/secp256k1.go @@ -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" ) @@ -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.