1 | <?php |
||
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; |
||
|
|||
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 |