{"size": 769, "ext": "js", "max_stars_repo_path": "lib/converter.js", "max_stars_repo_name": "acyuta108/iota-bip39", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "lib/converter.js", "max_issues_repo_name": "acyuta108/iota-bip39", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "lib/converter.js", "max_forks_repo_name": "acyuta108/iota-bip39", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 32.0416666667, "max_line_length": 80, "alphanum_fraction": 0.7087126138}
const bip39 = require("bip39");
const iotaCrypto = require('iota.lib.js/lib/crypto/converter/converter');
const BigNumber = require("bignumber.js");

const toTrytes = function(mnemonic, passphrase) {
    if (!bip39.validateMnemonic(mnemonic)) {
        throw new Error('The Mnemonic you provided is not valid!: ' + mnemonic);
    }
    const pass = passphrase ? passphrase.toString() : undefined
    const seedHex = bip39.mnemonicToSeedHex(mnemonic, pass);
    const seedDec = new BigNumber(parseInt(seedHex, 16), 10);
    const seedTrits = iotaCrypto.fromValue(seedDec);
    return iotaCrypto.trytes(seedTrits);
}

const toSeed = function(mnemonic, passphrase) {
    return toTrytes(mnemonic, passphrase).slice(0, 81);
}

module.exports = {
    toTrytes,
    toSeed
}
