Here is a sample article on generating, saving, importing a keypair into Phantom with Solana/Web3.js v2:
Generating, Saving, Importing a Keypair in Phantom using Solana/Web3.js v2
In this article, we will walk through the process of generating, saving, and importing a keypair into your Phantom wallet using the Solana/Web3.js v2 library.
Step 1: Generating a Keypair
Before we can generate a keypair, you need to set up the solana-web3.js
library on your machine. You can do this by running the following command in your terminal:
npm install solana-web3@2.7.0
Once installed, import the library and initialize it with the Solana network ID (PLT) and a key material file (more on this later):
const Web3 = require('web3');
const web3 = new Web3(new Web3.providers.HttpProvider('
// Load your key material file, for example: 'path/to/key/material.json'
const keyMaterial = await web3.eth.loadKey({
path: 'path/to/key.material.json',
});
// Generate a keypair
const keystore = await web3.eth.createKeypair();
Step 2: Saving the Keypair
To save the generated keypair, you’ll need to export it as a JSON object. You can do this by creating an import
statement in your JavaScript file and exporting the key material:
export default keystore;
Alternatively, you can use the web3.eth.getKeypairs()
method to get a list of available keypairs and then select one to export.
Step 3: Importing the Keypair into Phantom
To import your saved keypair into Phantom, follow these steps:
- Open the Solana CLI (Solana Command-Line Interface) on your machine:
solana cli
- Set the network ID to PLT (Mainnet).
- Run the following command to get your public and private keys:
solana key list --network plt
- The output will be a JSON object containing both your public and private keys.
- Import the JSON object into Phantom using the following steps:
a. Open the config.json
file in your project root directory.
b. Update the key material
field with the path to your exported key material file.
Here’s an example of what your config.json
file should look like:
{
"keyMaterial": "/path/to/key/material.json",
// Other configuration settings...
}
Complete Example
Here’s a complete example that demonstrates how to generate, save, and import a keypair into Phantom using Solana/Web3.js v2:
const Web3 = require('web3');
const web3 = new Web3(new Web3.providers.HttpProvider('
// Load your key material file (optional)
const keyMaterial = await web3.eth.loadKey({
path: 'path/to/key/material.json',
});
// Generate a keypair
const keystore = await web3.eth.createKeypair();
// Save the keypair
export default keystore;
// Import the keypair into Phantom
const config = require('./config.json');
web3.eth.getKeypairs().then((keypairs) => {
const privateKeys = [];
for (let i = 0; i < keypairs.length; i++) {
privateKeys.push(web3.eth.key.decodeRaw(keypairs[i].privateKey));
}
console.log(privateKeys);
});
// Update the config.json file with your saved keypair
const updatedConfig = { ...config, keyMaterial: "/path/to/key/material.json" };
require('./config.json')(updatedConfig);
Note that this is just an example and you should adapt it to your specific use case. Additionally, keep in mind that generating a keypair on the network can be expensive, so make sure you have enough funds available before proceeding.