Skip to content

Commit

Permalink
Merge pull request #1 from MaximFischuk/feature/keys-api-support
Browse files Browse the repository at this point in the history
Feature/keys api support
  • Loading branch information
MaximFischuk authored Jan 30, 2023
2 parents 69a711d + e77cd38 commit 9ab2ae5
Show file tree
Hide file tree
Showing 15 changed files with 1,571 additions and 64 deletions.
7 changes: 6 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.1.0"
version = "0.2.1"
edition = "2021"
repository = "https://github.com/MaximFischuk/quorum-vault-client"
documentation = "https://docs.rs/quorum-vault-client"
Expand All @@ -20,3 +20,8 @@ serde_json = "1.0.91"
rustify = "0.5.3"
rustify_derive = "0.5.2"
eth_checksum = "0.1.2"
base64 = "0.21.0"

[dev-dependencies]
tokio = { version = "1.20.1", features = ["full"] }
wiremock = "0.5.17"
156 changes: 153 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,20 @@ The following backends are supported:
* List Ethereum Accounts
* Read Ethereum Account by Address
* Sign Ethereum Transaction (only Legacy)
* Import Private Key
* Keys
* Create Key
* List Keys
* Read Key
* Delete Key
* Sign Data
* Import Private Key

## Installation
Add the following to your `Cargo.toml`:
```toml
[dependencies]
quorum-vault-client = "0.1.0"
quorum-vault-client = "0.2.1"
```

## Usage
Expand Down Expand Up @@ -167,7 +175,7 @@ async fn main() {

tx.gas_price = Some(U256::from(1));

let sign_transaction = quorum_vault_client::api::sign_transaction(&client, "quorum", 1, &tx).await.unwrap();
let sign_transaction = quorum_vault_client::api::sign_transaction(&client, "quorum", 1, tx).await.unwrap();
println!("result: {:?}", sign_transaction);
}

Expand All @@ -177,4 +185,146 @@ Result of the execution is the following:
```bash
> signature: EthereumSignTransactionResponse { signature: "0xf29001752503d05ae83874193a8d866d49fc897c1a2fcb6229a0c61e4b5663f7097817a26f4c6014bbfd24c484bad9587c9c627c6f70d020f8638a4067bb78e801" }
```
```
### Keys
**Create Key**
The following example creates a new key in the Vault.
```rust
use quorum_vault_client::{Client, VaultClient, VaultClientSettingsBuilder};
use quorum_vault_client::api::KeyCryptoAlgorithm;

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

let created_key = quorum_vault_client::api::create_key(&client, "quorum", "some-id", KeyCryptoAlgorithm::Secp256k1, [("tag".to_string(), "value".to_string())].into_iter().collect()).await.unwrap();

println!("result: {:?}", created_key);
}
```
Result of the execution is the following:
```bash
> result: KeyResponse { created_at: "2023-01-30T09:08:22.217224856Z", curve: "secp256k1", id: "some-id", namespace: "", public_key: "BIwm5UiSGTiXVRlB_rS7qYSzQ6XZbaWfUOJKVicU85q-N7zuAak2JQfAHUs2Sm2WAA7YyWdN7_4UFJFggEa6AKw=", signing_algorithm: "ecdsa", tags: {"tag": "value0"}, updated_at: "2023-01-30T09:08:22.217224856Z", version: 1 }
```
**Read Key**
The following example reads the key by id.
```rust
use quorum_vault_client::{Client, VaultClient, VaultClientSettingsBuilder};

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

let key = quorum_vault_client::api::read_key(&client, "quorum", "some-id").await.unwrap();
println!("result: {:?}", key);
}
```
Result of the execution is the following:
```bash
> result: KeyResponse { created_at: "2023-01-30T09:08:22.217224856Z", curve: "secp256k1", id: "some-id", namespace: "", public_key: "BIwm5UiSGTiXVRlB_rS7qYSzQ6XZbaWfUOJKVicU85q-N7zuAak2JQfAHUs2Sm2WAA7YyWdN7_4UFJFggEa6AKw=", signing_algorithm: "ecdsa", tags: {"tag": "value0"}, updated_at: "2023-01-30T09:08:22.217224856Z", version: 1 }
```
**List Keys**
The following example lists all keys in the Vault.
```rust
use quorum_vault_client::{Client, VaultClient, VaultClientSettingsBuilder};

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

let keys = quorum_vault_client::api::list_keys(&client, "quorum").await.unwrap();
println!("result: {:?}", keys);
}
```
Result of the execution is the following:
```bash
> result: KeysResponse { keys: ["some-id"] }
```
**Delete Key**
The following example deletes the key by id.
```rust
use quorum_vault_client::{Client, VaultClient, VaultClientSettingsBuilder};

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

