Skip to content

Commit

Permalink
fix: auth uses accountID as eth address
Browse files Browse the repository at this point in the history
  • Loading branch information
hopeyen committed Dec 20, 2024
1 parent fa32683 commit 5007d10
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 58 deletions.
16 changes: 2 additions & 14 deletions core/auth.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand All @@ -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")
}

Expand Down
18 changes: 3 additions & 15 deletions core/auth/authenticator.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand All @@ -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")
}

Expand Down
2 changes: 1 addition & 1 deletion core/auth/v2/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
36 changes: 8 additions & 28 deletions core/auth/v2/authenticator.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand All @@ -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")
}

Expand All @@ -62,26 +52,16 @@ 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)
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 != accountId {
return errors.New("signature doesn't match with provided public key")
}

Expand Down

0 comments on commit 5007d10

Please sign in to comment.