/
validation.ts
151 lines (136 loc) · 5.43 KB
/
validation.ts
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
/*
* 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('')[
// eslint-disable-next-line no-unexpected-multiline
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 = 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.toString()} words instead of expected ${expectedWords.toString()}. Please check the passphrase.`,
};
const whiteSpaceError: PassphraseError = {
actual: whiteSpacesInPassphrase,
code: 'INVALID_AMOUNT_OF_WHITESPACES',
expected: expectedWhitespaces,
location: locateConsecutiveWhitespaces(passphrase),
message: `Passphrase contains ${whiteSpacesInPassphrase.toString()} whitespaces instead of expected ${expectedWhitespaces.toString()}. Please check the passphrase.`,
};
const uppercaseCharacterError: PassphraseError = {
actual: uppercaseCharacterInPassphrase,
code: 'INVALID_AMOUNT_OF_UPPERCASE_CHARACTER',
expected: expectedUppercaseCharacterCount,
location: locateUppercaseCharacters(passphrase),
message: `Passphrase contains ${uppercaseCharacterInPassphrase.toString()} uppercase character instead of expected ${expectedUppercaseCharacterCount.toString()}. 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;
},
[],
);
};