From 25ee1eccba667d51b6fe4b5a4478e788b34c938a Mon Sep 17 00:00:00 2001 From: HolyGrease Date: Thu, 17 Aug 2023 09:36:52 +0300 Subject: [PATCH 1/2] add ss58 account id decode --- .../src/karma_coin/karma_coin_verifier.rs | 135 +++++++----------- .../src/services/verifier/verify_number.rs | 16 ++- 2 files changed, 57 insertions(+), 94 deletions(-) diff --git a/crates/base/src/karma_coin/karma_coin_verifier.rs b/crates/base/src/karma_coin/karma_coin_verifier.rs index 074675a..ccb0ee4 100644 --- a/crates/base/src/karma_coin/karma_coin_verifier.rs +++ b/crates/base/src/karma_coin/karma_coin_verifier.rs @@ -91,9 +91,7 @@ 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 => { @@ -107,9 +105,7 @@ 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, } } @@ -137,9 +133,7 @@ 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. @@ -157,8 +151,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::*; use tonic::codegen::http::Uri; + use tonic::codegen::*; /// mobile phone numbers verifier api service #[derive(Debug, Clone)] pub struct VerifierServiceClient { @@ -203,9 +197,8 @@ pub mod verifier_service_client { >::ResponseBody, >, >, - , - >>::Error: Into + Send + Sync, + >>::Error: + Into + Send + Sync, { VerifierServiceClient::new(InterceptedService::new(inner, interceptor)) } @@ -228,19 +221,13 @@ pub mod verifier_service_client { pub async fn send_verification_code( &mut self, request: impl tonic::IntoRequest, - ) -> 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()), - ) - })?; + ) -> Result, 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", @@ -252,15 +239,12 @@ 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", @@ -307,10 +291,7 @@ 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, { @@ -338,10 +319,7 @@ 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 { @@ -350,23 +328,18 @@ pub mod verifier_service_server { "/karma_coin.verifier.VerifierService/SendVerificationCode" => { #[allow(non_camel_case_types)] struct SendVerificationCodeSvc(pub Arc); - impl< - T: VerifierService, - > tonic::server::UnaryService - for SendVerificationCodeSvc { + impl + tonic::server::UnaryService + for SendVerificationCodeSvc + { type Response = super::SendVerificationCodeResponse; - type Future = BoxFuture< - tonic::Response, - tonic::Status, - >; + type Future = BoxFuture, 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) } } @@ -377,11 +350,10 @@ 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) }; @@ -390,23 +362,17 @@ pub mod verifier_service_server { "/karma_coin.verifier.VerifierService/VerifyNumber" => { #[allow(non_camel_case_types)] struct VerifyNumberSvc(pub Arc); - impl< - T: VerifierService, - > tonic::server::UnaryService - for VerifyNumberSvc { + impl tonic::server::UnaryService + for VerifyNumberSvc + { type Response = super::VerifyNumberResponse; - type Future = BoxFuture< - tonic::Response, - tonic::Status, - >; + type Future = BoxFuture, 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) } } @@ -417,28 +383,23 @@ 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/src/services/verifier/verify_number.rs b/crates/server/src/services/verifier/verify_number.rs index 9189aff..6b674d4 100644 --- a/crates/server/src/services/verifier/verify_number.rs +++ b/crates/server/src/services/verifier/verify_number.rs @@ -8,11 +8,15 @@ use base::karma_coin::karma_coin_verifier::{ VerificationResult, VerifyNumberRequest, VerifyNumberRequestData, VerifyNumberResponse, }; use base::server_config_service::ServerConfigService; -use ed25519_dalek::Verifier; use http::{header, StatusCode}; use prost::Message; use reqwest::Client; use serde::Deserialize; +use sp_core::{ + crypto::{AccountId32, Ss58Codec}, + ed25519::{Pair, Public, Signature}, + Pair as PairT, +}; use std::collections::HashMap; use xactor::*; @@ -56,14 +60,12 @@ impl Handler for VerifierService { return gen_verification_result(VerificationResult::MissingData).await; } - // verify request signature - use ed25519_dalek::ed25519::signature::Signature; - let signature = &Signature::from_bytes(req.signature.as_ref()).unwrap(); - // todo: extract public key from ss58 user account id - let pub_key = &ed25519_dalek::PublicKey::from_bytes(&[0x1, 0x0]).unwrap(); + let signature = Signature::from_slice(req.signature.as_ref()).unwrap(); + let account_id = AccountId32::from_ss58check(&user_data.account_id).unwrap(); + let pub_key = Public::from_raw(*account_id.as_ref()); // verify request data signature - if pub_key.verify(req.data.as_ref(), signature).is_err() { + if Pair::verify(&signature, req.data, &pub_key) { return gen_verification_result(VerificationResult::InvalidSignature).await; }; From 18381b1cd8465a10983ef511a6494554676f4669 Mon Sep 17 00:00:00 2001 From: HolyGrease Date: Thu, 17 Aug 2023 10:21:59 +0300 Subject: [PATCH 2/2] fix if --- crates/server/src/services/verifier/verify_number.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/server/src/services/verifier/verify_number.rs b/crates/server/src/services/verifier/verify_number.rs index 6b674d4..22ecd59 100644 --- a/crates/server/src/services/verifier/verify_number.rs +++ b/crates/server/src/services/verifier/verify_number.rs @@ -65,7 +65,7 @@ impl Handler for VerifierService { let pub_key = Public::from_raw(*account_id.as_ref()); // verify request data signature - if Pair::verify(&signature, req.data, &pub_key) { + if !Pair::verify(&signature, req.data, &pub_key) { return gen_verification_result(VerificationResult::InvalidSignature).await; };