dfns-sdk-rust 0.2.0

Dfns API SDK for Rust
Documentation
// Code generated by rust-sdk-generator. DO NOT EDIT.

pub mod delegated;
pub mod types;

#[allow(unused_imports)]
use types::*;

/// Client for keys operations.
#[derive(Clone)]
pub struct KeysClient {
    client: crate::client::Client,
}

impl KeysClient {
    pub fn new(client: crate::client::Client) -> Self {
        KeysClient { client }
    }

    /// Retrieve all keys registered for your organization.
    pub async fn list_keys(
        &self,
        query: Option<ListKeysQuery>,
    ) -> Result<ListKeysResponse, crate::error::Error> {
        let mut path = String::from("/keys");
        if let Some(query) = &query {
            let mut q: Vec<String> = Vec::new();
            if let Some(v) = &query.limit {
                q.push(format!("limit={}", urlencoding::encode(&v.to_string())));
            }
            if let Some(v) = &query.pagination_token {
                q.push(format!(
                    "paginationToken={}",
                    urlencoding::encode(&v.to_string())
                ));
            }
            if let Some(v) = &query.owner {
                q.push(format!("owner={}", urlencoding::encode(&v.to_string())));
            }
            if !q.is_empty() {
                path.push('?');
                path.push_str(&q.join("&"));
            }
        }
        self.client
            .request::<ListKeysResponse>(reqwest::Method::GET, &path, None, false)
            .await
    }

    /// Creates a key for the given scheme and curve. Returns the new key entity.
    pub async fn create_key(
        &self,
        body: CreateKeyRequest,
    ) -> Result<CreateKeyResponse, crate::error::Error> {
        let path = String::from("/keys");
        let body = serde_json::to_value(&body)?;
        self.client
            .request::<CreateKeyResponse>(reqwest::Method::POST, &path, Some(&body), true)
            .await
    }

    /// <Warning>
    /// Only keys created with "`delayDelegation: true`" can then be delegated to an end-user. It means you need to know ahead of time that you're creating a wallet meant to be delegated to an end-user later. This is a safety to prevent, for example, a treasury wallet from being unintentionally delegated to an end-user.
    /// </Warning>
    ///
    pub async fn delegate_key(
        &self,
        key_id: String,
        body: DelegateKeyRequest,
    ) -> Result<DelegateKeyResponse, crate::error::Error> {
        let path = format!("/keys/{}/delegate", urlencoding::encode(&key_id));
        let body = serde_json::to_value(&body)?;
        self.client
            .request::<DelegateKeyResponse>(reqwest::Method::POST, &path, Some(&body), true)
            .await
    }

    /// Retrieves a key information by its ID.
    pub async fn get_key(&self, key_id: String) -> Result<GetKeyResponse, crate::error::Error> {
        let path = format!("/keys/{}", urlencoding::encode(&key_id));
        self.client
            .request::<GetKeyResponse>(reqwest::Method::GET, &path, None, false)
            .await
    }

    /// Updates the name of an existing key.
    pub async fn update_key(
        &self,
        key_id: String,
        body: UpdateKeyRequest,
    ) -> Result<UpdateKeyResponse, crate::error::Error> {
        let path = format!("/keys/{}", urlencoding::encode(&key_id));
        let body = serde_json::to_value(&body)?;
        self.client
            .request::<UpdateKeyResponse>(reqwest::Method::PUT, &path, Some(&body), true)
            .await
    }

    /// Deletes the key and all wallets using this key. Once deleted, keys (and wallets) are not usable anymore, and won't count in your overall organisation wallet count.
    pub async fn delete_key(
        &self,
        key_id: String,
    ) -> Result<DeleteKeyResponse, crate::error::Error> {
        let path = format!("/keys/{}", urlencoding::encode(&key_id));
        self.client
            .request::<DeleteKeyResponse>(reqwest::Method::DELETE, &path, None, true)
            .await
    }

    /// Dfns decentralized key management network supports threshold Diffie-Hellman protocol based on [GLOW20 paper](https://eprint.iacr.org/2020/096). You can use the DH protocol to derive output from a domain separation tag and a seed value. The derivation process is deterministic, i.e. the same Diffie-Hellman key and seed will lead to the same derived output. To ensure reproducibility, we use hash to curve [RFC9380](https://www.rfc-editor.org/rfc/rfc9380.html) and standard ciphersuite `secp256k1_XMD:SHA-256_SSWU_RO_`.
    ///
    /// <Tip>
    /// The seed doesn’t need to be secret. Without access to the DH key, it is not possible to do the derivation, even if the seed is known. Moreover, if both seed and derived output are known, it’s also not possible to do the derivation for another seed without having access to the DH key.
    pub async fn derive_key(
        &self,
        key_id: String,
        body: DeriveKeyRequest,
    ) -> Result<DeriveKeyResponse, crate::error::Error> {
        let path = format!("/keys/{}/derive", urlencoding::encode(&key_id));
        let body = serde_json::to_value(&body)?;
        self.client
            .request::<DeriveKeyResponse>(reqwest::Method::POST, &path, Some(&body), true)
            .await
    }

