|
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\Classifier\OutputClassifier; |
|
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 ScriptInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
private $script; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var PublicKeyInterface[] |
|
29
|
|
|
*/ |
|
30
|
|
|
private $keys = []; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param ScriptInterface $script |
|
34
|
|
|
*/ |
|
35
|
36 |
|
public function __construct(ScriptInterface $script) |
|
36
|
|
|
{ |
|
37
|
36 |
|
$publicKeys = []; |
|
38
|
36 |
|
$parse = $script->getScriptParser()->decode(); |
|
39
|
36 |
|
if (count($parse) < 4) { |
|
40
|
|
|
throw new \InvalidArgumentException('Malformed multisig script'); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
36 |
|
$mCode = $parse[0]->getOp(); |
|
44
|
36 |
|
$nCode = $parse[count($parse) - 2]->getOp(); |
|
45
|
|
|
|
|
46
|
36 |
|
$this->m = (int) \BitWasp\Bitcoin\Script\decodeOpN($mCode); |
|
47
|
36 |
|
foreach (array_slice($parse, 1, -2) as $key) { |
|
48
|
|
|
/** @var \BitWasp\Bitcoin\Script\Parser\Operation $key */ |
|
49
|
36 |
|
if (!$key->isPush()) { |
|
50
|
|
|
throw new \RuntimeException('Malformed multisig script'); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
36 |
|
$publicKeys[] = PublicKeyFactory::fromHex($key->getData()); |
|
54
|
36 |
|
} |
|
55
|
|
|
|
|
56
|
36 |
|
$n = \BitWasp\Bitcoin\Script\decodeOpN($nCode); |
|
57
|
36 |
|
$this->n = count($publicKeys); |
|
58
|
36 |
|
if ($this->n === 0 || $this->n !== $n) { |
|
59
|
|
|
throw new \LogicException('No public keys found in script'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
36 |
|
$this->script = $script; |
|
63
|
36 |
|
$this->keys = $publicKeys; |
|
64
|
36 |
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @return int |
|
68
|
|
|
*/ |
|
69
|
24 |
|
public function getRequiredSigCount() |
|
70
|
|
|
{ |
|
71
|
24 |
|
return $this->m; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return int |
|
76
|
|
|
*/ |
|
77
|
30 |
|
public function getKeyCount() |
|
78
|
|
|
{ |
|
79
|
30 |
|
return $this->n; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @return string |
|
84
|
|
|
*/ |
|
85
|
|
|
public function classification() |
|
86
|
|
|
{ |
|
87
|
|
|
return OutputClassifier::MULTISIG; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param PublicKeyInterface $publicKey |
|
92
|
|
|
* @return bool |
|
93
|
|
|
*/ |
|
94
|
|
|
public function checkInvolvesKey(PublicKeyInterface $publicKey) |
|
95
|
|
|
{ |
|
96
|
|
|
$binary = $publicKey->getBinary(); |
|
97
|
|
|
foreach ($this->keys as $key) { |
|
98
|
|
|
if ($key->getBinary() === $binary) { |
|
99
|
|
|
return true; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return false; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @return \BitWasp\Bitcoin\Crypto\EcAdapter\Key\PublicKeyInterface[] |
|
108
|
|
|
*/ |
|
109
|
36 |
|
public function getKeys() |
|
110
|
|
|
{ |
|
111
|
36 |
|
return $this->keys; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|