How to sign raw transaction given a private key and SHA hash (in java) I have followed the instructions here to build my own bitcoin transaction.

Redeeming a raw transaction step by step example required

Currently my code can create a transaction and compute the SHA hash but I am struggling to sign it using ECDSA.

I have written so code to do it using bitcoinj but it is not working. I need some help signing a transaction using a java library or an API. 

Here is what I have so far. Any help you be super appreciated :). 

import org.apache.commons.codec.binary.Hex;
import org.bitcoinj.core.*;
import org.bitcoinj.core.ECKey.*;

public class Test3 {
    public static void main(String[] args) {

        //Given private key and SHA 256 Hash, sign a transaction

        String shaHash = "15953935a135031bfec37d36a9d662aea43e1deb0ea463d6932ac6e537cb3e81";
        String privateKey = "private key for the address";      

        byte[] priv = Base58.decode(privateKey);
        // Generate ecsda key    
        ECKey ecdsa = ECKey.fromPrivate(priv);      
        Sha256Hash hash = new Sha256Hash(shaHash);
        // Sign the transaction
        ECDSASignature sig = ecdsa.sign(hash);
        byte[] res = sig.encodeToDER();
        String res2 = Hex.encodeHexString(res);

        System.out.println(res2);



    }
}
This answer does not attempt to sign a transaction, but simply focuses on successfully calling the sign method of the ECKey class, i.e. making your code work. I am not yet familiar with the Transaction API of bitcoinj so I cannot go further than that. Your post suggests that your private key is given as a WiF so I have taken this as an assumption. The main difficulty is to instantiate an ECKey object from the WiF which I have added to your code. I have also replaced the deprecated new Sha256Hash(...) by the wrap method. I am using a different function to encode as an hexadecimal string, but you can easily change that. The code successfully compiles on runs.

import org.bitcoinj.core.ECKey;
import org.bitcoinj.core.Base58;
import org.bitcoinj.core.Sha256Hash;
import org.bitcoinj.core.DumpedPrivateKey;
import org.bitcoinj.params.MainNetParams;
import org.bitcoinj.core.NetworkParameters;
import org.bitcoinj.core.ECKey.ECDSASignature;
import javax.xml.bind.DatatypeConverter;

public class Test {

  public static void main(String[] args){

    // message (hash) to be signed with private key
    String msg = "15953935a135031bfec37d36a9d662aea43e1deb0ea463d6932ac6e537cb3e81";

    // an example of WiF for private key (taken from 'Mastering Bitcoin')
    String wif ="KxFC1jmwwCoACiCAWZ3eXa96mBM6tb3TYzGmf6YwgdGWZgawvrtJ";      

    // creating a key object from WiF
    DumpedPrivateKey dpk = DumpedPrivateKey.fromBase58(null, wif);
    ECKey key = dpk.getKey();

    // checking our key object
    NetworkParameters main =  MainNetParams.get();
    String check = key.getPrivateKeyAsWiF(main);
    System.out.println(wif.equals(check));  // true

    // creating Sha object from string
    Sha256Hash hash = Sha256Hash.wrap(msg);

    // creating signature
    ECDSASignature sig = key.sign(hash);

    // encoding
    byte[] res = sig.encodeToDER();

    // converting to hex
    String hex = DatatypeConverter.printHexBinary(res); 

    System.out.println(hex);  // 304502210081B528....

  }
}