Last active
June 20, 2020 12:57
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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