From a7d477f3d3c1995abb4fd566c42a35c04790ba07 Mon Sep 17 00:00:00 2001 From: HolyGrease Date: Fri, 18 Aug 2023 19:33:10 +0300 Subject: [PATCH] implement sign evidence --- .../src/karma_coin/karma_coin_verifier.rs | 135 +++++++++++------- crates/server/Cargo.toml | 3 + .../src/services/verifier/verify_number.rs | 14 +- 3 files changed, 101 insertions(+), 51 deletions(-) diff --git a/crates/base/src/karma_coin/karma_coin_verifier.rs b/crates/base/src/karma_coin/karma_coin_verifier.rs index ccb0ee4..074675a 100644 --- a/crates/base/src/karma_coin/karma_coin_verifier.rs +++ b/crates/base/src/karma_coin/karma_coin_verifier.rs @@ -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 => { @@ -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, } } @@ -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. @@ -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 { @@ -197,8 +203,9 @@ pub mod verifier_service_client { >::ResponseBody, >, >, - >>::Error: - Into + Send + Sync, + , + >>::Error: Into + Send + Sync, { VerifierServiceClient::new(InterceptedService::new(inner, interceptor)) } @@ -221,13 +228,19 @@ pub mod verifier_service_client { pub async fn send_verification_code( &mut self, request: impl tonic::IntoRequest, - ) -> Result, 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, + 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", @@ -239,12 +252,15 @@ pub mod verifier_service_client { &mut self, request: impl tonic::IntoRequest, ) -> Result, 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", @@ -291,7 +307,10 @@ pub mod verifier_service_server { send_compression_encodings: Default::default(), } } - pub fn with_interceptor(inner: T, interceptor: F) -> InterceptedService + pub fn with_interceptor( + inner: T, + interceptor: F, + ) -> InterceptedService where F: tonic::service::Interceptor, { @@ -319,7 +338,10 @@ pub mod verifier_service_server { type Response = http::Response; type Error = std::convert::Infallible; type Future = BoxFuture; - fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll> { + fn poll_ready( + &mut self, + _cx: &mut Context<'_>, + ) -> Poll> { Poll::Ready(Ok(())) } fn call(&mut self, req: http::Request) -> Self::Future { @@ -328,18 +350,23 @@ pub mod verifier_service_server { "/karma_coin.verifier.VerifierService/SendVerificationCode" => { #[allow(non_camel_case_types)] struct SendVerificationCodeSvc(pub Arc); - impl - tonic::server::UnaryService - for SendVerificationCodeSvc - { + impl< + T: VerifierService, + > tonic::server::UnaryService + for SendVerificationCodeSvc { type Response = super::SendVerificationCodeResponse; - type Future = BoxFuture, tonic::Status>; + type Future = BoxFuture< + tonic::Response, + tonic::Status, + >; fn call( &mut self, request: tonic::Request, ) -> 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) } } @@ -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) }; @@ -362,17 +390,23 @@ pub mod verifier_service_server { "/karma_coin.verifier.VerifierService/VerifyNumber" => { #[allow(non_camel_case_types)] struct VerifyNumberSvc(pub Arc); - impl tonic::server::UnaryService - for VerifyNumberSvc - { + impl< + T: VerifierService, + > tonic::server::UnaryService + for VerifyNumberSvc { type Response = super::VerifyNumberResponse; - type Future = BoxFuture, tonic::Status>; + type Future = BoxFuture< + tonic::Response, + tonic::Status, + >; fn call( &mut self, request: tonic::Request, ) -> 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) } } @@ -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(), + ) + }) + } } } } diff --git a/crates/server/Cargo.toml b/crates/server/Cargo.toml index 6c7f2e4..814a7e0 100644 --- a/crates/server/Cargo.toml +++ b/crates/server/Cargo.toml @@ -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" diff --git a/crates/server/src/services/verifier/verify_number.rs b/crates/server/src/services/verifier/verify_number.rs index 22ecd59..e158c24 100644 --- a/crates/server/src/services/verifier/verify_number.rs +++ b/crates/server/src/services/verifier/verify_number.rs @@ -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::*; @@ -142,10 +142,18 @@ impl Handler 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, };