bitcoinj icon indicating copy to clipboard operation
bitcoinj copied to clipboard

NullPointerException accessing TransactionInput.Coin.getValue when using ScriptType.P2WPKH

Open mathias21 opened this issue 3 years ago • 10 comments

java.lang.NullPointerException: Attempt to invoke virtual method 'long org.bitcoinj.core.Coin.getValue()' on a null object reference at org.bitcoinj.core.Transaction.hashForWitnessSignature(Transaction.java:1448) at org.bitcoinj.core.Transaction.hashForWitnessSignature(Transaction.java:1362) at org.bitcoinj.core.Transaction.calculateWitnessSignature(Transaction.java:1318) at org.bitcoinj.core.Transaction.calculateWitnessSignature(Transaction.java:1329) at org.bitcoinj.core.Transaction.addSignedInput(Transaction.java:1014) at org.bitcoinj.core.Transaction.addSignedInput(Transaction.java:1029) at org.bitcoinj.core.Transaction.addSignedInput(Transaction.java:1037)

It seems that Coin field never gets initialised when using P2WPKH but being used when calculating hash for witness signature.

mathias21 avatar Oct 03 '22 11:10 mathias21

It seems that his has been fixed in master branch. In order to have this solved, you need to pull master and either publish to local maven or generate the JAR file and add it to your project. The version that has the fix in master right now is 0.17-SNAPSHOT. Keeping this issue open, as we don't have this version released yet.

mathias21 avatar Oct 04 '22 16:10 mathias21

Another thing to mention, if you find yourself with an error like the following: java.lang.NoSuchMethodError: No virtual method addObject(Lorg/bouncycastle/asn1/ASN1Primitive;)

Check that you have the same dependency version for org.bouncycastle:bcprov-jdk15to18, with at least version 1.71. It seems that from 1.69 -> 1.71, some methods are missing.

mathias21 avatar Oct 04 '22 20:10 mathias21

Hello @mathias21 do you think you would have an idea as to why this is happening?: https://github.com/bitcoinj/bitcoinj/issues/2647

AndroDevcd avatar Oct 07 '22 03:10 AndroDevcd

The fix is implemented here: 1959dab5e4b4f1851f080dfb8d2c1e6db68d8ad4 Previously, when using Transaction.addSignedInput method (the big one), when instantiating TransactionInput, it is always passing null as Coin value parameter, so this was making it crash when using Segwit. What is still missing is the 0.17 release including all these fixes (at least in jitpack or Github page)

mathias21 avatar Oct 07 '22 08:10 mathias21

Thanks for locating the commit that fixes this for you. We could probably backport this to 0.16, it seems there is low risk of regression.

@msgilligan What do you think about this idea?

schildbach avatar Oct 07 '22 09:10 schildbach

Regarding your question about 0.17, let's discuss that on our Matrix room.

schildbach avatar Oct 07 '22 09:10 schildbach

@msgilligan What do you think about this idea?

Sorry for not responding sooner.

I just made a quick review of the code in https://github.com/bitcoinj/bitcoinj/commit/1959dab5e4b4f1851f080dfb8d2c1e6db68d8ad4 and I think it is reasonable to apply this commit to the 0.16 branch. I don't think there are any dependencies on previous commits on master, but we should do some further review and testing. (My memory of all the details is a little hazy 9 months later)

@mathias21 Was there anything else you had to do besides apply https://github.com/bitcoinj/bitcoinj/commit/1959dab5e4b4f1851f080dfb8d2c1e6db68d8ad4 to fix your issue?

msgilligan avatar Nov 11 '22 19:11 msgilligan

Hi @msgilligan

What I did was basically pull 0.17-SNAPSHOT and make the jar file. I've found some problems after the update when executing this in our app because we are using org.bouncycastle:bcprov-jdk15to18 dependency as well, so we've had to bump the dependency version to at least 1.71. Not sure if this will impact you, but definitely worth it to mention in your release notes to avoid someone else investigating where the error I've mentioned came from: https://github.com/bitcoinj/bitcoinj/issues/2646#issuecomment-1267546438

Let me know if you have more doubts. Happy to help!

mathias21 avatar Nov 11 '22 21:11 mathias21

I've backported 1959dab5e4b4f1851f080dfb8d2c1e6db68d8ad4 to 0.16.2-SNAPSHOT as fcb1b417fcd1edf729556c531c1fb1c48934f5fb. I decided to remove the method deprecations as this might lead us to inadvertently remove those methods in 0.17 already.

schildbach avatar Nov 20 '22 13:11 schildbach

Now that 0.16.2 is released I think we can close this. Right @schildbach ?

msgilligan avatar Apr 06 '24 21:04 msgilligan