user133407: 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?

user133407: This C# code scans blocks for transactions and returns the block number and transaction id based on user input.
Requirements:
NBitcoin
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))
                    {
                        Console.WriteLine($"\n{i}:{tx.GetHash()}");                        
                    }                                      
                   
                }
            }
        }
        
    }
}


Example output for a signet address:
Start block:
1
End block:
2500
First few chars of bitcoin address:
tb1ql5y
Last few chars of bitcoin address:
emazs

2:1248e5f56dd2959429d8ca1117802c0bf59c3deb88abbcafcf17205950ef8fb3

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

133407: <p>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?</p>
 133407: <p>This C# code scans blocks for transactions and returns the block number and transaction id based on user input.</p>
<p><strong>Requirements:</strong><br />
NBitcoin<br />
Bitcoin Core</p>
<pre><code>using NBitcoin;
using NBitcoin.RPC;

class Scanblocks
{

    const string Cookie = &quot;&lt;path for bitcoin core auth cookie file&gt;&quot;;

    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(&quot;Start block:&quot;);
        int start_block = Convert.ToInt32(Console.ReadLine());

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

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

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

        for (int i = start_block; i &lt;= 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 &amp;&amp; address_out.ToString().StartsWith(address_start) &amp;&amp; address_out.ToString().EndsWith(address_end))
                    {
                        Console.WriteLine($&quot;\n{i}:{tx.GetHash()}&quot;);                        
                    }                                      
                   
                }
            }
        }
        
    }
}

</code></pre>
<p><strong>Example output for a signet address:</strong></p>
<pre><code>Start block:
1
End block:
2500
First few chars of bitcoin address:
tb1ql5y
Last few chars of bitcoin address:
emazs

2:1248e5f56dd2959429d8ca1117802c0bf59c3deb88abbcafcf17205950ef8fb3
</code></pre>
<p>Bitcoin address was <code>tb1ql5yc896q7rstflrdtcjj0eqz925m3806aemazs</code> and used in the txid returned as output.</p>
