From fefb793062a386b4e6bd08cf95ec0b82b81075b5 Mon Sep 17 00:00:00 2001 From: Brennan Lamey Date: Fri, 17 Jan 2025 00:46:18 -0600 Subject: [PATCH] trying something else --- core/crypto/auth/eth_personal_sign.go | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/core/crypto/auth/eth_personal_sign.go b/core/crypto/auth/eth_personal_sign.go index c6c27f5d5..817ef601a 100644 --- a/core/crypto/auth/eth_personal_sign.go +++ b/core/crypto/auth/eth_personal_sign.go @@ -1,9 +1,12 @@ package auth import ( + "bytes" "encoding/hex" "fmt" + ethAccounts "github.com/ethereum/go-ethereum/accounts" + ethCommon "github.com/ethereum/go-ethereum/common" ethCrypto "github.com/ethereum/go-ethereum/crypto" "golang.org/x/crypto/sha3" ) @@ -60,14 +63,27 @@ 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) + // signature is 65 bytes, [R || S || V] format + if len(signature) != 65 { + return fmt.Errorf("invalid signature length: expected %d, received %d", + 65, len(signature)) + } + + if signature[ethCrypto.RecoveryIDOffset] == 27 || + signature[ethCrypto.RecoveryIDOffset] == 28 { + // Transform yellow paper V from 27/28 to 0/1 + signature[ethCrypto.RecoveryIDOffset] -= 27 + } - if len(signature) == 65 { - signature = signature[:64] + hash := ethAccounts.TextHash(msg) + pubkeyBytes, err := ethCrypto.Ecrecover(hash, signature) + if err != nil { + return fmt.Errorf("invalid signature: recover public key failed: %w", err) } - if !ethCrypto.VerifySignature(identity, hash, signature) { - return fmt.Errorf("invalid signature") + addr := ethCommon.BytesToAddress(ethCrypto.Keccak256(pubkeyBytes[1:])[12:]) + if !bytes.Equal(addr.Bytes(), identity) { + return fmt.Errorf("invalid signature: expected address %x, received %x", identity, addr.Bytes()) } return nil