espresso-cash/espresso-cash-public

How to generate a private key for a wallet

cannon-lz opened this issue · 4 comments

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'");
code_prieview

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

You can use extract() method:

final privateKey = (await wallet.extract()).bytes;

You can use extract() method:

final privateKey = (await wallet.extract()).bytes;

thanks for your help, but when I try to import the Private Key obtained using the above method to my Phantom wallet, I get an error "incorrect format"
photo_6178992664062573432_y

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

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