diff --git a/package.json b/package.json index c0cf9d5..a1f5740 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,8 @@ "coverage": "nyc --branches 100 --functions 100 --check-coverage npm run unit", "standard": "standard", "test": "npm run standard && npm run unit", - "unit": "tape test/*.js" + "unit": "tape test/*.js", + "update": "node -e \"require('./util/wordlists').update()\"" }, "author": "Wei Lu", "contributors": [ @@ -29,6 +30,7 @@ "unorm": "^1.3.3" }, "devDependencies": { + "node-fetch": "^1.6.3", "nyc": "^8.3.0", "proxyquire": "^1.7.10", "standard": "*", diff --git a/test/index.js b/test/index.js index c73d304..26bfe05 100644 --- a/test/index.js +++ b/test/index.js @@ -1,5 +1,6 @@ var bip39 = require('../') var proxyquire = require('proxyquire') +var download = require('../util/wordlists').download var WORDLISTS = { english: require('../wordlists/english.json'), japanese: require('../wordlists/japanese.json'), @@ -116,3 +117,13 @@ test('exposes standard wordlists', function (t) { t.same(bip39.wordlists.EN, WORDLISTS.english) t.equal(bip39.wordlists.EN.length, 2048) }) + +test('verify wordlists from https://github.com/bitcoin/bips/blob/master/bip-0039/bip-0039-wordlists.md', function (t) { + download().then(function (wordlists) { + Object.keys(wordlists).forEach(function (name) { + t.same(bip39.wordlists[name], wordlists[name]) + }) + + t.end() + }) +}) diff --git a/util/wordlists.js b/util/wordlists.js new file mode 100644 index 0000000..339308b --- /dev/null +++ b/util/wordlists.js @@ -0,0 +1,49 @@ +var fetch = require('node-fetch') +var fs = require('fs') +var path = require('path') + +var log = console.log +var WORDLISTS = ['english', 'french', 'italian', 'japanese', 'spanish'] + +function update () { + download().then(function (wordlists) { + var promises = Object.keys(wordlists).map(function (name) { return save(name, wordlists[name]) }) + return Promise.all(promises) + }) +} + +function download () { + var wordlists = {} + + var promises = WORDLISTS.map(function (name) { + return fetchRaw(name).then(toJSON).then(function (wordlist) { wordlists[name] = wordlist }) + }) + + return Promise.all(promises).then(function () { return wordlists }) +} + +function fetchRaw (name) { + var url = 'https://raw.githubusercontent.com/bitcoin/bips/master/bip-0039/' + name + '.txt' + log('download ' + url) + + return fetch(url).then(function (response) { return response.text() }) +} + +function toJSON (content) { + return content.trim().split('\n').map(function (word) { return word.trim() }) +} + +function save (name, wordlist) { + var location = path.join(__dirname, '..', 'wordlists', name + '.json') + var content = JSON.stringify(wordlist, null, 2) + '\n' + log('save ' + wordlist.length + ' words to ' + location) + + return new Promise(function (resolve, reject) { + fs.writeFile(location, content, function (err) { + if (err) reject(err) + else resolve() + }) + }) +} + +module.exports = { update: update, download: download } diff --git a/wordlists/french.json b/wordlists/french.json index 2f2b524..04ebdb2 100644 --- a/wordlists/french.json +++ b/wordlists/french.json @@ -1,4 +1,4 @@ -[ +[ "abaisser", "abandon", "abdiquer",