Script: Deprecate the script data extraction methods for CLTV payment channels.

Create and make sure to only use equivalents in ScriptPattern.
This commit is contained in:
Andreas Schildbach 2018-02-26 13:25:46 +01:00
parent c6bbd947a0
commit 4b129475b3
4 changed files with 39 additions and 27 deletions

View file

@ -257,35 +257,25 @@ public class Script {
throw new ScriptException(ScriptError.SCRIPT_ERR_UNKNOWN_ERROR, "Script not in the standard scriptPubKey form");
}
/**
* Retrieves the sender public key from a LOCKTIMEVERIFY transaction
* @return the sender public key
* @throws ScriptException
*/
@Deprecated
public byte[] getCLTVPaymentChannelSenderPubKey() throws ScriptException {
if (!ScriptPattern.isSentToCltvPaymentChannel(this)) {
if (!ScriptPattern.isSentToCltvPaymentChannel(this))
throw new ScriptException(ScriptError.SCRIPT_ERR_UNKNOWN_ERROR, "Script not a standard CHECKLOCKTIMVERIFY transaction: " + this);
}
return chunks.get(8).data;
return ScriptPattern.extractSenderPubKeyFromCltvPaymentChannel(this);
}
/**
* Retrieves the recipient public key from a LOCKTIMEVERIFY transaction
* @return the recipient public key
* @throws ScriptException
*/
@Deprecated
public byte[] getCLTVPaymentChannelRecipientPubKey() throws ScriptException {
if (!ScriptPattern.isSentToCltvPaymentChannel(this)) {
if (!ScriptPattern.isSentToCltvPaymentChannel(this))
throw new ScriptException(ScriptError.SCRIPT_ERR_UNKNOWN_ERROR, "Script not a standard CHECKLOCKTIMVERIFY transaction: " + this);
}
return chunks.get(1).data;
return ScriptPattern.extractRecipientPubKeyFromCltvPaymentChannel(this);
}
public BigInteger getCLTVPaymentChannelExpiry() {
if (!ScriptPattern.isSentToCltvPaymentChannel(this)) {
@Deprecated
public BigInteger getCLTVPaymentChannelExpiry() throws ScriptException {
if (!ScriptPattern.isSentToCltvPaymentChannel(this))
throw new ScriptException(ScriptError.SCRIPT_ERR_UNKNOWN_ERROR, "Script not a standard CHECKLOCKTIMEVERIFY transaction: " + this);
}
return castToBigInteger(chunks.get(4).data, 5, false);
return ScriptPattern.extractExpiryFromCltvPaymentChannel(this);
}
/**
@ -709,7 +699,7 @@ public class Script {
* @param requireMinimal check if the number is encoded with the minimum possible number of bytes
* @throws ScriptException if the chunk is longer than the specified maximum.
*/
private static BigInteger castToBigInteger(final byte[] chunk, final int maxLength, final boolean requireMinimal) throws ScriptException {
/* package private */ static BigInteger castToBigInteger(final byte[] chunk, final int maxLength, final boolean requireMinimal) throws ScriptException {
if (chunk.length > maxLength)
throw new ScriptException(ScriptError.SCRIPT_ERR_UNKNOWN_ERROR, "Script attempted to use an integer larger than " + maxLength + " bytes");

View file

@ -19,6 +19,7 @@ package org.bitcoinj.script;
import org.bitcoinj.core.Address;
import java.math.BigInteger;
import java.util.List;
import static org.bitcoinj.script.Script.decodeFromOpN;
@ -136,6 +137,27 @@ public class ScriptPattern {
return true;
}
/**
* Retrieves the public key of the sender from a LOCKTIMEVERIFY transaction.
*/
public static byte[] extractSenderPubKeyFromCltvPaymentChannel(Script script) {
return script.chunks.get(8).data;
}
/**
* Retrieves the public key of the recipient from a LOCKTIMEVERIFY transaction.
*/
public static byte[] extractRecipientPubKeyFromCltvPaymentChannel(Script script) {
return script.chunks.get(1).data;
}
/**
* Retrieves the locktime from a LOCKTIMEVERIFY transaction.
*/
public static BigInteger extractExpiryFromCltvPaymentChannel(Script script) {
return Script.castToBigInteger(script.chunks.get(4).data, 5, false);
}
public static boolean isOpReturn(Script script) {
List<ScriptChunk> chunks = script.chunks;
return chunks.size() > 0 && chunks.get(0).equalsOpCode(ScriptOpCodes.OP_RETURN);

View file

@ -4205,12 +4205,12 @@ public class Wallet extends BaseTaggableObject
}
} else if (ScriptPattern.isSentToCltvPaymentChannel(script)) {
// Any script for which we are the recipient or sender counts.
byte[] sender = script.getCLTVPaymentChannelSenderPubKey();
byte[] sender = ScriptPattern.extractSenderPubKeyFromCltvPaymentChannel(script);
ECKey senderKey = findKeyFromPubKey(sender);
if (senderKey != null && (senderKey.isEncrypted() || senderKey.hasPrivKey())) {
return true;
}
byte[] recipient = script.getCLTVPaymentChannelRecipientPubKey();
byte[] recipient = ScriptPattern.extractRecipientPubKeyFromCltvPaymentChannel(script);
ECKey recipientKey = findKeyFromPubKey(sender);
if (recipientKey != null && (recipientKey.isEncrypted() || recipientKey.hasPrivKey())) {
return true;

View file

@ -855,9 +855,9 @@ public class WalletTool {
}
ECKey key1 = wallet.findKeyFromPubKey(
lockTimeVerifyOutput.getScriptPubKey().getCLTVPaymentChannelSenderPubKey());
ScriptPattern.extractSenderPubKeyFromCltvPaymentChannel(lockTimeVerifyOutput.getScriptPubKey()));
ECKey key2 = wallet.findKeyFromPubKey(
lockTimeVerifyOutput.getScriptPubKey().getCLTVPaymentChannelRecipientPubKey());
ScriptPattern.extractRecipientPubKeyFromCltvPaymentChannel(lockTimeVerifyOutput.getScriptPubKey()));
if (key1 == null || key2 == null) {
System.err.println("Don't own private keys for both pubkeys");
return;
@ -945,7 +945,7 @@ public class WalletTool {
return;
}
req.tx.setLockTime(lockTimeVerifyOutput.getScriptPubKey().getCLTVPaymentChannelExpiry().longValue());
req.tx.setLockTime(ScriptPattern.extractExpiryFromCltvPaymentChannel(lockTimeVerifyOutput.getScriptPubKey()).longValue());
if (!value.equals(lockTimeVerifyOutput.getValue())) {
System.err.println("You must spend all the money in the input transaction");
@ -961,7 +961,7 @@ public class WalletTool {
}
ECKey key = wallet.findKeyFromPubKey(
lockTimeVerifyOutput.getScriptPubKey().getCLTVPaymentChannelSenderPubKey());
ScriptPattern.extractSenderPubKeyFromCltvPaymentChannel(lockTimeVerifyOutput.getScriptPubKey()));
if (key == null) {
System.err.println("Don't own private key for pubkey");
return;