How to generate a private key for a wallet
cannon-lz opened this issue · 4 comments
cannon-lz commented
I was wondering how I should generate a private key for my wallet using the solana dart library?
final Ed25519HDKeyPair wallet = await Wallet.fromSeedWithHdPath(seed: seed, hdPath: "m/44'/501'/0'/0'");
I can't get my private key after I create a wallet using the code above?how can I export my private key for backing up my wallet?Please help me
ookami-kb commented
You can use extract()
method:
final privateKey = (await wallet.extract()).bytes;
cannon-lz commented
ookami-kb commented
Phantom calls it private key but it actually expects private key + public key, you can get it like this:
import 'package:solana/base58.dart';
import 'package:solana/solana.dart';
Future<void> main() async {
final account = await Ed25519HDKeyPair.random();
final public = account.publicKey.bytes;
final private = (await account.extract()).bytes;
print(base58encode(private + public));
}
cannon-lz commented
Phantom calls it private key but it actually expects private key + public key, you can get it like this:
import 'package:solana/base58.dart'; import 'package:solana/solana.dart'; Future<void> main() async { final account = await Ed25519HDKeyPair.random(); final public = account.publicKey.bytes; final private = (await account.extract()).bytes; print(base58encode(private + public)); }
thank you, this solved my issue