Skip to main content

factom/
walletd.rs

1//! General utility functions relating to factom-walletd
2use super::*;
3
4/// Return the wallet seed and all addresses in the wallet for backup and offline 
5/// storage.
6/// # Example
7/// ```
8/// use factom::*;
9/// 
10/// #[tokio::main]
11/// async fn main() {
12///   let client = Factom::new();
13///   let response = walletd::wallet_backup(&client).await.unwrap();
14///   dbg!(&response);
15///   assert!(response.success());
16/// }
17/// ```
18pub async fn wallet_backup(api: &Factom)
19    -> Result<ApiResponse<WalletBackup>>
20  {
21  let req =  ApiRequest::new("wallet-backup");
22  let response = walletd_call(api, req).await;
23  parse(response).await
24} 
25
26/// The wallet-balances API is used to query the acknowledged and saved balances for 
27/// all addresses in the currently running factom-walletd. The saved balance is the 
28/// last saved to the database and the acknowledged or “ack” balance is the balance 
29/// after processing any in-flight transactions known to the Factom node responding 
30/// to the API call. The factoid address balance will be returned in factoshis 
31/// (a factoshi is 10^8 factoids) not factoids(FCT) and the entry credit balance 
32/// will be returned in entry credits.
33/// 
34/// * If walletd and factomd are not both running this call will not work.
35/// 
36/// * If factomd is not loaded up all the way to last saved block it will 
37/// return: “result”:{“Factomd Error”:“Factomd is not fully booted, please 
38/// wait and try again.”}
39/// 
40/// * If an address is not in the correct format the call will return: 
41/// “result”:{“Factomd Error”:”There was an error decoding an address”}
42/// 
43/// * If an address does not have a public and private address known to the wallet 
44/// it will not be included in the balance.
45/// 
46/// * "fctaccountbalances" are the total of all factoid account balances returned 
47/// in factoshis.
48/// 
49/// * "ecaccountbalances" are the total of all entry credit account balances 
50/// returned in entry credits.
51  pub async fn wallet_balances(api: &Factom)
52-> Result<ApiResponse<WalletBalances>>
53{
54  let req =  ApiRequest::new("wallet-balances");
55  let response = walletd_call(api, req).await;
56  parse(response).await
57} 
58
59///  Unlocks this wallet for the amount of time specified in seconds by timeout. 
60///  The maximum amount of time a wallet can be unlocked for is 230 seconds (Roughly 
61///  34 Years… Give or take a decade). This command will only work on wallets that 
62///  are encrypted. If successful, returns the expiration time of your access as a 
63///  Unix timestamp.
64/// 
65/// While the wallet is locked, the only accessible RPC API commands are get-height, 
66/// properties, transactions, and unlock-wallet.
67pub async fn unlock_wallet(
68  api: &Factom, 
69  passphrase :&str, 
70  timeout: usize
71) -> Result<ApiResponse<UnlockWallet>>
72{
73  let mut req =  ApiRequest::new("unlock-wallet");
74  req.params.insert("passphrase".to_string(), json!(passphrase));
75  req.params.insert("timeout".to_string(), json!(timeout));
76  let response = walletd_call(api, req).await;
77  parse(response).await
78}
79
80/// Get the current hight of blocks that have been cached by the wallet while syncing.
81/// # Example
82/// ```
83/// use factom::*;
84/// 
85/// #[tokio::main]
86/// async fn main() {
87///   let client = Factom::open_node();
88///   let response = walletd::wallet_height(&client).await.unwrap();
89///   dbg!(&response);
90///   assert!(response.success());
91/// }
92/// ```
93pub async fn wallet_height(api: &Factom)
94  -> Result<ApiResponse<Height>>
95{
96  let req =  ApiRequest::new("get-height");
97  let response = walletd_call(api, req).await;
98  parse(response).await
99}
100
101/// Retrieve current properties of factom-walletd, including the wallet and wallet 
102/// API versions.
103/// # Example
104/// ```
105/// use factom::*;
106/// 
107/// #[tokio::main]
108/// async fn main() {
109///   let client = Factom::open_node();
110///   let response = walletd::wallet_properties(&client).await.unwrap();
111///   dbg!(&response);
112///   assert!(response.success());
113/// }
114/// ```
115pub async fn wallet_properties(api: &Factom)
116  -> Result<ApiResponse<Properties>>
117{
118  let req =  ApiRequest::new("properties");
119  let response = walletd_call(api, req).await;
120  parse(response).await
121}
122
123/// unlock-wallet function
124#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
125pub struct UnlockWallet {
126  pub success: bool,
127  pub unlockeduntil: i64,
128}
129
130/// wallet-backup function
131#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
132pub struct WalletBackup {
133  #[serde(rename = "wallet-seed")]
134  pub wallet_seed: String,
135  pub addresses: Vec<Address>,
136}
137
138#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
139pub struct Address {
140  pub public: String,
141  pub secret: String,
142}
143
144/// wallet-balances function
145#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
146pub struct WalletBalances {
147  pub fctaccountbalances: Fctaccountbalances,
148  pub ecaccountbalances: Ecaccountbalances,
149}
150
151#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
152pub struct Fctaccountbalances {
153  pub ack: i64,
154  pub saved: i64,
155}
156
157#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
158pub struct Ecaccountbalances {
159  pub ack: i64,
160  pub saved: i64,
161}
162
163
164/// sign-data function
165#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
166pub struct SignData {
167  pub pubkey: String,
168  pub signature: String,
169}
170
171/// wallet-properties function
172#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
173pub struct Properties {
174  pub walletversion: String,
175  pub walletapiversion: String,
176}
177
178/// get-height function
179#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
180pub struct Height {
181  pub height: i64,
182}
183