How to use bip39 - 10 common examples

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 bitcoinjs / bitcoinjs-lib / test / integration / bip32.spec.ts View on Github external
it('can export a BIP32 xpriv, then import it', () => {
    const mnemonic =
      'praise you muffin lion enable neck grocery crumble super myself license ghost';
    const seed = bip39.mnemonicToSeedSync(mnemonic);
    const node = bip32.fromSeed(seed);
    const strng = node.toBase58();
    const restored = bip32.fromBase58(strng);

    assert.strictEqual(getAddress(node), getAddress(restored)); // same public key
    assert.strictEqual(node.toWIF(), restored.toWIF()); // same private key
  });
github oed / seedsplit / index.js View on Github external
const bip39 = require('bip39')
const seedsplit = require('./lib/seedsplit')

//let m1 = bip39.generateMnemonic(256) // 24 words
let m1 = bip39.generateMnemonic() // 12 words

console.log(m1)

let sm = seedsplit.split(m1, 3, 2)

sm.then((x) => console.log(x))

let m2 = sm.then((x) => seedsplit.combine(x.slice(1)))

m2.then((x) => {
  console.log(x)
  console.log('Is correct:', m1 === x)
})
github cronoh / nanovault / src / app / components / configure-wallet / configure-wallet.component.ts View on Github external
async createNewWallet() {
    // If a wallet already exists, confirm that the seed is saved
    const confirmed = await this.confirmWalletOverwrite();
    if (!confirmed) return;

    const newSeed = this.walletService.createNewWallet();
    this.newWalletSeed = newSeed;
    this.newWalletMnemonic = bip.entropyToMnemonic(newSeed);

    // Split the seed up so we can show 4 per line
    const words = this.newWalletMnemonic.split(' ');
    const lines = [
      words.slice(0, 4),
      words.slice(4, 8),
      words.slice(8, 12),
      words.slice(12, 16),
      words.slice(16, 20),
      words.slice(20, 24),
    ];
    this.newWalletMnemonicLines = lines;

    this.activePanel = 3;
    this.notifications.sendSuccess(`Successfully created new wallet! Make sure to write down your seed!`);
  }
github oed / seedsplit / lib / seedsplit.js View on Github external
function split(seed, numShards, threshold) {
  if (threshold > numShards) {
    throw new Error('Threshold can\'t be larger than the number of shards')
  }
  let ent = bip39.mnemonicToEntropy(seed)
  let shards = ssss.share(ent, numShards, threshold)

  let shardMnemonics = shards.map(shard => {
    let padding = '0'.repeat(numPaddingZeros)
    return bip39.entropyToMnemonic(padding + shard)
  })
  // due to padding first word is always the same
  return shardMnemonics.map(sm => sm.split(' ').slice(1).join(' '))
}
github brave / crypto / index.js View on Github external
toBytes32: function (passphrase/* : string */) {
    passphrase = passphrase.trim().replace(/\s+/gi, ' ')
    const words = passphrase.split(' ')
    if (words.length === module.exports.passphrase.NICEWARE_32_BYTE_WORD_COUNT) {
      return new Uint8Array(niceware.passphraseToBytes(words))
    } else if (words.length === module.exports.passphrase.BIP39_32_BYTE_WORD_COUNT) {
      return module.exports.hexToUint8(bip39.mnemonicToEntropy(passphrase))
    } else {
      throw new Error(`Input words length ${words.length} is not 24 or 16.`)
    }
  },
github TradaTech / icetea / web / wallet.js View on Github external
function toPkey (mnemonic, type) {
  console.log(mnemonic, type)
  if (!validateMnemonic(mnemonic)) {
    throw new Error('Wrong seed format. Seed must be 12 words.')
  }

  const seed = mnemonicToSeedSync(mnemonic)
  const hdkey = fromMasterSeed(seed).derive('m’/44’/349’/0’/0')

  let pkey, found
  for (let i = 0; !found; i++) {
    if (i > 100) {
      // there must be something wrong, because the ratio of regular account is 50%
      throw new Error('Too many tries deriving regular account from seed.')
    }
    pkey = hdkey.deriveChild(i).privateKey
    const { address } = ecc.toPubKeyAndAddress(pkey)
    found = codec.isAddressType(address, type)
  }

  return pkey
}
github Paratii-Video / paratii-js / src / paratii.eth.wallet.js View on Github external
async function createFromMnemonic (numberOfAccounts, mnemonic) {
    if (this.length > 0) {
      throw Error('This wallet has already been created!')
    }
    if (numberOfAccounts === undefined) {
      numberOfAccounts = 1
    }
    if (mnemonic === undefined) {
      mnemonic = bip39.generateMnemonic()
    }

    if (bip39.validateMnemonic(mnemonic)) {
      let seed = bip39.mnemonicToSeed(mnemonic)
      // contains masternode extended priv key and extended pub key
      let masternode = hdkey.fromMasterSeed(seed)

      for (let i = 0; i < numberOfAccounts; ++i) {
        // m / purpose' / coin_type' / account' / change / address_index
        let child = masternode.derive(`m/44'/60'/0'/0/${i}`)
        let privkeyHex = child.privateKey.toString('hex')
        var privateKey = this._accounts.privateKeyToAccount(add0x(privkeyHex)).privateKey
        this.add(privateKey)
      }
    } else {
      throw Error(`Mnemonic was not valid: ${mnemonic}`)
    }
    return this
  }
github ninjadotorg / handshake-app / src / services / Wallets / Ethereum.js View on Github external
createAddressPrivatekey() {
    const t0 = performance.now();

    if (this.mnemonic == '') {
      this.mnemonic = bip39.generateMnemonic(); // generates string
    }
    const seed = bip39.mnemonicToSeed(this.mnemonic); // creates seed buffer
    const root = hdkey.fromMasterSeed(seed);

    // Create address for eth ...
    const addrNode = root.derive(StringHelper.format('m/44\'/{0}\'/0\'/0/0', this.coinType));

    const pubKey = ethUtil.privateToPublic(addrNode._privateKey);
    const addr = ethUtil.publicToAddress(pubKey).toString('hex');
    const address = ethUtil.toChecksumAddress(addr);
    const privateKey = addrNode._privateKey.toString('hex');

    this.address = address;
    this.privateKey = privateKey;

    this.chainId = this.network == Ethereum.Network.Mainnet ? 1 : 4;

    const t1 = performance.now();
github BlueWallet / BlueWallet / class / hd-legacy-breadwallet-wallet.js View on Github external
getXpub() {
    if (this._xpub) {
      return this._xpub; // cache hit
    }
    const mnemonic = this.secret;
    const seed = bip39.mnemonicToSeed(mnemonic);
    const root = bip32.fromSeed(seed);

    const path = "m/0'";
    const child = root.derivePath(path).neutered();
    this._xpub = child.toBase58();

    return this._xpub;
  }
github BlueWallet / BlueWallet / class / hd-legacy-p2pkh-wallet.js View on Github external
getXpub() {
    if (this._xpub) {
      return this._xpub; // cache hit
    }
    const mnemonic = this.secret;
    const seed = bip39.mnemonicToSeed(mnemonic);
    const root = bitcoin.bip32.fromSeed(seed);

    const path = "m/44'/0'/0'";
    const child = root.derivePath(path).neutered();
    this._xpub = child.toBase58();

    return this._xpub;
  }