Transaction: do a better job at showing invalid scriptSigs in toString()

Previously, it was just showing an exception message. This is improved
as follows:

- if an input scriptSig cannot be parsed, the raw bytes are shown instead
- also, the input value is shown if available
- for the coinbase input, which may be arbitrary bytes, the exception message
  is suppressed
This commit is contained in:
Andreas Schildbach 2025-03-09 11:22:51 +01:00
parent 2e297169b1
commit 9b28fdbfa0
2 changed files with 10 additions and 2 deletions

View file

@ -810,7 +810,15 @@ public class Transaction extends BaseMessage {
s.append('\n');
}
} catch (Exception e) {
s.append("[exception: ").append(e.getMessage()).append("]\n");
s.append(ByteUtils.formatHex(getInput(0).getScriptBytes()));
if (!isCoinBase()) {
final Coin value = in.getValue();
if (value != null)
s.append(" ").append(value.toFriendlyString());
s.append('\n');
s.append(indent).append(" exception: ").append(e.getMessage());
}
s.append('\n');
}
}
} else {

View file

@ -515,7 +515,7 @@ public class TransactionTest {
};
tx.addInput(ti);
assertTrue(tx.toString().contains("[exception: "));
assertTrue(tx.toString().contains("exception: "));
}
@Test