<p>I'm much closer now I hope this helps someone else trying to do this!</p>

<p>REQUIREMENTS:</p>

<pre><code>NPM bitcoin bitcoin-js merkle-lib
</code></pre>

<p>CODE:</p>

<pre><code>client.getBlockTemplate( function(error, template) {
    if (error) return console.log(error);
    txhashes = template.transactions.map(function(tx){ return Buffer.from(tx.txid, 'hex').reverse() });
    merkleRoot = fastMerkleRoot(txhashes, bcrypto.hash256);
    console.log(merkleRoot);
)};
</code></pre>

<p>The result is in a Buffer and I'm having trouble converting it to hex.</p>

CC BY-SA 3.0
2018-03-11T02:48:24.267Z
2018-03-11T03:07:39.250Z
2018-03-11T03:07:39.250Z
Corbin
<p>I want to compute the merkle root for transactions in a given template block -  bitcoind:getblocktemplate(). </p>

<p>I understand the merkle root is a hash of all the transactions in a block from the last up to the coinbase transaction, but this is currently way above my pay grade and the documentation I can find for this process is abstract and sketchy at best.</p>

<p>Has anyone done this in JavaScript or know of a resource that could be helpful?</p>

<p>Thanks in advance.</p>

<pre><code>client.getBlockTemplate( function(error, template) {
    if (error) return console.log(error);

    // SETUP MINING
    nonce=0;
    const block = {
        version: template.version
        ,previousblockhash: template.previousblockhash
        ,merkleroot: calculateMerkleRoot(template)
        ,time: template.mintime
        ,bits: template.bits
    }           

    // START MINER
    miner(block,nonce);
});
</code></pre>

CC BY-SA 3.0
2018-03-11T03:00:07.813Z
It's named after Ralph Merkle, not Angela Merkel.
Pieter Wuille
CC BY-SA 3.0
2018-03-11T03:03:28.343Z
Oh HAHAHAHAHAHA, fixed.
Corbin
<bitcoind><json-rpc><getblocktemplate>
Building the merkle root from transactions in a template block
Corbin
# Building the merkle root from transactions in a template block

(Asked by: username_0 on 2018-03-10)

I want to compute the merkle root for transactions in a given template block - bitcoind:getblocktemplate().

I understand the merkle root is a hash of all the transactions in a block from the last up to the coinbase transaction, but this is currently way above my pay grade and the documentation I can find for this process is abstract and sketchy at best.

Has anyone done this in JavaScript or know of a resource that could be helpful?

Thanks in advance.

```
client.getBlockTemplate( function(error, template) {
    if (error) return console.log(error);

    // SETUP MINING
    nonce=0;
    const block = {
        version: template.version
        ,previousblockhash: template.previousblockhash
        ,merkleroot: calculateMerkleRoot(template)
        ,time: template.mintime
        ,bits: template.bits
    }           

    // START MINER
    miner(block,nonce);
});

```

**Tags:** bitcoind, json-rpc, getblocktemplate

> **Comments:**
> **username_1**: It's named after Ralph Merkle, not Angela Merkel.  
> **username_0**: Oh HAHAHAHAHAHA, fixed.  

---

## 1 answers

### Answer by username_0 on 2018-03-11. Score: 1

I'm much closer now I hope this helps someone else trying to do this!

REQUIREMENTS:

```
NPM bitcoin bitcoin-js merkle-lib

```

CODE:

```
client.getBlockTemplate( function(error, template) {
    if (error) return console.log(error);
    txhashes = template.transactions.map(function(tx){ return Buffer.from(tx.txid, 'hex').reverse() });
    merkleRoot = fastMerkleRoot(txhashes, bcrypto.hash256);
    console.log(merkleRoot);
)};

```

The result is in a Buffer and I'm having trouble converting it to hex.


---

