1 package filter
2 3 // BuildFromScripts builds a basic compact block filter from script elements and
4 // returns its BIP158 serialization.
5 //
6 // It removes empty scripts and exact duplicate scripts before building, which
7 // matches the BIP158 rule that nil elements are excluded and the basic filter
8 // element set is a set. It does not attempt to identify OP_RETURN scripts or to
9 // parse transactions: the caller must supply the basic filter element set,
10 // namely the previous output scripts spent by non-coinbase inputs plus the
11 // scriptPubKeys of all outputs except OP_RETURN outputs.
12 //
13 // The block hash is used in internal (little-endian) order, as for Build.
14 func BuildFromScripts(scripts [][]byte, blockHash [32]byte) []byte {
15 items := make([][]byte, 0, len(scripts))
16 seen := make(map[string]struct{}, len(scripts))
17 for _, script := range scripts {
18 if len(script) == 0 {
19 continue
20 }
21 if _, ok := seen[string(script)]; ok {
22 continue
23 }
24 seen[string(script)] = struct{}{}
25 items = append(items, script)
26 }
27 return Build(items, blockHash)
28 }
29