Skip to content

refactor(wallet_policy): follow up on BIP-388 review - #1031

Open
trevarj wants to merge 6 commits into
rust-bitcoin:masterfrom
trevarj:wallet-policy-1019-followups
Open

refactor(wallet_policy): follow up on BIP-388 review#1031
trevarj wants to merge 6 commits into
rust-bitcoin:masterfrom
trevarj:wallet-policy-1019-followups

Conversation

@trevarj

@trevarj trevarj commented Aug 22, 2026

Copy link
Copy Markdown
Contributor

Follow up to #1019 with incorporated feedback.

  • add an infallible DerivPaths::single constructor
  • move WalletPolicy and KeyInfo into their own private modules
  • number key information during left-to-right descriptor translation, now that fix(iter): translate_pk to iterate in left-to-right order #1025 is merged
  • centralize template and key-information invariant checks in check_policy
  • validate replacement key information before mutating a policy

- add an infallible constructor for the single-path case
- the fields and validate() are now private to the submodule, so every
  construction path validates
- the TryFrom<&str> template branch goes through a private from_template
  instead of a struct literal
With `translate_pk` now walking the descriptor in left-to-right order,
`from_descriptor` no longer needs a separate `iter_pk` pass to number
the keys in textual order.

- drop the `iter_pk` pre-pass in `from_descriptor`
- number each distinct key at its first occurrence in the translator
Every construction and mutation of a `WalletPolicy` now runs through the same
checker.

- replace `validate` with a `check_policy` fn that the constructors
  call before building the struct
- `set_key_info` drops its own count check, rejects an empty argument
  and revalidates through `check_policy` before assigning
Comment thread src/descriptor/wallet_policy/policy.rs Outdated
}
}

struct WalletPolicyTranslator {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In 4974394:

I would suggest going further than this:

  • Make WalletPolicy::from_template pub(super)
  • Similarly add as_template and into_template methods which are also pub(super)

Then you can move WalletPolicyTranslator, both your existing public constructors, n_keys, and the Display/FromStr/TryFrom impls, all out of policy.rs and up to mod.rs.

This will leave policy.rs very small and easy for a reviewer to determine that every single constructor obeys the invariants. It would have only the pub(super) methods, key_info and set_key_info, and the private check_* methods in it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Nice, thanks. done in 28da2fa

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Looks better when reviewing all commits at once now.

- keep validated state construction and mutation in the private policy module
- move translation and public API implementations to the parent module
@trevarj
trevarj requested a review from apoelstra August 24, 2026 12:24
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.

2 participants