1

I need to convert a public key into a form suitable for use with BouncyCastle.

To do this I open the debugger and write:

ListAddressGroupings

I then pick an address and write

validateaddress 1HQCZTfr7V7WAMUcVHM21TLfLoeQWEtNmS

And I get the result:

{
"isvalid" : true,
"address" : "1HQCZTfr7V7WAMUcVHM21TLfLoeQWEtNmS",
"ismine" : true,
"isscript" : false,
"pubkey" : "020ba3ebc2f55152df5653bb7aba6548f0615d67b072379bdd19e72bc63c052c50",
"iscompressed" : true,
"account" : "testDONOTUSE"
}

However I'm not sure what format this is in.

  • What is the encoding of JSON-RPC's validateaddress value named pubkey?

I am asking this question so that I can determine the "recId" value needed for signature recovery when a UTF8 message is signed. In other words, I need convert the public key below into an array that I can use for that purpose.

  • Is there an easier or better way to get the pubkey from a given wallet?

Sample data

Base58: 1HQCZTfr7V7WAMUcVHM21TLfLoeQWEtNmS
Public: 020ba3ebc2f55152df5653bb7aba6548f0615d67b072379bdd19e72bc63c052c50

SignMessage: Test
Output: IITRv4NdcaIgXP7DDNOkOrShBiJkBFoBqjXJXozKNClHnwSmNK3+QbT7ypKTkcc0F5UPsUCef5+gqhTb8sBZLuQ=

Further attempts:

  • When I convert public value "020ba3ebc2f55152df5653bb7aba6548f0615d67b072379bdd19e72bc63c052c50" as hex to bytes using this method on SO, I get the column below on the left. This is incorrect. However when I extract the public key from the private key, I get a similar but different result on the right.

    public static byte[] publicKeyFromPrivate(BigInteger privKey, bool compressed)
    { 
        X9ECParameters ecParams = Org.BouncyCastle.Asn1.Sec.SecNamedCurves.GetByName("secp256k1");
        ECPoint point1 = ecParams.G.Multiply(privKey);
        return point1.GetEncoded(compressed);
    } 
    

enter image description here

1 Answer 1

2

The public key is in the format returned by OpenSSL and is a straight hex-encoding of the bytes. If the first byte is 0x04, then it is a 65-byte uncompressed key (32-byte X and 32-byte Y). If the first byte is 0x02 or 0x03, it is a 33-byte compressed key (32-byte X and one bit for Y sign).

The recID is encoded as part of the signed message. Check the verifyMessage() method in the ECKey class to see how it is extracted.

The column on the right is correct and it matches what you got from validateaddress.

1
  • My code for reading the hex data was incorrect, it was case sensitive. Thanks! Commented Feb 11, 2014 at 17:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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