const { Connection, clusterApiUrl, PublicKey } = require('@solana/web3.
js');
// Create a connection to the Solana cluster
const connection = new Connection(clusterApiUrl('mainnet-beta'), 'confirmed');
// Function to fetch transaction history for a wallet
async function getTransactionHistory(publicKey) {
try {
const signatures = await connection.getConfirmedSignaturesForAddress2(new
PublicKey(publicKey));
return signatures;
} catch (err) {
console.error(`Error fetching transaction history: ${err}`);
return [];
}
}
// Function to fetch transaction details
async function getTransactionDetails(signature) {
try {
const transaction = await connection.getParsedTransaction(signature,
'confirmed');
return transaction;
} catch (err) {
console.error(`Error fetching transaction details: ${err}`);
return null;
}
}
// Function to extract the source wallet from a transaction
function getSourceWallet(transaction) {
try {
const instructions = transaction.transaction.message.instructions;
for (const instr of instructions) {
if (instr.parsed?.type === 'transfer') {
return instr.parsed.info.source;
}
}
return null;
} catch (err) {
console.error(`Error parsing transaction: ${err}`);
return null;
}
}
// Recursive function to trace back to the original wallet
async function traceOriginalWallet(wallet) {
console.log(`Tracing wallet: ${wallet}`);
const signatures = await getTransactionHistory(wallet);
if (signatures.length === 0) {
console.log(`Original wallet found: ${wallet}`);
return wallet; // No prior transactions, this is the original wallet
}
for (const sig of signatures) {
const transaction = await getTransactionDetails(sig.signature);
if (transaction) {
const sourceWallet = getSourceWallet(transaction);
if (sourceWallet) {
return await traceOriginalWallet(sourceWallet);
}
}
}
console.log(`Original wallet determined: ${wallet}`);
return wallet; // If no source wallet is found, assume this is the original
}
// Main function to execute the program
(async () => {
const startingWallet = 'YourTargetWalletAddressHere'; // Replace with your target
wallet address
const originalWallet = await traceOriginalWallet(startingWallet);
console.log('Original Wallet:', originalWallet);
})();