Skip to content

Commit

Permalink
feat: adds api call to providers
Browse files Browse the repository at this point in the history
  • Loading branch information
ckartik committed Dec 11, 2024
1 parent cf02ed0 commit ecc9cd6
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion p2p/integrationtest/provider/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@ package main
import (
"context"
"crypto/tls"
"encoding/hex"
"errors"
"fmt"
"log/slog"
"math/big"
"time"

"github.com/cloudflare/circl/sign/bls"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
debugapi "github.com/primev/mev-commit/p2p/gen/go/debugapi/v1"
providerapiv1 "github.com/primev/mev-commit/p2p/gen/go/providerapi/v1"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"google.golang.org/grpc"
Expand All @@ -18,6 +24,7 @@ import (
type ProviderClient struct {
conn *grpc.ClientConn
client providerapiv1.ProviderClient
debugClient debugapi.DebugServiceClient
logger *slog.Logger
senderC chan *providerapiv1.BidResponse
senderClosed chan struct{}
Expand Down Expand Up @@ -74,6 +81,7 @@ func NewProviderClient(

b := &ProviderClient{
conn: conn,
debugClient: debugapi.NewDebugServiceClient(conn),
client: providerapiv1.NewProviderClient(conn),
logger: logger,
senderC: make(chan *providerapiv1.BidResponse),
Expand All @@ -98,6 +106,14 @@ func (b *ProviderClient) CheckAndStake(_ []string) error {
return err
}

topology, err := b.debugClient.GetTopology(context.Background(), &debugapi.EmptyMessage{})
if err != nil {
b.logger.Error("failed to get topology", "err", err)
return err
}

ethAddress := topology.GetTopology().Fields["ethAddress"].GetStringValue()

b.logger.Info("stake amount", "stake", stakeAmt.Amount)

stakedAmt, set := big.NewInt(0).SetString(stakeAmt.Amount, 10)
Expand All @@ -111,8 +127,33 @@ func (b *ProviderClient) CheckAndStake(_ []string) error {
return nil
}

hashedMessage := crypto.Keccak256(common.HexToAddress(ethAddress).Bytes())
ikm := make([]byte, 32)
privateKey, err := bls.KeyGen[bls.G1](ikm, nil, nil)
if err != nil {
b.logger.Error("failed to generate private key", "error", err)
return fmt.Errorf("failed to generate private key: %w", err)
}
publicKey := privateKey.PublicKey()
signature := bls.Sign(privateKey, hashedMessage)

// Verify the signature
if !bls.Verify(publicKey, hashedMessage, signature) {
b.logger.Error("failed to verify generated BLS signature")
return fmt.Errorf("failed to verify generated BLS signature")
}

pubkeyb, err := publicKey.MarshalBinary()
if err != nil {
b.logger.Error("failed to marshal public key", "error", err)
return fmt.Errorf("failed to marshal public key: %w", err)
}

// Register a provider
_, err = b.client.Stake(context.Background(), &providerapiv1.StakeRequest{
Amount: "10000000000000000000",
Amount: "10000000000000000000",
BlsPublicKeys: []string{hex.EncodeToString(pubkeyb)},
BlsSignatures: []string{hex.EncodeToString(signature)},
})
if err != nil {
b.logger.Error("failed to register stake", "err", err)
Expand Down

0 comments on commit ecc9cd6

Please sign in to comment.