﻿using System.Collections.Generic;
using NBitcoin;

namespace MagicalCryptoWallet.Backend
{
	public class BlockFilterBuilder
	{
		private const int P = 20;

		public GolombRiceFilter Build(Block block)
		{
			var key = block.GetHash().ToBytes();

			var buffer = new List<byte[]>();
			buffer.Add(key);

			foreach (var tx in block.Transactions)
			{
				foreach (var txOutput in tx.Outputs)
				{
					var witDestination = PayToWitTemplate.Instance.ExtractScriptPubKeyParameters(txOutput.ScriptPubKey);
					var isValidPayToWitness = witDestination != null;

					if (isValidPayToWitness)
					{
						var scriptPubKeyBytes = txOutput.ScriptPubKey.ToBytes();
						buffer.Add(scriptPubKeyBytes);
					}
				}
			}

			return GolombRiceFilter.Build(key, P, buffer);
		}
	}
}

﻿using System.Collections.Generic;
using NBitcoin;

namespace MagicalCryptoWallet.Backend
{
	public class BlockFilterBuilder
	{
		private const int P = 20;
		private static readonly PayToWitPubKeyHashTemplate P2wpkh = PayToWitPubKeyHashTemplate.Instance;

		public GolombRiceFilter Build(Block block)
		{
			var key = block.GetHash().ToBytes();

			var buffer = new List<byte[]>();
			buffer.Add(key);

			foreach (var tx in block.Transactions)
			{
				foreach (var txOutput in tx.Outputs)
				{
					var isValidPayToWitness = P2wpkh.CheckScriptPubKey(txOutput.ScriptPubKey);

					if (isValidPayToWitness)
					{
						var witKeyId = P2wpkh.ExtractScriptPubKeyParameters(txOutput.ScriptPubKey);
						buffer.Add(witKeyId.ToBytes());
					}
				}
			}

			return GolombRiceFilter.Build(key, P, buffer);
		}
	}
}

Build filters only for P2WPKH scripts
Build filters only for P2WPKH scripts

Previous version created filters for p2wpkh and p2wsh scripts. This
version generates the filters using the push data part of the p2wpkh.
