Skip to content

Commit

Permalink
implement sign evidence
Browse files Browse the repository at this point in the history
  • Loading branch information
HolyGrease committed Aug 18, 2023
1 parent 18381b1 commit a7d477f
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 51 deletions.
135 changes: 87 additions & 48 deletions crates/base/src/karma_coin/karma_coin_verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ impl SendVerificationCodeResult {
/// (if the ProtoBuf definition does not change) and safe for programmatic use.
pub fn as_str_name(&self) -> &'static str {
match self {
SendVerificationCodeResult::Unspecified => "SEND_VERIFICATION_CODE_RESULT_UNSPECIFIED",
SendVerificationCodeResult::Unspecified => {
"SEND_VERIFICATION_CODE_RESULT_UNSPECIFIED"
}
SendVerificationCodeResult::Sent => "SEND_VERIFICATION_CODE_RESULT_SENT",
SendVerificationCodeResult::Failed => "SEND_VERIFICATION_CODE_RESULT_FAILED",
SendVerificationCodeResult::InvalidUserData => {
Expand All @@ -105,7 +107,9 @@ impl SendVerificationCodeResult {
"SEND_VERIFICATION_CODE_RESULT_UNSPECIFIED" => Some(Self::Unspecified),
"SEND_VERIFICATION_CODE_RESULT_SENT" => Some(Self::Sent),
"SEND_VERIFICATION_CODE_RESULT_FAILED" => Some(Self::Failed),
"SEND_VERIFICATION_CODE_RESULT_INVALID_USER_DATA" => Some(Self::InvalidUserData),
"SEND_VERIFICATION_CODE_RESULT_INVALID_USER_DATA" => {
Some(Self::InvalidUserData)
}
_ => None,
}
}
Expand Down Expand Up @@ -133,7 +137,9 @@ impl VerificationResult {
VerificationResult::Verified => "VERIFICATION_RESULT_VERIFIED",
VerificationResult::MissingData => "VERIFICATION_RESULT_MISSING_DATA",
VerificationResult::Failed => "VERIFICATION_RESULT_FAILED",
VerificationResult::InvalidSignature => "VERIFICATION_RESULT_INVALID_SIGNATURE",
VerificationResult::InvalidSignature => {
"VERIFICATION_RESULT_INVALID_SIGNATURE"
}
}
}
/// Creates an enum from field names used in the ProtoBuf definition.
Expand All @@ -151,8 +157,8 @@ impl VerificationResult {
/// Generated client implementations.
pub mod verifier_service_client {
#![allow(unused_variables, dead_code, missing_docs, clippy::let_unit_value)]
use tonic::codegen::http::Uri;
use tonic::codegen::*;
use tonic::codegen::http::Uri;
/// mobile phone numbers verifier api service
#[derive(Debug, Clone)]
pub struct VerifierServiceClient<T> {
Expand Down Expand Up @@ -197,8 +203,9 @@ pub mod verifier_service_client {
<T as tonic::client::GrpcService<tonic::body::BoxBody>>::ResponseBody,
>,
>,
<T as tonic::codegen::Service<http::Request<tonic::body::BoxBody>>>::Error:
Into<StdError> + Send + Sync,
<T as tonic::codegen::Service<
http::Request<tonic::body::BoxBody>,
>>::Error: Into<StdError> + Send + Sync,
{
VerifierServiceClient::new(InterceptedService::new(inner, interceptor))
}
Expand All @@ -221,13 +228,19 @@ pub mod verifier_service_client {
pub async fn send_verification_code(
&mut self,
request: impl tonic::IntoRequest<super::SendVerificationCodeRequest>,
) -> Result<tonic::Response<super::SendVerificationCodeResponse>, tonic::Status> {
self.inner.ready().await.map_err(|e| {
tonic::Status::new(
tonic::Code::Unknown,
format!("Service was not ready: {}", e.into()),
)
})?;
) -> Result<
tonic::Response<super::SendVerificationCodeResponse>,
tonic::Status,
> {
self.inner
.ready()
.await
.map_err(|e| {
tonic::Status::new(
tonic::Code::Unknown,
format!("Service was not ready: {}", e.into()),
)
})?;
let codec = tonic::codec::ProstCodec::default();
let path = http::uri::PathAndQuery::from_static(
"/karma_coin.verifier.VerifierService/SendVerificationCode",
Expand All @@ -239,12 +252,15 @@ pub mod verifier_service_client {
&mut self,
request: impl tonic::IntoRequest<super::VerifyNumberRequest>,
) -> Result<tonic::Response<super::VerifyNumberResponse>, tonic::Status> {
self.inner.ready().await.map_err(|e| {
tonic::Status::new(
tonic::Code::Unknown,
format!("Service was not ready: {}", e.into()),
)
})?;
self.inner
.ready()
.await
.map_err(|e| {
tonic::Status::new(
tonic::Code::Unknown,
format!("Service was not ready: {}", e.into()),
)
})?;
let codec = tonic::codec::ProstCodec::default();
let path = http::uri::PathAndQuery::from_static(
"/karma_coin.verifier.VerifierService/VerifyNumber",
Expand Down Expand Up @@ -291,7 +307,10 @@ pub mod verifier_service_server {
send_compression_encodings: Default::default(),
}
}
pub fn with_interceptor<F>(inner: T, interceptor: F) -> InterceptedService<Self, F>
pub fn with_interceptor<F>(
inner: T,
interceptor: F,
) -> InterceptedService<Self, F>
where
F: tonic::service::Interceptor,
{
Expand Down Expand Up @@ -319,7 +338,10 @@ pub mod verifier_service_server {
type Response = http::Response<tonic::body::BoxBody>;
type Error = std::convert::Infallible;
type Future = BoxFuture<Self::Response, Self::Error>;
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
fn poll_ready(
&mut self,
_cx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, req: http::Request<B>) -> Self::Future {
Expand All @@ -328,18 +350,23 @@ pub mod verifier_service_server {
"/karma_coin.verifier.VerifierService/SendVerificationCode" => {
#[allow(non_camel_case_types)]
struct SendVerificationCodeSvc<T: VerifierService>(pub Arc<T>);
impl<T: VerifierService>
tonic::server::UnaryService<super::SendVerificationCodeRequest>
for SendVerificationCodeSvc<T>
{
impl<
T: VerifierService,
> tonic::server::UnaryService<super::SendVerificationCodeRequest>
for SendVerificationCodeSvc<T> {
type Response = super::SendVerificationCodeResponse;
type Future = BoxFuture<tonic::Response<Self::Response>, tonic::Status>;
type Future = BoxFuture<
tonic::Response<Self::Response>,
tonic::Status,
>;
fn call(
&mut self,
request: tonic::Request<super::SendVerificationCodeRequest>,
) -> Self::Future {
let inner = self.0.clone();
let fut = async move { (*inner).send_verification_code(request).await };
let fut = async move {
(*inner).send_verification_code(request).await
};
Box::pin(fut)
}
}
Expand All @@ -350,10 +377,11 @@ pub mod verifier_service_server {
let inner = inner.0;
let method = SendVerificationCodeSvc(inner);
let codec = tonic::codec::ProstCodec::default();
let mut grpc = tonic::server::Grpc::new(codec).apply_compression_config(
accept_compression_encodings,
send_compression_encodings,
);
let mut grpc = tonic::server::Grpc::new(codec)
.apply_compression_config(
accept_compression_encodings,
send_compression_encodings,
);
let res = grpc.unary(method, req).await;
Ok(res)
};
Expand All @@ -362,17 +390,23 @@ pub mod verifier_service_server {
"/karma_coin.verifier.VerifierService/VerifyNumber" => {
#[allow(non_camel_case_types)]
struct VerifyNumberSvc<T: VerifierService>(pub Arc<T>);
impl<T: VerifierService> tonic::server::UnaryService<super::VerifyNumberRequest>
for VerifyNumberSvc<T>
{
impl<
T: VerifierService,
> tonic::server::UnaryService<super::VerifyNumberRequest>
for VerifyNumberSvc<T> {
type Response = super::VerifyNumberResponse;
type Future = BoxFuture<tonic::Response<Self::Response>, tonic::Status>;
type Future = BoxFuture<
tonic::Response<Self::Response>,
tonic::Status,
>;
fn call(
&mut self,
request: tonic::Request<super::VerifyNumberRequest>,
) -> Self::Future {
let inner = self.0.clone();
let fut = async move { (*inner).verify_number(request).await };
let fut = async move {
(*inner).verify_number(request).await
};
Box::pin(fut)
}
}
Expand All @@ -383,23 +417,28 @@ pub mod verifier_service_server {
let inner = inner.0;
let method = VerifyNumberSvc(inner);
let codec = tonic::codec::ProstCodec::default();
let mut grpc = tonic::server::Grpc::new(codec).apply_compression_config(
accept_compression_encodings,
send_compression_encodings,
);
let mut grpc = tonic::server::Grpc::new(codec)
.apply_compression_config(
accept_compression_encodings,
send_compression_encodings,
);
let res = grpc.unary(method, req).await;
Ok(res)
};
Box::pin(fut)
}
_ => Box::pin(async move {
Ok(http::Response::builder()
.status(200)
.header("grpc-status", "12")
.header("content-type", "application/grpc")
.body(empty_body())
.unwrap())
}),
_ => {
Box::pin(async move {
Ok(
http::Response::builder()
.status(200)
.header("grpc-status", "12")
.header("content-type", "application/grpc")
.body(empty_body())
.unwrap(),
)
})
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions crates/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,8 @@ features = ["std", "u64_backend", "serde"]
version = "1"
features = ["serde"]

[dependencies.sp-rpc]
git = "https://github.com/karma-coin/karmachain"

[dev-dependencies]
nix = "0.26.2"
14 changes: 11 additions & 3 deletions crates/server/src/services/verifier/verify_number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use serde::Deserialize;
use sp_core::{
crypto::{AccountId32, Ss58Codec},
ed25519::{Pair, Public, Signature},
Pair as PairT,
Encode, Pair as PairT,
};
use std::collections::HashMap;
use xactor::*;
Expand Down Expand Up @@ -142,10 +142,18 @@ impl Handler<Verify> for VerifierService {
}
}

// todo: generate scale-encoded signed data to be included in signup tx
let phone_number_hash =
sp_core::hashing::blake2_512(user_data.phone_number.clone().as_bytes());
let verification_evidence = sp_rpc::verifier::VerificationEvidence {
verifier_public_key: self.key_pair.unwrap().public(),
account_id: account_id,
username: user_data.user_name,
phone_number_hash: phone_number_hash,
};
let bytes = verification_evidence.encode();

let response = VerifyNumberResponse {
data: vec![],
data: self.key_pair.unwrap().sign(&bytes).0.to_vec(),
result: VerificationResult::Verified as i32,
};

Expand Down

0 comments on commit a7d477f

Please sign in to comment.