/
blacklist.go
65 lines (62 loc) · 1.7 KB
/
blacklist.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package blockchain
import (
"github.com/cybriq/p9/pkg/btcaddr"
"github.com/cybriq/p9/pkg/fork"
"github.com/cybriq/p9/pkg/txscript"
"github.com/cybriq/p9/pkg/util"
)
// ContainsBlacklisted returns true if one of the given addresses is found in the transaction
func ContainsBlacklisted(
b *BlockChain, tx *util.Tx,
blacklist []btcaddr.Address,
) (hasBlacklisted bool) {
// in tests this function is not relevant
if b == nil {
return false
}
// blacklist only applies from hard fork
if fork.GetCurrent(b.BestSnapshot().Height) < 1 {
return false
}
var addrs []btcaddr.Address
// first decode transaction and collect all addresses in the transaction outputs
txo := tx.MsgTx().TxOut
for i := range txo {
script := txo[i].PkScript
_, a, _, _ := txscript.ExtractPkScriptAddrs(script, b.params)
addrs = append(addrs, a...)
}
// next get the addresses from the input transactions outpoints
txi := tx.MsgTx().TxIn
for i := range txi {
bb, e := b.BlockByHash(&txi[i].PreviousOutPoint.Hash)
if e == nil {
txs := bb.WireBlock().Transactions
for j := range txs {
txitxo := txs[j].TxOut
for k := range txitxo {
script := txitxo[k].PkScript
_, a, _, _ := txscript.ExtractPkScriptAddrs(
script,
b.params,
)
addrs = append(addrs, a...)
}
}
}
}
// check if the set of addresses intersects with the blacklist
return Intersects(addrs, blacklist)
}
// Intersects returns whether one slice of byte slices contains a match in another
func Intersects(a, b []btcaddr.Address) bool {
for x := range a {
for y := range b {
if a[x].EncodeAddress() == b[y].EncodeAddress() {
// If we find one match the two arrays intersect
return true
}
}
}
return false
}