1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BitWasp\Bitcoin\Script\Factory; |
4
|
|
|
|
5
|
|
|
use BitWasp\Bitcoin\Script\Opcodes; |
6
|
|
|
use BitWasp\Bitcoin\Script\Parser\Operation; |
7
|
|
|
use BitWasp\Bitcoin\Script\Script; |
8
|
|
|
use BitWasp\Bitcoin\Script\ScriptInterface; |
9
|
|
|
use BitWasp\Bitcoin\Script\WitnessProgram; |
10
|
|
|
|
11
|
|
|
class WitnessScriptFactory |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var OutputScriptFactory |
15
|
|
|
*/ |
16
|
|
|
private $scriptPubKey; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var Opcodes |
20
|
|
|
*/ |
21
|
|
|
private $opcodes; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* WitnessScriptFactory constructor. |
25
|
|
|
* @param OutputScriptFactory $scriptPubKey |
26
|
|
|
* @param P2shScriptFactory $redeemScript |
27
|
|
|
* @param Opcodes $opcodes |
28
|
|
|
*/ |
29
|
|
|
public function __construct(OutputScriptFactory $scriptPubKey, P2shScriptFactory $redeemScript, Opcodes $opcodes) |
30
|
|
|
{ |
31
|
|
|
$this->scriptPubKey = $scriptPubKey; |
32
|
|
|
$this->redeemScript = $redeemScript; |
|
|
|
|
33
|
|
|
$this->opcodes = $opcodes; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param int $version |
38
|
|
|
* @param ScriptInterface $script |
39
|
|
|
* @return WitnessProgram |
40
|
|
|
*/ |
41
|
|
|
public function create($version, ScriptInterface $script) |
42
|
|
|
{ |
43
|
|
|
return new WitnessProgram($version, $script); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Parse a ScriptInterface into a WitnessProgram |
48
|
|
|
* |
49
|
|
|
* @param ScriptInterface $script |
50
|
|
|
* @return WitnessProgram |
51
|
|
|
*/ |
52
|
|
|
public function parse(ScriptInterface $script) |
53
|
|
|
{ |
54
|
|
|
$parser = $script->getScriptParser(); |
55
|
|
|
$decoded = $parser->decode(); |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var Operation $versionPush |
59
|
|
|
* @var Operation $witnessPush |
60
|
|
|
*/ |
61
|
|
|
list ($versionPush, $witnessPush) = $decoded; |
62
|
|
|
$version = $versionPush->getData()->getInt(); |
63
|
|
|
$program = new Script($witnessPush->getData()); |
64
|
|
|
|
65
|
|
|
return new WitnessProgram($version, $program, $this->opcodes); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: