Hi
I want to use my solana private key retrieved with the following request:
const solanaWallet = new SolanaWallet(provider)
const connectionConfig = await solanaWallet.request({
method: "solana_provider_config",
params: [],
});
const privateKey = await provider.request({
method: "solanaPrivateKey"
});
What is the format of the private key? it should be base58 encoded? when i try to decode it to base58, i have the following error:
Non-base58 character
What is the format of the retrieved private key? is it possible to convert it to a keypair with the following instruction:
Keypair.fromSecretKey(privateKey);
Regards,
vjgee
February 23, 2024, 3:26am
2
@ysissoko78 Thanks for your recent post.
The format is Base58
To convert a base58 Solana private key into a key pair in TypeScript, you can use the @solana/web3.js
library and follow this approach:
import { Keypair } from '@solana/web3.js';
import bs58 from 'bs58';
function base58ToKeypair(base58PrivateKey: string): Keypair {
try {
const privateKeyBuffer = bs58.decode(base58PrivateKey);
return Keypair.fromSecretKey(privateKeyBuffer);
} catch (error) {
throw new Error("Invalid base58 private key.");
}
}
// Example usage
const base58PrivateKey = "your_base58_private_key_here"; // Replace with actual base58 private key
const keypair = base58ToKeypair(base58PrivateKey);
console.log(`Public Key: ${keypair.publicKey.toBase58()}`); //prints the base58-encoded public key
console.log(`Private Key (Base58): ${keypair.secretKey.toBase58()}`); // prints the base58-encoded private key
You can also take a look at our example :
You can get the private key from here:
const privateKey = await rpc.getPrivateKey();
and here the wallet address
const solana_address = await solanaWallet.requestAccounts();
Thanks for your reply,
The getPrivateKey
method is from your class EthereumRpc, it is not working for Solana (because this is a different rpc provider for solana). I am using the following request to get the solana private key:
const privateKey = await provider.request({
method: "solanaPrivateKey"
});
I am getting a 128 bytes length string but not a 32 bytes base58 encoded string (i have this “Non-base58 character” error while trying to convert it). Can you clarify what is this 128 bytes length string?
vjgee
February 23, 2024, 7:43am
4
I will check with our Dev team and get back with an update.
1 Like
vjgee
February 23, 2024, 3:38pm
5
ysissoko78:
128 bytes length string
@ysissoko78 I believe this is a 128-byte hexadecimal string. Can you try encoding this to Base58 and then decode it:
import bs58 from "bs58";
const base58privatekey= bs58.encode(Buffer.from("enter private key", "hex"))
1 Like
The following code works for me:
Keypair.fromSecret(Buffer.from("enter private key", "hex"))
It was just encoded in hex format.
Thanks a lot!
1 Like
system
Closed
February 25, 2024, 4:36pm
7
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.