Skip to content

Add DecodeScriptSegwit Parsing and Model Support for decodescript RPC Segwit Field #290

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Her-Code
Copy link

@Her-Code Her-Code commented Jul 11, 2025

Summary

This PR addresses #270
It adds parsing logic and model representation for the segwit field returned by the decodescript RPC, which was previously ignored.

Changes

  • Introduce DecodeScriptSegwit struct to model segwit data.
  • Update DecodeScript to include an optional segwit field.
  • Add DecodeScriptSegwitError for field-level error handling when parsing segwit.
  • Re-export DecodeScriptSegwit and DecodeScriptSegwitError in v22, v23, and mod.rs.
  • Set segwit to None for Core versions that don’t support it.

Copy link
Collaborator

@jamillambert jamillambert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for having a look into this. A few pointers, some are only minor but might be useful for you to improve going forward.

The commit log could be improved. Should start with a capital. You aren't actually adding segwit support, you are adding a model to convert the returned data from the RPC into rust-bitcoin types. I have been told to write commit logs in the imperative (like you are telling the commit what to do), so also suggesting it to you. i.e. Add struct, not added, adding etc. It fits with what git does when generating messages, and makes reading them easier if everyone does the same thing.

The general comment though is have a look at other RPCs in this crate, it is all very structured with the same format, comment style etc. and any new additions should follow this. If you're using AI, be more specific in what you want and give it some files to look at as a reference.

