-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: adding example for fetching a signature and using it in rust
- Loading branch information
1 parent
74e8434
commit 2a87817
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use cerberus_api::{ | ||
client::SignerClient, | ||
SignGenericRequest, | ||
SignGenericResponse, | ||
}; | ||
use ark_bn254::{Fq,g1::G1Affine}; | ||
use ark_ff::PrimeField; | ||
use eigen_crypto_bls::BlsG1Point; | ||
#[tokio::main] | ||
async fn main() -> Result<(),Box<dyn std::error::Error>> { | ||
let mut client: SignerClient<tonic::transport::Channel> = SignerClient::connect("http://[::1]:50051").await?; | ||
let message = "Hello, world!"; | ||
let message_bytes = message.as_bytes(); | ||
let request = tonic::Request::new(SignGenericRequest { | ||
public_key: "bls_public_key".to_string(), | ||
data: message_bytes.to_vec(), | ||
password: "password".to_string(), | ||
}); | ||
let response: tonic::Response<SignGenericResponse> = client.sign_generic(request).await?; | ||
let g1_affine = response_to_g1_affine(response.into_inner())?; | ||
let bls_g1_point = BlsG1Point::new(g1_affine); | ||
println!("bls_g1_point={:?}", bls_g1_point); | ||
Ok(()) | ||
} | ||
|
||
fn response_to_g1_affine(response: SignGenericResponse) -> Result<G1Affine, Box<dyn std::error::Error>> { | ||
let signature_bytes = response.signature.clone(); | ||
let hex_string = hex::encode(&signature_bytes); | ||
let bytes = hex::decode(&hex_string).expect("Failed to decode hex"); | ||
let x_bytes = &bytes[0..32]; | ||
let y_bytes = &bytes[32..64]; | ||
let fqx = Fq::from_be_bytes_mod_order(x_bytes); | ||
let fqy = Fq::from_be_bytes_mod_order(y_bytes); | ||
Ok(G1Affine::new(fqx, fqy)) | ||
} |