bitcoinjs / bitcoinjs-lib

A javascript Bitcoin library for node.js and browsers.
MIT License
5.71k stars 2.11k forks source link

sighashType: bitcoin.Transaction.SIGHASH_DEFAULT return error when `tapLeafScript` is not present #2157

Closed jrwbabylonlab closed 2 months ago

jrwbabylonlab commented 2 months ago

Some what related to issue https://github.com/bitcoinjs/bitcoinjs-lib/issues/2046 but not entirely.

The bug we are facing is that we can not set the sighashType: bitcoin.Transaction.SIGHASH_DEFAULT when tapLeafScript is not present. It will throw the error of Cannot add duplicate data to input

Digger further, the error seems to come from https://github.com/bitcoinjs/bip174/blob/master/src/lib/converter/input/sighashType.js#L29-L31

In our case, the currentData is

 {
      unknownKeyVals: [],
      witnessUtxo: {
        script: <Buffer 00 14 59 93 0a fa a8 6b 8d 8c 65 81 d3 55 d5 1f ee f0 e8 d5 b6 c8>,
        value: 33130211
      }
    }

whereas the newData is 0

Due to !!0 is false in js, this canAdd method is false as well. hence throw at the bitcoinjs method of

if (!canAdd(mainData, data)) {
            throw new Error(`Can not add duplicate data to ${typeName}`);
}
junderw commented 2 months ago

newData should be { sighashType: 0 }

junderw commented 2 months ago

If this is a miscommunication of the problem, then it might be easier if you can write a small example showing the issue with actual code.

jrwbabylonlab commented 2 months ago

Hi @junderw thanks for getting back to me on this. Please check the example here: https://github.com/jrwbabylonlab/bitcoinjs-example

Let me know if further clarification is needed

jrwbabylonlab commented 2 months ago

hi @junderw or @jasonandjay could you please double check the example scripts i provided above when u have time? Not sure how to re-open this issue as there is no such option.

junderw commented 2 months ago

I'm still not sure what the issue is, but it seems like in your OP you found the issue.

Please create a PR on the bip174 repository and get it to pass all the tests. Also add a test if you can that will fail with the current code but is fixed with your new code.

I'll look it over and merge it if the tests pass and the change is small.