<p>That particular line of code asserts that the private key fits into a 32 byte-sized byte array properly with the necessary zero paddings. As the private key is simply some random Big Integer between 0 ~ 2^(256-1), javascript may encode that integer into different byte array sizes. For example, if the <code>d</code> value was simply 1, then it could easily fit into a 1 byte-sized byte array and you would not have the leading/trailing zeroes required depending on the endianess of your encoding of choice.</p>

CC BY-SA 3.0
2016-12-01T14:51:11.627Z
Ah ha! That makes sense. Thank you for the great clarification.
user1929274
CC BY-SA 3.0
2016-12-01T10:47:34.563Z
2016-12-01T10:58:24.673Z
2016-12-01T10:58:24.673Z
rny
<p>I'm trying to understand what a specific line of code does when signing a message using bitcoinjs-lib. Here's the code:</p>

<pre><code>var bitcoin = require('bitcoinjs-lib')
var bitcoinMessage = require('bitcoinjs-message')

var keyPair = bitcoin.ECPair.makeRandom();
//private key
var privateKey = keyPair.toWIF();
// Address
var address = keyPair.getAddress();

console.log(privateKey);
console.log(address);

privateKey = keyPair.d.toBuffer(32)
var message = 'your message'
var messagePrefix = bitcoin.networks.bitcoin.messagePrefix

var signature = bitcoinMessage.sign(message, messagePrefix, privateKey, keyPair.compressed)
console.log(signature.toString('base64'))
</code></pre>

<p>The line I'm curious about is this one:</p>

<pre><code>privateKey = keyPair.d.toBuffer(32)
</code></pre>

<p>What exactly is this code doing and why is it necessary?</p>

<p>Thanks,</p>

<p>Connor</p>

<bitcoin-core><cryptography><bitcoinjs>
Understanding signing messages with bitcoinjs-lib
user1929274
# Understanding signing messages with bitcoinjs-lib

(Asked by: username_0 on 2016-11-30)

I'm trying to understand what a specific line of code does when signing a message using bitcoinjs-lib. Here's the code:

```
var bitcoin = require('bitcoinjs-lib')
var bitcoinMessage = require('bitcoinjs-message')

var keyPair = bitcoin.ECPair.makeRandom();
//private key
var privateKey = keyPair.toWIF();
// Address
var address = keyPair.getAddress();

console.log(privateKey);
console.log(address);

privateKey = keyPair.d.toBuffer(32)
var message = 'your message'
var messagePrefix = bitcoin.networks.bitcoin.messagePrefix

var signature = bitcoinMessage.sign(message, messagePrefix, privateKey, keyPair.compressed)
console.log(signature.toString('base64'))

```

The line I'm curious about is this one:

```
privateKey = keyPair.d.toBuffer(32)

```

What exactly is this code doing and why is it necessary?

Thanks,

Connor

**Tags:** bitcoin-core, cryptography, bitcoinjs


---

## 1 answers

### Answer by username_1 on 2016-12-01. Score: 2 | Accepted answer

That particular line of code asserts that the private key fits into a 32 byte-sized byte array properly with the necessary zero paddings. As the private key is simply some random Big Integer between 0 ~ 2^(256-1), javascript may encode that integer into different byte array sizes. For example, if the `d` value was simply 1, then it could easily fit into a 1 byte-sized byte array and you would not have the leading/trailing zeroes required depending on the endianess of your encoding of choice.

> **Comments:**
> **username_0**: Ah ha! That makes sense. Thank you for the great clarification.  

---

