Here is a step-by-step article on how to convert an address to a public key in Solana using web3.js v2 with TypeScript:
Address to Public Key Conversion: A Guide to Working with Solana Wallet Addresses and Signatures
In this article, we will explore the process of converting a wallet address to a public key in Solana using web3.js v2. We will also see how to receive the wallet addresses and signatures from the frontend into our backend program.
Prerequisites
Before you begin, make sure you have:
- A Solana node installed (either locally or on a cloud provider)
- TypeScript configured with
@types/web3.js
andtypescript
- Necessary dependencies installed:
solana-web3
,web3
,typescript
Step 1: Create a wallet
Let’s start by creating a new wallet using the Solana CLI:
npx solana-keygen generate --pubkey-gen-fp 2048 --out
Replace
with the desired wallet name and
with the path where you want to store the private key file.
Step 2: Create a Web3 instance
Create a new TypeScript file (e.g. solana.ts
) and import the necessary dependencies:
import * as web3 from '@web3js/web3';
import * as solanaWeb3 from 'solana-web3';
Next, create a function to initialize the Solana wallet instance:
function initWallet() {
const keyPath = './path/to/wallet/private/key.json'; // Replace with the path to your private key file
return new web3.Web3(new solanaWeb3.PublicKey(keyPath));
}
Step 3: Converting an address to a public key
Now, let’s create a function that converts a wallet address to a public key:
function convertAddressToPubkey(address: string) {
const wallet = initWallet();
return wallet.publicKey;
}
This function takes a wallet address as input and returns the corresponding public key.
Step 4: Verifying the signature
In our backend program, we will need to verify the signature of a received message. We will create a new function that verifies the signature using the verifySignature
function from @solana/web3.js
. First, let’s import the necessary dependencies:
import { verifySignature } from '@solana/web3.js';
Next, we’ll add the following code to our backend program:
export async function verifyMessage(message: string) {
const address = 'your_wallet_address'; // Replace with the wallet address received from the frontend
const signature = 'your_signature'; // Replace with the signature received from the frontend
try {
await verifySignature(message, address, new web3.PublicKey(signature), { network: 'mainnet' });
} catch (error) {
console.error(error);
}
}
In this example, we use the verifySignature
function to verify the signature of a received message. We pass the address, signature, and network URL as arguments.
Step 5: Converting the address to a public key in the frontend
To receive wallet addresses and signatures on the frontend side, we can create a simple endpoint that takes an address and signature as input:
import axios from 'axios';
export async function receiveAddressAndSignature(address: string, signature: string) {
const response = await axios.post('/api/verify', { address, signature });
console.log(response.data);
}
This endpoint uses the axios
library to send a POST request to our backend program.
In this example, we send a POST request with the wallet address and signature as input. Our backend program will receive the request and verify the signature using the verifySignature
function from @solana/web3.js
.
That’s it! With these steps, you should be able to convert addresses to PubKeys in Solana using web3.js v2 with TypeScript.