Skip to content

Seperating fee application from signing #870

Closed

Description

@arminsabouri

A typical wallet architecture separates responsibilities between a wallet management system and one or more signers. Examples include shared or hybrid custody setups like Casa or Liana, or hardware wallet configurations such as Ledger with Ledger Live. In this model, the signer focuses solely on managing private keys and signing. Signers often lack the necessary resources to progress a Payjoin session’s typestate. Resources required for advancing a Payjoin session include persistent storage, a network connection, and, in some typestate transitions, access to a Bitcoin node (check_broadcast_suitability). Under the new SEL paradigm introduced in Payjoin 0.24, the resource rich wallet management system advances the session typestate. However, transitioning beyond the receiver’s provisionalProposal typestate requires signing.

Application developers may attempt to extract the PSBT containing the contributed inputs, sign it, and then inject the finalized PSBT into the finalize_proposal closure. However, this approach fails because the receiver has not yet applied its expected fee contribution at the time of signing. As a result, the signed PSBT reflects incorrect fee contributions and will be rejected by the sender.

At the time of writing, two potential solutions exist but I am sure there are more.

  1. Introduce a new receiver typestate after fee application

Applying fees to the PSBT introduces new information that should be preserved independently of signing. Currently, fee application and signing are tightly coupled. Introducing a new typestate to represent a “fee-applied” PSBT would allow this data to be stored and retrieved through session history. The history interface should clearly indicate that this PSBT is ready for signing. Additionally, we should consider adding a new state transition method to provisional_proposal that accepts a finalized PSBT directly.

application developers are currently forced to work around the current API like this:

let psbt = ... // finalized PSBT from the signer
let proposal = proposal
    .finalize_proposal(
        |_| {
            Ok(psbt)
        },
        None,
        None,
    )
    .save(persister)?;
  1. Expose apply_fee() as a public method

A less invasive change to rust-payjoin would be to expose the apply_fee() method publicly. This would allow applications to apply fee contributions during the provisionalProposal typestate, save the resulting PSBT, sign it, and inject the finalized PSBT into finalize_proposal.

