Giter VIP home page Giter VIP logo

bip38's Introduction

bip38

build status Coverage Status Version

js-standard-style

A JavaScript component that adheres to the BIP38 standard to secure your crypto currency private keys. Fully compliant with Node.js and the browser (via Browserify).

Why?

BIP38 is a standard process to encrypt Bitcoin and crypto currency private keys that is impervious to brute force attacks thus protecting the user.

Package Info

Usage

Installation

npm install --save bip38

Async methods

Async methods are available, but using them will be slower, but free up the event loop in intervals you choose.

For benchmark results, please see the section in the README of the scryptsy library. Increasing the interval will decrease the performance hit, but increase the span between event loop free ups (UI drawing etc.) promiseInterval is the last optional parameter after scryptParams. There is no recommendation currently, as the performance trade off is app specific.

API

encrypt(buffer, compressed, passphrase[, progressCallback, scryptParams])

var bip38 = require('bip38')
var wif = require('wif')

var myWifString = '5KN7MzqK5wt2TP1fQCYyHBtDrXdJuXbUzm4A9rKAteGu3Qi5CVR'
var decoded = wif.decode(myWifString)

var encryptedKey = bip38.encrypt(decoded.privateKey, decoded.compressed, 'TestingOneTwoThree')
console.log(encryptedKey)
// => '6PRVWUbkzzsbcVac2qwfssoUJAN1Xhrg6bNk8J7Nzm5H7kxEbn2Nh2ZoGg'

decrypt(encryptedKey, passphrase[, progressCallback, scryptParams])

var bip38 = require('bip38')
var wif = require('wif')

var encryptedKey = '6PRVWUbkzzsbcVac2qwfssoUJAN1Xhrg6bNk8J7Nzm5H7kxEbn2Nh2ZoGg'
var decryptedKey = bip38.decrypt(encryptedKey, 'TestingOneTwoThree', function (status) {
  console.log(status.percent) // will print the percent every time current increases by 1000
})

console.log(wif.encode(0x80, decryptedKey.privateKey, decryptedKey.compressed))
// => '5KN7MzqK5wt2TP1fQCYyHBtDrXdJuXbUzm4A9rKAteGu3Qi5CVR'

References

bip38's People

Contributors

cloneearth avatar d-yokoi avatar dcousens avatar dependabot[bot] avatar faustbrian avatar isghe avatar jprichardson avatar junderw avatar kklash avatar midnightlightning avatar reqshark avatar ryanzim avatar thehobbit85 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bip38's Issues

Add asynchronous support

BIP38 encryption should really be asynchronous. Otherwise the browser blocks and the user does not receive progress updates until after a key is fully encrypted.

I've implemented asynchronous support bip38 and scryptsy here and here for consideration.

Testnet support

I was using bip38 to add a passphrase to my wif private key.
On testnet3.
Wif: cP9vgjQNxajMj5AhPQwHjywhnRkBmqVyFEJfztbWhnNqmz1GQW1f
Wif (passphrase): 6PYWrxeW1MrM3v7twoJVfBSe2QR6RoFimt5eTQE9nS4mnRyKHVKLaCNqXN
Turns out bitaddress.org tells me the Wif address only is valid on testnet, while the passphrase version works on mainnet and NOT on testnet.
I would expect a testnet address to be valid on testnet and not on maintenant, passphrase or not.

Get encrypted private key from mnemonic for decryption?

Hey all,

First time really get my hands dirty with bitcoinjs-lib and related libraries. I'm working with a BIP39 mnemonic and a BIP38 passphrase for it. I'm not quite sure how to get the private key in the right format for the bip38.decrypt() function

const bip32 = require('bip32');
const bip38 = require('bip38');
const bip39 = require('bip39');
const bitcoin = require('bitcoinjs-lib');

const mnemonic = 'redacted mnemonic';
const passphrase = 'redacted passphrase';

const seedBuffer = bip39.mnemonicToSeedSync(mnemonic);
const root = bip32.fromSeed(seedBuffer);

