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