    /// Dfns secures private keys by generating them as MPC key shares in our decentralized key management network.  Our goal is to eliminate all single points of failure (SPOFs) associated with blockchain private keys.
    ///
    /// In certain circumstances, however, customers require Dfns to export a private key. In this case, Dfns exposes the following endpoint which can be used in conjunction with our [export SDK](https://github.com/dfns/dfns-sdk-ts/tree/m/examples/sdk/export-wallet). Each signer returns its key share encrypted to an encryption key you provide; the full private key is reconstituted client-side and is never assembled on Dfns servers.
    ///
    pub async fn export_key(
        &self,
        key_id: String,
        body: ExportKeyRequest,
    ) -> Result<ExportKeyResponse, crate::error::Error> {
        let path = format!("/keys/{}/export", urlencoding::encode(&key_id));
        let body = serde_json::to_value(&body)?;
        self.client
            .request::<ExportKeyResponse>(reqwest::Method::POST, &path, Some(&body), true)
            .await
    }

    /// List all signature requests for a key.
    pub async fn list_signatures(
        &self,
        key_id: String,
        query: Option<ListSignaturesQuery>,
    ) -> Result<ListSignaturesResponse, crate::error::Error> {
        let mut path = format!("/keys/{}/signatures", urlencoding::encode(&key_id));
        if let Some(query) = &query {
            let mut q: Vec<String> = Vec::new();
            if let Some(v) = &query.limit {
                q.push(format!("limit={}", urlencoding::encode(&v.to_string())));
            }
            if let Some(v) = &query.pagination_token {
                q.push(format!(
                    "paginationToken={}",
                    urlencoding::encode(&v.to_string())
                ));
            }
            if !q.is_empty() {
                path.push('?');
                path.push_str(&q.join("&"));
            }
        }
        self.client
            .request::<ListSignaturesResponse>(reqwest::Method::GET, &path, None, false)
            .await
    }

    /// Request to generate a signature with the key. **This process does not broadcast anything on-chain**, this is just an off-chain signature request.
    ///
    /// Dfns is compatible with any blockchain that uses a supported [key format](https://docs.dfns.co/networks/supported-key-formats). If Dfns doesn't officially integrate with a blockchain, you can use hash signing to generate the signatures to interact with the chain.
    ///
    pub async fn generate_signature(
        &self,
        key_id: String,
        body: GenerateSignatureRequest,
    ) -> Result<GenerateSignatureResponse, crate::error::Error> {
        let path = format!("/keys/{}/signatures", urlencoding::encode(&key_id));
        let body = serde_json::to_value(&body)?;
        self.client
            .request::<GenerateSignatureResponse>(reqwest::Method::POST, &path, Some(&body), true)
            .await
    }

    /// Retrieve a signature request details.
    pub async fn get_signature(
        &self,
        key_id: String,
        signature_id: String,
    ) -> Result<GetSignatureResponse, crate::error::Error> {
        let path = format!(
            "/keys/{}/signatures/{}",
            urlencoding::encode(&key_id),
            urlencoding::encode(&signature_id)
        );
        self.client
            .request::<GetSignatureResponse>(reqwest::Method::GET, &path, None, false)
            .await
    }

    /// Dfns secures private keys by generating them as MPC key shares in our decentralized key management network.  This happens by default when you create a [key](https://docs.dfns.co/api-reference/keys/create-key) or [wallet](https://docs.dfns.co/api-reference/wallets/create-wallet).
    ///
    /// In some circumstances, however, you may need to import an existing private key into Dfns infrastructure, instead of creating a brand new wallet with Dfns and transfer funds to it. As an example, you might want to keep an existing wallet if its address is tied to a smart contract which you don't want to re-deploy.
    ///
    pub async fn import_key(
        &self,
        body: ImportKeyRequest,
    ) -> Result<ImportKeyResponse, crate::error::Error> {
        let path = String::from("/keys/import");
        let body = serde_json::to_value(&body)?;
        self.client
            .request::<ImportKeyResponse>(reqwest::Method::POST, &path, Some(&body), true)
            .await
    }
}