Skip to content

Instantly share code, notes, and snippets.

@bitbug42
Last active June 20, 2020 12:57
Show Gist options
  • Save bitbug42/d70db35b600ddfe20723a9bf99110152 to your computer and use it in GitHub Desktop.
Save bitbug42/d70db35b600ddfe20723a9bf99110152 to your computer and use it in GitHub Desktop.
Scan Bitcoin blocks, looking for OP_RETURN outputs containing text. Requires a synced fullnode and the NBitcoin library
using NBitcoin;
using NBitcoin.RPC;
using System;
using System.IO;
using System.Linq;
using System.Text;
class OpReturnScan
{
/// <summary>
/// Insert path to your full-node auth cookie file
/// </summary>
const string CookieFile = "";
/// <summary>
/// Begin scan at this block
/// </summary>
const int Start = 630_000 - 2;
/// <summary>
/// End scan at this block
/// </summary>
const int End = 630_000 + 2;
static RPCClient InitBitcoinRpc()
{
var credentials = RPCCredentialString.Parse(File.ReadAllText(CookieFile));
var rpc = new RPCClient(credentials, Network.Main);
return rpc;
}
public static void Main(string[] args)
{
var rpc = InitBitcoinRpc();
for (int i = Start; i <= End; i++)
{
var blk = rpc.GetBlock(i);
foreach (var tx in blk.Transactions)
{
foreach (var vout in tx.Outputs)
{
var ops = vout.ScriptPubKey.ToOps().ToArray();
// Detect if the TXO script follows the format "OP_RETURN {data}"
if (ops.Length >= 2 &&
ops[0].Code == OpcodeType.OP_RETURN &&
ops[1].PushData?.Length > 0)
{
var text = Encoding.UTF8.GetString(ops[1].PushData);
// Discard op_return's about Omni and RSK because there's a lot of them
if (!text.StartsWith("omni") && !text.StartsWith("RSKBLOCK"))
{
Console.WriteLine($"{i}:{tx.GetHash()}: \"{text}\"");
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment