Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
near: verify signatures through HostFunctionProvider (#8)
Browse files Browse the repository at this point in the history
* near: verify signatures through HostFunctionProvider

* rename signature recovery to verification

* rm verify method on signature

Use host functions directly instead
  • Loading branch information
blasrodri authored and Wizdave97 committed Jun 9, 2022
1 parent c576870 commit 5e22505
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 29 deletions.
2 changes: 1 addition & 1 deletion modules/src/clients/host_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub trait HostFunctionsProvider: Clone {
) -> Option<Vec<u8>>;

/// Recover the ED25519 pubkey that produced this signature
fn ed25519_recover(signature: &[u8; 64], value: &[u8; 32]) -> Option<Vec<u8>>;
fn ed25519_verify(signature: &[u8; 64], value: &[u8; 32], pubkey: &[u8]) -> bool;

/// This function should verify membership in a trie proof using parity's sp-trie package
/// with a BlakeTwo256 Hasher
Expand Down
23 changes: 13 additions & 10 deletions modules/src/clients/ics13_near/client_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,8 @@ impl<T: HostFunctionsProvider> ClientDef for NearClient<T> {
}
}

// TODO: refactor to use [`HostFunctions`]
/// validates a light block that's contained on the `NearHeader` based on the current
/// state of the light client.
pub fn validate_light_block<H: HostFunctionsProvider>(
header: &NearHeader,
client_state: NearClientState,
Expand All @@ -259,7 +260,7 @@ pub fn validate_light_block<H: HostFunctionsProvider>(

let new_block_view = header.get_light_client_block_view();
let current_block_view = client_state.get_head();
let (_current_block_hash, _next_block_hash, _approval_message) =
let (_current_block_hash, _next_block_hash, approval_message) =
reconstruct_light_client_block_view_fields::<H>(new_block_view)?;

// (1)
Expand Down Expand Up @@ -307,14 +308,16 @@ pub fn validate_light_block<H: HostFunctionsProvider>(

approved_stake += bp_stake;

let _validator_public_key = bp_stake_view.public_key.clone();
// if !maybe_signature
// .as_ref()
// .unwrap()
// .verify::<H>(&H::sha256_digest(&approval_message), validator_public_key.clone())
// {
// return Err(NearError::invalid_signature().into());
// }
let validator_public_key = &bp_stake_view.public_key;
let data = H::sha256_digest(&approval_message);
let signature = maybe_signature.as_ref().unwrap();
if H::ed25519_verify(
signature.get_inner(),
&data,
validator_public_key.get_inner(),
) {
return Err(NearError::invalid_signature().into());
}
}

let threshold = total_stake * 2 / 3;
Expand Down
22 changes: 6 additions & 16 deletions modules/src/clients/ics13_near/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,33 +41,23 @@ impl Signature {
Self::Ed25519(Ed25519Signature::from_raw(raw.try_into().unwrap()))
}

pub fn as_bytes(&self) -> &[u8] {
pub fn get_inner(&self) -> &[u8; Self::LEN] {
match self {
Self::Ed25519(inner) => &inner.0,
}
}

// TODO: we might want to create a trait for signature verification
// or integrate this into HostFunctions
pub fn verify<T: HostFunctionsProvider>(
&self,
data: impl AsRef<[u8; 32]>,
public_key: PublicKey,
) -> bool {
match self {
Self::Ed25519(signature) => T::ed25519_recover(signature.as_ref(), data.as_ref())
.map(|key| key == public_key.0.as_ref())
.unwrap_or(false),
}
}
}

impl PublicKey {
const _LEN: usize = 32;
const LEN: usize = 32;

pub fn from_raw(raw: &[u8]) -> Self {
Self(raw.try_into().unwrap())
}

pub fn get_inner(&self) -> &[u8; Self::LEN] {
&self.0
}
}

impl TryFrom<&[u8]> for CryptoHash {
Expand Down
4 changes: 2 additions & 2 deletions modules/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ impl HostFunctionsProvider for Crypto {
.map(|val| val.to_vec())
}

fn ed25519_recover(_signature: &[u8; 64], _value: &[u8; 32]) -> Option<Vec<u8>> {
todo!()
fn ed25519_verify(_signature: &[u8; 64], _value: &[u8; 32], public_key: &[u8; 32]) -> bool {
true
}

fn verify_membership_trie_proof(
Expand Down

0 comments on commit 5e22505

Please sign in to comment.