Skip to content

Commit

Permalink
Complete message for signature with Puffer's validator_index (#51)
Browse files Browse the repository at this point in the history
* add val index for approve_custody and custody req

* change validator_index type to be encoded in msg
  • Loading branch information
lean-apple authored Feb 15, 2024
1 parent 737a9fa commit d971200
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/client/mock/guardian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl GuardianClientTrait for MockGuardianClient {

async fn validate_custody(
&self,
request: crate::enclave::types::ValidateCustodyRequest,
_request: crate::enclave::types::ValidateCustodyRequest,
) -> anyhow::Result<crate::enclave::types::ValidateCustodyResponse> {
match self.validate_custody_responses.lock().unwrap().pop_front() {
Some(res) => Ok(res),
Expand Down
2 changes: 2 additions & 0 deletions src/client/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ async fn registration_flow_succeeds() {
mrenclave,
mrsigner,
verify_remote_attestation,
validator_index: 0
};

// Guardian validates they received custody
Expand Down Expand Up @@ -129,6 +130,7 @@ async fn test_cli_keygen_verified_by_guardians() {
mrenclave: "".to_string(),
mrsigner: "".to_string(),
verify_remote_attestation,
validator_index: 0
};

// Guardian validates they received custody
Expand Down
21 changes: 15 additions & 6 deletions src/enclave/guardian/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use ethers::signers::{LocalWallet, Signer};
use log::info;
use sha3::Digest;
pub mod handlers;
use crate::eth2::eth_types::{BLSSignature, ValidatorIndex};
use anyhow::{anyhow, bail, Result};
use blsttc::PublicKeySet;
use ethers::{
core::types::U256,
signers::{LocalWallet, Signer},
};
use libsecp256k1::SecretKey as EthSecretKey;
use ssz::Encode;

use crate::eth2::eth_types::BLSSignature;

#[derive(Debug, serde::Deserialize, serde::Serialize)]
pub struct KeygenWithBlockhashRequest {
pub blockhash: String,
Expand Down Expand Up @@ -77,7 +79,12 @@ pub async fn verify_and_sign_custody_received(
)?;

// return guardian enclave signature
let signature: String = approve_custody(&request.keygen_payload, &guardian_enclave_sk).await?;
let signature: String = approve_custody(
&request.keygen_payload,
&guardian_enclave_sk,
&request.validator_index,
)
.await?;

Ok(crate::enclave::types::ValidateCustodyResponse {
enclave_signature: signature,
Expand Down Expand Up @@ -188,11 +195,13 @@ fn verify_custody(
async fn approve_custody(
keygen_payload: &crate::enclave::types::BlsKeygenPayload,
guardian_enclave_sk: &EthSecretKey,
validator_index: &ValidatorIndex,
) -> Result<String> {
let mut hasher = sha3::Keccak256::new();

// pubKey, withdrawalCredentials, signature, depositDataRoot
// validatorIndex, pubKey, withdrawalCredentials, signature, depositDataRoot
let msg = ethers::abi::encode(&[
ethers::abi::Token::Uint(U256::from(validator_index.clone())),
ethers::abi::Token::Bytes(
keygen_payload
.public_key_set()?
Expand Down Expand Up @@ -459,7 +468,7 @@ mod tests {
let (resp, g_sks, _mre, _mrs) = setup();

for g_sk in g_sks {
assert!(approve_custody(&resp, &g_sk).await.is_ok());
assert!(approve_custody(&resp, &g_sk, &0).await.is_ok());
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/enclave/types.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::io::remote_attestation::AttestationEvidence;
use crate::{crypto::eth_keys, strip_0x_prefix};
use crate::eth2::eth_types::ValidatorIndex;
use anyhow::{bail, Result};
use blsttc::{PublicKey as BlsPublicKey, PublicKeySet};
use ecies::{PublicKey as EthPublicKey, SecretKey as EthSecretKey};
Expand Down Expand Up @@ -137,6 +138,7 @@ pub struct ValidateCustodyRequest {
pub mrenclave: String,
pub mrsigner: String,
pub verify_remote_attestation: bool,
pub validator_index: ValidatorIndex
}

#[derive(Clone, Debug, Serialize, Deserialize)]
Expand Down

0 comments on commit d971200

Please sign in to comment.