Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | 1x 2x 1x 52x 1x 1x 1x 1x 1x 118x 9x 8x 1x 7x 7x 4x 3x 19x 2x 17x 2x 2x 11x 101x 4x 4x 1x 1x 1x 1x 7x | "use strict"; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; Eif (mod != null) for (var k in mod) Eif (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; Object.defineProperty(exports, "__esModule", { value: true }); /** * Copyright 2019 NEM * * Licensed under the BSD 2-Clause License (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://opensource.org/licenses/BSD-2-Clause * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ const bip39 = __importStar(require("bip39")); const crypto = __importStar(require("crypto")); /** * Class `MnemonicPassPhrase` describes a mnemonic pass phrase generator * as defined by the Bitcoin BIP39 standard which can be found at following * URL: * * https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki * * This class *uses* features provided by the `bitcoinjs/bip39` package * and therefor is licensed under the BSD-2 Clause License as mentioned * [here](https://github.com/bitcoinjs/bip39/blob/master/LICENSE). * * @see https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki * @see https://github.com/bitcoinjs/bip39 * @since 0.1.0 */ class MnemonicPassPhrase { /** * Create a `MnemonicPassPhrase` instance. * * @param plain {string} */ constructor(/** * The mnemonic pass phrase (plain text). * @var {string} */ plain) { this.plain = plain; } /** * Create a random mnemonic pass phrase. Arguments to this method are * all optional, default values are static variables of this class. * * This static method returns a sentence built following the Bitcoin * BIP39 standard using the `bitcoinjs/bip39` library. * * @todo `strength` replace by `countWords` for higher level approach * * @param language {string} (Optional) The language used for the wordlist. * @param strength {number} (Optional) Strength of mnemonic pass phrase (% 32 == 0). * @param rng {function} (Optional) Random Number Generator to be used. * @return {string} Returns the mnemonic pass phrase in plain text format. * @throws {Error} On unsupported `language` argument. */ static createRandom(language = MnemonicPassPhrase.DEFAULT_LANGUAGE, strength = MnemonicPassPhrase.DEFAULT_STRENGTH, rng = MnemonicPassPhrase.CATAPULT_RNG) { // check if `language` is supported or throw MnemonicPassPhrase.assertLanguageSupported(language); // check if `strength` is BIP39 compliant if (strength % 32 !== 0 || strength < 128 || strength > 256) { throw new Error('Invalid strength, must be multiple of 32 with: 128 >= strength <= 256.'); } // set active wordlist by language Iif (language !== MnemonicPassPhrase.DEFAULT_LANGUAGE) { bip39.setDefaultWordlist(language); } return new MnemonicPassPhrase(bip39.generateMnemonic(strength, rng)); } /** * Convert an entropy value to a mnemonic pass phrase in plain text. * * If the `language` argument is ommited, the default * language 'english' will be used. * * The `bitcoinjs/bip39` package's `entropyToMnemonic` function * validates the entropy value by parsing it, then builds the mnemonic * pass phrase by retrieving and joining words from the wordlist. * * @param mnemonic {string} The mnemonic pass phrase to validate. * @param language {string} (Optional) The language used for the wordlist. * @return {string} Returns the mnemonic pass phrase in plain text format. * @throws {Error} On unsupported `language` argument. */ static createFromEntropy(entropy, language = MnemonicPassPhrase.DEFAULT_LANGUAGE) { // check if `language` is supported or throw MnemonicPassPhrase.assertLanguageSupported(language); return new MnemonicPassPhrase(bip39.entropyToMnemonic(entropy, bip39.wordlists[language])); } /** * Assert whether `language` is a supported language string, or not. * * @internal * @see https://github.com/bitcoinjs/bip39/tree/master/ts_src/wordlists * @return {boolean} True for *supported languages*, never false. * @throws {Error} On unsupported `language` argument. */ static assertLanguageSupported(language) { // check if `language` is supported or throw if (!bip39.wordlists.hasOwnProperty(language)) { throw new Error('Language "' + language + '" is not supported.'); } return true; } /** * Validate a mnemonic pass phrase with optional `language`. * * If the `language` argument is ommited, the default * language 'english' will be used. * * The `bitcoinjs/bip39` package's `validateMnemonic` function * checks the mnemonic pass phrase by internally converting * to an entropy bytes array with `mnemonicToEntropy`. * * Validation steps include `size`, `checksum bits` and `checksum` * validations. * * @param mnemonic {string} The mnemonic pass phrase to validate. * @param language {string} (Optional) The language used for the wordlist. * @return {boolean} True for *valid mnemonic*, False for *invalid mnemonic*. * @throws {Error} On unsupported `language` argument. */ isValid(language = MnemonicPassPhrase.DEFAULT_LANGUAGE) { // check if `language` is supported or throw MnemonicPassPhrase.assertLanguageSupported(language); return bip39.validateMnemonic(this.plain, bip39.wordlists[language]); } /** * Get the array representation for the mnemonic pass phrase. * * Words are split using a white-space character as a separator. * * @return {string[]} Array of plain text words */ toArray() { return this.plain.split(' '); } /** * Convert a mnemonic to an **encrypted** hexadecimal seed. * * If the `password` argument is ommited, an empty password will be assumed. * * The `bitcoinjs/bip39` package's `mnemonicToSeedSync` function * will first *normalize* the mnemonic pass phrase Buffer to * [NFKD form](https://en.wikipedia.org/wiki/Unicode_equivalence#Normal_forms). * Afterwards the buffer will be *salted* with the `password` (or empty) prepend * by the string 'mnemonic'. * In its last step, the function will then use PBKDF2 to derivate the password- * protected hexadecimal seed from the salted buffer. * * @see https://en.wikipedia.org/wiki/Unicode_equivalence#Normal_forms * @param password {string} * @return {Buffer} Buffer containing bytes of the hexadecimal seed. */ toSeed(password) { return bip39.mnemonicToSeedSync(this.plain, password || ''); } /** * Converts a mnemonic to hexadecimal entropy (of `strength` bits). * * If the `language` argument is ommited, the default * language 'english' will be used. * * The `bitcoinjs/bip39` package's `mnemonicToEntropy` function * converts words into 11 bit binary strings, then validates the * checksum and finally, returns the built entropy hexadecimal * (of `strength` bits). * * It is not recommended to store the result of this function. Please, * have a look at `mnemonicToSeed(m, pw)` instead. * * @see {MnemonicPassPhrase}#mnemonicToSeed * @param language {string} (Optional) The language used for the wordlist. * @return {string} Returns the hexadecimal format of the entropy value. * @throws {Error} On unsupported `language` argument. */ toEntropy(language = MnemonicPassPhrase.DEFAULT_LANGUAGE) { // check if `language` is supported or throw MnemonicPassPhrase.assertLanguageSupported(language); return bip39.mnemonicToEntropy(this.plain, bip39.wordlists[language]); } } exports.MnemonicPassPhrase = MnemonicPassPhrase; /** * Default wordlist language used. * * @see [List of Supported Languages](https://github.com/bitcoinjs/bip39/tree/master/ts_src/wordlists) * @var {string} */ MnemonicPassPhrase.DEFAULT_LANGUAGE = 'english'; /** * Default mnemonic strength. * * @see https://github.com/bitcoinjs/bip39/blob/master/ts_src/index.ts#L131 * @see https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki#generating-the-mnemonic * @var {number} */ MnemonicPassPhrase.DEFAULT_STRENGTH = 256; /** * Random number generator using `nacl_catapult`. * * Implicit conversion to `Buffer` is needed to comply * with `bitcoinjs/bip39`. * * @param bytes {number} Number of bytes to generate. * @return {Buffer} */ MnemonicPassPhrase.CATAPULT_RNG = (bytes) => { return Buffer.from(crypto.randomBytes(bytes).buffer); }; |