|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BitWasp\Bitcoin\Script\Parser; |
|
4
|
|
|
|
|
5
|
|
|
use BitWasp\Bitcoin\Math\Math; |
|
6
|
|
|
use BitWasp\Bitcoin\Script\Opcodes; |
|
7
|
|
|
use BitWasp\Bitcoin\Script\Script; |
|
8
|
|
|
use BitWasp\Bitcoin\Script\ScriptInterface; |
|
9
|
|
|
use BitWasp\Buffertools\Buffer; |
|
10
|
|
|
use BitWasp\Buffertools\BufferInterface; |
|
11
|
|
|
|
|
12
|
|
|
class Parser implements \Iterator |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var Math |
|
16
|
|
|
*/ |
|
17
|
|
|
private $math; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var BufferInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
private $empty; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var ScriptInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
private $script; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var int |
|
31
|
|
|
*/ |
|
32
|
|
|
private $count = 0; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var int |
|
36
|
|
|
*/ |
|
37
|
|
|
private $position = 0; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var int |
|
41
|
|
|
*/ |
|
42
|
|
|
private $end = 0; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var int |
|
46
|
|
|
*/ |
|
47
|
|
|
private $execPtr = 0; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var string |
|
51
|
|
|
*/ |
|
52
|
|
|
private $data = ''; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var Operation[] |
|
56
|
|
|
*/ |
|
57
|
|
|
private $array = array(); |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* ScriptParser constructor. |
|
61
|
|
|
* @param Math $math |
|
62
|
|
|
* @param ScriptInterface $script |
|
63
|
|
|
*/ |
|
64
|
2574 |
|
public function __construct(Math $math, ScriptInterface $script) |
|
65
|
|
|
{ |
|
66
|
2574 |
|
$this->math = $math; |
|
67
|
2574 |
|
$buffer = $script->getBuffer(); |
|
68
|
2574 |
|
$this->data = $buffer->getBinary(); |
|
69
|
2574 |
|
$this->end = $buffer->getSize(); |
|
70
|
2574 |
|
$this->script = $script; |
|
71
|
2574 |
|
$this->empty = new Buffer('', 0, $math); |
|
72
|
2574 |
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return int |
|
76
|
|
|
*/ |
|
77
|
2 |
|
public function getPosition() |
|
78
|
|
|
{ |
|
79
|
2 |
|
return $this->position; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param string $packFormat |
|
84
|
|
|
* @param integer $strSize |
|
85
|
|
|
* @return array|bool |
|
86
|
|
|
*/ |
|
87
|
72 |
|
private function unpackSize($packFormat, $strSize) |
|
88
|
|
|
{ |
|
89
|
72 |
|
if ($this->end - $this->position < $strSize) { |
|
90
|
|
|
return false; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
72 |
|
$size = unpack($packFormat, substr($this->data, $this->position, $strSize)); |
|
94
|
72 |
|
$size = $size[1]; |
|
95
|
72 |
|
$this->position += $strSize; |
|
96
|
|
|
|
|
97
|
72 |
|
return $size; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param int $ptr |
|
102
|
|
|
* @return Operation |
|
103
|
|
|
*/ |
|
104
|
2548 |
|
private function doNext($ptr) |
|
105
|
|
|
{ |
|
106
|
2548 |
|
if ($this->position >= $this->end) { |
|
107
|
|
|
throw new \RuntimeException('Position exceeds end of script!'); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
2548 |
|
$opCode = ord($this->data[$this->position++]); |
|
111
|
2548 |
|
$pushData = $this->empty; |
|
112
|
2548 |
|
$dataSize = 0; |
|
113
|
|
|
|
|
114
|
2548 |
|
if ($opCode <= Opcodes::OP_PUSHDATA4) { |
|
115
|
1956 |
|
if ($opCode < Opcodes::OP_PUSHDATA1) { |
|
116
|
1916 |
|
$dataSize = $opCode; |
|
117
|
72 |
|
} else if ($opCode === Opcodes::OP_PUSHDATA1) { |
|
118
|
38 |
|
$dataSize = $this->unpackSize('C', 1); |
|
119
|
38 |
|
} else if ($opCode === Opcodes::OP_PUSHDATA2) { |
|
120
|
28 |
|
$dataSize = $this->unpackSize('v', 2); |
|
121
|
|
|
} else { |
|
122
|
10 |
|
$dataSize = $this->unpackSize('V', 4); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
1956 |
|
$delta = ($this->end - $this->position); |
|
126
|
1956 |
|
if ($dataSize === false || $delta < 0 || $delta < $dataSize) { |
|
127
|
10 |
|
throw new \RuntimeException('Failed to unpack data from Script'); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
1950 |
|
if ($dataSize > 0) { |
|
131
|
1470 |
|
$pushData = new Buffer(substr($this->data, $this->position, $dataSize), $dataSize, $this->math); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
1950 |
|
$this->position += $dataSize; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
2542 |
|
$this->array[$ptr] = new Operation($opCode, $pushData, $dataSize); |
|
138
|
2542 |
|
$this->count++; |
|
139
|
|
|
|
|
140
|
2542 |
|
return $this->array[$ptr]; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @param $begin |
|
145
|
|
|
* @param null|int $length |
|
146
|
|
|
* @return Script |
|
147
|
|
|
*/ |
|
148
|
130 |
|
public function slice($begin, $length = null) |
|
149
|
|
|
{ |
|
150
|
130 |
|
if ($begin < 0) { |
|
151
|
|
|
throw new \RuntimeException("Invalid start of script - cannot be negative or "); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
130 |
|
$maxLength = $this->end - $begin; |
|
155
|
|
|
|
|
156
|
130 |
|
if (null === $length) { |
|
157
|
|
|
$length = $maxLength; |
|
158
|
|
|
} else { |
|
159
|
130 |
|
if ($length > $maxLength) { |
|
160
|
|
|
throw new \RuntimeException("Cannot slice this much from script"); |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
130 |
|
return new Script(new Buffer(substr($this->data, $begin, $length))); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* |
|
169
|
|
|
*/ |
|
170
|
2560 |
|
public function rewind() |
|
171
|
|
|
{ |
|
172
|
2560 |
|
$this->execPtr = 0; |
|
173
|
2560 |
|
$this->position = 0; |
|
174
|
2560 |
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @return Operation |
|
178
|
|
|
*/ |
|
179
|
2548 |
|
public function current() |
|
180
|
|
|
{ |
|
181
|
2548 |
|
if (isset($this->array[$this->execPtr])) { |
|
182
|
130 |
|
$exec = $this->array[$this->execPtr]; |
|
183
|
|
|
} else { |
|
184
|
2548 |
|
$exec = $this->doNext($this->execPtr); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
2542 |
|
return $exec; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* @return int |
|
192
|
|
|
*/ |
|
193
|
|
|
public function key() |
|
194
|
|
|
{ |
|
195
|
|
|
return $this->execPtr; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @return Operation |
|
200
|
|
|
*/ |
|
201
|
2488 |
|
public function next() |
|
202
|
|
|
{ |
|
203
|
2488 |
|
$ptr = $this->execPtr; |
|
204
|
2488 |
|
if (isset($this->array[$ptr])) { |
|
205
|
2476 |
|
$this->execPtr++; |
|
206
|
2476 |
|
return $this->array[$ptr]; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
12 |
|
return null; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* @return bool |
|
214
|
|
|
*/ |
|
215
|
2560 |
|
public function valid() |
|
216
|
|
|
{ |
|
217
|
2560 |
|
return isset($this->array[$this->execPtr]) || $this->position < $this->end; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* @return Operation[] |
|
222
|
|
|
*/ |
|
223
|
1302 |
|
public function decode() |
|
224
|
|
|
{ |
|
225
|
1302 |
|
$result = []; |
|
226
|
1302 |
|
foreach ($this as $operation) { |
|
227
|
1290 |
|
$result[] = $operation; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
1302 |
|
return $result; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* @return string |
|
235
|
|
|
*/ |
|
236
|
6 |
|
public function getHumanReadable() |
|
237
|
|
|
{ |
|
238
|
6 |
|
return implode(' ', array_map( |
|
239
|
6 |
|
function (Operation $operation) { |
|
240
|
4 |
|
$op = $operation->getOp(); |
|
241
|
4 |
|
if ($op === Opcodes::OP_0 || $op === Opcodes::OP_1NEGATE || $op >= Opcodes::OP_1 && $op <= Opcodes::OP_16) { |
|
242
|
|
|
return $this->script->getOpcodes()->getOp($op); |
|
243
|
4 |
|
} else if ($operation->isPush()) { |
|
244
|
4 |
|
return $operation->getData()->getHex(); |
|
245
|
|
|
} else { |
|
246
|
2 |
|
return $this->script->getOpcodes()->getOp($operation->getOp()); |
|
247
|
|
|
} |
|
248
|
6 |
|
}, |
|
249
|
|
|
$this->decode() |
|
250
|
|
|
)); |
|
251
|
|
|
} |
|
252
|
|
|
} |
|
253
|
|
|
|