Completed
Pull Request — master (#286)
by thomas
23:49
created

Multisig::__construct()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 7.2269

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 28
ccs 15
cts 18
cp 0.8333
rs 6.7272
cc 7
eloc 16
nc 6
nop 1
crap 7.2269
1
<?php
2
3
namespace BitWasp\Bitcoin\Script\ScriptInfo;
4
5
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PublicKeyInterface;
6
use BitWasp\Bitcoin\Key\PublicKeyFactory;
7
use BitWasp\Bitcoin\Script\Opcodes;
8
use BitWasp\Bitcoin\Script\ScriptInterface;
9
10
class Multisig implements ScriptInfoInterface
11
{
12
    /**
13
     * @var int
14
     */
15
    private $m;
16
17
    /**
18
     * @var int
19
     */
20
    private $n;
21
22
    /**
23
     * @var PublicKeyInterface[]
24
     */
25
    private $keys = [];
26
27
    /**
28
     * @param ScriptInterface $script
29
     */
30 42
    public function __construct(ScriptInterface $script)
31
    {
32 42
        $publicKeys = [];
33 42
        $parse = $script->getScriptParser()->decode();
34 42
        if (count($parse) < 4 || end($parse)->getOp() !== Opcodes::OP_CHECKMULTISIG) {
35
            throw new \InvalidArgumentException('Malformed multisig script');
36
        }
37
38 42
        $mCode = $parse[0]->getOp();
39 42
        $nCode = $parse[count($parse) - 2]->getOp();
40
41 42
        $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...
42 42
        foreach (array_slice($parse, 1, -2) as $key) {
43
            /** @var \BitWasp\Bitcoin\Script\Parser\Operation $key */
44 42
            if (!$key->isPush()) {
45
                throw new \RuntimeException('Malformed multisig script');
46
            }
47
48 42
            $publicKeys[] = PublicKeyFactory::fromHex($key->getData());
49 42
        }
50
51 42
        $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...
52 42
        if ($this->n === 0 || $this->n !== count($publicKeys)) {
53
            throw new \LogicException('No public keys found in script');
54
        }
55
56 42
        $this->keys = $publicKeys;
57 42
    }
58
59
    /**
60
     * @return int
61
     */
62 30
    public function getRequiredSigCount()
63
    {
64 30
        return $this->m;
65
    }
66
67
    /**
68
     * @return int
69
     */
70 36
    public function getKeyCount()
71
    {
72 36
        return $this->n;
73
    }
74
75
    /**
76
     * @param PublicKeyInterface $publicKey
77
     * @return bool
78
     */
79 6
    public function checkInvolvesKey(PublicKeyInterface $publicKey)
80
    {
81 6
        $binary = $publicKey->getBinary();
82 6
        foreach ($this->keys as $key) {
83 6
            if ($key->getBinary() === $binary) {
84 6
                return true;
85
            }
86 6
        }
87
88 6
        return false;
89
    }
90
91
    /**
92
     * @return \BitWasp\Bitcoin\Crypto\EcAdapter\Key\PublicKeyInterface[]
93
     */
94 36
    public function getKeys()
95
    {
96 36
        return $this->keys;
97
    }
98
}
99