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 | 2x 2x 2x 23x 23x 2x 18x 2x 15x 15x 2x 14x 1066x 9x 1057x 2x 18x 1114x 2x 1112x 7x 1105x 4x 1101x 2x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 48x 3x 45x 6x 39x 1x 38x 6x 32x | /* * Copyright © 2019 Lisk Foundation * * See the LICENSE file at the top-level directory of this distribution * for licensing information. * * Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation, * no part of this software, including this file, may be copied, modified, * propagated, or distributed except according to the terms contained in the * LICENSE file. * * Removal or modification of this copyright notice is prohibited. * */ import * as Mnemonic from 'bip39'; interface PassphraseRegularExpression { readonly uppercaseRegExp: RegExp; readonly whitespaceRegExp: RegExp; } interface PassphraseError { readonly actual: number | boolean | string; readonly code: string; readonly expected: number | boolean | string; readonly location?: ReadonlyArray<number>; readonly message: string; } const passphraseRegularExpression: PassphraseRegularExpression = { uppercaseRegExp: /[A-Z]/g, whitespaceRegExp: /\s/g, }; export const countPassphraseWhitespaces = (passphrase: string): number => { const whitespaceMatches = passphrase.match( passphraseRegularExpression.whitespaceRegExp, ); return whitespaceMatches !== null ? whitespaceMatches.length : 0; }; export const countPassphraseWords = (passphrase: string): number => passphrase.split(' ').filter(Boolean).length; export const countUppercaseCharacters = (passphrase: string): number => { const uppercaseCharacterMatches = passphrase.match( passphraseRegularExpression.uppercaseRegExp, ); return uppercaseCharacterMatches !== null ? uppercaseCharacterMatches.length : 0; }; export const locateUppercaseCharacters = ( passphrase: string, ): ReadonlyArray<number> => passphrase .split('') .reduce( ( upperCaseIndexes: ReadonlyArray<number>, character: string, index: number, ) => { if ( character.match(passphraseRegularExpression.uppercaseRegExp) !== null ) { return [...upperCaseIndexes, index]; } return upperCaseIndexes; }, [], ); export const locateConsecutiveWhitespaces = ( passphrase: string, ): ReadonlyArray<number> => passphrase .split('') .reduce( ( whitespaceIndexes: ReadonlyArray<number>, character: string, index: number, ) => { if ( index === 0 && character.match(passphraseRegularExpression.whitespaceRegExp) !== null ) { return [...whitespaceIndexes, index]; } if ( index !== passphrase.length - 1 && character.match(passphraseRegularExpression.whitespaceRegExp) !== null && passphrase .split('') [index - 1].match(passphraseRegularExpression.whitespaceRegExp) !== null ) { return [...whitespaceIndexes, index]; } if ( index === passphrase.length - 1 && character.match(passphraseRegularExpression.whitespaceRegExp) !== null ) { return [...whitespaceIndexes, index]; } return whitespaceIndexes; }, [], ); export const getPassphraseValidationErrors = ( passphrase: string, wordlists?: ReadonlyArray<string>, expectedWords: number = 12, ): ReadonlyArray<PassphraseError> => { const expectedWhitespaces = expectedWords - 1; const expectedUppercaseCharacterCount = 0; const wordsInPassphrase = countPassphraseWords(passphrase); const whiteSpacesInPassphrase = countPassphraseWhitespaces(passphrase); const uppercaseCharacterInPassphrase = countUppercaseCharacters(passphrase); const passphraseWordError: PassphraseError = { actual: wordsInPassphrase, code: 'INVALID_AMOUNT_OF_WORDS', expected: expectedWords, message: `Passphrase contains ${wordsInPassphrase} words instead of expected ${expectedWords}. Please check the passphrase.`, }; const whiteSpaceError: PassphraseError = { actual: whiteSpacesInPassphrase, code: 'INVALID_AMOUNT_OF_WHITESPACES', expected: expectedWhitespaces, location: locateConsecutiveWhitespaces(passphrase), message: `Passphrase contains ${whiteSpacesInPassphrase} whitespaces instead of expected ${expectedWhitespaces}. Please check the passphrase.`, }; const uppercaseCharacterError: PassphraseError = { actual: uppercaseCharacterInPassphrase, code: 'INVALID_AMOUNT_OF_UPPERCASE_CHARACTER', expected: expectedUppercaseCharacterCount, location: locateUppercaseCharacters(passphrase), message: `Passphrase contains ${uppercaseCharacterInPassphrase} uppercase character instead of expected ${expectedUppercaseCharacterCount}. Please check the passphrase.`, }; const validationError: PassphraseError = { actual: false, code: 'INVALID_MNEMONIC', expected: true, message: 'Passphrase is not a valid mnemonic passphrase. Please check the passphrase.', }; const finalWordList = wordlists !== undefined ? [...wordlists] : Mnemonic.wordlists.english; return [ passphraseWordError, whiteSpaceError, uppercaseCharacterError, validationError, ].reduce( (errorArray: ReadonlyArray<PassphraseError>, error: PassphraseError) => { if ( error.code === passphraseWordError.code && wordsInPassphrase !== expectedWords ) { return [...errorArray, error]; } if ( error.code === whiteSpaceError.code && whiteSpacesInPassphrase !== expectedWhitespaces ) { return [...errorArray, error]; } if ( error.code === uppercaseCharacterError.code && uppercaseCharacterInPassphrase !== expectedUppercaseCharacterCount ) { return [...errorArray, error]; } if ( error.code === validationError.code && !Mnemonic.validateMnemonic(passphrase, finalWordList) ) { return [...errorArray, error]; } return errorArray; }, [], ); }; |