1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BitWasp\Bitcoin\Script; |
4
|
|
|
|
5
|
|
|
use BitWasp\Bitcoin\Bitcoin; |
6
|
|
|
use BitWasp\Bitcoin\Crypto\EcAdapter\Adapter\EcAdapterInterface; |
7
|
|
|
use BitWasp\Bitcoin\Math\Math; |
8
|
|
|
use BitWasp\Bitcoin\Script\Consensus\BitcoinConsensus; |
9
|
|
|
use BitWasp\Bitcoin\Script\Consensus\NativeConsensus; |
10
|
|
|
use BitWasp\Bitcoin\Script\Factory\OutputScriptFactory; |
11
|
|
|
use BitWasp\Bitcoin\Script\Factory\ScriptCreator; |
12
|
|
|
use BitWasp\Bitcoin\Script\Parser\Operation; |
13
|
|
|
use BitWasp\Buffertools\Buffer; |
14
|
|
|
use BitWasp\Buffertools\BufferInterface; |
15
|
|
|
|
16
|
|
|
class ScriptFactory |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var OutputScriptFactory |
20
|
|
|
*/ |
21
|
|
|
private static $outputScriptFactory = null; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param BufferInterface|string $string |
25
|
|
|
* @return ScriptInterface |
26
|
|
|
*/ |
27
|
46 |
|
public static function fromHex($string) |
28
|
|
|
{ |
29
|
46 |
|
return self::create($string instanceof BufferInterface ? $string : Buffer::hex($string))->getScript(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param BufferInterface|null $buffer |
34
|
|
|
* @param Opcodes|null $opcodes |
35
|
|
|
* @param Math|null $math |
36
|
|
|
* @return ScriptCreator |
37
|
|
|
*/ |
38
|
2776 |
|
public static function create(BufferInterface $buffer = null, Opcodes $opcodes = null, Math $math = null) |
39
|
|
|
{ |
40
|
2776 |
|
return new ScriptCreator($math ?: Bitcoin::getMath(), $opcodes ?: new Opcodes(), $buffer); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Create a script consisting only of push-data operations. |
45
|
|
|
* Suitable for a scriptSig. |
46
|
|
|
* |
47
|
2712 |
|
* @param BufferInterface[] $buffers |
48
|
|
|
* @return ScriptInterface |
49
|
2712 |
|
*/ |
50
|
|
|
public static function pushAll(array $buffers) |
51
|
|
|
{ |
52
|
|
|
return self::sequence(array_map(function ($buffer) { |
53
|
|
|
if (!($buffer instanceof BufferInterface)) { |
54
|
|
|
throw new \RuntimeException('Script contained a non-push opcode'); |
55
|
|
|
} |
56
|
40 |
|
|
57
|
|
|
$size = $buffer->getSize(); |
58
|
40 |
|
if ($size === 0) { |
59
|
40 |
|
return Opcodes::OP_0; |
60
|
38 |
|
} |
61
|
|
|
|
62
|
|
|
$first = ord($buffer->getBinary()[0]); |
63
|
|
|
if ($size === 1 && $first >= 1 && $first <= 16) { |
64
|
38 |
|
return \BitWasp\Bitcoin\Script\encodeOpN($first); |
65
|
|
|
} else { |
66
|
|
|
return $buffer; |
67
|
40 |
|
} |
68
|
|
|
}, $buffers)); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param int[]|\BitWasp\Bitcoin\Script\Interpreter\Number[]|BufferInterface[] $sequence |
73
|
188 |
|
* @return ScriptInterface |
74
|
|
|
*/ |
75
|
188 |
|
public static function sequence(array $sequence) |
76
|
|
|
{ |
77
|
|
|
return self::create()->sequence($sequence)->getScript(); |
78
|
|
|
} |
79
|
188 |
|
|
80
|
|
|
/** |
81
|
|
|
* @param Operation[] $operations |
82
|
|
|
* @return ScriptInterface |
83
|
|
|
*/ |
84
|
|
|
public static function fromOperations(array $operations) |
85
|
|
|
{ |
86
|
100 |
|
$sequence = []; |
87
|
|
|
foreach ($operations as $operation) { |
88
|
100 |
|
if (!($operation instanceof Operation)) { |
89
|
|
|
throw new \RuntimeException("Invalid input to fromOperations"); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$sequence[] = $operation->encode(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return self::sequence($sequence); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return OutputScriptFactory |
100
|
|
|
*/ |
101
|
|
|
public static function scriptPubKey() |
102
|
|
|
{ |
103
|
98 |
|
if (self::$outputScriptFactory === null) { |
104
|
|
|
self::$outputScriptFactory = new OutputScriptFactory(); |
105
|
98 |
|
} |
106
|
|
|
|
107
|
|
|
return self::$outputScriptFactory; |
108
|
98 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param EcAdapterInterface|null $ecAdapter |
112
|
|
|
* @return NativeConsensus |
113
|
|
|
*/ |
114
|
|
|
public static function getNativeConsensus(EcAdapterInterface $ecAdapter = null) |
115
|
|
|
{ |
116
|
|
|
return new NativeConsensus($ecAdapter ?: Bitcoin::getEcAdapter()); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return BitcoinConsensus |
121
|
|
|
*/ |
122
|
|
|
public static function getBitcoinConsensus() |
123
|
|
|
{ |
124
|
|
|
return new BitcoinConsensus(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param EcAdapterInterface|null $ecAdapter |
129
|
|
|
* @return \BitWasp\Bitcoin\Script\Consensus\ConsensusInterface |
130
|
|
|
*/ |
131
|
|
|
public static function consensus(EcAdapterInterface $ecAdapter = null) |
132
|
|
|
{ |
133
|
|
|
if (extension_loaded('bitcoinconsensus')) { |
134
|
|
|
return self::getBitcoinConsensus(); |
135
|
|
|
} else { |
136
|
|
|
return self::getNativeConsensus($ecAdapter); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|