1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BitWasp\Bitcoin\Script; |
4
|
|
|
|
5
|
|
|
use BitWasp\Buffertools\BufferInterface; |
6
|
|
|
|
7
|
|
|
class WitnessProgram |
8
|
|
|
{ |
9
|
|
|
const V0 = 0; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @var int |
13
|
|
|
*/ |
14
|
|
|
private $version; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var BufferInterface |
18
|
|
|
*/ |
19
|
|
|
private $program; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var |
23
|
|
|
*/ |
24
|
|
|
private $outputScript; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* WitnessProgram constructor. |
28
|
|
|
* @param int $version |
29
|
|
|
* @param BufferInterface $program |
30
|
|
|
*/ |
31
|
190 |
|
public function __construct($version, BufferInterface $program) |
32
|
|
|
{ |
33
|
190 |
|
if ($this->version < 0 || $this->version > 16) { |
34
|
|
|
throw new \RuntimeException("Invalid witness program version"); |
35
|
|
|
} |
36
|
|
|
|
37
|
190 |
|
if ($this->version === 0 && ($program->getSize() !== 20 && $program->getSize() !== 32)) { |
38
|
190 |
|
throw new \RuntimeException('Invalid size for V0 witness program - must be 20 or 32 bytes'); |
39
|
190 |
|
} |
40
|
|
|
|
41
|
|
|
$this->version = $version; |
42
|
|
|
$this->program = $program; |
43
|
|
|
} |
44
|
|
|
|
45
|
20 |
|
/** |
46
|
|
|
* @param BufferInterface $program |
47
|
20 |
|
* @return WitnessProgram |
48
|
6 |
|
*/ |
49
|
14 |
|
public static function v0(BufferInterface $program) |
50
|
12 |
|
{ |
51
|
|
|
if ($program->getSize() === 20) { |
52
|
2 |
|
return new self(self::V0, $program); |
53
|
|
|
} else if ($program->getSize() === 32) { |
54
|
|
|
return new self(self::V0, $program); |
55
|
|
|
} else { |
56
|
|
|
throw new \RuntimeException('Invalid size for V0 witness program - must be 20 or 32 bytes'); |
57
|
|
|
} |
58
|
|
|
} |
59
|
170 |
|
|
60
|
|
|
/** |
61
|
170 |
|
* @return int |
62
|
|
|
*/ |
63
|
|
|
public function getVersion() |
64
|
|
|
{ |
65
|
|
|
return $this->version; |
66
|
|
|
} |
67
|
168 |
|
|
68
|
|
|
/** |
69
|
168 |
|
* @return BufferInterface |
70
|
|
|
*/ |
71
|
|
|
public function getProgram() |
72
|
|
|
{ |
73
|
|
|
return $this->program; |
74
|
|
|
} |
75
|
26 |
|
|
76
|
|
|
/** |
77
|
26 |
|
* @return ScriptInterface |
78
|
24 |
|
*/ |
79
|
|
|
public function getScript() |
80
|
|
|
{ |
81
|
26 |
|
if (null === $this->outputScript) { |
82
|
|
|
$this->outputScript = ScriptFactory::sequence([encodeOpN($this->version), $this->program]); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $this->outputScript; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|