Add download/verify script

This commit is contained in:
Kirill Fomichev 2017-03-15 09:57:19 +03:00
parent ef755ad62e
commit ba6efa59a0
4 changed files with 64 additions and 2 deletions

View File

@ -7,7 +7,8 @@
"coverage": "nyc --branches 100 --functions 100 --check-coverage npm run unit", "coverage": "nyc --branches 100 --functions 100 --check-coverage npm run unit",
"standard": "standard", "standard": "standard",
"test": "npm run standard && npm run unit", "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", "author": "Wei Lu",
"contributors": [ "contributors": [
@ -29,6 +30,7 @@
"unorm": "^1.3.3" "unorm": "^1.3.3"
}, },
"devDependencies": { "devDependencies": {
"node-fetch": "^1.6.3",
"nyc": "^8.3.0", "nyc": "^8.3.0",
"proxyquire": "^1.7.10", "proxyquire": "^1.7.10",
"standard": "*", "standard": "*",

View File

@ -1,5 +1,6 @@
var bip39 = require('../') var bip39 = require('../')
var proxyquire = require('proxyquire') var proxyquire = require('proxyquire')
var download = require('../util/wordlists').download
var WORDLISTS = { var WORDLISTS = {
english: require('../wordlists/english.json'), english: require('../wordlists/english.json'),
japanese: require('../wordlists/japanese.json'), japanese: require('../wordlists/japanese.json'),
@ -116,3 +117,13 @@ test('exposes standard wordlists', function (t) {
t.same(bip39.wordlists.EN, WORDLISTS.english) t.same(bip39.wordlists.EN, WORDLISTS.english)
t.equal(bip39.wordlists.EN.length, 2048) 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()
})
})

49
util/wordlists.js Normal file
View File

@ -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 }

View File

@ -1,4 +1,4 @@
[ [
"abaisser", "abaisser",
"abandon", "abandon",
"abdiquer", "abdiquer",