Update DepositInput to spec v0.5.0

Also modifies the API for creating a proof of possession and adds a test
This commit is contained in:
Paul Hauner 2019-03-15 15:33:14 +11:00
parent 20a439101e
commit 2295322e3c
No known key found for this signature in database
GPG Key ID: D362883A9218FCC6
3 changed files with 37 additions and 17 deletions

View File

@ -7,7 +7,7 @@ use test_random_derive::TestRandom;
/// Data generated by the deposit contract. /// Data generated by the deposit contract.
/// ///
/// Spec v0.4.0 /// Spec v0.5.0
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Encode, Decode, TreeHash, TestRandom)] #[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Encode, Decode, TreeHash, TestRandom)]
pub struct DepositData { pub struct DepositData {
pub amount: u64, pub amount: u64,

View File

@ -9,7 +9,7 @@ use test_random_derive::TestRandom;
/// The data supplied by the user to the deposit contract. /// The data supplied by the user to the deposit contract.
/// ///
/// Spec v0.4.0 /// Spec v0.5.0
#[derive( #[derive(
Debug, Debug,
PartialEq, PartialEq,
@ -31,25 +31,23 @@ pub struct DepositInput {
impl DepositInput { impl DepositInput {
/// Generate the 'proof_of_posession' signature for a given DepositInput details. /// 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( pub fn create_proof_of_possession(
keypair: &Keypair, &self,
withdrawal_credentials: &Hash256, secret_key: &SecretKey,
domain: u64, epoch: Epoch,
fork: &Fork,
spec: &ChainSpec,
) -> Signature { ) -> Signature {
let signable_deposit_input = DepositInput { let msg = self.signed_root();
pubkey: keypair.pk.clone(), let domain = spec.get_domain(epoch, Domain::Deposit, fork);
withdrawal_credentials: withdrawal_credentials.clone(),
proof_of_possession: Signature::empty_signature(),
};
let msg = signable_deposit_input.signed_root();
Signature::new(msg.as_slice(), domain, &keypair.sk) Signature::new(msg.as_slice(), domain, secret_key)
} }
/// Verify that proof-of-possession is valid. /// Verify that proof-of-possession is valid.
/// ///
/// Spec v0.4.0 /// Spec v0.5.0
pub fn validate_proof_of_possession( pub fn validate_proof_of_possession(
&self, &self,
epoch: Epoch, epoch: Epoch,
@ -68,4 +66,23 @@ mod tests {
use super::*; use super::*;
ssz_tests!(DepositInput); 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));
}
} }

View File

@ -46,15 +46,18 @@ impl TestingDepositBuilder {
); );
let epoch = state.current_epoch(spec); 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.pubkey = keypair.pk.clone();
self.deposit self.deposit
.deposit_data .deposit_data
.deposit_input .deposit_input
.withdrawal_credentials = withdrawal_credentials.clone(); .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. /// Builds the deposit, consuming the builder.