import { FormControl } from '@angular/forms' ; import * as bip39 from 'bip39' ; import { BIPSigner } from '../models/BIP39Signer' ; export class MnemonicValidator { public static checkMnemonic ( mnemonic , wordlist ) { const words = BIPSigner . prepareMnemonic ( mnemonic ) . split ( ' ' ) ; if ( words . length % 3 !== 0 ) { throw new Error ( 'invalid secret' ) ; } words . forEach ( ( word ) => { const index = wordlist . indexOf ( word ) ; if ( index === - 1 ) { throw new Error ( 'invalid secret' ) ; } } ) ; } public static isValidShare ( control ) { try { MnemonicValidator . checkMnemonic ( control . value , bip39 . wordlists . EN as any ) ; } catch ( e ) { return { 'not a social mnemonic share' : true } ; } return null ; } public static isValid ( control ) { if ( control . value && BIPSigner . validateMnemonic ( control . value ) ) { return null ; } return { 'not a mnemonic' : true } ; } }