Can I scan the blockchain for transactions if I remember first and last few characters of a bitcoin address which was used in a transaction?

1 Answer 1


This C# code scans blocks for transactions and returns the block number and transaction id based on user input.

Bitcoin Core

using NBitcoin;
using NBitcoin.RPC;

class Scanblocks

    const string Cookie = "<path for bitcoin core auth cookie file>";

    static RPCClient InitRpc()
        var creds = RPCCredentialString.Parse(File.ReadAllText(Cookie));
        var rpc = new RPCClient(credentials, Network.TestNet);

        return rpc;

    public static void Main(string[] args)
        Console.WriteLine("Start block:");
        int start_block = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("End block:");
        int end_block = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("First few chars of bitcoin address:");
        string address_start = Console.ReadLine();

        Console.WriteLine("Last few chars of bitcoin address:");
        string address_end = Console.ReadLine();

        for (int i = start_block; i <= end_block; i++)
            var rpc = InitRpc();
            var blk = rpc.GetBlock(i);

            foreach (var tx in blk.Transactions)
                foreach (var vout in tx.Outputs)
                    var address_out = vout.ScriptPubKey.GetDestinationAddress(Network.TestNet);
                    if (address_out is not null && address_out.ToString().StartsWith(address_start) && address_out.ToString().EndsWith(address_end))

Example output for a signet address:

Start block:
End block:
First few chars of bitcoin address:
Last few chars of bitcoin address:


Bitcoin address was tb1ql5yc896q7rstflrdtcjj0eqz925m3806aemazs and used in the txid returned as output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.