From 103c1950ba9af114cadde6f92117b31ec584ac73 Mon Sep 17 00:00:00 2001 From: edouard Date: Tue, 20 Dec 2022 13:06:39 +0100 Subject: [PATCH] Remove change_index from listspendtxs response Multiple change indexes may be present in a spend draft transaction and can be detected instead in the response psbt with the bip32_derivation outputs fields. --- doc/API.md | 1 - src/commands/mod.rs | 11 ++--------- src/commands/utils.rs | 26 +------------------------- tests/test_rpc.py | 2 -- 4 files changed, 3 insertions(+), 37 deletions(-) diff --git a/doc/API.md b/doc/API.md index d976e673..9c0a160f 100644 --- a/doc/API.md +++ b/doc/API.md @@ -174,7 +174,6 @@ This command does not take any parameter for now. | Field | Type | Description | | -------------- | ----------------- | ----------------------------------------------------------------------- | | `psbt` | string | Base64-encoded PSBT of the Spend transaction. | -| `change_index` | int or null | Index of the change output in the transaction outputs, if there is one. | ### `delspendtx` diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 64a4bc08..878f727f 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -10,9 +10,7 @@ use crate::{ descriptors, DaemonControl, VERSION, }; -use utils::{ - change_index, deser_amount_from_sats, deser_base64, deser_hex, ser_amount, ser_base64, ser_hex, -}; +use utils::{deser_amount_from_sats, deser_base64, deser_hex, ser_amount, ser_base64, ser_hex}; use std::{ collections::{hash_map, BTreeMap, HashMap}, @@ -507,11 +505,7 @@ impl DaemonControl { let spend_txs = db_conn .list_spend() .into_iter() - .map(|psbt| { - let change_index = - change_index(&psbt, &mut db_conn).map(|i| i.try_into().expect("insane usize")); - ListSpendEntry { psbt, change_index } - }) + .map(|psbt| ListSpendEntry { psbt }) .collect(); ListSpendResult { spend_txs } } @@ -786,7 +780,6 @@ pub struct CreateSpendResult { pub struct ListSpendEntry { #[serde(serialize_with = "ser_base64", deserialize_with = "deser_base64")] pub psbt: Psbt, - pub change_index: Option, } #[derive(Debug, Clone, Serialize, Deserialize)] diff --git a/src/commands/utils.rs b/src/commands/utils.rs index 58fe4972..6c1ae85a 100644 --- a/src/commands/utils.rs +++ b/src/commands/utils.rs @@ -1,8 +1,4 @@ -use crate::database::DatabaseConnection; - -use miniscript::bitcoin::{ - self, consensus, hashes::hex::FromHex, util::psbt::PartiallySignedTransaction as Psbt, -}; +use miniscript::bitcoin::{self, consensus, hashes::hex::FromHex}; use serde::{de, Deserialize, Deserializer, Serializer}; /// Serialize an amount as sats @@ -54,23 +50,3 @@ where let s = Vec::from_hex(&s).map_err(de::Error::custom)?; consensus::deserialize(&s).map_err(de::Error::custom) } - -// Utility to gather the index of a change output in a Psbt, if there is one. -pub fn change_index(psbt: &Psbt, db_conn: &mut Box) -> Option { - let network = db_conn.network(); - - for (i, txo) in psbt.unsigned_tx.output.iter().enumerate() { - // Small optimization. TODO: adapt once we have Taproot support. - if !txo.script_pubkey.is_v0_p2wsh() { - continue; - } - - if let Ok(address) = bitcoin::Address::from_script(&txo.script_pubkey, network) { - if let Some((_, true)) = db_conn.derivation_index_by_address(&address) { - return Some(i); - } - } - } - - None -} diff --git a/tests/test_rpc.py b/tests/test_rpc.py index add3f1cd..0b45e178 100644 --- a/tests/test_rpc.py +++ b/tests/test_rpc.py @@ -168,9 +168,7 @@ def test_list_spend(lianad, bitcoind): list_res = lianad.rpc.listspendtxs()["spend_txs"] assert len(list_res) == 2 first_psbt = next(entry for entry in list_res if entry["psbt"] == res["psbt"]) - assert first_psbt["change_index"] == 1 second_psbt = next(entry for entry in list_res if entry["psbt"] == res_b["psbt"]) - assert second_psbt["change_index"] is None # If we delete the first one, we'll get only the second one. first_psbt = PSBT.from_base64(res["psbt"])