6

I have the JSON file containing the keypair which has an array containing 64 values:

$ cat ~/my-solana-wallet/demo.json
[4,182,130,247,119,117,227,207,112,73,170,126,222,197,244,99,215,107,255,202,33,43,36,17,104,111,157,246,196,192,174,95,240,23,238,206,118,215,154,238,229,96,11,37,156,123,51,223,5,231,17,117,86,136,103,14,75,95,175,132,148,54,1,46]  

How to convert this array to the corresponding public and private key seed phrase?

6 Answers 6

8

The first 32 values represent the private key:

private_key_bytes = [4, 182, 130, 247, 119, 117, 227, 207, 112, 73, 170, 126, 222, 197, 244, 99, 215, 107, 255, 202, 33, 43, 36, 17, 104, 111, 157, 246, 196, 192, 174, 95]

The second 32 values represent the public key:

In order to change this in alphanumeric phrases, we need to convert this array of integer values into base 58. In Ruby we’ll first convert the array to hex (base 16):

public_key_hex = public_key_bytes.pack("C*").unpack("H*").first

And then we can use a base 58 script to convert the hex into the base 58:

public_key = Base58.encode(public_key_hex)

And that gives us the same public key aka wallet address that we saw earlier in the Solana CLI output:

HAE1oNnc3XBmPudphRcHhyCvGShtgDYtZVzx2MocKEr1

We can follow the same process to get the base 58 private key:

KQ3cGFBdjJuRsB7U1K4to6cTGBPhgukqPgsi5pryr8v
0
1

You can use the method fromSecretKey:

fromSecretKey method

1
  • Should follow with then retrieving private and public keys from keypair.
    – Frank C.
    Commented Dec 7, 2022 at 9:06
1

In python you can do

import solana
import base58
from solana.keypair import Keypair

//Generate keypair object
key_array = [4,182,130,247,119,117,227,207,112,73,170,126,222,197,244,99,215,107,255,202,33,43,36,17,104,111,157,246,196,192,174,95,240,23,238,206,118,215,154,238,229,96,11,37,156,123,51,223,5,231,17,117,86,136,103,14,75,95,175,132,148,54,1,46]
generated_keypair = Keypair.from_secret_key(bytes(key_array))

generated_keypair.public_key //Public Key
base58.b58encode(generated_keypair.secret_key) //Raw 64-byte secret key

https://michaelhly.github.io/solana-py/core/keypair/

0

The answer from ZeroNine doesn't work anymore; tested with solana.py v0.32.

Here's a version without solana.py:

import base58

key_array = [4,182,130,247,119,117,227,207,112,73,170,126,222,197,244,99,215,107,255,202,33,43,36,17,104,111,157,246,196,192,174,95,240,23,238,206,118,215,154,238,229,96,11,37,156,123,51,223,5,231,17,117,86,136,103,14,75,95,175,132,148,54,1,46]

secret_key = key_array[0:32]
public_key = key_array[32:64]

sk = base58.b58encode(bytes(secret_key))
pk = base58.b58encode(bytes(public_key))

print(pk)
0

Here is a more typescript version solution

    import { bs58 } from "@coral-xyz/anchor/dist/cjs/utils/bytes";
const Main = () => {
  const keyArray = [
    4, 182, 130, 247, 119, 117, 227, 207, 112, 73, 170, 126, 222, 197, 244, 99,
    215, 107, 255, 202, 33, 43, 36, 17, 104, 111, 157, 246, 196, 192, 174, 95,
    240, 23, 238, 206, 118, 215, 154, 238, 229, 96, 11, 37, 156, 123, 51, 223,
    5, 231, 17, 117, 86, 136, 103, 14, 75, 95, 175, 132, 148, 54, 1, 46,
  ];
  const secretKey = keyArray.slice(0, 32);
  const publicKey = keyArray.slice(32, 64);

  const sk = bs58.encode(Buffer.from(secretKey));
  const pk = bs58.encode(Buffer.from(publicKey));

  console.log("Public Key:", pk);
  console.log("Secret Key:", sk);
};
Main();
0

import base58 from solana.rpc.api import Keypair

key_array = [141, 12, ...] # insert the full array keypair = Keypair.from_bytes(key_array)

print(f"Keypair is: {keypair}")

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.