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

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

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

CC BY-SA 3.0
2014-02-11T17:09:28.867Z
My code for reading the hex data was incorrect, it was case sensitive.  Thanks!
makerofthings7
CC BY-SA 3.0
2014-02-11T05:02:54.797Z
2014-02-11T05:02:54.797Z
ScripterRon
<p>I need to convert a public key into a form suitable for use with BouncyCastle.</p>

<p>To do this I open the debugger and write:</p>

<pre><code>ListAddressGroupings
</code></pre>

<p>I then pick an address and write</p>

<pre><code>validateaddress 1HQCZTfr7V7WAMUcVHM21TLfLoeQWEtNmS
</code></pre>

<p>And I get the result: </p>

<pre><code>{
"isvalid" : true,
"address" : "1HQCZTfr7V7WAMUcVHM21TLfLoeQWEtNmS",
"ismine" : true,
"isscript" : false,
"pubkey" : "020ba3ebc2f55152df5653bb7aba6548f0615d67b072379bdd19e72bc63c052c50",
"iscompressed" : true,
"account" : "testDONOTUSE"
}
</code></pre>

<p>However I'm not sure what format this is in.</p>

<ul>
<li>What is the encoding of JSON-RPC's validateaddress value named <code>pubkey</code>?</li>
</ul>

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

<ul>
<li>Is there an easier or better way to get the pubkey from a given wallet?</li>
</ul>

<p><strong>Sample data</strong></p>

<pre><code>Base58: 1HQCZTfr7V7WAMUcVHM21TLfLoeQWEtNmS
Public: 020ba3ebc2f55152df5653bb7aba6548f0615d67b072379bdd19e72bc63c052c50

SignMessage: Test
Output: IITRv4NdcaIgXP7DDNOkOrShBiJkBFoBqjXJXozKNClHnwSmNK3+QbT7ypKTkcc0F5UPsUCef5+gqhTb8sBZLuQ=
</code></pre>

<p>Further attempts:</p>

<ul>
<li><p>When I convert public value "020ba3ebc2f55152df5653bb7aba6548f0615d67b072379bdd19e72bc63c052c50" as hex to bytes <a href="https://stackoverflow.com/a/9995303">using this method</a> on SO, I get the column below on the left.  This is incorrect.  However when I <a href="https://code.google.com/p/bitcoinj/source/browse/core/src/main/java/com/google/bitcoin/core/ECKey.java#253" rel="nofollow noreferrer">extract the public key from the private key</a>, I get a similar but different result on the right.</p>

<pre><code>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);
} 
</code></pre></li>
</ul>

<p><img src="https://i.sstatic.net/UJbvXl.png" alt="enter image description here"></p>

<address><development><json-rpc><c#>
What encoding is "pubkey" from the JSONRPC validateaddress command?
makerofthings7
# What encoding is "pubkey" from the JSONRPC validateaddress command?

(Asked by: username_0 on 2014-02-10)

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](https://stackoverflow.com/a/9995303) on SO, I get the column below on the left. This is incorrect. However when I [extract the public key from the private key](https://code.google.com/p/bitcoinj/source/browse/core/src/main/java/com/google/bitcoin/core/ECKey.java#253), 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](https://i.sstatic.net/UJbvXl.png)

**Tags:** address, development, json-rpc, c#


---

## 1 answers

### Answer by username_1 on 2014-02-11. Score: 2 | Accepted answer

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.

> **Comments:**
> **username_0**: My code for reading the hex data was incorrect, it was case sensitive.  Thanks!  

---

