How to use the @polkadot/util-crypto.mnemonicValidate function in @polkadot/util-crypto

To help you get started, we’ve selected a few @polkadot/util-crypto examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github polkadot-js / common / docs / examples / util-crypto / 03_mnemonic_generate_validate_bip39 / index.js View on Github external
async function main () {
  // Create mnemonic string for Alice using BIP39
  const mnemonicAlice = mnemonicGenerate();

  console.log(`Generated mnemonic: ${mnemonicAlice}`);

  // Validate the mnemic string that was generated
  const isValidMnemonic = mnemonicValidate(mnemonicAlice);

  console.log(`isValidMnemonic: ${isValidMnemonic}`);

  // Create valid seed from mnemonic as u8a and convert it to a string
  // FIXME - Replace with mnemonicToSeed once exposed
  const seedAlice = mnemonicToSeed(mnemonicAlice);

  // Generate new public/secret keypair for Alice from the supplied seed
  const { secretKey, publicKey } = naclKeypairFromSeed(seedAlice);

  // Encrypt, Sign and Validate the message. See Example 'Sign & Verify Message'
}
github polkadot-js / extension / packages / extension / src / background / handlers / Extension.ts View on Github external
private seedValidate ({ suri, type }: RequestSeedValidate): ResponseSeedValidate {
    const { phrase } = keyExtractSuri(suri);

    if (isHex(phrase)) {
      assert(isHex(phrase, 256), 'Hex seed needs to be 256-bits');
    } else {
      // sadly isHex detects as string, so we need a cast here
      assert(SEED_LENGTHS.includes((phrase as string).split(' ').length), `Mnemonic needs to contain ${SEED_LENGTHS.join(', ')} words`);
      assert(mnemonicValidate(phrase), 'Not a valid mnemonic seed');
    }

    return {
      address: keyring.createFromUri(suri, {}, type).address,
      suri
    };
  }
github polkadot-js / apps / packages / app-staking / src / Actions / Account / InjectKeys.tsx View on Github external
useEffect((): void => {
    try {
      const { phrase } = keyExtractSuri(suri);

      assert(mnemonicValidate(phrase), 'Invalid mnemonic phrase');

      setPublicKey(u8aToHex(keyring.createFromUri(suri, {}, crypto).publicKey));
    } catch (error) {
      setPublicKey(EMPTY_KEY);
    }
  }, [crypto, suri]);
github polkadot-js / apps / packages / app-accounts / src / modals / Create.tsx View on Github external
function updateAddress (seed: string, derivePath: string, seedType: SeedType, pairType: KeypairType): AddressState {
  const deriveError = deriveValidate(seed, derivePath, pairType);
  let isSeedValid = seedType === 'raw'
    ? rawValidate(seed)
    : mnemonicValidate(seed);
  let address: string | null = null;

  if (!deriveError && isSeedValid) {
    try {
      address = addressFromSeed(seed, derivePath, pairType);
    } catch (error) {
      isSeedValid = false;
    }
  }

  return {
    address,
    deriveError,
    derivePath,
    isSeedValid,
    pairType,
github polkadot-js / apps / packages / app-accounts / src / modals / Create.tsx View on Github external
function updateAddress (seed: string, derivePath: string, seedType: SeedType, pairType: KeypairType): AddressState {
  const deriveError = deriveValidate(seed, derivePath, pairType);
  let isSeedValid = seedType === 'raw'
    ? rawValidate(seed)
    : mnemonicValidate(seed);
  let address: string | null = null;

  if (!deriveError && isSeedValid) {
    try {
      address = addressFromSeed(seed, derivePath, pairType);
    } catch (error) {
      isSeedValid = false;
    }
  }

  return {
    address,
    deriveError,
    derivePath,
    isSeedValid,
    pairType,
github w3f / polkadot-deployer / lib / network / crypto.js View on Github external
function generateValidMnemonic() {
  const maxCount = 3;
  let count = 0;
  let isValidMnemonic = false;
  let mnemonic;

  while (!isValidMnemonic) {
    if (count > maxCount) {
      throw new Error('could not generate valid mnemonic!');
    }
    mnemonic = mnemonicGenerate();
    isValidMnemonic = mnemonicValidate(mnemonic);
    count++;
  }
  return mnemonic;
}