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

Switch account signing over to PSBT, allow deposits/account creation from P2TR inputs #370

Merged
merged 7 commits into from
Jul 4, 2022

Conversation

guggero
Copy link
Member

@guggero guggero commented Jun 9, 2022

Depends on lightninglabs/lndclient#105.

This PR moves over the signing operations from SignOutputRaw and ComputeInputScript to SignPsbt and FinalizePsbt in order to prepare for Taproot/MuSig2 compatible trader accounts.

With this change we also allow using P2TR wallet inputs for creating new accounts or for depositing into existing accounts.

Pull Request Checklist

  • LndServices minimum version has been updated if new lnd apis/fields are
    used.

return order.ChannelTypePeerDependent
}

// TODO(guggero): Use order.ChannelTypeScriptEnforced here as
Copy link
Contributor

Choose a reason for hiding this comment

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

💯

@guggero guggero force-pushed the sign-psbt branch 3 times, most recently from 8d8eb53 to 17866e9 Compare June 10, 2022 13:04
pIn.FinalScriptWitness, err = serializeWitness([][]byte{ourSig})
// We temporarily set the final witness to the partial sig to allow the
// extraction of the final TX. Unless we're using the expiry path in
// which case we _can_ create the full and final witness.
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

@lightninglabs-deploy
Copy link
Collaborator

@Roasbeef: review reminder

Copy link
Member

@Roasbeef Roasbeef left a comment

Choose a reason for hiding this comment

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

PSBT all the things!

Took an initial pass over the PR, my main question/comment is: why not just also use FundPSBT so we can remove all the coin selection logic (which was copied from lnd mostly so it's duplicated) into a single place? This would also mean that users would be able to use lnd's other coin selection mode (random inputs) as well.

account/manager.go Outdated Show resolved Hide resolved
account/manager.go Show resolved Hide resolved
account/manager.go Outdated Show resolved Hide resolved
account/manager.go Outdated Show resolved Hide resolved
account/manager.go Show resolved Hide resolved
account/manager.go Show resolved Hide resolved
account/coin_selection.go Outdated Show resolved Hide resolved
account/manager.go Outdated Show resolved Hide resolved
@guggero
Copy link
Member Author

guggero commented Jun 24, 2022

@Roasbeef thanks for the review! I did think about using FundPsbt but then was a bit disappointed that we (meaning I) built it in a way that coin selection only happens if you don't specify any inputs. And since we always have the account input, that wouldn't work. But after seeing your trick in Taro where you create a fake output for something to fund the PSBT that got me thinking that we could do some tricks here too. Basically fund a PSBT just for the deposit amount being sent to a P2WSH output and some additional fees (1 extra account witness input), then add the input after funding.
Going to attempt that, will be very nice to get rid of all the coin selection code!

@dstadulis
Copy link
Contributor

oli needs to rework to use the PSBT call for the deposit, needs some tricks bc the RPC doesn't allow inputs to specified

@guggero
Copy link
Member Author

guggero commented Jun 28, 2022

Addressed all comments but something doesn't seem to be working just yet. Will debug further tomorrow.

Copy link
Member

@Roasbeef Roasbeef left a comment

Choose a reason for hiding this comment

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

Love all the code deletion in this latest diff! Happy we were able to work around the lack of functionality on the lnd PSBT side. I think we'll need to do something similar on a new project we're working on as well

account/coin_selection.go Show resolved Hide resolved
// Unfortunately the FundPsbt call doesn't allow us to specify _any_
// inputs, otherwise it won't perform coin selection at all. So what we
// do instead is to fund our account output just for the funding amount
// plus whatever we need to pay for the additional input (which we know
Copy link
Member

Choose a reason for hiding this comment

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

Nice work around! We'll probably eventually patch this behavior, but good to be abble to get by w/ less as well.

Copy link
Member

Choose a reason for hiding this comment

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

cc @jharveyb -- this is the workaround I was talking about earlier

// We'll start by obtaining our global lock ID.
lockID, err := m.cfg.Store.LockID()
outputToFund := &wire.TxOut{
Value: int64(depositAmount + acctInputFee),
Copy link
Member

Choose a reason for hiding this comment

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

This should be ok, but part of me wonders if there's some weird edge case here w/ rounding and amounts. Worst case, I guess we get rejected due to having insufficient fees.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, we should always over estimate by a little bit anyway since most of the time the signatures don't end up being the full 73 bytes. But I went ahead and added a min relay fee check anyway.

@guggero
Copy link
Member Author

guggero commented Jun 30, 2022

Okay, after spending way too much time finding out that my problem was actually lightningnetwork/lnd#6626 I created the fix in lightningnetwork/lnd#6687.
Now we have the problem that we can't accept deposits from NP2WKH inputs if you don't have lnd v0.15.1-beta or later.
I added a workaround that just errors out in that case (which is important to do before we contact the auctioneer, otherwise we'll end up with an account diff that never confirms).

I hope that workaround is okay!

@guggero guggero requested review from Roasbeef and positiveblue June 30, 2022 12:14
guggero added 3 commits June 30, 2022 15:39
With lnd 0.15.0 almost out of the door, it is reasonable for us to ask
users to be on the latest 0.14.x version.
We use the walletrpc.SignPsbt method in Pool which was added in 0.14.2.
@dstadulis
Copy link
Contributor

@Roasbeef
Copy link
Member

I added a workaround that just errors out in that case (which is important to do before we contact the auctioneer, otherwise we'll end up with an account diff that never confirms).

Ahh, makes sense. I think this is OK, as it lets us have the code be uniform vs special casing the np2wkh case. TBH, I don't think it's used all that often these days, since even bech32m usage is on the rise.

Copy link
Member

@Roasbeef Roasbeef left a comment

Choose a reason for hiding this comment

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

LGTM 🎊

Copy link
Contributor

@positiveblue positiveblue left a comment

Choose a reason for hiding this comment

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

🚀

@guggero guggero merged commit b6da8dd into master Jul 4, 2022
@guggero guggero deleted the sign-psbt branch July 4, 2022 09:50
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.

5 participants