Skip to content

Commit f394b7a

Browse files
authored
Merge pull request #121 from Tibo-lg/create-wallet-options-and-listwallets
CreateWallet update+ListWallets+GetWalletInfo
2 parents 77e2b73 + 0e203cf commit f394b7a

File tree

3 files changed

+175
-8
lines changed

3 files changed

+175
-8
lines changed

client/src/client.rs

+20-2
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,27 @@ pub trait RpcApi: Sized {
272272
&self,
273273
wallet: &str,
274274
disable_private_keys: Option<bool>,
275+
blank: Option<bool>,
276+
passphrase: Option<&str>,
277+
avoid_reuse: Option<bool>,
275278
) -> Result<json::LoadWalletResult> {
276-
let mut args = [wallet.into(), opt_into_json(disable_private_keys)?];
277-
self.call("createwallet", handle_defaults(&mut args, &[null()]))
279+
let mut args = [
280+
wallet.into(),
281+
opt_into_json(disable_private_keys)?,
282+
opt_into_json(blank)?,
283+
opt_into_json(passphrase)?,
284+
opt_into_json(avoid_reuse)?,
285+
];
286+
self.call("createwallet", handle_defaults(
287+
&mut args, &[false.into(), false.into(), into_json("")?, false.into()]))
288+
}
289+
290+
fn list_wallets(&self) -> Result<Vec<String>> {
291+
self.call("listwallets", &[])
292+
}
293+
294+
fn get_wallet_info(&self) -> Result<json::GetWalletInfoResult> {
295+
self.call("getwalletinfo", &[])
278296
}
279297

