-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[payments] disperser server metering (#792)
- Loading branch information
Showing
17 changed files
with
969 additions
and
318 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package auth | ||
|
||
import ( | ||
"crypto/ecdsa" | ||
"fmt" | ||
"log" | ||
|
||
commonpb "github.com/Layr-Labs/eigenda/api/grpc/common" | ||
"github.com/Layr-Labs/eigenda/core" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/crypto" | ||
) | ||
|
||
type PaymentSigner struct { | ||
PrivateKey *ecdsa.PrivateKey | ||
} | ||
|
||
var _ core.PaymentSigner = &PaymentSigner{} | ||
|
||
func NewPaymentSigner(privateKeyHex string) *PaymentSigner { | ||
|
||
privateKeyBytes := common.FromHex(privateKeyHex) | ||
privateKey, err := crypto.ToECDSA(privateKeyBytes) | ||
if err != nil { | ||
log.Fatalf("Failed to parse private key: %v", err) | ||
} | ||
|
||
return &PaymentSigner{ | ||
PrivateKey: privateKey, | ||
} | ||
} | ||
|
||
// SignBlobPayment signs the payment header and returns the signature | ||
func (s *PaymentSigner) SignBlobPayment(header *commonpb.PaymentHeader) ([]byte, error) { | ||
header.AccountId = s.GetAccountID() | ||
pm := core.ConvertPaymentHeader(header) | ||
hash, err := pm.Hash() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to hash payment header: %v", err) | ||
} | ||
|
||
sig, err := crypto.Sign(hash[:], s.PrivateKey) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to sign hash: %v", err) | ||
} | ||
|
||
return sig, nil | ||
} | ||
|
||
type NoopPaymentSigner struct{} | ||
|
||
func NewNoopPaymentSigner() *NoopPaymentSigner { | ||
return &NoopPaymentSigner{} | ||
} | ||
|
||
func (s *NoopPaymentSigner) SignBlobPayment(header *commonpb.PaymentHeader) ([]byte, error) { | ||
return nil, fmt.Errorf("noop signer cannot sign blob payment header") | ||
} | ||
|
||
func (s *NoopPaymentSigner) GetAccountID() string { | ||
return "" | ||
} | ||
|
||
// VerifyPaymentSignature verifies the signature against the payment metadata | ||
func VerifyPaymentSignature(paymentHeader *commonpb.PaymentHeader, paymentSignature []byte) bool { | ||
pm := core.ConvertPaymentHeader(paymentHeader) | ||
hash, err := pm.Hash() | ||
if err != nil { | ||
return false | ||
} | ||
|
||
recoveredPubKey, err := crypto.SigToPub(hash[:], paymentSignature) | ||
if err != nil { | ||
log.Printf("Failed to recover public key from signature: %v\n", err) | ||
return false | ||
} | ||
|
||
recoveredAddress := crypto.PubkeyToAddress(*recoveredPubKey) | ||
accountId := common.HexToAddress(paymentHeader.AccountId) | ||
if recoveredAddress != accountId { | ||
log.Printf("Signature address %s does not match account id %s\n", recoveredAddress.Hex(), accountId.Hex()) | ||
return false | ||
} | ||
|
||
return crypto.VerifySignature( | ||
crypto.FromECDSAPub(recoveredPubKey), | ||
hash[:], | ||
paymentSignature[:len(paymentSignature)-1], // Remove recovery ID | ||
) | ||
} | ||
|
||
// GetAccountID returns the Ethereum address of the signer | ||
func (s *PaymentSigner) GetAccountID() string { | ||
publicKey := crypto.FromECDSAPub(&s.PrivateKey.PublicKey) | ||
hash := crypto.Keccak256(publicKey[1:]) | ||
|
||
return common.BytesToAddress(hash[12:]).Hex() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package auth_test | ||
|
||
import ( | ||
"encoding/hex" | ||
"testing" | ||
|
||
commonpb "github.com/Layr-Labs/eigenda/api/grpc/common" | ||
"github.com/Layr-Labs/eigenda/core/auth" | ||
"github.com/ethereum/go-ethereum/crypto" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestPaymentSigner(t *testing.T) { | ||
privateKey, err := crypto.GenerateKey() | ||
require.NoError(t, err) | ||
|
||
privateKeyHex := hex.EncodeToString(crypto.FromECDSA(privateKey)) | ||
signer := auth.NewPaymentSigner(privateKeyHex) | ||
|
||
t.Run("SignBlobPayment", func(t *testing.T) { | ||
header := &commonpb.PaymentHeader{ | ||
BinIndex: 1, | ||
CumulativePayment: []byte{0x01, 0x02, 0x03}, | ||
AccountId: "", | ||
} | ||
|
||
signature, err := signer.SignBlobPayment(header) | ||
require.NoError(t, err) | ||
assert.NotEmpty(t, signature) | ||
|
||
// Verify the signature | ||
isValid := auth.VerifyPaymentSignature(header, signature) | ||
assert.True(t, isValid) | ||
}) | ||
|
||
t.Run("VerifyPaymentSignature_InvalidSignature", func(t *testing.T) { | ||
header := &commonpb.PaymentHeader{ | ||
BinIndex: 1, | ||
CumulativePayment: []byte{0x01, 0x02, 0x03}, | ||
AccountId: "", | ||
} | ||
|
||
// Create an invalid signature | ||
invalidSignature := make([]byte, 65) | ||
isValid := auth.VerifyPaymentSignature(header, invalidSignature) | ||
assert.False(t, isValid) | ||
}) | ||
|
||
t.Run("VerifyPaymentSignature_ModifiedHeader", func(t *testing.T) { | ||
header := &commonpb.PaymentHeader{ | ||
BinIndex: 1, | ||
CumulativePayment: []byte{0x01, 0x02, 0x03}, | ||
AccountId: "", | ||
} | ||
|
||
signature, err := signer.SignBlobPayment(header) | ||
require.NoError(t, err) | ||
|
||
// Modify the header after signing | ||
header.BinIndex = 2 | ||
|
||
isValid := auth.VerifyPaymentSignature(header, signature) | ||
assert.False(t, isValid) | ||
}) | ||
} | ||
|
||
func TestNoopPaymentSigner(t *testing.T) { | ||
signer := auth.NewNoopPaymentSigner() | ||
|
||
t.Run("SignBlobRequest", func(t *testing.T) { | ||
_, err := signer.SignBlobPayment(nil) | ||
assert.Error(t, err) | ||
assert.Contains(t, err.Error(), "noop signer cannot sign blob payment header") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.