Skip to content

Commit

Permalink
remove unwraps
Browse files Browse the repository at this point in the history
Signed-off-by: Jun Kimura <[email protected]>
  • Loading branch information
bluele committed Jul 1, 2024
1 parent 98e49a2 commit 1423f5f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
17 changes: 9 additions & 8 deletions elc/src/commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ pub fn decode_eip1184_rlp_proof(proof: &[u8]) -> Result<Vec<Vec<u8>>, Error> {
if r.is_list() {
Ok(r.into_iter()
.map(|r| {
let elems: Vec<Vec<u8>> = r.as_list().unwrap();
rlp::encode_list::<Vec<u8>, Vec<u8>>(&elems).into()
let elems: Vec<Vec<u8>> = r.as_list()?;
Ok(rlp::encode_list::<Vec<u8>, Vec<u8>>(&elems).into())
})
.collect())
.collect::<Result<Vec<Vec<u8>>, Error>>()?)
} else {
Err(Error::InvalidRLPFormatNotList(proof.to_vec()))
}
Expand All @@ -40,14 +40,15 @@ pub fn keccak256(bz: &[u8]) -> [u8; 32] {
}

pub fn verify_signature(sign_hash: H256, signature: &[u8]) -> Result<Address, Error> {
assert!(signature.len() == 65);

if signature.len() != 65 {
return Err(Error::InvalidSignatureLength(signature.len()));
}
let mut s = Scalar::default();
let _ = s.set_b32(&sign_hash.to_be_bytes());

let sig = Signature::parse_overflowing_slice(&signature[..64]).unwrap();
let rid = RecoveryId::parse(signature[64]).unwrap();
let signer: PublicKey = libsecp256k1::recover(&Message(s), &sig, &rid).unwrap();
let sig = Signature::parse_overflowing_slice(&signature[..64])?;
let rid = RecoveryId::parse(signature[64])?;
let signer: PublicKey = libsecp256k1::recover(&Message(s), &sig, &rid)?;
Ok(address_from_pubkey(&signer))
}

Expand Down
8 changes: 6 additions & 2 deletions elc/src/consensus_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ impl TryFrom<RawConsensusState> for ConsensusState {
validators: value
.validators
.iter()
.map(|v| v.as_slice().try_into().unwrap())
.collect(),
.map(|v| {
v.as_slice()
.try_into()
.map_err(Error::SliceToArrayConversionError)
})
.collect::<Result<_, _>>()?,
})
}
}
Expand Down
12 changes: 11 additions & 1 deletion elc/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ pub enum Error {

/// invalid rlp format: not list: `{0:?}``
InvalidRLPFormatNotList(Vec<u8>),

/// invalid state root length: `{0}`
InvalidStateRootLength(usize),
/// invalid block number length: `{0}`
Expand All @@ -41,6 +40,9 @@ pub enum Error {
/// unexpected client type: `{0}`
UnexpectedClientType(String),

/// Invalid signature length: `{0}`
InvalidSignatureLength(usize),

/// untrusted validators and committed seals length mismatch: untrusted_validators_len={untrusted_validators_len} committed_seals_len={committed_seals_len}
UntrustedValidatorsAndCommittedSealsLengthMismatch {
untrusted_validators_len: usize,
Expand Down Expand Up @@ -84,6 +86,8 @@ pub enum Error {
Decode(prost::DecodeError),
/// rlp decode error: `{0}`
Rlp(rlp::DecoderError),
/// secp256k1 error: `{0}`
Secp256k1(libsecp256k1::Error),
/// conversion error from slice to array: `{0}`
SliceToArrayConversionError(core::array::TryFromSliceError),
}
Expand Down Expand Up @@ -113,3 +117,9 @@ impl From<rlp::DecoderError> for Error {
Self::Rlp(value)
}
}

impl From<libsecp256k1::Error> for Error {
fn from(value: libsecp256k1::Error) -> Self {
Self::Secp256k1(value)
}
}
1 change: 1 addition & 0 deletions elc/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ impl QbftExtra {
if it.len() != 5 {
return Err(Error::InvalidHeaderExtraSize(it.len()));
}
// unwrap is safe here because we have checked the length
let vanity_data: Vec<u8> = it.next().unwrap().as_val()?;
let validators: Vec<Vec<u8>> = it.next().unwrap().as_list()?;
let raw_vote = it.next().unwrap().as_raw();
Expand Down

0 comments on commit 1423f5f

Please sign in to comment.