TransactionOutput: fix NullPointerException in toString()

This commit is contained in:
Andreas Schildbach 2023-04-01 19:39:30 +02:00
parent 8b663363ef
commit 1c04dd94e7
2 changed files with 20 additions and 6 deletions

View File

@ -337,13 +337,15 @@ public class TransactionOutput extends Message {
StringBuilder buf = new StringBuilder("TxOut of ");
buf.append(Coin.valueOf(value).toFriendlyString());
if (ScriptPattern.isP2PKH(script) || ScriptPattern.isP2WPKH(script) || ScriptPattern.isP2TR(script)
|| ScriptPattern.isP2SH(script))
buf.append(" to ").append(script.getToAddress(params.network()));
else if (ScriptPattern.isP2PK(script))
|| ScriptPattern.isP2SH(script)) {
buf.append(" to ").append(script.getScriptType().name());
if (params != null)
buf.append(" ").append(script.getToAddress(params.network()));
} else if (ScriptPattern.isP2PK(script)) {
buf.append(" to pubkey ").append(ByteUtils.formatHex(ScriptPattern.extractKeyFromP2PK(script)));
else if (ScriptPattern.isSentToMultisig(script))
buf.append(" to multisig");
else
} else if (ScriptPattern.isSentToMultisig(script)) {
final StringBuilder append = buf.append(" to multisig");
} else
buf.append(" (unknown type)");
buf.append(" script:").append(script);
return buf.toString();

View File

@ -105,4 +105,16 @@ public class TransactionOutputTest extends TestWithWallet {
BitcoinNetwork.TESTNET));
assertEquals(Coin.valueOf(294), p2wpkh.getMinNonDustValue());
}
@Test
public void toString_() {
TransactionOutput p2pk = new TransactionOutput(null, Coin.COIN, myKey);
p2pk.toString();
TransactionOutput p2pkh = new TransactionOutput(null, Coin.COIN, myKey.toAddress(ScriptType.P2PKH,
BitcoinNetwork.TESTNET));
p2pkh.toString();
TransactionOutput p2wpkh = new TransactionOutput(null, Coin.COIN, myKey.toAddress(ScriptType.P2WPKH,
BitcoinNetwork.TESTNET));
p2wpkh.toString();
}
}