diff --git a/core/src/main/java/org/bitcoinj/script/Script.java b/core/src/main/java/org/bitcoinj/script/Script.java index 69b36372..ecdf9a02 100644 --- a/core/src/main/java/org/bitcoinj/script/Script.java +++ b/core/src/main/java/org/bitcoinj/script/Script.java @@ -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"); diff --git a/core/src/main/java/org/bitcoinj/script/ScriptPattern.java b/core/src/main/java/org/bitcoinj/script/ScriptPattern.java index d6afe82f..1703763e 100644 --- a/core/src/main/java/org/bitcoinj/script/ScriptPattern.java +++ b/core/src/main/java/org/bitcoinj/script/ScriptPattern.java @@ -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 chunks = script.chunks; return chunks.size() > 0 && chunks.get(0).equalsOpCode(ScriptOpCodes.OP_RETURN); diff --git a/core/src/main/java/org/bitcoinj/wallet/Wallet.java b/core/src/main/java/org/bitcoinj/wallet/Wallet.java index 43ed36b7..37c4e809 100644 --- a/core/src/main/java/org/bitcoinj/wallet/Wallet.java +++ b/core/src/main/java/org/bitcoinj/wallet/Wallet.java @@ -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; diff --git a/tools/src/main/java/org/bitcoinj/tools/WalletTool.java b/tools/src/main/java/org/bitcoinj/tools/WalletTool.java index 4909a80d..1f31d74e 100644 --- a/tools/src/main/java/org/bitcoinj/tools/WalletTool.java +++ b/tools/src/main/java/org/bitcoinj/tools/WalletTool.java @@ -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;