How to use the bip39.validateMnemonic function in bip39

To help you get started, we’ve selected a few bip39 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 blockstack / blockstack-browser / app / js / utils / workers / decrypt.main.js View on Github external
.digest()
    .toString('hex')

  const hmacDigestHash = crypto
    .createHash('sha256')
    .update(hmacDigest)
    .digest()
    .toString('hex')

  if (hmacSigHash !== hmacDigestHash) {
    // not authentic
    throw new Error('Wrong password (HMAC mismatch)')
  }

  const mnemonic = await denormalizeMnemonic(plaintext)
  if (!bip39.validateMnemonic(mnemonic)) {
    throw new Error('Wrong password (invalid plaintext)')
  }

  return mnemonic
}
github rate-engineering / rate3-monorepo / packages / wallet / WalletManager.js View on Github external
switch (numberOfWords) {
            case 12:
              return 128;
            case 18:
              return 160;
            case 21:
              return 224;
            case 24:
              return 256;
            default:
              return 128;
          }
        }(arguments[0]));
        this.seed = bip39.generateMnemonic(strength);
      } else if (typeof (arguments[0]) === 'string') {
        if (bip39.validateMnemonic(arguments[0])) {
          this.seed = arguments[0];
        } else {
          this.seed = null;
          console.log('The input is not a set of valid seed phrases.');
        }
      } else {
        this.seed = null;
        console.log('The number of seed phrases must be one of 12, 18, 21, 24.');
      }
    } else {
      console.log('The argument must be empty, number of seed phrases, or valid seed phrases.');
    }
    return this.seed;
  }
github Jasonhcwong / lnswaps / btc_blockchain_task.js View on Github external
default:
    logger.fatal('Unsupported blockchain:', LNSWAP_CHAIN);
    process.exit();
}
const bitcoinjsNetwork = networks[LNSWAP_CHAIN];
const [partA, partB] = LNSWAP_CHAIN_RPC_API.split('@');
const [rpcUsername, rpcPassword] = partA.split(':');
const [rpcHost, rpcPort] = partB.split(':');

if (!LNSWAP_CLAIM_ADDRESS) {
  logger.fatal('Please set variable LNSWAP_CLAIM_ADDRESS');
  process.exit();
}

if (!validateMnemonic(LNSWAP_CLAIM_BIP39_SEED)) {
  logger.fatal('ExpectedValidMnemonic');
  process.exit();
}
const seed = mnemonicToSeed(LNSWAP_CLAIM_BIP39_SEED);
const root = HDNode.fromSeedBuffer(seed, bitcoinjsNetwork);

const zmqRawBlockSocket = zmq.socket('sub');
const zmqRawTxSocket = zmq.socket('sub');

let currentBlockHeight = 0;

function getAddressesFromOuts(outs) {
  const outputAddresses = [];
  outs.forEach(({ script, value }, index) => {
    try {
      const address = bitcoin.address.fromOutputScript(script, bitcoinjsNetwork);
github blocktrail / wallet-recovery-tool / src / libs / blocktrail-sdk / lib / webworker.js View on Github external
(function() {
                    try {
                        var mnemonic = data.mnemonic;
                        var passphrase = data.passphrase;

                        if (!bip39.validateMnemonic(mnemonic)) {
                            e = new Error('Invalid passphrase');
                            e.id = data.id;
                            throw e;
                        }
                        var seed = bip39.mnemonicToSeedHex(mnemonic, passphrase);

                        self.postMessage({id: data.id, seed: seed, mnemonic: mnemonic});
                    } catch (e) {
                        e.id = data.id;
                        throw e;
                    }
                })();
            break;
github ontio / ontology-ts-sdk / src / account.ts View on Github external
static importWithMnemonic(
        label: string,
        mnemonic: string,
        password: string,
        params?: ScryptParams
    ): Account {
        mnemonic = mnemonic.trim();
        if (!bip39.validateMnemonic(mnemonic)) {
            throw ERROR_CODE.INVALID_PARAMS;
        }
        const seed = bip39.mnemonicToSeedHex(mnemonic);
        const hdkey = HDKey.fromMasterSeed(Buffer.from(seed, 'hex'));
        const pri = hdkey.derive(ONT_BIP44_PATH);
        const key = Buffer.from(pri.privateKey).toString('hex');
        const privateKey = new PrivateKey(key);
        const account = Account.create(privateKey, password, label, params);
        return account;
    }
github jolocom / smartwallet-app / src / ui / recovery / container / inputSeedPhrase.tsx View on Github external
private selectWord = (word: string): void => {
    const { mnemonic, markedWord } = this.state
    const isLastWord = markedWord === mnemonic.length
    mnemonic[markedWord] = word
    const mnemonicValid =
      mnemonic.length === 12 && validateMnemonic(mnemonic.join(' '))
    if (mnemonicValid && this.textInput) {
      this.textInput.blur()
    }
    this.setState({
      inputValue: isLastWord ? '' : word,
      mnemonic,
      markedWord: isLastWord ? mnemonic.length : markedWord,
      isMnemonicValid: mnemonicValid,
      suggestions: [],
      inputState: WordState.editing,
    })
  }
github vitelabs / vite.js / src / wallet / hdKey.ts View on Github external
export function validateMnemonics(mnemonic: string, wordlist: Array = bip39.wordlists.EN): Boolean {
    return mnemonic && bip39.validateMnemonic(mnemonic, wordlist);
}
github airgap-it / airgap-vault / src / models / BIP39Signer.ts View on Github external
static determineWordList(mnemonic: string): any[] {
    for (const list of BIP39Signer.wordLists()) {
      if (bip39.validateMnemonic(BIP39Signer.prepareMnemonic(mnemonic), list)) {
        return list
      }
    }
  }
github TronLink / tronlink-extension / app / popup / src / pages / Import / MnemonicPhrase / index.js View on Github external
hasInvalidWords() {
        return !bip39.validateMnemonic(this.state.wordList);
    }