Skip to content

Commit 17e19ce

Browse files
committed
Merge #340: Test walletcreatefundedpsbt and walletprocesspsbt
1eff3fc Run the formatter (Jamil Lambert, PhD) e0d928c Test walletprocesspsbt (Jamil Lambert, PhD) 9c491d9 Test walletcreatefundedpsbt (Jamil Lambert, PhD) f0e8e4f Fix walletcreatefundedpsbt macro (Jamil Lambert, PhD) Pull request description: `walletcreatefundedpsbt` and `walletprocesspsbt` were implemented in v17 but untested. `walletcreatefundedpsbt` has no return changes up to v29, `walletprocesspsbt` changes once in v26. - Fix `walletcreatefundedpsbt` macro: The client macro passed an `Amount` to the RPC which parses to the wrong format resulting in 0 BTC for that address. The `inputs` struct has private fields and no constructor. Add a constructor for `WalletCreateFundedPsbtInput`. Convert the `Amount` in the `outputs` argument to an f64 in BTC before calling the RPC. - Add a test for `walletcreatefundedpsbt` and update the types table. - Update the `walletprocesspsbt` struct in v26, test and update the types table. - Run the formatter. ACKs for top commit: tcharding: ACK 1eff3fc Tree-SHA512: 9737def9bb6ee3a0804bda9a5e3443b0ffe37ddb5efec4d53ace7a43264a85f73c27343146c09c546f25d78226afaaaf1ad27709fde43a22093ce477851f7c3e
2 parents 0814d06 + 1eff3fc commit 17e19ce

File tree

21 files changed

+174
-45
lines changed

21 files changed

+174
-45
lines changed

client/src/client_sync/v17/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,11 @@ pub struct WalletCreateFundedPsbtInput {
241241
vout: u32,
242242
}
243243

244+
impl WalletCreateFundedPsbtInput {
245+
/// Create a new walletcreatefundedpsbt input entry.
246+
pub fn new(txid: Txid, vout: u32) -> Self { Self { txid, vout } }
247+
}
248+
244249
/// Args for the `addnode` method.
245250
#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
246251
#[serde(rename_all = "lowercase")]

