Skip to content

Restore script type formatters - #12

Merged
praveenperera merged 2 commits into
masterfrom
restore-script-type-formatters
Jun 18, 2026
Merged

praveenperera merged 2 commits into
masterfrom
restore-script-type-formatters

Conversation

@praveenperera

Copy link
Copy Markdown
Member

No description provided.

Track failures from each supported parse path

including generic JSON, Wasabi, Electrum, descriptor, and xpub.

This replaces single-terminal failure with actionable diagnostics.

Add coverage for malformed and unsupported payload classification.
@greptile-apps

greptile-apps Bot commented Jun 18, 2026

Copy link
Copy Markdown

Greptile Summary

This PR restores two script-type formatter methods (descriptor_derivation_path and wrap_with) to ScriptType, and refactors format detection in try_new_from_str to collect per-format failure details into a new FormatDetectionErrors struct rather than silently discarding them.

  • ScriptType::descriptor_derivation_path returns the hardened mainnet account path string (e.g. "84'/0'/0'") for each script type; ScriptType::wrap_with wraps a key expression string in the appropriate descriptor function (e.g. wpkh(...), tr(...)).
  • Format::try_new_from_str now accumulates all intermediate detection errors into Error::UnsupportedFormat(Box<FormatDetectionErrors>) instead of returning only the last failure, making diagnosis easier for callers.
  • The old JsonNoDecriptor (typo) and unused InvalidDescriptorInJson variants are removed and replaced with more precise per-stage error types (GenericJsonDetectionError, WalletJsonDetectionError, ChildXpubDetectionError).

Confidence Score: 5/5

The changes are safe to merge: the detection logic is additive (collecting errors that were previously discarded), the new ScriptType helpers are small and deterministic, and new tests cover the added error paths.

Both changed files introduce well-scoped additions — two pure formatting helpers on ScriptType and a structured rework of detection-error collection in formats.rs. The existing test for test_parse_key_expression_returns_non_incomplete_descriptor_errors remains satisfied because fatal descriptor errors still propagate directly via ? rather than being swallowed into UnsupportedFormat. No logic regressions or data-loss paths were identified.

No files require special attention.

Important Files Changed

Filename Overview
src/descriptor/script_type.rs Adds descriptor_derivation_path (returns a static &str for the BIP mainnet account path) and wrap_with (wraps a key expression in the script-type's descriptor function); both are straightforward and correct.
src/formats.rs Refactors format detection to collect all per-format failures into FormatDetectionErrors; removes the typo'd JsonNoDecriptor variant and unused InvalidDescriptorInJson; adds matching tests. Logic, error handling, and test coverage all look correct.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[try_new_from_str] --> B[detect_generic_json]
    B -->|Ok| R1[return Format::Json]
    B -->|Err| C[detect_wasabi_json\ncollect GenericJsonDetectionError]
    C -->|Ok| R2[return Format::Wasabi]
    C -->|Err| D[parse ElectrumJson\ncollect WalletJsonDetectionError]
    D -->|Ok| R3[return Format::Electrum]
    D -->|Err| E[Descriptors::try_from str\ncollect WalletJsonDetectionError]
    E -->|Ok| R4[return Format::Descriptor]
    E -->|Err| F[try_from_child_xpub_str\ncollect descriptor::Error]
    F -->|Ok| R5[return Format::Json]
    F -->|Err| G[KeyExpression::try_from_str\ncollect ChildXpubDetectionError]
    G -->|Err| H[return Error::UnsupportedFormat\nFormatDetectionErrors with all failures]
    G -->|Ok| I{has_descriptor_fields?}
    I -->|Yes| R6[return Format::KeyExpression]
    I -->|No| R7[return Format::Json via xpub path]
Loading
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
flowchart TD
    A[try_new_from_str] --> B[detect_generic_json]
    B -->|Ok| R1[return Format::Json]
    B -->|Err| C[detect_wasabi_json\ncollect GenericJsonDetectionError]
    C -->|Ok| R2[return Format::Wasabi]
    C -->|Err| D[parse ElectrumJson\ncollect WalletJsonDetectionError]
    D -->|Ok| R3[return Format::Electrum]
    D -->|Err| E[Descriptors::try_from str\ncollect WalletJsonDetectionError]
    E -->|Ok| R4[return Format::Descriptor]
    E -->|Err| F[try_from_child_xpub_str\ncollect descriptor::Error]
    F -->|Ok| R5[return Format::Json]
    F -->|Err| G[KeyExpression::try_from_str\ncollect ChildXpubDetectionError]
    G -->|Err| H[return Error::UnsupportedFormat\nFormatDetectionErrors with all failures]
    G -->|Ok| I{has_descriptor_fields?}
    I -->|Yes| R6[return Format::KeyExpression]
    I -->|No| R7[return Format::Json via xpub path]
Loading

Reviews (1): Last reviewed commit: "Add script type descriptor helpers" | Re-trigger Greptile

@praveenperera
praveenperera merged commit 3f9b301 into master Jun 18, 2026
9 checks passed
@praveenperera
praveenperera deleted the restore-script-type-formatters branch June 18, 2026 19:32
@coderabbitai

coderabbitai Bot commented Jun 18, 2026

Copy link
Copy Markdown

Caution

Review failed

An error occurred during the review process. Please try again later.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch restore-script-type-formatters

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant