'How to convert mnemonic to 32 byte seed?
I have a 24-word mnemonic, and I want to convert it to public and private keys.
This is how I did it:
const hex = HexCoder.instance;
final seed = bip39.mnemonicToSeedHex(_mnmonic);
final algorithm = Ed25519();
// The hex.decode(seed) have 64 bytes lengths.
final keyPair = await algorithm.newKeyPairFromSeed(hex.decode(seed));
final newPublicKey = await keyPair.extractPublicKey();
But I get this error:
ArgumentError (Invalid argument(s): Seed must have 32 bytes)
What am I missing?
Solution 1:[1]
Update
Use sha256:
import 'package:crypto/crypto.dart' show sha256;
final seed = bip39.mnemonicToSeed(mnemonic);
final digest = sha256.convert(seed); // 32 bytes
Old answer
Here are some examples how people generate keys from mnemonics:
final seed = bip39.mnemonicToSeed(mnemonic);
final root = bip32.BIP32.fromSeed(seed);
getAddress(root.derivePath("m/0'/0/0"));
Here is another example:
Uint8List seed = await compute(bip39.mnemonicToSeed, mnemonic);
bip32.BIP32 masterNode = bip32.BIP32.fromSeed(seed);
String hdPath = "m/44'/12586'/$accountIndex'/0/0";
bip32.BIP32 child0 = masterNode.derivePath(hdPath);
Uint8List rawPrivateKey = child0.privateKey!;
rawPrivateKey[0] &= 0x3f;
final privateKeyHex = HEX.encode(rawPrivateKey);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |