1 | <?php |
||
16 | class ScriptCreator |
||
17 | { |
||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | private $script = ''; |
||
22 | |||
23 | /** |
||
24 | * @var Opcodes |
||
25 | */ |
||
26 | private $opcodes; |
||
27 | |||
28 | /** |
||
29 | * @var Math |
||
30 | */ |
||
31 | private $math; |
||
32 | |||
33 | /** |
||
34 | * @param Math $math |
||
35 | * @param Opcodes $opcodes |
||
36 | * @param BufferInterface|null $buffer |
||
37 | */ |
||
38 | 772 | public function __construct(Math $math, Opcodes $opcodes, BufferInterface $buffer = null) |
|
47 | |||
48 | /** |
||
49 | * @param int[]|\BitWasp\Bitcoin\Script\Interpreter\Number[]|BufferInterface[] $sequence |
||
50 | * @return $this |
||
51 | */ |
||
52 | 192 | public function sequence(array $sequence) |
|
53 | { |
||
54 | 192 | $new = new self($this->math, $this->opcodes, null); |
|
55 | 192 | foreach ($sequence as $operation) { |
|
56 | 192 | if (is_int($operation)) { |
|
57 | 184 | if (!$this->opcodes->offsetExists($operation)) { |
|
58 | throw new \RuntimeException('Unknown opcode'); |
||
59 | } |
||
60 | |||
61 | 184 | $new->script .= chr($operation); |
|
62 | 162 | } elseif ($operation instanceof Number) { |
|
63 | $new->push($operation->getBuffer()); |
||
64 | 144 | } elseif ($operation instanceof BufferInterface) { |
|
65 | 192 | $new->push($operation); |
|
66 | 144 | } else { |
|
67 | 48 | throw new \RuntimeException('Input was neither an opcode or BufferInterfacecc'); |
|
68 | } |
||
69 | 144 | } |
|
70 | |||
71 | 192 | $this->concat($new->getScript()); |
|
72 | 192 | return $this; |
|
73 | } |
||
74 | |||
75 | /** |
||
76 | * Add an opcode to the script |
||
77 | * |
||
78 | * @param string $name |
||
79 | * @return $this |
||
80 | */ |
||
81 | 116 | public function op($name) |
|
87 | |||
88 | /** |
||
89 | * Push data into the stack. |
||
90 | * |
||
91 | * @param $data |
||
92 | * @return $this |
||
93 | * @throws \Exception |
||
94 | */ |
||
95 | 252 | public function push(BufferInterface $data) |
|
125 | |||
126 | /** |
||
127 | * @param int $n |
||
128 | * @return $this |
||
129 | */ |
||
130 | 52 | public function int($n) |
|
142 | |||
143 | /** |
||
144 | * @param Serializable $object |
||
145 | * @return $this |
||
146 | */ |
||
147 | public function pushSerializable(Serializable $object) |
||
152 | |||
153 | /** |
||
154 | * @param Serializable[] $serializable |
||
155 | * @return $this |
||
156 | */ |
||
157 | public function pushSerializableArray(array $serializable) |
||
165 | |||
166 | /** |
||
167 | * @param ScriptInterface $script |
||
168 | * @return $this |
||
169 | */ |
||
170 | 192 | public function concat(ScriptInterface $script) |
|
175 | |||
176 | /** |
||
177 | * @return ScriptInterface |
||
178 | */ |
||
179 | 768 | public function getScript() |
|
183 | } |
||
184 |