<p>You could try using <a href="https://github.com/bitchan/eccrypto" rel="nofollow noreferrer"><code>eccrypto</code></a></p>
<p><strong><code>index.js</code></strong>:</p>
<pre><code>var crypto = require(&quot;crypto&quot;);
var eccrypto = require(&quot;eccrypto&quot;);

// A new random 32-byte private key.
var privateKey = Buffer.from('2fd7cab0970c692b4151d77a6aeebcae2a3284556cbfc6f182c571eccfc2424f', 'hex')
// Corresponding uncompressed (65-byte) public key.
var publicKey = eccrypto.getPublic(privateKey);

var buf = Buffer.from('05de69f5d37f41340eed3230f03d2394dde5e497738a76f027b7d962a0cbdf39', 'hex')
// Always hash you message to sign!
var msg = crypto.createHash(&quot;sha256&quot;).update(buf).digest();

eccrypto.sign(privateKey, buf).then(function(sig) {
  console.log(&quot;Signature in DER format (hex):&quot;, sig.toString('hex'))
  eccrypto.verify(publicKey, buf, sig).then(function() {
    console.log(&quot;Signature is OK&quot;);
  }).catch(function() {
    console.log(&quot;Signature is BAD&quot;);
  });
});
</code></pre>
<p><strong>console output</strong>:</p>
<pre><code>$ node index.js
Signature in DER format (hex): 30440220352f6d47bc33e2596600c6a297a0e3e84079a9b2f16d10cb9396993370365976022062a30c63adfeef315bcfc690e254dd3b6d4f14fe9919317e14ebb15c29325326
Signature is OK
</code></pre>

CC BY-SA 4.0
2019-01-11T16:49:35.713Z
2021-12-20T16:27:44.603Z
2021-12-20T16:27:44.603Z
JBaczuk
<p>Using BlockCypher’s signing tool (<a href="https://github.com/blockcypher/btcutils/tree/master/signer" rel="nofollow noreferrer">link</a>), I can sign the following data:</p>

<pre><code>05de69f5d37f41340eed3230f03d2394dde5e497738a76f027b7d962a0cbdf39
</code></pre>

<p>with the private key:</p>

<pre><code>2fd7cab0970c692b4151d77a6aeebcae2a3284556cbfc6f182c571eccfc2424f
</code></pre>

<p>to get the result:</p>

<pre><code>30440220352f6d47bc33e2596600c6a297a0e3e84079a9b2f16d10cb9396993370365976022062a30c63adfeef315bcfc690e254dd3b6d4f14fe9919317e14ebb15c29325326
</code></pre>

<p>However, I'm am not able to replicate this using javascript using the bitcoinjs-lib module. My attempt is shown below:</p>

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

var dataToSign = '05de69f5d37f41340eed3230f03d2394dde5e497738a76f027b7d962a0cbdf39'

var wifObj = wif.decode('KxpiD4T1QHFahGVyg9onfLXjbwBSf5Dyh3BPYY899uXQHn6gfnbz')
var keys = bitcoin.ECPair.fromPrivateKey(wifObj.privateKey)

var signed = keys.sign(Buffer.from(dataToSign, 'hex'))
var signedHex = signed.toString('hex')
</code></pre>

CC BY-SA 4.0
2019-01-11T14:29:02.937Z
What exactly are you trying to accomplish? Are you trying to match the signatures exactly? Signatures are not always deterministic, do you get the same result running blockcypher's tool multiple times with the same data?
JBaczuk
CC BY-SA 4.0
2019-01-11T14:35:13.547Z
I'm basically trying to replicate blockcypher's signing tool in javascript and I think I'm quite close... Yes, it's deterministic
timothyylim
CC BY-SA 4.0
2019-01-11T17:18:35.470Z
If it uses RFC6979, signatures can be deterministic, otherwise they are probably not (and should not be).
Thorkil Værge
<transactions><private-key><javascript><bitcoinjs><blockcypher>
How do you sign data with a private key? [javascript]
timothyylim
# How do you sign data with a private key? [javascript]

(Asked by: username_0 on 2019-01-11)

Using BlockCypher’s signing tool ([link](https://github.com/blockcypher/btcutils/tree/master/signer)), I can sign the following data:

```
05de69f5d37f41340eed3230f03d2394dde5e497738a76f027b7d962a0cbdf39

```

with the private key:

```
2fd7cab0970c692b4151d77a6aeebcae2a3284556cbfc6f182c571eccfc2424f

```

to get the result:

```
30440220352f6d47bc33e2596600c6a297a0e3e84079a9b2f16d10cb9396993370365976022062a30c63adfeef315bcfc690e254dd3b6d4f14fe9919317e14ebb15c29325326

```

However, I'm am not able to replicate this using javascript using the bitcoinjs-lib module. My attempt is shown below:

```
var bitcoin = require('bitcoinjs-lib')
var wif = require('wif')

var dataToSign = '05de69f5d37f41340eed3230f03d2394dde5e497738a76f027b7d962a0cbdf39'

var wifObj = wif.decode('KxpiD4T1QHFahGVyg9onfLXjbwBSf5Dyh3BPYY899uXQHn6gfnbz')
var keys = bitcoin.ECPair.fromPrivateKey(wifObj.privateKey)

var signed = keys.sign(Buffer.from(dataToSign, 'hex'))
var signedHex = signed.toString('hex')

```

**Tags:** transactions, private-key, javascript, bitcoinjs, blockcypher

> **Comments:**
> **username_1**: What exactly are you trying to accomplish? Are you trying to match the signatures exactly? Signatures are not always deterministic, do you get the same result running blockcypher's tool multiple times with the same data?  
> **username_0**: I'm basically trying to replicate blockcypher's signing tool in javascript and I think I'm quite close... Yes, it's deterministic  
> **username_2**: If it uses RFC6979, signatures can be deterministic, otherwise they are probably not (and should not be).  

---

## 1 answers

### Answer by username_1 on 2019-01-11. Score: 1 | Accepted answer

You could try using [`eccrypto`](https://github.com/bitchan/eccrypto)

**`index.js`**:

```
var crypto = require("crypto");
var eccrypto = require("eccrypto");

// A new random 32-byte private key.
var privateKey = Buffer.from('2fd7cab0970c692b4151d77a6aeebcae2a3284556cbfc6f182c571eccfc2424f', 'hex')
// Corresponding uncompressed (65-byte) public key.
var publicKey = eccrypto.getPublic(privateKey);

var buf = Buffer.from('05de69f5d37f41340eed3230f03d2394dde5e497738a76f027b7d962a0cbdf39', 'hex')
// Always hash you message to sign!
var msg = crypto.createHash("sha256").update(buf).digest();

eccrypto.sign(privateKey, buf).then(function(sig) {
  console.log("Signature in DER format (hex):", sig.toString('hex'))
  eccrypto.verify(publicKey, buf, sig).then(function() {
    console.log("Signature is OK");
  }).catch(function() {
    console.log("Signature is BAD");
  });
});

```

**console output**:

```
$ node index.js
Signature in DER format (hex): 30440220352f6d47bc33e2596600c6a297a0e3e84079a9b2f16d10cb9396993370365976022062a30c63adfeef315bcfc690e254dd3b6d4f14fe9919317e14ebb15c29325326
Signature is OK

```


---

