library / Use cases / Wallet products / UC0154
Imported account discovery
A recovery phrase can produce different Solana addresses in different wallets. The derivation path decides which address you get, and this script reads SOL, token accounts and recent activity for each candidate with no signing.
Why one phrase gives two addresses
A recovery phrase is a seed. A wallet runs it through key derivation along a path, and the public address is what comes out the other end. Solana keys use the BIP44 coin type 501, but the path after that differs between wallet apps. One may derive at m/44'/501'/0', another at m/44'/501'/0'/0', and many let you choose an account index. The same words on a different path give a different address, and each address holds its own SOL and tokens.
The balance can differ for a second reason. Native SOL sits in the account itself. Tokens sit in token accounts, and one owner can hold more than one token account for the same mint. Classic SPL Token and Token-2022 are separate programs, so a wallet that scans only one of them lists fewer tokens than the address holds.
Nothing moves between these addresses. Each one holds its own funds, so the first step is to find which address your funds are on.
Read each candidate on chain
The script reads a list of public addresses. You derive them in your wallet's own tools, one for each path or account index you want to check, and pass them as arguments. The recovery phrase never reaches the script, and the script never signs or sends anything. For each address it reads four things, all at confirmed, which the script passes on every call. A call that leaves commitment out gets the server default, finalized, so the choice is explicit here.
| Read | Method | What the script keeps |
|---|---|---|
| SOL balance | getBalance | lamports the account itself holds, shown in SOL |
| SPL token accounts | getTokenAccountsByOwner | accounts under the SPL Token program with a non-zero amount |
| Token-2022 accounts | getTokenAccountsByOwner | the same count under the Token-2022 program |
| Newest activity | getSignaturesForAddress | the block time of the latest signature, limit 1, from the 30-day index |
Every read goes to the RPC endpoint with the key in the x-api-key header. The calls are standard Solana JSON-RPC, and each one draws on the account's budget under the method costs.
A read-only script
With no arguments, the script finds candidates on its own. It reads the five newest signatures of the SPL Token program, takes the fee payer of each transaction (the first account key), and removes duplicates. Those addresses belong to wallets that paid fees on busy traffic, so the run shows real output without a list from you. The discovery step costs one signature lookup and five transaction lookups, at 50 cells each.
Transactions are read with maxSupportedTransactionVersion: 1, because mainnet carries v1 transactions. Without that option the node answers -32015 for a v1 transaction, and the script would skip it. A null reply is skipped too, and one failing address does not stop the rest.
// UC0154 · Imported account discovery// https://carbium.sh/library/usecase/imported-account-discovery// Copyright (c) Carbium Corporation. MIT License (LICENSE). Keep this notice.// SPDX-License-Identifier: MIT// Read-only: reads SOL, token accounts and newest activity for candidate addresses.const key = process.env.CARBIUM_KEY;if (!key) { console.error("Set CARBIUM_KEY to your Carbium API key."); process.exit(1); }const url = process.env.CARBIUM_RPC_URL ?? "https://services.carbium.sh";const SPL = "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA";const TOKEN_2022 = "TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb";const CONFIG = { commitment: "confirmed" } as const;type Reply<T> = { id: number; result?: T; error?: { code: number; message: string } };type Tokens = { value: { account: { data: { parsed: { info: { tokenAmount: { amount: string } } } } } }[] };let id = 0;async function rpc<T>(method: string, params: unknown[]): Promise<T> { const res = await fetch(url, { method: "POST", headers: { "content-type": "application/json", "x-api-key": key! }, body: JSON.stringify({ jsonrpc: "2.0", id: ++id, method, params }), }); if (res.status === 429) throw new Error(`429, retry-after ${res.headers.get("retry-after") ?? "not set"}`); const out = (await res.json()) as Reply<T>; if (out.error) throw new Error(`${out.error.code} ${out.error.message}`); return out.result as T;}async function holdings(program: string, owner: string): Promise<number> { const r = await rpc<Tokens>("getTokenAccountsByOwner", [owner, { programId: program }, { encoding: "jsonParsed", ...CONFIG }]); return r.value.filter((v) => v.account.data.parsed.info.tokenAmount.amount !== "0").length;}async function inspect(addr: string): Promise<string> { const sol = await rpc<{ value: number }>("getBalance", [addr, CONFIG]); const spl = await holdings(SPL, addr); const t22 = await holdings(TOKEN_2022, addr); const last = await rpc<{ blockTime: number | null }[]>("getSignaturesForAddress", [addr, { limit: 1 }]); const t = last[0]?.blockTime; const when = t ? new Date(t * 1000).toISOString() : "none in the 30-day index"; return `${addr}\n SOL ${(sol.value / 1e9).toFixed(4)} SPL tokens ${spl} Token-2022 tokens ${t22} last activity ${when}`;}async function discover(): Promise<string[]> { const sigs = await rpc<{ signature: string }[]>("getSignaturesForAddress", [SPL, { limit: 5 }]); const payers: string[] = []; for (const s of sigs) { const tx = await rpc<{ transaction: { message: { accountKeys: string[] } } } | null>("getTransaction", [s.signature, { encoding: "json", maxSupportedTransactionVersion: 1, ...CONFIG }]); if (tx) payers.push(tx.transaction.message.accountKeys[0]!); } return [...new Set(payers)];}const given = process.argv.slice(2);const candidates = (given.length ? given : await discover()).slice(0, 20);console.log(`Checking ${candidates.length} address(es) at confirmed`);const rows = await Promise.allSettled(candidates.map((a) => inspect(a)));rows.forEach((r, i) => console.log(r.status === "fulfilled" ? r.value : `${candidates[i]} failed: ${(r.reason as Error).message}`));
run: CARBIUM_KEY=$CARBIUM_KEY node main.ts
Run it with addresses as arguments to check your own list. The output is one block per address, with SOL, the two token counts and the date of the newest activity.
Reading the result
- Same address on one wallet, different on the other: the wallets used different paths or account indexes. Compare the derivation path each wallet reports for the account.
- Tokens under Token-2022 only: the wallet that lists them scans that program, and the other wallet does not.
- Zero SOL, no tokens and no recent activity: the address is empty, or its last activity is older than the index keeps. The script cannot tell those two apart.
- Recent activity but zero balance: the funds left the address. For the token detail, read wallet token holdings or token accounts by owner.
Where it ends
- Activity: getSignaturesForAddress reaches back 30 days at most, from the address index. The line none in the 30-day index is not proof of an empty account, so read the balance before you decide.
- Old signatures: the index can list a signature the validator no longer holds, and getTransaction then answers null. The discovery step skips those, and the lookup of a specific old signature needs its own handling.
- Derivation: the script does not derive keys and cannot tell which path a wallet used. Only the wallet knows that.
- Token counts: a count is the number of token accounts with a non-zero amount. The script does not decode token extensions or price anything.
- Budget: each candidate costs four reads, and the keys on one account share one budget. A call over its rate waits once, then gets 429 with a retry-after hint. The script reports that address as failed and does not retry it.
- Methods: a method the service does not serve answers -32601, which the error codes list as a final answer. The script does not retry it.
When it goes wrong
| what goes wrong | what to do |
|---|---|
| Every candidate shows zero SOL and zero tokens, though the phrase should hold funds. | Check the derivation path. The funds may sit on an address from another path or account index. Derive that index with the other path in your wallet's tools and run the script again. |
| Tokens listed in one wallet do not appear in the other. | Read both token programs. Token-2022 accounts sit under their own program ID, so the script counts them in a separate column. |
| A run stops with 429 for several addresses. | Each address costs four reads, and the account's keys share one budget. Check fewer addresses per run, space the runs apart, or move to a plan with a higher rate. The 429 response carries a retry-after hint. |
| getTransaction returns null for an older signature from the index. | Skip that signature and use the next one. History older than the 30-day index is outside what this method can reach. |
| A call returns -32601. | Do not retry. Check the method name against the not-served list in the error codes, and remove the call. |
| The recovery phrase is pasted into the command line or a file. | Treat the phrase as exposed and move the funds with the wallet's own tools. Pass only public addresses to this script. |
Questions
Does this script need my recovery phrase?
No. Pass public addresses only, and derive them in your own wallet or a trusted tool first. The script reads the chain and never signs.
Why do two wallets show different SOL for the same phrase?
They derived different addresses, and each address holds its own balance. Compare the addresses first, then compare the amounts on each one.
Can the script find old transactions for an address?
Only within the 30-day address index. Older history can be missing, and an empty page past that point marks the index's age limit.