如何将助记符转换为32个字节种子?

发布于 2025-01-28 10:50:13 字数 461 浏览 5 评论 0原文

我有一个24个字的助记符,我想将其转换为公共和私钥。

这就是我这样做的方式:

  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();

但是我得到了这个错误:

参数(无效参数:种子必须具有32个字节)

我缺少什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谈情不如逗狗 2025-02-04 10:50:13

更新

使用sha256:

import 'package:crypto/crypto.dart' show sha256;

final seed = bip39.mnemonicToSeed(mnemonic);
final digest = sha256.convert(seed); // 32 bytes

旧答案

final seed = bip39.mnemonicToSeed(mnemonic);
final root = bip32.BIP32.fromSeed(seed);
getAddress(root.derivePath("m/0'/0/0"));

这里是另一个例子:

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);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文