Completed
Pull Request — master (#270)
by thomas
73:11
created

Bip69   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 0
cbo 8
dl 0
loc 63
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A sortInputs() 0 5 1
A compareInputs() 0 8 2
A sortOutputs() 0 5 1
A compareOutputs() 0 4 2
A check() 0 12 2
1
<?php
2
3
namespace BitWasp\Bitcoin\Transaction\Bip69;
4
5
6
use BitWasp\Bitcoin\Transaction\TransactionInputInterface;
7
use BitWasp\Bitcoin\Transaction\TransactionInterface;
8
use BitWasp\Bitcoin\Transaction\TransactionOutputInterface;
9
10
class Bip69
11
{
12
    /**
13
     * @param array $vTxin
14
     * @return array
15
     */
16
    public function sortInputs(array $vTxin)
17
    {
18
        usort($vTxin, [$this, 'compareInputs']);
19
        return $vTxin;
20
    }
21
22
    /**
23
     * @param TransactionInputInterface $tx1
24
     * @param TransactionInputInterface $tx2
25
     * @return bool
26
     */
27
    public function compareInputs(TransactionInputInterface $tx1, TransactionInputInterface $tx2)
28
    {
29
        $outpoint1 = $tx1->getOutPoint();
30
        $outpoint2 = $tx2->getOutPoint();
31
        $txid1 = $outpoint1->getTxId();
32
        $txid2 = $outpoint2->getTxId();
33
        return strcmp($txid1->getBinary(), $txid2->getBinary()) || $outpoint1->getVout() - $outpoint2->getVout();
34
    }
35
36
    /**
37
     * @param TransactionOutputInterface[] $vTxout
38
     * @return TransactionOutputInterface[]
39
     */
40
    public function sortOutputs($vTxout)
41
    {
42
        usort($vTxout, [$this, 'compareOutputs']);
43
        return $vTxout;
44
    }
45
46
    /**
47
     * @param TransactionOutputInterface $tx1
48
     * @param TransactionOutputInterface $tx2
49
     * @return bool
50
     */
51
    public function compareOutputs(TransactionOutputInterface $tx1, TransactionOutputInterface $tx2)
52
    {
53
        return $tx1->getValue() - $tx2->getValue() || strcmp($tx1->getScript()->getBinary(), $tx2->getScript()->getBinary());
54
    }
55
56
    /**
57
     * @param TransactionInterface $tx
58
     * @return bool
59
     */
60
    public function check(TransactionInterface $tx)
61
    {
62
        $inputs = $tx->getInputs()->all();
63
        $sortedInputs = $this->sortInputs($inputs);
64
65
        $outputs = $tx->getOutputs()->all();
66
        $sortedOutputs = $this->sortOutputs($outputs);
67
68
        $valid = $sortedInputs === $inputs && $sortedOutputs === $sortedOutputs;
69
70
        return $valid;
71
    }
72
}