Skip to content

Commit

Permalink
Merge pull request #2 from karma-coin/ss58-address-decode
Browse files Browse the repository at this point in the history
Add ss58 account id decode
  • Loading branch information
avive authored Aug 17, 2023
2 parents cda19a0 + 18381b1 commit f970598
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 94 deletions.
135 changes: 48 additions & 87 deletions crates/base/src/karma_coin/karma_coin_verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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,
}
}
Expand Down Expand Up @@ -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.
Expand All @@ -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<T> {
Expand Down Expand Up @@ -203,9 +197,8 @@ 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 @@ -228,19 +221,13 @@ 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 @@ -252,15 +239,12 @@ 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 @@ -307,10 +291,7 @@ 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 @@ -338,10 +319,7 @@ 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 @@ -350,23 +328,18 @@ 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 @@ -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)
};
Expand All @@ -390,23 +362,17 @@ 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 @@ -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())
}),
}
}
}
Expand Down
16 changes: 9 additions & 7 deletions crates/server/src/services/verifier/verify_number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;

Expand Down Expand Up @@ -56,14 +60,12 @@ impl Handler<Verify> 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;
};

Expand Down

0 comments on commit f970598

Please sign in to comment.