Skip to content

Commit

Permalink
Merge pull request #2 from MaximFischuk/feature/zksnarks-api-support
Browse files Browse the repository at this point in the history
Feature/zksnarks api support
  • Loading branch information
MaximFischuk authored Mar 8, 2023
2 parents 9ab2ae5 + 6265d59 commit a8bdfbe
Show file tree
Hide file tree
Showing 10 changed files with 505 additions and 3 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "quorum-vault-client"
version = "0.2.1"
version = "0.3.0"
edition = "2021"
repository = "https://github.com/MaximFischuk/quorum-vault-client"
documentation = "https://docs.rs/quorum-vault-client"
Expand All @@ -25,3 +25,5 @@ base64 = "0.21.0"
[dev-dependencies]
tokio = { version = "1.20.1", features = ["full"] }
wiremock = "0.5.17"
secp256k1 = "0.26.0"
hex = "0.4.3"
2 changes: 1 addition & 1 deletion examples/ethereum_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ async fn main() {
let client = VaultClient::new(
VaultClientSettingsBuilder::default()
.address("http://127.0.0.1:8200")
.token("s.NgpQWnkfYEAxVPC83Bxfa7cy")
.token("root")
.build()
.unwrap(),
)
Expand Down
2 changes: 1 addition & 1 deletion examples/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ async fn main() {
let client = VaultClient::new(
VaultClientSettingsBuilder::default()
.address("http://127.0.0.1:8200")
.token("s.NgpQWnkfYEAxVPC83Bxfa7cy")
.token("root")
.build()
.unwrap(),
)
Expand Down
44 changes: 44 additions & 0 deletions examples/zksnarks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use vaultrs::client::{VaultClient, VaultClientSettingsBuilder};

#[tokio::main]
async fn main() {
// Create a client
let client = VaultClient::new(
VaultClientSettingsBuilder::default()
.address("http://localhost:8200")
.token("root")
.build()
.unwrap(),
)
.unwrap();

// Create a new account
let account = quorum_vault_client::api::create_zksnarks_account(&client, "quorum")
.await
.unwrap();
println!("account: {:?}", account);

// Read the account
let account =
quorum_vault_client::api::read_zksnarks_account(&client, "quorum", &account.public_key)
.await
.unwrap();
println!("account: {:?}", account);

// List the accounts
let accounts = quorum_vault_client::api::list_zksnarks_accounts(&client, "quorum")
.await
.unwrap();
println!("accounts: {:?}", accounts);

// Sign a message
let signature = quorum_vault_client::api::zksnarks_sign(
&client,
"quorum",
&account.public_key,
"some-data".as_bytes(),
)
.await
.unwrap();
println!("signature: {:?}", signature);
}
100 changes: 100 additions & 0 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@ use crate::api::keys::requests::{
SignRequest, UpdateKeyTagsRequest,
};
use crate::api::keys::responses::{KeyResponse, KeysResponse, SignResponse};
use crate::api::zksnarks::requests::{
CreateZkSnarksAccountRequest, ListZkSnarksAccountsRequest, ReadZkSnarksAccountRequest,
ZkSnarksSignRequest,
};
use crate::api::zksnarks::responses::{
ZkSnarksAccountResponse, ZkSnarksAccountsResponse, ZkSnarksSignResponse,
};
use crate::error::ClientError;
use crate::H256;
use base64::Engine;
use std::collections::HashMap;
use vaultrs::client::Client;
Expand All @@ -19,6 +27,7 @@ use web3::types::{Address, TransactionRequest};

pub mod ethereum;
pub mod keys;
pub mod zksnarks;

/// Key crypto algorithm.
pub enum KeyCryptoAlgorithm {
Expand Down Expand Up @@ -286,3 +295,94 @@ impl KeyCryptoAlgorithm {
}
}
}

/// Create a zk-SNARKs account (eddsa)
/// See [CreateZkSnarksAccountRequest]
pub async fn create_zksnarks_account(
client: &impl Client,
mount: &str,
) -> Result<ZkSnarksAccountResponse, ClientError> {
let request = CreateZkSnarksAccountRequest::builder()
.mount(mount)
.build()
.unwrap();
vaultrs::api::exec_with_result(client, request)
.await
.map_err(Into::into)
}

/// Read a zk-SNARKs account
/// See [ReadZkSnarksAccountRequest]
pub async fn read_zksnarks_account(
client: &impl Client,
mount: &str,
id: &str,
) -> Result<ZkSnarksAccountResponse, ClientError> {
let request = ReadZkSnarksAccountRequest::builder()
.mount(mount)
.id(id)
.build()
.unwrap();
vaultrs::api::exec_with_result(client, request)
.await
.map_err(Into::into)
}

/// List zk-SNARKs accounts
/// See [ListZkSnarksAccountsRequest]
pub async fn list_zksnarks_accounts(
client: &impl Client,
mount: &str,
) -> Result<ZkSnarksAccountsResponse, ClientError> {
let request = ListZkSnarksAccountsRequest::builder()
.mount(mount)
.build()
.unwrap();
vaultrs::api::exec_with_result(client, request)
.await
.map_err(Into::into)
}

/// Sign a message with a zk-SNARKs account (eddsa)
/// See [ZkSnarksSignResponse]
pub async fn zksnarks_sign(
client: &impl Client,
mount: &str,
id: &str,
data: &[u8],
) -> Result<ZkSnarksSignResponse, ClientError> {
let hash = keccak256(data);
let hex = H256::from(hash);
let encoded = format!("{:?}", hex);
let request = ZkSnarksSignRequest::builder()
.mount(mount)
.id(id)
.data(encoded)
.build()
.unwrap();
vaultrs::api::exec_with_result(client, request)
.await
.map_err(Into::into)
}

/// Sign a message with a zk-SNARKs account (eddsa)
/// Data must be a 32 byte hash
/// See [ZkSnarksSignResponse]
pub async fn zksnarks_sign_hash(
client: &impl Client,
mount: &str,
id: &str,
data: [u8; 32],
) -> Result<ZkSnarksSignResponse, ClientError> {
let hex = H256::from(data);
let encoded = format!("{:?}", hex);
let request = ZkSnarksSignRequest::builder()
.mount(mount)
.id(id)
.data(encoded)
.build()
.unwrap();
vaultrs::api::exec_with_result(client, request)
.await
.map_err(Into::into)
}
2 changes: 2 additions & 0 deletions src/api/zksnarks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod requests;
pub mod responses;
83 changes: 83 additions & 0 deletions src/api/zksnarks/requests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use crate::api::zksnarks::responses::*;
use rustify_derive::Endpoint;

/// ## Create Zk-Snarks Account
/// This endpoint creates a new Zk-Snarks account.
///
/// * Path: /zk-snarks/accounts
/// * Method: POST
/// * Response: [ZkSnarksAccountResponse]
#[derive(Builder, Debug, Endpoint)]
#[endpoint(
path = "{self.mount}/zk-snarks/accounts",
method = "POST",
response = "ZkSnarksAccountResponse",
builder = "true"
)]
#[builder(setter(into))]
pub struct CreateZkSnarksAccountRequest {
#[endpoint(skip)]
pub mount: String,
}

