// Import statements
import { DynamicSvmWalletClient } from "@dynamic-labs-wallet/node-svm";
import {
Connection,
LAMPORTS_PER_SOL,
PublicKey,
SystemProgram,
Transaction,
} from "@solana/web3.js";
import { ThresholdSignatureScheme } from "@dynamic-labs-wallet/node";
import fs from "fs";
// Initialize SVM client
export const authenticatedSvmClient = async () => {
const client = new DynamicSvmWalletClient({
environmentId: process.env.DYNAMIC_ENVIRONMENT_ID!,
});
await client.authenticateApiToken(process.env.DYNAMIC_AUTH_TOKEN!);
return client;
};
// Example usage demonstrating all available functions
async function main() {
try {
console.log("đ Starting SVM Client Demo...\n");
// Initialize the SVM client
const svmClient = await authenticatedSvmClient();
console.log("â
SVM client authenticated successfully\n");
// 1. Create a new SVM wallet
console.log("đ Creating new SVM wallet...");
const svmWallet = await svmClient.createWalletAccount({
thresholdSignatureScheme: ThresholdSignatureScheme.TWO_OF_TWO,
onError: (error: Error) => {
console.error("â SVM wallet creation error:", error);
},
backUpToDynamic: true,
});
console.log("â
SVM wallet created:", svmWallet.accountAddress);
console.log("externalServerKeyShares:", svmWallet.externalServerKeyShares);
console.log("rawPublicKey:", svmWallet.rawPublicKey);
// Save wallet info to file
fs.writeFileSync("svmWallet.json", JSON.stringify(svmWallet, null, 2));
console.log("đž Wallet saved to svmWallet.json\n");
// 2. Get wallet information
console.log("đ Getting wallet details...");
try {
const walletDetails = await svmClient.getWallet({
accountAddress: svmWallet.accountAddress,
});
console.log("â
Wallet details retrieved:", walletDetails);
} catch (error) {
console.log("â ī¸ Could not retrieve wallet details:", error);
}
// 3. Get all SVM wallets
console.log("\nđ Getting all SVM wallets...");
try {
const allWallets = await svmClient.getSvmWallets();
console.log("â
Found", allWallets.length, "SVM wallets");
} catch (error) {
console.log("â ī¸ Could not retrieve all wallets:", error);
}
// 4. Sign a message
console.log("\nâī¸ Signing a message...");
try {
const message = "Hello, Solana! This is a test message from Dynamic SVM wallet.";
const signedMessage = await svmClient.signMessage({
accountAddress: svmWallet.accountAddress,
message,
});
console.log("â
Message signed successfully");
console.log("Signature:", signedMessage);
} catch (error) {
console.log("â ī¸ Message signing failed:", error instanceof Error ? error.message : String(error));
}
// 5. Sign a transaction (Solana transfer example)
console.log("\nđ¸ Preparing Solana transaction...");
const connection = new Connection("https://api.devnet.solana.com", "confirmed");
try {
const amountInSol = 0.001;
const recipientAddress = "EukgLDgaWNQRLemZSHK8Wc9EGhPhbG3wL9a1mxa3LHg6";
// Create transaction
const transaction = new Transaction().add(
SystemProgram.transfer({
fromPubkey: new PublicKey(svmWallet.accountAddress),
toPubkey: new PublicKey(recipientAddress),
lamports: amountInSol * LAMPORTS_PER_SOL,
})
);
// Get latest blockhash
const { blockhash } = await connection.getLatestBlockhash();
transaction.recentBlockhash = blockhash;
transaction.feePayer = new PublicKey(svmWallet.accountAddress);
console.log("đ Transaction prepared, signing with SVM wallet...");
// Sign the transaction â returns base58 Ed25519 signature (not the full signed tx)
const signatureBase58 = await svmClient.signTransaction({
walletMetadata: svmWalletMetadata,
transaction,
});
// To broadcast: decode the signature and add it back to the transaction
// import { decodeBase58, addSignatureToTransaction } from '@dynamic-labs-wallet/node-svm';
// const signedTx = addSignatureToTransaction({ transaction, signature: decodeBase58(signatureBase58), signerPublicKey });
// const txid = await connection.sendRawTransaction(signedTx.serialize());
console.log("â
Transaction signed successfully");
console.log("Signature (base58):", signatureBase58);
} catch (error) {
console.log("â ī¸ Transaction signing failed:", error instanceof Error ? error.message : String(error));
}
// 6. Import private key (if you have one)
console.log("\nđ Testing private key import...");
try {
const importedWallet = await svmClient.importPrivateKey({
privateKey: "your-private-key-here", // Replace with actual private key
chainName: "SVM",
thresholdSignatureScheme: ThresholdSignatureScheme.TWO_OF_TWO,
backUpToDynamic: true,
password: "test-password",
});
console.log("â
Private key imported:", importedWallet.accountAddress);
} catch (error) {
console.log("âšī¸ Private key import skipped (no valid key provided)");
}
// 7. Export key information
console.log("\nđ¤ Exporting key information...");
try {
const exportedKey = await svmClient.exportKey({
accountAddress: svmWallet.accountAddress,
chainName: "SVM",
});
console.log("â
Key exported:", exportedKey);
} catch (error) {
console.log("âšī¸ Key export not available or requires additional setup");
}
// 8. Export private key
console.log("\nđ Exporting private key...");
try {
const privateKey = await svmClient.exportPrivateKey({
accountAddress: svmWallet.accountAddress,
});
console.log("â
Private key exported:", privateKey);
} catch (error) {
console.log("âšī¸ Private key export not available or requires additional setup");
}
// 9. Test connection to Solana network
console.log("\nđ Testing Solana network connection...");
try {
const balance = await connection.getBalance(new PublicKey(svmWallet.accountAddress));
console.log("â
Solana connection successful");
console.log("Wallet balance:", balance / LAMPORTS_PER_SOL, "SOL");
} catch (error) {
console.log("âšī¸ Solana network connection test skipped");
}
console.log("\nđ SVM Client Demo completed successfully!");
console.log("đ Check svmWallet.json for wallet details");
// Summary of what was accomplished
console.log("\nđ Demo Summary:");
console.log("â
Successfully completed:");
console.log(` âĸ Created SVM wallet: ${svmWallet.accountAddress}`);
console.log(` âĸ Raw public key: ${svmWallet.rawPublicKey}`);
console.log(` âĸ External server key shares: ${svmWallet.externalServerKeyShares?.length || 0}`);
console.log(` âĸ Solana network connection: Working`);
// Try to get additional wallet details if available
try {
const walletDetails = await svmClient.getWallet({
accountAddress: svmWallet.accountAddress,
});
console.log(` âĸ Wallet ID: ${walletDetails.walletId || "N/A"}`);
console.log(` âĸ Threshold scheme: ${walletDetails.thresholdSignatureScheme || "N/A"}`);
console.log(` âĸ Chain name: ${walletDetails.chainName || "N/A"}`);
} catch (error) {
console.log(" âĸ Additional wallet details: Not available");
}
} catch (error) {
console.error("â Error in SVM demo:", error);
}
}
// Run the example
main();