Skip to main content

dfns_sdk_rust/keys/
mod.rs

1// Code generated by rust-sdk-generator. DO NOT EDIT.
2
3pub mod delegated;
4pub mod types;
5
6#[allow(unused_imports)]
7use types::*;
8
9/// Client for keys operations.
10#[derive(Clone)]
11pub struct KeysClient {
12    client: crate::client::Client,
13}
14
15impl KeysClient {
16    pub fn new(client: crate::client::Client) -> Self {
17        KeysClient { client }
18    }
19
20    /// Retrieve all keys registered for your organization.
21    pub async fn list_keys(
22        &self,
23        query: Option<ListKeysQuery>,
24    ) -> Result<ListKeysResponse, crate::error::Error> {
25        let mut path = String::from("/keys");
26        if let Some(query) = &query {
27            let mut q: Vec<String> = Vec::new();
28            if let Some(v) = &query.limit {
29                q.push(format!("limit={}", urlencoding::encode(&v.to_string())));
30            }
31            if let Some(v) = &query.pagination_token {
32                q.push(format!(
33                    "paginationToken={}",
34                    urlencoding::encode(&v.to_string())
35                ));
36            }
37            if let Some(v) = &query.owner {
38                q.push(format!("owner={}", urlencoding::encode(&v.to_string())));
39            }
40            if !q.is_empty() {
41                path.push('?');
42                path.push_str(&q.join("&"));
43            }
44        }
45        self.client
46            .request::<ListKeysResponse>(reqwest::Method::GET, &path, None, false)
47            .await
48    }
49
50    /// Creates a key for the given scheme and curve. Returns the new key entity.
51    pub async fn create_key(
52        &self,
53        body: CreateKeyRequest,
54    ) -> Result<CreateKeyResponse, crate::error::Error> {
55        let path = String::from("/keys");
56        let body = serde_json::to_value(&body)?;
57        self.client
58            .request::<CreateKeyResponse>(reqwest::Method::POST, &path, Some(&body), true)
59            .await
60    }
61
62    /// <Warning>
63    /// 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.
64    /// </Warning>
65    ///
66    pub async fn delegate_key(
67        &self,
68        key_id: String,
69        body: DelegateKeyRequest,
70    ) -> Result<DelegateKeyResponse, crate::error::Error> {
71        let path = format!("/keys/{}/delegate", urlencoding::encode(&key_id));
72        let body = serde_json::to_value(&body)?;
73        self.client
74            .request::<DelegateKeyResponse>(reqwest::Method::POST, &path, Some(&body), true)
75            .await
76    }
77
78    /// Retrieves a key information by its ID.
79    pub async fn get_key(&self, key_id: String) -> Result<GetKeyResponse, crate::error::Error> {
80        let path = format!("/keys/{}", urlencoding::encode(&key_id));
81        self.client
82            .request::<GetKeyResponse>(reqwest::Method::GET, &path, None, false)
83            .await
84    }
85
86    /// Updates the name of an existing key.
87    pub async fn update_key(
88        &self,
89        key_id: String,
90        body: UpdateKeyRequest,
91    ) -> Result<UpdateKeyResponse, crate::error::Error> {
92        let path = format!("/keys/{}", urlencoding::encode(&key_id));
93        let body = serde_json::to_value(&body)?;
94        self.client
95            .request::<UpdateKeyResponse>(reqwest::Method::PUT, &path, Some(&body), true)
96            .await
97    }
98
99    /// 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.
100    pub async fn delete_key(
101        &self,
102        key_id: String,
103    ) -> Result<DeleteKeyResponse, crate::error::Error> {
104        let path = format!("/keys/{}", urlencoding::encode(&key_id));
105        self.client
106            .request::<DeleteKeyResponse>(reqwest::Method::DELETE, &path, None, true)
107            .await
108    }
109
110    /// 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_`.
111    ///
112    /// <Tip>
113    /// 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.
114    pub async fn derive_key(
115        &self,
116        key_id: String,
117        body: DeriveKeyRequest,
118    ) -> Result<DeriveKeyResponse, crate::error::Error> {
119        let path = format!("/keys/{}/derive", urlencoding::encode(&key_id));
120        let body = serde_json::to_value(&body)?;
121        self.client
122            .request::<DeriveKeyResponse>(reqwest::Method::POST, &path, Some(&body), true)
123            .await
124    }
125
126    /// 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.
127    ///
128    /// 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.
129    ///
130    pub async fn export_key(
131        &self,
132        key_id: String,
133        body: ExportKeyRequest,
134    ) -> Result<ExportKeyResponse, crate::error::Error> {
135        let path = format!("/keys/{}/export", urlencoding::encode(&key_id));
136        let body = serde_json::to_value(&body)?;
137        self.client
138            .request::<ExportKeyResponse>(reqwest::Method::POST, &path, Some(&body), true)
139            .await
140    }
141
142    /// List all signature requests for a key.
143    pub async fn list_signatures(
144        &self,
145        key_id: String,
146        query: Option<ListSignaturesQuery>,
147    ) -> Result<ListSignaturesResponse, crate::error::Error> {
148        let mut path = format!("/keys/{}/signatures", urlencoding::encode(&key_id));
149        if let Some(query) = &query {
150            let mut q: Vec<String> = Vec::new();
151            if let Some(v) = &query.limit {
152                q.push(format!("limit={}", urlencoding::encode(&v.to_string())));
153            }
154            if let Some(v) = &query.pagination_token {
155                q.push(format!(
156                    "paginationToken={}",
157                    urlencoding::encode(&v.to_string())
158                ));
159            }
160            if !q.is_empty() {
161                path.push('?');
162                path.push_str(&q.join("&"));
163            }
164        }
165        self.client
166            .request::<ListSignaturesResponse>(reqwest::Method::GET, &path, None, false)
167            .await
168    }
169
170    /// Request to generate a signature with the key. **This process does not broadcast anything on-chain**, this is just an off-chain signature request.
171    ///
172    /// 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.
173    ///
174    pub async fn generate_signature(
175        &self,
176        key_id: String,
177        body: GenerateSignatureRequest,
178    ) -> Result<GenerateSignatureResponse, crate::error::Error> {
179        let path = format!("/keys/{}/signatures", urlencoding::encode(&key_id));
180        let body = serde_json::to_value(&body)?;
181        self.client
182            .request::<GenerateSignatureResponse>(reqwest::Method::POST, &path, Some(&body), true)
183            .await
184    }
185
186    /// Retrieve a signature request details.
187    pub async fn get_signature(
188        &self,
189        key_id: String,
190        signature_id: String,
191    ) -> Result<GetSignatureResponse, crate::error::Error> {
192        let path = format!(
193            "/keys/{}/signatures/{}",
194            urlencoding::encode(&key_id),
195            urlencoding::encode(&signature_id)
196        );
197        self.client
198            .request::<GetSignatureResponse>(reqwest::Method::GET, &path, None, false)
199            .await
200    }
201
202    /// 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).
203    ///
204    /// 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.
205    ///
206    pub async fn import_key(
207        &self,
208        body: ImportKeyRequest,
209    ) -> Result<ImportKeyResponse, crate::error::Error> {
210        let path = String::from("/keys/import");
211        let body = serde_json::to_value(&body)?;
212        self.client
213            .request::<ImportKeyResponse>(reqwest::Method::POST, &path, Some(&body), true)
214            .await
215    }
216}