Skip to content

Commit

Permalink
TxValidator: Fix crash on missing vin[0].prevout
Browse files Browse the repository at this point in the history
The `checkFeeAmountBTC` method looks up the `value` field of the
`vin[0].prevout` field without checking whether `vin[0].prevout` exists.
When the field is missing `JsonElement jsonVIn0Value =
jsonVin0.getAsJsonObject("prevout").get("value");` (line 193) throws a
NullPointerException.
  • Loading branch information
alvasw committed Jan 15, 2024
1 parent ba09db4 commit 516d3c8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,13 @@ private FeeValidationStatus checkFeeAmountBTC(String jsonTxt, Coin tradeAmount,
JsonArray jsonVout = getVinAndVout(jsonTxt).second;
JsonObject jsonVin0 = jsonVin.get(0).getAsJsonObject();
JsonObject jsonVout0 = jsonVout.get(0).getAsJsonObject();
JsonElement jsonVIn0Value = jsonVin0.getAsJsonObject("prevout").get("value");

JsonObject jsonVin0PreVout = jsonVin0.getAsJsonObject("prevout");
if (jsonVin0PreVout == null) {
throw new JsonSyntaxException("vin[0].prevout missing");
}

JsonElement jsonVIn0Value = jsonVin0PreVout.get("value");
JsonElement jsonFeeValue = jsonVout0.get("value");
if (jsonVIn0Value == null || jsonFeeValue == null) {
throw new JsonSyntaxException("vin/vout missing data");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,24 @@ void checkFeeAddressBtcNoTooFewVout(int numberOfVouts) throws IOException {
assertThat(txValidator1.getStatus(), is(FeeValidationStatus.NACK_JSON_ERROR));
}

@Test
void checkFeeAmountMissingVinPreVout() throws IOException {
JsonObject json = MakerTxValidatorSanityCheckTests.getValidBtcMakerFeeMempoolJsonResponse();
JsonObject firstInput = json.get("vin").getAsJsonArray().get(0).getAsJsonObject();
firstInput.remove("prevout");

boolean hasPreVout = json.get("vin").getAsJsonArray()
.get(0).getAsJsonObject()
.has("prevout");
assertThat(hasPreVout, is(false));

String jsonContent = new Gson().toJson(json);
TxValidator txValidator1 = txValidator.parseJsonValidateMakerFeeTx(jsonContent,
MakerTxValidatorSanityCheckTests.FEE_RECEIVER_ADDRESSES);

assertThat(txValidator1.getStatus(), is(FeeValidationStatus.NACK_JSON_ERROR));
}

@Test
void responseHasDifferentTxId() throws IOException {
String differentTxId = "abcde971ead7d03619e3a9eeaa771ed5adba14c448839e0299f857f7bb4ec07";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,24 @@ void checkFeeAddressBtcNoTooFewVout(int numberOfVouts) throws IOException {
assertThat(txValidator1.getStatus(), is(FeeValidationStatus.NACK_JSON_ERROR));
}

@Test
void checkFeeAmountMissingVinPreVout() throws IOException {
JsonObject json = MakerTxValidatorSanityCheckTests.getValidBtcMakerFeeMempoolJsonResponse();
JsonObject firstInput = json.get("vin").getAsJsonArray().get(0).getAsJsonObject();
firstInput.remove("prevout");

boolean hasPreVout = json.get("vin").getAsJsonArray()
.get(0).getAsJsonObject()
.has("prevout");
assertThat(hasPreVout, is(false));

String jsonContent = new Gson().toJson(json);
TxValidator txValidator1 = txValidator.parseJsonValidateMakerFeeTx(jsonContent,
MakerTxValidatorSanityCheckTests.FEE_RECEIVER_ADDRESSES);

assertThat(txValidator1.getStatus(), is(FeeValidationStatus.NACK_JSON_ERROR));
}

@Test
void responseHasDifferentTxId() throws IOException {
String differentTxId = "abcde971ead7d03619e3a9eeaa771ed5adba14c448839e0299f857f7bb4ec07";
Expand Down

0 comments on commit 516d3c8

Please sign in to comment.