Java Code Examples for org.bitcoinj.crypto.TransactionSignature#encodeToBitcoin()

The following examples show how to use org.bitcoinj.crypto.TransactionSignature#encodeToBitcoin() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Signature.java    From balzac with Apache License 2.0 6 votes vote down vote up
public static Signature computeSignature(
    PrivateKey key,
    ITransactionBuilder txBuilder,
    ECKeyStore keyStore,
    int inputIndex,
    SignatureModifier modifier) {

    Transaction tx = txBuilder.toTransaction(keyStore);

    Input input = txBuilder.getInputs().get(inputIndex);
    int outputIndex = input.getOutIndex();
    Output output = input.getParentTx().getOutputs().get(outputIndex);
    byte[] outputScript = output.getScript().build().getProgram();

    TransactionSignature sig = tx.calculateSignature(inputIndex, ECKey.fromPrivate(key.getBytes()), outputScript,
        modifier.toHashType(), modifier.toAnyoneCanPay());

    return new Signature(sig.encodeToBitcoin(), key.toPublicKey());
}
 
Example 2
Source File: TransactionWitness.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Create a witness that can redeem a pay-to-witness-pubkey-hash output.
 */
public static TransactionWitness createWitness(@Nullable final TransactionSignature signature, final ECKey pubKey) {
    final byte[] sigBytes = signature != null ? signature.encodeToBitcoin() : new byte[]{};
    final byte[] pubKeyBytes = pubKey.getPubKey();
    final TransactionWitness witness = new TransactionWitness(2);
    witness.setPush(0, sigBytes);
    witness.setPush(1, pubKeyBytes);
    return witness;
}
 
Example 3
Source File: PaymentChannelV1ServerState.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
private void signMultisigInput(Transaction tx, Transaction.SigHash hashType,
                               boolean anyoneCanPay, @Nullable KeyParameter userKey) {
    TransactionSignature signature = tx.calculateSignature(0, serverKey, userKey, getContractScript(), hashType, anyoneCanPay);
    byte[] mySig = signature.encodeToBitcoin();
    Script scriptSig = ScriptBuilder.createMultiSigInputScriptBytes(ImmutableList.of(bestValueSignature, mySig));
    tx.getInput(0).setScriptSig(scriptSig);
}
 
