Skip to content
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

Type accountID as Address instead of string #1335

Merged
merged 2 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions api/clients/v2/accountant.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import (
disperser_rpc "github.com/Layr-Labs/eigenda/api/grpc/disperser/v2"
"github.com/Layr-Labs/eigenda/core"
"github.com/Layr-Labs/eigenda/core/meterer"
gethcommon "github.com/ethereum/go-ethereum/common"
)

var requiredQuorums = []uint8{0, 1}

type Accountant struct {
// on-chain states
accountID string
accountID gethcommon.Address
reservation *core.ReservedPayment
onDemand *core.OnDemandPayment
reservationWindow uint64
Expand All @@ -38,7 +39,7 @@ type PeriodRecord struct {
Usage uint64
}

func NewAccountant(accountID string, reservation *core.ReservedPayment, onDemand *core.OnDemandPayment, reservationWindow uint64, pricePerSymbol uint64, minNumSymbols uint64, numBins uint32) *Accountant {
func NewAccountant(accountID gethcommon.Address, reservation *core.ReservedPayment, onDemand *core.OnDemandPayment, reservationWindow uint64, pricePerSymbol uint64, minNumSymbols uint64, numBins uint32) *Accountant {
periodRecords := make([]PeriodRecord, numBins)
for i := range periodRecords {
periodRecords[i] = PeriodRecord{Index: uint32(i), Usage: 0}
Expand Down
19 changes: 10 additions & 9 deletions api/clients/v2/accountant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/Layr-Labs/eigenda/core"
"github.com/Layr-Labs/eigenda/core/meterer"
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/assert"
)
Expand All @@ -33,7 +34,7 @@ func TestNewAccountant(t *testing.T) {

privateKey1, err := crypto.GenerateKey()
assert.NoError(t, err)
accountId := hex.EncodeToString(privateKey1.D.Bytes())
accountId := gethcommon.HexToAddress(hex.EncodeToString(privateKey1.D.Bytes()))
accountant := NewAccountant(accountId, reservation, onDemand, reservationWindow, pricePerSymbol, minNumSymbols, numBins)

assert.NotNil(t, accountant)
Expand Down Expand Up @@ -63,7 +64,7 @@ func TestAccountBlob_Reservation(t *testing.T) {

privateKey1, err := crypto.GenerateKey()
assert.NoError(t, err)
accountId := hex.EncodeToString(privateKey1.D.Bytes())
accountId := gethcommon.HexToAddress(hex.EncodeToString(privateKey1.D.Bytes()))
accountant := NewAccountant(accountId, reservation, onDemand, reservationWindow, pricePerSymbol, minNumSymbols, numBins)

ctx := context.Background()
Expand Down Expand Up @@ -114,7 +115,7 @@ func TestAccountBlob_OnDemand(t *testing.T) {

privateKey1, err := crypto.GenerateKey()
assert.NoError(t, err)
accountId := hex.EncodeToString(privateKey1.D.Bytes())
accountId := gethcommon.HexToAddress(hex.EncodeToString(privateKey1.D.Bytes()))
accountant := NewAccountant(accountId, reservation, onDemand, reservationWindow, pricePerSymbol, minNumSymbols, numBins)

ctx := context.Background()
Expand Down Expand Up @@ -142,7 +143,7 @@ func TestAccountBlob_InsufficientOnDemand(t *testing.T) {

privateKey1, err := crypto.GenerateKey()
assert.NoError(t, err)
accountId := hex.EncodeToString(privateKey1.D.Bytes())
accountId := gethcommon.HexToAddress(hex.EncodeToString(privateKey1.D.Bytes()))
accountant := NewAccountant(accountId, reservation, onDemand, reservationWindow, pricePerSymbol, minNumSymbols, numBins)

ctx := context.Background()
Expand Down Expand Up @@ -170,7 +171,7 @@ func TestAccountBlobCallSeries(t *testing.T) {

privateKey1, err := crypto.GenerateKey()
assert.NoError(t, err)
accountId := hex.EncodeToString(privateKey1.D.Bytes())
accountId := gethcommon.HexToAddress(hex.EncodeToString(privateKey1.D.Bytes()))
accountant := NewAccountant(accountId, reservation, onDemand, reservationWindow, pricePerSymbol, minNumSymbols, numBins)

ctx := context.Background()
Expand Down Expand Up @@ -223,7 +224,7 @@ func TestAccountBlob_BinRotation(t *testing.T) {

privateKey1, err := crypto.GenerateKey()
assert.NoError(t, err)
accountId := hex.EncodeToString(privateKey1.D.Bytes())
accountId := gethcommon.HexToAddress(hex.EncodeToString(privateKey1.D.Bytes()))
accountant := NewAccountant(accountId, reservation, onDemand, reservationWindow, pricePerSymbol, minNumSymbols, numBins)

ctx := context.Background()
Expand Down Expand Up @@ -264,7 +265,7 @@ func TestConcurrentBinRotationAndAccountBlob(t *testing.T) {

privateKey1, err := crypto.GenerateKey()
assert.NoError(t, err)
accountId := hex.EncodeToString(privateKey1.D.Bytes())
accountId := gethcommon.HexToAddress(hex.EncodeToString(privateKey1.D.Bytes()))
accountant := NewAccountant(accountId, reservation, onDemand, reservationWindow, pricePerSymbol, minNumSymbols, numBins)

ctx := context.Background()
Expand Down Expand Up @@ -307,7 +308,7 @@ func TestAccountBlob_ReservationWithOneOverflow(t *testing.T) {

privateKey1, err := crypto.GenerateKey()
assert.NoError(t, err)
accountId := hex.EncodeToString(privateKey1.D.Bytes())
accountId := gethcommon.HexToAddress(hex.EncodeToString(privateKey1.D.Bytes()))
accountant := NewAccountant(accountId, reservation, onDemand, reservationWindow, pricePerSymbol, minNumSymbols, numBins)

ctx := context.Background()
Expand Down Expand Up @@ -354,7 +355,7 @@ func TestAccountBlob_ReservationOverflowReset(t *testing.T) {

privateKey1, err := crypto.GenerateKey()
assert.NoError(t, err)
accountId := hex.EncodeToString(privateKey1.D.Bytes())
accountId := gethcommon.HexToAddress(hex.EncodeToString(privateKey1.D.Bytes()))
accountant := NewAccountant(accountId, reservation, onDemand, reservationWindow, pricePerSymbol, minNumSymbols, numBins)

ctx := context.Background()
Expand Down
2 changes: 1 addition & 1 deletion api/clients/v2/disperser_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ func (c *disperserClient) GetPaymentState(ctx context.Context) (*disperser_rpc.G
}

request := &disperser_rpc.GetPaymentStateRequest{
AccountId: accountID,
AccountId: accountID.Hex(),
Signature: signature,
}
return c.client.GetPaymentState(ctx, request)
Expand Down
3 changes: 2 additions & 1 deletion api/clients/v2/disperser_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/Layr-Labs/eigenda/core"
corev2 "github.com/Layr-Labs/eigenda/core/v2"
"github.com/Layr-Labs/eigenda/encoding"
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
)

Expand All @@ -23,7 +24,7 @@ func TestVerifyReceivedBlobKey(t *testing.T) {
quorumNumbers[0] = 8

paymentMetadata := core.PaymentMetadata{
AccountID: "asdf",
AccountID: gethcommon.Address{1},
Timestamp: 5,
CumulativePayment: big.NewInt(6),
}
Expand Down
7 changes: 5 additions & 2 deletions api/clients/v2/test/relay_payload_retriever_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
prover2 "github.com/Layr-Labs/eigenda/encoding/kzg/prover"
"github.com/Layr-Labs/eigenda/encoding/utils/codec"
"github.com/consensys/gnark-crypto/ecc/bn254"
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -114,8 +115,10 @@ func buildBlobAndCert(
blobHeader := &commonv2.BlobHeader{
Version: 1,
QuorumNumbers: make([]uint32, 0),
PaymentHeader: &commonv2.PaymentHeader{},
Commitment: commitmentsProto,
PaymentHeader: &commonv2.PaymentHeader{
AccountId: gethcommon.Address{1}.Hex(),
},
Commitment: commitmentsProto,
}

blobCertificate := &commonv2.BlobCertificate{
Expand Down
6 changes: 5 additions & 1 deletion api/clients/v2/verification/conversion_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,11 @@ func blobHeaderProtoToBinding(inputHeader *commonv2.BlobHeader) (*contractEigenD
return nil, fmt.Errorf("convert blob commitment: %s", err)
}

paymentHeaderHash, err := core.ConvertToPaymentMetadata(inputHeader.GetPaymentHeader()).Hash()
paymentHeader, err := core.ConvertToPaymentMetadata(inputHeader.GetPaymentHeader())
if err != nil {
return nil, fmt.Errorf("convert payment header: %s", err)
}
paymentHeaderHash, err := paymentHeader.Hash()
if err != nil {
return nil, fmt.Errorf("hash payment header: %s", err)
}
Expand Down
9 changes: 5 additions & 4 deletions core/auth/v2/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
corev2 "github.com/Layr-Labs/eigenda/core/v2"
"github.com/Layr-Labs/eigenda/encoding"
"github.com/consensys/gnark-crypto/ecc/bn254/fp"
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -69,7 +70,7 @@ func TestNoopSignerFail(t *testing.T) {
assert.EqualError(t, err, "noop signer cannot sign blob request")
}

func testHeader(t *testing.T, accountID string) *corev2.BlobHeader {
func testHeader(t *testing.T, accountID gethcommon.Address) *corev2.BlobHeader {
var commitX, commitY fp.Element
_, err := commitX.SetString("21661178944771197726808973281966770251114553549453983978976194544185382599016")
assert.NoError(t, err)
Expand Down Expand Up @@ -133,15 +134,15 @@ func TestAuthenticatePaymentStateRequestValid(t *testing.T) {
func TestAuthenticatePaymentStateRequestInvalidSignatureLength(t *testing.T) {
authenticator := auth.NewAuthenticator()

err := authenticator.AuthenticatePaymentStateRequest([]byte{1, 2, 3}, "0x123")
err := authenticator.AuthenticatePaymentStateRequest([]byte{1, 2, 3}, gethcommon.HexToAddress("0x123"))
assert.Error(t, err)
assert.Contains(t, err.Error(), "signature length is unexpected")
}

func TestAuthenticatePaymentStateRequestInvalidPublicKey(t *testing.T) {
authenticator := auth.NewAuthenticator()

err := authenticator.AuthenticatePaymentStateRequest(make([]byte, 65), "not-hex-encoded")
err := authenticator.AuthenticatePaymentStateRequest(make([]byte, 65), gethcommon.Address{})
assert.Error(t, err)
assert.Contains(t, err.Error(), "failed to recover public key from signature")
}
Expand Down Expand Up @@ -175,7 +176,7 @@ func TestAuthenticatePaymentStateRequestCorruptedSignature(t *testing.T) {
accountId, err := signer.GetAccountID()
assert.NoError(t, err)

hash := sha256.Sum256([]byte(accountId))
hash := sha256.Sum256(accountId.Bytes())
signature, err := crypto.Sign(hash[:], signer.PrivateKey)
assert.NoError(t, err)

Expand Down
11 changes: 6 additions & 5 deletions core/auth/v2/authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ func (*authenticator) AuthenticateBlobRequest(header *core.BlobHeader, signature
return fmt.Errorf("failed to recover public key from signature: %v", err)
}

accountId := header.PaymentMetadata.AccountID
accountAddr := common.HexToAddress(accountId)
accountAddr := header.PaymentMetadata.AccountID
pubKeyAddr := crypto.PubkeyToAddress(*sigPublicKeyECDSA)

if accountAddr.Cmp(pubKeyAddr) != 0 {
Expand All @@ -46,20 +45,22 @@ func (*authenticator) AuthenticateBlobRequest(header *core.BlobHeader, signature
return nil
}

func (*authenticator) AuthenticatePaymentStateRequest(sig []byte, accountId string) error {
// AuthenticatePaymentStateRequest verifies the signature of the payment state request
// The signature is signed over the byte representation of the account ID
// See implementation of BlobRequestSigner.SignPaymentStateRequest for more details
func (*authenticator) AuthenticatePaymentStateRequest(sig []byte, accountAddr common.Address) error {
// Ensure the signature is 65 bytes (Recovery ID is the last byte)
if len(sig) != 65 {
return fmt.Errorf("signature length is unexpected: %d", len(sig))
}

// Verify the signature
hash := sha256.Sum256([]byte(accountId))
hash := sha256.Sum256(accountAddr.Bytes())
sigPublicKeyECDSA, err := crypto.SigToPub(hash[:], sig)
if err != nil {
return fmt.Errorf("failed to recover public key from signature: %v", err)
}

accountAddr := common.HexToAddress(accountId)
pubKeyAddr := crypto.PubkeyToAddress(*sigPublicKeyECDSA)

if accountAddr.Cmp(pubKeyAddr) != 0 {
Expand Down
12 changes: 6 additions & 6 deletions core/auth/v2/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

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

Expand Down Expand Up @@ -49,7 +50,7 @@ func (s *LocalBlobRequestSigner) SignPaymentStateRequest() ([]byte, error) {
return nil, fmt.Errorf("failed to get account ID: %v", err)
}

hash := sha256.Sum256([]byte(accountId))
hash := sha256.Sum256(accountId.Bytes())
// Sign the account ID using the private key
sig, err := crypto.Sign(hash[:], s.PrivateKey)
if err != nil {
Expand All @@ -59,9 +60,8 @@ func (s *LocalBlobRequestSigner) SignPaymentStateRequest() ([]byte, error) {
return sig, nil
}

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

accountId := crypto.PubkeyToAddress(s.PrivateKey.PublicKey).Hex()
func (s *LocalBlobRequestSigner) GetAccountID() (gethcommon.Address, error) {
accountId := crypto.PubkeyToAddress(s.PrivateKey.PublicKey)
return accountId, nil
}

Expand All @@ -81,6 +81,6 @@ func (s *LocalNoopSigner) SignPaymentStateRequest() ([]byte, error) {
return nil, fmt.Errorf("noop signer cannot sign payment state request")
}

func (s *LocalNoopSigner) GetAccountID() (string, error) {
return "", fmt.Errorf("noop signer cannot get accountID")
func (s *LocalNoopSigner) GetAccountID() (gethcommon.Address, error) {
return gethcommon.Address{}, fmt.Errorf("noop signer cannot get accountID")
}
9 changes: 5 additions & 4 deletions core/auth/v2/signer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
core "github.com/Layr-Labs/eigenda/core/v2"
"github.com/Layr-Labs/eigenda/encoding"
"github.com/consensys/gnark-crypto/ecc/bn254/fp"
gethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -17,7 +18,7 @@ import (
func TestGetAccountID(t *testing.T) {
// Test case with known private key and expected account ID
privateKey := "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcded"
expectedAccountID := "0x1aa8226f6d354380dDE75eE6B634875c4203e522"
expectedAccountID := gethcommon.HexToAddress("0x1aa8226f6d354380dDE75eE6B634875c4203e522")

// Create signer instance
signer, err := NewLocalBlobRequestSigner(privateKey)
Expand All @@ -35,7 +36,7 @@ func TestSignBlobRequest(t *testing.T) {
require.NoError(t, err)
accountID, err := signer.GetAccountID()
require.NoError(t, err)
require.Equal(t, "0x1aa8226f6d354380dDE75eE6B634875c4203e522", accountID)
require.Equal(t, gethcommon.HexToAddress("0x1aa8226f6d354380dDE75eE6B634875c4203e522"), accountID)

var commitX, commitY fp.Element
_, err = commitX.SetString("21661178944771197726808973281966770251114553549453983978976194544185382599016")
Expand Down Expand Up @@ -96,7 +97,7 @@ func TestSignBlobRequest(t *testing.T) {

// Verify that the recovered address matches the signer's address
recoveredAddr := crypto.PubkeyToAddress(*pubKey).Hex()
assert.Equal(t, accountID, recoveredAddr)
assert.Equal(t, accountID, gethcommon.HexToAddress(recoveredAddr))
}

func TestSignPaymentStateRequest(t *testing.T) {
Expand All @@ -106,7 +107,7 @@ func TestSignPaymentStateRequest(t *testing.T) {
expectedAddr := "0x1aa8226f6d354380dDE75eE6B634875c4203e522"
accountID, err := signer.GetAccountID()
require.NoError(t, err)
hash := sha256.Sum256([]byte(accountID))
hash := sha256.Sum256(accountID.Bytes())

// Sign payment state request
signature, err := signer.SignPaymentStateRequest()
Expand Down
Loading
Loading