/// ## Read Zk-Snarks Account
/// This endpoint gets a Zk-Snarks account by ID.
///
/// * Path: /zk-snarks/accounts/{self.id}
/// * Method: GET
/// * Response: [ZkSnarksAccountResponse]
#[derive(Builder, Debug, Endpoint)]
#[endpoint(
path = "{self.mount}/zk-snarks/accounts/{self.id}",
method = "GET",
response = "ZkSnarksAccountResponse",
builder = "true"
)]
#[builder(setter(into))]
pub struct ReadZkSnarksAccountRequest {
#[endpoint(skip)]
pub mount: String,
pub id: String,
}

/// ## List Zk-Snarks Accounts
/// This endpoint gets all Zk-Snarks accounts.
///
/// * Path: /zk-snarks/accounts
/// * Method: GET
/// * Response: [ZkSnarksAccountsResponse]
#[derive(Builder, Debug, Endpoint)]
#[endpoint(
path = "{self.mount}/zk-snarks/accounts",
method = "GET",
response = "ZkSnarksAccountsResponse",
builder = "true"
)]
#[builder(setter(into))]
pub struct ListZkSnarksAccountsRequest {
#[endpoint(skip)]
pub mount: String,
}

/// ## Sign data with Zk-Snarks Account
/// This endpoint signs data with a Zk-Snarks account.
///
/// * Path: /zk-snarks/accounts/{self.id}/sign
/// * Method: POST
/// * Response: [ZkSnarksSignResponse]
#[derive(Builder, Debug, Endpoint)]
#[endpoint(
path = "{self.mount}/zk-snarks/accounts/{self.id}/sign",
method = "POST",
response = "ZkSnarksSignResponse",
builder = "true"
)]
#[builder(setter(into))]
pub struct ZkSnarksSignRequest {
#[endpoint(skip)]
pub mount: String,
#[endpoint(skip)]
pub id: String,
#[endpoint(body)]
pub data: String,
}
22 changes: 22 additions & 0 deletions src/api/zksnarks/responses.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use serde::{Deserialize, Serialize};

/// Response from executing [CreateZkSnarksAccountRequest][crate::api::zksnarks::requests::CreateZkSnarksAccountRequest]
#[derive(Deserialize, Debug, Serialize)]
pub struct ZkSnarksAccountResponse {
pub curve: String,
pub namespace: String,
pub public_key: String,
pub signing_algorithm: String,
}

/// Response from executing [ListZkSnarksAccountsRequest][crate::api::zksnarks::requests::ListZkSnarksAccountsRequest]
#[derive(Deserialize, Debug, Serialize)]
pub struct ZkSnarksAccountsResponse {
pub keys: Vec<String>,
}

/// Response from executing [ZkSnarksSignRequest][crate::api::zksnarks::requests::ZkSnarksSignRequest]
#[derive(Deserialize, Debug, Serialize)]
pub struct ZkSnarksSignResponse {
pub signature: String,
}
1 change: 1 addition & 0 deletions tests/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
mod ethereum;
mod keys;
mod zksnarks;
Loading

0 comments on commit a8bdfbe

Please sign in to comment.