Skip to content

Commit

Permalink
Merge pull request #16 from thehobbit85/master
Browse files Browse the repository at this point in the history
Some minor fixes and adding progressCallback functionality
  • Loading branch information
jprichardson committed Nov 3, 2015
2 parents f8a95c5 + a5b6e04 commit dd86946
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 20 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
node_modules
logs
coverage
node_modules
*.rdb
*.log
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
bip38
=====

[![build status](https://secure.travis-ci.org/cryptocoinjs/bip38.svg)](http://travis-ci.org/cryptocoinjs/bip38)
[![build status](https://secure.travis-ci.org/bitcoinjs/bip38.svg)](http://travis-ci.org/bitcoinjs/bip38)
[![Coverage Status](https://img.shields.io/coveralls/cryptocoinjs/bip38.svg)](https://coveralls.io/r/cryptocoinjs/bip38)
[![Version](http://img.shields.io/npm/v/bip38.svg)](https://www.npmjs.org/package/bip38)

[![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard)

A JavaScript component that adheres to the [BIP38](https://github.com/bitcoin/bips/blob/master/bip-0038.mediawiki) standard to secure your crypto currency private keys. Fully compliant with Node.js and the browser (via Browserify).


Expand Down Expand Up @@ -80,9 +82,11 @@ bip38.scryptParams = {
```


### encrypt(wif, passphrase, address)
### encrypt(wif, passphrase, address, progressCallback)

A method that encrypts the private key. `wif` is the string value of the wallet import format key. `passphrase` the passphrase to encrypt the key with. `address` is the public address.
`progressCallback` is a function that receives an object in the form of:
{current: 1000, total: 262144, percent: 0.3814697265625}


Returns the encrypted string.
Expand All @@ -95,15 +99,19 @@ var Bip38 = require('bip38')
var privateKeyWif = '5KN7MzqK5wt2TP1fQCYyHBtDrXdJuXbUzm4A9rKAteGu3Qi5CVR'

var bip38 = new Bip38()
var encrypted = bip38.encrypt(privateKeyWif, 'TestingOneTwoThree', "1Jq6MksXQVWzrznvZzxkV6oY57oWXD9TXB")
var encrypted = bip38.encrypt(privateKeyWif, 'TestingOneTwoThree', "1Jq6MksXQVWzrznvZzxkV6oY57oWXD9TXB", function (status) {
console.log(status.percent) // Will print the precent every time current increases by 1000
})
console.log(encrypted)
// => 6PRVWUbkzzsbcVac2qwfssoUJAN1Xhrg6bNk8J7Nzm5H7kxEbn2Nh2ZoGg
```


### decrypt(encryptedKey, passhprase)
### decrypt(encryptedKey, passhprase, progressCallback)

A method that decrypts the encrypted string. `encryptedKey` is the string value of the encrypted key. `passphrase` is the passphrase to decrypt the key with.
`progressCallback` is a function that receives an object in the form of:
{current: 1000, total: 262144, percent: 0.3814697265625}


```js
Expand All @@ -112,7 +120,9 @@ var Bip38 = require('bip38')
var encryptedKey = '6PRVWUbkzzsbcVac2qwfssoUJAN1Xhrg6bNk8J7Nzm5H7kxEbn2Nh2ZoGg'

var bip38 = new Bip38()
var privateKeyWif = bip38.decrypt(encryptedKey, 'TestingOneTwoThree')
var privateKeyWif = bip38.decrypt(encryptedKey, 'TestingOneTwoThree', function (status) {
console.log(status.percent) // Will print the precent every time current increases by 1000
})
console.log(privateKeyWif)
// => '5KN7MzqK5wt2TP1fQCYyHBtDrXdJuXbUzm4A9rKAteGu3Qi5CVR'
```
Expand Down
24 changes: 11 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function Bip38 (versions) {
}
}

Bip38.prototype.encryptRaw = function (buffer, compressed, passphrase, saltAddress) {
Bip38.prototype.encryptRaw = function (buffer, compressed, passphrase, saltAddress, progressCallback) {
assert.equal(buffer.length, 32, 'Invalid private key length')

var secret = new Buffer(passphrase, 'utf8')
Expand All @@ -40,7 +40,7 @@ Bip38.prototype.encryptRaw = function (buffer, compressed, passphrase, saltAddre
var r = this.scryptParams.r
var p = this.scryptParams.p

var scryptBuf = scrypt(secret, salt, N, r, p, 64)
var scryptBuf = scrypt(secret, salt, N, r, p, 64, progressCallback)
var derivedHalf1 = scryptBuf.slice(0, 32)
var derivedHalf2 = scryptBuf.slice(32, 64)

Expand All @@ -61,7 +61,7 @@ Bip38.prototype.encryptRaw = function (buffer, compressed, passphrase, saltAddre
return Buffer.concat([prefix, salt, cipherText])
}

Bip38.prototype.encrypt = function (wif, passphrase, saltAddress) {
Bip38.prototype.encrypt = function (wif, passphrase, saltAddress, progressCallback) {
var d = cs.decode(wif).slice(1)
var compressed = (d.length === 33) && (d[32] === 0x01)

Expand All @@ -70,12 +70,12 @@ Bip38.prototype.encrypt = function (wif, passphrase, saltAddress) {
d = d.slice(0, -1)
}

return cs.encode(this.encryptRaw(d, compressed, passphrase, saltAddress))
return cs.encode(this.encryptRaw(d, compressed, passphrase, saltAddress, progressCallback))
}

// some of the techniques borrowed from: https://github.com/pointbiz/bitaddress.org
// todo: (optimization) init buffer in advance, and use copy instead of concat
Bip38.prototype.decryptRaw = function (encData, passphrase) {
Bip38.prototype.decryptRaw = function (encData, passphrase, progressCallback) {
// 39 bytes: 2 bytes prefix, 37 bytes payload
assert.equal(encData.length, 39, 'Invalid BIP38 data length')

Expand All @@ -85,7 +85,7 @@ Bip38.prototype.decryptRaw = function (encData, passphrase) {
// check if BIP38 EC multiply
var type = encData.readUInt8(1)
if (type === 0x43) {
return this.decryptECMult(encData, passphrase)
return this.decryptECMult(encData, passphrase, progressCallback)
}

passphrase = new Buffer(passphrase, 'utf8')
Expand All @@ -103,7 +103,7 @@ Bip38.prototype.decryptRaw = function (encData, passphrase) {
var p = this.scryptParams.p

var addresshash = encData.slice(3, 7)
var scryptBuf = scrypt(passphrase, addresshash, N, r, p, 64)
var scryptBuf = scrypt(passphrase, addresshash, N, r, p, 64, progressCallback)
var derivedHalf1 = scryptBuf.slice(0, 32)
var derivedHalf2 = scryptBuf.slice(32, 64)

Expand All @@ -121,9 +121,9 @@ Bip38.prototype.decryptRaw = function (encData, passphrase) {
}
}

Bip38.prototype.decrypt = function (encryptedBase58, passphrase) {
Bip38.prototype.decrypt = function (encryptedBase58, passphrase, progressCallback) {
var encBuffer = cs.decode(encryptedBase58)
var decrypt = this.decryptRaw(encBuffer, passphrase)
var decrypt = this.decryptRaw(encBuffer, passphrase, progressCallback)

// Convert to WIF
var bufferLen = decrypt.compressed ? 34 : 33
Expand All @@ -139,7 +139,7 @@ Bip38.prototype.decrypt = function (encryptedBase58, passphrase) {
return cs.encode(buffer)
}

Bip38.prototype.decryptECMult = function (encData, passphrase) {
Bip38.prototype.decryptECMult = function (encData, passphrase, progressCallback) {
passphrase = new Buffer(passphrase, 'utf8')
encData = encData.slice(1) // FIXME: we can avoid this

Expand Down Expand Up @@ -167,13 +167,12 @@ Bip38.prototype.decryptECMult = function (encData, passphrase) {
var N = this.scryptParams.N
var r = this.scryptParams.r
var p = this.scryptParams.p
var preFactor = scrypt(passphrase, ownerSalt, N, r, p, 32)
var preFactor = scrypt(passphrase, ownerSalt, N, r, p, 32, progressCallback)

var passFactor
if (hasLotSeq) {
var hashTarget = Buffer.concat([preFactor, ownerEntropy])
passFactor = sha256x2(hashTarget)

} else {
passFactor = preFactor
}
Expand Down Expand Up @@ -232,7 +231,6 @@ Bip38.prototype.verify = function (encryptedBase58) {
// EC mult
} else if (type === 0x43) {
if ((flag & ~0x24)) return false

} else {
return false
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"scripts": {
"browser-test": "mochify --wd -R spec --timeout 100000",
"coverage": "istanbul cover _mocha -- --reporter list test/*.js",
"coveralls": "npm run-script coverage && node coveralls < coverage/lcov.info",
"coveralls": "npm run-script coverage && coveralls < coverage/lcov.info",
"standard": "standard",
"test": "npm run standard && npm run unit",
"unit": "mocha --ui bdd --timeout 120000"
Expand Down

0 comments on commit dd86946

Please sign in to comment.