Activity

  1. benalleng commented on Jul 11, 2025

    @benalleng
    Collaborator

    What is the initial reason for keeping .apply_fee() private before now, just not useful outside of this specific case?

  2. spacebear21 commented on Jul 11, 2025

    @spacebear21
    Collaborator

    I'm in favor of introducing a new typestate like ProvisionalProposal::apply_fee() -> ProvisionalProposalWithFee.

    Additionally, we should consider adding a new state transition method to provisional_proposal that accepts a finalized PSBT directly.

    If the fee application is done in a prior state, wouldn't ProvisionalProposalWithFee::finalize_proposal() work just fine for simple signers?

  3. arminsabouri commented on Jul 11, 2025

    @arminsabouri
    ContributorAuthor

    What is the initial reason for keeping .apply_fee() private before now, just not useful outside of this specific case?

    I am not entirely sure. Perhaps because rust-payjoin assumes what ever entity is progressing the typestate also has access to the keys.

  4. arminsabouri commented on Jul 11, 2025

    @arminsabouri
    ContributorAuthor

    If the fee application is done in a prior state, wouldn't ProvisionalProposalWithFee::finalize_proposal() work just fine for simple signers?

    To be clear I am not advocating that we remove the current state transition (finalize_proposal). Just that we should have a method that allows you to transition the state if you already have a finalized psbt. i.e commit_finalized_psbt(&self, psbt: &Psbt) . This is only useful if signing is happening outside the wallet managment service.

  5. DanGould commented on Jul 11, 2025

    @DanGould
    Member

    Whatever decision is made, I'd much rather have one code path, even if it requires multiple calls, than two parallel branching and reuniting code paths for the sake of simplicity.

    My understanding is that the problem is that the fee application might be done in a completely separate and asynchronous process to signing and the program should be treated as though it could even shut down between these phases? If so, I agree with the state separation strategy. It was built this way assuming the constraint you're describing didn't exist.

    I'd prefer more descriptive state transition names than "ProvisionalProposalWithFee". Perhaps the existing WantsInputs to WantsFeeRange (where min/max receiver fee preferences can be applied) to ProvisionalProposal (or a newWantsSigner type which carries more meaning) to PayjoinProposal

    Now I'm wondering even if it's possible for a receiver to have multiple signers, and if this design with a separate but explicit WantsSigner is sufficient with single finalize_proposal function or if we need to support adding signatures + a separate commitment as we do with WantsInputs/WantsOutputs

    Fundamentally this issue presents as solution-oriented. Please make sure we understand the problem before executing a particular solution.

  6. spacebear21 commented on Jul 16, 2025

    @spacebear21
    Collaborator

    Carrying over this comment by @arminsabouri in #881 (comment) regarding renaming ProvisionalProposal to WantsSigner:

    I don't understand the rationale for this naming change or the new methods. rust-payjoin should less involved in signing operations and by extension application business logic. All rust-payjoin cares about from ProvisionalProposal to PayjoinProposal is that all the inputs belonging to the reciever are finalized. How it finalizes the inputs should be a application concern. Perhaps the naming should be WantsFinalizedInputs or WantsFinalizedProposal.

  7. DanGould commented on Jul 16, 2025

    @DanGould
    Member

    I don't understand the rationale for this naming change

    I'd prefer more descriptive state transition names ... a new WantsSigner type which carries more meaning [about the operation the type requires than the current name ProvisionalProposal]

    or the new methods

    I'm wondering even if it's possible for a receiver to have multiple signers

    whether or not this is a payjoin concern is valid. That's what I'm asking

  8. arminsabouri commented on Jul 16, 2025

    @arminsabouri
    ContributorAuthor

    I understand the rationale for "more descriptive state transition names" but not for the typestate names suggested. I would advocate for more general alternatives for the provisional proposal typestate (WantsFinalizedInputs or WantsFinalizedPsbt) for the same rationale listed in my original comment. #870 (comment)

  9. DanGould commented on Jul 17, 2025

    @DanGould
    Member

    Ah yep it was the first thing that came to mind. My understanding is that both signing and input finalizer psbt roles are left to execute at the sender at that time there, where signing took precedence in my mind. I'm sure your decision wrt name will suffice.

  10. spacebear21 commented on Jul 17, 2025

    @spacebear21
    Collaborator

    consider adding a new state transition method to provisional_proposal that accepts a finalized PSBT directly.

    Just making sure I understand the issue, currently Liana does this:

                let proposal = proposal
                    .finalize_proposal(|_| {
                        let mut psbt = psbt.clone();
                        finalize_psbt(&mut psbt, secp);
                        Ok(psbt)
                    })

    Instead of something like:

                let proposal = proposal
                    .finalize_proposal(|psbt| {
                        Ok(finalize_psbt(&psbt, secp))
                    })

    Because Liana must get the PSBT from DB in order to sign it? I don't totally understand the requirement here.

    Letting callers pass any arbitrary PSBT to the PayjoinProposal state seems like it would break the typestate mold since we can no longer guarantee that the PSBT being finalized is a valid payjoin PSBT?

  11. spacebear21 commented on Jul 17, 2025

    @spacebear21
    Collaborator

    Another thing I noticed while staring at this:

    finalize_proposal takes a closure wallet_process_psbt for the signing operation, but that parameter name is tightly coupled with the specific bitcoin-cli rpc command. This closure should be renamed to e.g. apply_receiver_signatures.

  12. arminsabouri commented on Jul 28, 2025

    @arminsabouri
    ContributorAuthor

    Apologies I missed this comment.

    Because Liana must get the PSBT from DB in order to sign it? I don't totally understand the requirement here.

    Yes, this is correct. The Liana gui application responsible for signing will pull PSBTs from the Liana Deamon process. Lianad will in turn pull PSBTs from a database table. When we apply the reciever fee preferences to the payjoin PSBT we then save it in Lianad's PSBT table. One way to prevent an arbituary PSBT from being provided to finalize_proposal is to compare ntxid against the previous typestate.

  13. DanGould commented on Sep 2, 2025

    @DanGould
    Member

    Closed by #881 ? @arminsabouri

  14. arminsabouri commented on Sep 2, 2025

    @arminsabouri
    ContributorAuthor

    Closed by #881 ? @arminsabouri

    yes!

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions