2

I have the following example private key:

4YFq9y5f5hi77Bq8kDCE6VgqoAqKGSQN87yW9YeGybpNfqKUG4WxnwhboHGUeXjY7g8262mhL1kCCM9yy8uGvdj7

It's paired with the address:

B7EZQq7AV8FnEEvEZUiFDPbfUWNE12XZv6wvn9ubFUJV

How do I import this key into Solana cli without having to install a bunch of other libraries or tools?

4
  • Most of the CLI that need a keypair (private and public) take a filepath to keyfile as option
    – Frank C.
    Commented Sep 2, 2022 at 20:20
  • can i just copy that private key into a file and it still works? Commented Sep 2, 2022 at 20:33
  • May I ask what the use case of needing to pass the base-58 version into the CLI? The JSON-string version (the 32 number JSON array) is alot of characters, but it works with stdin for feeding keypairs into the solana CLI.
    – HelmetFace
    Commented Oct 15, 2022 at 6:01
  • @HelmetFace I have a service where I save the base-58 private keys of my users and when the users process a send transaction I have a script that is suppose to take that as an input. Commented Oct 17, 2022 at 15:47

5 Answers 5

2

If you are using the Solana CLI, it is possible to pass a keypair in from stdin without writing it to a file first.

This can be demonstrated using any keypair file and doing:

$ cat my-keypair.json | solana --keypair stdin:// address
<your-address-will-appear-here>

As you can see, it just expects a byte-array, one that is identical to the contents of a typical keypair file, not a Base58 string.

Now, if I understand you correctly, you have some program that calls the Solana CLI as a subprocess, and you want to pass in the private key. As per the above, you can convert your Base58 representation of your private key into a JSON string of a byte array (e.g. [234, 10, 56, 42 ...], and then you can pass that string into stdin of your subprocess.

2

Are you comfortable with command line, node, and npm? If so, the snippet below will output the file that you can use for Solana CLI.

May sound like a lot, but I just went through this with a non-dev friend and they succeeded.

STEPS:

  1. In your command line, type node -v does this say something like v16.17.0 ? If not, you can install node.

  2. Then, create a new directory with a JS file... named convert.js for example and copy the code below into it. Don't forget to update the privatekeyexportedfromphantom value.

  3. You can then type npm i bs58 from the command-line in that directory. (installs bs58)

  4. Finally, type node convert.js and it will create a file named mykey.json

CODE:

const bs58 = require('bs58');
const fs = require('fs');
b = bs58.decode('privatekeyexportedfromphantom');
j = new Uint8Array(b.buffer, b.byteOffset, b.byteLength / Uint8Array.BYTES_PER_ELEMENT);
fs.writeFileSync('mykey.json', `[${j}]`);
1

I wrote a small Python script that converts private key into byte array. It is fairly simple and it has come in handy many times.

 python3 -m pip install base58

If you are using python3, first install base58 using the command above. You need pip to install this package.

import base58
byte_array = base58.b58decode('4YFq9y5f5hi77Bq8kDCE6VgqoAqKGSQN87yW9YeGybpNfqKUG4WxnwhboHGUeXjY7g8262mhL1kCCM9yy8uGvdj7')

json_string = "[" + ",".join(map(lambda b: str(b), byte_array)) + "]"
print(json_string)
2
  • Doesn't work. Getting errors: prnt.sc/28SOZBeHwJHi --- " ModuleNotFoundError: No module named 'base58' " Commented Sep 5, 2022 at 19:18
  • if you are using python3, please use this to install it first before you can import. python3 -m pip install base58 Commented Sep 6, 2022 at 4:53
0

As per my comment and your follow up question:

  • Many of the commands to the solana CLI take a keypair file as an argument
  • The keypair file is an array of bytes written out as a json array.
  • Decode the string from base58 into an array then use a json writer to put to a file.
0

Steps

  1. Initialize a new Node.js project (if you don't have one already):

    mkdir solana-key-import
    cd solana-key-import
    npm init -y
    
  2. Install required packages:

    npm install @solana/web3.js bs58
    
  3. Create a script to import the private key:

    Create a file named importKey.js and add the following code:

    const bs58 = require('bs58');
    const { Keypair } = require('@solana/web3.js');
    
    const privateKey = '4YFq9y5f5hi77Bq8kDCE6VgqoAqKGSQN87yW9YeGybpNfqKUG4WxnwhboHGUeXjY7g8262mhL1kCCM9yy8uGvdj7';
    
    const keypairData = Keypair.fromSeed(Uint8Array.from(bs58.default.decode(privateKey).slice(0, 32)));
    
    console.log('Public Key:', keypairData.publicKey.toString());
    console.log('Secret Key:', keypairData.secretKey.toString());
    
  4. Run the script:

    node importKey.js
    
  5. Export the private key for use with Solana CLI:

    The Solana CLI expects the private key in a JSON file. You can generate this file by modifying your script slightly to save the secret key:

    Update importKey.js:

    const fs = require('fs');
    
    const bs58 = require('bs58');
    const { Keypair } = require('@solana/web3.js');
    
    const privateKey = '4YFq9y5f5hi77Bq8kDCE6VgqoAqKGSQN87yW9YeGybpNfqKUG4WxnwhboHGUeXjY7g8262mhL1kCCM9yy8uGvdj7';
    
    const keypairData = Keypair.fromSeed(Uint8Array.from(bs58.default.decode(privateKey).slice(0, 32)));
    
    const secretKey = `[${keypairData.secretKey.toString()}]`;
    
    fs.writeFileSync('keypair.json', secretKey);
    
    console.log('Public Key:', keypairData.publicKey.toString());
    console.log('Secret Key saved to keypair.json');
    

    Run the script again:

    node importKey.js
    

    This will create a file named keypair.json with your secret key.

  6. Import the keypair into Solana CLI:

    Use the Solana CLI to set the keypair:

    solana config set --keypair ./keypair.json
    

    Now your Solana CLI is configured to use the imported keypair.

Verification

To verify that the keypair is correctly configured, you can run:

solana address

This should display the public address of the imported keypair, which should match the one printed by your Node.js script.

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.