Better error message when normalize is missing.

This commit is contained in:
Richard Moore 2018-11-09 14:34:14 -05:00
parent 7bfaf292db
commit fad902b438
No known key found for this signature in database
GPG Key ID: 525F70A6FCABC295
6 changed files with 21 additions and 4 deletions

View File

@ -122,3 +122,13 @@ export function setCensorship(censorship: boolean, permanent?: boolean): void {
_censorErrors = !!censorship;
_permanentCensorErrors = !!permanent;
}
export function checkNormalize(): void {
try {
if (String.fromCharCode(0xe9).normalize('NFD') !== String.fromCharCode(0x65, 0x0301)) {
throw new Error('broken')
}
} catch (error) {
throwError('platform missing String.prototype.normalize', UNSUPPORTED_OPERATION, { operation: 'String.prototype.normalize' });
}
}

View File

@ -201,6 +201,8 @@ export function mnemonicToSeed(mnemonic: string, password?: string): string {
export function mnemonicToEntropy(mnemonic: string, wordlist?: Wordlist): string {
if (!wordlist) { wordlist = langEn; }
errors.checkNormalize();
var words = wordlist.split(mnemonic);
if ((words.length % 3) !== 0) { throw new Error('invalid mnemonic'); }

View File

@ -1,7 +1,7 @@
'use strict';
import { HashZero } from '../constants';
import { checkNormalize } from '../errors';
import { arrayify, concat, hexlify } from './bytes';
///////////////////////////////
@ -23,6 +23,7 @@ export enum UnicodeNormalizationForm {
export function toUtf8Bytes(str: string, form: UnicodeNormalizationForm = UnicodeNormalizationForm.current): Uint8Array {
if (form != UnicodeNormalizationForm.current) {
checkNormalize();
str = str.normalize(form);
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -132,9 +132,7 @@ class LangJa extends Wordlist {
}
split(mnemonic: string): Array<string> {
if (!mnemonic.normalize) {
errors.throwError('Japanese is unsupported on this platform; missing String.prototype.normalize', errors.UNSUPPORTED_OPERATION, { operation: 'String.prototype.normalize' });
}
errors.checkNormalize();
return mnemonic.split(/(?:\u3000| )+/g);
}