Completed
Pull Request — master (#451)
by thomas
65:43 queued 63:02
created

Multisig::getRequiredSigCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace BitWasp\Bitcoin\Script\ScriptInfo;
4
5
use BitWasp\Bitcoin\Bitcoin;
6
use BitWasp\Bitcoin\Crypto\EcAdapter\EcSerializer;
7
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PublicKeyInterface;
8
use BitWasp\Bitcoin\Crypto\EcAdapter\Serializer\Key\PublicKeySerializerInterface;
9
use BitWasp\Bitcoin\Script\Opcodes;
10
use BitWasp\Bitcoin\Script\ScriptInterface;
11
use BitWasp\Buffertools\BufferInterface;
12
13
class Multisig implements ScriptInfoInterface
14
{
15
    /**
16
     * @var int
17
     */
18
    private $m;
19
20
    /**
21
     * @var int
22
     */
23
    private $n;
24
25
    /**
26
     * @var PublicKeyInterface[]
27
     */
28
    private $keys = [];
29
30
    /**
31
     * @var BufferInterface[]
32
     */
33
    private $keyBuffers = [];
34
35
    /**
36
     * @var PublicKeySerializerInterface
37
     */
38
    private $pubKeySerializer;
39
40
    /**
41
     * Multisig constructor.
42
     * @param ScriptInterface $script
43
     * @param PublicKeySerializerInterface|null $pubKeySerializer
44
     */
45 20
    public function __construct(ScriptInterface $script, PublicKeySerializerInterface $pubKeySerializer = null)
46
    {
47 20
        if (null === $pubKeySerializer) {
48 6
            $pubKeySerializer = EcSerializer::getSerializer(PublicKeySerializerInterface::class, false, Bitcoin::getEcAdapter());
49
        }
50
51 20
        $publicKeys = [];
52 20
        $parse = $script->getScriptParser()->decode();
53 20
        if (count($parse) < 4 || end($parse)->getOp() !== Opcodes::OP_CHECKMULTISIG) {
54
            throw new \InvalidArgumentException('Malformed multisig script');
55
        }
56
57 20
        $mCode = $parse[0]->getOp();
58 20
        $nCode = $parse[count($parse) - 2]->getOp();
59
60 20
        $this->m = \BitWasp\Bitcoin\Script\decodeOpN($mCode);
0 ignored issues
show
Documentation Bug introduced by
It seems like \BitWasp\Bitcoin\Script\decodeOpN($mCode) can also be of type double. However, the property $m is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
61 20
        $publicKeyBuffers = [];
62 20
        foreach (array_slice($parse, 1, -2) as $key) {
63
            /** @var \BitWasp\Bitcoin\Script\Parser\Operation $key */
64 20
            if (!$key->isPush()) {
65
                throw new \RuntimeException('Malformed multisig script');
66
            }
67
68 20
            $buffer = $key->getData();
69 20
            $publicKeys[] = $pubKeySerializer->parse($buffer);
70 20
            $publicKeyBuffers[] = $buffer;
71
        }
72
73 20
        $this->n = \BitWasp\Bitcoin\Script\decodeOpN($nCode);
0 ignored issues
show
Documentation Bug introduced by
It seems like \BitWasp\Bitcoin\Script\decodeOpN($nCode) can also be of type double. However, the property $n is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
74 20
        if ($this->n === 0 || $this->n !== count($publicKeys)) {
75
            throw new \LogicException('No public keys found in script');
76
        }
77
78 20
        $this->keys = $publicKeys;
79 20
        $this->keyBuffers = $publicKeyBuffers;
80 20
        $this->pubKeySerializer = $pubKeySerializer;
81 20
    }
82
83
    /**
84
     * @return int
85
     */
86 18
    public function getRequiredSigCount()
87
    {
88 18
        return $this->m;
89
    }
90
91
    /**
92
     * @return int
93
     */
94 2
    public function getKeyCount()
95
    {
96 2
        return $this->n;
97
    }
98
99
    /**
100
     * @param PublicKeyInterface $publicKey
101
     * @return bool
102
     */
103 2
    public function checkInvolvesKey(PublicKeyInterface $publicKey)
104
    {
105 2
        $buffer = $this->pubKeySerializer->serialize($publicKey);
106 2
        foreach ($this->keyBuffers as $key) {
107 2
            if ($key->equals($buffer)) {
0 ignored issues
show
Documentation introduced by
$buffer is of type object<BitWasp\Buffertools\BufferInterface>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
108 2
                return true;
109
            }
110
        }
111
112 2
        return false;
113
    }
114
115
    /**
116
     * @return \BitWasp\Bitcoin\Crypto\EcAdapter\Key\PublicKeyInterface[]
117
     */
118 16
    public function getKeys()
119
    {
120 16
        return $this->keys;
121
    }
122
123
    /**
124
     * @return array|BufferInterface[]
125
     */
126 14
    public function getKeyBuffers()
127
    {
128 14
        return $this->keyBuffers;
129
    }
130
}
131