<p>Take a look at the DiabloMiner source code (Java): <a href="https://github.com/Diablo-D3/DiabloMiner" rel="nofollow">https://github.com/Diablo-D3/DiabloMiner</a></p>

<p>It's a GPU miner, but it has the JSON-RPC getwork stuff you need.</p>

<p>After you get basic getwork going you may want to look at implementing extensions: <a href="https://en.bitcoin.it/wiki/Getwork" rel="nofollow">https://en.bitcoin.it/wiki/Getwork</a></p>

CC BY-SA 3.0
2012-03-17T05:16:07.350Z
I have looked Diablo's code over; I was looking for something easier to understand. (Diablo doesn't comment at all, and I have little Java experience) If no one else pipes up, I'll go over that more closely and try to simplify it myself. Thanks for the suggestion in any case.
BinaryMage
CC BY-SA 3.0
2012-03-16T08:34:37.727Z
2012-03-16T08:34:37.727Z
Dr.Haribo
<p>For everyone else, who stumble upon this question :</p>

<p>I've written a Java wrapper around the JSON-RPC provided by bitcoin/litecoin.</p>

<p><a href="https://github.com/coding-idiot/Litecoin-Bitcoin-RPC-Java-Connector" rel="nofollow">https://github.com/coding-idiot/Litecoin-Bitcoin-RPC-Java-Connector</a></p>

<p>It uses Htmlunit &amp; GoogleGson and quite easy to understand and extend.</p>

CC BY-SA 3.0
2014-04-19T23:39:34.660Z
Looks good. Will be testing it
marscom
CC BY-SA 3.0
2014-02-28T06:13:56.343Z
2014-02-28T06:13:56.343Z
coding_idiot
<p>The JSON-RPC protocol is very simple.  You POST to the mining pool (or the bitcoind client):</p>

<pre><code>{"method": "getwork", "params": [], "id": "anything"}
</code></pre>

<p>and get back:</p>

<pre><code>{"result": {"data": "hex bytes....", "target": "hex bytes...", ...}, ... }
</code></pre>

<p>You just need the first 80 bytes from <code>result.data</code> - you change the last 4 bytes (nonce) and compute the hashes.  Then, when you find a hash that is less than "target" (generally a hash with the high order 32 bits equal to zero), you call getwork again to report your hash (and earn a share in the pool):</p>

<pre><code>{"method": "getwork", "params": ["hex-header"], "id": "anything"}
</code></pre>

<p>There a bunch on wonky byte-order encoding in the standard getwork call.  You'll generally want to reverse the bytes is each long integer (4 bytes) before hashing it, and do the same before reporting your found block back to the pool.  See the sample <a href="https://github.com/bitcoin/bitcoin/blob/master/contrib/pyminer/pyminer.py" rel="nofollow">pyminer</a> for a simple reference implementation of a miner (in python). Inside the <a href="https://github.com/bitcoin/bitcoin/blob/master/contrib/pyminer/pyminer.py#L93" rel="nofollow">Miner.work</a><code>()</code> function is where the magic happens. The work function takes two parameters: <code>datastr</code>, this is the value stored inside <code>result.data</code> and <code>targetstr</code> this is the value stored inside <code>result.target</code>.</p>

<p>One other thing - most pools require that you use Basic Authentication when you POST, so you'll need to include your username and password in the header of your request.</p>

CC BY-SA 3.0
2012-03-17T19:04:47.927Z
2014-02-06T20:50:17.967Z
2014-02-06T20:50:17.967Z
mckoss
<p>I'm not entirely sure whether to ask this question on Bitcoin-SE or Stack Overflow, but I decided to post it here due to the direct relevancy of the question to Bitcoin and the general prominence of programming experience I have observed on Bitcoin-SE. If Bitcoin-SE is the incorrect place to post it, I apologize in advance; I'm new to SE and this was my best guess.</p>

<p>I'm attempting, in order to better understand how Bitcoin mining works, to code a simple Bitcoin CPU miner in Java, which I have some but not much experience with. All the online resources on JSON-RPC implementation in Java I have found have either been out of date and no longer functional or incomprehensible for someone with limited Java experience. How, in the most simplistic way possible, would I code a JSON-RPC <code>getwork</code> request (to a pool server) in Java and correctly parse the output into the variables required for mining?</p>

CC BY-SA 3.0
2014-02-06T17:26:36.160Z
Or you could use [BitCoinJ](https://code.google.com/p/bitcoinj/).
0xcaff
CC BY-SA 3.0
2012-03-15T08:15:30.277Z
+1 This is the right forum to ask. (And bitcoinj would be a good place to implement this feature, not sure if they have it already).
Thilo
CC BY-SA 3.0
2012-03-16T01:15:28.430Z
@Thilo Thanks, glad I posted it in the right place. BitcoinJ implements a client-only node; it won't process RPC requests of any sort. Good idea, but I'm afraid it won't do the job.
BinaryMage
CC BY-SA 3.0
2012-03-16T02:00:19.307Z
It won't right now, but (at least) for an RPC *client* it should offer support one day (IMHO).
Thilo
CC BY-SA 3.0
2012-03-16T02:07:09.160Z
Sorry if the "client-only node" was misleading; that's just what's stated on their Google Code page. I looked through the documentation and found nothing whatsoever on RPC. I agree it _should_ implement RPC client support, but that doesn't mean it _will_. In any case, it doesn't currently.
BinaryMage
CC BY-SA 3.0
2012-03-16T02:25:29.460Z
Yes, we can agree on that ;-) Please update this post with an answer of your own if you find out how to do RPC with Java (even if you have to hand-roll it with just the JDK libs).
Thilo
<json-rpc><mining-software><cpu-mining><getwork>
How can I code a Bitcoin JSON-RPC "getwork" request in Java?
BinaryMage
# How can I code a Bitcoin JSON-RPC "getwork" request in Java?

