How to create a brainwallet with bitcoinjs?

2

2

This is how to create a brainwallet with bitcore

const Bitcore = require("bitcore-lib");
// const Mnemonic = require("bitcore-mnemonic");
// https://bitcore.io/api/lib/crypto
let brainsrc= 'satoshi'
let input = new Buffer(brainsrc)
let hash = Bitcore.crypto.Hash.sha256(input)
let bn = Bitcore.crypto.BN.fromBuffer(hash)
let pk = new Bitcore.PrivateKey(bn).toWIF()
let addy = new Bitcore.PrivateKey(bn).toAddress();
console.log('The brain wallet of '+brainsrc+' \nAddress: '+addy,' Private key:'+pk)

For bitcoinjs, I only know how to create random address:

// https://github.com/bitcoinjs/bitcoinjs-lib/blob/v3.3.2/test/integration/addresses.js

const bitcoin = require('bitcoinjs-lib')
var keyPair = bitcoin.ECPair.makeRandom()
var secret = (keyPair.toWIF())
var addr = (keyPair.getAddress())

console.log('The brain wallet of '+brainsrc+' \nAddress: '+addr,' Private key:'+secret)

How to do it?

TSR

Posted 2018-09-12T00:39:26.013

Reputation: 133

Answers

0

First of all, don't use 1 word seeds, that will be too easy to brute force...

index.js

const bitcoin = require('bitcoinjs-lib')

const brainsrc = 'satoshi'
const hash = bitcoin.crypto.sha256(Buffer.from(brainsrc))
const keyPair = bitcoin.ECPair.fromPrivateKey(hash)
const addr = bitcoin.payments.p2pkh({ pubkey: keyPair.publicKey }).address
const secret = keyPair.toWIF()

console.log('The brain wallet of '+brainsrc+' \nAddress: '+addr,' Private key:'+secret)

output:

$ node index.js
The brain wallet of satoshi
Address: 1xm4vFerV3pSgvBFkyzLgT1Ew3HQYrS1V  Private key:L4XnHhvLC1b4ag9L2PM9kRicQxUoYT1Q36PQ21YtLNkrAdWZNos6

Examples can be found on the git repo: https://github.com/bitcoinjs/bitcoinjs-lib#examples

JBaczuk

Posted 2018-09-12T00:39:26.013

Reputation: 6 172

TypeError: bitcoin.ECPair.fromPrivateKey is not a functionTSR 2018-09-13T00:48:58.123

what version of bitcoinjs-lib and node are you using?JBaczuk 2018-09-13T01:31:31.193

Yes node. I upgraded to 4.0.0 and it works.TSR 2018-09-13T02:05:29.460

It would be a plus if you can provide a link to the documentationTSR 2018-09-13T02:05:58.867

Good idea, just edited the answer, Bitcoinjs-lib doesn't have formal docs, but there are examples.JBaczuk 2018-09-13T02:20:15.020