mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-07-31 20:11:23 +00:00
Transaction: Add 'to addresses' to outputs in toString().
This commit is contained in:
@@ -20,6 +20,7 @@ package org.bitcoinj.core;
|
|||||||
import org.bitcoinj.core.TransactionConfidence.ConfidenceType;
|
import org.bitcoinj.core.TransactionConfidence.ConfidenceType;
|
||||||
import org.bitcoinj.crypto.TransactionSignature;
|
import org.bitcoinj.crypto.TransactionSignature;
|
||||||
import org.bitcoinj.script.Script;
|
import org.bitcoinj.script.Script;
|
||||||
|
import org.bitcoinj.script.Script.ScriptType;
|
||||||
import org.bitcoinj.script.ScriptBuilder;
|
import org.bitcoinj.script.ScriptBuilder;
|
||||||
import org.bitcoinj.script.ScriptError;
|
import org.bitcoinj.script.ScriptError;
|
||||||
import org.bitcoinj.script.ScriptException;
|
import org.bitcoinj.script.ScriptException;
|
||||||
@@ -733,8 +734,8 @@ public class Transaction extends ChildMessage {
|
|||||||
s.append(" ");
|
s.append(" ");
|
||||||
s.append("out ");
|
s.append("out ");
|
||||||
try {
|
try {
|
||||||
String scriptPubKeyStr = out.getScriptPubKey().toString();
|
Script scriptPubKey = out.getScriptPubKey();
|
||||||
s.append(!Strings.isNullOrEmpty(scriptPubKeyStr) ? scriptPubKeyStr : "<no scriptPubKey>");
|
s.append(scriptPubKey.getChunks().size() > 0 ? scriptPubKey.toString() : "<no scriptPubKey>");
|
||||||
s.append(" ");
|
s.append(" ");
|
||||||
s.append(out.getValue().toFriendlyString());
|
s.append(out.getValue().toFriendlyString());
|
||||||
if (!out.isAvailableForSpending()) {
|
if (!out.isAvailableForSpending()) {
|
||||||
@@ -745,6 +746,10 @@ public class Transaction extends ChildMessage {
|
|||||||
s.append(" by ");
|
s.append(" by ");
|
||||||
s.append(spentBy.getParentTransaction().getHashAsString());
|
s.append(spentBy.getParentTransaction().getHashAsString());
|
||||||
}
|
}
|
||||||
|
s.append('\n');
|
||||||
|
ScriptType scriptType = scriptPubKey.getScriptType();
|
||||||
|
if (scriptType != null)
|
||||||
|
s.append(" " + scriptType + " addr:" + scriptPubKey.getToAddress(params));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
s.append("[exception: ").append(e.getMessage()).append("]");
|
s.append("[exception: ").append(e.getMessage()).append("]");
|
||||||
}
|
}
|
||||||
|
@@ -1614,6 +1614,10 @@ public class Script {
|
|||||||
return ScriptType.P2PK;
|
return ScriptType.P2PK;
|
||||||
if (ScriptPattern.isPayToScriptHash(this))
|
if (ScriptPattern.isPayToScriptHash(this))
|
||||||
return ScriptType.P2SH;
|
return ScriptType.P2SH;
|
||||||
|
if (ScriptPattern.isPayToWitnessPubKeyHash(this))
|
||||||
|
return ScriptType.P2WPKH;
|
||||||
|
if (ScriptPattern.isPayToWitnessScriptHash(this))
|
||||||
|
return ScriptType.P2WSH;
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -156,6 +156,34 @@ public class ScriptPattern {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if this script is of the form OP_0 <hash> and hash is 20 bytes long. This can only be a P2WPKH
|
||||||
|
* scriptPubKey. This script type was introduced with segwit.
|
||||||
|
*/
|
||||||
|
public static boolean isPayToWitnessPubKeyHash(Script script) {
|
||||||
|
if (!isPayToWitnessHash(script))
|
||||||
|
return false;
|
||||||
|
List<ScriptChunk> chunks = script.chunks;
|
||||||
|
if (!chunks.get(0).equalsOpCode(OP_0))
|
||||||
|
return false;
|
||||||
|
byte[] chunk1data = chunks.get(1).data;
|
||||||
|
return chunk1data != null && chunk1data.length == SegwitAddress.WITNESS_PROGRAM_LENGTH_PKH;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if this script is of the form OP_0 <hash> and hash is 32 bytes long. This can only be a P2WSH
|
||||||
|
* scriptPubKey. This script type was introduced with segwit.
|
||||||
|
*/
|
||||||
|
public static boolean isPayToWitnessScriptHash(Script script) {
|
||||||
|
if (!isPayToWitnessHash(script))
|
||||||
|
return false;
|
||||||
|
List<ScriptChunk> chunks = script.chunks;
|
||||||
|
if (!chunks.get(0).equalsOpCode(OP_0))
|
||||||
|
return false;
|
||||||
|
byte[] chunk1data = chunks.get(1).data;
|
||||||
|
return chunk1data != null && chunk1data.length == SegwitAddress.WITNESS_PROGRAM_LENGTH_SH;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extract the pubkey hash from a P2WPKH or the script hash from a P2WSH scriptPubKey. It's important that the
|
* Extract the pubkey hash from a P2WPKH or the script hash from a P2WSH scriptPubKey. It's important that the
|
||||||
* script is in the correct form, so you will want to guard calls to this method with
|
* script is in the correct form, so you will want to guard calls to this method with
|
||||||
|
@@ -47,10 +47,10 @@ public class ScriptPatternTest {
|
|||||||
assertTrue(ScriptPattern.isPayToPubKey(
|
assertTrue(ScriptPattern.isPayToPubKey(
|
||||||
ScriptBuilder.createOutputScript(keys.get(0))
|
ScriptBuilder.createOutputScript(keys.get(0))
|
||||||
));
|
));
|
||||||
assertTrue(ScriptPattern.isPayToWitnessHash(
|
assertTrue(ScriptPattern.isPayToWitnessPubKeyHash(
|
||||||
ScriptBuilder.createOutputScript(SegwitAddress.fromHash(MAINNET, keys.get(0).getPubKeyHash()))
|
ScriptBuilder.createOutputScript(SegwitAddress.fromHash(MAINNET, keys.get(0).getPubKeyHash()))
|
||||||
));
|
));
|
||||||
assertTrue(ScriptPattern.isPayToWitnessHash(
|
assertTrue(ScriptPattern.isPayToWitnessScriptHash(
|
||||||
ScriptBuilder.createOutputScript(SegwitAddress.fromHash(MAINNET, Sha256Hash.hash(new byte[0])))
|
ScriptBuilder.createOutputScript(SegwitAddress.fromHash(MAINNET, Sha256Hash.hash(new byte[0])))
|
||||||
));
|
));
|
||||||
assertTrue(ScriptPattern.isSentToMultisig(
|
assertTrue(ScriptPattern.isSentToMultisig(
|
||||||
|
Reference in New Issue
Block a user