quorum_vault_client::api::destroy_key(&client, "quorum", "some-id").await.unwrap();
}
```
**Sign data**
The following example signs the data by key id.
```rust
use quorum_vault_client::{Client, VaultClient, VaultClientSettingsBuilder};

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

let signature = quorum_vault_client::api::sign(&client, "quorum", "some-id", "some-data".as_bytes().await.unwrap();
println!("signature: {:?}", signature);
}
```
Result of the execution is the following:
```bash
> signature: SignResponse { signature: "Z1ibkBIGjMLh5pSR5mFZ5NbesrM57g-FGkFr0sbIyIlI_M0BYVN_LD-Nt7x1wUo6AoLQyL0I-z7PD8MsdgmkhQ==" }
```
45 changes: 18 additions & 27 deletions examples/simple/src/main.rs → examples/ethereum_wallet.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
use vaultrs::client::{VaultClient, VaultClientSettingsBuilder};
use web3::types::{Address, TransactionRequest, U256};
use std::str::FromStr;
use std::io::Write;
use web3::types::{TransactionRequest, U256};

#[tokio::main]
async fn main() {
init_logger();
let client = VaultClient::new(
VaultClientSettingsBuilder::default()
.address("http://127.0.0.1:8200")
.token("s.abx7eCduabnQNgMJZs5TECdi")
.token("s.NgpQWnkfYEAxVPC83Bxfa7cy")
.build()
.unwrap()
).unwrap();
.unwrap(),
)
.unwrap();

let created_account = quorum_vault_client::api::create_account(&client, "quorum").await.unwrap();
let created_account = quorum_vault_client::api::create_account(&client, "quorum")
.await
.unwrap();
println!("result: {:?}", created_account);

let addresses = quorum_vault_client::api::list_accouns(&client, "quorum").await.unwrap();
let addresses = quorum_vault_client::api::list_accounts(&client, "quorum")
.await
.unwrap();
println!("addresses: {:?}", addresses);

let address = Address::from_str("0x8d3113e29CB92F44F1762E52D2a0276509b36b82").unwrap();
let address = addresses.keys.first().copied().unwrap();

let result = quorum_vault_client::api::read_account(&client, "quorum", address).await.unwrap();
let result = quorum_vault_client::api::read_account(&client, "quorum", address)
.await
.unwrap();
println!("result: {:?}", result);

let mut tx: TransactionRequest = TransactionRequest::builder()
Expand All @@ -35,21 +39,8 @@ async fn main() {

tx.gas_price = Some(U256::from(1));

let signature = quorum_vault_client::api::sign_transaction(&client, "quorum", 1, tx).await.unwrap();
let signature = quorum_vault_client::api::sign_transaction(&client, "quorum", 1, tx)
.await
.unwrap();
println!("signature: {:?}", signature);
}

fn init_logger() {
let mut builder = env_logger::Builder::from_default_env();
builder.format(|buf, record| {
writeln!(
buf,
"{} [{}] - {}",
chrono::Local::now().format("%Y-%m-%d %H:%M:%S"),
record.level(),
record.args()
)
});
builder.filter_level(log::LevelFilter::Info);
builder.init();
}
63 changes: 63 additions & 0 deletions examples/keys.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use quorum_vault_client::api::KeyCryptoAlgorithm;
use vaultrs::client::{VaultClient, VaultClientSettingsBuilder};

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

let key_id = "some-id";

let new_key = quorum_vault_client::api::create_key(
&client,
"quorum",
key_id,
KeyCryptoAlgorithm::Secp256k1,
[("tag".to_string(), "value0".to_string())]
.into_iter()
.collect(),
)
.await
.unwrap();
println!("key: {new_key:?}");

let keys = quorum_vault_client::api::list_keys(&client, "quorum")
.await
.unwrap();
println!("keys: {keys:?}");

let read_key = quorum_vault_client::api::read_key(&client, "quorum", key_id)
.await
.unwrap();
println!("read_key: {read_key:?}");

let signature =
quorum_vault_client::api::sign(&client, "quorum", key_id, "some-data".as_bytes())
.await
.unwrap();
println!("signature: {signature:?}");

let updated_tags = quorum_vault_client::api::update_key_tags(
&client,
"quorum",
key_id,
[("tag".to_string(), "value1".to_string())]
.into_iter()
.collect(),
)
.await
.unwrap();
println!("updated_tags: {updated_tags:?}");

quorum_vault_client::api::destroy_key(&client, "quorum", "some-id")
.await
.unwrap();
println!("destroyed key: {key_id}");
}
15 changes: 0 additions & 15 deletions examples/simple/Cargo.toml

This file was deleted.

Loading

0 comments on commit 9ab2ae5

Please sign in to comment.