Skip to content

Commit

Permalink
golang usage of prediction request
Browse files Browse the repository at this point in the history
  • Loading branch information
abergasov committed Jan 26, 2024
1 parent 7a0b3e0 commit ddecc14
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 3 deletions.
17 changes: 14 additions & 3 deletions README.md

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions cmd/vem_prediction/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"flag"
"vem_message_generator/pkg/service"
"vem_message_generator/pkg/utils"
)

var (
ecdhPrivateKey = flag.String("ecdhPrivateKey", "", "ecdh private key")
encryptedMessage = flag.String("encryptedMessage", "", "encrypted message")
validatorsList = flag.String("validators", "", "comma-separated list of validators")
predictedIndex = flag.String("predictedIndex", "", "comma-separated list of indexes")
pk = flag.String("pk", "", "private key")
)

func main() {
flag.Parse()
srv := service.NewGenerator()
if utils.ValidateString(pk) {
srv.PrepareVEMPredictionRequest(*pk, *validatorsList, *predictedIndex)
} else {
srv.DecryptData(ecdhPrivateKey, encryptedMessage)
}
}
File renamed without changes.
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ require (
github.com/attestantio/go-eth2-client v0.19.5
github.com/ethereum/go-ethereum v1.13.5
github.com/google/uuid v1.4.0
github.com/stretchr/testify v1.8.4
)

require (
github.com/btcsuite/btcd/btcec/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/ferranbt/fastssz v0.1.3 // indirect
Expand All @@ -21,9 +23,11 @@ require (
github.com/minio/sha256-simd v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prysmaticlabs/go-bitfield v0.0.0-20210809151128-385d8c5e3fb7 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
44 changes: 44 additions & 0 deletions pkg/entities/vem_request_predict.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package entities

import (
"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/google/uuid"
)

type VEMPredictRequest struct {
// ValidatorsPubKeys of validators for whom we want to generate predicted VEMs
ValidatorsPubKeys []string `json:"validators_pub_keys"`
// PredictedIndexes are indexes of validators for whom we want to generate predicted VEMs
PredictedIndexes []uint64 `json:"predicted_indexes"`
// ClientECDHPublicKeyBase64 is base64 encoded ECDH public key of client. we will generate shared secret and encrypt VEMs with it
ClientECDHPublicKeyBase64 string `json:"ecdh_client_pubkey"`
}

type VEMPredictRequestContainer struct {
VEMRequestID uuid.UUID `json:"vem_request_uid"`

VEMPredictRequest string `json:"vem_predict_request,omitempty"` // expect json of VEMPredictRequest
VEMPredictRequestSignature string `json:"vem_predict_request_signature,omitempty"`
VEMPredictRequestSignedBy string `json:"vem_predict_request_signed_by,omitempty"`

// Rabbit routing keys where answer should be sent
ResultRoutingKey string `json:"resultRoutingKey,omitempty"`
ErrRoutingKey string `json:"errorRoutingKey,omitempty"`
}

func (v *VEMPredictRequestContainer) ErrorRoutingKey() string {
return ""
}

type VEMPredictResult struct {
PredictedVems map[string]map[uint64]*phase0.SignedVoluntaryExit `json:"requested_vems"`
}

type VEMPredictResultContainer struct {
Result RequestVEMStatus `json:"result"`
VEMRequestID uuid.UUID `json:"vem_request_uid"`

VEMResult string `json:"vem_result"` // expect json of VEMPredictResult
Error string `json:"error,omitempty"`
ForkVersion string `json:"fork_version"`
}
77 changes: 77 additions & 0 deletions pkg/service/vem_predict_generator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package service

import (
"crypto/ecdh"
"crypto/rand"
"encoding/hex"
"encoding/json"
"fmt"
"log"
"strconv"
"strings"
"vem_message_generator/pkg/entities"
"vem_message_generator/pkg/utils"

"github.com/ethereum/go-ethereum/crypto"
"github.com/google/uuid"
)

func (s *Generator) PrepareVEMPredictionRequest(privateKey, validatorsList, predictedIndex string) {
validatorsPubKeys := strings.Split(validatorsList, ",")
if len(validatorsPubKeys) == 0 {
log.Fatal("no validators provided")
}
predictedIndexesStr := strings.Split(predictedIndex, ",")
if len(predictedIndexesStr) == 0 {
log.Fatal("no predicted indexes provided")
}
predictedIndexes := make([]uint64, len(predictedIndexesStr))
for i := range predictedIndexesStr {
index, err := strconv.Atoi(predictedIndexesStr[i])
if err != nil {
log.Fatal("failed to parse predicted index: ", err)
}
predictedIndexes[i] = uint64(index)
}
log.Println(fmt.Sprintf("generate vem prediction for validators: %s, for indexes: %s", validatorsPubKeys, predictedIndex))

privateKeyECDSA, err := crypto.HexToECDSA(strings.ReplaceAll(privateKey, "0x", ""))
if err != nil {
log.Fatal("failed to cast key: ", err)
}

ecdhClientKey, err := ecdh.P256().GenerateKey(rand.Reader)
if err != nil {
log.Fatal("failed generate ecdh key: ", err)
}

encodedKey, err := utils.MarshalECDHPublicKey(ecdhClientKey.PublicKey())
if err != nil {
log.Fatal("failed to marshal ecdh public key: ", err)
}
generatedVemPredictionRequest, err := json.Marshal(entities.VEMPredictRequest{
ValidatorsPubKeys: validatorsPubKeys,
PredictedIndexes: predictedIndexes,
ClientECDHPublicKeyBase64: encodedKey,
})
if err != nil {
log.Fatal("failed to marshal sample data: ", err)
}
signature := sign(privateKeyECDSA, string(generatedVemPredictionRequest))
signedBy := checkSign(signature, string(generatedVemPredictionRequest))
clientAddress, err := utils.KeyToAddress(privateKeyECDSA)
if signedBy != clientAddress.String() {
log.Fatal("signatures mismatch")
}

signedContainer, err := json.Marshal(entities.VEMRequestContainer{
VemRequestID: uuid.New(),
VemRequest: string(generatedVemPredictionRequest),
VemRequestSignature: signature,
VemRequestSignedBy: clientAddress.String(),
})

log.Println("vem prediction request signature: ", string(signedContainer))
log.Println("secret key:", hex.EncodeToString(ecdhClientKey.Bytes()))
log.Println("secret public key:", encodedKey)
}

0 comments on commit ddecc14

Please sign in to comment.