<p>This answer does not attempt to sign a transaction, but simply focuses on successfully calling the <code>sign</code> method of the <code>ECKey</code> class, i.e. <strong>making your code work</strong>. I am not yet familiar with the <code>Transaction</code> API of <code>bitcoinj</code> so I cannot go further than that. Your post suggests that your private key is given as a <code>WiF</code> so I have taken this as an assumption. The main difficulty is to instantiate an <code>ECKey</code> object from the <code>WiF</code> which I have added to your code. I have also replaced the deprecated <code>new Sha256Hash(...)</code> by the <code>wrap</code> 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.</p>

<pre><code>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....

  }
}
</code></pre>

CC BY-SA 3.0
2017-01-05T23:47:23.060Z
Perfect, thanks so much for your help! My code is now working.
alex676
CC BY-SA 3.0
2017-01-05T15:06:24.007Z
2017-01-05T15:06:24.007Z
Sven Williamson
<p>I have followed the instructions here to build my own bitcoin transaction.</p>

<p><a href="https://bitcoin.stackexchange.com/questions/32628/redeeming-a-raw-transaction-step-by-step-example-required">Redeeming a raw transaction step by step example required</a></p>

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

<p>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. </p>

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

<pre><code>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);



    }
}
</code></pre>

CC BY-SA 3.0
2017-01-05T01:03:29.553Z
So I think that problem may be that the private key is in a WIF format that I need to convert into an ECDSA private key, but not sure how to do that.
alex676
<bitcoinj><signature><raw-transaction><ecdsa><java>
How to sign raw transaction given a private key and SHA hash (in java)
alex676
# How to sign raw transaction given a private key and SHA hash (in java)

(Asked by: username_0 on 2017-01-04)

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);



    }
}

**Tags:** bitcoinj, signature, raw-transaction, ecdsa, java

> **Comments:**
> **username_0**: So I think that problem may be that the private key is in a WIF format that I need to convert into an ECDSA private key, but not sure how to do that.  

---

## 1 answers

### Answer by username_1 on 2017-01-05. Score: 7 | Accepted answer

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....

  }
}

> **Comments:**
> **username_0**: Perfect, thanks so much for your help! My code is now working.  

---

