Skip to content
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

Use payjoin crate #8

Closed
wants to merge 2 commits into from
Closed

Conversation

evanlinjin
Copy link
Collaborator

@evanlinjin evanlinjin commented Aug 13, 2022

Replaces and closes #1

This is a continuation of @DanGould's work.

  • Use the payjoin crate for logic and various checks.
  • Add a bunch of TODO comments everywhere.
  • Some slight refactoring.

@evanlinjin evanlinjin changed the title Make proposal with payjoin crate (2) Use payjoin crate Aug 13, 2022
@evanlinjin evanlinjin requested a review from DanGould August 13, 2022 15:43
@evanlinjin evanlinjin marked this pull request as ready for review August 13, 2022 15:43
let reader = &*bytes;
let proposal = UncheckedProposal::from_request(reader, query, headers).unwrap();
// extract original PSBT from HTTP request
// TODO: `UncheckedProposal` is not really the proposal, but the original PSBT? Is the naming wrong?
Copy link
Contributor

Choose a reason for hiding this comment

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

UncheckedProposal is the OriginalPSBT + the sender's paramaters. It is a PayJoin proposal

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I mean the UncheckedProposal struct is badly named, because the proposed PSBT is what is returned to the receiver.

let proposal = UncheckedProposal::from_request(reader, query, headers).unwrap();
// extract original PSBT from HTTP request
// TODO: `UncheckedProposal` is not really the proposal, but the original PSBT? Is the naming wrong?
// TODO: Unneeded and/or undocumented traits in bip-78 library? (`Proposal`, `Headers`)
Copy link
Contributor

Choose a reason for hiding this comment

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

Proposal is probably unecessary, but I imagine as a way to extract psbt() -> Psbt between check states if necessary. That might only be necessary at the fully checked / unlockedProposal stage. We can play with the interface to arrive at v1.0

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is also an issue for the bip78.

// extract original PSBT from HTTP request
// TODO: `UncheckedProposal` is not really the proposal, but the original PSBT? Is the naming wrong?
// TODO: Unneeded and/or undocumented traits in bip-78 library? (`Proposal`, `Headers`)
// TODO: We should really save the original PSBT just in case the sender does not sign
Copy link
Contributor

Choose a reason for hiding this comment

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

Pretty much. This is most important in the case when the receiver is a payment processor that generates bip21 URIs on request, called a non-interactive receiver. If you're scanning someone next to you's phone it's not so important to broadcast the UncheckedProposal.extract_tx() in the failure case or even make sure it can be broadcast because humans will be able to interact to solve the fail case by trying again.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

As previously mentioned, if UncheckedProposal is renamed to OriginalPsbtRequest or something alike, it would be less confusing.

let fees = calculate_fees(
scheduled_payjoin.channels.len() as u64,
scheduled_payjoin.fee_rate,
scheduled_payjoin.wallet_amount != bitcoin::Amount::ZERO,
);

// TODO: not great
Copy link
Contributor

Choose a reason for hiding this comment

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

make TODOs actionable. Explain qualitative disagreements.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I mean we shouldn't have anything that panics when handling an http request

Comment on lines +325 to +328
psbt.unsigned_tx
.input
.iter_mut()
.for_each(|txin| txin.script_sig = bitcoin::Script::new());
Copy link
Contributor

Choose a reason for hiding this comment

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

Yes and this should be automated by the payjoin crate instead of written here. Best downstream code doesn't need to grok this piece

Comment on lines +338 to +340
// TODO: make sure it is impossible to create a BIP21pj linked to `ScheduledPayJoin`
// with a "total channel amount" larger than requested + fee
let total_channel_amount = scheduled_payjoin
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the problem with total_channel_amount > Bip78Uri.amount()`? The edge case I imagine is BTCPayServer requests an invoice for a strict amount and the excess is either 1. added to on chain single sig or 2. increase a channel amount. It does not seem like requesting more is dire.

Comment on lines +369 to +370
// TODO: Instead of interfacing with `tonic_lnd` directly, consider having a trait
// so this can be extended in the future
Copy link
Contributor

Choose a reason for hiding this comment

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

Agree. As soon as we duplicate code, abstract into a trait to support LDK & CLN.

// TODO: Creating `OpenChannelRequest` should be one thing, and async calls another
// TODO: Instead of interfacing with `tonic_lnd` directly, consider having a trait
// so this can be extended in the future
// TODO: What if channel open fails or takes too long?
Copy link
Contributor

Choose a reason for hiding this comment

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

We get a corrupt channel state 😬 This is the worst failure case in this code. Most important error to address.

Comment on lines 1 to 30
use std::collections::HashMap;
use std::sync::{Arc, Mutex};

use bitcoin::{Address, Script, TxOut};

use crate::ScheduledPayJoin;

#[derive(Default)]
pub(crate) struct PayJoinScheduler {
// maps scheduled pjs via spk
by_spk: HashMap<Script, ScheduledPayJoin>,
}

impl PayJoinScheduler {
fn insert(&mut self, addr: &Address, pj: &ScheduledPayJoin) -> Result<(), ()> {
let current_pj = &*self.by_spk.entry(addr.script_pubkey()).or_insert(pj.clone());
if current_pj == pj {
Err(()) // not inserted
} else {
Ok(()) // inserted
}
}

/// pop scheduled payjoin alongside txout
fn find_mut<'a, I>(&mut self, mut txouts: I) -> Option<(&'a mut TxOut, ScheduledPayJoin)>
where
I: Iterator<Item = &'a mut TxOut>,
{
txouts.find_map(|txo| self.by_spk.remove(&txo.script_pubkey).map(|pj| (txo, pj)))
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This file is superfluous and should be outside of our history

@DanGould DanGould closed this Aug 14, 2022
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