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

Add partial support for PSBT Taproot Fields (BIP371) #128

Merged
merged 3 commits into from
Aug 21, 2024
Merged

Conversation

sstone
Copy link
Member

@sstone sstone commented Jun 19, 2024

We only add what is necessary to create, update and sign taproot (BIP86) outputs.

@sstone sstone force-pushed the snapshot/psbt-v2 branch from bf58d9c to a2e1477 Compare June 20, 2024 10:37
@sstone sstone marked this pull request as ready for review June 21, 2024 08:21
@sstone sstone added this to the 0.20.0 milestone Aug 12, 2024
build.gradle.kts Outdated Show resolved Hide resolved
@t-bast
Copy link
Member

t-bast commented Aug 20, 2024

It seems to me that PSBTv2 is something completely different from adding taproot or musig fields to PSBT. Can you clarify exactly what is covered by this PR? As far as I understand, Bitcoin Core hasn't merged support for PSBTv2 yet (which gets rid of the global tx field), so we should stick to PSBTv0 and just add support for BIPs 371 and 373?

@sstone sstone changed the title Add partial support for PSBT v2 Add partial support for PSBT Taproot Fields (BIP371) Aug 20, 2024
Here we only support taproot fields that would allow us to sign BIP86 transactions.
@sstone
Copy link
Member Author

sstone commented Aug 20, 2024

It seems to me that PSBTv2 is something completely different from adding taproot or musig fields to PSBT. Can you clarify exactly what is covered by this PR? As far as I understand, Bitcoin Core hasn't merged support for PSBTv2 yet (which gets rid of the global tx field), so we should stick to PSBTv0 and just add support for BIPs 371 and 373?

Yes I've updated the PR description and commit message, here I'm just adding what we need to support p2tr wallets in eclair.

Script.parse(input.txOut.publicKeyScript)
}.getOrElse {
return Either.Left(UpdateFailure.InvalidWitnessUtxo("failed to parse redeem script"))
val spentOutputs = this.inputs.mapNotNull { it.witnessUtxo ?: it.nonWitnessUtxo?.txOut?.get(this.global.tx.txIn[inputIndex].outPoint.index.toInt()) }
Copy link
Member

@t-bast t-bast Aug 20, 2024

Choose a reason for hiding this comment

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

I don't think this is correct: you are trying to get the list of TxOut that are spent by this transaction, right? But the issue is that you're always using inputIndex instead of using each input's index? We must use a mapIndexed here I believe, and since we need all the txOut, we should fail if one of them is null?

Otherwise we will fail to sign transaction that also include non-witness inputs, which is something that could be controlled by a malicious counterparty providing the PSBT.

This is fixed in #134

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes that was wrong, thanks for the fix!

createOutput(redeemScript, witnessScript, derivationPaths, unknown)
val taprootInternalKey = known.find { it.key[0] == 0x05.toByte() }?.let {
when {
it.key.size() != 1 -> return Either.Left(ParseFailure.InvalidTxInput("taproot internal key entry must have an empty key"))
Copy link
Member

@t-bast t-bast Aug 21, 2024

Choose a reason for hiding this comment

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

The error should be InvalidTxOutput here and below (fixed in #134).

- fix a bug in the index used for previous inputs
- use `InvalidTxOutput` for invalid taproot output fields
- add test vectors from BIP 371
- reformat to match the rest of the file

Note that I updated the "build and sign a BIP86 psbt" test to include a
non-segwit input, this way it fails without the fix to the input index.
@sstone sstone merged commit 7773a27 into master Aug 21, 2024
4 checks passed
@sstone sstone deleted the snapshot/psbt-v2 branch August 21, 2024 12:57
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