Completed
Pull Request — master (#518)
by thomas
58:22 queued 55:50
created

Multisig::checkInvolvesKey()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 3
rs 9.4285
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
14
{
15
    /**
16
     * @var int
17
     */
18
    private $m;
19
20
    /**
21
     * @var int
22
     */
23
    private $n;
24
25
    /**
26
     * @var BufferInterface[]
27
     */
28
    private $keyBuffers = [];
29
30
    /**
31
     * @var PublicKeySerializerInterface
32
     */
33
    private $pubKeySerializer;
34
35
    /**
36
     * Multisig constructor.
37
     * @param ScriptInterface $script
38
     * @param PublicKeySerializerInterface|null $pubKeySerializer
39
     */
40 26
    public function __construct(ScriptInterface $script, PublicKeySerializerInterface $pubKeySerializer = null)
41
    {
42 26
        if (null === $pubKeySerializer) {
43 6
            $pubKeySerializer = EcSerializer::getSerializer(PublicKeySerializerInterface::class, false, Bitcoin::getEcAdapter());
44
        }
45
46 26
        $parse = $script->getScriptParser()->decode();
47 26
        if (count($parse) < 4 || end($parse)->getOp() !== Opcodes::OP_CHECKMULTISIG) {
48
            throw new \InvalidArgumentException('Malformed multisig script');
49
        }
50
51 26
        $mCode = $parse[0]->getOp();
52 26
        $nCode = $parse[count($parse) - 2]->getOp();
53
54 26
        $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...
55 26
        $publicKeyBuffers = [];
56 26
        foreach (array_slice($parse, 1, -2) as $key) {
57
            /** @var \BitWasp\Bitcoin\Script\Parser\Operation $key */
58 26
            if (!$key->isPush()) {
59
                throw new \RuntimeException('Malformed multisig script');
60
            }
61
62 26
            $buffer = $key->getData();
63 26
            $publicKeyBuffers[] = $buffer;
64
        }
65
66 26
        $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...
67 26
        if ($this->n === 0 || $this->n !== count($publicKeyBuffers)) {
68
            throw new \LogicException('No public keys found in script');
69
        }
70 26
        $this->keyBuffers = $publicKeyBuffers;
71 26
        $this->pubKeySerializer = $pubKeySerializer;
72 26
    }
73
74
    /**
75
     * @return int
76
     */
77 24
    public function getRequiredSigCount()
78
    {
79 24
        return $this->m;
80
    }
81
82
    /**
83
     * @return int
84
     */
85 22
    public function getKeyCount()
86
    {
87 22
        return $this->n;
88
    }
89
90
    /**
91
     * @param PublicKeyInterface $publicKey
92
     * @return bool
93
     */
94 2
    public function checkInvolvesKey(PublicKeyInterface $publicKey)
95
    {
96 2
        $buffer = $this->pubKeySerializer->serialize($publicKey);
97 2
        foreach ($this->keyBuffers as $key) {
98 2
            if ($key->equals($buffer)) {
99 2
                return true;
100
            }
101
        }
102
103 2
        return false;
104
    }
105
106
    /**
107
     * @return array|BufferInterface[]
108
     */
109 24
    public function getKeyBuffers()
110
    {
111 24
        return $this->keyBuffers;
112
    }
113
}
114