client/src/client_sync/v17/wallet.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,16 @@ macro_rules! impl_client_v17__wallet_create_funded_psbt {
706706
inputs: Vec<WalletCreateFundedPsbtInput>,
707707
outputs: Vec<BTreeMap<Address, Amount>>,
708708
) -> Result<WalletCreateFundedPsbt> {
709-
self.call("walletcreatefundedpsbt", &[into_json(inputs)?, into_json(outputs)?])
709+
// Convert outputs: Vec<BTreeMap<Address, Amount>> to Vec<BTreeMap<String, f64>>
710+
let outputs_json: Vec<_> = outputs
711+
.into_iter()
712+
.map(|map| {
713+
map.into_iter()
714+
.map(|(addr, amt)| (addr.to_string(), amt.to_btc()))
715+
.collect::<BTreeMap<_, _>>()
716+
})
717+
.collect();
718+
self.call("walletcreatefundedpsbt", &[into_json(inputs)?, into_json(outputs_json)?])
710719
}
711720
}
712721
};
@@ -756,7 +765,11 @@ macro_rules! impl_client_v17__wallet_process_psbt {
756765
() => {
757766
impl Client {
758767
pub fn wallet_process_psbt(&self, psbt: &bitcoin::Psbt) -> Result<WalletProcessPsbt> {
759-
self.call("walletprocesspsbt", &[into_json(psbt)?])
768+
// Core expects the PSBT as a base64 string argument (same representation
769+
// used by `finalizepsbt`). Serializing the struct with `into_json` produced
770+
// an object which Core rejected ("Expected type string, got object").
771+
let psbt = format!("{}", psbt);
772+
self.call("walletprocesspsbt", &[psbt.into()])
760773
}
761774
}
762775
};

integration_test/tests/wallet.rs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@ use bitcoin::address::{Address, KnownHrp, NetworkChecked};
99
use bitcoin::bip32::{Xpriv, Xpub};
1010
use bitcoin::{secp256k1, Amount, CompressedPublicKey, FeeRate, Network, PrivateKey, PublicKey};
1111
use integration_test::{Node, NodeExt as _, Wallet};
12-
use node::{mtype, AddressType, ImportMultiRequest, ImportMultiScriptPubKey, ImportMultiTimestamp};
12+
use node::{
13+
mtype, AddressType, ImportMultiRequest, ImportMultiScriptPubKey, ImportMultiTimestamp, WalletCreateFundedPsbtInput
14+
};
1315

1416
#[cfg(not(feature = "v20_and_below"))]
1517
use node::ImportDescriptorsRequest;
1618

1719
use node::vtype::*; // All the version specific types.
20+
use std::collections::BTreeMap;
1821
use std::fs;
1922
use std::time::{SystemTime, UNIX_EPOCH};
2023

@@ -988,6 +991,47 @@ fn wallet__simulate_raw_transaction() {
988991
assert!(model.balance_change.is_negative());
989992
}
990993

994+
#[test]
995+
fn wallet__wallet_create_funded_psbt__modelled() {
996+
let node = Node::with_wallet(Wallet::Default, &[]);
997+
node.fund_wallet();
998+
999+
let addr = node.client.new_address().expect("newaddress");
1000+
let outputs = BTreeMap::from([(addr, Amount::from_sat(100_000))]);
1001+
let json: WalletCreateFundedPsbt = node
1002+
.client
1003+
.wallet_create_funded_psbt(vec![], vec![outputs])
1004+
.expect("walletcreatefundedpsbt");
1005+
1006+
let model: Result<mtype::WalletCreateFundedPsbt, WalletCreateFundedPsbtError> = json.into_model();
1007+
let psbt = model.unwrap();
1008+
1009+
assert!(!psbt.psbt.inputs.is_empty());
1010+
}
1011+
1012+
#[test]
1013+
fn wallet__wallet_process_psbt__modelled() {
1014+
let node = Node::with_wallet(Wallet::Default, &[]);
1015+
node.fund_wallet();
1016+
1017+
let addr = node.client.new_address().expect("newaddress");
1018+
let outputs = BTreeMap::from([(addr, Amount::from_sat(50_000))]);
1019+
let funded_psbt: WalletCreateFundedPsbt = node
1020+
.client
1021+
.wallet_create_funded_psbt(vec![], vec![outputs])
1022+
.expect("walletcreatefundedpsbt");
1023+
let funded_psbt_model: mtype::WalletCreateFundedPsbt = funded_psbt.into_model().unwrap();
1024+
1025+
let json: WalletProcessPsbt = node
1026+
.client
1027+
.wallet_process_psbt(&funded_psbt_model.psbt)
1028+
.expect("walletprocesspsbt");
1029+
let model: Result<mtype::WalletProcessPsbt, _> = json.into_model();
1030+
let processed = model.unwrap();
1031+
1032+
assert_eq!(processed.psbt.inputs.len(), funded_psbt_model.psbt.inputs.len());
1033+
}
1034+
9911035
#[test]
9921036
fn wallet__wallet_lock() {
9931037
let node = Node::with_wallet(Wallet::Default, &[]);

types/src/model/wallet.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,4 +867,6 @@ pub struct WalletProcessPsbt {
867867
pub psbt: Psbt,
868868
/// If the transaction has a complete set of signatures.
869869
pub complete: bool,
870+
/// The hex-encoded network transaction if complete.
871+
pub hex: Option<Transaction>,
870872
}

types/src/v17/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,11 @@
203203
//! | signmessage | version + model | |
204204
//! | signrawtransactionwithwallet | version + model | |
205205
//! | unloadwallet | returns nothing | |
206-
//! | walletcreatefundedpsbt | version + model | UNTESTED |
206+
//! | walletcreatefundedpsbt | version + model | |
207207
//! | walletlock | returns nothing | |
208208
//! | walletpassphrase | returns nothing | |
209209
//! | walletpassphrasechange | returns nothing | |
210-
//! | walletprocesspsbt | version + model | UNTESTED |
210+
//! | walletprocesspsbt | version + model | |
211211
//!
212212
//! </details>
213213
//!

types/src/v17/wallet/into.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,10 @@ impl WalletProcessPsbt {
763763
/// Converts version specific type to a version nonspecific, more strongly typed type.
764764
pub fn into_model(self) -> Result<model::WalletProcessPsbt, PsbtParseError> {
765765
let psbt = self.psbt.parse::<Psbt>()?;
766-
Ok(model::WalletProcessPsbt { psbt, complete: self.complete })
766+
Ok(model::WalletProcessPsbt {
767+
psbt,
768+
complete: self.complete,
769+
hex: None, // v26 and later only.
770+
})
767771
}
768772
}

types/src/v18/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,11 +205,11 @@
205205
//! | signmessage | version + model | |
206206
//! | signrawtransactionwithwallet | version + model | |
207207
//! | unloadwallet | returns nothing | |
208-
//! | walletcreatefundedpsbt | version + model | UNTESTED |
208+
//! | walletcreatefundedpsbt | version + model | |
209209
//! | walletlock | returns nothing | |
210210
//! | walletpassphrase | returns nothing | |
211211
//! | walletpassphrasechange | returns nothing | |
212-
//! | walletprocesspsbt | version + model | UNTESTED |
212+
//! | walletprocesspsbt | version + model | |
213213
//!
214214
//! </details>
215215
//!

types/src/v19/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,11 @@
207207
//! | signmessage | version + model | |
208208
//! | signrawtransactionwithwallet | version + model | |
209209
//! | unloadwallet | returns nothing | |
210-
//! | walletcreatefundedpsbt | version + model | UNTESTED |
210+
//! | walletcreatefundedpsbt | version + model | |
211211
//! | walletlock | returns nothing | |
212212
//! | walletpassphrase | returns nothing | |
213213
//! | walletpassphrasechange | returns nothing | |
214-
//! | walletprocesspsbt | version + model | UNTESTED |
214+
//! | walletprocesspsbt | version + model | |
215215
//!
216216
//! </details>
217217
//!

types/src/v20/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,11 @@
208208
//! | signmessage | version + model | |
209209
//! | signrawtransactionwithwallet | version + model | |
210210
//! | unloadwallet | returns nothing | |
211-
//! | walletcreatefundedpsbt | version + model | UNTESTED |
211+
//! | walletcreatefundedpsbt | version + model | |
212212
//! | walletlock | returns nothing | |
213213
//! | walletpassphrase | returns nothing | |
214214
//! | walletpassphrasechange | returns nothing | |
215-
//! | walletprocesspsbt | version + model | UNTESTED |
215+
//! | walletprocesspsbt | version + model | |
216216
//!
217217
//! </details>
218218
//!

types/src/v21/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,11 @@
214214
//! | signrawtransactionwithwallet | version + model | |
215215
//! | unloadwallet | returns nothing | |
216216
//! | upgradewallet | version | |
217-
//! | walletcreatefundedpsbt | version + model | UNTESTED |
217+
//! | walletcreatefundedpsbt | version + model | |
218218
//! | walletlock | returns nothing | |
219219
//! | walletpassphrase | returns nothing | |
220220
//! | walletpassphrasechange | returns nothing | |
221-
//! | walletprocesspsbt | version + model | UNTESTED |
221+
//! | walletprocesspsbt | version + model | |
222222
//!
223223
//! </details>
224224
//!

0 commit comments

Comments
 (0)