Steps
Initialize a new Node.js project (if you don't have one already):
mkdir solana-key-import cd solana-key-import npm init -y
Install required packages:
npm install @solana/web3.js bs58
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());
Run the script:
node importKey.js
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.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.