From 2295322e3c3d1921b205fab3cf13ec5bde5ad85e Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Fri, 15 Mar 2019 15:33:14 +1100 Subject: [PATCH] Update DepositInput to spec v0.5.0 Also modifies the API for creating a proof of possession and adds a test --- eth2/types/src/deposit_data.rs | 2 +- eth2/types/src/deposit_input.rs | 43 +++++++++++++------ .../src/test_utils/testing_deposit_builder.rs | 9 ++-- 3 files changed, 37 insertions(+), 17 deletions(-) diff --git a/eth2/types/src/deposit_data.rs b/eth2/types/src/deposit_data.rs index 9d6c1bda7..a1e30032f 100644 --- a/eth2/types/src/deposit_data.rs +++ b/eth2/types/src/deposit_data.rs @@ -7,7 +7,7 @@ use test_random_derive::TestRandom; /// Data generated by the deposit contract. /// -/// Spec v0.4.0 +/// Spec v0.5.0 #[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Encode, Decode, TreeHash, TestRandom)] pub struct DepositData { pub amount: u64, diff --git a/eth2/types/src/deposit_input.rs b/eth2/types/src/deposit_input.rs index 9a9031901..9da53a042 100644 --- a/eth2/types/src/deposit_input.rs +++ b/eth2/types/src/deposit_input.rs @@ -9,7 +9,7 @@ use test_random_derive::TestRandom; /// The data supplied by the user to the deposit contract. /// -/// Spec v0.4.0 +/// Spec v0.5.0 #[derive( Debug, PartialEq, @@ -31,25 +31,23 @@ pub struct DepositInput { impl DepositInput { /// Generate the 'proof_of_posession' signature for a given DepositInput details. /// - /// Spec v0.4.0 + /// Spec v0.5.0 pub fn create_proof_of_possession( - keypair: &Keypair, - withdrawal_credentials: &Hash256, - domain: u64, + &self, + secret_key: &SecretKey, + epoch: Epoch, + fork: &Fork, + spec: &ChainSpec, ) -> Signature { - let signable_deposit_input = DepositInput { - pubkey: keypair.pk.clone(), - withdrawal_credentials: withdrawal_credentials.clone(), - proof_of_possession: Signature::empty_signature(), - }; - let msg = signable_deposit_input.signed_root(); + let msg = self.signed_root(); + let domain = spec.get_domain(epoch, Domain::Deposit, fork); - Signature::new(msg.as_slice(), domain, &keypair.sk) + Signature::new(msg.as_slice(), domain, secret_key) } /// Verify that proof-of-possession is valid. /// - /// Spec v0.4.0 + /// Spec v0.5.0 pub fn validate_proof_of_possession( &self, epoch: Epoch, @@ -68,4 +66,23 @@ mod tests { use super::*; ssz_tests!(DepositInput); + + #[test] + fn can_create_and_validate() { + let spec = ChainSpec::foundation(); + let fork = Fork::genesis(&spec); + let keypair = Keypair::random(); + let epoch = Epoch::new(0); + + let mut deposit_input = DepositInput { + pubkey: keypair.pk.clone(), + withdrawal_credentials: Hash256::zero(), + proof_of_possession: Signature::empty_signature(), + }; + + deposit_input.proof_of_possession = + deposit_input.create_proof_of_possession(&keypair.sk, epoch, &fork, &spec); + + assert!(deposit_input.validate_proof_of_possession(epoch, &fork, &spec)); + } } diff --git a/eth2/types/src/test_utils/testing_deposit_builder.rs b/eth2/types/src/test_utils/testing_deposit_builder.rs index 729311468..90c8d325d 100644 --- a/eth2/types/src/test_utils/testing_deposit_builder.rs +++ b/eth2/types/src/test_utils/testing_deposit_builder.rs @@ -46,15 +46,18 @@ impl TestingDepositBuilder { ); let epoch = state.current_epoch(spec); - let domain = spec.get_domain(epoch, Domain::Deposit, &state.fork); self.deposit.deposit_data.deposit_input.pubkey = keypair.pk.clone(); self.deposit .deposit_data .deposit_input .withdrawal_credentials = withdrawal_credentials.clone(); - self.deposit.deposit_data.deposit_input.proof_of_possession = - DepositInput::create_proof_of_possession(&keypair, &withdrawal_credentials, domain); + + self.deposit.deposit_data.deposit_input.proof_of_possession = self + .deposit + .deposit_data + .deposit_input + .create_proof_of_possession(&keypair.sk, epoch, &state.fork, spec); } /// Builds the deposit, consuming the builder.