mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-21 05:44:44 +00:00
Use SignatureBytes and PublicKeyBytes for deposits (#472)
* Replace deposit signatures with SignatureBytes, a struct which lazyly parsers signatures only on demand. * check byte length when parsing SignatureBytes * add comment to struct * distinguish BadSignature and BadSignatureBytes in verify_deposit_signature * add test for valid signature * Implements TryInto<Signature> for &SignatureBytes and From<Signature> for &SignatureBytes * add and use PublicKeyBytes + fix formatting * fix compiler warning + docs for macro generated structs * adds tests to ensure correct byte lengths * small style improvement as suggested by michaelsproul
This commit is contained in:
committed by
Paul Hauner
parent
845f336a59
commit
01054ecf2f
43
eth2/utils/bls/src/public_key_bytes.rs
Normal file
43
eth2/utils/bls/src/public_key_bytes.rs
Normal file
@@ -0,0 +1,43 @@
|
||||
use ssz::{Decode, DecodeError, Encode};
|
||||
|
||||
use super::{PublicKey, BLS_PUBLIC_KEY_BYTE_SIZE};
|
||||
|
||||
bytes_struct!(
|
||||
PublicKeyBytes,
|
||||
PublicKey,
|
||||
BLS_PUBLIC_KEY_BYTE_SIZE,
|
||||
"public key",
|
||||
U48
|
||||
);
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::convert::TryInto;
|
||||
|
||||
use ssz::ssz_encode;
|
||||
|
||||
use super::super::Keypair;
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
pub fn test_valid_public_key() {
|
||||
let keypair = Keypair::random();
|
||||
|
||||
let bytes = ssz_encode(&keypair.pk);
|
||||
let public_key_bytes = PublicKeyBytes::from_bytes(&bytes).unwrap();
|
||||
let public_key: Result<PublicKey, _> = (&public_key_bytes).try_into();
|
||||
assert!(public_key.is_ok());
|
||||
assert_eq!(keypair.pk, public_key.unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn test_invalid_public_key() {
|
||||
let mut public_key_bytes = [0; BLS_PUBLIC_KEY_BYTE_SIZE];
|
||||
public_key_bytes[0] = 255; //a_flag1 == b_flag1 == c_flag1 == 1 and x1 = 0 shouldn't be allowed
|
||||
let public_key_bytes = PublicKeyBytes::from_bytes(&public_key_bytes[..]);
|
||||
assert!(public_key_bytes.is_ok());
|
||||
|
||||
let public_key: Result<PublicKey, _> = public_key_bytes.as_ref().unwrap().try_into();
|
||||
assert!(public_key.is_err());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user