ethers.js/lib.commonjs/wordlists/wordlist-owl.js

70 lines
2.3 KiB
JavaScript
Raw Permalink Normal View History

2022-09-05 16:57:11 -04:00
"use strict";
// Use the encode-latin.js script to create the necessary
// data files to be consumed by this class
Object.defineProperty(exports, "__esModule", { value: true });
exports.WordlistOwl = void 0;
2022-09-15 22:58:45 -04:00
const index_js_1 = require("../hash/index.js");
const index_js_2 = require("../utils/index.js");
2022-09-05 16:57:11 -04:00
const decode_owl_js_1 = require("./decode-owl.js");
const wordlist_js_1 = require("./wordlist.js");
2022-11-30 15:44:23 -05:00
/**
* An OWL format Wordlist is an encoding method that exploits
* the general locality of alphabetically sorted words to
* achieve a simple but effective means of compression.
*
* This class is generally not useful to most developers as
* it is used mainly internally to keep Wordlists for languages
* based on ASCII-7 small.
*
* If necessary, there are tools within the ``generation/`` folder
2023-06-01 17:52:58 -04:00
* to create the necessary data.
2022-11-30 15:44:23 -05:00
*/
2022-09-05 16:57:11 -04:00
class WordlistOwl extends wordlist_js_1.Wordlist {
#data;
#checksum;
2022-11-30 15:44:23 -05:00
/**
* Creates a new Wordlist for %%locale%% using the OWL %%data%%
* and validated against the %%checksum%%.
*/
2022-09-05 16:57:11 -04:00
constructor(locale, data, checksum) {
super(locale);
this.#data = data;
this.#checksum = checksum;
this.#words = null;
}
2023-06-01 17:52:58 -04:00
/**
* The OWL-encoded data.
*/
2022-09-05 16:57:11 -04:00
get _data() { return this.#data; }
2023-06-01 17:52:58 -04:00
/**
* Decode all the words for the wordlist.
*/
2022-09-05 16:57:11 -04:00
_decodeWords() {
return (0, decode_owl_js_1.decodeOwl)(this.#data);
}
#words;
#loadWords() {
if (this.#words == null) {
const words = this._decodeWords();
// Verify the computed list matches the official list
2022-09-15 22:58:45 -04:00
const checksum = (0, index_js_1.id)(words.join("\n") + "\n");
2022-09-05 16:57:11 -04:00
/* c8 ignore start */
if (checksum !== this.#checksum) {
throw new Error(`BIP39 Wordlist for ${this.locale} FAILED`);
}
/* c8 ignore stop */
this.#words = words;
}
return this.#words;
}
getWord(index) {
const words = this.#loadWords();
2022-11-09 02:57:02 -05:00
(0, index_js_2.assertArgument)(index >= 0 && index < words.length, `invalid word index: ${index}`, "index", index);
2022-09-05 16:57:11 -04:00
return words[index];
}
getWordIndex(word) {
return this.#loadWords().indexOf(word);
}
}
exports.WordlistOwl = WordlistOwl;
//# sourceMappingURL=wordlist-owl.js.map