Completed
Pull Request — master (#286)
by thomas
04:30
created

Multisig::__construct()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 6.1417
Metric Value
dl 0
loc 29
ccs 16
cts 19
cp 0.8421
rs 8.439
cc 6
eloc 17
nc 6
nop 1
crap 6.1417
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 36
    public function __construct(ScriptInterface $script)
31
    {
32 36
        $publicKeys = [];
33 36
        $parse = $script->getScriptParser()->decode();
34 36
        if (count($parse) < 4 || end($parse)->getOp() !== Opcodes::OP_CHECKMULTISIG) {
35
            throw new \InvalidArgumentException('Malformed multisig script');
36
        }
37
38 36
        $mCode = $parse[0]->getOp();
39 36
        $nCode = $parse[count($parse) - 2]->getOp();
40
41 36
        $this->m = \BitWasp\Bitcoin\Script\decodeOpN($mCode);
42 36
        foreach (array_slice($parse, 1, -2) as $key) {
43
            /** @var \BitWasp\Bitcoin\Script\Parser\Operation $key */
44 36
            if (!$key->isPush()) {
45
                throw new \RuntimeException('Malformed multisig script');
46
            }
47
48 36
            $publicKeys[] = PublicKeyFactory::fromHex($key->getData());
49 36
        }
50
51 36
        $this->n = \BitWasp\Bitcoin\Script\decodeOpN($nCode);
52 36
        if ($this->n === 0 || $this->n !== count($publicKeys)) {
53 36
            throw new \LogicException('No public keys found in script');
54
        }\
55
56
        $this->keys = $publicKeys;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_VARIABLE, expecting T_STRING
Loading history...
57 36
    }
58 36
59
    /**
60
     * @return int
61
     */
62
    public function getRequiredSigCount()
63 24
    {
64
        return $this->m;
65 24
    }
66
67
    /**
68
     * @return int
69
     */
70
    public function getKeyCount()
71 30
    {
72
        return $this->n;
73 30
    }
74
75
    /**
76
     * @param PublicKeyInterface $publicKey
77
     * @return bool
78
     */
79
    public function checkInvolvesKey(PublicKeyInterface $publicKey)
80
    {
81
        $binary = $publicKey->getBinary();
82
        foreach ($this->keys as $key) {
83
            if ($key->getBinary() === $binary) {
84
                return true;
85
            }
86
        }
87
88
        return false;
89
    }
90
91
    /**
92
     * @return \BitWasp\Bitcoin\Crypto\EcAdapter\Key\PublicKeyInterface[]
93
     */
94
    public function getKeys()
95 36
    {
96
        return $this->keys;
97 36
    }
98
}
99