Skip to content

Commit

Permalink
refactor: undo chanages to v1
Browse files Browse the repository at this point in the history
  • Loading branch information
hopeyen committed Dec 20, 2024
1 parent 1017692 commit b18a4e5
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 8 deletions.
16 changes: 14 additions & 2 deletions core/auth.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
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 @@ -23,14 +25,24 @@ 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)
}

pubKey := crypto.PubkeyToAddress(*sigPublicKeyECDSA).Hex()
if pubKey != accountAddr.Hex() {
if !bytes.Equal(pubKey.X.Bytes(), sigPublicKeyECDSA.X.Bytes()) || !bytes.Equal(pubKey.Y.Bytes(), sigPublicKeyECDSA.Y.Bytes()) {
return errors.New("signature doesn't match with provided public key")
}

Expand Down
18 changes: 15 additions & 3 deletions core/auth/authenticator.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
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 @@ -29,15 +31,25 @@ 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)
}

pubKey := crypto.PubkeyToAddress(*sigPublicKeyECDSA).Hex()

if pubKey != header.AccountID {
if !bytes.Equal(pubKey.X.Bytes(), sigPublicKeyECDSA.X.Bytes()) || !bytes.Equal(pubKey.Y.Bytes(), sigPublicKeyECDSA.Y.Bytes()) {
return errors.New("signature doesn't match with provided public key")
}

Expand Down
5 changes: 3 additions & 2 deletions core/auth/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/Layr-Labs/eigenda/core"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
)

Expand Down Expand Up @@ -48,8 +49,8 @@ func (s *LocalBlobRequestSigner) SignBlobRequest(header core.BlobAuthHeader) ([]

func (s *LocalBlobRequestSigner) GetAccountID() (string, error) {

accountId := crypto.PubkeyToAddress(s.PrivateKey.PublicKey).Hex()
return accountId, nil
publicKeyBytes := crypto.FromECDSAPub(&s.PrivateKey.PublicKey)
return hexutil.Encode(publicKeyBytes), nil

}

Expand Down
20 changes: 19 additions & 1 deletion disperser/apiserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
"github.com/Layr-Labs/eigenda/encoding"
"github.com/Layr-Labs/eigenda/encoding/rs"
"github.com/Layr-Labs/eigensdk-go/logging"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
grpcprom "github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus"
"github.com/prometheus/client_golang/prometheus"
"google.golang.org/grpc"
Expand Down Expand Up @@ -136,7 +138,23 @@ func (s *DispersalServer) DisperseBlobAuthenticated(stream pb.Disperser_Disperse
return api.NewErrorInvalidArg(err.Error())
}

authenticatedAddress := blob.RequestHeader.BlobAuthHeader.AccountID
// Get the ethereum address associated with the public key. This is just for convenience so we can put addresses instead of public keys in the allowlist.
// Decode public key
publicKeyBytes, err := hexutil.Decode(blob.RequestHeader.AccountID)
if err != nil {
s.metrics.HandleInvalidArgRpcRequest("DisperseBlobAuthenticated")
s.metrics.HandleInvalidArgRequest("DisperseBlobAuthenticated")
return api.NewErrorInvalidArg(fmt.Sprintf("failed to decode account ID (%v): %v", blob.RequestHeader.AccountID, err))
}

pubKey, err := crypto.UnmarshalPubkey(publicKeyBytes)
if err != nil {
s.metrics.HandleInvalidArgRpcRequest("DisperseBlobAuthenticated")
s.metrics.HandleInvalidArgRequest("DisperseBlobAuthenticated")
return api.NewErrorInvalidArg(fmt.Sprintf("failed to decode public key (%v): %v", hexutil.Encode(publicKeyBytes), err))
}

authenticatedAddress := crypto.PubkeyToAddress(*pubKey).String()

// Send back challenge to client
challengeBytes := make([]byte, 32)
Expand Down

0 comments on commit b18a4e5

Please sign in to comment.