// none of the following values work as encryptedPrivKey in bip38.decrypt()
const encryptedPrivKey = root.toBase58();
// encryptedPrivKey
// root.privateKey
// root.toWIF()
// seedBuffer

const privateKeyWif = bip38.decrypt(encryptedPrivKey, passphrase, function (status) {
  console.log(status.percent)
});

Apologies if this question is better suited for one of the other repos.

api change

My code stopped working, did the API change? Could you make clear how to use the library now?

I get:

var bip38 = new Bip38()
            ^

TypeError: Bip38 is not a function

BIP38: Passphrase Unicode Normalization

According to BIP38, the passphrase should be normalized:

Parameters: passphrase is the passphrase itself encoded in UTF-8 and normalized using Unicode Normalization Form C (NFC).

But there isn't any treatment in this library. It should either be implemented or explicitly warned in the README.

Example (No Compression, No EC-multiply Test Vector 3):
Unencrypted Key (WIF): 5Jajm8eQ22H3pGWLEVCXyvND8dQZhiQhoLJNKjYXk9roUFTMSZ4
Unencrypted Key (hex): 64eeab5f9be2a01a8365a579511eb3373c87c40da6d2a25f05bda68fe077b66e
Passphrase ϓ␀𐐀💩 (\u03D2\u0301\u0000\U00010400\U0001F4A9)
Encrypted Key: 6PRW5o9FLp4gJDDVqJQKJFTpMvdsSGJxMYHtHaQBF3ooa8mwD69bapcDQn

const passphrase = '\u03D2\u0301\u{0000}\u{00010400}\u{0001F4A9}';
const unencrypted = Buffer.from('64eeab5f9be2a01a8365a579511eb3373c87c40da6d2a25f05bda68fe077b66e', 'hex');

// This doesn't pass the test
bip38.encrypt(unencrypted, false, passphrase);  
> 6PRW5o9FMK7cTQUL1mbVD1NzNjHvhkXwkafES6NB1yvbEeZc3tcseHu5CW

// This works
bip38.encrypt(unencrypted, false, passphrase.normalize("NFC")); 
> 6PRW5o9FLp4gJDDVqJQKJFTpMvdsSGJxMYHtHaQBF3ooa8mwD69bapcDQn

Altcoin encrypted wallets?

Does this work with altcoins or only bitcoin? There's a comment in the code with a TODO about being bitcoin only?

I seem to get different values when i encrypt and decrypt a private key i have generated for my altcoin using your other library.

The WIF private key and public keys generated are correct and working, just this BIP38 encrypt / decyrpt gives me different values..

Also, i had to write the code different from your examples. It was complaining that Bip38 was not a contstructor. So here's my code

var CoinKey = require('coinkey')
var secureRandom = require('secure-random')
var qrcode = require('qrcode-generator')
var bip38 = require('bip38')
var wif = require('wif')

var version = {
    private: 0x96,
    public: 0x35
  }
  var bytes = secureRandom.randomBuffer(32)
  var key = new CoinKey(bytes, version)

  var decoded = wif.decode(key.privateWif)

  bip38.version = version;

  var encryptedKey = bip38.encrypt(decoded.privateKey, decoded.compressed, 'MyPassword', function (status) {
    console.log('encrypting', Math.round(status.percent), '%')
  })

  var decryptedKey = bip38.decrypt(encryptedKey, 'MyPassword', function (status) {
    console.log('decrypting', Math.round(status.percent), '%')
  })

  console.log(wif.encode(decryptedKey, decryptedKey.compressed), key.privateWif, decryptedKey, encryptedKey)

and the output:

12dsPqsKogz4yTLLVAVXLMTT5cMbKn1NYePLb5U6hPoVeLjVP2D2 PDcTA3vG4kQh95KZSXhUkTeWQnVSYYgQxuptXDSJMArFszXXfVPC Object {privateKey: Uint8Array(32), compressed: true} 6PYLsBPwgLhrtzfReFf4phQk9Yb3iLCtoXb8ehbVmiPfmeSQxaZdiftCoD

