Skip to main content

dfns_sdk_rust/keys/
mod.rs

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