Comment on lines 127 to 144
pub struct DecodeScriptSegwit {
pub asm: String,
pub hex: String,
#[serde(rename = "type")]
pub type_: String,
pub address: Option<bitcoin::Address<bitcoin::address::NetworkUnchecked>>,
#[serde(rename = "reqSigs")]
pub required_signatures: Option<u64>,
pub addresses: Vec<bitcoin::Address<bitcoin::address::NetworkUnchecked>>,
#[serde(rename = "p2sh-segwit")]
pub p2sh_segtwit: Option<bitcoin::Address<bitcoin::address::NetworkUnchecked>>,
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of the structs in this crate have a specific format. Have a look at the others, e.g. the one above, and follow what is done there including comments, type declarations, use of serde rename in types and not model.

Comment on lines 30 to 37
let segwit = self.segwit.map(|s| DecodeScriptSegwit {
asm: s.asm,
hex: s.hex,
type_: s.type_,
address: s.address,
required_signatures: s.required_signatures,
addresses: s.addresses,
p2sh_segtwit: s.p2sh_segtwit,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be implemented as an into_model function for the DecodeScriptSegwit type, and all future versions should import and use it unless there is a change in the RPC. Have a look at another RPC that returns a json object that is modelled like GetTransaction and it's sub type GetTransactionDetail in v20 wallet.

@Her-Code Her-Code force-pushed the model-for-segwit branch 3 times, most recently from 448feed to c9964af Compare July 14, 2025 10:35
@Her-Code Her-Code requested a review from jamillambert July 14, 2025 10:52
Copy link
Collaborator

@jamillambert jamillambert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Getting better but still some things to work on.

Can you also update the PR title and description, you are not adding segwit support.

#[derive(Debug)]
pub enum DecodeScriptSegwitError {
/// An invalid or missing field in the `DecodeScriptSegwit` data.
InvalidField(&'static str),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this error variant or why it exists?

@@ -253,7 +253,7 @@ pub use self::{
blockchain::GetMempoolInfo,
control::Logging,
network::{Banned, GetPeerInfo, ListBanned},
raw_transactions::{DecodeScript, DecodeScriptError},
raw_transactions::{DecodeScript, DecodeScriptError,DecodeScriptSegwit,DecodeScriptSegwitError},
Copy link
Collaborator

@jamillambert jamillambert Jul 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
raw_transactions::{DecodeScript, DecodeScriptError,DecodeScriptSegwit,DecodeScriptSegwitError},
raw_transactions::{DecodeScript, DecodeScriptError, DecodeScriptSegwit, DecodeScriptSegwitError},

There are a number of formatting issues like this one. There is a script that will fix most of them just fmt it runs cargo fmt on the current nightly for the crate stored in nightly-version, which is not the most recent nightly. There are other helpful scripts in the justfile that are worth a look, there have been some changes in the rust nightly recently and the most recent nightly might give a few lint errors.

You should also be able to configure your editor to remove trailing white spaces. There are quite a few in this PR.

/// List of bitcoin addresses.
pub addresses: Vec<Address<NetworkUnchecked>>,
/// Address of the P2SH script wrapping this witness redeem script.
pub p2sh_segwit: Option<String>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be Address<NetworkUnchecked> not String. I think the same should also be done for DecodeScript.

@Her-Code Her-Code requested a review from jamillambert July 15, 2025 09:35
Copy link
Collaborator

@jamillambert jamillambert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR title still needs to be changed.

Can you squash the commits? and if you are still going to do the changes to existing docs can you do this in a separate commit.

The next step is to add a test, or update the existing test. And add all the reexports.


/// Error when converting a `DecodeScriptSegwit` type into the model type.
#[derive(Debug)]
pub enum DecodeScriptSegwitError {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not needed, it is identical the the exiting one. Import the previous version one and use that.

/// The raw output script bytes, hex-encoded.
pub hex: ScriptBuf,
/// Inferred descriptor for the script. v23 and later only.
pub descriptor: Option<String>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

descriptor should be where it appears in the list of the help, i.e. second last.


/// Error when converting a `DecodeScriptSegwit` type into the model type.
#[derive(Debug)]
pub enum DecodeScriptSegwitError {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be in v17

p2sh_segwit: self.p2sh_segwit,
})
}
}

impl DecodeScriptSegwit {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is needed in v17 as well

@@ -310,6 +310,7 @@ impl DecodeScript {
addresses,
p2sh,
p2sh_segwit: self.p2sh_segwit,
segwit: None,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is returned in v17 and just not documented until v19

@@ -23,9 +25,9 @@ pub use self::error::DecodeScriptError;
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct DecodeScript {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the docs changes in DecodeScript are not related to adding the DecodeScriptSegwit model. They appear to have been changed from what one version outputs to another's. I would just leave them as they were, or if they are wrong you can change them but do it in a separate commit.

@Her-Code Her-Code changed the title Add segwit support to DecodeScript model and conversions Add DecodeScriptSegwit Parsing and Model Support for decodescript RPC Segwit Field Jul 16, 2025
@Her-Code Her-Code requested a review from jamillambert July 16, 2025 12:35
- Add `DecodeScriptSegwit` struct to model the `segwit` field returned by the `decodescript` RPC.
- Update `DecodeScript` to include an optional `segwit` field.

Add DecodeScriptSegwit struct, conversions, and model support

- Add `DecodeScriptSegwit` struct to both versioned and model representations.
- Implement `into_model()` for `DecodeScriptSegwit` and update `DecodeScript` accordingly.
- Use `ScriptBuf` instead of `String` for `hex` to strongly type the field.
- Replace `String` with `Address<NetworkUnchecked>` for `p2sh_segwit` and other fields.
- Normalize and correct field comments to match Core `decodescript` RPC output.
-  Clean up formatting errors

Add DecodeScriptSegwit into_model to v17 and refactor error handling
- Add `into_model` implementation for `DecodeScriptSegwit` in v17.
- Return `segwit` in v17, as it is present in RPC output despite not being documented until v19.
- Add `DecodeScriptSegwitError` enum in v17, as `address` is sometimes `None` and error handling is needed.
- Remove duplicate `DecodeScriptSegwitError` from v23 and reuse the one from v22 via import.
- Move `descriptor` field in `DecodeScriptSegwit` model struct to match the field order in Bitcoin Core's `decodescript` RPC response.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants