Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CPFP fixups and followups #332

Merged
merged 13 commits into from
Dec 14, 2021
Merged
88 changes: 43 additions & 45 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ path = "src/bin/cli.rs"
bench = []

[dependencies]
revault_tx = { git = "https://github.com/danielabrozzoni/revault_tx/", branch = "2021109_support_cpfp", features = ["use-serde"] }
revault_net = { git = "https://github.com/danielabrozzoni/revault_net/", branch = "revault_tx_update" }
revault_tx = { git = "https://github.com/revault/revault_tx", features = ["use-serde"] }
revault_net = { git = "https://github.com/revault/revault_net" }

# In order to have a backtrace on panic, because the
# stdlib does not have a programmatic interface yet
Expand Down
35 changes: 15 additions & 20 deletions src/daemon/bitcoind/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ use crate::common::config::BitcoindConfig;
use crate::daemon::{bitcoind::BitcoindError, revaultd::BlockchainTip};
use revault_tx::{
bitcoin::{
blockdata::constants::COIN_VALUE,
consensus::{encode, Decodable},
hashes::hex::FromHex,
util::bip32::ChildNumber,
Amount, BlockHash, OutPoint, Script, Transaction, TxOut, Txid,
blockdata::constants::COIN_VALUE, consensus::encode, util::bip32::ChildNumber,
util::psbt::PartiallySignedTransaction as Psbt, Amount, BlockHash, OutPoint, Script,
Transaction, TxOut, Txid,
},
transactions::{DUST_LIMIT, UNVAULT_CPFP_VALUE},
};
Expand Down Expand Up @@ -717,8 +715,15 @@ impl BitcoinD {
})
}

pub fn sign_psbt(&self, psbt: String) -> Result<(bool, String), BitcoindError> {
let res = self.make_cpfp_request("walletprocesspsbt", &params!(Json::String(psbt)))?;
/// Make bitcoind:
/// 1. Add information to the PSBT inputs
/// 2. Sign the PSBT inputs it can
/// 3. Finalize the PSBT if it is complete
pub fn sign_psbt(&self, psbt: &Psbt) -> Result<(bool, Psbt), BitcoindError> {
let res = self.make_cpfp_request(
"walletprocesspsbt",
&params!(Json::String(base64::encode(&encode::serialize(psbt)))),
)?;
let complete = res
.get("complete")
.expect("API break: no 'complete' in 'walletprocesspsbt' result")
Expand All @@ -730,22 +735,12 @@ impl BitcoinD {
.as_str()
.expect("API break: invalid 'psbt' in 'walletprocesspsbt' result")
.to_string();
let psbt =
encode::deserialize(&base64::decode(psbt).expect("bitcoind returned invalid base64"))
.expect("bitcoind returned an invalid PSBT.");
Ok((complete, psbt))
}

pub fn finalize_psbt(&self, psbt: String) -> Result<Transaction, BitcoindError> {
let res = self.make_cpfp_request("finalizepsbt", &params!(Json::String(psbt)))?;
let hex_str = res
.get("hex")
.expect("API break: no 'hex' in 'finalizepsbt' result")
.as_str()
.expect("API break: invalid 'hex' in 'finalizepsbt' result");
let hex = <Vec<u8>>::from_hex(hex_str)
.expect("API break: invalid 'hex' in 'finalizepsbt' result");
Ok(Transaction::consensus_decode(hex.as_slice())
.expect("API break: invalid 'hex' in 'finalizepsbt' result"))
}

/// Broadcast a transaction with 'sendrawtransaction', discarding the returned txid
pub fn broadcast_transaction(&self, tx: &Transaction) -> Result<(), BitcoindError> {
let tx_hex = encode::serialize_hex(tx);
Expand Down
Loading