280298
fn backup_wallet(&self, destination: Option<&str>) -> Result<()> {

integration_test/src/main.rs

+116-6
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,23 @@ fn sbtc<F: Into<f64>>(btc: F) -> SignedAmount {
7575
SignedAmount::from_btc(btc.into()).unwrap()
7676
}
7777

78-
fn main() {
79-
let rpc_url = std::env::var("RPC_URL").expect("RPC_URL must be set");
80-
let auth = if let Ok(cookie) = std::env::var("RPC_COOKIE") {
81-
Auth::CookieFile(cookie.into())
78+
fn get_rpc_url() -> String {
79+
return std::env::var("RPC_URL").expect("RPC_URL must be set");
80+
}
81+
82+
fn get_auth() -> bitcoincore_rpc::Auth {
83+
if let Ok(cookie) = std::env::var("RPC_COOKIE") {
84+
return Auth::CookieFile(cookie.into());
8285
} else if let Ok(user) = std::env::var("RPC_USER") {
83-
Auth::UserPass(user, std::env::var("RPC_PASS").unwrap_or_default())
86+
return Auth::UserPass(user, std::env::var("RPC_PASS").unwrap_or_default());
8487
} else {
8588
panic!("Either RPC_COOKIE or RPC_USER + RPC_PASS must be set.");
8689
};
90+
}
91+
92+
fn main() {
93+
let rpc_url = get_rpc_url();
94+
let auth = get_auth();
8795

8896
let cl = Client::new(rpc_url, auth).unwrap();
8997

@@ -138,6 +146,7 @@ fn main() {
138146
test_ping(&cl);
139147
test_get_peer_info(&cl);
140148
test_rescan_blockchain(&cl);
149+
test_create_wallet(&cl);
141150
//TODO import_multi(
142151
//TODO verify_message(
143152
//TODO wait_for_new_block(&self, timeout: u64) -> Result<json::BlockRef> {
@@ -149,7 +158,6 @@ fn main() {
149158
//TODO add_multisig_address(
150159
//TODO load_wallet(&self, wallet: &str) -> Result<json::LoadWalletResult> {
151160
//TODO unload_wallet(&self, wallet: Option<&str>) -> Result<()> {
152-
//TODO create_wallet(
153161
//TODO backup_wallet(&self, destination: Option<&str>) -> Result<()> {
154162
test_stop(cl);
155163
}
@@ -787,6 +795,108 @@ fn test_rescan_blockchain(cl: &Client) {
787795
assert_eq!(stop, Some(count - 1));
788796
}
789797

798+
fn test_create_wallet(cl: &Client) {
799+
let wallet_names = vec!["alice", "bob", "carol", "denise", "emily"];
800+
801+
struct WalletParams<'a> {
802+
name: &'a str,
803+
disable_private_keys: Option<bool>,
804+
blank: Option<bool>,
805+
passphrase: Option<&'a str>,
806+
avoid_reuse: Option<bool>,
807+
}
808+
809+
let mut wallet_params = vec![
810+
WalletParams {
811+
name: wallet_names[0],
812+
disable_private_keys: None,
813+
blank: None,
814+
passphrase: None,
815+
avoid_reuse: None,
816+
},
817+
WalletParams {
818+
name: wallet_names[1],
819+
disable_private_keys: Some(true),
820+
blank: None,
821+
passphrase: None,
822+
avoid_reuse: None,
823+
},
824+
WalletParams {
825+
name: wallet_names[2],
826+
disable_private_keys: None,
827+
blank: Some(true),
828+
passphrase: None,
829+
avoid_reuse: None,
830+
}
831+
];
832+
833+
if version() >= 190000 {
834+
wallet_params.push(
835+
WalletParams {
836+
name: wallet_names[3],
837+
disable_private_keys: None,
838+
blank: None,
839+
passphrase: Some("pass"),
840+
avoid_reuse: None,
841+
}
842+
);
843+
wallet_params.push(
844+
WalletParams {
845+
name: wallet_names[4],
846+
disable_private_keys: None,
847+
blank: None,
848+
passphrase: None,
849+
avoid_reuse: Some(true),
850+
}
851+
);
852+
}
853+
854+
for wallet_param in wallet_params {
855+
let result = cl
856+
.create_wallet(
857+
wallet_param.name,
858+
wallet_param.disable_private_keys,
859+
wallet_param.blank,
860+
wallet_param.passphrase,
861+
wallet_param.avoid_reuse,
862+
)
863+
.unwrap();
864+
865+
assert_eq!(result.name, wallet_param.name);
866+
let expected_warning = match (wallet_param.passphrase, wallet_param.avoid_reuse) {
867+
(None, Some(true)) => Some("Empty string given as passphrase, wallet will not be encrypted.".to_string()),
868+
_ => Some("".to_string()),
869+
};
870+
assert_eq!(result.warning, expected_warning);
871+
872+
let wallet_client_url = format!("{}{}{}", get_rpc_url(), "/wallet/", wallet_param.name);
873+
let wallet_client = Client::new(wallet_client_url, get_auth()).unwrap();
874+
let wallet_info = wallet_client.get_wallet_info().unwrap();
875+
876+
assert_eq!(wallet_info.wallet_name, wallet_param.name);
877+
878+
let has_private_keys = !wallet_param.disable_private_keys.unwrap_or(false);
879+
assert_eq!(wallet_info.private_keys_enabled, has_private_keys);
880+
let has_hd_seed = has_private_keys && !wallet_param.blank.unwrap_or(false);
881+
assert_eq!(wallet_info.hd_seed_id.is_some(), has_hd_seed);
882+
let has_avoid_reuse = wallet_param.avoid_reuse.unwrap_or(false);
883+
assert_eq!(wallet_info.avoid_reuse.unwrap_or(false), has_avoid_reuse);
884+
assert_eq!(
885+
wallet_info.scanning.unwrap_or(json::ScanningDetails::NotScanning(false)),
886+
json::ScanningDetails::NotScanning(false));
887+
}
888+
889+
let mut wallet_list = cl.list_wallets().unwrap();
890+
891+
wallet_list.sort();
892+
893+
// Default wallet
894+
assert_eq!(wallet_list.remove(0), "");
895+
896+
// Created wallets
897+
assert!(wallet_list.iter().zip(wallet_names).all(|(a, b)| a == b));
898+
}
899+
790900
fn test_stop(cl: Client) {
791901
println!("Stopping: '{}'", cl.stop().unwrap());
792902
}

json/src/lib.rs

+39
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,45 @@ pub struct LoadWalletResult {
123123
pub warning: Option<String>,
124124
}
125125

126+
#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
127+
pub struct GetWalletInfoResult {
128+
#[serde(rename = "walletname")]
129+
pub wallet_name: String,
130+
#[serde(rename = "walletversion")]
131+
pub wallet_version: u32,
132+
#[serde(with = "bitcoin::util::amount::serde::as_btc")]
133+
pub balance: Amount,
134+
#[serde(with = "bitcoin::util::amount::serde::as_btc")]
135+
pub unconfirmed_balance: Amount,
136+
#[serde(with = "bitcoin::util::amount::serde::as_btc")]
137+
pub immature_balance: Amount,
138+
#[serde(rename = "txcount")]
139+
pub tx_count: usize,
140+
#[serde(rename = "keypoololdest")]
141+
pub keypool_oldest: usize,
142+
#[serde(rename = "keypoolsize")]
143+
pub keypool_size: usize,
144+
#[serde(rename = "keypoolsize_hd_internal")]
145+
pub keypool_size_hd_internal: usize,
146+
pub unlocked_until: Option<u64>,
147+
#[serde(rename = "paytxfee", with = "bitcoin::util::amount::serde::as_btc")]
148+
pub pay_tx_fee: Amount,
149+
#[serde(rename = "hdseedid")]
150+
pub hd_seed_id: Option<bitcoin::XpubIdentifier>,
151+
pub private_keys_enabled: bool,
152+
pub avoid_reuse: Option<bool>,
153+
pub scanning: Option<ScanningDetails>,
154+
}
155+
156+
#[derive(Clone, PartialEq, Debug, Deserialize, Serialize)]
157+
#[serde(untagged)]
158+
pub enum ScanningDetails {
159+
Scanning { duration: usize, progress: f32 },
160+
NotScanning(bool),
161+
}
162+
163+
impl Eq for ScanningDetails {}
164+
126165
#[derive(Clone, PartialEq, Debug, Deserialize, Serialize)]
127166
#[serde(rename_all = "camelCase")]
128167
pub struct GetBlockResult {

0 commit comments

Comments
 (0)