i thought the output of the wif.encode should match key.privateWif?

Poor performance when decrypting 2+ keys

Hey,
I'd like to know why it takes about 2-3s to decrypt one key, but then it gets a lot worse for subsequent keys (12-15s). Is it possible to improve that?

That happens either running with Node or Web browser.

var bip38 = require("bip38");
var keyList = ["6PnQYMjJ7ZXpX8rbh8b6PfzK38WG5KL9mGqscB2RXFGb4ZKwXBaP5gMmhC", "6PnPNwvsTy3jeogyENrV23F15om4ecbEmFZcYrjrTsPgvKEwfnubVEAEEc"];
var password = "123";

for (var key of keyList) {
  var t0 = new Date().getTime();
  bip38.decrypt(key, password);
  console.log((new Date().getTime() - t0) + " ms");
}

Is there way to recognize wrong passphrase?

When I use wrong passphrase decrypt() returns wif which is correct, because I can generate public address from that wif.
But address is wrong, because passphrase is wrong.

Is there way to recognize wrong passphrase?

v2.0 interface changes

Related to #4

  • consider removing coinstring and only keeping raw methods, rename raw methods to encrypt/decrypt
  • consider removing class instantiation, pass script params as optional param
  • consider returning from raw privKey instead of privateKey

TypeError: Cannot read property 'copy' of undefined

Decode function have a problem.
The code is:

var bip38 = require('bip38')
var wif = require('wif')
var encryptedKey = '6PRVWUbkzzsbcVac2qwfssoUJAN1Xhrg6bNk8J7Nzm5H7kxEbn2Nh2ZoGg'
var decryptedKey = bip38.decrypt(encryptedKey, 'TestingOneTwoThree', function (status) {
    //console.log(status.percent) // will print the precent every time current increases by 1000
})
console.log(wif.encode(decryptedKey.privateKey, decryptedKey.compressed))

The error is:

$ node test.js 
/home/grio/temp/node_modules/wif/index.js:33
  privateKey.copy(result, 1)
            ^

TypeError: Cannot read property 'copy' of undefined
    at encodeRaw (/home/grio/temp/node_modules/wif/index.js:33:13)
    at Object.encode (/home/grio/temp/node_modules/wif/index.js:50:5)
    at Object.<anonymous> (/home/grio/temp/test.js:8:17)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:389:7)

Is there a way to verify incorrect Encrypted key/passphrase pair?

Im using 6PfQu77ygVyJLZjfvMLyhLMQbYnu5uguoJJ4kMCLqWwPEdfpwANVS76gTX from https://en.bitcoin.it/wiki/BIP_0038

minimal example with incorrect password aaa

var bip38 = require('bip38')
var wif = require('wif')

var encryptedKey = '6PfQu77ygVyJLZjfvMLyhLMQbYnu5uguoJJ4kMCLqWwPEdfpwANVS76gTX'
var decryptedKey = bip38.decrypt(encryptedKey, 'aaa', function (status) {
//        console.log(status.percent) // will print the percent every time current increases by 1000
})

console.log(wif.encode(0x80, decryptedKey.privateKey, decryptedKey.compressed))

produces 5JdGEnZHRWpGRijtAeZ1pUcgKQhn7bSQcBqjcMZUN3pxKjHPYR6 without throwing exception or returning false or anything.

correct password TestingOneTwoThree produces correct output

This might be EC multiply, no compression, no lot/sequence numbers specific, as on other examples lib does throws an exception.

Example code

Please tell me the js code is working for the decrypt bip38 function because the code from the current version README.md is not working.
#29
#30

No support for SegWit addresses

6PYRFsPKrpyk2qdz4Sm4QSrWzMKpNy6LCoCZJ8z5gtmubk3kG9P6Vft9dz
password 12345

