1

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.
    Sep 2, 2022 at 20:20
  • can i just copy that private key into a file and it still works? 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
    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. Oct 17, 2022 at 15:47

4 Answers 4

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' " 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 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.

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.