5

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



    }
}
  • 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 Jan 5 '17 at 1:03
6

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

  }
}
  • 1
    Perfect, thanks so much for your help! My code is now working. – alex676 Jan 5 '17 at 23:47

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.