1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//! General utility functions relating to factom-walletd
use super::*;

/// Return the wallet seed and all addresses in the wallet for backup and offline 
/// storage.
/// # Example
/// ```
/// use factom::*;
/// 
/// #[tokio::main]
/// async fn main() {
///   let client = Factom::new();
///   let response = walletd::wallet_backup(&client).await.unwrap();
///   dbg!(&response);
///   assert!(response.success());
/// }
/// ```
pub async fn wallet_backup(api: &Factom)
    -> Result<ApiResponse<WalletBackup>>
  {
  let req =  ApiRequest::new("wallet-backup");
  let response = walletd_call(api, req).await;
  parse(response).await
} 

/// The wallet-balances API is used to query the acknowledged and saved balances for 
/// all addresses in the currently running factom-walletd. The saved balance is the 
/// last saved to the database and the acknowledged or “ack” balance is the balance 
/// after processing any in-flight transactions known to the Factom node responding 
/// to the API call. The factoid address balance will be returned in factoshis 
/// (a factoshi is 10^8 factoids) not factoids(FCT) and the entry credit balance 
/// will be returned in entry credits.
/// 
/// * If walletd and factomd are not both running this call will not work.
/// 
/// * If factomd is not loaded up all the way to last saved block it will 
/// return: “result”:{“Factomd Error”:“Factomd is not fully booted, please 
/// wait and try again.”}
/// 
/// * If an address is not in the correct format the call will return: 
/// “result”:{“Factomd Error”:”There was an error decoding an address”}
/// 
/// * If an address does not have a public and private address known to the wallet 
/// it will not be included in the balance.
/// 
/// * "fctaccountbalances" are the total of all factoid account balances returned 
/// in factoshis.
/// 
/// * "ecaccountbalances" are the total of all entry credit account balances 
/// returned in entry credits.
  pub async fn wallet_balances(api: &Factom)
-> Result<ApiResponse<WalletBalances>>
{
  let req =  ApiRequest::new("wallet-balances");
  let response = walletd_call(api, req).await;
  parse(response).await
} 

///  Unlocks this wallet for the amount of time specified in seconds by timeout. 
///  The maximum amount of time a wallet can be unlocked for is 230 seconds (Roughly 
///  34 Years… Give or take a decade). This command will only work on wallets that 
///  are encrypted. If successful, returns the expiration time of your access as a 
///  Unix timestamp.
/// 
/// While the wallet is locked, the only accessible RPC API commands are get-height, 
/// properties, transactions, and unlock-wallet.
pub async fn unlock_wallet(
  api: &Factom, 
  passphrase :&str, 
  timeout: usize
) -> Result<ApiResponse<UnlockWallet>>
{
  let mut req =  ApiRequest::new("unlock-wallet");
  req.params.insert("passphrase".to_string(), json!(passphrase));
  req.params.insert("timeout".to_string(), json!(timeout));
  let response = walletd_call(api, req).await;
  parse(response).await
}

/// Get the current hight of blocks that have been cached by the wallet while syncing.
/// # Example
/// ```
/// use factom::*;
/// 
/// #[tokio::main]
/// async fn main() {
///   let client = Factom::open_node();
///   let response = walletd::wallet_height(&client).await.unwrap();
///   dbg!(&response);
///   assert!(response.success());
/// }
/// ```
pub async fn wallet_height(api: &Factom)
  -> Result<ApiResponse<Height>>
{
  let req =  ApiRequest::new("get-height");
  let response = walletd_call(api, req).await;
  parse(response).await
}

/// Retrieve current properties of factom-walletd, including the wallet and wallet 
/// API versions.
/// # Example
/// ```
/// use factom::*;
/// 
/// #[tokio::main]
/// async fn main() {
///   let client = Factom::open_node();
///   let response = walletd::wallet_properties(&client).await.unwrap();
///   dbg!(&response);
///   assert!(response.success());
/// }
/// ```
pub async fn wallet_properties(api: &Factom)
  -> Result<ApiResponse<Properties>>
{
  let req =  ApiRequest::new("properties");
  let response = walletd_call(api, req).await;
  parse(response).await
}

/// unlock-wallet function
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct UnlockWallet {
  pub success: bool,
  pub unlockeduntil: i64,
}

/// wallet-backup function
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct WalletBackup {
  #[serde(rename = "wallet-seed")]
  pub wallet_seed: String,
  pub addresses: Vec<Address>,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Address {
  pub public: String,
  pub secret: String,
}

/// wallet-balances function
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct WalletBalances {
  pub fctaccountbalances: Fctaccountbalances,
  pub ecaccountbalances: Ecaccountbalances,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Fctaccountbalances {
  pub ack: i64,
  pub saved: i64,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Ecaccountbalances {
  pub ack: i64,
  pub saved: i64,
}


/// sign-data function
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SignData {
  pub pubkey: String,
  pub signature: String,
}

/// wallet-properties function
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Properties {
  pub walletversion: String,
  pub walletapiversion: String,
}

/// get-height function
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Height {
  pub height: i64,
}