(Asked by: username_0 on 2012-03-15)

I'm not entirely sure whether to ask this question on Bitcoin-SE or Stack Overflow, but I decided to post it here due to the direct relevancy of the question to Bitcoin and the general prominence of programming experience I have observed on Bitcoin-SE. If Bitcoin-SE is the incorrect place to post it, I apologize in advance; I'm new to SE and this was my best guess.

I'm attempting, in order to better understand how Bitcoin mining works, to code a simple Bitcoin CPU miner in Java, which I have some but not much experience with. All the online resources on JSON-RPC implementation in Java I have found have either been out of date and no longer functional or incomprehensible for someone with limited Java experience. How, in the most simplistic way possible, would I code a JSON-RPC `getwork` request (to a pool server) in Java and correctly parse the output into the variables required for mining?

**Tags:** json-rpc, mining-software, cpu-mining, getwork

> **Comments:**
> **username_2**: +1 This is the right forum to ask. (And bitcoinj would be a good place to implement this feature, not sure if they have it already).  
> **username_0**: @username_2 Thanks, glad I posted it in the right place. BitcoinJ implements a client-only node; it won't process RPC requests of any sort. Good idea, but I'm afraid it won't do the job.  
> **username_2**: It won't right now, but (at least) for an RPC *client* it should offer support one day (IMHO).  
> **username_0**: Sorry if the "client-only node" was misleading; that's just what's stated on their Google Code page. I looked through the documentation and found nothing whatsoever on RPC. I agree it _should_ implement RPC client support, but that doesn't mean it _will_. In any case, it doesn't currently.  
> **username_2**: Yes, we can agree on that ;-) Please update this post with an answer of your own if you find out how to do RPC with Java (even if you have to hand-roll it with just the JDK libs).  
> **username_1**: Or you could use [BitCoinJ](https://code.google.com/p/bitcoinj/).  

---

## 3 answers

### Answer by username_6 on 2012-03-17. Score: 3 | Accepted answer

The JSON-RPC protocol is very simple. You POST to the mining pool (or the bitcoind client):

```
{"method": "getwork", "params": [], "id": "anything"}

```

and get back:

```
{"result": {"data": "hex bytes....", "target": "hex bytes...", ...}, ... }

```

You just need the first 80 bytes from `result.data` - you change the last 4 bytes (nonce) and compute the hashes. Then, when you find a hash that is less than "target" (generally a hash with the high order 32 bits equal to zero), you call getwork again to report your hash (and earn a share in the pool):

```
{"method": "getwork", "params": ["hex-header"], "id": "anything"}

```

There a bunch on wonky byte-order encoding in the standard getwork call. You'll generally want to reverse the bytes is each long integer (4 bytes) before hashing it, and do the same before reporting your found block back to the pool. See the sample [pyminer](https://github.com/bitcoin/bitcoin/blob/master/contrib/pyminer/pyminer.py) for a simple reference implementation of a miner (in python). Inside the [Miner.work](https://github.com/bitcoin/bitcoin/blob/master/contrib/pyminer/pyminer.py#L93)`()` function is where the magic happens. The work function takes two parameters: `datastr`, this is the value stored inside `result.data` and `targetstr` this is the value stored inside `result.target`.

One other thing - most pools require that you use Basic Authentication when you POST, so you'll need to include your username and password in the header of your request.


---

### Answer by username_3 on 2012-03-16. Score: 1

Take a look at the DiabloMiner source code (Java): <https://github.com/Diablo-D3/DiabloMiner>

It's a GPU miner, but it has the JSON-RPC getwork stuff you need.

After you get basic getwork going you may want to look at implementing extensions: <https://en.bitcoin.it/wiki/Getwork>

> **Comments:**
> **username_0**: I have looked Diablo's code over; I was looking for something easier to understand. (Diablo doesn't comment at all, and I have little Java experience) If no one else pipes up, I'll go over that more closely and try to simplify it myself. Thanks for the suggestion in any case.  

---

### Answer by username_4 on 2014-02-28. Score: 1

For everyone else, who stumble upon this question :

I've written a Java wrapper around the JSON-RPC provided by bitcoin/litecoin.

<https://github.com/username_4/Litecoin-Bitcoin-RPC-Java-Connector>

It uses Htmlunit & GoogleGson and quite easy to understand and extend.

> **Comments:**
> **username_5**: Looks good. Will be testing it  

---

