diff --git a/core/auth.go b/core/auth.go index 7e7669aa2..8937f3690 100644 --- a/core/auth.go +++ b/core/auth.go @@ -1,12 +1,10 @@ package core import ( - "bytes" "errors" "fmt" geth "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/crypto" ) @@ -25,24 +23,14 @@ func VerifySignature(message []byte, accountAddr geth.Address, sig []byte) error return fmt.Errorf("signature length is unexpected: %d", len(sig)) } - publicKeyBytes, err := hexutil.Decode(accountAddr.Hex()) - if err != nil { - return fmt.Errorf("failed to decode public key (%v): %v", accountAddr.Hex(), err) - } - - // Decode public key - pubKey, err := crypto.UnmarshalPubkey(publicKeyBytes) - if err != nil { - return fmt.Errorf("failed to decode public key (%v): %v", accountAddr.Hex(), err) - } - // Verify the signature sigPublicKeyECDSA, err := crypto.SigToPub(message, sig) if err != nil { return fmt.Errorf("failed to recover public key from signature: %v", err) } - if !bytes.Equal(pubKey.X.Bytes(), sigPublicKeyECDSA.X.Bytes()) || !bytes.Equal(pubKey.Y.Bytes(), sigPublicKeyECDSA.Y.Bytes()) { + pubKey := crypto.PubkeyToAddress(*sigPublicKeyECDSA).Hex() + if pubKey != accountAddr.Hex() { return errors.New("signature doesn't match with provided public key") } diff --git a/core/auth/authenticator.go b/core/auth/authenticator.go index 74fe4bf50..64024f5cb 100644 --- a/core/auth/authenticator.go +++ b/core/auth/authenticator.go @@ -1,14 +1,12 @@ package auth import ( - "bytes" "encoding/binary" "errors" "fmt" "github.com/Layr-Labs/eigenda/core" - "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/crypto" ) @@ -31,25 +29,15 @@ func (*authenticator) AuthenticateBlobRequest(header core.BlobAuthHeader) error buf := make([]byte, 4) binary.BigEndian.PutUint32(buf, header.Nonce) hash := crypto.Keccak256(buf) - - publicKeyBytes, err := hexutil.Decode(header.AccountID) - if err != nil { - return fmt.Errorf("failed to decode public key (%v): %v", header.AccountID, err) - } - - // Decode public key - pubKey, err := crypto.UnmarshalPubkey(publicKeyBytes) - if err != nil { - return fmt.Errorf("failed to decode public key (%v): %v", header.AccountID, err) - } - // Verify the signature sigPublicKeyECDSA, err := crypto.SigToPub(hash, sig) if err != nil { return fmt.Errorf("failed to recover public key from signature: %v", err) } - if !bytes.Equal(pubKey.X.Bytes(), sigPublicKeyECDSA.X.Bytes()) || !bytes.Equal(pubKey.Y.Bytes(), sigPublicKeyECDSA.Y.Bytes()) { + pubKey := crypto.PubkeyToAddress(*sigPublicKeyECDSA).Hex() + + if pubKey != header.AccountID { return errors.New("signature doesn't match with provided public key") } diff --git a/core/auth/v2/auth_test.go b/core/auth/v2/auth_test.go index fb7c60cec..8893a24c9 100644 --- a/core/auth/v2/auth_test.go +++ b/core/auth/v2/auth_test.go @@ -144,7 +144,7 @@ func TestAuthenticatePaymentStateRequestInvalidPublicKey(t *testing.T) { err := authenticator.AuthenticatePaymentStateRequest(make([]byte, 65), "not-hex-encoded") assert.Error(t, err) - assert.Contains(t, err.Error(), "failed to decode public key") + assert.Contains(t, err.Error(), "failed to recover public key from signature") } func TestAuthenticatePaymentStateRequestSignatureMismatch(t *testing.T) { diff --git a/core/auth/v2/authenticator.go b/core/auth/v2/authenticator.go index 055a0edfb..ecd8b0ba7 100644 --- a/core/auth/v2/authenticator.go +++ b/core/auth/v2/authenticator.go @@ -1,13 +1,11 @@ package v2 import ( - "bytes" "crypto/sha256" "errors" "fmt" core "github.com/Layr-Labs/eigenda/core/v2" - "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/crypto" ) @@ -32,24 +30,16 @@ func (*authenticator) AuthenticateBlobRequest(header *core.BlobHeader) error { return fmt.Errorf("failed to get blob key: %v", err) } - publicKeyBytes, err := hexutil.Decode(header.PaymentMetadata.AccountID) - if err != nil { - return fmt.Errorf("failed to decode public key (%v): %v", header.PaymentMetadata.AccountID, err) - } - - // Decode public key - pubKey, err := crypto.UnmarshalPubkey(publicKeyBytes) - if err != nil { - return fmt.Errorf("failed to convert bytes to public key (%v): %v", header.PaymentMetadata.AccountID, err) - } - - // Verify the signature + // Recover public key from signature sigPublicKeyECDSA, err := crypto.SigToPub(blobKey[:], sig) if err != nil { return fmt.Errorf("failed to recover public key from signature: %v", err) } - if !bytes.Equal(pubKey.X.Bytes(), sigPublicKeyECDSA.X.Bytes()) || !bytes.Equal(pubKey.Y.Bytes(), sigPublicKeyECDSA.Y.Bytes()) { + accountId := header.PaymentMetadata.AccountID + pubKey := crypto.PubkeyToAddress(*sigPublicKeyECDSA).Hex() + + if pubKey != accountId { return errors.New("signature doesn't match with provided public key") } @@ -62,18 +52,6 @@ func (*authenticator) AuthenticatePaymentStateRequest(sig []byte, accountId stri return fmt.Errorf("signature length is unexpected: %d", len(sig)) } - // Decode public key - publicKeyBytes, err := hexutil.Decode(accountId) - if err != nil { - return fmt.Errorf("failed to decode public key (%v): %v", accountId, err) - } - - // Convert bytes to public key - pubKey, err := crypto.UnmarshalPubkey(publicKeyBytes) - if err != nil { - return fmt.Errorf("failed to convert bytes to public key (%v): %v", accountId, err) - } - // Verify the signature hash := sha256.Sum256([]byte(accountId)) sigPublicKeyECDSA, err := crypto.SigToPub(hash[:], sig) @@ -81,7 +59,9 @@ func (*authenticator) AuthenticatePaymentStateRequest(sig []byte, accountId stri return fmt.Errorf("failed to recover public key from signature: %v", err) } - if !bytes.Equal(pubKey.X.Bytes(), sigPublicKeyECDSA.X.Bytes()) || !bytes.Equal(pubKey.Y.Bytes(), sigPublicKeyECDSA.Y.Bytes()) { + pubKey := crypto.PubkeyToAddress(*sigPublicKeyECDSA).Hex() + + if pubKey != accountId { return errors.New("signature doesn't match with provided public key") }