Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/robonode-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ rand = "0.7"
reqwest = "0.11"
sc-tracing = "3"
serde = { version = "1", features = ["derive"] }
sp-application-crypto = { git = "https://github.com/humanode-network/substrate", branch = "master" }
sp-core = { git = "https://github.com/humanode-network/substrate", branch = "master" }
tokio = { version = "1", features = ["full"] }
tracing = "0.1"
uuid = { version = "0.8", features = ["v4"] }
Expand Down
7 changes: 3 additions & 4 deletions crates/robonode-server/src/http/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use mockall::predicate::*;
use mockall::*;
use primitives_auth_ticket::OpaqueAuthTicket;
use primitives_liveness_data::OpaqueLivenessData;
use sp_application_crypto::sp_core::hexdisplay::AsBytesRef;
use warp::hyper::StatusCode;

use crate::{
Expand Down Expand Up @@ -167,7 +166,7 @@ async fn it_works_authenticate() {
let expected_response = serde_json::to_string(&provide_authenticate_response()).unwrap();

assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.body().as_bytes_ref(), expected_response.as_bytes());
assert_eq!(res.body().as_ref(), expected_response.as_bytes());
}

#[tokio::test]
Expand Down Expand Up @@ -221,7 +220,7 @@ async fn it_works_get_facetec_session_token() {
let expected_response = serde_json::to_string(&provide_facetec_session_token()).unwrap();

assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.body().as_bytes_ref(), expected_response.as_bytes());
assert_eq!(res.body().as_ref(), expected_response.as_bytes());
}

#[tokio::test]
Expand Down Expand Up @@ -274,5 +273,5 @@ async fn it_works_get_facetec_device_sdk_params() {
let expected_response = serde_json::to_string(&provide_facetec_device_sdk_params()).unwrap();

assert_eq!(res.status(), StatusCode::OK);
assert_eq!(res.body().as_bytes_ref(), expected_response.as_bytes());
assert_eq!(res.body().as_ref(), expected_response.as_bytes());
}
2 changes: 1 addition & 1 deletion crates/robonode-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub fn init(
execution_id,
facetec: facetec_api_client,
signer: robonode_keypair,
public_key_type: PhantomData::<validator_key::AuraPublic>,
public_key_type: PhantomData::<validator_key::SubstratePublic<sp_core::sr25519::Public>>,
}),
facetec_device_sdk_params,
};
Expand Down
33 changes: 19 additions & 14 deletions crates/robonode-server/src/validator_key.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,49 @@
//! The validator key integration logic.

use std::convert::TryFrom;
use std::convert::{TryFrom, TryInto};

use sp_application_crypto::Public;
use sp_application_crypto::RuntimePublic;
use sp_core::crypto::{CryptoType, Pair, Public};

use crate::logic;

/// A validator public key implemented via substrate's aura consensus key.
pub struct AuraPublic(sp_application_crypto::sr25519::Public);
/// A validator public key implemented via substrate crypto.
pub struct SubstratePublic<T: CryptoType>(<T::Pair as Pair>::Public);

#[async_trait::async_trait]
impl logic::Verifier<Vec<u8>> for AuraPublic {
impl<T: CryptoType> logic::Verifier<Vec<u8>> for SubstratePublic<T>
where
<T::Pair as Pair>::Signature: for<'a> TryFrom<&'a [u8]>,
{
type Error = ();

async fn verify<'a, D>(&self, data: D, signature: Vec<u8>) -> Result<bool, Self::Error>
where
D: AsRef<[u8]> + Send + 'a,
{
let data = data.as_ref();
let signature = sp_application_crypto::sr25519::Signature::try_from(signature.as_slice())?;
Ok(self.0.verify(&data, &signature))
let signature = signature.as_slice().try_into().map_err(|_| ())?;
Ok(T::Pair::verify(&signature, &data, &self.0))
}
}

impl TryFrom<&[u8]> for AuraPublic {
impl<T: CryptoType> TryFrom<&[u8]> for SubstratePublic<T>
where
<T::Pair as Pair>::Public: for<'a> TryFrom<&'a [u8]>,
{
type Error = ();

fn try_from(val: &[u8]) -> Result<Self, Self::Error> {
Ok(Self(sp_application_crypto::sr25519::Public::try_from(val)?))
Ok(Self(val.try_into().map_err(|_| ())?))
}
}

impl From<AuraPublic> for Vec<u8> {
fn from(val: AuraPublic) -> Self {
val.as_ref().to_vec()
impl<T: CryptoType> From<SubstratePublic<T>> for Vec<u8> {
fn from(val: SubstratePublic<T>) -> Self {
val.0.to_raw_vec()
}
}

impl AsRef<[u8]> for AuraPublic {
impl<T: CryptoType> AsRef<[u8]> for SubstratePublic<T> {
fn as_ref(&self) -> &[u8] {
self.0.as_slice()
}
Expand Down