Wallet entropy using bip39 - derive seed and build wallet using nodejs

I created a new wallet using Lace wallet. And I got 24 words. Now I would like to know how I can derive the seed from 24 words in nodejs. I was using bip39 and was able to build my wallet. Here is my code:
const entropy = bip39.mnemonicToEntropy(my_mnemonic, wordlist);
seed = Buffer.from(entropy).toString('hex');

But right now the code is not working anymore. It returns a different wallet address. It worked fine in the past.

Thanks

1 Like

I encounter the same problem. How to get seed from 24 words that were generate by Lace wallet preview. I want to pass this seed to WalletBuilder.build function from @midnight-ntwrk/wallet

I was able to share this issue with someone from Midnight. He isn’t a dev, but will try to get some help for this issue (derive seed from mnemonic words)

1 Like

I was investigating this some weeks ago, I’m not fully sure if this is a valid way of doing it, and I couldn’t get it to properly derive the correct seed, but this is what I was attempting without success:

import * as bip39 from '@scure/bip39';
import { WalletBuilder } from '@midnight-ntwrk/wallet';
import { createHash, createHmac } from 'crypto';

const seed64 = await bip39.mnemonicToSeed(mnemonic, '');
const hmac = createHmac('sha512', seed64);
hmac.update(Buffer.from(''));
const hmacResult = hmac.digest();
const masterPrivateKey = hmacResult.slice(0, 32);
const seed = masterPrivateKey.toString('hex');
const wallet = await WalletBuilder.build(.., seed, ...);

I found this:
https://www.npmjs.com/package/@midnight-ntwrk/wallet-sdk-hd

I don’t see an API in that lib to get the seed from a given mnemonic.

With the help from Midnight team and Lace, I learned how to derive it:
Use BIP39 library: https://www.npmjs.com/package/bip39

const mnemonic = ''abandon car water …. “;  // your mnemonic
const seed = bip39.mnemonicToSeedSync(mnemonic);
const generatedWallet = HDWallet.fromSeed(seed);

let finalSeed;
if (generatedWallet.type == 'seedOk') {

  const zswapKey =  generatedWallet.hdWallet.selectAccount(0).selectRole(Roles.Zswap).deriveKeyAt(0);

  if (zswapKey.type === 'keyDerived') {
     finalSeed = Buffer.from(zswapKey.key).toString('hex')
  } else {
    console.error('Error deriving key');
  }

} else {
  console.error('Error generating HDWallet');
}

await WalletBuilder.build(
this.indexerUrl,
this.indexerWsUrl,
this.proofServer,
this.node,
finalSeed || '',
getZswapNetworkId(),
'info',
 );
1 Like

I was going to comment on something, but then I realised that this solution is effective! great job guys :slight_smile: