Marketplace has launched, further enabling blockchain developers! Learn more

How to Create an Address in Solana using JavaScript

September 23, 2022

Overview

Hello reader! Welcome to QuickNode's first Solana guide. Solana is an up-and-coming blockchain that seeks to solve the scalability issues that Ethereum has been handling. You will walk through step-by-step how to create a Solana address using the @solana/web3.js library in this guide. Prerequisites:

  • NodeJS installed
  • Terminal/CLI familiarity
  • Text editor

What is Solana?

Solana's goal is singular in nature. That goal is to scale the blockchain for global adoption. Solana Labs, the developers of the Solana Protocol, are doing a few different things to achieve that dream. Blockchain technology has a few knobs when it comes to tuning performance. One of which is the consensus mechanism. This is how nodes communicate together to arrive at an identical conclusion.

Bitcoin uses Proof of Work or PoW. Binance Smart Chain, also known as BSC, uses a Proof of Staked Authority or PoSA. And Ethereum is migrating to Proof of Stake aka PoS. As you can tell, consensus is by no means a solved game at this point in time. Solana uses a consensus called Proof of History. Proof of History works through a time-stamping solution; each transaction has a time stamp allocated to it that allows it to be verified as a legitimate transaction by the rest of the network in mere fractions of a second. Solana has a breakdown of the eight technologies that they believe position themselves as the fastest, most scalable, and secure blockchain in the game.

How to Set Up a Solana Node

You could use any node to connect to the Solana Blockchain. However, if you were to run your own node, you would need to meet Solana's validator hardware requirements, which is no small task. To meet the minimum requirements, you'll need 128GB of ram.

QuickNode offers Solana nodes starting at $9.00/Mo to make it easier on the average developer. You can start with a free trial to go through this tutorial and see if it's a good fit for you.

After setting up your account, you can select the plan that you would like. Then select the Solana node and whichever network you would like to use. We will not be writing any data to the blockchain in this tutorial, which means we won't require any fees or other transaction costs associated with writing to a blockchain. For this guide, we will use a mainnet node. The process will be the same for any network on Solana.

With your node setup, copy your HTTP provider.
 
Screenshot of Quicknode Solana endpoint

 
That is all it takes to set up a node on Solana using QuickNode! With a node to use, you need to a script to connect it to.

Connecting to a Solana Node with JavaScript

You have all of the tools that you need to generate an address on Solana. The first thing you need to do is connect your script to the Solana Node you created in the previous section.

We need to create a project directory. Feel free to use a different directory name.

connecting to a solana node with javascript

Copy
mkdir SolanaAddressJavaScript
cd SolanaAddressJavaScript
In this new directory, you will need to install the dependencies required for our project. Execute the following commands to do so.

connecting to a solana node with javascript

Copy
npm install --save @solana/web3.js
touch solana.js
With your dependencies configured, open the solana.js file you created a moment ago.

connecting to a solana node with javascript

Copy
//solana.js
const solanaWeb3 = require('@solana/web3.js');

const Solana = new solanaWeb3.Connection(
  "YOUR_QUICKNODE_HTTP_PROVIDER__HERE"
);


Code Breakdown:

  • Line 1: Importing the Solana web3.js library.

Note: All of the @solana/web3.js library is typed if you prefer to write in TypeScript instead.

  • Line 3-5: Connecting to the Solana Node. Copy your HTTP Provider from Quicknode and paste it as a string into the solanaWeb3.Connection().

You just connected your script to the Solana Network! To test your connection, you can take a look at the most recent blockchain information by adding the following to the bottom of your script:

connecting to a solana node with javascript

Copy
//solana.js
const getRecentBlockInfo = async () => {
    const recentInfo = await Solana.getEpochInfo()
    console.log("~~~~~~~~~~~~~~~~~EPOCH INFO~~~~~~~~~~~~\n", recentInfo);
}
getRecentBlockInfo();
Code Breakdown:

  • Line 1: Creating an asynchronous function allows you to use the await keyword needed for the following web3 method.
  • Line 2: Calling the getEpochInfo() method from the solana/web3.js library. We store this in the recentInfo constant.
  • Line 3: We print out the info stored in the recentInfo variable, along with a large string that should make it stick out in the console.
With your updated script, run the following from your root directory:

connecting to a solana node with javascript

Copy
node solana.js
Upon running this command, you will see an output similar to this:
 


If you have a similar output to the picture above, you are now successfully connected to the Solana Network! With a successful connection established, we can now create an address to send and receive funds.

Creating an Address in Solana

Creating an address in Solana is a bit different than other libraries, as it will expose the raw Uint8Array for you to manipulate. There are several methods that you can look at over here. However, we will only be going over how to generate a keypair as all of the remaining methods are based off having a pre-existing secret key.

To create a new key pair, you will need to call exactly one function. This is the Keypair.generate() method attached to the Solana Client we initialized earlier. Modify your script to look like this, and you will be all done.

creating an address in solana

Copy
const solanaWeb3 =  require("@solana/web3.js");

const Solana = new solanaWeb3.Connection(
    "YOUR_QUICKNODE_HTTP_PROVIDER__HERE"
);

const firstBlock = async () => {
  const recentBlock = await Solana.getEpochInfo();
  console.log("~~~~~~~~~~~~~~~~~NEW BLOCK~~~~~~~~~~~~\n", recentBlock);
  const keyPair = solanaWeb3.Keypair.generate();

  console.log("Public Key:", keyPair.publicKey.toString());
  console.log("Secret Key:",keyPair.secretKey)
};

firstBlock();
This is the same script from earlier, but with three lines added:

  • Line 10: Calling the Keypair.generate() function, which gives us access to our public and private keys.
  • Line 11-12: Printing out the public and private key.

With this put together, your script will print out the current block information and generate a new keyPair; then print out both the public and secret key. If you would like to keep the same key, you will need to copy the secret key and use one of the methods from the documentation that we linked to above.

Run the following to see the fruits of your labor:

creating an address in solana

Copy
node solana.js
This will show an output like this:
 


The first portion is the current block information. The second section is your public key outputted in the familiar address format of a long string of random characters. Along with the public key is your secret key. The secret key is the array of numbers. Be sure not to share that with anyone, as they would have full access to your funds.

Conclusion

Congrats on making it to the end! By doing so, you have now successfully connected a script to the Solana network and even generated an address to sign transactions and hold funds. With this new address, you are ready to venture forth into the new frontier that Solana is building. There are many cool projects, and the ecosystem is ripe for participation should you have the wherewithal to take advantage.

Subscribe to our newsletter for more articles and guides on Ethereum. If you have any feedback, feel free to reach out to us via Twitter. You can always chat with us on our Discord community server, featuring some of the coolest developers you'll ever meet :)

Related articles 33

Como crear un NFT en SOLANA
Published: Dec 27, 2021
Updated: Sep 23, 2022

¡Hola querido lector! Bienvenidos a una nueva guía de Solana.Solana es una blockchain que promete mucho a la hora de intentar resolver los problemas de escalabilidad que...

Continue reading
How to Get Transaction Logs on Solana
Published: Jun 24, 2022
Updated: Oct 27, 2022

Ever need to pull all the transactions associated with a Wallet? Want to see all of the mint transactions associated with a Candy Machine? Or maybe see transaction history of an NFT? Solana's...

Continue reading
How to Transfer SPL Tokens on Solana
Published: Sep 23, 2022
Updated: Sep 23, 2022

Sending Solana Program Library (SPL) Tokens is a critical mechanism for Solana development. Whether you are airdropping whitelist tokens to your community, bulk sending NFTs to another wallet,...

Continue reading
How to Mint an NFT on Solana
Published: Aug 27, 2021
Updated: Sep 23, 2022

Updated at: April 10, 2022Welcome to another QuickNode guide on Solana - the up-and-coming blockchain that seeks to solve the scalability issues of Ethereum. We will be walking through...

Continue reading
How to Send Bulk Transactions on Solana
Published: Aug 31, 2022
Updated: Oct 3, 2022

Are you running a batch process that has many transactions? Perhaps an airdrop to your community's NFT holders or a token distribution to early users of your dApp. Solana transaction...

Continue reading
Solana Fundamentals Reference Guide
Published: Oct 27, 2022
Updated: Oct 27, 2022

The Solana blockchain is a powerful tool, delivering thousands of transactions per second with almost no-cost transaction fees. If you are new to Web3 or have developed on EVM-based...

Continue reading