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.
This commit is contained in:
edouard 2022-12-20 13:06:39 +01:00
parent edb1659f89
commit 103c1950ba
4 changed files with 3 additions and 37 deletions

View File

@ -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`

View File

@ -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<u32>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]

View File

@ -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<dyn DatabaseConnection>) -> Option<usize> {
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
}

View File

@ -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"])