3

I'm trying to encode a privatekey into bs58 and login into wallets but it says its not in the correct format. However i'm encoding into bs58 and even decoding to make sure i'm getting the same original bytes, wich according to the logs are correctly decoding and encoding, here is my snippet

use solana_sdk::signature::{Keypair, Signer};
use bs58;

fn main() {
    println!("Hello, world!");

    let keypair = Keypair::new();
    let public_key = keypair.pubkey();
    let secret_key = keypair.secret().to_bytes();
    let bs58_encode = bs58::encode(secret_key).into_string();
    let bs58_decode = bs58::decode(bs58_encode.clone()).into_vec();

    println!("Chave Pública: {}", public_key);
    println!("Chave Privada: {:?}", secret_key);
    println!("bs58: {:?}", bs58_encode);
    println!("bs58 decode: {:?}", bs58_decode);
}
    Chave Pública: 5cBhgaZ6Tjq8AhGWjPeGavdqv6nh6mQnab9g23LUgmKV
    Chave Privada: [190, 254, 81, 191, 188, 251, 2, 110, 124, 255, 154, 228, 12, 147, 48, 115, 138, 224, 49, 33, 213, 236, 238, 139, 148, 114, 85, 177, 208, 82, 32, 60]
    bs58: "DrZPGNoMRaP9snVhjLRZEgLKcQHvzNJ2JBZCiK9UJeS7"
    bs58 decode: Ok([190, 254, 81, 191, 188, 251, 2, 110, 124, 255, 154, 228, 12, 147, 48, 115, 138, 224, 49, 33, 213, 236, 238, 139, 148, 114, 85, 177, 208, 82, 32, 60])

as you can see I'm able to get the original bytes sequence from the encoded string, so I have no idea of what's going wrong

1 Answer 1

2

Most wallets expect the secret key to actually be the private key concatenated with the public key. Try this:

fn main() {
    let keypair = Keypair::new();
    let public_key = keypair.pubkey();
    let secret_key = keypair.secret().to_bytes();
    
    let mut combined = secret_key.to_vec();
    combined.extend_from_slice(&public_key.to_bytes());
    
    // Convert the concatenated result to base58
    let bs58_encoded = bs58::encode(&combined).into_string();
    
    println!("Base58 encoded result: {}", bs58_encoded);
}

If against expectation that doesn't work, try switching around the endianess.

2
  • Thanks it worked, I tried this but unifying by hand. If you dont mind just to confirm, you: 1. encoded the pubkey to bytes 2. concatenaded both as bytes on the same vector and just then parsed the full vector to base58 Commented Aug 12 at 22:18
  • Yes your analysis is correct, that's exactly what I did. Glad it worked! Make sure to accept the question so others know this has been resolved :)
    – McBain
    Commented Aug 12 at 22:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.