mirror of
https://github.com/Qortal/altcoinj.git
synced 2025-07-31 20:11:23 +00:00
TransactionOutput: Deprecate getAddressFromP2PKHScript() and getAddressFromP2SH() methods.
This commit is contained in:
@@ -119,40 +119,20 @@ public class TransactionOutput extends ChildMessage {
|
||||
return scriptPubKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>If the output script pays to an address as in <a href="https://bitcoin.org/en/developer-guide#term-p2pkh">
|
||||
* P2PKH</a>, return the address of the receiver, i.e., a base58 encoded hash of the public key in the script. </p>
|
||||
*
|
||||
* @param networkParameters needed to specify an address
|
||||
* @return null, if the output script is not the form <i>OP_DUP OP_HASH160 <PubkeyHash> OP_EQUALVERIFY OP_CHECKSIG</i>,
|
||||
* i.e., not P2PKH
|
||||
* @return an address made out of the public key hash
|
||||
*/
|
||||
@Nullable
|
||||
public LegacyAddress getAddressFromP2PKHScript(NetworkParameters networkParameters) throws ScriptException{
|
||||
@Deprecated
|
||||
public LegacyAddress getAddressFromP2PKHScript(NetworkParameters params) throws ScriptException {
|
||||
if (ScriptPattern.isPayToPubKeyHash(getScriptPubKey()))
|
||||
return (LegacyAddress) getScriptPubKey().getToAddress(networkParameters);
|
||||
|
||||
return LegacyAddress.fromPubKeyHash(params,
|
||||
ScriptPattern.extractHashFromPayToPubKeyHash(getScriptPubKey()));
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>If the output script pays to a redeem script, return the address of the redeem script as described by,
|
||||
* i.e., a base58 encoding of [one-byte version][20-byte hash][4-byte checksum], where the 20-byte hash refers to
|
||||
* the redeem script.</p>
|
||||
*
|
||||
* <p>P2SH is described by <a href="https://github.com/bitcoin/bips/blob/master/bip-0016.mediawiki">BIP 16</a> and
|
||||
* <a href="https://bitcoin.org/en/developer-guide#p2sh-scripts">documented in the Bitcoin Developer Guide</a>.</p>
|
||||
*
|
||||
* @param networkParameters needed to specify an address
|
||||
* @return null if the output script does not pay to a script hash
|
||||
* @return an address that belongs to the redeem script
|
||||
*/
|
||||
@Nullable
|
||||
public LegacyAddress getAddressFromP2SH(NetworkParameters networkParameters) throws ScriptException{
|
||||
@Deprecated
|
||||
public LegacyAddress getAddressFromP2SH(NetworkParameters params) throws ScriptException {
|
||||
if (ScriptPattern.isPayToScriptHash(getScriptPubKey()))
|
||||
return (LegacyAddress) getScriptPubKey().getToAddress(networkParameters);
|
||||
|
||||
return LegacyAddress.fromP2SHHash(params, ScriptPattern.extractHashFromPayToScriptHash(getScriptPubKey()));
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@@ -65,9 +65,14 @@ public class ScriptPattern {
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Whether or not this is a scriptPubKey representing a pay-to-script-hash output. In such outputs, the logic that
|
||||
* <p>
|
||||
* Whether or not this is a scriptPubKey representing a pay-to-script-hash output. In such outputs, the logic that
|
||||
* controls reclamation is not actually in the output at all. Instead there's just a hash, and it's up to the
|
||||
* spending input to provide a program matching that hash.
|
||||
* </p>
|
||||
* <p>
|
||||
* P2SH is described by <a href="https://github.com/bitcoin/bips/blob/master/bip-0016.mediawiki">BIP16</a>.
|
||||
* </p>
|
||||
*/
|
||||
public static boolean isPayToScriptHash(Script script) {
|
||||
List<ScriptChunk> chunks = script.chunks;
|
||||
|
@@ -20,6 +20,7 @@ import com.google.common.collect.ImmutableList;
|
||||
import org.bitcoinj.params.MainNetParams;
|
||||
import org.bitcoinj.script.Script;
|
||||
import org.bitcoinj.script.ScriptBuilder;
|
||||
import org.bitcoinj.script.ScriptPattern;
|
||||
import org.bitcoinj.testing.TestWithWallet;
|
||||
import org.bitcoinj.wallet.SendRequest;
|
||||
import org.hamcrest.CoreMatchers;
|
||||
@@ -71,15 +72,16 @@ public class TransactionOutputTest extends TestWithWallet {
|
||||
Script script = ScriptBuilder.createOutputScript(P2SHAddress);
|
||||
Transaction tx = new Transaction(MAINNET);
|
||||
tx.addOutput(Coin.COIN, script);
|
||||
assertEquals(P2SHAddressString, tx.getOutput(0).getAddressFromP2SH(MAINNET).toString());
|
||||
assertEquals(P2SHAddressString, tx.getOutput(0).getScriptPubKey().getToAddress(MAINNET).toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAddressTests() throws Exception {
|
||||
Transaction tx = new Transaction(MAINNET);
|
||||
tx.addOutput(Coin.CENT, ScriptBuilder.createOpReturnScript("hello world!".getBytes()));
|
||||
assertNull(tx.getOutput(0).getAddressFromP2SH(UNITTEST));
|
||||
assertNull(tx.getOutput(0).getAddressFromP2PKHScript(UNITTEST));
|
||||
assertTrue(ScriptPattern.isOpReturn(tx.getOutput(0).getScriptPubKey()));
|
||||
assertFalse(ScriptPattern.isPayToPubKey(tx.getOutput(0).getScriptPubKey()));
|
||||
assertFalse(ScriptPattern.isPayToPubKeyHash(tx.getOutput(0).getScriptPubKey()));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@@ -19,6 +19,7 @@ package org.bitcoinj.wallet;
|
||||
|
||||
import org.bitcoinj.core.listeners.TransactionConfidenceEventListener;
|
||||
import org.bitcoinj.core.AbstractBlockChain;
|
||||
import org.bitcoinj.core.Address;
|
||||
import org.bitcoinj.core.LegacyAddress;
|
||||
import org.bitcoinj.core.Block;
|
||||
import org.bitcoinj.core.BlockChain;
|
||||
@@ -3035,7 +3036,7 @@ public class WalletTest extends TestWithWallet {
|
||||
wallet.setKeyRotationTime(goodKey.getCreationTimeSeconds());
|
||||
List<Transaction> txns = wallet.doMaintenance(null, false).get();
|
||||
assertEquals(1, txns.size());
|
||||
LegacyAddress output = txns.get(0).getOutput(0).getAddressFromP2PKHScript(UNITTEST);
|
||||
Address output = txns.get(0).getOutput(0).getScriptPubKey().getToAddress(UNITTEST);
|
||||
ECKey usedKey = wallet.findKeyFromPubHash(output.getHash());
|
||||
assertEquals(goodKey.getCreationTimeSeconds(), usedKey.getCreationTimeSeconds());
|
||||
assertEquals(goodKey.getCreationTimeSeconds(), wallet.freshReceiveKey().getCreationTimeSeconds());
|
||||
|
Reference in New Issue
Block a user