mirror of
https://github.com/status-im/bip39.git
synced 2025-02-03 08:13:42 +00:00
use safe-buffer
This commit is contained in:
parent
e29c2f2ec9
commit
2efba14c03
9
index.js
9
index.js
@ -1,3 +1,4 @@
|
|||||||
|
var Buffer = require('safe-buffer').Buffer
|
||||||
var createHash = require('create-hash')
|
var createHash = require('create-hash')
|
||||||
var pbkdf2 = require('pbkdf2').pbkdf2Sync
|
var pbkdf2 = require('pbkdf2').pbkdf2Sync
|
||||||
var randomBytes = require('randombytes')
|
var randomBytes = require('randombytes')
|
||||||
@ -44,8 +45,8 @@ function salt (password) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function mnemonicToSeed (mnemonic, password) {
|
function mnemonicToSeed (mnemonic, password) {
|
||||||
var mnemonicBuffer = new Buffer(unorm.nfkd(mnemonic), 'utf8')
|
var mnemonicBuffer = Buffer.from(unorm.nfkd(mnemonic), 'utf8')
|
||||||
var saltBuffer = new Buffer(salt(unorm.nfkd(password)), 'utf8')
|
var saltBuffer = Buffer.from(salt(unorm.nfkd(password)), 'utf8')
|
||||||
|
|
||||||
return pbkdf2(mnemonicBuffer, saltBuffer, 2048, 64, 'sha512')
|
return pbkdf2(mnemonicBuffer, saltBuffer, 2048, 64, 'sha512')
|
||||||
}
|
}
|
||||||
@ -79,7 +80,7 @@ function mnemonicToEntropy (mnemonic, wordlist) {
|
|||||||
if (entropyBytes.length > 32) throw new Error(INVALID_ENTROPY)
|
if (entropyBytes.length > 32) throw new Error(INVALID_ENTROPY)
|
||||||
if (entropyBytes.length % 4 !== 0) throw new Error(INVALID_ENTROPY)
|
if (entropyBytes.length % 4 !== 0) throw new Error(INVALID_ENTROPY)
|
||||||
|
|
||||||
var entropy = new Buffer(entropyBytes)
|
var entropy = Buffer.from(entropyBytes)
|
||||||
var newChecksum = deriveChecksumBits(entropy)
|
var newChecksum = deriveChecksumBits(entropy)
|
||||||
if (newChecksum !== checksumBits) throw new Error(INVALID_CHECKSUM)
|
if (newChecksum !== checksumBits) throw new Error(INVALID_CHECKSUM)
|
||||||
|
|
||||||
@ -96,7 +97,7 @@ function entropyToMnemonic (entropyHex, wordlist) {
|
|||||||
// multiple of 4
|
// multiple of 4
|
||||||
if (entropyHex.length % 8 !== 0) throw new TypeError(INVALID_ENTROPY)
|
if (entropyHex.length % 8 !== 0) throw new TypeError(INVALID_ENTROPY)
|
||||||
|
|
||||||
var entropy = new Buffer(entropyHex, 'hex')
|
var entropy = Buffer.from(entropyHex, 'hex')
|
||||||
var entropyBits = bytesToBinary([].slice.call(entropy))
|
var entropyBits = bytesToBinary([].slice.call(entropy))
|
||||||
var checksumBits = deriveChecksumBits(entropy)
|
var checksumBits = deriveChecksumBits(entropy)
|
||||||
|
|
||||||
|
@ -27,6 +27,7 @@
|
|||||||
"create-hash": "^1.1.0",
|
"create-hash": "^1.1.0",
|
||||||
"pbkdf2": "^3.0.9",
|
"pbkdf2": "^3.0.9",
|
||||||
"randombytes": "^2.0.1",
|
"randombytes": "^2.0.1",
|
||||||
|
"safe-buffer": "^5.0.1",
|
||||||
"unorm": "^1.3.3"
|
"unorm": "^1.3.3"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
var bip39 = require('../')
|
var bip39 = require('../')
|
||||||
|
var Buffer = require('safe-buffer').Buffer
|
||||||
var download = require('../util/wordlists').download
|
var download = require('../util/wordlists').download
|
||||||
var WORDLISTS = {
|
var WORDLISTS = {
|
||||||
english: require('../wordlists/english.json'),
|
english: require('../wordlists/english.json'),
|
||||||
@ -21,7 +22,7 @@ function testVector (description, wordlist, password, v, i) {
|
|||||||
t.equal(bip39.mnemonicToSeedHex(vmnemonic, password), vseedHex, 'mnemonicToSeedHex returns ' + vseedHex.slice(0, 40) + '...')
|
t.equal(bip39.mnemonicToSeedHex(vmnemonic, password), vseedHex, 'mnemonicToSeedHex returns ' + vseedHex.slice(0, 40) + '...')
|
||||||
t.equal(bip39.entropyToMnemonic(ventropy, wordlist), vmnemonic, 'entropyToMnemonic returns ' + vmnemonic.slice(0, 40) + '...')
|
t.equal(bip39.entropyToMnemonic(ventropy, wordlist), vmnemonic, 'entropyToMnemonic returns ' + vmnemonic.slice(0, 40) + '...')
|
||||||
|
|
||||||
function rng () { return new Buffer(ventropy, 'hex') }
|
function rng () { return Buffer.from(ventropy, 'hex') }
|
||||||
t.equal(bip39.generateMnemonic(undefined, rng, wordlist), vmnemonic, 'generateMnemonic returns RNG entropy unmodified')
|
t.equal(bip39.generateMnemonic(undefined, rng, wordlist), vmnemonic, 'generateMnemonic returns RNG entropy unmodified')
|
||||||
t.equal(bip39.validateMnemonic(vmnemonic, wordlist), true, 'validateMnemonic returns true')
|
t.equal(bip39.validateMnemonic(vmnemonic, wordlist), true, 'validateMnemonic returns true')
|
||||||
})
|
})
|
||||||
@ -35,15 +36,15 @@ test('invalid entropy', function (t) {
|
|||||||
t.plan(3)
|
t.plan(3)
|
||||||
|
|
||||||
t.throws(function () {
|
t.throws(function () {
|
||||||
bip39.entropyToMnemonic(new Buffer('', 'hex'))
|
bip39.entropyToMnemonic(Buffer.from('', 'hex'))
|
||||||
}, /^TypeError: Invalid entropy$/, 'throws for empty entropy')
|
}, /^TypeError: Invalid entropy$/, 'throws for empty entropy')
|
||||||
|
|
||||||
t.throws(function () {
|
t.throws(function () {
|
||||||
bip39.entropyToMnemonic(new Buffer('000000', 'hex'))
|
bip39.entropyToMnemonic(Buffer.from('000000', 'hex'))
|
||||||
}, /^TypeError: Invalid entropy$/, 'throws for entropy that\'s not a multitude of 4 bytes')
|
}, /^TypeError: Invalid entropy$/, 'throws for entropy that\'s not a multitude of 4 bytes')
|
||||||
|
|
||||||
t.throws(function () {
|
t.throws(function () {
|
||||||
bip39.entropyToMnemonic(new Buffer(new Array(1028 + 1).join('00'), 'hex'))
|
bip39.entropyToMnemonic(Buffer.from(new Array(1028 + 1).join('00'), 'hex'))
|
||||||
}, /^TypeError: Invalid entropy$/, 'throws for entropy that is larger than 1024')
|
}, /^TypeError: Invalid entropy$/, 'throws for entropy that is larger than 1024')
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -74,7 +75,7 @@ test('generateMnemonic requests the exact amount of data from an RNG', function
|
|||||||
|
|
||||||
bip39.generateMnemonic(160, function (size) {
|
bip39.generateMnemonic(160, function (size) {
|
||||||
t.equal(size, 160 / 8)
|
t.equal(size, 160 / 8)
|
||||||
return new Buffer(size)
|
return Buffer.allocUnsafe(size)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user