It fails because it's a SegWit address.
Decrypted key is KxQdS9UeCDv8stcfADiMMttVXgNvFAnwhrGhseDMjUHXW2Unxaoz. Address ​bc1qkjntlwk9m99xj6j2tdpgzefu6uku0k88k94q6a

From BlueWallet/BlueWallet#4156

Cracking a BIP38 password, with many BIP38 samples

Given

  1. a wallet.dat file with ~100 addresses.
  2. 40 addresses in the same wallet, with addr, pub keys, encrypted private keys AND unencrypted private keys
  3. mkey with: {crypted key, salt, and nDeriveIterations, etc)
  4. Anything else we need from pywallet, etc

Read in a pwd file list, and compare to known values... to reverse engineer pwd? Most tools assume someone does not have some of the unencrypted keys in the wallet. Would be good for recovery...

We have come across a situation were we have some private keys but not all in a single encrypted wallet (backups) that used the same pwd.

Scryptsy library complications

Hello, I'm trying to include bip38 in one of my react-native projects, but since it relies on scryptsy which relies on the node module crypto which react-native does not have access to, it is failing.

Edit: I am mistaken, thanks for responses.

new Error('Invalid checksum')

Still have a problem.
Code is:
`var bip38 = require('bip38')
var wif = require('wif')
var encryptedKey = Buffer.from('6PRVWUbkzzsbcVac2qwfssoUJAN1Xhrg6bNk8J7Nzm5H7kxEbn2Nh2ZoGg', 'hex')
var decryptedKey = bip38.decrypt(encryptedKey, 'TestingOneTwoThree', function (status) {
//console.log(status.percent) // will print the precent every time current increases by 1000
})

console.log(wif.encode(decryptedKey.privateKey, decryptedKey.compressed))
`
Error is:

`$ node de.js
/home/grio/temp/node_modules/bs58check/index.js:46
if (!payload) throw new Error('Invalid checksum')
^

Error: Invalid checksum
at Object.decode (/home/grio/temp/node_modules/bs58check/index.js:46:23)
at Object.decrypt (/home/grio/temp/node_modules/bip38/index.js:130:31)
at Object. (/home/grio/temp/de.js:4:26)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:389:7)
`

TypeError: Bip38 is not a constructor

bip38 is installed by
npm install bip38
└─┬ [email protected]
└── [email protected]

test.js is

var Bip38 = require('bip38');

var privateKeyWif = '5KN7MzqK5wt2TP1fQCYyHBtDrXdJuXbUzm4A9rKAteGu3Qi5CVR';

var bip38 = new Bip38();
bip38.version = {private: 0x80, public: 0x0};
bip38.encrypt(privateKeyWif, "super-secret", "1Jq6MksXQVWzrznvZzxkV6oY57oWXD9TXB");

error is

var bip38 = new Bip38();
            ^

TypeError: Bip38 is not a constructor

Slower than bitaddress.org?

Hi! I'm seeing this library run about 3x slower than bitaddress.org. Just curious if others have seen the same and if there's any way / plans to make it run faster while still being BIP38 compliant. Thanks!

new Bip38?

In the readme file, in the decrypt section, we have the following line:

var bip38 = new Bip38()

where to get Bip38? where the first line is var bip38=require("bip38");

Doesn't seem to work

I tried running BIP38 encryption to few test keys but I can't get it to work. On Node side it just times out and on browser (Chrome 35) it throws an error:

Error: Trying to read beyond buffer length
at assert (http://0.0.0.0:3000/assets/bip38/bip38.bundle.js?body=1:3943:20)
at readUInt32 (http://0.0.0.0:3000/assets/bip38/bip38.bundle.js?body=1:3351:5)
at Uint8Array.Buffer.readUInt32BE (http://0.0.0.0:3000/assets/bip38/bip38.bundle.js?body=1:3384:10)

Move to bitcoinjs organization

As discussed a while back, if you're still up to it.

This repository is already in a crypto related organization, but, I still think it'd be worth putting them in one place.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.