MetacoSA / NBitcoin

Comprehensive Bitcoin library for the .NET framework.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Non-segwit inputs are not serialized in PSBTs

Jossec101 opened this issue · comments

It looks like that after getting the base64 string of a PSBT the non-witness UTXO fields are not serialised hence empty/null after deserialising back to the PSBT object.

Looks like orphanTxOut is not used much

internal TxOut? orphanTxOut = null; // When this input is not segwit, but we don't have the previous tx

If I retrieve a PSBTInput object, named input an do input.GetCoin().TxOutis filled if the PSBT was generated programatically from code (.i.e. PSBT Builder), this is because the orphanTxOut is filled.
image

However, after deserialising back from a base64 PSBT, orphanTxOut is no longer present and therefore I cannot get the Coin of a non-segwit input. This is happening in the picture below.

image

Hope I explained well enough 👯

I managed to make it work by adding the NonWitnessUtxo by hand using Nbxplorer

foreach (var item1Input in result.Item1.Inputs)
          {
              item1Input.NonWitnessUtxo =                (await  _nbXplorerService.GetTransactionAsync(item1Input.PrevOut.Hash, default)).Transaction;

          }

@Jossec101 if you are using NBXplorer, this endpoint https://github.com/dgarage/NBXplorer/blob/master/docs/API.md#create-partially-signed-bitcoin-transaction has alwaysIncludeNonWitnessUTXO.

Else, you are correct, the TransactionBuilder's PSBT doesn't include the NonWitnessUtxo, because it has no knowledge about it. (The Coin abstraction can't have any transaction attached to it)

I guess an improvement to the builder should programatically allow to add the NonWitnessUtxo for serialisation, it serialised to base64 well after adding it manually.

The problem is that it doesn't have this information. There is no way to feed the previous transactions to the builder.