<p>I have this:</p>

<pre><code>const crypto = require('crypto')
const bs58 = require('bs58')

const a = crypto.randomBytes(32)
const b = crypto.createHash('sha256').update(a).digest('hex')
const c = '80' + b
const d = crypto.createHash('sha256').update(c).digest('hex')
const e = crypto.createHash('sha256').update(d).digest('hex')
const f = c + e.substr(0, 4)
const bytes = Buffer.from(f, 'hex')
const address = bs58.encode(bytes)
console.log(address)
</code></pre>

<p>Which was inspired from <a href="https://bitcoin.stackexchange.com/questions/8247/how-can-i-convert-a-sha256-hash-into-a-bitcoin-base58-private-key/91321#91321">this</a>. But when I do this:</p>

<pre><code> bitcoin-cli importprivkey "&lt;key&gt;" "foo"
</code></pre>

<p>I get this:</p>

<pre><code>error code: -5
error message:
Invalid private key encoding
</code></pre>

<p>What am I doing wrong?</p>

CC BY-SA 4.0
2019-10-28T14:34:32.240Z
Possible duplicate of [OP\_SHA256 mismatch!](https://bitcoin.stackexchange.com/questions/40924/op-sha256-mismatch)
Claris
CC BY-SA 4.0
2019-10-28T15:03:59.000Z
I don't understand what that is saying :/
Lance Pollard
CC BY-SA 4.0
2019-10-28T15:33:30.243Z
@Anonymous I don't think that's the problem.
Pieter Wuille
CC BY-SA 4.0
2019-10-28T15:35:11.770Z
Did you intend for `a` to be the private key or for it's hash `b` to be the private key?
Ava Chow
CC BY-SA 4.0
2019-10-28T16:41:42.893Z
@PieterWuille They are hashing the output of tohex(). It’s at least one of the problems.
Claris
<private-key><node.js>
What am I doing wrong when calculating the BitCoin Base58 Private Key?
Lance Pollard
# What am I doing wrong when calculating the BitCoin Base58 Private Key?

(Asked by: username_0 on 2019-10-28)

I have this:

```
const crypto = require('crypto')
const bs58 = require('bs58')

const a = crypto.randomBytes(32)
const b = crypto.createHash('sha256').update(a).digest('hex')
const c = '80' + b
const d = crypto.createHash('sha256').update(c).digest('hex')
const e = crypto.createHash('sha256').update(d).digest('hex')
const f = c + e.substr(0, 4)
const bytes = Buffer.from(f, 'hex')
const address = bs58.encode(bytes)
console.log(address)

```

Which was inspired from [this](https://bitcoin.stackexchange.com/questions/8247/how-can-i-convert-a-sha256-hash-into-a-bitcoin-base58-private-key/91321#91321). But when I do this:

```
 bitcoin-cli importprivkey "<key>" "foo"

```

I get this:

```
error code: -5
error message:
Invalid private key encoding

```

What am I doing wrong?

**Tags:** private-key, node.js

> **Comments:**
> **username_1**: Possible duplicate of [OP\_SHA256 mismatch!](https://bitcoin.stackexchange.com/questions/40924/op-sha256-mismatch)  
> **username_0**: I don't understand what that is saying :/  
> **username_2**: @Anonymous I don't think that's the problem.  
> **username_3**: Did you intend for `a` to be the private key or for it's hash `b` to be the private key?  
> **username_1**: @username_2 They are hashing the output of tohex(). It’s at least one of the problems.  

---

## 0 answers