Example 4
Source File: PaymentChannelV1ServerState.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Called when the client provides the refund transaction.
 * The refund transaction must have one input from the multisig contract (that we don't have yet) and one output
 * that the client creates to themselves. This object will later be modified when we start getting paid.
 *
 * @param refundTx The refund transaction, this object will be mutated when payment is incremented.
 * @param clientMultiSigPubKey The client's pubkey which is required for the multisig output
 * @return Our signature that makes the refund transaction valid
 * @throws VerificationException If the transaction isnt valid or did not meet the requirements of a refund transaction.
 */
public synchronized byte[] provideRefundTransaction(Transaction refundTx, byte[] clientMultiSigPubKey) throws VerificationException {
    checkNotNull(refundTx);
    checkNotNull(clientMultiSigPubKey);
    stateMachine.checkState(State.WAITING_FOR_REFUND_TRANSACTION);
    log.info("Provided with refund transaction: {}", refundTx);
    // Do a few very basic syntax sanity checks.
    refundTx.verify();
    // Verify that the refund transaction has a single input (that we can fill to sign the multisig output).
    if (refundTx.getInputs().size() != 1)
        throw new VerificationException("Refund transaction does not have exactly one input");
    // Verify that the refund transaction has a time lock on it and a sequence number that does not disable lock time.
    if (refundTx.getInput(0).getSequenceNumber() == TransactionInput.NO_SEQUENCE)
        throw new VerificationException("Refund transaction's input's sequence number disables lock time");
    if (refundTx.getLockTime() < minExpireTime)
        throw new VerificationException("Refund transaction has a lock time too soon");
    // Verify the transaction has one output (we don't care about its contents, its up to the client)
    // Note that because we sign with SIGHASH_NONE|SIGHASH_ANYOENCANPAY the client can later add more outputs and
    // inputs, but we will need only one output later to create the paying transactions
    if (refundTx.getOutputs().size() != 1)
        throw new VerificationException("Refund transaction does not have exactly one output");

    refundTransactionUnlockTimeSecs = refundTx.getLockTime();

    // Sign the refund tx with the scriptPubKey and return the signature. We don't have the spending transaction
    // so do the steps individually.
    clientKey = ECKey.fromPublicOnly(clientMultiSigPubKey);
    Script multisigPubKey = ScriptBuilder.createMultiSigOutputScript(2, ImmutableList.of(clientKey, serverKey));
    // We are really only signing the fact that the transaction has a proper lock time and don't care about anything
    // else, so we sign SIGHASH_NONE and SIGHASH_ANYONECANPAY.
    TransactionSignature sig = refundTx.calculateSignature(0, serverKey, multisigPubKey, Transaction.SigHash.NONE, true);
    log.info("Signed refund transaction.");
    this.clientOutput = refundTx.getOutput(0);
    stateMachine.transition(State.WAITING_FOR_MULTISIG_CONTRACT);
    return sig.encodeToBitcoin();
}
 
Example 5
Source File: PaymentChannelV2ServerState.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
private void signP2SHInput(Transaction tx, Transaction.SigHash hashType,
                           boolean anyoneCanPay, @Nullable KeyParameter userKey) {
    TransactionSignature signature = tx.calculateSignature(0, serverKey, userKey, createP2SHRedeemScript(), hashType, anyoneCanPay);
    byte[] mySig = signature.encodeToBitcoin();
    Script scriptSig = ScriptBuilder.createCLTVPaymentChannelP2SHInput(bestValueSignature, mySig, createP2SHRedeemScript());
    tx.getInput(0).setScriptSig(scriptSig);
}
 
Example 6
Source File: LNEstablishMessageFactoryImpl.java    From thundernetwork with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public LNEstablishCMessage getEstablishMessageC (Transaction anchor, TransactionSignature escapeSignature, TransactionSignature escapeFastSignature) {
    LNEstablishCMessage message = new LNEstablishCMessage(
            escapeSignature.encodeToBitcoin(),
            escapeFastSignature.encodeToBitcoin(),
            anchor.getHash().getBytes());
    return message;
}
 
Example 7
Source File: WalletHelperImpl.java    From thunder with GNU Affero General Public License v3.0 5 votes vote down vote up
private static Transaction signTransaction (Transaction transaction, Wallet wallet) {
    //TODO: Currently only working if we have P2PKH outputs in our wallet
    int j = 0;
    for (int i = 0; i < transaction.getInputs().size(); ++i) {
        TransactionInput input = transaction.getInput(i);
        Optional<TransactionOutput> optional =
                wallet.calculateAllSpendCandidates().stream().filter(out -> input.getOutpoint().equals(out.getOutPointFor())).findAny();
        if (optional.isPresent()) {
            TransactionOutput output = optional.get();
            Address address = output.getAddressFromP2PKHScript(Constants.getNetwork());

            //Only sign P2PKH and only those that we possess the key for..
            if (address != null) {
                ECKey key = wallet.findKeyFromPubHash(address.getHash160());
                if (key != null) {
                    TransactionSignature sig = Tools.getSignature(transaction, i, output, key);
                    byte[] s = sig.encodeToBitcoin();
                    ScriptBuilder builder = new ScriptBuilder();
                    builder.data(s);
                    builder.data(key.getPubKey());
                    transaction.getInput(i).setScriptSig(builder.build());
                }
            }
        }
    }
    return transaction;
}
 
Example 8
Source File: PaymentChannelV1ServerState.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
private void signMultisigInput(Transaction tx, Transaction.SigHash hashType,
                               boolean anyoneCanPay, @Nullable KeyParameter userKey) {
    TransactionSignature signature = tx.calculateSignature(0, serverKey, userKey, getContractScript(), hashType, anyoneCanPay);
    byte[] mySig = signature.encodeToBitcoin();
    Script scriptSig = ScriptBuilder.createMultiSigInputScriptBytes(ImmutableList.of(bestValueSignature, mySig));
    tx.getInput(0).setScriptSig(scriptSig);
}
 
Example 9
Source File: PaymentChannelV1ServerState.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Called when the client provides the refund transaction.
 * The refund transaction must have one input from the multisig contract (that we don't have yet) and one output
 * that the client creates to themselves. This object will later be modified when we start getting paid.
 *
 * @param refundTx The refund transaction, this object will be mutated when payment is incremented.
 * @param clientMultiSigPubKey The client's pubkey which is required for the multisig output
 * @return Our signature that makes the refund transaction valid
 * @throws VerificationException If the transaction isnt valid or did not meet the requirements of a refund transaction.
 */
public synchronized byte[] provideRefundTransaction(Transaction refundTx, byte[] clientMultiSigPubKey) throws VerificationException {
    checkNotNull(refundTx);
    checkNotNull(clientMultiSigPubKey);
    stateMachine.checkState(State.WAITING_FOR_REFUND_TRANSACTION);
    log.info("Provided with refund transaction: {}", refundTx);
    // Do a few very basic syntax sanity checks.
    refundTx.verify();
    // Verify that the refund transaction has a single input (that we can fill to sign the multisig output).
    if (refundTx.getInputs().size() != 1)
        throw new VerificationException("Refund transaction does not have exactly one input");
    // Verify that the refund transaction has a time lock on it and a sequence number that does not disable lock time.
    if (refundTx.getInput(0).getSequenceNumber() == TransactionInput.NO_SEQUENCE)
        throw new VerificationException("Refund transaction's input's sequence number disables lock time");
    if (refundTx.getLockTime() < minExpireTime)
        throw new VerificationException("Refund transaction has a lock time too soon");
    // Verify the transaction has one output (we don't care about its contents, its up to the client)
    // Note that because we sign with SIGHASH_NONE|SIGHASH_ANYOENCANPAY the client can later add more outputs and
    // inputs, but we will need only one output later to create the paying transactions
    if (refundTx.getOutputs().size() != 1)
        throw new VerificationException("Refund transaction does not have exactly one output");

    refundTransactionUnlockTimeSecs = refundTx.getLockTime();

    // Sign the refund tx with the scriptPubKey and return the signature. We don't have the spending transaction
    // so do the steps individually.
    clientKey = ECKey.fromPublicOnly(clientMultiSigPubKey);
    Script multisigPubKey = ScriptBuilder.createMultiSigOutputScript(2, ImmutableList.of(clientKey, serverKey));
    // We are really only signing the fact that the transaction has a proper lock time and don't care about anything
    // else, so we sign SIGHASH_NONE and SIGHASH_ANYONECANPAY.
    TransactionSignature sig = refundTx.calculateSignature(0, serverKey, multisigPubKey, Transaction.SigHash.NONE, true);
    log.info("Signed refund transaction.");
    this.clientOutput = refundTx.getOutput(0);
    stateMachine.transition(State.WAITING_FOR_MULTISIG_CONTRACT);
    return sig.encodeToBitcoin();
}
 
Example 10
Source File: PaymentChannelV2ServerState.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
private void signP2SHInput(Transaction tx, Transaction.SigHash hashType,
                           boolean anyoneCanPay, @Nullable KeyParameter userKey) {
    TransactionSignature signature = tx.calculateSignature(0, serverKey, userKey, createP2SHRedeemScript(), hashType, anyoneCanPay);
    byte[] mySig = signature.encodeToBitcoin();
    Script scriptSig = ScriptBuilder.createCLTVPaymentChannelP2SHInput(bestValueSignature, mySig, createP2SHRedeemScript());
    tx.getInput(0).setScriptSig(scriptSig);
}
 
Example 11
Source File: PaymentChannelV1ServerState.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
private void signMultisigInput(Transaction tx, Transaction.SigHash hashType,
                               boolean anyoneCanPay, @Nullable KeyParameter userKey) {
    TransactionSignature signature = tx.calculateSignature(0, serverKey, userKey, getContractScript(), hashType, anyoneCanPay);
    byte[] mySig = signature.encodeToBitcoin();
    Script scriptSig = ScriptBuilder.createMultiSigInputScriptBytes(ImmutableList.of(bestValueSignature, mySig));
    tx.getInput(0).setScriptSig(scriptSig);
}
 
Example 12
Source File: PaymentChannelV1ServerState.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Called when the client provides the refund transaction.
 * The refund transaction must have one input from the multisig contract (that we don't have yet) and one output
 * that the client creates to themselves. This object will later be modified when we start getting paid.
 *
 * @param refundTx             The refund transaction, this object will be mutated when payment is incremented.
 * @param clientMultiSigPubKey The client's pubkey which is required for the multisig output
 * @return Our signature that makes the refund transaction valid
 * @throws VerificationException If the transaction isnt valid or did not meet the requirements of a refund transaction.
 */
public synchronized byte[] provideRefundTransaction(Transaction refundTx, byte[] clientMultiSigPubKey) throws VerificationException {
    checkNotNull(refundTx);
    checkNotNull(clientMultiSigPubKey);
    stateMachine.checkState(State.WAITING_FOR_REFUND_TRANSACTION);
    log.info("Provided with refund transaction: {}", refundTx);
    // Do a few very basic syntax sanity checks.
    refundTx.verify();
    // Verify that the refund transaction has a single input (that we can fill to sign the multisig output).
    if (refundTx.getInputs().size() != 1)
        throw new VerificationException("Refund transaction does not have exactly one input");
    // Verify that the refund transaction has a time lock on it and a sequence number that does not disable lock time.
    if (refundTx.getInput(0).getSequenceNumber() == TransactionInput.NO_SEQUENCE)
        throw new VerificationException("Refund transaction's input's sequence number disables lock time");
    if (refundTx.getLockTime() < minExpireTime)
        throw new VerificationException("Refund transaction has a lock time too soon");
    // Verify the transaction has one output (we don't care about its contents, its up to the client)
    // Note that because we sign with SIGHASH_NONE|SIGHASH_ANYOENCANPAY the client can later add more outputs and
    // inputs, but we will need only one output later to create the paying transactions
    if (refundTx.getOutputs().size() != 1)
        throw new VerificationException("Refund transaction does not have exactly one output");

    refundTransactionUnlockTimeSecs = refundTx.getLockTime();

    // Sign the refund tx with the scriptPubKey and return the signature. We don't have the spending transaction
    // so do the steps individually.
    clientKey = ECKey.fromPublicOnly(clientMultiSigPubKey);
    Script multisigPubKey = ScriptBuilder.createMultiSigOutputScript(2, ImmutableList.of(clientKey, serverKey));
    // We are really only signing the fact that the transaction has a proper lock time and don't care about anything
    // else, so we sign SIGHASH_NONE and SIGHASH_ANYONECANPAY.
    TransactionSignature sig = refundTx.calculateSignature(0, serverKey, multisigPubKey, Transaction.SigHash.NONE, true);
    log.info("Signed refund transaction.");
    this.clientOutput = refundTx.getOutput(0);
    stateMachine.transition(State.WAITING_FOR_MULTISIG_CONTRACT);
    return sig.encodeToBitcoin();
}
 
Example 13
Source File: PaymentChannelV2ServerState.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
private void signP2SHInput(Transaction tx, Transaction.SigHash hashType,
                           boolean anyoneCanPay, @Nullable KeyParameter userKey) {
    TransactionSignature signature = tx.calculateSignature(0, serverKey, userKey, createP2SHRedeemScript(), hashType, anyoneCanPay);
    byte[] mySig = signature.encodeToBitcoin();
    Script scriptSig = ScriptBuilder.createCLTVPaymentChannelP2SHInput(bestValueSignature, mySig, createP2SHRedeemScript());
    tx.getInput(0).setScriptSig(scriptSig);
}
 
Example 14
Source File: ScriptBuilder.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Creates a scriptSig that can redeem a P2PKH output.
 * If given signature is null, incomplete scriptSig will be created with OP_0 instead of signature
 */
public static Script createInputScript(@Nullable TransactionSignature signature, ECKey pubKey) {
    byte[] pubkeyBytes = pubKey.getPubKey();
    byte[] sigBytes = signature != null ? signature.encodeToBitcoin() : new byte[]{};
    return new ScriptBuilder().data(sigBytes).data(pubkeyBytes).build();
}
 
Example 15
Source File: ScriptBuilder.java    From green_android with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Creates a scriptSig that can redeem a pay-to-pubkey output.
 * If given signature is null, incomplete scriptSig will be created with OP_0 instead of signature
 */
public static Script createInputScript(@Nullable TransactionSignature signature) {
    byte[] sigBytes = signature != null ? signature.encodeToBitcoin() : new byte[]{};
    return new ScriptBuilder().data(sigBytes).build();
}
 
Example 16
Source File: ScriptBuilder.java    From green_android with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Creates a scriptSig that can redeem a pay-to-address output.
 * If given signature is null, incomplete scriptSig will be created with OP_0 instead of signature
 */
public static Script createInputScript(@Nullable TransactionSignature signature, ECKey pubKey) {
    byte[] pubkeyBytes = pubKey.getPubKey();
    byte[] sigBytes = signature != null ? signature.encodeToBitcoin() : new byte[]{};
    return new ScriptBuilder().data(sigBytes).data(pubkeyBytes).build();
}
 
Example 17
Source File: LNEstablishBMessage.java    From thunder with GNU Affero General Public License v3.0 4 votes vote down vote up
public LNEstablishBMessage (TransactionSignature signature) {
    this.channelSignature = signature.encodeToBitcoin();
}
 
Example 18
Source File: ISigningWallet.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
public static byte[] getTxSignature(final ECKey.ECDSASignature sig) {
    final TransactionSignature txSig = new TransactionSignature(sig, Transaction.SigHash.ALL, false);
    return txSig.encodeToBitcoin();
}
 
Example 19
Source File: ScriptBuilder.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Creates a scriptSig that can redeem a pay-to-pubkey output.
 * If given signature is null, incomplete scriptSig will be created with OP_0 instead of signature
 */
public static Script createInputScript(@Nullable TransactionSignature signature) {
    byte[] sigBytes = signature != null ? signature.encodeToBitcoin() : new byte[]{};
    return new ScriptBuilder().data(sigBytes).build();
}
 
Example 20
Source File: ScriptBuilder.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Creates a scriptSig that can redeem a P2PK output.
 * If given signature is null, incomplete scriptSig will be created with OP_0 instead of signature
 */
public static Script createInputScript(@Nullable TransactionSignature signature) {
    byte[] sigBytes = signature != null ? signature.encodeToBitcoin() : new byte[]{};
    return new ScriptBuilder().data(sigBytes).build();
}