-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
164 additions
and
3 deletions.
There are no files selected for viewing
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,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.
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,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"` | ||
} |
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,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) | ||
} |