Skip to content

Refactor SessionContext into precise, state-specific structs (remove bogus Option<T> fields) #929

Closed

Description

@Johnosezele

Context: Issue #897 – @DanGould pointed out that our current SessionContext struct is overloaded with Option<T> fields that are only "optional" because a single struct is reused across multiple protocol versions / states. Examples:

  • e: Option<HpkePublicKey> – meaningless for v1, mandatory for v2
  • fee-handling fields that may or may not exist depending on how far the state machine has advanced

This makes the type unsafe and forces every consumer to unwrap Options that, in practice, should never be None once the session is in a given state.

Proposed work:

  1. Split SessionContext into precise, non-optional structs per protocol/version (e.g. V1ReceiverContext, V2ReceiverContext).
  2. Move truly shared fields into a small common trait or embedding struct; everything else should be required by the type system.
  3. Update state-machine transitions and persister serialization accordingly.
  4. Remove now-bogus Option<T> wrappers.

Proposed Benefits:
• Stronger compile-time guarantees (no more silent None cases).
• Clearer code – each state knows exactly which data exists.
• Lays groundwork for future feature additions without adding more nullable fields.

This is larger than the narrow fee-rate bug in #897, so tracking it separately here.


Originating comment: #897 (comment)

Activity

  1. arminsabouri commented on Aug 5, 2025

    @arminsabouri
    Contributor

    e: Option – meaningless for v1, mandatory for v2

    Please keep in mind that the v2 reciever may receive a non-encrypted v1 payload -- with no reply key. In which case, the current code sets context.e = None. And if e is None the v2 receiver falls back to a un-encrypted v1 payload.

    if let Some(e) = &self.context.e {

    We could have a two version of struct V2ReceiverSessionContextWithReplyKey and V2ReceiverSessionContext but I feel like there is a better abstraction we can use here. I'll need to think about it a bit more

  2. arminsabouri commented on Aug 6, 2025

    @arminsabouri
    Contributor

    It feels a bit odd that fallbacks to v1 payloads are not configurable. As a user of wallet that uses bip77 I am opt'ing to using directory for convenience but with the understanding that the directory cannot see my payjoin proposal. However, if my counterparty decides to not send a reply key then I silently fallback to v1. Are my assumptions here correct? If so this should be configurable on SessionContext or some other abstraction. If the bip77 user does not opt into falling back we fatally fail the session. Just want to collect some feedback on this idea.

  3. nothingmuch commented on Aug 6, 2025

    @nothingmuch
    Contributor

    That is correct. Additionally there is no way for BIP 77 users to opt out of backwards compatibility entirely, i.e. require that the sender only use BIP 77.

    The argument for backwards compatibility could be that without doing any payjoin, what would be the fallback transaction is just broadcast by the sender, so in some sense BIP78 backwards compatibility through the directory is strictly an improvement on just broadcasting the fallback, but point of view overlooks that the receiver may not trust the directory with knowledge that they support, accept or participate in payjoins, which can have implications on analyzing proximate transactions.

    I strongly believe that we should have a BIP77 only mode, but I haven't yet made any proposed changes to the code and the spec to do this. I think the simplest way would be to allow either a pj parameter or a pjv2 parameter in the payment URIs, where the former is the same as it works now, but the latter would only be recognized by BIP 77 implementations, thereby opting out of BIP 78.

    Of course, nothing can be done to prevent a malicious sender from posting compromising information to the directory if it is malicious itself, for example it could select a non-random DL for the ECDH used in HPKE such that the directory can brute force that and decrypt the messages.

  4. DanGould commented on Sep 4, 2025

    @DanGould
    Member

    related: #797

  5. arminsabouri commented on Sep 22, 2025

    @arminsabouri
    Contributor

    An option here is fine. The complexity it would introduce into our state machine code is not worth it

  6. arminsabouri commented on Sep 22, 2025

    @arminsabouri
    Contributor

    #1104 superceeds this. Everytthing else that is optional in the session context is truly optional and can be left as is

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions