Node
Learn how to sign Solana transactions using Dynamic’s Node SDK
bun add @solana/web3.js
import { authenticatedSvmClient } from './client'; import { Connection, PublicKey, Transaction, SystemProgram, LAMPORTS_PER_SOL } from '@solana/web3.js'; const svmClient = await authenticatedSvmClient(); // Create a simple SOL transfer transaction const fromPubkey = new PublicKey('YourSolanaWalletAddress'); const toPubkey = new PublicKey('11111111111111111111111111111112'); const transaction = new Transaction().add( SystemProgram.transfer({ fromPubkey, toPubkey, lamports: LAMPORTS_PER_SOL * 0.001, // 0.001 SOL }) ); // Sign the transaction const signedTransaction = await svmClient.signTransaction({ senderAddress: 'YourSolanaWalletAddress', externalServerKeyShares: [ { chainName: 'solana', keyShare: '0xYourKeyShare', }, ], transaction: transaction, password: 'your-password', // optional }); console.log('Transaction signed successfully');
import { Connection, sendAndConfirmTransaction } from '@solana/web3.js'; const connection = new Connection('https://api.devnet.solana.com', 'confirmed'); const txHash = await sendAndConfirmTransaction(connection, signedTransaction); console.log('Transaction hash:', txHash);
export const sendSol = async ({ fromAddress, toAddress, amount, externalServerKeyShares, password, }: { fromAddress: string; toAddress: string; amount: number; // Amount in SOL externalServerKeyShares: any[]; password?: string; }) => { const svmClient = await authenticatedSvmClient(); // Create transaction const fromPubkey = new PublicKey(fromAddress); const toPubkey = new PublicKey(toAddress); const transaction = new Transaction().add( SystemProgram.transfer({ fromPubkey, toPubkey, lamports: LAMPORTS_PER_SOL * amount, }) ); // Sign transaction const signedTransaction = await svmClient.signTransaction({ senderAddress: fromAddress, externalServerKeyShares, transaction, password, }); // Send transaction const connection = new Connection('https://api.devnet.solana.com', 'confirmed'); const txHash = await sendAndConfirmTransaction(connection, signedTransaction); return txHash; }; // Usage const txHash = await sendSol({ fromAddress: 'YourSolanaWalletAddress', toAddress: 'RecipientAddress', amount: 0.001, externalServerKeyShares: [ { chainName: 'solana', keyShare: '0xYourKeyShare', }, ], password: 'your-password', }); console.log('SOL sent:', txHash);
const transaction = new Transaction().add( SystemProgram.transfer({ fromPubkey: new PublicKey('YourAddress'), toPubkey: new PublicKey('RecipientAddress'), lamports: LAMPORTS_PER_SOL * 0.1, }) );
import { Token, TOKEN_PROGRAM_ID } from '@solana/spl-token'; const transaction = new Transaction().add( Token.createTransferInstruction( TOKEN_PROGRAM_ID, fromTokenAccount, toTokenAccount, fromPubkey, [], amount ) );
Was this page helpful?