Skip to content

Commit

Permalink
util/
Browse files Browse the repository at this point in the history
  • Loading branch information
naftalimurgor committed Jul 23, 2023
1 parent 57e498b commit b363eaa
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions util/wordlists.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* istanbul ignore file */
var fetch = require('node-fetch')
var fs = require('fs')
var path = require('path')

var log = console.log
var WORDLISTS = [
'chinese_simplified',
'chinese_traditional',
'czech',
'english',
'french',
'italian',
'japanese',
'korean',
'portuguese',
'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, '..', 'ts_src', '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 }

0 comments on commit b363eaa

Please sign in to comment.