Payment channels: Add KeyParameter arguments to Transaction.calculateSignature

This commit is contained in:
Will Shackleton
2016-04-08 14:34:24 +01:00
committed by Andreas Schildbach
parent 2e09ed8581
commit 56f1db8dd8

View File

@@ -31,6 +31,7 @@ import com.google.common.primitives.Ints;
import com.google.common.primitives.Longs;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spongycastle.crypto.params.KeyParameter;
import javax.annotation.Nullable;
import java.io.*;
@@ -908,6 +909,49 @@ public class Transaction extends ChildMessage {
return new TransactionSignature(key.sign(hash), hashType, anyoneCanPay);
}
/**
* Calculates a signature that is valid for being inserted into the input at the given position. This is simply
* a wrapper around calling {@link Transaction#hashForSignature(int, byte[], org.bitcoinj.core.Transaction.SigHash, boolean)}
* followed by {@link ECKey#sign(Sha256Hash)} and then returning a new {@link TransactionSignature}. The key
* must be usable for signing as-is: if the key is encrypted it must be decrypted first external to this method.
*
* @param inputIndex Which input to calculate the signature for, as an index.
* @param key The private key used to calculate the signature.
* @param aesKey The AES key to use for decryption of the private key. If null then no decryption is required.
* @param redeemScript Byte-exact contents of the scriptPubKey that is being satisified, or the P2SH redeem script.
* @param hashType Signing mode, see the enum for documentation.
* @param anyoneCanPay Signing mode, see the SigHash enum for documentation.
* @return A newly calculated signature object that wraps the r, s and sighash components.
*/
public TransactionSignature calculateSignature(int inputIndex, ECKey key,
@Nullable KeyParameter aesKey,
byte[] redeemScript,
SigHash hashType, boolean anyoneCanPay) {
Sha256Hash hash = hashForSignature(inputIndex, redeemScript, hashType, anyoneCanPay);
return new TransactionSignature(key.sign(hash, aesKey), hashType, anyoneCanPay);
}
/**
* Calculates a signature that is valid for being inserted into the input at the given position. This is simply
* a wrapper around calling {@link Transaction#hashForSignature(int, byte[], org.bitcoinj.core.Transaction.SigHash, boolean)}
* followed by {@link ECKey#sign(Sha256Hash)} and then returning a new {@link TransactionSignature}.
*
* @param inputIndex Which input to calculate the signature for, as an index.
* @param key The private key used to calculate the signature.
* @param aesKey The AES key to use for decryption of the private key. If null then no decryption is required.
* @param redeemScript The scriptPubKey that is being satisified, or the P2SH redeem script.
* @param hashType Signing mode, see the enum for documentation.
* @param anyoneCanPay Signing mode, see the SigHash enum for documentation.
* @return A newly calculated signature object that wraps the r, s and sighash components.
*/
public TransactionSignature calculateSignature(int inputIndex, ECKey key,
@Nullable KeyParameter aesKey,
Script redeemScript,
SigHash hashType, boolean anyoneCanPay) {
Sha256Hash hash = hashForSignature(inputIndex, redeemScript.getProgram(), hashType, anyoneCanPay);
return new TransactionSignature(key.sign(hash, aesKey), hashType, anyoneCanPay);
}
/**
* <p>Calculates a signature hash, that is, a hash of a simplified form of the transaction. How exactly the transaction
* is simplified is specified by the type and anyoneCanPay parameters.</p>