tcharding / rust-psbt

Partially Signed Bitcoin Transactions
Creative Commons Zero v1.0 Universal
13 stars 6 forks source link

Convert the inner PSBT v2 to a PSBT v0 (lossy) #31

Open DanGould opened 3 weeks ago

DanGould commented 3 weeks ago

It'd be nice to be able to convert between PSBTv2 and PSBTv0, but this code is commented out. @tcharding, what hurdles did you face when attempting to implement this function in the first place? Looks like it wasn't as straightforward as once imagined?

https://github.com/tcharding/rust-psbt/blob/c32f7547d590c06d93305a51070220fc60142b56/src/v2/mod.rs#L407

tcharding commented 3 weeks ago

I took 2 minutes looking and I cannot remember what hurdle I hit. I want to say that v0 doesn't map to v2, but that would be just from squinting my eyes hard and trying to remember. FTR I don't normally leave commented code lying around so my guess is I wrote that stuff then hit a problem trying to implement into_v0 on the map types and left the commented code to remind me what was broken, which it hasn't so fail :) I'd have to go back and try to re-implement it to work it out. How bad do you want to know, I can put half an hour into it if you want me to. Holla at me.

oooo, I just wrote that then looked at the issue title again, I bet it was because its lossy that I didn't do it. Again just guessing what Tobin six months ago did.

DanGould commented 3 weeks ago

It'd definitely be useful if we could use PSBTv2 internally in rust-payjoin and still serialize (the bulk of) PSBTv2 data to PSBTv0 format. Doing so would let us take advantage of PSBTv2's maps for internal mutation and still serialize to payjoin v1 messages which require for PSBTv0. Additionally, we could use PSBTv2 in the Async Payjoin (payjoin v2) protocol spec which helps simplilfy implementation but is not strictly necessary, it's just a good way to advance PSBTv2 adoption for easier multi-party transaction contribution.

To be explicit for posterity: the conversion from PSBTv2 to PSBTv0 is lossy because PSBTv2 has new fields like the modification flags

In order to support payjoin V2, we'd also to be able to take a PSBTv0 and serialize it into v2 for internal mutation after which it would be converted back into PSBTv0

tcharding commented 3 weeks ago

If the conversion is lossy what do you propose we do about the lost information?

DanGould commented 3 weeks ago

I think dropping that info and documenting it is OK. That's what the PSBTv2 BIP 370 recommends.

tcharding commented 3 weeks ago

Ah nice, sounds reasonable then, thanks.