bip39#validateMnemonic TypeScript Examples

The following examples show how to use bip39#validateMnemonic. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: mnemonic.ts    From nautilus-wallet with MIT License 6 votes vote down vote up
function validator(words: []) {
  if (isEmpty(words)) {
    return false;
  }

  try {
    return validateMnemonic(join(words, " "));
  } catch (e) {
    return false;
  }
}
Example #2
Source File: index.ts    From oasis-wallet-web with Apache License 2.0 6 votes vote down vote up
slice = createSlice({
  name: 'createWallet',
  initialState,
  reducers: {
    /**
     * Generate a new mnemonic
     */
    generateMnemonic(state, action: PayloadAction<void>) {
      state.mnemonic = hdkey.HDKey.generateMnemonic(256).split(' ')

      // Verify there's no drift between HDKey.generateMnemonic and bip39
      if (!validateMnemonic(state.mnemonic.join(' '))) {
        throw new Error('Generated mnemonic is not valid')
      }
      state.checkbox = false
    },
    setChecked(state, action: PayloadAction<boolean>) {
      state.checkbox = action.payload
    },
    clear(state, action: PayloadAction<void>) {
      state.mnemonic = []
      state.checkbox = false
    },
  },
})
Example #3
Source File: utils.ts    From ldk with MIT License 6 votes vote down vote up
export function checkMnemonic(mnemonic: string, language?: string) {
  if (language) setDefaultWordlist(language);
  if (!validateMnemonic(mnemonic)) throw new Error('Mnemonic is not valid.');
}
Example #4
Source File: mnemonic-phrase.service.ts    From xBull-Wallet with GNU Affero General Public License v3.0 5 votes vote down vote up
validateMnemonicPhrase(text: string): boolean {
    return validateMnemonic(text, this.getWordList());
  }
Example #5
Source File: validation.ts    From no-bad-chihuahua with MIT License 5 votes vote down vote up
mnemonicValidation = async (input: string) => {
  if (!validateMnemonic(input)) {
    return 'Invalid mnemonic';
  }
  return true;
}
Example #6
Source File: mnemonic.ts    From marina with MIT License 5 votes vote down vote up
export function createMnemonic(mnemo: string): Mnemonic {
  // Trim start-end and replace multiple spaces in between with a single space
  const mnemonic = mnemo.trim().replace(/ +(?= )/g, '');

  if (!validateMnemonic(mnemonic)) throw new Error(INVALID_MNEMONIC_ERROR);
  return mnemonic;
}
Example #7
Source File: index.tsx    From oasis-wallet-web with Apache License 2.0 4 votes vote down vote up
export function MnemonicValidation(props: Props) {
  const { t } = useTranslation()

  const [rawMnemonic, setRawMnemonic] = React.useState('')
  const [mnemonicIsValid, setMnemonicIsValid] = React.useState(true)
  const size = React.useContext(ResponsiveContext)

  const mnemonic = rawMnemonic.trim().replace(/[ \n]+/g, ' ')
  const onChange = (event: React.ChangeEvent<HTMLTextAreaElement>) => {
    setRawMnemonic(event.target.value)
  }
  const onSubmit = () => {
    const isValid = validateMnemonic(mnemonic)
    setMnemonicIsValid(isValid)

    if (isValid) {
      props.successHandler(mnemonic)
    }
  }

  return (
    <Box
      background="background-front"
      margin="small"
      pad="medium"
      round="5px"
      border={{ color: 'background-front-border', size: '1px' }}
    >
      <Grid gap="small" pad="small" columns={size === 'small' ? '100%' : ['1fr', '1fr']}>
        <Box margin={{ left: 'small', vertical: 'small', right: 'large' }}>
          <Form>
            <Heading margin="0">{t('openWallet.mnemonic.header', 'Enter your keyphrase')}</Heading>
            <Paragraph>
              {t(
                'openWallet.mnemonic.instruction',
                'Enter all your keyphrase words below separated by spaces. Most keyphrases are made of either 24 or 12 words.',
              )}
            </Paragraph>
            <Box border={false}>
              <FormField
                htmlFor="mnemonic"
                error={
                  mnemonicIsValid === false
                    ? t(
                        'openWallet.mnemonic.error',
                        'Invalid keyphrase. Please make sure to input the words in the right order, all lowercase.',
                      )
                    : ''
                }
              >
                <Box border={false}>
                  <TextArea
                    id="mnemonic"
                    data-testid="mnemonic"
                    placeholder={t('openWallet.mnemonic.enterPhraseHere', 'Enter your keyphrase here')}
                    size="medium"
                    rows={5}
                    value={rawMnemonic}
                    onChange={onChange}
                    fill
                  />
                </Box>
              </FormField>
            </Box>
            <Box direction="row" gap="small" margin={{ top: 'medium' }}>
              <Button
                type="submit"
                label={t('openWallet.mnemonic.import', 'Import my wallet')}
                style={{ borderRadius: '4px' }}
                primary
                onClick={onSubmit}
              />
              {props.abortHandler && (
                <Button
                  label={t('common.cancel', 'Cancel')}
                  style={{ borderRadius: '4px' }}
                  secondary
                  onClick={props.abortHandler}
                />
              )}
            </Box>
          </Form>
        </Box>
        <Box background="background-contrast">
          <MnemonicGrid mnemonic={mnemonic.split(' ')} />
        </Box>
      </Grid>
    </Box>
  )
}