1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BitWasp\Bitcoin\Script\Factory; |
4
|
|
|
|
5
|
|
|
use BitWasp\Bitcoin\Script\Opcodes; |
6
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Key\PublicKeyInterface; |
7
|
|
|
use BitWasp\Bitcoin\Script\Parser\Operation; |
8
|
|
|
use BitWasp\Bitcoin\Script\Script; |
9
|
|
|
use BitWasp\Bitcoin\Script\ScriptInterface; |
10
|
|
|
use BitWasp\Bitcoin\Script\WitnessProgram; |
11
|
|
|
|
12
|
|
|
class WitnessScriptFactory |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var OutputScriptFactory |
16
|
|
|
*/ |
17
|
|
|
private $scriptPubKey; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Opcodes |
21
|
|
|
*/ |
22
|
|
|
private $opcodes; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* WitnessScriptFactory constructor. |
26
|
|
|
* @param OutputScriptFactory $scriptPubKey |
27
|
|
|
* @param Opcodes $opcodes |
28
|
|
|
*/ |
29
|
|
|
public function __construct(OutputScriptFactory $scriptPubKey, Opcodes $opcodes) |
30
|
|
|
{ |
31
|
|
|
$this->scriptPubKey = $scriptPubKey; |
32
|
|
|
$this->opcodes = $opcodes; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Parse a ScriptInterface into a WitnessProgram |
37
|
|
|
* |
38
|
|
|
* @param ScriptInterface $script |
39
|
|
|
* @return WitnessProgram |
40
|
|
|
*/ |
41
|
|
|
public function parse(ScriptInterface $script) |
42
|
|
|
{ |
43
|
|
|
$parser = $script->getScriptParser(); |
44
|
|
|
$decoded = $parser->decode(); |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var Operation $versionPush |
48
|
|
|
* @var Operation $witnessPush |
49
|
|
|
*/ |
50
|
|
|
list ($versionPush, $witnessPush) = $decoded; |
51
|
|
|
$version = $versionPush->getData()->getInt(); |
52
|
|
|
$program = new Script($witnessPush->getData()); |
53
|
|
|
|
54
|
|
|
return new WitnessProgram($version, $program, $this->opcodes); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Create a multisig witness program |
59
|
|
|
* |
60
|
|
|
* @param int $version |
61
|
|
|
* @param int $m |
62
|
|
|
* @param array $keys |
63
|
|
|
* @param bool|true $sort |
64
|
|
|
* @return WitnessProgram |
65
|
|
|
*/ |
66
|
|
|
public function multisig($version, $m, array $keys, $sort = true) |
67
|
|
|
{ |
68
|
|
|
return new WitnessProgram($version, $this->scriptPubKey->multisig($m, $keys, $sort), $this->opcodes); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Create a pay-to-pubkey witness program |
73
|
|
|
* |
74
|
|
|
* @param int $version |
75
|
|
|
* @param PublicKeyInterface $publicKey |
76
|
|
|
* @return WitnessProgram |
77
|
|
|
*/ |
78
|
|
|
public function payToPubKey($version, PublicKeyInterface $publicKey) |
79
|
|
|
{ |
80
|
|
|
return new WitnessProgram($version, $this->scriptPubKey->payToPubKey($publicKey), $this->opcodes); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Create a pay-to-pubkey-hash witness program |
85
|
|
|
* |
86
|
|
|
* @param int $version |
87
|
|
|
* @param PublicKeyInterface $publicKey |
88
|
|
|
* @return WitnessProgram |
89
|
|
|
*/ |
90
|
|
|
public function payToPubKeyHash($version, PublicKeyInterface $publicKey) |
91
|
|
|
{ |
92
|
|
|
return new WitnessProgram($version, $this->scriptPubKey->payToPubKeyHash($publicKey), $this->opcodes); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|