Skip to content

Commit 478a53c

Browse files
committed
Use ScriptPubKey model in GetDescriptorActivity
A new model with strongly typed fields for ScriptPubKey has been added. Update GetDescriptorActivity model to use it. Add the required error variants and into_model function.
1 parent 267c004 commit 478a53c

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

types/src/model/blockchain.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use bitcoin::{
1515
};
1616
use serde::{Deserialize, Serialize};
1717

18-
use crate::ScriptPubkey;
18+
use super::ScriptPubkey;
1919

2020
/// Models the result of JSON-RPC method `dumptxoutset`.
2121
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]

types/src/v29/blockchain/error.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use bitcoin::hex::HexToBytesError;
88
use bitcoin::{address, amount, hex, network};
99

1010
use crate::error::write_err;
11-
use crate::NumericError;
11+
use crate::{NumericError, ScriptPubkeyError};
1212

1313
/// Error when converting a `GetBlockVerboseOne` type into the model type.
1414
#[derive(Debug)]
@@ -295,6 +295,10 @@ pub enum GetDescriptorActivityError {
295295
/// We wrap the inner error to provide context. This might not be strictly necessary
296296
/// if the inner errors are distinct enough, but can be helpful.
297297
ActivityEntry(Box<GetDescriptorActivityError>), // Use Box to avoid recursive type size issues
298+
/// Conversion of the `prevout_spk` field failed.
299+
PrevoutSpk(ScriptPubkeyError),
300+
/// Conversion of the `output_spk` field failed.
301+
OutputSpk(ScriptPubkeyError),
298302
}
299303

300304
impl fmt::Display for GetDescriptorActivityError {
@@ -308,6 +312,8 @@ impl fmt::Display for GetDescriptorActivityError {
308312
Script(ref e) => write_err!(f, "conversion of the script `hex` field failed"; e),
309313
Address(ref e) => write_err!(f, "conversion of the `address` field failed"; e),
310314
ActivityEntry(ref e) => write_err!(f, "conversion of an activity entry failed"; e),
315+
PrevoutSpk(ref e) => write_err!(f, "conversion of the `prevout_spk` field failed"; e),
316+
OutputSpk(ref e) => write_err!(f, "conversion of the `output_spk` field failed"; e),
311317
}
312318
}
313319
}
@@ -324,6 +330,8 @@ impl std::error::Error for GetDescriptorActivityError {
324330
Script(ref e) => Some(e),
325331
Address(ref e) => Some(e),
326332
ActivityEntry(ref e) => Some(&**e), // Deref the Box to get the inner error
333+
PrevoutSpk(ref e) => Some(e),
334+
OutputSpk(ref e) => Some(e),
327335
}
328336
}
329337
}

types/src/v29/blockchain/into.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ impl GetDescriptorActivity {
225225
spend.height.map(|h| crate::to_u32(h, "height")).transpose()?;
226226
let spend_txid = Txid::from_str(&spend.spend_txid).map_err(E::Hash)?;
227227
let prevout_txid = Txid::from_str(&spend.prevout_txid).map_err(E::Hash)?;
228+
let prevout_spk = spend.prevout_spk.into_model().map_err(E::PrevoutSpk)?;
228229

229230
Ok(model::ActivityEntry::Spend(model::SpendActivity {
230231
amount,
@@ -234,7 +235,7 @@ impl GetDescriptorActivity {
234235
spend_vout: spend.spend_vout,
235236
prevout_txid,
236237
prevout_vout: spend.prevout_vout,
237-
prevout_spk: spend.prevout_spk,
238+
prevout_spk,
238239
}))
239240
}
240241
ActivityEntry::Receive(receive) => {
@@ -247,14 +248,15 @@ impl GetDescriptorActivity {
247248
let height =
248249
receive.height.map(|h| crate::to_u32(h, "height")).transpose()?; // Uses From<NumericError>
249250
let txid = Txid::from_str(&receive.txid).map_err(E::Hash)?;
251+
let output_spk = receive.output_spk.into_model().map_err(E::OutputSpk)?;
250252

251253
Ok(model::ActivityEntry::Receive(model::ReceiveActivity {
252254
amount,
253255
block_hash,
254256
height,
255257
txid,
256258
vout: receive.vout,
257-
output_spk: receive.output_spk,
259+
output_spk,
258260
}))
259261
}
260262
}

0 commit comments

Comments
 (0)