Skip to content

[v2] eth address as account id #1053

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion contracts/script/SetUpEigenDA.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ contract SetupEigenDA is EigenDADeployer, EigenLayerUtils {
quorumNumbers: hex"0001",
quorumSplits: hex"3232"
});
address clientAddress = address(0x641691973c98dFe68b07Ee3613E270406285DFE8);
address clientAddress = address(0x1aa8226f6d354380dDE75eE6B634875c4203e522);
vm.startBroadcast(msg.sender);
paymentVault.setReservation(clientAddress, reservation);
// Deposit OnDemand
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 {
Copy link
Contributor

@ian-shim ian-shim Dec 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we're comparing strings, but this will fail if accountID isn't checksummed string.
I think it's safer to convert accountID to common.Address (common.HexToAddress()) and compare bytearrays using Address.Cmp()

Do we have a test capturing this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to use address type.

We have auth_test that check for authenticating the requests, but not as specific as whether it is checksum or not.

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
6 changes: 2 additions & 4 deletions core/auth/v2/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

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

Expand Down Expand Up @@ -63,9 +62,8 @@ func (s *LocalBlobRequestSigner) SignPaymentStateRequest() ([]byte, error) {

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

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

accountId := crypto.PubkeyToAddress(s.PrivateKey.PublicKey).Hex()
return accountId, nil
}

type LocalNoopSigner struct{}
Expand Down
22 changes: 22 additions & 0 deletions core/auth/v2/signer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package v2

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetAccountID(t *testing.T) {
// Test case with known private key and expected account ID
// privateKey := "73ae7e3a40b59caacb1cda8fa04f4e7fa5bb2b37101f9f3506290c201f57bf7b"
privateKey := "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcded"
expectedAccountID := "0x1aa8226f6d354380dDE75eE6B634875c4203e522"

// Create signer instance
signer := NewLocalBlobRequestSigner(privateKey)

// Get account ID
accountID, err := signer.GetAccountID()
assert.NoError(t, err)
assert.Equal(t, expectedAccountID, accountID)
}
Loading