#key #vault #ethereum #api-key #account #private-key #account-address

quorum-vault-client

Quorum Vault 插件客户端的 Rust 客户端

4 个版本 (1 个稳定版本)

1.0.0 2024 年 1 月 30 日
0.3.0 2023 年 3 月 8 日
0.2.1 2023 年 1 月 30 日
0.1.0 2023 年 1 月 18 日

#2394 in 神奇豆

Download history 1/week @ 2024-03-14 11/week @ 2024-03-28 4/week @ 2024-04-04 2/week @ 2024-04-11 6/week @ 2024-04-18 1/week @ 2024-04-25 3/week @ 2024-05-02 5/week @ 2024-05-09 9/week @ 2024-05-16 3/week @ 2024-05-23

每月 100 次下载

MIT 许可证

48KB
683

Quorum Vault Client


Quorum Vault 插件 API 的 Rust 客户端。

此客户端基于 Vault Client.

以下后端支持

  • Ethereum
    • 创建 Ethereum 账户
    • 列出 Ethereum 账户
    • 通过地址读取 Ethereum 账户
    • 签署 Ethereum 交易(仅限 Legacy)
    • 导入私钥
  • 密钥
    • 创建密钥
    • 列出密钥
    • 读取密钥
    • 删除密钥
    • 签署数据
    • 导入私钥

安装

将以下内容添加到您的 Cargo.toml

[dependencies]
quorum-vault-client = "1.0.0"

用法

基本

客户端用于配置与 Vault 的连接,并且对于执行所有 API 调用是必需的。在幕后,它使用来自 Reqwest 的异步客户端与 Vault 通信。

use quorum_vault_client::{Client, VaultClient, VaultClientSettingsBuilder};

fn main() {
    let client = VaultClient::new(
        VaultClientSettingsBuilder::default()
            .address("https://127.0.0.1:8200")
            .token("TOKEN")
            .build()
            .unwrap()
    ).unwrap();
}

Ethereum

创建新的 Ethereum 钱包

以下示例在 Vault 中创建一个新的 Ethereum 钱包。

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();

    // By default the plugin mounts the Ethereum backend at the path "quorum"
    let created_account = quorum_vault_client::api::create_account(&client, "quorum").await.unwrap();
    println!("result: {:?}", created_account);
}

执行结果如下

> result: EthereumAccountResponse { address: 0x1a669bad7bda1f553087df8568b8782bcb0023ac, compressed_public_key: "0x020e44fde7435da96f8260788a89d4c37f2b3d96fd936dd978b886de6872d73062", public_key: "0x040e44fde7435da96f8260788a89d4c37f2b3d96fd936dd978b886de6872d730629c94a4803d3073b0bbe9a3d46f201eef5beec04d0e6f464e07704c159edd2c64", namespace: "" }

列出所有 Ethereum 钱包

以下示例获取 Vault 中所有 Ethereum 钱包的列表。

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 list_accounts = quorum_vault_client::api::list_accounts(&client, "quorum").await.unwrap();
    println!("result: {:?}", list_accounts);
}

执行结果如下

> result: EthereumAccountsResponse { keys: [0x1a669bad7bda1f553087df8568b8782bcb0023ac, 0x8d3113e29cb92f44f1762e52d2a0276509b36b82] }

读取 Ethereum 钱包

以下示例通过地址获取 Ethereum 钱包。

use quorum_vault_client::{Client, VaultClient, VaultClientSettingsBuilder, Address};
use std::str::FromStr;

#[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 address = Address::from_str("0x8d3113e29CB92F44F1762E52D2a0276509b36b82").unwrap();
    let read_account = quorum_vault_client::api::read_account(&client, "quorum", account).await.unwrap();
    println!("result: {:?}", read_account);
}

执行结果如下

> result: EthereumAccountResponse { address: 0x8d3113e29cb92f44f1762e52d2a0276509b36b82, compressed_public_key: "0x03b1c069a45b14697567661e6426ab0639f73762d7526765b2bd6891a73d84ebb5", public_key: "0x04b1c069a45b14697567661e6426ab0639f73762d7526765b2bd6891a73d84ebb57e6abbec4c9738a025d1a611e431ecf006227dbf6ca400f85518df70e5d101cb", namespace: "" }

签署 Ethereum 交易

以下示例签署 Ethereum 交易。

use quorum_vault_client::{Client, VaultClient, VaultClientSettingsBuilder, TransactionRequest, Address, U256};
use std::str::FromStr;

#[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 address = Address::from_str("0x8d3113e29CB92F44F1762E52D2a0276509b36b82").unwrap();
    let mut tx: TransactionRequest = TransactionRequest::builder()
        .from(address)
        .to(address)
        .value(U256::from_dec_str("1000000000000000000").unwrap())
        .gas(U256::from(21000))
        .nonce(U256::from(0))
        .build();

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

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

执行结果如下

> signature: EthereumSignTransactionResponse { signature: "0xf29001752503d05ae83874193a8d866d49fc897c1a2fcb6229a0c61e4b5663f7097817a26f4c6014bbfd24c484bad9587c9c627c6f70d020f8638a4067bb78e801" }

密钥

创建密钥

以下示例在 Vault 中创建新的密钥。

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: 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 }

读取密钥

以下示例通过 ID 读取密钥。

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: 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 }

列出密钥

以下示例列出 Vault 中的所有密钥。

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: KeysResponse { keys: ["some-id"] }

删除密钥

以下示例通过 ID 删除密钥。

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();
}

签署数据

以下示例通过密钥ID签名数据。

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);
}

执行结果如下

> signature: SignResponse { signature: "Z1ibkBIGjMLh5pSR5mFZ5NbesrM57g-FGkFr0sbIyIlI_M0BYVN_LD-Nt7x1wUo6AoLQyL0I-z7PD8MsdgmkhQ==" }

依赖项

~14–30MB
~472K SLoC