Skip to main content

Integrate Embedded Wallets with the Solana Blockchain in Node.js

Use the Web3Auth Node SDK to get a Solana signer for server-side operations. The Node SDK returns a TransactionSigner that can be used with @solana/web3.js for blockchain operations without exposing the private key directly.

Chain Details for Solana

Installation

To interact with the Solana blockchain in Node.js, you need to install the required packages:

npm install --save @solana/web3.js @solana/signers

Initializing Web3Auth Node SDK

The Web3Auth Node SDK provides a TransactionSigner for Solana when using the SOLANA chain namespace. Here's how to set it up:

import { Web3Auth } from '@web3auth/node-sdk'
import { CHAIN_NAMESPACES } from '@web3auth/base'

const web3auth = new Web3Auth({
clientId: 'YOUR_WEB3AUTH_CLIENT_ID',
chainConfig: {
chainNamespace: CHAIN_NAMESPACES.SOLANA,
chainId: '0x1', // Use 0x1 for Mainnet, 0x2 for Testnet, 0x3 for Devnet
rpcTarget: 'https://api.mainnet-beta.solana.com',
},
})

await web3auth.init()

// Connect with JWT token or other method
const result = await web3auth.connect({
verifier: 'YOUR_VERIFIER_NAME',
verifierId: 'USER_ID',
idToken: 'JWT_TOKEN',
})

// result.signer is the Solana TransactionSigner
const solanaSigner = result.signer

Examples

Below examples use the TransactionSigner returned by the Node SDK. The signer provides access to the user's Solana address and signing capabilities.

const { LAMPORTS_PER_SOL, Connection, PublicKey } = require('@solana/web3.js')
const { createSignableMessage } = require('@solana/signers')

// Get Solana Accounts
const getAccounts = async signer => {
const publicKey = signer.address
console.log('Public Key:', publicKey)
}

// Get Solana Balance
const getBalance = async signer => {
const publicKey = new PublicKey(signer.address)
const connection = new Connection('https://api.devnet.solana.com')
const balance = (await connection.getBalance(publicKey)) / LAMPORTS_PER_SOL
console.log('Balance:', balance, 'SOL')
}

// Sign Solana Message
const signMessage = async (signer, message) => {
const messageBytes = new TextEncoder().encode(message)
const signableMessage = createSignableMessage(messageBytes)
const [signature] = await signer.signMessages([signableMessage])
console.log('Signed Message:', signature)
}

// Usage
await getAccounts(result.signer)
await getBalance(result.signer)
await signMessage(result.signer, 'Hello Web3Auth!')